Legacy Blur for Post Processing Stack v2

Hey everyone,

I couldn’t find much info on a blur effect for the new stack so I’ve converted the old ‘Blur (Optimized)’ image effect over to work with the new Post Processing Stack v2. Just thought I’d post it here as I’ve seen a few requests for something similar :slight_smile:

Cheers.

3183801–242914–Blur.shader (5.96 KB)
3183801–243920–Blur.cs (3.28 KB)

10 Likes

This is neat. How do you define the order in the stack?

Nice!

Would like to know about this too, creating custom stack seems too abstract for me atm

And how do I reuse pass, or the result of a blur, I’d like to know that.

Docs seem super clear on this tbh Home · Unity-Technologies/PostProcessing Wiki · GitHub

You did look at them? You get 3 places for now, but more may come in future. If that’s not acceptable you will need to clone the entire post effect and modify it extensively. From what I can see this is hardly necessary.

I thank you for the shader that I am using but I have some questions in case someone can help. After starting to use it we noticed a reproducible crash happening quite often. It’s a player crash, which means there is something wrong in the Unity code, but it’s related to this shader.

The crash happens inside the unityplayer.dll in the ShaderLab::TexEnv::SetTextureInfo function called by the SetupLightShaderProperties from inside ForwardRenderingLoopJob.

I don’t have the complete stack with my right now, but it should give a good idea.

The problem is that the shader is corrupted, the memory pointed by the shader is obviously rubbish. The editor actually manages to complain as well before the crash, giving this:

