|
| 1 | +#ifdef DEBUG_DISPLAY // Guard define here to be compliant with how shader graph generate code for include |
| 2 | + |
| 3 | +#ifndef UNITY_DEBUG_DISPLAY_INCLUDED |
| 4 | +#define UNITY_DEBUG_DISPLAY_INCLUDED |
| 5 | + |
| 6 | +#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Debug.hlsl" |
| 7 | +#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl" |
| 8 | +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.cs.hlsl" |
| 9 | +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Debug/MaterialDebug.cs.hlsl" |
| 10 | +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Debug/LightingDebug.cs.hlsl" |
| 11 | +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Debug/MipMapDebug.cs.hlsl" |
| 12 | +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Debug/ColorPickerDebug.cs.hlsl" |
| 13 | + |
| 14 | + |
| 15 | +// Local shader variables |
| 16 | +static SHADOW_TYPE g_DebugShadowAttenuation = 0; |
| 17 | + |
| 18 | +StructuredBuffer<int2> _DebugDepthPyramidOffsets; |
| 19 | + |
| 20 | +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Debug/PBRValidator.hlsl" |
| 21 | + |
| 22 | +// When displaying lux meter we compress the light in order to be able to display value higher than 65504 |
| 23 | +// The sun is between 100 000 and 150 000, so we use 4 to be able to cover such a range (4 * 65504) |
| 24 | +#define LUXMETER_COMPRESSION_RATIO 4 |
| 25 | + |
| 26 | +TEXTURE2D(_DebugFont); // Debug font to write string in shader |
| 27 | +TEXTURE2D(_DebugMatCapTexture); |
| 28 | + |
| 29 | +void GetPropertiesDataDebug(uint paramId, inout float3 result, inout bool needLinearToSRGB) |
| 30 | +{ |
| 31 | + switch (paramId) |
| 32 | + { |
| 33 | + case DEBUGVIEWPROPERTIES_TESSELLATION: |
| 34 | +#ifdef TESSELLATION_ON |
| 35 | + result = float3(1.0, 0.0, 0.0); |
| 36 | +#else |
| 37 | + result = float3(0.0, 0.0, 0.0); |
| 38 | +#endif |
| 39 | + break; |
| 40 | + |
| 41 | + case DEBUGVIEWPROPERTIES_PIXEL_DISPLACEMENT: |
| 42 | +#ifdef _PIXEL_DISPLACEMENT // Caution: This define is related to a shader features (But it may become a standard features for HD) |
| 43 | + result = float3(1.0, 0.0, 0.0); |
| 44 | +#else |
| 45 | + result = float3(0.0, 0.0, 0.0); |
| 46 | +#endif |
| 47 | + break; |
| 48 | + |
| 49 | + case DEBUGVIEWPROPERTIES_VERTEX_DISPLACEMENT: |
| 50 | +#ifdef _VERTEX_DISPLACEMENT // Caution: This define is related to a shader features (But it may become a standard features for HD) |
| 51 | + result = float3(1.0, 0.0, 0.0); |
| 52 | +#else |
| 53 | + result = float3(0.0, 0.0, 0.0); |
| 54 | +#endif |
| 55 | + break; |
| 56 | + |
| 57 | + case DEBUGVIEWPROPERTIES_TESSELLATION_DISPLACEMENT: |
| 58 | +#ifdef _TESSELLATION_DISPLACEMENT // Caution: This define is related to a shader features (But it may become a standard features for HD) |
| 59 | + result = float3(1.0, 0.0, 0.0); |
| 60 | +#else |
| 61 | + result = float3(0.0, 0.0, 0.0); |
| 62 | +#endif |
| 63 | + break; |
| 64 | + |
| 65 | + case DEBUGVIEWPROPERTIES_DEPTH_OFFSET: |
| 66 | +#ifdef _DEPTHOFFSET_ON // Caution: This define is related to a shader features (But it may become a standard features for HD) |
| 67 | + result = float3(1.0, 0.0, 0.0); |
| 68 | +#else |
| 69 | + result = float3(0.0, 0.0, 0.0); |
| 70 | +#endif |
| 71 | + break; |
| 72 | + |
| 73 | + case DEBUGVIEWPROPERTIES_LIGHTMAP: |
| 74 | +#if defined(LIGHTMAP_ON) || defined (DIRLIGHTMAP_COMBINED) || defined(DYNAMICLIGHTMAP_ON) |
| 75 | + result = float3(1.0, 0.0, 0.0); |
| 76 | +#else |
| 77 | + result = float3(0.0, 0.0, 0.0); |
| 78 | +#endif |
| 79 | + break; |
| 80 | + |
| 81 | + case DEBUGVIEWPROPERTIES_INSTANCING: |
| 82 | +#if defined(UNITY_INSTANCING_ENABLED) |
| 83 | + result = float3(1.0, 0.0, 0.0); |
| 84 | +#else |
| 85 | + result = float3(0.0, 0.0, 0.0); |
| 86 | +#endif |
| 87 | + break; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +float3 GetTextureDataDebug(uint paramId, float2 uv, Texture2D tex, float4 texelSize, float4 mipInfo, float3 originalColor) |
| 92 | +{ |
| 93 | + float3 outColor = originalColor; |
| 94 | + |
| 95 | + switch (paramId) |
| 96 | + { |
| 97 | + case DEBUGMIPMAPMODE_MIP_RATIO: |
| 98 | + outColor = GetDebugMipColorIncludingMipReduction(originalColor, tex, texelSize, uv, mipInfo); |
| 99 | + break; |
| 100 | + case DEBUGMIPMAPMODE_MIP_COUNT: |
| 101 | + outColor = GetDebugMipCountColor(originalColor, tex); |
| 102 | + break; |
| 103 | + case DEBUGMIPMAPMODE_MIP_COUNT_REDUCTION: |
| 104 | + outColor = GetDebugMipReductionColor(tex, mipInfo); |
| 105 | + break; |
| 106 | + case DEBUGMIPMAPMODE_STREAMING_MIP_BUDGET: |
| 107 | + outColor = GetDebugStreamingMipColor(tex, mipInfo); |
| 108 | + break; |
| 109 | + case DEBUGMIPMAPMODE_STREAMING_MIP: |
| 110 | + outColor = GetDebugStreamingMipColorBlended(originalColor, tex, mipInfo); |
| 111 | + break; |
| 112 | + } |
| 113 | + |
| 114 | + return outColor; |
| 115 | +} |
| 116 | + |
| 117 | +// DebugFont code assume black and white font with texture size 256x128 with bloc of 16x16 |
| 118 | +#define DEBUG_FONT_TEXT_WIDTH 16 |
| 119 | +#define DEBUG_FONT_TEXT_HEIGHT 16 |
| 120 | +#define DEBUG_FONT_TEXT_COUNT_X 16 |
| 121 | +#define DEBUG_FONT_TEXT_COUNT_Y 8 |
| 122 | +#define DEBUG_FONT_TEXT_ASCII_START 32 |
| 123 | + |
| 124 | +#define DEBUG_FONT_TEXT_SCALE_WIDTH 10 // This control the spacing between characters (if a character fill the text block it will overlap). |
| 125 | + |
| 126 | +// Only support ASCII symbol from DEBUG_FONT_TEXT_ASCII_START to 126 |
| 127 | +// return black or white depends if we hit font character or not |
| 128 | +// currentUnormCoord is current unormalized screen position |
| 129 | +// fixedUnormCoord is the position where we want to draw something, this will be incremented by block font size in provided direction |
| 130 | +// color is current screen color |
| 131 | +// color of the font to use |
| 132 | +// direction is 1 or -1 and indicate fixedUnormCoord block shift |
| 133 | +void DrawCharacter(uint asciiValue, float3 fontColor, uint2 currentUnormCoord, inout uint2 fixedUnormCoord, inout float3 color, int direction, int fontTextScaleWidth) |
| 134 | +{ |
| 135 | + // Are we inside a font display block on the screen ? |
| 136 | + uint2 localCharCoord = currentUnormCoord - fixedUnormCoord; |
| 137 | + if (localCharCoord.x >= 0 && localCharCoord.x < DEBUG_FONT_TEXT_WIDTH && localCharCoord.y >= 0 && localCharCoord.y < DEBUG_FONT_TEXT_HEIGHT) |
| 138 | + { |
| 139 | + localCharCoord.y = DEBUG_FONT_TEXT_HEIGHT - localCharCoord.y; |
| 140 | + |
| 141 | + asciiValue -= DEBUG_FONT_TEXT_ASCII_START; // Our font start at ASCII table 32; |
| 142 | + uint2 asciiCoord = uint2(asciiValue % DEBUG_FONT_TEXT_COUNT_X, asciiValue / DEBUG_FONT_TEXT_COUNT_X); |
| 143 | + // Unorm coordinate inside the font texture |
| 144 | + uint2 unormTexCoord = asciiCoord * uint2(DEBUG_FONT_TEXT_WIDTH, DEBUG_FONT_TEXT_HEIGHT) + localCharCoord; |
| 145 | + // normalized coordinate |
| 146 | + float2 normTexCoord = float2(unormTexCoord) / float2(DEBUG_FONT_TEXT_WIDTH * DEBUG_FONT_TEXT_COUNT_X, DEBUG_FONT_TEXT_HEIGHT * DEBUG_FONT_TEXT_COUNT_Y); |
| 147 | + |
| 148 | +#if UNITY_UV_STARTS_AT_TOP |
| 149 | + normTexCoord.y = 1.0 - normTexCoord.y; |
| 150 | +#endif |
| 151 | + |
| 152 | + float charColor = SAMPLE_TEXTURE2D_LOD(_DebugFont, s_point_clamp_sampler, normTexCoord, 0).r; |
| 153 | + color = color * (1.0 - charColor) + charColor * fontColor; |
| 154 | + } |
| 155 | + |
| 156 | + fixedUnormCoord.x += fontTextScaleWidth * direction; |
| 157 | +} |
| 158 | + |
| 159 | +void DrawCharacter(uint asciiValue, float3 fontColor, uint2 currentUnormCoord, inout uint2 fixedUnormCoord, inout float3 color, int direction) |
| 160 | +{ |
| 161 | + DrawCharacter(asciiValue, fontColor, currentUnormCoord, fixedUnormCoord, color, direction, DEBUG_FONT_TEXT_SCALE_WIDTH); |
| 162 | +} |
| 163 | + |
| 164 | +// Shortcut to not have to file direction |
| 165 | +void DrawCharacter(uint asciiValue, float3 fontColor, uint2 currentUnormCoord, inout uint2 fixedUnormCoord, inout float3 color) |
| 166 | +{ |
| 167 | + DrawCharacter(asciiValue, fontColor, currentUnormCoord, fixedUnormCoord, color, 1); |
| 168 | +} |
| 169 | + |
| 170 | +// Draw a signed integer |
| 171 | +// Can't display more than 16 digit |
| 172 | +// The two following parameter are for float representation |
| 173 | +// leading0 is used when drawing frac part of a float to draw the leading 0 (call is in charge of it) |
| 174 | +// forceNegativeSign is used to force to display a negative sign as -0 is not recognize |
| 175 | +void DrawInteger(int intValue, float3 fontColor, uint2 currentUnormCoord, inout uint2 fixedUnormCoord, inout float3 color, int leading0, bool forceNegativeSign) |
| 176 | +{ |
| 177 | + const uint maxStringSize = 16; |
| 178 | + |
| 179 | + uint absIntValue = abs(intValue); |
| 180 | + |
| 181 | + // 1. Get size of the number of display |
| 182 | + int numEntries = min((intValue == 0 ? 0 : log10(absIntValue)) + ((intValue < 0 || forceNegativeSign) ? 1 : 0) + leading0, maxStringSize); |
| 183 | + |
| 184 | + // 2. Shift curseur to last location as we will go reverse |
| 185 | + fixedUnormCoord.x += numEntries * DEBUG_FONT_TEXT_SCALE_WIDTH; |
| 186 | + |
| 187 | + // 3. Display the number |
| 188 | + bool drawCharacter = true; // bit weird, but it is to appease the compiler. |
| 189 | + for (uint j = 0; j < maxStringSize; ++j) |
| 190 | + { |
| 191 | + // Numeric value incurrent font start on the second row at 0 |
| 192 | + if(drawCharacter) |
| 193 | + DrawCharacter((absIntValue % 10) + '0', fontColor, currentUnormCoord, fixedUnormCoord, color, -1); |
| 194 | + |
| 195 | + if (absIntValue < 10) |
| 196 | + drawCharacter = false; |
| 197 | + |
| 198 | + absIntValue /= 10; |
| 199 | + } |
| 200 | + |
| 201 | + // 4. Display leading 0 |
| 202 | + if (leading0 > 0) |
| 203 | + { |
| 204 | + for (int i = 0; i < leading0; ++i) |
| 205 | + { |
| 206 | + DrawCharacter('0', fontColor, currentUnormCoord, fixedUnormCoord, color, -1); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + // 5. Display sign |
| 211 | + if (intValue < 0 || forceNegativeSign) |
| 212 | + { |
| 213 | + DrawCharacter('-', fontColor, currentUnormCoord, fixedUnormCoord, color, -1); |
| 214 | + } |
| 215 | + |
| 216 | + // 6. Reset cursor at end location |
| 217 | + fixedUnormCoord.x += (numEntries + 2) * DEBUG_FONT_TEXT_SCALE_WIDTH; |
| 218 | +} |
| 219 | + |
| 220 | +void DrawInteger(int intValue, float3 fontColor, uint2 currentUnormCoord, inout uint2 fixedUnormCoord, inout float3 color) |
| 221 | +{ |
| 222 | + DrawInteger(intValue, fontColor, currentUnormCoord, fixedUnormCoord, color, 0, false); |
| 223 | +} |
| 224 | + |
| 225 | +void DrawFloatExplicitPrecision(float floatValue, float3 fontColor, uint2 currentUnormCoord, uint digitCount, inout uint2 fixedUnormCoord, inout float3 color) |
| 226 | +{ |
| 227 | + if (IsNaN(floatValue)) |
| 228 | + { |
| 229 | + DrawCharacter('N', fontColor, currentUnormCoord, fixedUnormCoord, color); |
| 230 | + DrawCharacter('a', fontColor, currentUnormCoord, fixedUnormCoord, color); |
| 231 | + DrawCharacter('N', fontColor, currentUnormCoord, fixedUnormCoord, color); |
| 232 | + } |
| 233 | + else |
| 234 | + { |
| 235 | + int intValue = int(floatValue); |
| 236 | + bool forceNegativeSign = floatValue >= 0.0f ? false : true; |
| 237 | + DrawInteger(intValue, fontColor, currentUnormCoord, fixedUnormCoord, color, 0, forceNegativeSign); |
| 238 | + DrawCharacter('.', fontColor, currentUnormCoord, fixedUnormCoord, color); |
| 239 | + int fracValue = int(frac(abs(floatValue)) * pow(10, digitCount)); |
| 240 | + int leading0 = digitCount - (int(log10(fracValue)) + 1); // Counting leading0 to add in front of the float |
| 241 | + DrawInteger(fracValue, fontColor, currentUnormCoord, fixedUnormCoord, color, leading0, false); |
| 242 | + } |
| 243 | +} |
| 244 | + |
| 245 | +void DrawFloat(float floatValue, float3 fontColor, uint2 currentUnormCoord, inout uint2 fixedUnormCoord, inout float3 color) |
| 246 | +{ |
| 247 | + DrawFloatExplicitPrecision(floatValue, fontColor, currentUnormCoord, 6, fixedUnormCoord, color); |
| 248 | +} |
| 249 | + |
| 250 | +// Debug rendering is performed at the end of the frame (after post-processing). |
| 251 | +// Debug textures are never flipped upside-down automatically. Therefore, we must always flip manually. |
| 252 | +bool ShouldFlipDebugTexture() |
| 253 | +{ |
| 254 | + #if UNITY_UV_STARTS_AT_TOP |
| 255 | + return (_ProjectionParams.x > 0); |
| 256 | + #else |
| 257 | + return (_ProjectionParams.x < 0); |
| 258 | + #endif |
| 259 | +} |
| 260 | + |
| 261 | +#endif |
| 262 | + |
| 263 | +#endif // DEBUG_DISPLAY |
0 commit comments