Skip to content

Commit

Permalink
Add mipmap support (#36)
Browse files Browse the repository at this point in the history
* Add mipmap support and better blur controls

* Update package.json
  • Loading branch information
lukakldiashvili authored Feb 26, 2025
1 parent cefacec commit 4c63053
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 28 deletions.
2 changes: 2 additions & 0 deletions Runtime/BlurConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public struct BlurConfig

public int Width;
public int Height;

public bool EnableMipMaps;
}
}
6 changes: 4 additions & 2 deletions Runtime/BlurPasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static void KawaseExecutePass<TPass, T>(TPass data, T cmd) where TPass :
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static float CalculateOffset(BlurConfig blurConfig, int iteration)
{
return (blurConfig.Offset + iteration * blurConfig.Scale) / blurConfig.Downsample * blurConfig.Intensity;
return (blurConfig.Offset + iteration * blurConfig.Scale) / blurConfig.Downsample;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -55,7 +55,9 @@ private static void BlitTexture<T>(T cmd, Texture sourceHandle, Texture destinat
mpb.SetVector(Constants.BlurParamsId, new Vector4(blurConfig.Intensity, blurConfig.Scale, blurConfig.Downsample, blurConfig.Offset));
mpb.SetTexture(Constants.BlitTextureId, sourceHandle);

// TODO: Implement mipLevel and depthSlice support, if relevant
// TODO: add a lookup for getting mipmap level from offset value
mpb.SetFloat(Constants.BlitMipLevelId, blurConfig.EnableMipMaps ? Mathf.Log(offset, 2) : 0);

cmd.SetRenderTarget(destinationHandle, 0, CubemapFace.Unknown, 0);
cmd.DrawProcedural(Matrix4x4.identity, blurConfig.Material, 0, MeshTopology.Quads, 4, 1, mpb);
}
Expand Down
1 change: 1 addition & 0 deletions Runtime/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static class Constants
public static readonly int BlurParamsId = Shader.PropertyToID("_BlurParams");
public static readonly int BlitTextureId = Shader.PropertyToID("_BlitTexture");
public static readonly int BlitScaleBiasId = Shader.PropertyToID("_BlitScaleBias");
public static readonly int BlitMipLevelId = Shader.PropertyToID("_BlitMipLevel");
public static readonly int GlobalFullScreenBlurTextureId = Shader.PropertyToID("_GlobalUniversalBlurTexture");
}
}
24 changes: 17 additions & 7 deletions Runtime/UniversalBlurFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ namespace Unified.UniversalBlur.Runtime
public class UniversalBlurFeature : ScriptableRendererFeature
{
[Header("Blur Settings")]
[Range(1, 8)] [SerializeField] private int iterations = 4;

[Space]

[Range(0f, 1f)] [SerializeField] public float intensity = 1.0f;
[Range(1, 12)] [SerializeField] private int iterations = 4;
[Range(1f, 10f)] [SerializeField] private float downsample = 2.0f;

[Tooltip("Enable mipmaps for more efficient blur")]
[SerializeField] private bool enableMipMaps = true;
// [Range(0f, 10f)]
[SerializeField] private float scale = 1f;
// [Range(0f, 10f)]
[SerializeField] private float offset = 2f;
[SerializeField] private float offset = 1f;

[Space]

Expand All @@ -33,6 +32,8 @@ public class UniversalBlurFeature : ScriptableRendererFeature
"\n\nOther: BeforeRenderingTransparents (will hide transparents)")]
[SerializeField] private RenderPassEvent injectionPoint = RenderPassEvent.AfterRenderingPostProcessing;

private float _intensity = 1.0f;

[SerializeField]
[HideInInspector]
[Reload("Shaders/Blur.shader")]
Expand All @@ -41,6 +42,13 @@ public class UniversalBlurFeature : ScriptableRendererFeature
private Material _material;
private UniversalBlurPass _blurPass;
private float _renderScale;

// Avoid changing intensity value, but useful for transitions
public float Intensity
{
get => _intensity;
set => _intensity = Mathf.Clamp(value, 0f, 1f);
}

/// <inheritdoc/>
public override void Create()
Expand Down Expand Up @@ -111,11 +119,13 @@ private BlurConfig GetBlurConfig(in RenderingData renderingData)
Height = height,

