Skip to content

Commit bd04875

Browse files
GGijonUnitypastasfuture
authored andcommitted
Added Texel Density debug view (#28)
1 parent d2e9550 commit bd04875

File tree

18 files changed

+250
-4
lines changed

18 files changed

+250
-4
lines changed

com.unity.render-pipelines.high-definition/Editor/Material/ShaderGraph/Templates/ShaderPass.template

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,18 @@ Pass
290290
ApplyDebugToBuiltinData(builtinData);
291291
#endif
292292

293+
#ifdef DEBUG_DISPLAY
294+
if (_DebugFullScreenMode == FULLSCREENDEBUGMODE_TEXEL_DENSITY)
295+
{
296+
// ShaderGraph texel density debug relies on the convention that the main color texture is named _BaseColorMap
297+
float3 wsPosition = posInput.positionWS;
298+
float2 uv = fragInputs.texCoord0;
299+
float2 texDimension;
300+
_BaseColorMap.GetDimensions(texDimension.x, texDimension.y);
301+
surfaceData.baseColor.rgb = DebugTexelDensityColor(wsPosition, uv, texDimension);
302+
}
303+
#endif
304+
293305
RAY_TRACING_OPTIONAL_ALPHA_TEST_PASS
294306
}
295307

com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ unsafe struct ShaderVariablesDebugDisplay
3636
public Vector4 _MouseClickPixelCoord; // xy unorm, zw norm
3737
public Vector4 _DebugLutParams; // x: 1/width, y: 1/height, z: height-1, w: unused
3838
public Vector4 _DebugShowHeightMaps; // xyz = color to use for materials not using heightmaps, w = albedo blend
39+
public Vector4 _DebugTexelDensityTarget; // xyz = color for surfaces at the target density, w = target density
40+
public Vector4 _DebugTexelDensityLower; // xyx = color for surfaces below the target density, w = log2 step limit
41+
public Vector4 _DebugTexelDensityHigher; // xyx = color for surfaces above the target density, w = checkerboard texel size
3942

4043
public int _MatcapMixAlbedo;
4144
public float _MatcapViewScale;
@@ -123,6 +126,8 @@ public enum FullScreenDebugMode
123126
ValidateSpecularColor,
124127
/// <summary>Display material heightmaps.</summary>
125128
Heightmaps,
129+
/// <summary>Display texel density.</summary>
130+
TexelDensity,
126131
/// <summary>Maximum Full Screen Material debug mode value (used internally).</summary>
127132
MaxMaterialFullScreenDebug,
128133
// TODO: Move before count for 11.0
@@ -336,6 +341,7 @@ internal DebugDisplaySettings()
336341
s_MaterialFullScreenDebugStrings[(int)FullScreenDebugMode.ValidateDiffuseColor - ((int)FullScreenDebugMode.MinMaterialFullScreenDebug)] = new GUIContent("Diffuse Color");
337342
s_MaterialFullScreenDebugStrings[(int)FullScreenDebugMode.ValidateSpecularColor - ((int)FullScreenDebugMode.MinMaterialFullScreenDebug)] = new GUIContent("Metal or SpecularColor");
338343
s_MaterialFullScreenDebugStrings[(int)FullScreenDebugMode.Heightmaps - ((int)FullScreenDebugMode.MinMaterialFullScreenDebug)] = new GUIContent("Show Heightmaps");
344+
s_MaterialFullScreenDebugStrings[(int)FullScreenDebugMode.TexelDensity - ((int)FullScreenDebugMode.MinMaterialFullScreenDebug)] = new GUIContent("Texel Density");
339345

340346
s_MsaaSamplesDebugStrings = Enum.GetNames(typeof(MSAASamples))
341347
.Select(t => new GUIContent(t))
@@ -518,7 +524,7 @@ public bool IsDebugExposureModeEnabled()
518524
/// <returns>True if any material validation is enabled.</returns>
519525
public bool IsMaterialValidationEnabled()
520526
{
521-
return (data.fullScreenDebugMode == FullScreenDebugMode.ValidateDiffuseColor) || (data.fullScreenDebugMode == FullScreenDebugMode.ValidateSpecularColor) || (data.fullScreenDebugMode == FullScreenDebugMode.Heightmaps);
527+
return (data.fullScreenDebugMode == FullScreenDebugMode.ValidateDiffuseColor) || (data.fullScreenDebugMode == FullScreenDebugMode.ValidateSpecularColor) || (data.fullScreenDebugMode == FullScreenDebugMode.Heightmaps) || (data.fullScreenDebugMode == FullScreenDebugMode.TexelDensity);
522528
}
523529