ArgumentException: Invalid shader ()
at UnityEngine.Rendering.PostProcessing.PropertySheetFactory.Get (UnityEngine.Shader shader) [0x00018] in C:\Users\rgent\AppData\Local\Unity\cache\packages[packages.unity.com](http://packages.unity.com/)\com.unity.postprocessing@2.0.7-preview\PostProcessing\Runtime\Utils\PropertySheetFactory.cs:30
at UnityEngine.Rendering.PostProcessing.BlurEffect.Render (UnityEngine.Rendering.PostProcessing.PostProcessRenderContext context) [0x00085] in C:\Users\rgent\Perforce\rgent_RGent_Release_1.53\CardLifeUnity\Assets\Standard Assets\Blur.cs:50
at UnityEngine.Rendering.PostProcessing.PostProcessLayer.RenderList (System.Collections.Generic.List`1 list, UnityEngine.Rendering.PostProcessing.PostProcessRenderContext context, System.String marker) [0x000ab] in C:\Users\rgent\AppData\Local\Unity\cache\packages[packages.unity.com](http://packages.unity.com/)\com.unity.postprocessing@2.0.7-preview\PostProcessing\Runtime\PostProcessLayer.cs:752
at UnityEngine.Rendering.PostProcessing.PostProcessLayer.RenderInjectionPoint (PostProcessEvent evt, UnityEngine.Rendering.PostProcessing.PostProcessRenderContext context, System.String marker, Int32 releaseTargetAfterUse) [0x00049] in C:\Users\rgent\AppData\Local\Unity\cache\packages[packages.unity.com](http://packages.unity.com/)\com.unity.postprocessing@2.0.7-preview\PostProcessing\Runtime\PostProcessLayer.cs:720
at UnityEngine.Rendering.PostProcessing.PostProcessLayer.Render (UnityEngine.Rendering.PostProcessing.PostProcessRenderContext context) [0x001dd] in C:\Users\rgent\AppData\Local\Unity\cache\packages[packages.unity.com](http://packages.unity.com/)\com.unity.postprocessing@2.0.7-preview\PostProcessing\Runtime\PostProcessLayer.cs:695
at UnityEngine.Rendering.PostProcessing.PostProcessLayer.BuildCommandBuffers () [0x003ad] in C:\Users\rgent\AppData\Local\Unity\cache\packages[packages.unity.com](http://packages.unity.com/)\com.unity.postprocessing@2.0.7-preview\PostProcessing\Runtime\PostProcessLayer.cs:437
at UnityEngine.Rendering.PostProcessing.PostProcessLayer.OnPreCull () [0x00096] in C:\Users\rgent\AppData\Local\Unity\cache\packages[packages.unity.com](http://packages.unity.com/)\com.unity.postprocessing@2.0.7-preview\PostProcessing\Runtime\PostProcessLayer.cs:294

Now I am 95% sure that this is due to the Shader.Find() failing to retrieve the right shader. I can still be wrong, I am investigating, but meanwhile I am asking this:

The PostProcessEffectRenderer Render method is not only called every frame, it’s even called when the editor is paused (why?). So

a) Shader.Find every frame? Is it wise?
b) why building a command buffer every frame?

1 Like

Anyhow, I just remember(although not tested yet) that the shader is not in the resource folder, so very likely that;s the problem, but my previous observations are still valid. Furthermore Unity should really warn about this mistake and not crash.

1 Like

Am I only one who has this issue?
I’m not really into shaders, but its not working -_-

Shader error in 'Hidden/Legacy/Blur': failed to open source file: '../../PostProcessing/Shaders/StdLib.hlsl' at line 6 (on d3d11)

Compiling Vertex program
Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP UNITY_COLORSPACE_GAMMA UNITY_LIGHT_PROBE_PROXY_VOLUME UNITY_LIGHTMAP_RGBM_ENCODING

Maybe code became legacy after some time? Or am I doing something wrong?

Target platform: WebGL

@vmp1r3
I had the same issue in 2018.2.10f1 with version 2.0.17-preview of the post-processing stack. Changing the line
#include **"../../PostProcessing/Shaders/StdLib.hlsl"**
to:
#include "PostProcessing/Shaders/StdLib.hlsl"
fixed it.

If that doesn’t work, try
**#include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"**
instead.

4 Likes

It works for me ! I tried first to move the files inside the package cache, but I got an error talking about missing meta files due to read only.

btw I wanted to have the HDR support for the blur (to exceed 0-1 values), to have that you only need to add “RenderTextureFormat.DefaultHDR” on every “GetTemporaryRT” call.

How did you solve the problem later?

No, but I found blur shader for UI element somewhere, I’m using it instead of this one.
Also check custom solutions at Asset Store.

could you share that blur shader ??

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "UI/Blur"
{
Properties
{
_Radius("Radius", Range(1, 255)) = 1
}

Category
{
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Opaque" }

SubShader
{
GrabPass
{
Tags{ "LightMode" = "Always" }
}

Pass
{
Tags{ "LightMode" = "Always" }

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"

struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord: TEXCOORD0;
};

struct v2f
{
float4 vertex : POSITION;
float4 uvgrab : TEXCOORD0;
};

v2f vert(appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
o.uvgrab.zw = o.vertex.zw;
return o;
}

sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
float _Radius;

half4 frag(v2f i) : COLOR
{
half4 sum = half4(0,0,0,0);

#define GRABXYPIXEL(kernelx, kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely, i.uvgrab.z, i.uvgrab.w)))

sum += GRABXYPIXEL(0.0, 0.0);
int measurments = 1;

for (float range = 0.1f; range <= _Radius; range += 0.1f)
{
sum += GRABXYPIXEL(range, range);
sum += GRABXYPIXEL(range, -range);
sum += GRABXYPIXEL(-range, range);
sum += GRABXYPIXEL(-range, -range);
measurments += 4;
}

return sum / measurments;
}
ENDCG
}
GrabPass
{
Tags{ "LightMode" = "Always" }
}

Pass
{
Tags{ "LightMode" = "Always" }

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"

struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord: TEXCOORD0;
};

struct v2f
{
float4 vertex : POSITION;
float4 uvgrab : TEXCOORD0;
};

v2f vert(appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
o.uvgrab.zw = o.vertex.zw;
return o;
}

sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
float _Radius;

half4 frag(v2f i) : COLOR
{

half4 sum = half4(0,0,0,0);
float radius = 1.41421356237 * _Radius;

#define GRABXYPIXEL(kernelx, kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely, i.uvgrab.z, i.uvgrab.w)))

sum += GRABXYPIXEL(0.0, 0.0);
int measurments = 1;

for (float range = 1.41421356237f; range <= radius * 1.41; range += 1.41421356237f)
{
sum += GRABXYPIXEL(range, 0);
sum += GRABXYPIXEL(-range, 0);
sum += GRABXYPIXEL(0, range);
sum += GRABXYPIXEL(0, -range);
measurments += 4;
}

return sum / measurments;
}
ENDCG
}
}
}
}

Future here, you don’t need to change the shader, just replace this line in the legacy blur shader:

#include "PostProcessing/Shaders/StdLib.hlsl

with this line:

#include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl

2 Likes

So cool! You are the best!