Skip to content

Commit 1de3000

Browse files
kennytannEvergreen
authored andcommitted
[Backport 6000.0][UUM-82787] Fix Rendergraph 2D shadow pass to remove low level light pass
Fix UUM-82787
1 parent 7b17ee0 commit 1de3000

19 files changed

Lines changed: 3238 additions & 116 deletions

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ internal struct LightStats
1515
public uint blendStylesUsed;
1616
public uint blendStylesWithLights;
1717

18-
public bool useAnyLights { get { return totalLights + totalShadowLights > 0; } }
1918
public bool useLights { get { return totalLights > 0; } }
2019
public bool useShadows { get { return totalShadows > 0; } }
2120
public bool useVolumetricLights { get { return totalVolumetricUsage > 0; } }
@@ -62,7 +61,7 @@ public bool IsGameView()
6261
public LightStats GetLightStatsByLayer(int layerID, ref LayerBatch layer)
6362
{
6463
layer.lights.Clear();
65-
layer.shadowLights.Clear();
64+
layer.shadowIndices.Clear();
6665
layer.shadowCasters.Clear();
6766
var returnStats = new LightStats();
6867

@@ -109,13 +108,11 @@ public LightStats GetLightStatsByLayer(int layerID, ref LayerBatch layer)
109108
if (isShadowed)
110109
{
111110
returnStats.totalShadowLights++;
112-
layer.shadowLights.Add(light);
113-
}
114-
else
115-
{
116-
returnStats.totalLights++;
117-
layer.lights.Add(light);
111+
layer.shadowIndices.Add(layer.lights.Count);
118112
}
113+
114+
returnStats.totalLights++;
115+
layer.lights.Add(light);
119116
}
120117

121118
return returnStats;

Packages/com.unity.render-pipelines.universal/Runtime/2D/Passes/Render2DLightingPass.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ private int DrawLayerBatches(
249249
// This is a local copy of the array element (it's a struct). Remember to add a ref here if you need to modify the real thing.
250250
var layerBatch = layerBatches[i];
251251

252-
if (layerBatch.lightStats.useAnyLights)
252+
if (layerBatch.lightStats.useLights)
253253
{
254254
for (var blendStyleIndex = 0; blendStyleIndex < blendStylesCount; blendStyleIndex++)
255255
{
@@ -303,6 +303,8 @@ private int DrawLayerBatches(
303303
CopyCameraSortingLayerRenderTexture(context, renderingData, copyStoreAction);
304304
}
305305

306+
RendererLighting.DisableAllKeywords(CommandBufferHelpers.GetRasterCommandBuffer(cmd));
307+
306308
// Draw light volumes
307309
if (drawLights && (layerBatch.lightStats.totalVolumetricUsage > 0))
308310
{

Packages/com.unity.render-pipelines.universal/Runtime/2D/Passes/Utility/LayerUtility.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal struct LayerBatch
1818
private unsafe fixed bool renderTargetUsed[4];
1919

2020
public List<Light2D> lights;
21-
public List<Light2D> shadowLights;
21+
public List<int> shadowIndices;
2222
public List<ShadowCasterGroup2D> shadowCasters;
2323

2424
internal int[] activeBlendStylesIndices;
@@ -35,7 +35,7 @@ public void InitRTIds(int index)
3535
}
3636

3737
lights = new List<Light2D>();
38-
shadowLights = new List<Light2D>();
38+
shadowIndices = new List<int>();
3939
shadowCasters = new List<ShadowCasterGroup2D>();
4040
}
4141

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

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ public void Setup(RenderGraph renderGraph, ref Renderer2DData rendererData)
3131
}
3232
}
3333

