Unity-NorthStar / data /Packages /com.unity.render-pipelines.universal /Shaders /PostProcessing /GaussianDepthOfField.shader
| Shader "Hidden/Universal Render Pipeline/GaussianDepthOfField" | |
| { | |
| HLSLINCLUDE | |
| TEXTURE2D_X(_ColorTexture); | |
| TEXTURE2D_X(_FullCoCTexture); | |
| TEXTURE2D_X(_HalfCoCTexture); | |
| float4 _SourceSize; | |
| float4 _DownSampleScaleFactor; | |
| float3 _CoCParams; | |
| // Offsets & coeffs for optimized separable bilinear 3-tap gaussian (5-tap equivalent) | |
| const static int kTapCount = 3; | |
| const static float kOffsets[] = { | |
| -1.33333333, | |
| 0.00000000, | |
| 1.33333333 | |
| }; | |
| const static half kCoeffs[] = { | |
| 0.35294118, | |
| 0.29411765, | |
| 0.35294118 | |
| }; | |
| // Offsets & coeffs for optimized separable bilinear 5-tap gaussian (9-tap equivalent) | |
| const static int kTapCount = 5; | |
| const static float kOffsets[] = { | |
| -3.23076923, | |
| -1.38461538, | |
| 0.00000000, | |
| 1.38461538, | |
| 3.23076923 | |
| }; | |
| const static half kCoeffs[] = { | |
| 0.07027027, | |
| 0.31621622, | |
| 0.22702703, | |
| 0.31621622, | |
| 0.07027027 | |
| }; | |
| half FragCoC(Varyings input) : SV_Target | |
| { | |
| UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); | |
| float2 uv = UnityStereoTransformScreenSpaceTex(input.texcoord); | |
| float depth = LOAD_TEXTURE2D_X(_CameraDepthTexture, _SourceSize.xy * uv).x; | |
| depth = LinearEyeDepth(depth, _ZBufferParams); | |
| half coc = (depth - FarStart) / (FarEnd - FarStart); | |
| return saturate(coc); | |
| } | |
| struct PrefilterOutput | |
| { | |
| half coc : SV_Target0; | |
| half4 color : SV_Target1; | |
| }; | |
| PrefilterOutput FragPrefilter(Varyings input) | |
| { | |
| UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); | |
| float2 uv = UnityStereoTransformScreenSpaceTex(input.texcoord); | |
| // Use a rotated grid to minimize artifacts coming from horizontal and vertical boundaries | |
| // "High Quality Antialiasing" [Lorach07] | |
| const int kCount = 5; | |
| const float2 kTaps[] = { | |
| float2( 0.0, 0.0), | |
| float2( 0.9, -0.4), | |
| float2(-0.9, 0.4), | |
| float2( 0.4, 0.9), | |
| float2(-0.4, -0.9) | |
| }; | |
| half4 colorAcc = 0.0; | |
| half farCoCAcc = 0.0; | |
| UNITY_UNROLL | |
| for (int i = 0; i < kCount; i++) | |
| { | |
| float2 tapCoord = _SourceSize.zw * kTaps[i] + uv; | |
| half4 tapColor = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, tapCoord); | |
| half coc = SAMPLE_TEXTURE2D_X(_FullCoCTexture, sampler_LinearClamp, tapCoord).x; | |
| // Pre-multiply CoC to reduce bleeding of background blur on focused areas | |
| colorAcc += tapColor * coc; | |
| farCoCAcc += coc; | |
| } | |
| half4 color = colorAcc * rcp(kCount); | |
| half farCoC = farCoCAcc * rcp(kCount); | |
| // Bilinear sampling the coc is technically incorrect but we're aiming for speed here | |
| half farCoC = SAMPLE_TEXTURE2D_X(_FullCoCTexture, sampler_LinearClamp, uv).x; | |
| // Fast bilinear downscale of the source target and pre-multiply the CoC to reduce | |
| // bleeding of background blur on focused areas | |
| half4 color = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, uv); | |
| color *= farCoC; | |
| PrefilterOutput o; | |
| o.coc = farCoC; | |
| o.color = color; | |
| o.color = half4(color.xyz, 0); | |
| return o; | |
| } | |
| half4 Blur(Varyings input, float2 dir, float premultiply) | |
| { | |
| UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); | |
| float2 uv = UnityStereoTransformScreenSpaceTex(input.texcoord); | |
| // Use the center CoC as radius | |
| int2 positionSS = int2(_SourceSize.xy * _DownSampleScaleFactor.xy * uv); | |
| half samp0CoC = LOAD_TEXTURE2D_X(_HalfCoCTexture, positionSS).x; | |
| float2 offset = _SourceSize.zw * _DownSampleScaleFactor.zw * dir * samp0CoC * MaxRadius; | |
| half4 acc = 0.0; | |
| half accAlpha = 0.0; | |
| UNITY_UNROLL | |
| for (int i = 0; i < kTapCount; i++) | |
| { | |
| float2 sampCoord = uv + kOffsets[i] * offset; | |
| half sampCoC = SAMPLE_TEXTURE2D_X(_HalfCoCTexture, sampler_LinearClamp, sampCoord).x; | |
| half4 sampColor = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, sampCoord); | |
| // Weight & pre-multiply to limit bleeding on the focused area | |
| half weight = saturate(1.0 - (samp0CoC - sampCoC)); | |
| acc += half4(sampColor.xyz, premultiply ? sampCoC : 1.0) * kCoeffs[i] * weight; | |
| accAlpha += sampColor.a * kCoeffs[i] * weight; | |
| } | |
| acc.xyz /= acc.w + 1e-4; // Zero-div guard | |
| accAlpha /= acc.w + 1e-4; // Zero-div guard | |
| return half4(acc.xyz, accAlpha); | |
| return half4(acc.xyz, 1.0); | |
| } | |
| half4 FragBlurH(Varyings input) : SV_Target | |
| { | |
| return Blur(input, float2(1.0, 0.0), 1.0); | |
| } | |
| half4 FragBlurV(Varyings input) : SV_Target | |
| { | |
| return Blur(input, float2(0.0, 1.0), 0.0); | |
| } | |
| half4 FragComposite(Varyings input) : SV_Target | |
| { | |
| UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); | |
| float2 uv = UnityStereoTransformScreenSpaceTex(input.texcoord); | |
| half4 baseColor = LOAD_TEXTURE2D_X(_BlitTexture, _SourceSize.xy * uv); | |
| half coc = LOAD_TEXTURE2D_X(_FullCoCTexture, _SourceSize.xy * uv).x; | |
| half4 farColor = SampleTexture2DBicubic(TEXTURE2D_X_ARGS(_ColorTexture, sampler_LinearClamp), uv, _SourceSize * _DownSampleScaleFactor, 1.0, unity_StereoEyeIndex); | |
| half4 farColor = SAMPLE_TEXTURE2D_X(_ColorTexture, sampler_LinearClamp, uv); | |
| half4 dstColor = 0.0; | |
| half dstAlpha = 1.0; | |
| UNITY_BRANCH | |
| if (coc > 0.0) | |
| { | |
| // Non-linear blend | |
| // "CryEngine 3 Graphics Gems" [Sousa13] | |
| half blend = sqrt(coc * TWO_PI); | |
| dstColor = farColor * saturate(blend); | |
| dstAlpha = saturate(1.0 - blend); | |
| } | |
| half4 outColor = dstColor + baseColor * dstAlpha; | |
| // Preserve the original value of the pixels with zero alpha | |
| outColor.rgb = outColor.a > 0 ? outColor.rgb : baseColor.rgb; | |
| return outColor; | |
| return half4(dstColor.rgb + baseColor.rgb * dstAlpha, 1.0); | |
| } | |
| ENDHLSL | |
| SubShader | |
| { | |
| Tags { "RenderPipeline" = "UniversalPipeline" } | |
| LOD 100 | |
| ZTest Always ZWrite Off Cull Off | |
| Pass | |
| { | |
| Name "Gaussian Depth Of Field CoC" | |
| HLSLPROGRAM | |
| ENDHLSL | |
| } | |
| Pass | |
| { | |
| Name "Gaussian Depth Of Field Prefilter" | |
| HLSLPROGRAM | |
| ENDHLSL | |
| } | |
| Pass | |
| { | |
| Name "Gaussian Depth Of Field Blur Horizontal" | |
| HLSLPROGRAM | |
| ENDHLSL | |
| } | |
| Pass | |
| { | |
| Name "Gaussian Depth Of Field Blur Vertical" | |
| HLSLPROGRAM | |
| ENDHLSL | |
| } | |
| Pass | |
| { | |
| Name "Gaussian Depth Of Field Composite" | |
| HLSLPROGRAM | |
| ENDHLSL | |
| } | |
| } | |
| } | |