Skip to content

Commit 67c40bc

Browse files
kennytannEvergreen
authored andcommitted
[UUM-116276][6000.4][2D] Fix Rendering Debug views for Rendergraph2D
1 parent 9acf106 commit 67c40bc

File tree

13 files changed

+97
-29
lines changed

13 files changed

+97
-29
lines changed

Packages/com.unity.render-pipelines.universal/Editor/2D/ShaderGraph/Includes/SpriteLitPass.hlsl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ PackedVaryings vert(Attributes input)
1212
input.positionOS = UnityFlipSprite(input.positionOS, unity_SpriteProps.xy);
1313
output = BuildVaryings(input);
1414
output.color *= _RendererColor * unity_SpriteColor; // vertex color has to applied here
15+
#if defined(DEBUG_DISPLAY)
16+
output.normalWS = TransformObjectToWorldNormal(input.normalOS);
17+
#endif
1518
PackedVaryings packedOutput = PackVaryings(output);
1619
return packedOutput;
1720
}
@@ -40,11 +43,12 @@ half4 frag(PackedVaryings packedInput) : SV_TARGET
4043
#endif
4144

4245
SurfaceData2D surfaceData;
43-
InitializeSurfaceData(color.rgb, color.a, surfaceDescription.SpriteMask, surfaceData);
46+
InitializeSurfaceData(color.rgb, color.a, surfaceDescription.SpriteMask, surfaceDescription.NormalTS, surfaceData);
4447
InputData2D inputData;
4548
InitializeInputData(unpacked.texCoord0.xy, half2(unpacked.screenPosition.xy / unpacked.screenPosition.w), inputData);
4649
#if defined(DEBUG_DISPLAY)
4750
SETUP_DEBUG_DATA_2D(inputData, unpacked.positionWS, unpacked.positionCS);
51+
surfaceData.normalWS = unpacked.normalWS;
4852
#endif
4953

5054
return CombinedShapeLightShared(surfaceData, inputData);

Packages/com.unity.render-pipelines.universal/Editor/2D/ShaderGraph/Includes/SpriteUnlitPass.hlsl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ PackedVaryings vert(Attributes input)
1313
input.positionOS = UnityFlipSprite(input.positionOS, unity_SpriteProps.xy);
1414
output = BuildVaryings(input);
1515
output.color *= _RendererColor * unity_SpriteColor; // vertex color has to applied here
16+
#if defined(DEBUG_DISPLAY)
17+
output.normalWS = TransformObjectToWorldNormal(input.normalOS);
18+
#endif
1619
PackedVaryings packedOutput = PackVaryings(output);
1720
return packedOutput;
1821
}
@@ -38,9 +41,13 @@ half4 frag(PackedVaryings packedInput) : SV_TARGET
3841
clip(color.a - surfaceDescription.AlphaClipThreshold);
3942
#endif
4043

41-
#if defined(DEBUG_DISPLAY)
44+
#if defined(DEBUG_DISPLAY)
4245
SurfaceData2D surfaceData;
4346
InitializeSurfaceData(color.rgb, color.a, surfaceData);
47+
surfaceData.normalWS = unpacked.normalWS;
48+
#if (SHADERPASS == SHADERPASS_SPRITELIT)
49+
surfaceData.normalTS = surfaceDescription.NormalTS;
50+
#endif
4451
InputData2D inputData;
4552
InitializeInputData(unpacked.positionWS.xy, half2(unpacked.texCoord0.xy), inputData);
4653
half4 debugColor = 0;
@@ -51,7 +58,7 @@ half4 frag(PackedVaryings packedInput) : SV_TARGET
5158
{
5259
return debugColor;
5360
}
54-
#endif
61+
#endif
5562

5663
// Disable vertex color multiplication. Users can get the color from VertexColor node
5764
#if !defined(HAVE_VFX_MODIFICATION) && !defined(_DISABLE_COLOR_TINT)

