DebuggingCommon.hlsl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/Debug.hlsl"
  7. // Material settings...
  8. int _DebugMaterialMode;
  9. int _DebugVertexAttributeMode;
  10. int _DebugMaterialValidationMode;
  11. // Rendering settings...
  12. int _DebugFullScreenMode;
  13. int _DebugSceneOverrideMode;
  14. int _DebugMipInfoMode;
  15. int _DebugValidationMode;
  16. // Lighting settings...
  17. int _DebugLightingMode;
  18. int _DebugLightingFeatureFlags;
  19. half _DebugValidateAlbedoMinLuminance = 0.01;
  20. half _DebugValidateAlbedoMaxLuminance = 0.90;
  21. half _DebugValidateAlbedoSaturationTolerance = 0.214;
  22. half _DebugValidateAlbedoHueTolerance = 0.104;
  23. half3 _DebugValidateAlbedoCompareColor = half3(0.5, 0.5, 0.5);
  24. half _DebugValidateMetallicMinValue = 0;
  25. half _DebugValidateMetallicMaxValue = 0.9;
  26. float4 _DebugColor;
  27. float4 _DebugColorInvalidMode;
  28. float4 _DebugValidateBelowMinThresholdColor;
  29. float4 _DebugValidateAboveMaxThresholdColor;
  30. half3 GetDebugColor(uint index)
  31. {
  32. uint clampedIndex = clamp(index, 0, DEBUG_COLORS_COUNT-1);
  33. return kDebugColorGradient[clampedIndex].rgb;
  34. }
  35. bool TryGetDebugColorInvalidMode(out half4 debugColor)
  36. {
  37. // Depending upon how we want to deal with invalid modes, this code may need to change,
  38. // for now we'll simply make each pixel use "_DebugColorInvalidMode"...
  39. debugColor = _DebugColorInvalidMode;
  40. return true;
  41. }
  42. uint GetMipMapLevel(float2 nonNormalizedUVCoordinate)
  43. {
  44. // The OpenGL Graphics System: A Specification 4.2
  45. // - chapter 3.9.11, equation 3.21
  46. float2 dx_vtc = ddx(nonNormalizedUVCoordinate);
  47. float2 dy_vtc = ddy(nonNormalizedUVCoordinate);
  48. float delta_max_sqr = max(dot(dx_vtc, dx_vtc), dot(dy_vtc, dy_vtc));
  49. return (uint)(0.5 * log2(delta_max_sqr));
  50. }
  51. bool CalculateValidationAlbedo(half3 albedo, out half4 color)
  52. {
  53. half luminance = Luminance(albedo);
  54. if (luminance < _DebugValidateAlbedoMinLuminance)
  55. {
  56. color = _DebugValidateBelowMinThresholdColor;
  57. }
  58. else if (luminance > _DebugValidateAlbedoMaxLuminance)
  59. {
  60. color = _DebugValidateAboveMaxThresholdColor;
  61. }
  62. else
  63. {
  64. half3 hsv = RgbToHsv(albedo);
  65. half hue = hsv.r;
  66. half sat = hsv.g;
  67. half3 compHSV = RgbToHsv(_DebugValidateAlbedoCompareColor.rgb);
  68. half compHue = compHSV.r;
  69. half compSat = compHSV.g;
  70. if ((compSat - _DebugValidateAlbedoSaturationTolerance > sat) || ((compHue - _DebugValidateAlbedoHueTolerance > hue) && (compHue - _DebugValidateAlbedoHueTolerance + 1.0 > hue)))
  71. {
  72. color = _DebugValidateBelowMinThresholdColor;
  73. }
  74. else if ((sat > compSat + _DebugValidateAlbedoSaturationTolerance) || ((hue > compHue + _DebugValidateAlbedoHueTolerance) && (hue > compHue + _DebugValidateAlbedoHueTolerance - 1.0)))
  75. {
  76. color = _DebugValidateAboveMaxThresholdColor;
  77. }
  78. else
  79. {
  80. color = half4(luminance, luminance, luminance, 1.0);
  81. }
  82. }
  83. return true;
  84. }
  85. bool CalculateColorForDebugSceneOverride(out half4 color)
  86. {
  87. if (_DebugSceneOverrideMode == DEBUGSCENEOVERRIDEMODE_NONE)
  88. {
  89. color = 0;
  90. return false;
  91. }
  92. else
  93. {
  94. color = _DebugColor;
  95. return true;
  96. }
  97. }
  98. #endif
  99. bool IsAlphaDiscardEnabled()
  100. {
  101. #if defined(DEBUG_DISPLAY)
  102. return (_DebugSceneOverrideMode == DEBUGSCENEOVERRIDEMODE_NONE);
  103. #else
  104. return true;
  105. #endif
  106. }
  107. bool IsFogEnabled()
  108. {
  109. #if defined(DEBUG_DISPLAY)
  110. return (_DebugMaterialMode == DEBUGMATERIALMODE_NONE) &&
  111. (_DebugVertexAttributeMode == DEBUGVERTEXATTRIBUTEMODE_NONE) &&
  112. (_DebugMaterialValidationMode == DEBUGMATERIALVALIDATIONMODE_NONE) &&
  113. (_DebugSceneOverrideMode == DEBUGSCENEOVERRIDEMODE_NONE) &&
  114. (_DebugMipInfoMode == DEBUGMIPINFOMODE_NONE) &&
  115. (_DebugLightingMode == DEBUGLIGHTINGMODE_NONE) &&
  116. (_DebugLightingFeatureFlags == 0) &&
  117. (_DebugValidationMode == DEBUGVALIDATIONMODE_NONE);
  118. #else
  119. return true;
  120. #endif
  121. }
  122. bool IsLightingFeatureEnabled(uint bitMask)
  123. {
  124. #if defined(DEBUG_DISPLAY)
  125. return (_DebugLightingFeatureFlags == 0) || ((_DebugLightingFeatureFlags & bitMask) != 0);
  126. #else
  127. return true;
  128. #endif
  129. }
  130. bool IsOnlyAOLightingFeatureEnabled()
  131. {
  132. #if defined(DEBUG_DISPLAY)
  133. return _DebugLightingFeatureFlags == DEBUGLIGHTINGFEATUREFLAGS_AMBIENT_OCCLUSION;
  134. #else
  135. return false;
  136. #endif
  137. }
  138. #endif