FinalPost.shader 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. Shader "Hidden/Universal Render Pipeline/FinalPost"
  2. {
  3. HLSLINCLUDE
  4. #pragma multi_compile_local _ _FXAA
  5. #pragma multi_compile_local _ _FILM_GRAIN
  6. #pragma multi_compile_local _ _DITHERING
  7. #pragma multi_compile_local _ _LINEAR_TO_SRGB_CONVERSION
  8. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  9. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  10. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  11. #include "Packages/com.unity.render-pipelines.universal/Shaders/PostProcessing/Common.hlsl"
  12. TEXTURE2D_X(_BlitTex);
  13. TEXTURE2D(_Grain_Texture);
  14. TEXTURE2D(_BlueNoise_Texture);
  15. float4 _BlitTex_TexelSize;
  16. float2 _Grain_Params;
  17. float4 _Grain_TilingParams;
  18. float4 _Dithering_Params;
  19. #define GrainIntensity _Grain_Params.x
  20. #define GrainResponse _Grain_Params.y
  21. #define GrainScale _Grain_TilingParams.xy
  22. #define GrainOffset _Grain_TilingParams.zw
  23. #define DitheringScale _Dithering_Params.xy
  24. #define DitheringOffset _Dithering_Params.zw
  25. #define FXAA_SPAN_MAX (8.0)
  26. #define FXAA_REDUCE_MUL (1.0 / 8.0)
  27. #define FXAA_REDUCE_MIN (1.0 / 128.0)
  28. half3 Fetch(float2 coords, float2 offset)
  29. {
  30. float2 uv = coords + offset;
  31. return SAMPLE_TEXTURE2D_X(_BlitTex, sampler_LinearClamp, uv).xyz;
  32. }
  33. half3 Load(int2 icoords, int idx, int idy)
  34. {
  35. #if SHADER_API_GLES
  36. float2 uv = (icoords + int2(idx, idy)) * _BlitTex_TexelSize.xy;
  37. return SAMPLE_TEXTURE2D_X(_BlitTex, sampler_LinearClamp, uv).xyz;
  38. #else
  39. return LOAD_TEXTURE2D_X(_BlitTex, clamp(icoords + int2(idx, idy), 0, _BlitTex_TexelSize.zw - 1.0)).xyz;
  40. #endif
  41. }
  42. half4 Frag(Varyings input) : SV_Target
  43. {
  44. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  45. float2 uv = UnityStereoTransformScreenSpaceTex(input.uv);
  46. float2 positionNDC = uv;
  47. int2 positionSS = uv * _BlitTex_TexelSize.zw;
  48. half3 color = Load(positionSS, 0, 0).xyz;
  49. #if _FXAA
  50. {
  51. // Edge detection
  52. half3 rgbNW = Load(positionSS, -1, -1);
  53. half3 rgbNE = Load(positionSS, 1, -1);
  54. half3 rgbSW = Load(positionSS, -1, 1);
  55. half3 rgbSE = Load(positionSS, 1, 1);
  56. rgbNW = saturate(rgbNW);
  57. rgbNE = saturate(rgbNE);
  58. rgbSW = saturate(rgbSW);
  59. rgbSE = saturate(rgbSE);
  60. color = saturate(color);
  61. half lumaNW = Luminance(rgbNW);
  62. half lumaNE = Luminance(rgbNE);
  63. half lumaSW = Luminance(rgbSW);
  64. half lumaSE = Luminance(rgbSE);
  65. half lumaM = Luminance(color);
  66. float2 dir;
  67. dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
  68. dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
  69. half lumaSum = lumaNW + lumaNE + lumaSW + lumaSE;
  70. float dirReduce = max(lumaSum * (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);
  71. float rcpDirMin = rcp(min(abs(dir.x), abs(dir.y)) + dirReduce);
  72. dir = min((FXAA_SPAN_MAX).xx, max((-FXAA_SPAN_MAX).xx, dir * rcpDirMin)) * _BlitTex_TexelSize.xy;
  73. // Blur
  74. half3 rgb03 = Fetch(positionNDC, dir * (0.0 / 3.0 - 0.5));
  75. half3 rgb13 = Fetch(positionNDC, dir * (1.0 / 3.0 - 0.5));
  76. half3 rgb23 = Fetch(positionNDC, dir * (2.0 / 3.0 - 0.5));
  77. half3 rgb33 = Fetch(positionNDC, dir * (3.0 / 3.0 - 0.5));
  78. rgb03 = saturate(rgb03);
  79. rgb13 = saturate(rgb13);
  80. rgb23 = saturate(rgb23);
  81. rgb33 = saturate(rgb33);
  82. half3 rgbA = 0.5 * (rgb13 + rgb23);
  83. half3 rgbB = rgbA * 0.5 + 0.25 * (rgb03 + rgb33);
  84. half lumaB = Luminance(rgbB);
  85. half lumaMin = Min3(lumaM, lumaNW, Min3(lumaNE, lumaSW, lumaSE));
  86. half lumaMax = Max3(lumaM, lumaNW, Max3(lumaNE, lumaSW, lumaSE));
  87. color = ((lumaB < lumaMin) || (lumaB > lumaMax)) ? rgbA : rgbB;
  88. }
  89. #endif
  90. #if _FILM_GRAIN
  91. {
  92. color = ApplyGrain(color, positionNDC, TEXTURE2D_ARGS(_Grain_Texture, sampler_LinearRepeat), GrainIntensity, GrainResponse, GrainScale, GrainOffset);
  93. }
  94. #endif
  95. #if _LINEAR_TO_SRGB_CONVERSION
  96. {
  97. color = LinearToSRGB(color);
  98. }
  99. #endif
  100. #if _DITHERING
  101. {
  102. color = ApplyDithering(color, positionNDC, TEXTURE2D_ARGS(_BlueNoise_Texture, sampler_PointRepeat), DitheringScale, DitheringOffset);
  103. }
  104. #endif
  105. return half4(color, 1.0);
  106. }
  107. ENDHLSL
  108. SubShader
  109. {
  110. Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline"}
  111. LOD 100
  112. ZTest Always ZWrite Off Cull Off
  113. Pass
  114. {
  115. Name "FinalPost"
  116. HLSLPROGRAM
  117. #pragma vertex Vert
  118. #pragma fragment Frag
  119. ENDHLSL
  120. }
  121. }
  122. }