524530
/// <summary>
@@ -1028,6 +1034,21 @@ void RegisterMaterialDebug()
10281034
}
10291035
});
10301036
}
1037+
else if (data.fullScreenDebugMode == FullScreenDebugMode.TexelDensity)
1038+
{
1039+
list.Add(new DebugUI.Container
1040+
{
1041+
children =
1042+
{
1043+
new DebugUI.FloatField { displayName = "Target Density", getter = () => data.materialDebugSettings.texelDensityTarget, setter = (v) => data.materialDebugSettings.texelDensityTarget = v, min = () => 256.0f, max = () => 16384.0f, },
1044+
new DebugUI.FloatField { displayName = "Log2 Step Limit", getter = () => data.materialDebugSettings.texelDensityLog2StepsLimit, setter = (v) => data.materialDebugSettings.texelDensityLog2StepsLimit = v, min = () => 1.0f, max = () => 8.0f, },
1045+
new DebugUI.ColorField { displayName = "Target Density Color", getter = () => data.materialDebugSettings.texelDensityTargetColor, setter = value => data.materialDebugSettings.texelDensityTargetColor = value, showAlpha = false, hdr = false },
1046+
new DebugUI.ColorField { displayName = "Lower Density Color", getter = () => data.materialDebugSettings.texelDensityLowerColor, setter = value => data.materialDebugSettings.texelDensityLowerColor = value, showAlpha = false, hdr = false },
1047+
new DebugUI.ColorField { displayName = "Higher Density Color", getter = () => data.materialDebugSettings.texelDensityHigherColor, setter = value => data.materialDebugSettings.texelDensityHigherColor = value, showAlpha = false, hdr = false },
1048+
new DebugUI.FloatField { displayName = "Checkerboard Texels", getter = () => data.materialDebugSettings.texelDensityCheckerboardTexels, setter = (v) => data.materialDebugSettings.texelDensityCheckerboardTexels = v, min = () => 1.0f, max = () => 4096.0f, },
1049+
}
1050+
});
1051+
}
10311052

10321053
m_DebugMaterialItems = list.ToArray();
10331054
var panel = DebugManager.instance.GetPanel(k_PanelMaterials, true);

com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.cs.hlsl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@
3838
#define FULLSCREENDEBUGMODE_VALIDATE_DIFFUSE_COLOR (28)
3939
#define FULLSCREENDEBUGMODE_VALIDATE_SPECULAR_COLOR (29)
4040
#define FULLSCREENDEBUGMODE_HEIGHTMAPS (30)
41-
#define FULLSCREENDEBUGMODE_MAX_MATERIAL_FULL_SCREEN_DEBUG (31)
42-
#define FULLSCREENDEBUGMODE_SCREEN_SPACE_REFLECTIONS_PREV (32)
43-
#define FULLSCREENDEBUGMODE_SCREEN_SPACE_REFLECTIONS_ACCUM (33)
41+
#define FULLSCREENDEBUGMODE_TEXEL_DENSITY (31)
42+
#define FULLSCREENDEBUGMODE_MAX_MATERIAL_FULL_SCREEN_DEBUG (32)
43+
#define FULLSCREENDEBUGMODE_SCREEN_SPACE_REFLECTIONS_PREV (33)
44+
#define FULLSCREENDEBUGMODE_SCREEN_SPACE_REFLECTIONS_ACCUM (34)
4445

4546
// Generated from UnityEngine.Rendering.HighDefinition.ShaderVariablesDebugDisplay
4647
// PackingRules = Exact
@@ -68,6 +69,9 @@ CBUFFER_START(ShaderVariablesDebugDisplay)
6869
float4 _MouseClickPixelCoord;
6970
float4 _DebugLutParams;
7071
float4 _DebugShowHeightMaps;
72+
float4 _DebugTexelDensityTarget;
73+
float4 _DebugTexelDensityLower;
74+
float4 _DebugTexelDensityHigher;
7175
int _MatcapMixAlbedo;
7276
float _MatcapViewScale;
7377
int _DebugSingleShadowIndex;

