Skip to content

Commit b008e10

Browse files
author
runes
committed
Tile debugging 2.0
1 parent 1eebdb8 commit b008e10

File tree

3 files changed

+118
-34
lines changed

3 files changed

+118
-34
lines changed

Assets/ScriptableRenderLoop/HDRenderLoop/Debug/Resources/DebugViewMaterialGBuffer.shader

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Shader "Hidden/HDRenderLoop/DebugViewMaterialGBuffer"
2828
DECLARE_GBUFFER_TEXTURE(_GBufferTexture);
2929

3030
TEXTURE2D(_CameraDepthTexture);
31-
SAMPLER2D(sampler_CameraDepthTexture);
31+
SAMPLER2D(sampler_CameraDepthTexture);
3232
int _DebugViewMaterial;
3333

3434
struct Attributes
@@ -53,8 +53,8 @@ Shader "Hidden/HDRenderLoop/DebugViewMaterialGBuffer"
5353

5454
float4 Frag(Varyings input) : SV_Target
5555
{
56-
float4 unPositionSS = input.positionCS; // as input we have the vpos
57-
Coordinate coord = GetCoordinate(unPositionSS.xy, _ScreenSize.zw);
56+
float4 unPositionSS = input.positionCS; // as input we have the vpos
57+
Coordinate coord = GetCoordinate(unPositionSS.xy, _ScreenSize.zw);
5858

5959
float depth = LOAD_TEXTURE2D(_CameraDepthTexture, coord.unPositionSS).x;
6060

Assets/ScriptableRenderLoop/HDRenderLoop/Debug/Resources/DebugViewTiles.shader

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,25 @@ Shader "Hidden/HDRenderLoop/DebugViewTiles"
3838
// variable declaration
3939
//-------------------------------------------------------------------------------------
4040

41-
uint _ViewTilesFlags;
42-
4341
TEXTURE2D(_CameraDepthTexture);
4442
SAMPLER2D(sampler_CameraDepthTexture);
4543

4644
float4x4 _InvViewProjMatrix;
4745

46+
uint _ViewTilesFlags;
47+
float2 _MousePixelCoord;
48+
49+
4850
float4 Vert(float3 positionOS : POSITION): SV_POSITION
4951
{
5052
return TransformWorldToHClip(TransformObjectToWorld(positionOS));
5153
}
5254

55+
float4 AlphaBlend(float4 c0, float4 c1) // c1 over c0
56+
{
57+
return float4(lerp(c0.rgb, c1.rgb, c1.a), c0.a + c1.a - c0.a * c1.a);
58+
}
59+
5360
float4 OverlayHeatMap(uint2 pixCoord, uint numLights)
5461
{
5562
const float4 kRadarColors[12] =
@@ -90,15 +97,20 @@ Shader "Hidden/HDRenderLoop/DebugViewTiles"
9097
float4 Frag(float4 positionCS : SV_POSITION) : SV_Target
9198
{
9299
Coordinate coord = GetCoordinate(positionCS.xy, _ScreenSize.zw);
93-
94-
#ifdef USE_CLUSTERED_LIGHTLIST
100+
101+
int2 pixelCoord = coord.unPositionSS.xy;
102+
int2 tileCoord = pixelCoord / TILE_SIZE;
103+
int2 mouseTileCoord = _MousePixelCoord / TILE_SIZE;
104+
int2 offsetInTile = pixelCoord - tileCoord * TILE_SIZE;
105+
106+
#ifdef USE_CLUSTERED_LIGHTLIST
95107
// Perform same calculation than in deferred.shader
96108
float depth = LOAD_TEXTURE2D(_CameraDepthTexture, coord.unPositionSS).x;
97109
float3 positionWS = UnprojectToWorld(depth, coord.positionSS, _InvViewProjMatrix);
98110
float linearDepth = TransformWorldToView(positionWS).z; // View space linear depth
99-
#else
111+
#else
100112
float linearDepth = 0.0; // unused
101-
#endif
113+
#endif
102114

103115
int n = 0;
104116
for (int category = 0; category < LIGHTCATEGORY_COUNT; category++)
@@ -113,14 +125,59 @@ Shader "Hidden/HDRenderLoop/DebugViewTiles"
113125
}
114126
}
115127

128+
float4 result = 0.0f;
129+
130+
// Tile overlap counter
116131
if (n > 0)
117132
{
118-
return OverlayHeatMap(int2(coord.unPositionSS.xy) & 15, n);
133+
result = OverlayHeatMap(int2(coord.unPositionSS.xy) & (TILE_SIZE - 1), n);
134+
}
135+
136+
// Highlight selected tile
137+
if(all(mouseTileCoord == tileCoord))
138+
{
139+
bool border = any(offsetInTile == 0 || offsetInTile == TILE_SIZE - 1);
140+
float4 result2 = float4(1, 1, 1, border ? 1.0f : 0.5);
141+
result = AlphaBlend(result, result2);
119142
}
120-
else
143+
144+
// Print light lists for selected tile at the bottom of the screen
145+
int maxLights = 32;
146+
if(tileCoord.y < LIGHTCATEGORY_COUNT && tileCoord.x < maxLights + 3)
121147
{
122-
return 0.0;
148+
Coordinate mouseCoord = GetCoordinate(_MousePixelCoord, _ScreenSize.zw);
149+
150+
int category = (LIGHTCATEGORY_COUNT - 1) - tileCoord.y;
151+
int start;
152+
int count;
153+
GetCountAndStart(mouseCoord, category, linearDepth, start, count);
154+
155+
float4 result2 = float4(.1,.1,.1,.9);
156+
int2 fontCoord = int2(pixelCoord.x, offsetInTile.y);
157+
int lightListIndex = tileCoord.x - 2;
158+
159+
int n = -1;
160+
if(tileCoord.x == 0)
161+
{
162+
n = count;
163+
}
164+
else if(lightListIndex >= 0 && lightListIndex < count)
165+
{
166+
n = FetchIndex(start, lightListIndex);
167+
}
168+
169+
if(n >= 0)
170+
{
171+
if(SampleDebugFontNumber(offsetInTile, n))
172+
result2 = float4(0, 0, 0, 1);
173+
if(SampleDebugFontNumber(offsetInTile + 1, n))
174+
result2 = float4(1, 1, 1, 1);
175+
}
176+
177+
result = AlphaBlend(result, result2);
123178
}
179+
180+
return result;
124181
}
125182

