暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

HDRDebugView.shader 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. Shader "Hidden/Universal/HDRDebugView"
  2. {
  3. HLSLINCLUDE
  4. #pragma target 4.5
  5. #pragma editor_sync_compilation
  6. #pragma multi_compile_fragment _ DEBUG_DISPLAY
  7. #pragma multi_compile_local_fragment _ HDR_ENCODING
  8. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  9. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  10. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  11. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/ACES.hlsl"
  12. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/HDROutput.hlsl"
  13. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Debug/DebuggingFullscreen.hlsl"
  14. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Debug.hlsl"
  15. #include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
  16. TEXTURE2D_X(_OverlayUITexture);
  17. TEXTURE2D(_xyBuffer);
  18. int _DebugHDRMode;
  19. float4 _HDRDebugParams; // xy: xyBuffer size, zw: unused
  20. float4 _HDROutputLuminanceParams; // xy: brightness min/max, z: paper white brightness, w: one over paper white brightness
  21. #define _MinNits _HDROutputLuminanceParams.x
  22. #define _MaxNits _HDROutputLuminanceParams.y
  23. #define _PaperWhite _HDROutputLuminanceParams.z
  24. float2 RGBtoxy(float3 rgb)
  25. {
  26. float3 XYZ = RotateOutputSpaceToXYZ(rgb);
  27. return XYZtoxy(XYZ);
  28. }
  29. float3 uvToGamut(float2 uv)
  30. {
  31. float3 xyzColor = xyYtoXYZ(float3(uv.x, uv.y, 1.0f));
  32. float3 linearRGB = RotateXYZToOutputSpace(xyzColor);
  33. float scale = 1.0f / length(linearRGB);
  34. float desat = dot(linearRGB, 0.333f);
  35. scale *= 1.0 + exp(-length(linearRGB - desat) * 2.0f) * 0.5f;
  36. linearRGB *= scale;
  37. return linearRGB;
  38. }
  39. bool IsInCIExyMapping(float2 xy)
  40. {
  41. return SAMPLE_TEXTURE2D_LOD(_xyBuffer, sampler_PointClamp, xy, 0.0).x != 0;
  42. }
  43. float3 ValuesAbovePaperWhite(half4 color, float2 uv)
  44. {
  45. float maxC = max(color.x, max(color.y, color.z));
  46. if (maxC > _PaperWhite)
  47. {
  48. if ( maxC > _MaxNits)
  49. {
  50. return float3(0, 0, _PaperWhite);
  51. }
  52. else
  53. {
  54. float t = (maxC - _PaperWhite) / (_MaxNits - _PaperWhite);
  55. return lerp(float3(_PaperWhite, _PaperWhite, 0), float3(_PaperWhite, 0, 0), saturate(t));
  56. }
  57. }
  58. else
  59. {
  60. return Luminance(color).xxx;
  61. }
  62. }
  63. void RenderDebugHDR(half4 color, float2 uv, inout half4 debugColor)
  64. {
  65. if (_DebugHDRMode == HDRDEBUGMODE_VALUES_ABOVE_PAPER_WHITE)
  66. {
  67. debugColor.xyz = ValuesAbovePaperWhite(color, uv);
  68. return;
  69. }
  70. int displayClip = (_DebugHDRMode == HDRDEBUGMODE_GAMUT_CLIP);
  71. int gamutPiPSize = _ScreenSize.x / 3.0f;
  72. float2 r_2020 = float2(0.708, 0.292);
  73. float2 g_2020 = float2(0.170, 0.797);
  74. float2 b_2020 = float2(0.131, 0.046);
  75. float2 r_709 = float2(0.64, 0.33);
  76. float2 g_709 = float2(0.3, 0.6);
  77. float2 b_709 = float2(0.15, 0.06);
  78. float2 r_p3 = float2(0.68, 0.32);
  79. float2 g_p3 = float2(0.265, 0.69);
  80. float2 b_p3 = float2(0.15, 0.06);
  81. float2 pos = uv * _ScreenSize.xy;
  82. float lineThickness = 0.002;
  83. float2 xy = RGBtoxy(color.rgb);
  84. float3 rec2020Color = float3(_PaperWhite, 0, 0);
  85. float3 rec2020ColorDesat = float3(3.0, 0.5, 0.5);
  86. float3 rec709Color = float3(0, _PaperWhite, 0);
  87. float3 rec709ColorDesat = float3(0.4, 0.6, 0.4);
  88. float3 p3Color = float3(0, 0, _PaperWhite);
  89. float3 p3ColorDesat = float3(0.4, 0.4, 0.6);
  90. //Display Gamut Clip Scene colour conversion
  91. if (displayClip)
  92. {
  93. float clipAlpha = 0.2f;
  94. if (IsPointInTriangle(xy, r_709, g_709, b_709))
  95. {
  96. color.rgb = (color.rgb * (1 - clipAlpha) + clipAlpha * rec709Color);
  97. }
  98. else if (IsPointInTriangle(xy, r_p3, g_p3, b_p3))
  99. {
  100. color.rgb = (color.rgb * (1 - clipAlpha) + clipAlpha * p3Color);
  101. }
  102. else if (IsPointInTriangle(xy, r_2020, g_2020, b_2020))
  103. {
  104. color.rgb = (color.rgb * (1 - clipAlpha) + clipAlpha * rec2020Color);
  105. }
  106. }
  107. float4 gamutColor = 0;
  108. if (all(pos < gamutPiPSize))
  109. {
  110. float2 uv = pos / gamutPiPSize; // scale-down uv
  111. float4 lineColor = DrawSegment(uv, g_709, b_709, lineThickness, float3(0, 0, 0)) + DrawSegment(uv, b_709, r_709, lineThickness, float3(0, 0, 0)) +
  112. DrawSegment(uv, r_709, g_709, lineThickness, float3(0, 0, 0)) +
  113. DrawSegment(uv, g_2020, b_2020, lineThickness, float3(0, 0, 0)) + DrawSegment(uv, b_2020, r_2020, lineThickness, float3(0, 0, 0)) +
  114. DrawSegment(uv, r_2020, g_2020, lineThickness, float3(0, 0, 0)) +
  115. DrawSegment(uv, g_p3, b_p3, lineThickness, float3(0, 0, 0)) + DrawSegment(uv, b_p3, r_p3, lineThickness, float3(0, 0, 0)) +
  116. DrawSegment(uv, r_p3, g_p3, lineThickness, float3(0, 0, 0));
  117. float3 linearRGB = 0;
  118. bool pointInRec709 = true;
  119. if (IsPointInTriangle(uv, r_2020, g_2020, b_2020))
  120. {
  121. float3 colorSpaceColor = rec709Color;
  122. linearRGB = uvToGamut(uv);
  123. if (displayClip)
  124. {
  125. if (IsPointInTriangle(uv, r_709, g_709, b_709))
  126. {
  127. colorSpaceColor = rec709Color;
  128. linearRGB.rgb = rec709ColorDesat;
  129. }
  130. else if (IsPointInTriangle(uv, r_p3, g_p3, b_p3))
  131. {
  132. colorSpaceColor = p3Color;
  133. linearRGB.rgb = p3ColorDesat;
  134. }
  135. else
  136. {
  137. colorSpaceColor = rec2020Color;
  138. linearRGB.rgb = rec2020ColorDesat;
  139. }
  140. }
  141. gamutColor.a = max(lineColor.a, 0.15);
  142. gamutColor.rgb = linearRGB * _PaperWhite;
  143. if (IsInCIExyMapping(uv))
  144. {
  145. gamutColor.a = 1;
  146. if (displayClip)
  147. gamutColor.rgb = colorSpaceColor;
  148. }
  149. }
  150. gamutColor.rgb = gamutColor.rgb * (1.0f - lineColor.a) + lineColor.rgb;
  151. }
  152. debugColor.rgb = gamutColor.rgb * gamutColor.a + color.rgb * (1 - gamutColor.a);
  153. }
  154. ENDHLSL
  155. SubShader
  156. {
  157. Tags{ "RenderPipeline" = "UniversalPipeline" }
  158. Pass
  159. {
  160. ZWrite Off
  161. ZTest Always
  162. ZClip Off
  163. Blend Off
  164. Cull Off
  165. HLSLPROGRAM
  166. #pragma vertex Vert
  167. #pragma fragment Frag
  168. RW_TEXTURE2D(float, _xyBufferRW);
  169. #define _SizePerDim _HDRDebugParams.xy
  170. half4 Frag(Varyings input) : SV_Target
  171. {
  172. float4 col = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_PointClamp, input.texcoord);
  173. float2 xy = (RGBtoxy(col.rgb));
  174. _xyBufferRW[(xy * _SizePerDim)] = 1;
  175. return col;
  176. }
  177. ENDHLSL
  178. }
  179. Pass
  180. {
  181. ZWrite Off
  182. ZTest Always
  183. ZClip Off
  184. Blend Off
  185. Cull Off
  186. HLSLPROGRAM
  187. #pragma vertex Vert
  188. #pragma fragment Frag
  189. half4 Frag(Varyings input) : SV_Target
  190. {
  191. float4 outCol = 0;
  192. float2 uv = input.texcoord;
  193. half4 col = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_PointClamp, uv);
  194. half4 outColor = 0;
  195. RenderDebugHDR(col, uv, outColor);
  196. #if defined(HDR_ENCODING)
  197. float4 uiSample = SAMPLE_TEXTURE2D_X(_OverlayUITexture, sampler_PointClamp, input.texcoord);
  198. outColor.rgb = SceneUIComposition(uiSample, outColor.rgb, _PaperWhite, _MaxNits);
  199. outColor.rgb = OETF(outColor.rgb, _MaxNits);
  200. #endif
  201. #if defined(DEBUG_DISPLAY)
  202. half4 debugColor = 0;
  203. if (CanDebugOverrideOutputColor(outColor, uv, debugColor))
  204. {
  205. return debugColor;
  206. }
  207. #endif
  208. return outColor;
  209. }
  210. ENDHLSL
  211. }
  212. }
  213. }