com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.hlsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ static SHADOW_TYPE g_DebugShadowAttenuation = 0;
1818
StructuredBuffer<int2> _DebugDepthPyramidOffsets;
1919

2020
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Debug/PBRValidator.hlsl"
21+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Debug/TexelDensity.hlsl"
2122

2223
// When displaying lux meter we compress the light in order to be able to display value higher than 65504
2324
// The sun is between 100 000 and 150 000, so we use 4 to be able to cover such a range (4 * 65504)

com.unity.render-pipelines.high-definition/Runtime/Debug/MaterialDebug.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,19 @@ static void BuildDebugRepresentation()
445445
public Color showHeightMapsDefaultColor = new Color(0.5f, 0.5f, 0.5f);
446446
/// <summary>How much from the albedo map is used to tint the heightmap.</summary>
447447
public float showHeightMapsBlendAlbedo = 0.2f;
448+
/// <summary>Target texel density.</summary>
449+
public float texelDensityTarget = 1024.0f;
450+
/// <summary>How many orders of magnitude (in base 2) away from the target are considered within the acceptable margin.</summary>
451+
/// Eg: With TexelDensityTarget=1024 and Log2StepsLimit=2, the valid range would be between 256 and 4096
452+
public float texelDensityLog2StepsLimit = 2.0f;
453+
/// <summary>Color for displaying surfaces at the target texel density.</summary>
454+
public Color texelDensityTargetColor = new Color(0.0f, 0.0f, 1.0f);
455+
/// <summary>Color for displaying surfaces below the target texel density.</summary>
456+
public Color texelDensityLowerColor = new Color(1.0f, 0.0f, 0.0f);
457+
/// <summary>Color for displaying surfaces above the target texel density.</summary>
458+
public Color texelDensityHigherColor = new Color(0.0f, 1.0f, 0.0f);
459+
/// <summary>How many texels each square of the checkerboard represents.</summary>
460+
public float texelDensityCheckerboardTexels = 32.0f;
448461

449462
/// <summary>
450463
/// Current Debug View Material.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#ifdef DEBUG_DISPLAY
2+
3+
#ifndef UNITY_DEBUG_TEXEL_DENSITY_INCLUDED
4+
#define UNITY_DEBUG_TEXEL_DENSITY_INCLUDED
5+
6+
static const float checkerboardDark = 0.6;
7+
static const float checkerboardBright = 0.8;
8+
9+
float DrawCheckerboard(float2 pixelPosition, uint checkerboardSize)
10+
{
11+
// Draws a checkerboard based on the absolute pixel positions (pixelPosition = uv * texSize)
12+
uint2 checkerboardIndexes = pixelPosition / checkerboardSize;
13+
return ((checkerboardIndexes.x) % 2) == ((checkerboardIndexes.y) % 2) ? checkerboardBright : checkerboardDark;
14+
}
15+
16+
float3 CalculateTexelDensityDebugColor
17+
(
18+
float texelDensity,
19+
float targetDensity,
20+
float log2stepsLimit,
21+
float3 targetColor,
22+
float3 lowerColor,
23+
float3 higherColor
24+
)
25+
{
26+
// Validation:
27+
// <-1: Lower density than the minimum allowed
28+
// [-1,0): Low density
29+
// 0: Target density
30+
// (0,1]: High density
31+
// >1: Higher density than the maximum allowed
32+
float validation = log2(texelDensity / targetDensity) / log2stepsLimit;
33+
34+
float3 debugColor = targetColor;
35+
if (validation < 0)
36+
debugColor = lerp(debugColor, lowerColor, saturate(-validation));
37+
else
38+
debugColor = lerp(debugColor, higherColor, saturate(validation));
39+
return debugColor;
40+
}
41+
42+
float3 DebugTexelDensityColor
43+
(
44+
float3 worldPosition,
45+
float2 uv,
46+
float2 texDimension
47+
)
48+
{
49+
// CB Constants
50+
float targetDensity = _DebugTexelDensityTarget.w;
51+
float log2stepsLimit = _DebugTexelDensityLower.w;
52+
uint checkerboardPixels = _DebugTexelDensityHigher.w;
53+
float3 targetTDColor = _DebugTexelDensityTarget.rgb;
54+
float3 lowerTDColor = _DebugTexelDensityLower.rgb;
55+
float3 higherTDColor = _DebugTexelDensityHigher.rgb;
56+
57+
// Texel density calculation
58+
float3 worldDeltaX = ddx(worldPosition);
59+
float3 worldDeltaY = ddy(worldPosition);
60+
float worldSurface = length(worldDeltaX) * length(worldDeltaY);
61+
62+
float2 texelDeltaX = ddx(uv) * texDimension;
63+
float2 texelDeltaY = ddy(uv) * texDimension;
64+
float texelSurface = length(texelDeltaX) * length(texelDeltaY);
65+
66+
float texelDensity = sqrt(texelSurface / worldSurface);
67+
68+
// Colors
69+
float3 debugColor = CalculateTexelDensityDebugColor(
70+
texelDensity,
71+
targetDensity,
72+
log2stepsLimit,
73+
targetTDColor,
74+
lowerTDColor,
75+
higherTDColor);
76+
77+
float checkerboard = DrawCheckerboard(uv * texDimension, checkerboardPixels);
78+
79+
return debugColor * checkerboard;
80+
}
81+
82+
#endif // UNITY_DEBUG_TEXEL_DENSITY_INCLUDED
83+
#endif // DEBUG_DISPLAY