Packages/com.unity.render-pipelines.universal/Editor/2D/ShaderGraph/Targets/UniversalSpriteCustomLitSubTarget.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,16 @@ static class SpriteLitBlockMasks
224224
{
225225
BlockFields.SurfaceDescription.BaseColor,
226226
BlockFields.SurfaceDescriptionLegacy.SpriteColor,
227-
BlockFields.SurfaceDescription.Alpha,
228227
UniversalBlockFields.SurfaceDescription.SpriteMask,
228+
BlockFields.SurfaceDescription.NormalTS,
229+
BlockFields.SurfaceDescription.Alpha,
229230
BlockFields.SurfaceDescription.AlphaClipThreshold,
230231
};
231232

232233
public static BlockFieldDescriptor[] FragmentNormal = new BlockFieldDescriptor[]
233234
{
234-
BlockFields.SurfaceDescription.Alpha,
235235
BlockFields.SurfaceDescription.NormalTS,
236+
BlockFields.SurfaceDescription.Alpha,
236237
BlockFields.SurfaceDescription.AlphaClipThreshold,
237238
};
238239

@@ -255,6 +256,7 @@ static class SpriteLitRequiredFields
255256
StructFields.Varyings.positionWS,
256257
StructFields.Varyings.texCoord0,
257258
StructFields.Varyings.screenPosition,
259+
StructFields.Varyings.normalWS,
258260
};
259261

260262
public static FieldCollection Normal = new FieldCollection()

Packages/com.unity.render-pipelines.universal/Editor/2D/ShaderGraph/Targets/UniversalSpriteLitSubTarget.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,17 +245,18 @@ static class SpriteLitBlockMasks
245245
{
246246
BlockFields.SurfaceDescription.BaseColor,
247247
BlockFields.SurfaceDescriptionLegacy.SpriteColor,
248-
BlockFields.SurfaceDescription.Alpha,
249248
UniversalBlockFields.SurfaceDescription.SpriteMask,
249+
BlockFields.SurfaceDescription.NormalTS,
250+
BlockFields.SurfaceDescription.Alpha,
250251
BlockFields.SurfaceDescription.AlphaClipThreshold,
251252
};
252253

253254
public static BlockFieldDescriptor[] FragmentForwardNormal = new BlockFieldDescriptor[]
254255
{
255256
BlockFields.SurfaceDescription.BaseColor,
256257
BlockFields.SurfaceDescriptionLegacy.SpriteColor,
257-
BlockFields.SurfaceDescription.Alpha,
258258
BlockFields.SurfaceDescription.NormalTS,
259+
BlockFields.SurfaceDescription.Alpha,
259260
BlockFields.SurfaceDescription.AlphaClipThreshold,
260261
};
261262
}
@@ -270,6 +271,7 @@ static class SpriteLitRequiredFields
270271
StructFields.Varyings.positionWS,
271272
StructFields.Varyings.texCoord0,
272273
StructFields.Varyings.screenPosition,
274+
StructFields.Varyings.normalWS,
273275
};
274276

275277
public static FieldCollection Normal = new FieldCollection()

Packages/com.unity.render-pipelines.universal/Editor/2D/ShaderGraph/Targets/UniversalSpriteUnlitSubTarget.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ static class SpriteUnlitRequiredFields
214214
StructFields.Varyings.positionWS,
215215
StructFields.Varyings.color,
216216
StructFields.Varyings.texCoord0,
217+
StructFields.Varyings.normalWS,
217218
};
218219
}
219220
#endregion