34+
static bool TryGetShadowIndex(ref LayerBatch layerBatch, int lightIndex, out int shadowIndex)
35+
{
36+
shadowIndex = 0;
37+
38+
for (int i = 0; i < layerBatch.shadowIndices.Count; ++i)
39+
{
40+
if (layerBatch.shadowIndices[i] == lightIndex)
41+
{
42+
shadowIndex = i;
43+
return true;
44+
}
45+
}
46+
47+
return false;
48+
}
49+
3450
[Obsolete(DeprecationMessage.CompatibilityScriptingAPIObsolete, false)]
3551
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
3652
{
@@ -84,14 +100,15 @@ private static void Execute(RasterCommandBuffer cmd, PassData passData, ref Laye
84100
if (passData.layerBatch.lightStats.useNormalMap)
85101
s_PropertyBlock.SetTexture(k_NormalMapID, passData.normalMap);
86102

87-
if (passData.layerBatch.lightStats.useShadows)
88-
s_PropertyBlock.SetTexture(k_ShadowMapID, passData.shadowMap);
103+
var useShadows = passData.layerBatch.lightStats.useShadows && layerBatch.shadowIndices.Contains(j);
104+
if (useShadows && TryGetShadowIndex(ref layerBatch, j, out var shadowIndex))
105+
s_PropertyBlock.SetTexture(k_ShadowMapID, passData.shadowTextures[shadowIndex]);
89106

90107
if (!passData.isVolumetric || (passData.isVolumetric && light.volumetricEnabled))
91108
RendererLighting.SetCookieShaderProperties(light, s_PropertyBlock);
92109

93110
// Set shader global properties
94-
RendererLighting.SetPerLightShaderGlobals(cmd, light, slotIndex, passData.isVolumetric, false, LightBatch.isBatchingSupported);
111+
RendererLighting.SetPerLightShaderGlobals(cmd, light, slotIndex, passData.isVolumetric, useShadows, LightBatch.isBatchingSupported);
95112

96113
if (light.normalMapQuality != Light2D.NormalMapQuality.Disabled || light.lightType == Light2D.LightType.Point)
97114
RendererLighting.SetPerPointLightShaderGlobals(cmd, light, slotIndex, LightBatch.isBatchingSupported);
@@ -112,7 +129,7 @@ private static void Execute(RasterCommandBuffer cmd, PassData passData, ref Laye
112129
}
113130
}
114131

115-
internal static void ExecuteUnsafe(UnsafeCommandBuffer cmd, PassData passData, ref LayerBatch layerBatch, List<Light2D> lights, bool useShadows = false)
132+
internal static void ExecuteUnsafe(UnsafeCommandBuffer cmd, PassData passData, ref LayerBatch layerBatch, List<Light2D> lights)
116133
{
117134
cmd.SetGlobalFloat(k_InverseHDREmulationScaleID, 1.0f / passData.rendererData.hdrEmulationScale);
118135

@@ -161,8 +178,9 @@ internal static void ExecuteUnsafe(UnsafeCommandBuffer cmd, PassData passData, r
161178
if (passData.layerBatch.lightStats.useNormalMap)
162179
s_PropertyBlock.SetTexture(k_NormalMapID, passData.normalMap);
163180

164-
if (passData.layerBatch.lightStats.useShadows)
165-
s_PropertyBlock.SetTexture(k_ShadowMapID, passData.shadowMap);
181+
var useShadows = passData.layerBatch.lightStats.useShadows && layerBatch.shadowIndices.Contains(j);
182+
if (useShadows && TryGetShadowIndex(ref layerBatch, j, out var shadowIndex))
183+
s_PropertyBlock.SetTexture(k_ShadowMapID, passData.shadowTextures[shadowIndex]);
166184

167185
if (!passData.isVolumetric || (passData.isVolumetric && light.volumetricEnabled))
168186
RendererLighting.SetCookieShaderProperties(light, s_PropertyBlock);
@@ -196,14 +214,12 @@ internal class PassData
196214
internal bool isVolumetric;
197215

198216
internal TextureHandle normalMap;
199-
internal TextureHandle shadowMap;
217+
internal TextureHandle[] shadowTextures;
200218

201219
// TODO: Optimize and remove low level pass
202220
// For low level shadow and light pass
203-
internal RenderTargetIdentifier[] lightTexturesRT;
204221
internal TextureHandle[] lightTextures;
205222
internal TextureHandle depthTexture;
206-
internal TextureHandle shadowDepth;
207223
}
208224

209225
public void Render(RenderGraph graph, ContextContainer frameData, Renderer2DData rendererData, ref LayerBatch layerBatch, int batchIndex, bool isVolumetric = false)
@@ -233,7 +249,11 @@ public void Render(RenderGraph graph, ContextContainer frameData, Renderer2DData
233249
builder.UseTexture(universal2DResourceData.normalsTexture[batchIndex]);
234250

235251
if (layerBatch.lightStats.useShadows)
236-
builder.UseTexture(universal2DResourceData.shadowsTexture);
252+
{
253+
passData.shadowTextures = universal2DResourceData.shadowTextures[batchIndex];
254+
for (var i = 0; i < passData.shadowTextures.Length; i++)
255+
builder.UseTexture(passData.shadowTextures[i]);
256+
}
237257

238258
foreach (var light in layerBatch.lights)
239259
{
@@ -248,7 +268,6 @@ public void Render(RenderGraph graph, ContextContainer frameData, Renderer2DData
248268
passData.rendererData = rendererData;
249269
passData.isVolumetric = isVolumetric;
250270
passData.normalMap = layerBatch.lightStats.useNormalMap ? universal2DResourceData.normalsTexture[batchIndex] : TextureHandle.nullHandle;
251-
passData.shadowMap = layerBatch.lightStats.useShadows ? universal2DResourceData.shadowsTexture : TextureHandle.nullHandle;
252271

253272
builder.AllowPassCulling(false);
254273
builder.AllowGlobalStateModification(true);
@@ -277,7 +296,11 @@ public void Render(RenderGraph graph, ContextContainer frameData, Renderer2DData
277296
builder.UseTexture(universal2DResourceData.normalsTexture[batchIndex]);
278297

279298
if (layerBatch.lightStats.useShadows)
280-
builder.UseTexture(universal2DResourceData.shadowsTexture);
299+
{
300+
passData.shadowTextures = universal2DResourceData.shadowTextures[batchIndex];
301+
for (var i = 0; i < passData.shadowTextures.Length; i++)
302+
builder.UseTexture(passData.shadowTextures[i]);
303+
}
281304

282305
foreach (var light in layerBatch.lights)
283306
{
@@ -292,7 +315,6 @@ public void Render(RenderGraph graph, ContextContainer frameData, Renderer2DData
292315
passData.rendererData = rendererData;
293316
passData.isVolumetric = isVolumetric;
294317
passData.normalMap = layerBatch.lightStats.useNormalMap ? universal2DResourceData.normalsTexture[batchIndex] : TextureHandle.nullHandle;
295-
passData.shadowMap = layerBatch.lightStats.useShadows ? universal2DResourceData.shadowsTexture : TextureHandle.nullHandle;
296318

297319
builder.AllowPassCulling(false);
298320
builder.AllowGlobalStateModification(true);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ private static void Execute(RasterCommandBuffer cmd, PassData passData)
3030
public void Render(RenderGraph graph, ContextContainer frameData, Renderer2DData rendererData, ref LayerBatch layerBatch, int batchIndex)
3131
{
3232
Universal2DResourceData universal2DResourceData = frameData.Get<Universal2DResourceData>();
33-
int lastBatchIndex = universal2DResourceData.normalsTexture.Length - 1;
3433

3534
if (!layerBatch.useNormals)
3635
return;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void Render(RenderGraph graph, ContextContainer frameData, Renderer2DData
9595
{
9696
using (var builder = graph.AddRasterRenderPass<SetGlobalPassData>(k_SetLightBlendTexture, out var passData, m_SetLightBlendTextureProfilingSampler))
9797
{
98-
if (layerBatch.lightStats.useAnyLights)
98+
if (layerBatch.lightStats.useLights)
9999
{
100100
passData.lightTextures = universal2DResourceData.lightTextures[batchIndex];
101101
for (var i = 0; i < passData.lightTextures.Length; i++)
@@ -120,7 +120,7 @@ public void Render(RenderGraph graph, ContextContainer frameData, Renderer2DData
120120
passData.blendStyleIndices = layerBatch.activeBlendStylesIndices;
121121
passData.hdrEmulationScale = rendererData.hdrEmulationScale;
122122
passData.isSceneLit = rendererData.lightCullResult.IsSceneLit();
123-
passData.layerUseLights = layerBatch.lightStats.useAnyLights;
123+
passData.layerUseLights = layerBatch.lightStats.useLights;
124124

125125
#if UNITY_EDITOR
126126
passData.isLitView = true;
@@ -184,7 +184,7 @@ void SetGlobalLightTextures(RenderGraph graph, IRasterRenderGraphBuilder builder
184184
if (isLitView)
185185
#endif
186186
{
187-
if (layerBatch.lightStats.useAnyLights)
187+
if (layerBatch.lightStats.useLights)
188188
{
189189
for (var i = 0; i < lightTextures.Length; i++)
190190
{

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

Lines changed: 27 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,32 @@ internal class DrawShadow2DPass : ScriptableRenderPass
1313

1414
private static readonly ProfilingSampler m_ProfilingSampler = new ProfilingSampler(k_ShadowPass);
1515
private static readonly ProfilingSampler m_ProfilingSamplerVolume = new ProfilingSampler(k_ShadowVolumetricPass);
16-
private static readonly ProfilingSampler m_ExecuteProfilingSampler = new ProfilingSampler("Draw Shadow");
17-
private static readonly ProfilingSampler m_ExecuteLightProfilingSampler = new ProfilingSampler("Draw Light");
18-
19-
TextureHandle[] intermediateTexture = new TextureHandle[1];
20-
static List<Light2D> intermediateLight = new List<Light2D>(1);
2116

2217
[Obsolete(DeprecationMessage.CompatibilityScriptingAPIObsolete, false)]
2318
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
2419
{
2520
throw new NotImplementedException();
2621
}
2722

28-
private static void ExecuteShadowPass(UnsafeCommandBuffer cmd, DrawLight2DPass.PassData passData, Light2D light)
23+
private static void ExecuteShadowPass(UnsafeCommandBuffer cmd, PassData passData, Light2D light, int batchIndex)
2924
{
30-
using (new ProfilingScope(cmd, m_ExecuteProfilingSampler))
31-
{
32-
cmd.SetRenderTarget(passData.shadowMap, passData.shadowDepth);
33-
cmd.ClearRenderTarget(RTClearFlags.All, Color.clear, 1, 0);
25+
cmd.SetRenderTarget(passData.shadowTextures[batchIndex], passData.shadowDepth);
3426

35-
var projectedShadowMaterial = passData.rendererData.GetProjectedShadowMaterial();
36-
var projectedUnshadowMaterial = passData.rendererData.GetProjectedUnshadowMaterial();
27+
// Reusing the depth/stencil so we have to clear it
28+
cmd.ClearRenderTarget(RTClearFlags.All, Color.clear, 1, 0);
3729

38-
ShadowRendering.PrerenderShadows(cmd, passData.rendererData, ref passData.layerBatch, light, 0, light.shadowIntensity);
39-
}
30+
var projectedShadowMaterial = passData.rendererData.GetProjectedShadowMaterial();
31+
var projectedUnshadowMaterial = passData.rendererData.GetProjectedUnshadowMaterial();
32+
33+
ShadowRendering.PrerenderShadows(cmd, passData.rendererData, ref passData.layerBatch, light, 0, light.shadowIntensity);
34+
}
35+
36+
internal class PassData
37+
{
38+
internal LayerBatch layerBatch;
39+
internal Renderer2DData rendererData;
40+
internal TextureHandle[] shadowTextures;
41+
internal TextureHandle shadowDepth;
4042
}
4143

4244
public void Render(RenderGraph graph, ContextContainer frameData, Renderer2DData rendererData, ref LayerBatch layerBatch, int batchIndex, bool isVolumetric = false)
@@ -48,82 +50,31 @@ public void Render(RenderGraph graph, ContextContainer frameData, Renderer2DData
4850
isVolumetric && !layerBatch.lightStats.useVolumetricShadowLights)
4951
return;
5052

51-
var shadowTexture = universal2DResourceData.shadowsTexture;
52-
var depthTexture = universal2DResourceData.shadowsDepth;
53-
54-
using (var builder = graph.AddUnsafePass<DrawLight2DPass.PassData>(!isVolumetric ? k_ShadowPass : k_ShadowVolumetricPass, out var passData, !isVolumetric ? m_ProfilingSampler : m_ProfilingSamplerVolume))
53+
using (var builder = graph.AddUnsafePass<PassData>(!isVolumetric ? k_ShadowPass : k_ShadowVolumetricPass, out var passData, !isVolumetric ? m_ProfilingSampler : m_ProfilingSamplerVolume))
5554
{
5655
passData.layerBatch = layerBatch;
5756
passData.rendererData = rendererData;
58-
passData.isVolumetric = isVolumetric;
59-
passData.shadowMap = shadowTexture;
60-
passData.shadowDepth = depthTexture;
61-
passData.normalMap = layerBatch.lightStats.useNormalMap ? universal2DResourceData.normalsTexture[batchIndex] : TextureHandle.nullHandle;
57+
passData.shadowTextures = universal2DResourceData.shadowTextures[batchIndex];
58+
passData.shadowDepth = universal2DResourceData.shadowDepth;
6259

63-
if (!isVolumetric)
64-
{
65-
passData.lightTextures = universal2DResourceData.lightTextures[batchIndex];
66-
passData.depthTexture = universal2DResourceData.intermediateDepth;
67-
builder.UseTexture(passData.depthTexture, AccessFlags.Write);
68-
}
69-
else
70-
{
71-
intermediateTexture[0] = commonResourceData.activeColorTexture;
72-
passData.lightTextures = intermediateTexture;
73-
}
74-
75-
if (passData.lightTexturesRT == null || passData.lightTexturesRT.Length != passData.lightTextures.Length)
76-
passData.lightTexturesRT = new RenderTargetIdentifier[passData.lightTextures.Length];
77-
78-
for (int i = 0; i < passData.lightTextures.Length; ++i)
79-
builder.UseTexture(passData.lightTextures[i], AccessFlags.Write);
80-
81-
if (layerBatch.lightStats.useNormalMap)
82-
builder.UseTexture(universal2DResourceData.normalsTexture[batchIndex]);
60+
for (var i = 0; i < passData.shadowTextures.Length; i++)
61+
builder.UseTexture(passData.shadowTextures[i], AccessFlags.Write);
8362

84-
builder.UseTexture(shadowTexture, AccessFlags.Write);
85-
builder.UseTexture(depthTexture, AccessFlags.Write);
86-
87-
foreach (var light in layerBatch.shadowLights)
88-
{
89-
if (light == null || !light.m_CookieSpriteTextureHandle.IsValid())
90-
continue;
91-
92-
if (!isVolumetric || (isVolumetric && light.volumetricEnabled))
93-
builder.UseTexture(light.m_CookieSpriteTextureHandle);
94-
}
63+
builder.UseTexture(passData.shadowDepth, AccessFlags.Write);
9564

9665
builder.AllowPassCulling(false);
9766
builder.AllowGlobalStateModification(true);
9867

99-
builder.SetRenderFunc((DrawLight2DPass.PassData data, UnsafeGraphContext context) =>
68+
builder.SetRenderFunc((PassData data, UnsafeGraphContext context) =>
10069
{
101-
for (int i = 0; i < data.layerBatch.shadowLights.Count; ++i)
70+
for (int i = 0; i < data.layerBatch.shadowIndices.Count; ++i)
10271
{
103-
intermediateLight.Clear();
104-
intermediateLight.Add(data.layerBatch.shadowLights[i]);
105-
10672
var cmd = context.cmd;
73+
var index = data.layerBatch.shadowIndices[i];
74+
var light = data.layerBatch.lights[index];
10775

10876
// Shadow Pass
109-
ExecuteShadowPass(cmd, data, intermediateLight[0]);
110-
111-
// Set up MRT
112-
if (Renderer2D.supportsMRT && !data.isVolumetric)
113-
{
114-
for (int j = 0; j < data.lightTextures.Length; ++j)
115-
data.lightTexturesRT[j] = data.lightTextures[j];
116-
117-
cmd.SetRenderTarget(data.lightTexturesRT, data.depthTexture);
118-
}
119-
else
120-
cmd.SetRenderTarget(data.lightTextures[0]);
121-
122-
// Light Pass
123-
using (new ProfilingScope(cmd, DrawLight2DPass.m_ProfilingSamplerLowLevel))
124-
{
125-
DrawLight2DPass.ExecuteUnsafe(cmd, data, ref data.layerBatch, intermediateLight, true);
126-
}
77+
ExecuteShadowPass(cmd, data, light, i);
12778
}
12879
});
12980
}

0 commit comments

Comments
 (0)