Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FinalPost.shader 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. Shader "Hidden/Universal Render Pipeline/FinalPost"
  2. {
  3. HLSLINCLUDE
  4. #pragma multi_compile_local_fragment _ _POINT_SAMPLING _RCAS _EASU_RCAS_AND_HDR_INPUT
  5. #pragma multi_compile_local_fragment _ _FXAA
  6. #pragma multi_compile_local_fragment _ _FILM_GRAIN
  7. #pragma multi_compile_local_fragment _ _DITHERING
  8. #pragma multi_compile_local_fragment _ _LINEAR_TO_SRGB_CONVERSION
  9. #pragma multi_compile_local_fragment _ _ENABLE_ALPHA_OUTPUT
  10. #pragma multi_compile_fragment _ DEBUG_DISPLAY
  11. #pragma multi_compile_fragment _ SCREEN_COORD_OVERRIDE
  12. #pragma multi_compile_local_fragment _ HDR_INPUT HDR_COLORSPACE_CONVERSION HDR_ENCODING HDR_COLORSPACE_CONVERSION_AND_ENCODING
  13. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  14. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  15. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/ScreenCoordOverride.hlsl"
  16. #if defined(HDR_COLORSPACE_CONVERSION) || defined(HDR_ENCODING) || defined(HDR_COLORSPACE_CONVERSION_AND_ENCODING)
  17. #define HDR_INPUT 1 // this should be defined when HDR_COLORSPACE_CONVERSION or HDR_ENCODING are defined
  18. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/HDROutput.hlsl"
  19. #endif
  20. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  21. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Debug/DebuggingFullscreen.hlsl"
  22. #include "Packages/com.unity.render-pipelines.universal/Shaders/PostProcessing/Common.hlsl"
  23. TEXTURE2D(_Grain_Texture);
  24. TEXTURE2D(_BlueNoise_Texture);
  25. TEXTURE2D_X(_OverlayUITexture);
  26. float4 _SourceSize;
  27. float2 _Grain_Params;
  28. float4 _Grain_TilingParams;
  29. float4 _Dithering_Params;
  30. float4 _HDROutputLuminanceParams;
  31. #define GrainIntensity _Grain_Params.x
  32. #define GrainResponse _Grain_Params.y
  33. #define GrainScale _Grain_TilingParams.xy
  34. #define GrainOffset _Grain_TilingParams.zw
  35. #define DitheringScale _Dithering_Params.xy
  36. #define DitheringOffset _Dithering_Params.zw
  37. #define MinNits _HDROutputLuminanceParams.x
  38. #define MaxNits _HDROutputLuminanceParams.y
  39. #define PaperWhite _HDROutputLuminanceParams.z
  40. #define OneOverPaperWhite _HDROutputLuminanceParams.w
  41. #if SHADER_TARGET >= 45
  42. #define FSR_INPUT_TEXTURE _BlitTexture
  43. #define FSR_INPUT_SAMPLER sampler_LinearClamp
  44. // If HDR_INPUT is defined, we must also define FSR_EASU_ONE_OVER_PAPER_WHITE before including the FSR common header.
  45. // URP doesn't actually uses EASU from finalPost shader, only RCAS.
  46. #define FSR_EASU_ONE_OVER_PAPER_WHITE OneOverPaperWhite
  47. #include "Packages/com.unity.render-pipelines.core/Runtime/PostProcessing/Shaders/FSRCommon.hlsl"
  48. #endif
  49. half4 FragFinalPost(Varyings input) : SV_Target
  50. {
  51. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  52. float2 uv = UnityStereoTransformScreenSpaceTex(input.texcoord);
  53. float2 positionNDC = uv;
  54. int2 positionSS = uv * _SourceSize.xy;
  55. #if _POINT_SAMPLING
  56. // Hlsl specifies missing input.a to fill 1 (0 for .rgb).
  57. half4 color = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_PointClamp, uv);
  58. #elif (_RCAS || _EASU_RCAS_AND_HDR_INPUT) && SHADER_TARGET >= 45
  59. half4 color = half4(ApplyRCAS(positionSS), 1.0);
  60. // When Unity is configured to use gamma color encoding, we must convert back from linear after RCAS is performed.
  61. // (The input color data for this shader variant is always linearly encoded because RCAS requires it)
  62. #if _EASU_RCAS_AND_HDR_INPUT
  63. // Revert operation from ScalingSetup.shader
  64. color.rgb = FastTonemapInvert(color.rgb) * PaperWhite;
  65. #endif
  66. #if UNITY_COLORSPACE_GAMMA
  67. color = GetLinearToSRGB(color);
  68. #endif
  69. #if _ENABLE_ALPHA_OUTPUT
  70. color.a = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, uv).a;
  71. #endif
  72. #else
  73. half4 color = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, uv);
  74. #endif
  75. #if _FXAA
  76. {
  77. #if _ENABLE_ALPHA_OUTPUT
  78. // When alpha processing is enabled, FXAA should not affect pixels with zero alpha
  79. UNITY_BRANCH
  80. if(color.a > 0)
  81. color.rgb = ApplyFXAA(color.rgb, positionNDC, positionSS, _SourceSize, _BlitTexture, PaperWhite, OneOverPaperWhite);
  82. #else
  83. color.rgb = ApplyFXAA(color.rgb, positionNDC, positionSS, _SourceSize, _BlitTexture, PaperWhite, OneOverPaperWhite);
  84. #endif
  85. }
  86. #endif
  87. #if _FILM_GRAIN
  88. {
  89. color.rgb = ApplyGrain(color.rgb, SCREEN_COORD_APPLY_SCALEBIAS(positionNDC), TEXTURE2D_ARGS(_Grain_Texture, sampler_LinearRepeat), GrainIntensity, GrainResponse, GrainScale, GrainOffset, OneOverPaperWhite);
  90. }
  91. #endif
  92. #if _LINEAR_TO_SRGB_CONVERSION
  93. {
  94. color = LinearToSRGB(color);
  95. }
  96. #endif
  97. #if _DITHERING
  98. {
  99. color.rgb = ApplyDithering(color.rgb, SCREEN_COORD_APPLY_SCALEBIAS(positionNDC), TEXTURE2D_ARGS(_BlueNoise_Texture, sampler_PointRepeat), DitheringScale, DitheringOffset, PaperWhite, OneOverPaperWhite);
  100. }
  101. #endif
  102. #ifdef HDR_COLORSPACE_CONVERSION
  103. {
  104. color.rgb = RotateRec709ToOutputSpace(color.rgb) * PaperWhite;
  105. }
  106. #endif
  107. #ifdef HDR_ENCODING
  108. {
  109. float4 uiSample = SAMPLE_TEXTURE2D_X(_OverlayUITexture, sampler_PointClamp, input.texcoord);
  110. color.rgb = SceneUIComposition(uiSample, color.rgb, PaperWhite, MaxNits);
  111. color.rgb = OETF(color.rgb, MaxNits);
  112. }
  113. #endif
  114. #if _ENABLE_ALPHA_OUTPUT
  115. half4 finalColor = color;
  116. #else
  117. half4 finalColor = half4(color.rgb, 1.0);
  118. #endif
  119. #if defined(DEBUG_DISPLAY)
  120. half4 debugColor = 0;
  121. if(CanDebugOverrideOutputColor(finalColor, uv, debugColor))
  122. {
  123. return debugColor;
  124. }
  125. #endif
  126. return finalColor;
  127. }
  128. ENDHLSL
  129. /// Standard FinalPost shader variant with support for FSR
  130. /// Note: FSR requires shader target 4.5 because it relies on texture gather instructions
  131. SubShader
  132. {
  133. Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline"}
  134. LOD 100
  135. ZTest Always ZWrite Off Cull Off
  136. Pass
  137. {
  138. Name "FinalPost"
  139. HLSLPROGRAM
  140. #pragma vertex Vert
  141. #pragma fragment FragFinalPost
  142. #pragma target 4.5
  143. ENDHLSL
  144. }
  145. }
  146. /// Fallback version of FinalPost shader which lacks support for FSR
  147. SubShader
  148. {
  149. Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline"}
  150. LOD 100
  151. ZTest Always ZWrite Off Cull Off
  152. Pass
  153. {
  154. Name "FinalPost"
  155. HLSLPROGRAM
  156. #pragma vertex Vert
  157. #pragma fragment FragFinalPost
  158. ENDHLSL
  159. }
  160. }
  161. }