UberPost.shader 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. Shader "Hidden/Universal Render Pipeline/UberPost"
  2. {
  3. HLSLINCLUDE
  4. #pragma multi_compile_local _ _DISTORTION
  5. #pragma multi_compile_local _ _CHROMATIC_ABERRATION
  6. #pragma multi_compile_local _ _BLOOM_LQ _BLOOM_HQ _BLOOM_LQ_DIRT _BLOOM_HQ_DIRT
  7. #pragma multi_compile_local _ _HDR_GRADING _TONEMAP_ACES _TONEMAP_NEUTRAL
  8. #pragma multi_compile_local _ _FILM_GRAIN
  9. #pragma multi_compile_local _ _DITHERING
  10. #pragma multi_compile_local _ _LINEAR_TO_SRGB_CONVERSION
  11. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  12. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Filtering.hlsl"
  13. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  14. #include "Packages/com.unity.render-pipelines.universal/Shaders/PostProcessing/Common.hlsl"
  15. // Hardcoded dependencies to reduce the number of variants
  16. #if _BLOOM_LQ || _BLOOM_HQ || _BLOOM_LQ_DIRT || _BLOOM_HQ_DIRT
  17. #define BLOOM
  18. #if _BLOOM_LQ_DIRT || _BLOOM_HQ_DIRT
  19. #define BLOOM_DIRT
  20. #endif
  21. #endif
  22. TEXTURE2D_X(_BlitTex);
  23. TEXTURE2D_X(_Bloom_Texture);
  24. TEXTURE2D(_LensDirt_Texture);
  25. TEXTURE2D(_Grain_Texture);
  26. TEXTURE2D(_InternalLut);
  27. TEXTURE2D(_UserLut);
  28. TEXTURE2D(_BlueNoise_Texture);
  29. float4 _Lut_Params;
  30. float4 _UserLut_Params;
  31. float4 _Bloom_Params;
  32. float _Bloom_RGBM;
  33. float4 _LensDirt_Params;
  34. float _LensDirt_Intensity;
  35. float4 _Distortion_Params1;
  36. float4 _Distortion_Params2;
  37. float _Chroma_Params;
  38. half4 _Vignette_Params1;
  39. float4 _Vignette_Params2;
  40. float2 _Grain_Params;
  41. float4 _Grain_TilingParams;
  42. float4 _Bloom_Texture_TexelSize;
  43. float4 _Dithering_Params;
  44. #define DistCenter _Distortion_Params1.xy
  45. #define DistAxis _Distortion_Params1.zw
  46. #define DistTheta _Distortion_Params2.x
  47. #define DistSigma _Distortion_Params2.y
  48. #define DistScale _Distortion_Params2.z
  49. #define DistIntensity _Distortion_Params2.w
  50. #define ChromaAmount _Chroma_Params.x
  51. #define BloomIntensity _Bloom_Params.x
  52. #define BloomTint _Bloom_Params.yzw
  53. #define BloomRGBM _Bloom_RGBM.x
  54. #define LensDirtScale _LensDirt_Params.xy
  55. #define LensDirtOffset _LensDirt_Params.zw
  56. #define LensDirtIntensity _LensDirt_Intensity.x
  57. #define VignetteColor _Vignette_Params1.xyz
  58. #define VignetteCenter _Vignette_Params2.xy
  59. #define VignetteIntensity _Vignette_Params2.z
  60. #define VignetteSmoothness _Vignette_Params2.w
  61. #define VignetteRoundness _Vignette_Params1.w
  62. #define LutParams _Lut_Params.xyz
  63. #define PostExposure _Lut_Params.w
  64. #define UserLutParams _UserLut_Params.xyz
  65. #define UserLutContribution _UserLut_Params.w
  66. #define GrainIntensity _Grain_Params.x
  67. #define GrainResponse _Grain_Params.y
  68. #define GrainScale _Grain_TilingParams.xy
  69. #define GrainOffset _Grain_TilingParams.zw
  70. #define DitheringScale _Dithering_Params.xy
  71. #define DitheringOffset _Dithering_Params.zw
  72. float2 DistortUV(float2 uv)
  73. {
  74. // Note: this variant should never be set with XR
  75. #if _DISTORTION
  76. {
  77. uv = (uv - 0.5) * DistScale + 0.5;
  78. float2 ruv = DistAxis * (uv - 0.5 - DistCenter);
  79. float ru = length(float2(ruv));
  80. UNITY_BRANCH
  81. if (DistIntensity > 0.0)
  82. {
  83. float wu = ru * DistTheta;
  84. ru = tan(wu) * (rcp(ru * DistSigma));
  85. uv = uv + ruv * (ru - 1.0);
  86. }
  87. else
  88. {
  89. ru = rcp(ru) * DistTheta * atan(ru * DistSigma);
  90. uv = uv + ruv * (ru - 1.0);
  91. }
  92. }
  93. #endif
  94. return uv;
  95. }
  96. half4 Frag(Varyings input) : SV_Target
  97. {
  98. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  99. float2 uv = UnityStereoTransformScreenSpaceTex(input.uv);
  100. float2 uvDistorted = DistortUV(uv);
  101. half3 color = (0.0).xxx;
  102. #if _CHROMATIC_ABERRATION
  103. {
  104. // Very fast version of chromatic aberration from HDRP using 3 samples and hardcoded
  105. // spectral lut. Performs significantly better on lower end GPUs.
  106. float2 coords = 2.0 * uv - 1.0;
  107. float2 end = uv - coords * dot(coords, coords) * ChromaAmount;
  108. float2 delta = (end - uv) / 3.0;
  109. half r = SAMPLE_TEXTURE2D_X(_BlitTex, sampler_LinearClamp, uvDistorted ).x;
  110. half g = SAMPLE_TEXTURE2D_X(_BlitTex, sampler_LinearClamp, DistortUV(delta + uv) ).y;
  111. half b = SAMPLE_TEXTURE2D_X(_BlitTex, sampler_LinearClamp, DistortUV(delta * 2.0 + uv)).z;
  112. color = half3(r, g, b);
  113. }
  114. #else
  115. {
  116. color = SAMPLE_TEXTURE2D_X(_BlitTex, sampler_LinearClamp, uvDistorted).xyz;
  117. }
  118. #endif
  119. // Gamma space... Just do the rest of Uber in linear and convert back to sRGB at the end
  120. #if UNITY_COLORSPACE_GAMMA
  121. {
  122. color = SRGBToLinear(color);
  123. }
  124. #endif
  125. #if defined(BLOOM)
  126. {
  127. #if _BLOOM_HQ && !defined(SHADER_API_GLES)
  128. half4 bloom = SampleTexture2DBicubic(TEXTURE2D_X_ARGS(_Bloom_Texture, sampler_LinearClamp), uvDistorted, _Bloom_Texture_TexelSize.zwxy, (1.0).xx, unity_StereoEyeIndex);
  129. #else
  130. half4 bloom = SAMPLE_TEXTURE2D_X(_Bloom_Texture, sampler_LinearClamp, uvDistorted);
  131. #endif
  132. #if UNITY_COLORSPACE_GAMMA
  133. bloom.xyz *= bloom.xyz; // γ to linear
  134. #endif
  135. UNITY_BRANCH
  136. if (BloomRGBM > 0)
  137. {
  138. bloom.xyz = DecodeRGBM(bloom);
  139. }
  140. bloom.xyz *= BloomIntensity;
  141. color += bloom.xyz * BloomTint;
  142. #if defined(BLOOM_DIRT)
  143. {
  144. // UVs for the dirt texture should be DistortUV(uv * DirtScale + DirtOffset) but
  145. // considering we use a cover-style scale on the dirt texture the difference
  146. // isn't massive so we chose to save a few ALUs here instead in case lens
  147. // distortion is active.
  148. half3 dirt = SAMPLE_TEXTURE2D(_LensDirt_Texture, sampler_LinearClamp, uvDistorted * LensDirtScale + LensDirtOffset).xyz;
  149. dirt *= LensDirtIntensity;
  150. color += dirt * bloom.xyz;
  151. }
  152. #endif
  153. }
  154. #endif
  155. // To save on variants we'll use an uniform branch for vignette. Lower end platforms
  156. // don't like these but if we're running Uber it means we're running more expensive
  157. // effects anyway. Lower-end devices would limit themselves to on-tile compatible effect
  158. // and thus this shouldn't too much of a problem (famous last words).
  159. UNITY_BRANCH
  160. if (VignetteIntensity > 0)
  161. {
  162. color = ApplyVignette(color, uvDistorted, VignetteCenter, VignetteIntensity, VignetteRoundness, VignetteSmoothness, VignetteColor);
  163. }
  164. // Color grading is always enabled when post-processing/uber is active
  165. {
  166. color = ApplyColorGrading(color, PostExposure, TEXTURE2D_ARGS(_InternalLut, sampler_LinearClamp), LutParams, TEXTURE2D_ARGS(_UserLut, sampler_LinearClamp), UserLutParams, UserLutContribution);
  167. }
  168. #if _FILM_GRAIN
  169. {
  170. color = ApplyGrain(color, uv, TEXTURE2D_ARGS(_Grain_Texture, sampler_LinearRepeat), GrainIntensity, GrainResponse, GrainScale, GrainOffset);
  171. }
  172. #endif
  173. // Back to sRGB
  174. #if UNITY_COLORSPACE_GAMMA || _LINEAR_TO_SRGB_CONVERSION
  175. {
  176. color = LinearToSRGB(color);
  177. }
  178. #endif
  179. #if _DITHERING
  180. {
  181. color = ApplyDithering(color, uv, TEXTURE2D_ARGS(_BlueNoise_Texture, sampler_PointRepeat), DitheringScale, DitheringOffset);
  182. }
  183. #endif
  184. return half4(color, 1.0);
  185. }
  186. ENDHLSL
  187. SubShader
  188. {
  189. Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline"}
  190. LOD 100
  191. ZTest Always ZWrite Off Cull Off
  192. Pass
  193. {
  194. Name "UberPost"
  195. HLSLPROGRAM
  196. #pragma vertex Vert
  197. #pragma fragment Frag
  198. ENDHLSL
  199. }
  200. }
  201. }