Material = _material,
Intensity = intensity,
Intensity = _intensity,
Downsample = downsample,
Offset = offset,
BlurType = blurType,
Iterations = iterations,

EnableMipMaps = enableMipMaps
};
}

Expand Down
30 changes: 17 additions & 13 deletions Runtime/UniversalBlurPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,20 @@ public void DrawDefaultTexture()
Shader.SetGlobalTexture(Constants.GlobalFullScreenBlurTextureId, Texture2D.linearGrayTexture);
}

private RenderTextureDescriptor GetDescriptor() =>
new(_blurConfig.Width, _blurConfig.Height, GraphicsFormat.B10G11R11_UFloatPack32, 0)
{
useMipMap = _blurConfig.EnableMipMaps,
autoGenerateMips = _blurConfig.EnableMipMaps
};

public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
var cmd = CommandBufferPool.Get();
var descriptor = new RenderTextureDescriptor(_blurConfig.Width, _blurConfig.Height, GraphicsFormat.B10G11R11_UFloatPack32, 0);
#if UNITY_6000_0_OR_NEWER

var descriptor = GetDescriptor();

#if UNITY_6000_0_OR_NEWER
RenderingUtils.ReAllocateHandleIfNeeded(ref _sourceRT, descriptor, FilterMode.Bilinear, TextureWrapMode.Clamp, name: k_BlurTextureSourceName);
RenderingUtils.ReAllocateHandleIfNeeded(ref _destinationRT, descriptor, FilterMode.Bilinear, TextureWrapMode.Clamp, name: k_BlurTextureDestinationName);
#else
Expand Down Expand Up @@ -96,15 +103,12 @@ public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer

var cameraColorSource = resourceData.activeColorTexture;

var rtDescriptor = renderGraph.GetTextureDesc(cameraColorSource);
rtDescriptor.width = _blurConfig.Width;
rtDescriptor.height = _blurConfig.Height;
rtDescriptor.clearBuffer = false;

rtDescriptor.name = k_BlurTextureSourceName;
TextureHandle source = renderGraph.CreateTexture(rtDescriptor);
rtDescriptor.name = k_BlurTextureDestinationName;
TextureHandle destination = renderGraph.CreateTexture(rtDescriptor);
var descriptor = new TextureDesc(GetDescriptor());

descriptor.name = k_BlurTextureSourceName;
TextureHandle source = renderGraph.CreateTexture(descriptor);
descriptor.name = k_BlurTextureDestinationName;
TextureHandle destination = renderGraph.CreateTexture(descriptor);

using (var builder = renderGraph.AddUnsafePass<RenderGraphPassData>(k_PassName, out var passData, _profilingSampler))
{
Expand Down
8 changes: 3 additions & 5 deletions Shaders/Common.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ SAMPLER(sampler_BlitTexture);
// Function defines
#define SAMPLE(textureName, coord2) SAMPLE_TEXTURE2D_LOD(textureName, sampler_LinearClamp, coord2, _BlitMipLevel);

#define SAMPLE_BASEMAP(uv) half4(SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_BlitTexture, UnityStereoTransformScreenSpaceTex(uv)));
#define SAMPLE_BASEMAP_R(uv) half(SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_BlitTexture, UnityStereoTransformScreenSpaceTex(uv)).r);

#define SAMPLE_BASEMAP(uv) half4(SAMPLE_TEXTURE2D_LOD(_BlitTexture, sampler_LinearClamp, UnityStereoTransformScreenSpaceTex(uv), _BlitMipLevel));

// Constants
static const half HALF_POINT_ONE = half(0.1);
Expand Down Expand Up @@ -186,7 +184,7 @@ half4 KawaseBlur(Varyings input) : SV_Target
half2 uv = input.texcoord;
half2 texelSize = TEXEL_SIZE.xy * rcp(DOWNSAMPLE);

half4 col = KawaseBlurFilter(uv, texelSize, ITERATION);

half4 col = KawaseBlurFilter(uv, texelSize * INTENSITY, ITERATION * OFFSET);
return col;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "com.unify.unified-universal-blur",
"displayName": "Unified Blur",
"description": "Unified Blur enables you to use translucent blur effect on your canvas.",
"version": "0.7.1",
"version": "0.7.2",
"unity": "2022.3",
"license": "MIT",
"repository": {
Expand Down

0 comments on commit 4c63053

Please sign in to comment.