No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UberPost.shader 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. Shader "Hidden/Universal Render Pipeline/UberPost"
  2. {
  3. HLSLINCLUDE
  4. #pragma multi_compile_local_fragment _ _DISTORTION
  5. #pragma multi_compile_local_fragment _ _CHROMATIC_ABERRATION
  6. #pragma multi_compile_local_fragment _ _BLOOM_LQ _BLOOM_HQ _BLOOM_LQ_DIRT _BLOOM_HQ_DIRT
  7. #pragma multi_compile_local_fragment _ _HDR_GRADING _TONEMAP_ACES _TONEMAP_NEUTRAL
  8. #pragma multi_compile_local_fragment _ _FILM_GRAIN
  9. #pragma multi_compile_local_fragment _ _DITHERING
  10. #pragma multi_compile_local_fragment _ _GAMMA_20 _LINEAR_TO_SRGB_CONVERSION
  11. #pragma multi_compile_local_fragment _ _USE_FAST_SRGB_LINEAR_CONVERSION
  12. #pragma multi_compile_local_fragment _ _ENABLE_ALPHA_OUTPUT
  13. #include_with_pragmas "Packages/com.unity.render-pipelines.core/ShaderLibrary/FoveatedRenderingKeywords.hlsl"
  14. #pragma multi_compile_fragment _ DEBUG_DISPLAY
  15. #pragma multi_compile_fragment _ SCREEN_COORD_OVERRIDE
  16. #pragma multi_compile_local_fragment _ HDR_INPUT HDR_ENCODING
  17. #ifdef HDR_ENCODING
  18. #define HDR_INPUT 1 // this should be defined when HDR_ENCODING is defined
  19. #endif
  20. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  21. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Filtering.hlsl"
  22. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/ScreenCoordOverride.hlsl"
  23. #if defined(HDR_ENCODING)
  24. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  25. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/HDROutput.hlsl"
  26. #endif
  27. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  28. #include "Packages/com.unity.render-pipelines.universal/Shaders/PostProcessing/Common.hlsl"
  29. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Debug/DebuggingFullscreen.hlsl"
  30. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/DynamicScalingClamping.hlsl"
  31. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/FoveatedRendering.hlsl"
  32. // Hardcoded dependencies to reduce the number of variants
  33. #if _BLOOM_LQ || _BLOOM_HQ || _BLOOM_LQ_DIRT || _BLOOM_HQ_DIRT
  34. #define BLOOM
  35. #if _BLOOM_LQ_DIRT || _BLOOM_HQ_DIRT
  36. #define BLOOM_DIRT
  37. #endif
  38. #endif
  39. TEXTURE2D_X(_Bloom_Texture);
  40. TEXTURE2D(_LensDirt_Texture);
  41. TEXTURE2D(_Grain_Texture);
  42. TEXTURE2D(_InternalLut);
  43. TEXTURE2D(_UserLut);
  44. TEXTURE2D(_BlueNoise_Texture);
  45. TEXTURE2D_X(_OverlayUITexture);
  46. float4 _BloomTexture_TexelSize;
  47. float4 _Lut_Params;
  48. float4 _UserLut_Params;
  49. float4 _Bloom_Params;
  50. float _Bloom_RGBM;
  51. float4 _LensDirt_Params;
  52. float _LensDirt_Intensity;
  53. float4 _Distortion_Params1;
  54. float4 _Distortion_Params2;
  55. float _Chroma_Params;
  56. half4 _Vignette_Params1;
  57. float4 _Vignette_Params2;
  58. #ifdef USING_STEREO_MATRICES
  59. float4 _Vignette_ParamsXR;
  60. #endif
  61. float2 _Grain_Params;
  62. float4 _Grain_TilingParams;
  63. float4 _Bloom_Texture_TexelSize;
  64. float4 _Dithering_Params;
  65. float4 _HDROutputLuminanceParams;
  66. #define DistCenter _Distortion_Params1.xy
  67. #define DistAxis _Distortion_Params1.zw
  68. #define DistTheta _Distortion_Params2.x
  69. #define DistSigma _Distortion_Params2.y
  70. #define DistScale _Distortion_Params2.z
  71. #define DistIntensity _Distortion_Params2.w
  72. #define ChromaAmount _Chroma_Params.x
  73. #define BloomIntensity _Bloom_Params.x
  74. #define BloomTint _Bloom_Params.yzw
  75. #define BloomRGBM _Bloom_RGBM.x
  76. #define LensDirtScale _LensDirt_Params.xy
  77. #define LensDirtOffset _LensDirt_Params.zw
  78. #define LensDirtIntensity _LensDirt_Intensity.x
  79. #define VignetteColor _Vignette_Params1.xyz
  80. #ifdef USING_STEREO_MATRICES
  81. #define VignetteCenterEye0 _Vignette_ParamsXR.xy
  82. #define VignetteCenterEye1 _Vignette_ParamsXR.zw
  83. #else
  84. #define VignetteCenter _Vignette_Params2.xy
  85. #endif
  86. #define VignetteIntensity _Vignette_Params2.z
  87. #define VignetteSmoothness _Vignette_Params2.w
  88. #define VignetteRoundness _Vignette_Params1.w
  89. #define LutParams _Lut_Params.xyz
  90. #define PostExposure _Lut_Params.w
  91. #define UserLutParams _UserLut_Params.xyz
  92. #define UserLutContribution _UserLut_Params.w
  93. #define GrainIntensity _Grain_Params.x
  94. #define GrainResponse _Grain_Params.y
  95. #define GrainScale _Grain_TilingParams.xy
  96. #define GrainOffset _Grain_TilingParams.zw
  97. #define DitheringScale _Dithering_Params.xy
  98. #define DitheringOffset _Dithering_Params.zw
  99. #define AlphaScale 1.0
  100. #define AlphaBias 0.0
  101. #define MinNits _HDROutputLuminanceParams.x
  102. #define MaxNits _HDROutputLuminanceParams.y
  103. #define PaperWhite _HDROutputLuminanceParams.z
  104. #define OneOverPaperWhite _HDROutputLuminanceParams.w
  105. float2 DistortUV(float2 uv)
  106. {
  107. // Note: this variant should never be set with XR
  108. #if _DISTORTION
  109. {
  110. uv = (uv - 0.5) * DistScale + 0.5;
  111. float2 ruv = DistAxis * (uv - 0.5 - DistCenter);
  112. float ru = length(float2(ruv));
  113. UNITY_BRANCH
  114. if (DistIntensity > 0.0)
  115. {
  116. float wu = ru * DistTheta;
  117. ru = tan(wu) * (rcp(ru * DistSigma));
  118. uv = uv + ruv * (ru - 1.0);
  119. }
  120. else
  121. {
  122. ru = rcp(ru) * DistTheta * atan(ru * DistSigma);
  123. uv = uv + ruv * (ru - 1.0);
  124. }
  125. }
  126. #endif
  127. return uv;
  128. }
  129. half4 FragUberPost(Varyings input) : SV_Target
  130. {
  131. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  132. float2 uv = SCREEN_COORD_APPLY_SCALEBIAS(UnityStereoTransformScreenSpaceTex(input.texcoord));
  133. float2 uvDistorted = DistortUV(uv);
  134. // NOTE: Hlsl specifies missing input.a to fill 1 (0 for .rgb).
  135. // InputColor is a "bottom" layer for alpha output.
  136. half4 inputColor = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, ClampUVForBilinear(SCREEN_COORD_REMOVE_SCALEBIAS(uvDistorted), _BlitTexture_TexelSize.xy));
  137. half3 color = inputColor.rgb;
  138. #if _CHROMATIC_ABERRATION
  139. {
  140. // Very fast version of chromatic aberration from HDRP using 3 samples and hardcoded
  141. // spectral lut. Performs significantly better on lower end GPUs.
  142. float2 coords = 2.0 * uv - 1.0;
  143. float2 end = uv - coords * dot(coords, coords) * ChromaAmount;
  144. float2 delta = (end - uv) / 3.0;
  145. half r = color.r;
  146. half g = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, ClampUVForBilinear(SCREEN_COORD_REMOVE_SCALEBIAS(DistortUV(delta + uv) ), _BlitTexture_TexelSize.xy)).y;
  147. half b = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, ClampUVForBilinear(SCREEN_COORD_REMOVE_SCALEBIAS(DistortUV(delta * 2.0 + uv)), _BlitTexture_TexelSize.xy)).z;
  148. color = half3(r, g, b);
  149. }
  150. #endif
  151. // Gamma space... Just do the rest of Uber in linear and convert back to sRGB at the end
  152. #if UNITY_COLORSPACE_GAMMA
  153. {
  154. color = GetSRGBToLinear(color);
  155. inputColor = GetSRGBToLinear(inputColor); // Deadcode removal if no effect on output color
  156. }
  157. #endif
  158. #if defined(BLOOM)
  159. {
  160. float2 uvBloom = ClampUVForBilinear(uvDistorted, _BloomTexture_TexelSize.xy);
  161. #if defined(SUPPORTS_FOVEATED_RENDERING_NON_UNIFORM_RASTER)
  162. UNITY_BRANCH if (_FOVEATED_RENDERING_NON_UNIFORM_RASTER)
  163. {
  164. uvBloom = RemapFoveatedRenderingNonUniformToLinear(uvBloom);
  165. }
  166. #endif
  167. #if _BLOOM_HQ
  168. half4 bloom = SampleTexture2DBicubic(TEXTURE2D_X_ARGS(_Bloom_Texture, sampler_LinearClamp), SCREEN_COORD_REMOVE_SCALEBIAS(uvBloom), _Bloom_Texture_TexelSize.zwxy, (1.0).xx, unity_StereoEyeIndex);
  169. #else
  170. half4 bloom = SAMPLE_TEXTURE2D_X(_Bloom_Texture, sampler_LinearClamp, SCREEN_COORD_REMOVE_SCALEBIAS(uvBloom));
  171. #endif
  172. #if UNITY_COLORSPACE_GAMMA
  173. bloom.xyz *= bloom.xyz; // γ to linear
  174. #endif
  175. UNITY_BRANCH
  176. if (BloomRGBM > 0)
  177. {
  178. bloom.xyz = DecodeRGBM(bloom);
  179. }
  180. bloom.xyz *= BloomIntensity;
  181. color += bloom.xyz * BloomTint;
  182. #if defined(BLOOM_DIRT)
  183. {
  184. // UVs for the dirt texture should be DistortUV(uv * DirtScale + DirtOffset) but
  185. // considering we use a cover-style scale on the dirt texture the difference
  186. // isn't massive so we chose to save a few ALUs here instead in case lens
  187. // distortion is active.
  188. half3 dirt = SAMPLE_TEXTURE2D(_LensDirt_Texture, sampler_LinearClamp, uvDistorted * LensDirtScale + LensDirtOffset).xyz;
  189. dirt *= LensDirtIntensity;
  190. color += dirt * bloom.xyz;
  191. }
  192. #endif
  193. #if _ENABLE_ALPHA_OUTPUT
  194. // Bloom should also spread in areas with zero alpha, so we save the image with bloom here to do the mixing at the end of the shader
  195. inputColor.xyz = color.xyz;
  196. #endif
  197. }
  198. #endif
  199. // To save on variants we'll use an uniform branch for vignette. Lower end platforms
  200. // don't like these but if we're running Uber it means we're running more expensive
  201. // effects anyway. Lower-end devices would limit themselves to on-tile compatible effect
  202. // and thus this shouldn't too much of a problem (famous last words).
  203. UNITY_BRANCH
  204. if (VignetteIntensity > 0)
  205. {
  206. #ifdef USING_STEREO_MATRICES
  207. // With XR, the views can use asymmetric FOV which will have the center of each
  208. // view be at a different location.
  209. const float2 VignetteCenter = unity_StereoEyeIndex == 0 ? VignetteCenterEye0 : VignetteCenterEye1;
  210. #endif
  211. color = ApplyVignette(color, uvDistorted, VignetteCenter, VignetteIntensity, VignetteRoundness, VignetteSmoothness, VignetteColor);
  212. }
  213. // Color grading is always enabled when post-processing/uber is active
  214. {
  215. color = ApplyColorGrading(color, PostExposure, TEXTURE2D_ARGS(_InternalLut, sampler_LinearClamp), LutParams, TEXTURE2D_ARGS(_UserLut, sampler_LinearClamp), UserLutParams, UserLutContribution);
  216. }
  217. #if _FILM_GRAIN
  218. {
  219. color = ApplyGrain(color, uv, TEXTURE2D_ARGS(_Grain_Texture, sampler_LinearRepeat), GrainIntensity, GrainResponse, GrainScale, GrainOffset, OneOverPaperWhite);
  220. }
  221. #endif
  222. // When Unity is configured to use gamma color encoding, we ignore the request to convert to gamma 2.0 and instead fall back to sRGB encoding
  223. #if _GAMMA_20 && !UNITY_COLORSPACE_GAMMA
  224. {
  225. color = LinearToGamma20(color);
  226. inputColor = LinearToGamma20(inputColor);
  227. }
  228. // Back to sRGB
  229. #elif UNITY_COLORSPACE_GAMMA || _LINEAR_TO_SRGB_CONVERSION
  230. {
  231. color = GetLinearToSRGB(color);
  232. inputColor = LinearToSRGB(inputColor);
  233. }
  234. #endif
  235. #if _DITHERING
  236. {
  237. color = ApplyDithering(color, uv, TEXTURE2D_ARGS(_BlueNoise_Texture, sampler_PointRepeat), DitheringScale, DitheringOffset, PaperWhite, OneOverPaperWhite);
  238. // Assume color > 0 and prevent 0 - ditherNoise.
  239. // Negative colors can cause problems if fed back to the postprocess via render to FP16 texture.
  240. color = max(color, 0);
  241. }
  242. #endif
  243. #ifdef HDR_ENCODING
  244. {
  245. // HDR UI composition
  246. float4 uiSample = SAMPLE_TEXTURE2D_X(_OverlayUITexture, sampler_PointClamp, input.texcoord);
  247. color.rgb = SceneUIComposition(uiSample, color.rgb, PaperWhite, MaxNits);
  248. }
  249. #endif
  250. // Alpha mask
  251. #if _ENABLE_ALPHA_OUTPUT
  252. {
  253. // Post processing is not applied on pixels with zero alpha
  254. // The alpha scale and bias control how steep is the transition between the post-processed and plain regions
  255. half alpha = inputColor.a * AlphaScale + AlphaBias;
  256. // Saturate is necessary to avoid issues when additive blending pushes the alpha over 1.
  257. // NOTE: in UNITY_COLORSPACE_GAMMA we alpha blend in gamma here, linear otherwise.
  258. color.xyz = lerp(inputColor.xyz, color.xyz, saturate(alpha));
  259. }
  260. #endif
  261. #ifdef HDR_ENCODING
  262. {
  263. color.rgb = OETF(color.rgb, MaxNits);
  264. }
  265. #endif
  266. #if defined(DEBUG_DISPLAY)
  267. half4 debugColor = 0;
  268. if(CanDebugOverrideOutputColor(half4(color, 1), uv, debugColor))
  269. {
  270. return debugColor;
  271. }
  272. #endif
  273. #if _ENABLE_ALPHA_OUTPUT
  274. return half4(color, inputColor.a);
  275. #else
  276. return half4(color, 1);
  277. #endif
  278. }
  279. ENDHLSL
  280. SubShader
  281. {
  282. Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline"}
  283. LOD 100
  284. ZTest Always ZWrite Off Cull Off
  285. //ColorMask RGB
  286. Pass
  287. {
  288. Name "UberPost"
  289. HLSLPROGRAM
  290. #pragma vertex Vert
  291. #pragma fragment FragUberPost
  292. ENDHLSL
  293. }
  294. }
  295. }