com.unity.render-pipelines.high-definition/Runtime/Debug/TexelDensity.hlsl.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxF.hlsl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ float4 GetHeightMapDebug(SurfaceData surfaceData)
345345
return float4(surfaceData.diffuseColor, 1);
346346
}
347347

348+
float4 GetTexelDensityDebug(SurfaceData surfaceData)
349+
{
350+
return float4(surfaceData.diffuseColor, 1);
351+
}
352+
348353

349354
// This function is used to help with debugging and must be implemented by any lit material
350355
// Implementer must take into account what are the current override component and

com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxFData.hlsl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,15 @@ void GetSurfaceAndBuiltinData(FragInputs input, float3 V, inout PositionInputs p
678678
#endif
679679
surfaceData.diffuseColor = lerp(heightmap, surfaceData.diffuseColor, _DebugShowHeightMaps.a);
680680
}
681+
682+
if (_DebugFullScreenMode == FULLSCREENDEBUGMODE_TEXEL_DENSITY)
683+
{
684+
float3 wsPosition = posInput.positionWS;
685+
float2 uv = uvMapping.uvBase;
686+
float2 texDimension;
687+
_SVBRDF_DiffuseColorMap.GetDimensions(texDimension.x, texDimension.y);
688+
surfaceData.diffuseColor = DebugTexelDensityColor(wsPosition, uv, texDimension);
689+
}
681690
#endif
682691

683692
// -------------------------------------------------------------

com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLitData.hlsl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,27 @@ void GetSurfaceAndBuiltinData(FragInputs input, float3 V, inout PositionInputs p
847847

848848
surfaceData.baseColor = lerp(heightmap, surfaceData.baseColor, _DebugShowHeightMaps.a);
849849
}
850+
851+
852+
if (_DebugFullScreenMode == FULLSCREENDEBUGMODE_TEXEL_DENSITY)
853+
{
854+
float3 wsPosition = posInput.positionWS;
855+
float2 texDimension;
856+
857+
_BaseColorMap0.GetDimensions(texDimension.x, texDimension.y);
858+
float3 texDensity0 = DebugTexelDensityColor(wsPosition, layerTexCoord.base0.uv, texDimension);
859+
860+
_BaseColorMap1.GetDimensions(texDimension.x, texDimension.y);
861+
float3 texDensity1 = DebugTexelDensityColor(wsPosition, layerTexCoord.base1.uv, texDimension);
862+
863+
_BaseColorMap2.GetDimensions(texDimension.x, texDimension.y);
864+
float3 texDensity2 = DebugTexelDensityColor(wsPosition, layerTexCoord.base2.uv, texDimension);
865+
866+
_BaseColorMap3.GetDimensions(texDimension.x, texDimension.y);
867+
float3 texDensity3 = DebugTexelDensityColor(wsPosition, layerTexCoord.base3.uv, texDimension);
868+
869+
surfaceData.baseColor = BlendLayeredVector3(texDensity0, texDensity1, texDensity2, texDensity3, weights);
870+
}
850871
#endif
851872

852873
// By default we use the ambient occlusion with Tri-ace trick (apply outside) for specular occlusion.

0 commit comments

Comments
 (0)