126183
ENDHLSL

Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/TilePass/TilePass.cs

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ public struct LightVolumeData
8282

8383
public class LightLoop : BaseLightLoop
8484
{
85-
public const int k_MaxDirectionalLightsOnSCreen = 10;
86-
public const int k_MaxPunctualLightsOnSCreen = 512;
85+
public const int k_MaxDirectionalLightsOnScreen = 10;
86+
public const int k_MaxPunctualLightsOnScreen = 512;
8787
public const int k_MaxAreaLightsOnSCreen = 128;
88-
public const int k_MaxLightsOnSCreen = k_MaxDirectionalLightsOnSCreen + k_MaxPunctualLightsOnSCreen + k_MaxAreaLightsOnSCreen;
89-
public const int k_MaxEnvLightsOnSCreen = 64;
88+
public const int k_MaxLightsOnScreen = k_MaxDirectionalLightsOnScreen + k_MaxPunctualLightsOnScreen + k_MaxAreaLightsOnSCreen;
89+
public const int k_MaxEnvLightsOnScreen = 64;
9090
public const int k_MaxShadowOnScreen = 16;
9191
public const int k_MaxCascadeCount = 4; //Should be not less than m_Settings.directionalLightCascadeCount;
9292

@@ -214,9 +214,9 @@ public override void Build(TextureSettings textureSettings)
214214
m_lightList = new LightList();
215215
m_lightList.Allocate();
216216

217-
s_DirectionalLightDatas = new ComputeBuffer(k_MaxDirectionalLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(DirectionalLightData)));
218-
s_LightDatas = new ComputeBuffer(k_MaxPunctualLightsOnSCreen + k_MaxAreaLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(LightData)));
219-
s_EnvLightDatas = new ComputeBuffer(k_MaxEnvLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(EnvLightData)));
217+
s_DirectionalLightDatas = new ComputeBuffer(k_MaxDirectionalLightsOnScreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(DirectionalLightData)));
218+
s_LightDatas = new ComputeBuffer(k_MaxPunctualLightsOnScreen + k_MaxAreaLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(LightData)));
219+
s_EnvLightDatas = new ComputeBuffer(k_MaxEnvLightsOnScreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(EnvLightData)));
220220
s_shadowDatas = new ComputeBuffer(k_MaxCascadeCount + k_MaxShadowOnScreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(ShadowData)));
221221

