Skip to content

Commit 9697995

Browse files
Olof-AVEvergreen
authored andcommitted
[HDRP] Fix Terrains with 4 layers or less displaying checker texture when using debug views
This PR fixes an issue where a checker texture would appear on Terrains with 4 layers or less when using, for example, the "Unlit Draw Mode". | Before | After | | :---: | :---: | | <img src="https://media.github.cds.internal.unity3d.com/user/3541/files/31f50cb2-4a40-49bb-b568-f586056a98d3" width="100%" height="100%"> | <img src="https://media.github.cds.internal.unity3d.com/user/3541/files/919c1beb-7d07-4dac-98aa-da7215e5e8f2" width="100%" height="100%"> | The issue was caused by https://github.cds.internal.unity3d.com/unity/unity/pull/38619, where we started force-enabling the `_TERRAIN_8_LAYERS` shader keyword whenever `DEBUG_DISPLAY` was defined. - For the mipmap debug views, no issue would ever appear since we only display debug information for 1 specific Terrain layer at a time. Because we forced `_TERRAIN_8_LAYERS`, debugging layers 4 to 7 on a 4-layered Terrain would thus display debug information for the default texture set by the Terrain system. (checker texture **_in editor_**, tiny white texture in player) - For all other debug views, the checker texture bug would appear on 4-layered Terrain because force-enabling `_TERRAIN_8_LAYERS` also means that the Terrain uses a second Control texture, but the Terrain system obviously has none to provide so a default grey texture was used instead → layers 4 / 5 / 6 / 7 thus suddenly appeared at half opacity on top of other layers. Of course, we could somehow ensure that the default texture used for the second Control texture is always fully black (feels quite sloppy though?), but I do feel like force-enabling the `_TERRAIN_8_LAYERS` shader keyword when debugging wasn't the best approach in the first place. - Forcefully enabling the keyword essentially means that 4 extra layers appear out of thin air. When debugging individual layers with the mipmap debug views, it is thus impossible to differentiate 4-layered Terrain vs 8-layered Terrain and might even give users the impression that HDRP Terrains are always 8-layered behind-the-scenes. - By reworking the code a little, we can still display appropriate mipmap debug information even if we have no layer texture available, so there is in fact no need to forcefully enable the keyword just to get a layer texture to sample / obtain mipcount from / ... I've thus simply reworked the mipmap debug views code (see 23f28b58b6ff61699def6be1ac64b0265d1fee9f) so that it displays "invalid" / "missing" information when debugging layers 4 / 5 / 6 / 7 on a 4-layered Terrain (we use white as "texture colour"). When using, for example, the `Mip Count` view to debug the 5th layer of a 4-layered Terrain, the result now looks like this: | Before | After | | :---: | :---: | | <img src="https://media.github.cds.internal.unity3d.com/user/3541/files/1ce3209f-dd83-4c6d-a313-0633fddb6800" width="100%" height="100%"> | <img src="https://media.github.cds.internal.unity3d.com/user/3541/files/058117fb-38b1-48ad-88ec-16cd5f47fbe1" width="100%" height="100%"> |
1 parent 8bfae03 commit 9697995

18 files changed

