Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DebuggingCommon.hlsl 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #ifndef UNIVERSAL_DEBUGGING_COMMON_INCLUDED
  2. #define UNIVERSAL_DEBUGGING_COMMON_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Debug/DebugViewEnums.cs.hlsl"
  4. #if defined(DEBUG_DISPLAY)
  5. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  6. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/DebugMipmapStreaming.hlsl"
  7. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Debug.hlsl"
  8. // Material settings...
  9. int _DebugMaterialMode;
  10. int _DebugVertexAttributeMode;
  11. int _DebugMaterialValidationMode;
  12. // Rendering settings...
  13. int _DebugFullScreenMode;
  14. int _DebugSceneOverrideMode;
  15. int _DebugMipInfoMode;
  16. int _DebugMipMapStatusMode;
  17. int _DebugMipMapShowStatusCode;
  18. half _DebugMipMapOpacity;
  19. half _DebugMipMapRecentlyUpdatedCooldown;
  20. int _DebugMipMapTerrainTextureMode;
  21. int _DebugValidationMode;
  22. // Lighting settings...
  23. int _DebugLightingMode;
  24. int _DebugLightingFeatureFlags;
  25. half _DebugValidateAlbedoMinLuminance = 0.01;
  26. half _DebugValidateAlbedoMaxLuminance = 0.90;
  27. half _DebugValidateAlbedoSaturationTolerance = 0.214;
  28. half _DebugValidateAlbedoHueTolerance = 0.104;
  29. half3 _DebugValidateAlbedoCompareColor = half3(0.5, 0.5, 0.5);
  30. half _DebugValidateMetallicMinValue = 0;
  31. half _DebugValidateMetallicMaxValue = 0.9;
  32. float4 _DebugColor;
  33. float4 _DebugColorInvalidMode;
  34. float4 _DebugValidateBelowMinThresholdColor;
  35. float4 _DebugValidateAboveMaxThresholdColor;
  36. // Not particular to any settings; used by MipMap debugging because we don't have reliable access to _Time.y
  37. float _DebugCurrentRealTime;
  38. // We commonly need to undo a previous TRANSFORM_TEX to get UV0s back, followed by a TRANSFORM_TEX with the UV0s / _ST of the StreamingDebugTex... hence, this macro.
  39. #define UNDO_TRANSFORM_TEX(uv, undoTex) ((uv - undoTex##_ST.zw) / undoTex##_ST.xy)
  40. half3 GetDebugColor(uint index)
  41. {
  42. uint clampedIndex = clamp(index, 0, DEBUG_COLORS_COUNT-1);
  43. return kDebugColorGradient[clampedIndex].rgb;
  44. }
  45. bool TryGetDebugColorInvalidMode(out half4 debugColor)
  46. {
  47. // Depending upon how we want to deal with invalid modes, this code may need to change,
  48. // for now we'll simply make each pixel use "_DebugColorInvalidMode"...
  49. debugColor = _DebugColorInvalidMode;
  50. return true;
  51. }
  52. uint GetMipMapLevel(float2 nonNormalizedUVCoordinate)
  53. {
  54. // The OpenGL Graphics System: A Specification 4.2
  55. // - chapter 3.9.11, equation 3.21
  56. float2 dx_vtc = ddx(nonNormalizedUVCoordinate);
  57. float2 dy_vtc = ddy(nonNormalizedUVCoordinate);
  58. float delta_max_sqr = max(dot(dx_vtc, dx_vtc), dot(dy_vtc, dy_vtc));
  59. return (uint)(0.5 * log2(delta_max_sqr));
  60. }
  61. bool CalculateValidationAlbedo(half3 albedo, out half4 color)
  62. {
  63. half luminance = Luminance(albedo);
  64. if (luminance < _DebugValidateAlbedoMinLuminance)
  65. {
  66. color = _DebugValidateBelowMinThresholdColor;
  67. }
  68. else if (luminance > _DebugValidateAlbedoMaxLuminance)
  69. {
  70. color = _DebugValidateAboveMaxThresholdColor;
  71. }
  72. else
  73. {
  74. half3 hsv = RgbToHsv(albedo);
  75. half hue = hsv.r;
  76. half sat = hsv.g;
  77. half3 compHSV = RgbToHsv(_DebugValidateAlbedoCompareColor.rgb);
  78. half compHue = compHSV.r;
  79. half compSat = compHSV.g;
  80. if ((compSat - _DebugValidateAlbedoSaturationTolerance > sat) || ((compHue - _DebugValidateAlbedoHueTolerance > hue) && (compHue - _DebugValidateAlbedoHueTolerance + 1.0 > hue)))
  81. {
  82. color = _DebugValidateBelowMinThresholdColor;
  83. }
  84. else if ((sat > compSat + _DebugValidateAlbedoSaturationTolerance) || ((hue > compHue + _DebugValidateAlbedoHueTolerance) && (hue > compHue + _DebugValidateAlbedoHueTolerance - 1.0)))
  85. {
  86. color = _DebugValidateAboveMaxThresholdColor;
  87. }
  88. else
  89. {
  90. color = half4(luminance, luminance, luminance, 1.0);
  91. }
  92. }
  93. return true;
  94. }
  95. bool CalculateColorForDebugSceneOverride(out half4 color)
  96. {
  97. if (_DebugSceneOverrideMode == DEBUGSCENEOVERRIDEMODE_NONE)
  98. {
  99. color = 0;
  100. return false;
  101. }
  102. else
  103. {
  104. color = _DebugColor;
  105. return true;
  106. }
  107. }
  108. half4 BlitScreenSpaceDigit(half4 originalColor, uint2 screenSpaceCoords, int digit, uint spacing, bool invertColors)
  109. {
  110. half4 outColor = originalColor;
  111. const uint2 pixCoord = screenSpaceCoords / 2;
  112. const uint2 tileSize = uint2(spacing, spacing);
  113. const int2 coord = (pixCoord & (tileSize - 1)) - int2(tileSize.x/4+1, tileSize.y/3-3);
  114. UNITY_LOOP for (int i = 0; i <= 1; ++i)
  115. {
  116. // 0 == shadow, 1 == text
  117. if (SampleDebugFontNumber2Digits(coord + i, digit))
  118. {
  119. outColor = (i == 0)
  120. ? (invertColors ? half4(1, 1, 1, 1) : half4(0, 0, 0, 1))
  121. : (invertColors ? half4(0, 0, 0, 1) : half4(1, 1, 1, 1));
  122. }
  123. }
  124. return outColor;
  125. }
  126. void GetHatchedColor(uint2 screenSpaceCoords, half4 hatchingColor, inout half4 debugColor)
  127. {
  128. const uint spacing = 16; // increase spacing compared to the legend (easier on the eyes)
  129. const uint thickness = 3;
  130. if((screenSpaceCoords.x + screenSpaceCoords.y) % spacing < thickness)
  131. debugColor = hatchingColor;
  132. }
  133. void GetHatchedColor(uint2 screenSpaceCoords, inout half4 debugColor)
  134. {
  135. GetHatchedColor(screenSpaceCoords, half4(0.1, 0.1, 0.1, 1), debugColor);
  136. }
  137. // Keep in sync with GetTextureDataDebug in HDRP's Runtime/Debug/DebugDisplay.hlsl
  138. bool CalculateColorForDebugMipmapStreaming(in uint mipCount, uint2 screenSpaceCoords, in float4 texelSize, in float2 uv, in float4 mipInfo, in float4 streamInfo, in half3 originalColor, inout half4 debugColor)
  139. {
  140. bool hasDebugColor = false;
  141. bool needsHatching;
  142. switch (_DebugMipInfoMode)
  143. {
  144. case DEBUGMIPINFOMODE_NONE:
  145. hasDebugColor = false;
  146. break;
  147. case DEBUGMIPINFOMODE_MIP_COUNT:
  148. debugColor = half4(GetDebugMipCountColor(mipCount, needsHatching), 1);
  149. if (needsHatching)
  150. {
  151. half4 hatchingColor = half4(GetDebugMipCountHatchingColor(mipCount), 1);
  152. GetHatchedColor(screenSpaceCoords, hatchingColor, debugColor);
  153. }
  154. if (mipCount > 0 && mipCount <= 14)
  155. debugColor = BlitScreenSpaceDigit(debugColor, screenSpaceCoords, mipCount, 32, true);
  156. hasDebugColor = true;
  157. break;
  158. case DEBUGMIPINFOMODE_MIP_RATIO:
  159. debugColor = half4(GetDebugMipColorIncludingMipReduction(originalColor, mipCount, texelSize, uv, mipInfo), 1);
  160. hasDebugColor = true;
  161. break;
  162. case DEBUGMIPINFOMODE_MIP_STREAMING_PERFORMANCE:
  163. debugColor = half4(GetDebugStreamingMipColor(mipCount, mipInfo, streamInfo, needsHatching), 1);
  164. if (needsHatching)
  165. GetHatchedColor(screenSpaceCoords, debugColor);
  166. hasDebugColor = true;
  167. break;
  168. case DEBUGMIPINFOMODE_MIP_STREAMING_STATUS:
  169. if(_DebugMipMapStatusMode == DEBUGMIPMAPSTATUSMODE_TEXTURE)
  170. debugColor = half4(GetDebugStreamingStatusColor(streamInfo, needsHatching), 1);
  171. else
  172. debugColor = half4(GetDebugPerMaterialStreamingStatusColor(streamInfo, needsHatching), 1);
  173. if (needsHatching)
  174. GetHatchedColor(screenSpaceCoords, debugColor);
  175. if (_DebugMipMapShowStatusCode && _DebugMipMapStatusMode == DEBUGMIPMAPSTATUSMODE_TEXTURE && !IsStreaming(streamInfo))
  176. {
  177. if (GetStatusCode(streamInfo, false) != kMipmapDebugStatusCodeNotSet && GetStatusCode(streamInfo, false) != kMipmapDebugStatusCodeNoTexture) // we're ignoring these because there's just one status anyway (so the color itself is enough)
  178. debugColor = BlitScreenSpaceDigit(debugColor, screenSpaceCoords, GetStatusCode(streamInfo, false), 16, false);
  179. }
  180. hasDebugColor = true;
  181. break;
  182. case DEBUGMIPINFOMODE_MIP_STREAMING_PRIORITY:
  183. debugColor = half4(GetDebugStreamingPriorityColor(streamInfo), 1);
  184. hasDebugColor = true;
  185. break;
  186. case DEBUGMIPINFOMODE_MIP_STREAMING_ACTIVITY:
  187. debugColor = half4(GetDebugStreamingRecentlyUpdatedColor(_DebugCurrentRealTime, _DebugMipMapRecentlyUpdatedCooldown, _DebugMipMapStatusMode == DEBUGMIPMAPSTATUSMODE_MATERIAL, streamInfo), 1);
  188. hasDebugColor = true;
  189. break;
  190. default:
  191. hasDebugColor = TryGetDebugColorInvalidMode(debugColor);
  192. break;
  193. }
  194. // Blend the original color with the debug color
  195. if(hasDebugColor)
  196. debugColor = lerp(half4(originalColor, 1), debugColor, _DebugMipMapOpacity);
  197. return hasDebugColor;
  198. }
  199. #else
  200. // When "DEBUG_DISPLAY" isn't defined this macro just returns the original UVs.
  201. #define UNDO_TRANSFORM_TEX(uv, undoTex) uv
  202. #endif
  203. bool IsAlphaDiscardEnabled()
  204. {
  205. #if defined(DEBUG_DISPLAY)
  206. return (_DebugSceneOverrideMode == DEBUGSCENEOVERRIDEMODE_NONE);
  207. #else
  208. return true;
  209. #endif
  210. }
  211. bool IsFogEnabled()
  212. {
  213. #if defined(DEBUG_DISPLAY)
  214. return (_DebugMaterialMode == DEBUGMATERIALMODE_NONE) &&
  215. (_DebugVertexAttributeMode == DEBUGVERTEXATTRIBUTEMODE_NONE) &&
  216. (_DebugMaterialValidationMode == DEBUGMATERIALVALIDATIONMODE_NONE) &&
  217. (_DebugSceneOverrideMode == DEBUGSCENEOVERRIDEMODE_NONE) &&
  218. (_DebugMipInfoMode == DEBUGMIPINFOMODE_NONE) &&
  219. (_DebugLightingMode == DEBUGLIGHTINGMODE_NONE) &&
  220. (_DebugLightingFeatureFlags == 0) &&
  221. (_DebugValidationMode == DEBUGVALIDATIONMODE_NONE);
  222. #else
  223. return true;
  224. #endif
  225. }
  226. bool IsLightingFeatureEnabled(uint bitMask)
  227. {
  228. #if defined(DEBUG_DISPLAY)
  229. return (_DebugLightingFeatureFlags == 0) || ((_DebugLightingFeatureFlags & bitMask) != 0);
  230. #else
  231. return true;
  232. #endif
  233. }
  234. bool IsOnlyAOLightingFeatureEnabled()
  235. {
  236. #if defined(DEBUG_DISPLAY)
  237. return _DebugLightingFeatureFlags == DEBUGLIGHTINGFEATUREFLAGS_AMBIENT_OCCLUSION;
  238. #else
  239. return false;
  240. #endif
  241. }
  242. #endif