Packages/com.unity.render-pipelines.universal/Runtime/2D/Rendergraph/DrawLight2DPass.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,22 @@ public void Render(RenderGraph graph, ContextContainer frameData, Renderer2DData
227227
{
228228
Universal2DResourceData universal2DResourceData = frameData.Get<Universal2DResourceData>();
229229
CommonResourceData commonResourceData = frameData.Get<CommonResourceData>();
230+
UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
231+
232+
DebugHandler debugHandler = ScriptableRenderPass.GetActiveDebugHandler(cameraData);
233+
var isDebugLightingActive = debugHandler?.IsLightingActive ?? true;
234+
235+
#if UNITY_EDITOR
236+
if (cameraData.isSceneViewCamera && UnityEditor.SceneView.currentDrawingSceneView != null)
237+
isDebugLightingActive &= UnityEditor.SceneView.currentDrawingSceneView.sceneLighting;
238+
239+
if (cameraData.camera.cameraType == CameraType.Preview)
240+
isDebugLightingActive = false;
241+
#endif
230242

231243
if (!layerBatch.lightStats.useLights ||
232-
isVolumetric && !layerBatch.lightStats.useVolumetricLights)
244+
isVolumetric && !layerBatch.lightStats.useVolumetricLights ||
245+
!isDebugLightingActive)
233246
return;
234247

235248
// Render single RTs by using low level pass for apis that don't support MRTs

Packages/com.unity.render-pipelines.universal/Runtime/2D/Rendergraph/DrawRenderer2DPass.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,15 @@ private static void Execute(RasterGraphContext context, PassData passData)
5656
}
5757
}
5858

59-
// Draw all renderers in layer batch
60-
cmd.DrawRendererList(passData.rendererList);
59+
if (passData.activeDebugHandler)
60+
{
61+
passData.debugRendererLists.DrawWithRendererList(cmd);
62+
}
63+
else
64+
{
65+
// Draw all renderers in layer batch
66+
cmd.DrawRendererList(passData.rendererList);
67+
}
6168

6269
RendererLighting.DisableAllKeywords(cmd);
6370
}
@@ -76,6 +83,8 @@ class PassData
7683
internal bool layerUseLights;
7784
internal TextureHandle[] lightTextures;
7885
internal RendererListHandle rendererList;
86+
internal DebugRendererLists debugRendererLists;
87+
internal bool activeDebugHandler;
7988