+334
-36
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,13 @@ void GetHatchedColor(uint2 screenSpaceCoords, inout float3 debugColor)
132132
// Keep in sync with CalculateColorForDebugMipmapStreaming in URP's ShaderLibrary/Debug/DebuggingCommon.hlsl
133133
#define TERRAIN_STREAM_INFO float4(0.0f, 0.0f, float(6 | (4 << 4)), 0.0f) // 0-15 are reserved for per-texture codes (use "6" to indicate terrain); per-material code "4" signifies "warnings/issues"
134134
#define GET_TEXTURE_STREAMING_DEBUG_FOR_TERRAIN_TEX(positionSS, streamingUv, tex) GetTextureDataDebug(_DebugMipMapMode, positionSS, streamingUv, tex, tex##_TexelSize, tex##_MipInfo, TERRAIN_STREAM_INFO)
135+
#define GET_TEXTURE_STREAMING_DEBUG_FOR_TERRAIN_NO_TEX(positionSS, streamingUv) GetTextureDataDebug(_DebugMipMapMode, positionSS, streamingUv, float3(1.0f, 1.0f, 1.0f), 0, float4(0.0f, 0.0f, 0.0f, 0.0f), float4(0.0f, 0.0f, 0.0f, 0.0f), TERRAIN_STREAM_INFO) // Used exclusively for layers 4-7 debug when terrain only has 4 layers
135136
#define GET_TEXTURE_STREAMING_DEBUG(positionSS, uv) GetTextureDataDebug(_DebugMipMapMode, positionSS, TRANSFORM_TEX(uv.xy, unity_MipmapStreaming_DebugTex), unity_MipmapStreaming_DebugTex, unity_MipmapStreaming_DebugTex_TexelSize, unity_MipmapStreaming_DebugTex_MipInfo, unity_MipmapStreaming_DebugTex_StreamInfo)
136137
#define GET_TEXTURE_STREAMING_DEBUG_NO_UV(positionSS) GetTextureDataDebug(_DebugMipMapMode, positionSS, float2(0.0f, 0.0f), unity_MipmapStreaming_DebugTex, unity_MipmapStreaming_DebugTex_TexelSize, unity_MipmapStreaming_DebugTex_MipInfo, unity_MipmapStreaming_DebugTex_StreamInfo)
137-
float3 GetTextureDataDebug(uint paramId, uint2 screenSpaceCoords, float2 uv, Texture2D tex, float4 texelSize, float4 mipInfo, float4 streamInfo)
138+
float3 GetTextureDataDebug(uint paramId, uint2 screenSpaceCoords, float2 uv, float3 originalColor, uint mipCount, float4 texelSize, float4 mipInfo, float4 streamInfo)
138139
{
139-
float3 originalColor = SAMPLE_TEXTURE2D(tex, s_linear_repeat_sampler, uv).xyz;
140140
float3 outColor = originalColor;
141141

142-
uint mipCount = GetMipCount(TEXTURE2D_ARGS(tex, s_point_clamp_sampler));
143-
144142
bool needsHatching;
145143
switch (paramId)
146144
{
@@ -193,6 +191,14 @@ float3 GetTextureDataDebug(uint paramId, uint2 screenSpaceCoords, float2 uv, Tex
193191
return lerp(originalColor, outColor, _DebugMipMapOpacity);
194192
}
195193

194+
float3 GetTextureDataDebug(uint paramId, uint2 screenSpaceCoords, float2 uv, Texture2D tex, float4 texelSize, float4 mipInfo, float4 streamInfo)
195+
{
196+
const float3 originalColor = SAMPLE_TEXTURE2D(tex, s_linear_repeat_sampler, uv).xyz;
197+
const uint mipCount = GetMipCount(TEXTURE2D_ARGS(tex, s_point_clamp_sampler));
198+
199+
return GetTextureDataDebug(paramId, screenSpaceCoords, uv, originalColor, mipCount, texelSize, mipInfo, streamInfo);
200+
}
201+
196202
// Draw a signed integer
197203
// Can't display more than 16 digit
198204
// The two following parameter are for float representation

Packages/com.unity.render-pipelines.high-definition/Runtime/Material/TerrainLit/TerrainLit.shader

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ Shader "HDRP/TerrainLit"
8383
// Only has effect if the global mip bias is enabled in shader config and DRS is enabled.
8484
#define SUPPORT_GLOBAL_MIP_BIAS
8585

86-
#ifdef DEBUG_DISPLAY
87-
// Mipmap debug views can inspect all 8 layers: this ensures that, even if
88-
// layers 4 / 5 / 6 / 7 aren't set up, we can display something reasonable.
89-
#define _TERRAIN_8_LAYERS
90-
#endif
91-
9286
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/TerrainLit/TerrainLit_Splatmap_Includes.hlsl"
9387

9488
ENDHLSL

Packages/com.unity.render-pipelines.high-definition/Runtime/Material/TerrainLit/TerrainLit_Splatmap.hlsl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,12 @@ void TerrainLitDebug(float2 uv, uint2 screenSpaceCoords, out float3 baseColor)
285285
{
286286
baseColor = GET_TEXTURE_STREAMING_DEBUG_FOR_TERRAIN_TEX(screenSpaceCoords, uv * _Splat7_ST.xy + _Splat7_ST.zw, _Splat7);
287287
}
288+
#else
289+
else
290+
{
291+
// User is trying to debug layer 4/5/6/7 but this terrain only has 4 layers: let's try to display some basic "invalid" debug info...
292+
baseColor = GET_TEXTURE_STREAMING_DEBUG_FOR_TERRAIN_NO_TEX(screenSpaceCoords, uv);
293+
}
288294
#endif
289295
#endif
290296
}

0 commit comments

Comments
 (0)