222222
m_CookieTexArray = new TextureCache2D();
@@ -233,9 +233,9 @@ public override void Build(TextureSettings textureSettings)
233233

234234
s_GenAABBKernel = buildScreenAABBShader.FindKernel("ScreenBoundsAABB");
235235
s_GenListPerTileKernel = buildPerTileLightListShader.FindKernel(enableBigTilePrepass ? "TileLightListGen_SrcBigTile" : "TileLightListGen");
236-
s_AABBBoundsBuffer = new ComputeBuffer(2 * k_MaxLightsOnSCreen, 3 * sizeof(float));
237-
s_ConvexBoundsBuffer = new ComputeBuffer(k_MaxLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(SFiniteLightBound)));
238-
s_LightVolumeDataBuffer = new ComputeBuffer(k_MaxLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(LightVolumeData)));
236+
s_AABBBoundsBuffer = new ComputeBuffer(2 * k_MaxLightsOnScreen, 3 * sizeof(float));
237+
s_ConvexBoundsBuffer = new ComputeBuffer(k_MaxLightsOnScreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(SFiniteLightBound)));
238+
s_LightVolumeDataBuffer = new ComputeBuffer(k_MaxLightsOnScreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(LightVolumeData)));
239239

240240
buildScreenAABBShader.SetBuffer(s_GenAABBKernel, "g_data", s_ConvexBoundsBuffer);
241241
buildPerTileLightListShader.SetBuffer(s_GenListPerTileKernel, "g_vBoundsBuffer", s_AABBBoundsBuffer);
@@ -285,11 +285,20 @@ public override void Build(TextureSettings textureSettings)
285285
m_DebugViewTilesMaterial = Utilities.CreateEngineMaterial("Hidden/HDRenderLoop/DebugViewTiles");
286286

287287
m_SingleDeferredMaterial = Utilities.CreateEngineMaterial("Hidden/HDRenderLoop/Deferred");
288-
m_SingleDeferredMaterial.EnableKeyword("LIGHTLOOP_SINGLE_PASS");
288+
m_SingleDeferredMaterial.EnableKeyword("LIGHTLOOP_SINGLE_PASS");
289+
290+
#if UNITY_EDITOR
291+
UnityEditor.SceneView.onSceneGUIDelegate -= OnSceneGUI;
292+
UnityEditor.SceneView.onSceneGUIDelegate += OnSceneGUI;
293+
#endif
289294
}
290295