8089
#if UNITY_EDITOR
8190
internal bool isLitView; // Required for prefab view and preview camera
@@ -142,9 +151,22 @@ public void Render(RenderGraph graph, ContextContainer frameData, Renderer2DData
142151
RendererLighting.GetTransparencySortingMode(rendererData, cameraData.camera, ref sortSettings);
143152
drawSettings.sortingSettings = sortSettings;
144153

145-
var param = new RendererListParams(renderingData.cullResults, drawSettings, filterSettings);
146-
passData.rendererList = graph.CreateRendererList(param);
147-
builder.UseRendererList(passData.rendererList);
154+
var activeDebugHandler = GetActiveDebugHandler(cameraData);
155+
passData.activeDebugHandler = activeDebugHandler != null;
156+
157+
if (activeDebugHandler != null)
158+
{
159+
var renderStateBlock = new RenderStateBlock(RenderStateMask.Nothing);
160+
passData.debugRendererLists = activeDebugHandler.CreateRendererListsWithDebugRenderState(graph,
161+
ref renderingData.cullResults, ref drawSettings, ref filterSettings, ref renderStateBlock);
162+
passData.debugRendererLists.PrepareRendererListForRasterPass(builder);
163+
}
164+
else
165+
{
166+
var param = new RendererListParams(renderingData.cullResults, drawSettings, filterSettings);
167+
passData.rendererList = graph.CreateRendererList(param);
168+
builder.UseRendererList(passData.rendererList);
169+
}
148170

149171
if (passData.layerUseLights)
150172
{

Packages/com.unity.render-pipelines.universal/Runtime/2D/Rendergraph/Renderer2DRendergraph.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ ImportResourceSummary GetImportResourceSummary(RenderGraph renderGraph, Universa
217217
{
218218
clearColor = true;
219219
clearDepth = true;
220+
debugHandler.TryGetScreenClearColor(ref cameraBackgroundColor);
220221
}
221222

222223
output.cameraColorParams.clearOnFirstUse = clearColor;
@@ -947,11 +948,14 @@ private void OnAfterRendering(RenderGraph renderGraph)
947948
// If HDR debug views are enabled, DebugHandler will perform the blit from debugScreenColor (== finalColorHandle) to backBufferColor.
948949
DebugHandler?.Render(renderGraph, cameraData, finalColorHandle, commonResourceData.overlayUITexture, commonResourceData.backBufferColor);
949950

950-
if (cameraData.isSceneViewCamera)
951-
DrawRenderGraphWireOverlay(renderGraph, frameData, commonResourceData.backBufferColor);
951+
if (cameraData.resolveFinalTarget)
952+
{
953+
if (cameraData.isSceneViewCamera)
954+
DrawRenderGraphWireOverlay(renderGraph, frameData, commonResourceData.backBufferColor);
952955

953-
if (drawGizmos)
954-
DrawRenderGraphGizmos(renderGraph, frameData, commonResourceData.activeColorTexture, commonResourceData.activeDepthTexture, GizmoSubset.PostImageEffects);
956+
if (drawGizmos)
957+
DrawRenderGraphGizmos(renderGraph, frameData, commonResourceData.activeColorTexture, commonResourceData.activeDepthTexture, GizmoSubset.PostImageEffects);
958+
}
955959
}
956960

957961
public Renderer2DData GetRenderer2DData()

Packages/com.unity.render-pipelines.universal/ShaderLibrary/Debug/Debugging2D.hlsl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ bool CalculateDebugColorMaterialSettings(in SurfaceData2D surfaceData, in InputD
3838

3939
case DEBUGMATERIALMODE_ALBEDO:
4040
{
41-
debugColor = half4(surfaceData.albedo, 1);
41+
debugColor = half4(surfaceData.albedo, surfaceData.alpha);
4242
return true;
4343
}
4444

@@ -54,10 +54,15 @@ bool CalculateDebugColorMaterialSettings(in SurfaceData2D surfaceData, in InputD
5454
return true;
5555
}
5656

57-
case DEBUGMATERIALMODE_NORMAL_TANGENT_SPACE:
5857
case DEBUGMATERIALMODE_NORMAL_WORLD_SPACE:
5958
{
60-
debugColor = half4(surfaceData.normalTS, 1);
59+
debugColor = half4(surfaceData.normalWS * 0.5 + 0.5, surfaceData.alpha);
60+
return true;
61+
}
62+
63+
case DEBUGMATERIALMODE_NORMAL_TANGENT_SPACE:
64+
{
65+
debugColor = half4(surfaceData.normalTS * 0.5 + 0.5, surfaceData.alpha);
6166
return true;
6267
}
6368

Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/Core2D.hlsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define COMMON_2D_INPUTS \
2020
float3 positionOS : POSITION; \
2121
float2 uv : TEXCOORD0; \
22+
float3 normal : NORMAL; \
2223
UNITY_VERTEX_INPUT_INSTANCE_ID
2324

2425
#define COMMON_2D_OUTPUTS_SHARED \
@@ -29,7 +30,8 @@
2930
#if defined(DEBUG_DISPLAY)
3031
#define COMMON_2D_OUTPUTS \
3132
COMMON_2D_OUTPUTS_SHARED \
32-
float3 positionWS : TEXCOORD2;
33+
float3 positionWS : TEXCOORD2; \
34+
half3 normalWS : TEXCOORD3;
3335
#else
3436
#define COMMON_2D_OUTPUTS \
3537
COMMON_2D_OUTPUTS_SHARED
@@ -38,7 +40,6 @@
3840
// Normals
3941
#define COMMON_2D_NORMALS_INPUTS \
4042
COMMON_2D_INPUTS \
41-
float3 normal : NORMAL; \
4243
float4 tangent : TANGENT; \
4344

4445
#define COMMON_2D_NORMALS_OUTPUTS \

0 commit comments

Comments
 (0)