291296
public override void Cleanup()
292-
{
297+
{
298+
#if UNITY_EDITOR
299+
UnityEditor.SceneView.onSceneGUIDelegate -= OnSceneGUI;
300+
#endif
301+
293302
Utilities.SafeRelease(s_DirectionalLightDatas);
294303
Utilities.SafeRelease(s_LightDatas);
295304
Utilities.SafeRelease(s_EnvLightDatas);
@@ -827,7 +836,7 @@ public override void PrepareLightsForGPU(CullResults cullResults, Camera camera,
827836
int punctualLightcount = 0;
828837
int areaLightCount = 0;
829838

830-
var sortKeys = new uint[Math.Min(cullResults.visibleLights.Length, k_MaxLightsOnSCreen)];
839+
var sortKeys = new uint[Math.Min(cullResults.visibleLights.Length, k_MaxLightsOnScreen)];
831840
int sortCount = 0;
832841

833842
for (int lightIndex = 0, numLights = cullResults.visibleLights.Length; lightIndex < numLights; ++lightIndex)
@@ -853,7 +862,7 @@ public override void PrepareLightsForGPU(CullResults cullResults, Camera camera,
853862
switch (light.lightType)
854863
{
855864
case LightType.Point:
856-
if (punctualLightcount >= k_MaxPunctualLightsOnSCreen)
865+
if (punctualLightcount >= k_MaxPunctualLightsOnScreen)
857866
continue;
858867
lightCategory = LightCategory.Punctual;
859868
gpuLightType = GPULightType.Point;
@@ -862,7 +871,7 @@ public override void PrepareLightsForGPU(CullResults cullResults, Camera camera,
862871
break;
863872

864873
case LightType.Spot:
865-
if (punctualLightcount >= k_MaxPunctualLightsOnSCreen)
874+
if (punctualLightcount >= k_MaxPunctualLightsOnScreen)
866875
continue;
867876
lightCategory = LightCategory.Punctual;
868877
gpuLightType = GPULightType.Spot;
@@ -871,7 +880,7 @@ public override void PrepareLightsForGPU(CullResults cullResults, Camera camera,
871880
break;
872881

873882
case LightType.Directional:
874-
if (directionalLightcount >= k_MaxDirectionalLightsOnSCreen)
883+
if (directionalLightcount >= k_MaxDirectionalLightsOnScreen)
875884
continue;
876885
lightCategory = LightCategory.Punctual;
877886
gpuLightType = GPULightType.Directional;
@@ -967,14 +976,14 @@ public override void PrepareLightsForGPU(CullResults cullResults, Camera camera,
967976
// Redo everything but this time with envLights
968977
int envLightCount = 0;
969978

970-
sortKeys = new uint[Math.Min(cullResults.visibleReflectionProbes.Length, k_MaxEnvLightsOnSCreen)];
979+
sortKeys = new uint[Math.Min(cullResults.visibleReflectionProbes.Length, k_MaxEnvLightsOnScreen)];
971980
sortCount = 0;
972981

973982
for (int probeIndex = 0, numProbes = cullResults.visibleReflectionProbes.Length; probeIndex < numProbes; probeIndex++)
974983
{
975984
var probe = cullResults.visibleReflectionProbes[probeIndex];
976985

977-
if (envLightCount >= k_MaxEnvLightsOnSCreen)
986+
if (envLightCount >= k_MaxEnvLightsOnScreen)
978987
continue;
979988

980989
// TODO: Support LightVolumeType.Sphere, currently in UI there is no way to specify a sphere influence volume
@@ -1182,15 +1191,32 @@ public override void PushGlobalParams(Camera camera, RenderLoop loop)
11821191

11831192
loop.ExecuteCommandBuffer(cmd);
11841193
cmd.Dispose();
1185-
}
1186-
1194+
}
1195+
1196+
#if UNITY_EDITOR
1197+
private Vector2 m_mousePosition = Vector2.zero;
1198+
private void OnSceneGUI(UnityEditor.SceneView sceneview)
1199+
{
1200+
m_mousePosition = Event.current.mousePosition;
1201+
}
1202+
#endif
1203+
11871204
public override void RenderDeferredLighting(Camera camera, RenderLoop renderLoop, RenderTargetIdentifier cameraColorBufferRT)
11881205
{
11891206
var bUseClusteredForDeferred = !usingFptl;
11901207

11911208
var invViewProj = Utilities.GetViewProjectionMatrix(camera).inverse;
1192-
var screenSize = Utilities.ComputeScreenSize(camera);
1193-
1209+
var screenSize = Utilities.ComputeScreenSize(camera);
1210+
1211+
Vector2 mousePixelCoord = Input.mousePosition;
1212+
#if UNITY_EDITOR
1213+
if (!UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
1214+
{
1215+
mousePixelCoord = m_mousePosition;
1216+
mousePixelCoord.y = (screenSize.y - 1.0f) - mousePixelCoord.y;
1217+
}
1218+
#endif
1219+
11941220
m_DeferredDirectMaterial.SetMatrix("_InvViewProjMatrix", invViewProj);
11951221
m_DeferredDirectMaterial.SetVector("_ScreenSize", screenSize);
11961222
m_DeferredDirectMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
@@ -1215,6 +1241,7 @@ public override void RenderDeferredLighting(Camera camera, RenderLoop renderLoop
12151241
m_DebugViewTilesMaterial.SetMatrix("_InvViewProjMatrix", invViewProj);
12161242
m_DebugViewTilesMaterial.SetVector("_ScreenSize", screenSize);
12171243
m_DebugViewTilesMaterial.SetInt("_ViewTilesFlags", debugViewTilesFlags);
1244+
m_DebugViewTilesMaterial.SetVector("_MousePixelCoord", mousePixelCoord);
12181245
m_DebugViewTilesMaterial.EnableKeyword(bUseClusteredForDeferred ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST");
12191246
m_DebugViewTilesMaterial.DisableKeyword(!bUseClusteredForDeferred ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST");
12201247

0 commit comments

Comments
 (0)