Skip to content

Commit d6d1d1d

Browse files
elliomanEvergreen
authored andcommitted
[Unity6][URP] Make sure we only allocate shadow textures when needed (UUM-70142)
Fixes UUM-70142. The issue is that the constructor was allocating shadow maps, which caused memory leakage when switching between graphics quality. This PR changes it so the textures are only allocated when needed and with a check for if it has already been created.
1 parent acd40a9 commit d6d1d1d

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

Packages/com.unity.render-pipelines.universal/Runtime/Passes/AdditionalLightsShadowCasterPass.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ private static class AdditionalShadowsConstantBuffer
3939
private bool m_EmptyShadowmapNeedsClear = false;
4040
private RTHandle m_EmptyAdditionalLightShadowmapTexture;
4141
private const int k_EmptyShadowMapDimensions = 1;
42-
private const string k_EmptyShadowMapName = "_EmptyAdditionalLightShadowmapTexture";
42+
private const string k_AdditionalLightShadowMapTextureName = "_AdditionalLightsShadowmapTexture";
43+
private const string k_EmptyAdditionalLightShadowMapTextureName = "_EmptyAdditionalLightShadowmapTexture";
4344
internal static Vector4[] s_EmptyAdditionalLightIndexToShadowParams = null;
4445

4546
float m_MaxShadowDistanceSq;
@@ -78,7 +79,7 @@ public AdditionalLightsShadowCasterPass(RenderPassEvent evt)
7879
AdditionalShadowsConstantBuffer._AdditionalShadowOffset1 = Shader.PropertyToID("_AdditionalShadowOffset1");
7980
AdditionalShadowsConstantBuffer._AdditionalShadowFadeParams = Shader.PropertyToID("_AdditionalShadowFadeParams");
8081
AdditionalShadowsConstantBuffer._AdditionalShadowmapSize = Shader.PropertyToID("_AdditionalShadowmapSize");
81-
m_AdditionalLightsShadowmapID = Shader.PropertyToID("_AdditionalLightsShadowmapTexture");
82+
m_AdditionalLightsShadowmapID = Shader.PropertyToID(k_AdditionalLightShadowMapTextureName);
8283

8384
m_AdditionalLightsWorldToShadow_SSBO = Shader.PropertyToID("_AdditionalLightsWorldToShadow_SSBO");
8485
m_AdditionalShadowParams_SSBO = Shader.PropertyToID("_AdditionalShadowParams_SSBO");
@@ -108,7 +109,6 @@ public AdditionalLightsShadowCasterPass(RenderPassEvent evt)
108109
m_AdditionalLightShadowSliceIndexTo_WorldShadowMatrix = new Matrix4x4[maxVisibleAdditionalLights];
109110
}
110111

111-
m_EmptyAdditionalLightShadowmapTexture = ShadowUtils.AllocShadowRT(k_EmptyShadowMapDimensions, k_EmptyShadowMapDimensions, k_ShadowmapBufferBits, 1, 0, name: k_EmptyShadowMapName);
112112
m_EmptyShadowmapNeedsClear = true;
113113
}
114114

@@ -576,7 +576,7 @@ public bool Setup(UniversalRenderingData renderingData, UniversalCameraData came
576576
m_AdditionalLightShadowSliceIndexTo_WorldShadowMatrix[globalShadowSliceIndex] = sliceTransform * m_AdditionalLightShadowSliceIndexTo_WorldShadowMatrix[globalShadowSliceIndex];
577577
}
578578

579-
ShadowUtils.ShadowRTReAllocateIfNeeded(ref m_AdditionalLightsShadowmapHandle, renderTargetWidth, renderTargetHeight, k_ShadowmapBufferBits, name: "_AdditionalLightsShadowmapTexture");
579+
ShadowUtils.ShadowRTReAllocateIfNeeded(ref m_AdditionalLightsShadowmapHandle, renderTargetWidth, renderTargetHeight, k_ShadowmapBufferBits, name: k_AdditionalLightShadowMapTextureName);
580580

581581
m_MaxShadowDistanceSq = cameraData.maxShadowDistance * cameraData.maxShadowDistance;
582582
m_CascadeBorder = shadowData.mainLightShadowCascadeBorder;
@@ -596,7 +596,7 @@ bool SetupForEmptyRendering(bool stripShadowsOffVariants, UniversalShadowData sh
596596
useNativeRenderPass = false;
597597

598598
// Required for scene view camera(URP renderer not initialized)
599-
if (ShadowUtils.ShadowRTReAllocateIfNeeded(ref m_EmptyAdditionalLightShadowmapTexture, k_EmptyShadowMapDimensions, k_EmptyShadowMapDimensions, k_ShadowmapBufferBits, name: k_EmptyShadowMapName))
599+
if (ShadowUtils.ShadowRTReAllocateIfNeeded(ref m_EmptyAdditionalLightShadowmapTexture, k_EmptyShadowMapDimensions, k_EmptyShadowMapDimensions, k_ShadowmapBufferBits, name: k_EmptyAdditionalLightShadowMapTextureName))
600600
m_EmptyShadowmapNeedsClear = true;
601601

602602
// initialize _AdditionalShadowParams

Packages/com.unity.render-pipelines.universal/Runtime/Passes/MainLightShadowCasterPass.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ private static class MainLightShadowConstantBuffer
3232

3333
int m_MainLightShadowmapID;
3434
internal RTHandle m_MainLightShadowmapTexture;
35-
private RTHandle m_EmptyLightShadowmapTexture;
35+
private RTHandle m_EmptyMainLightShadowmapTexture;
3636
private const int k_EmptyShadowMapDimensions = 1;
37-
private const string k_EmptyShadowMapName = "_EmptyLightShadowmapTexture";
37+
private const string k_MainLightShadowMapTextureName = "_MainLightShadowmapTexture";
38+
private const string k_EmptyMainLightShadowMapTextureName = "_EmptyMainLightShadowmapTexture";
3839
private static readonly Vector4 s_EmptyShadowParams = new Vector4(1, 0, 1, 0);
3940
private static readonly Vector4 s_EmptyShadowmapSize = s_EmptyShadowmapSize = new Vector4(k_EmptyShadowMapDimensions, 1f / k_EmptyShadowMapDimensions, k_EmptyShadowMapDimensions, k_EmptyShadowMapDimensions);
4041

@@ -76,9 +77,7 @@ public MainLightShadowCasterPass(RenderPassEvent evt)
7677
MainLightShadowConstantBuffer._ShadowOffset1 = Shader.PropertyToID("_MainLightShadowOffset1");
7778
MainLightShadowConstantBuffer._ShadowmapSize = Shader.PropertyToID("_MainLightShadowmapSize");
7879

79-
m_MainLightShadowmapID = Shader.PropertyToID("_MainLightShadowmapTexture");
80-
81-
m_EmptyLightShadowmapTexture = ShadowUtils.AllocShadowRT(k_EmptyShadowMapDimensions, k_EmptyShadowMapDimensions, k_ShadowmapBufferBits, 1, 0, name: k_EmptyShadowMapName);
80+
m_MainLightShadowmapID = Shader.PropertyToID(k_MainLightShadowMapTextureName);
8281
m_EmptyShadowmapNeedsClear = true;
8382
}
8483

@@ -88,7 +87,7 @@ public MainLightShadowCasterPass(RenderPassEvent evt)
8887
public void Dispose()
8988
{
9089
m_MainLightShadowmapTexture?.Release();
91-
m_EmptyLightShadowmapTexture?.Release();
90+
m_EmptyMainLightShadowmapTexture?.Release();
9291
}
9392

9493
/// <summary>
@@ -141,8 +140,7 @@ public bool Setup(UniversalRenderingData renderingData, UniversalCameraData came
141140
Debug.LogWarning("Only directional lights are supported as main light.");
142141
}
143142

144-
Bounds bounds;
145-
if (!renderingData.cullResults.GetShadowCasterBounds(shadowLightIndex, out bounds))
143+
if (!renderingData.cullResults.GetShadowCasterBounds(shadowLightIndex, out Bounds _))
146144
return SetupForEmptyRendering(cameraData.renderer.stripShadowsOffVariants);
147145

148146
m_ShadowCasterCascadesCount = shadowData.mainLightShadowCascadesCount;
@@ -161,7 +159,7 @@ public bool Setup(UniversalRenderingData renderingData, UniversalCameraData came
161159
return SetupForEmptyRendering(cameraData.renderer.stripShadowsOffVariants);
162160
}
163161

164-
ShadowUtils.ShadowRTReAllocateIfNeeded(ref m_MainLightShadowmapTexture, renderTargetWidth, renderTargetHeight, k_ShadowmapBufferBits, name: "_MainLightShadowmapTexture");
162+
ShadowUtils.ShadowRTReAllocateIfNeeded(ref m_MainLightShadowmapTexture, renderTargetWidth, renderTargetHeight, k_ShadowmapBufferBits, name: k_MainLightShadowMapTextureName);
165163

166164
m_MaxShadowDistanceSq = cameraData.maxShadowDistance * cameraData.maxShadowDistance;
167165
m_CascadeBorder = shadowData.mainLightShadowCascadeBorder;
@@ -180,7 +178,7 @@ bool SetupForEmptyRendering(bool stripShadowsOffVariants)
180178
useNativeRenderPass = false;
181179

182180
// Required for scene view camera(URP renderer not initialized)
183-
if(ShadowUtils.ShadowRTReAllocateIfNeeded(ref m_EmptyLightShadowmapTexture, k_EmptyShadowMapDimensions, k_EmptyShadowMapDimensions, k_ShadowmapBufferBits, name: k_EmptyShadowMapName))
181+
if(ShadowUtils.ShadowRTReAllocateIfNeeded(ref m_EmptyMainLightShadowmapTexture, k_EmptyShadowMapDimensions, k_EmptyShadowMapDimensions, k_ShadowmapBufferBits, name: k_EmptyMainLightShadowMapTextureName))
184182
m_EmptyShadowmapNeedsClear = true;
185183

186184
return true;
@@ -197,7 +195,7 @@ public override void Configure(CommandBuffer cmd, RenderTextureDescriptor camera
197195
#pragma warning disable CS0618
198196
if (m_CreateEmptyShadowmap)
199197
{
200-
ConfigureTarget(m_EmptyLightShadowmapTexture);
198+
ConfigureTarget(m_EmptyMainLightShadowmapTexture);
201199
m_EmptyShadowmapNeedsClear = false;
202200
}
203201
else
@@ -220,8 +218,7 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
220218
if (m_CreateEmptyShadowmap)
221219
{
222220
SetEmptyMainLightCascadeShadowmap(CommandBufferHelpers.GetRasterCommandBuffer(universalRenderingData.commandBuffer));
223-
universalRenderingData.commandBuffer.SetGlobalTexture(m_MainLightShadowmapID, m_EmptyLightShadowmapTexture.nameID);
224-
221+
universalRenderingData.commandBuffer.SetGlobalTexture(m_MainLightShadowmapID, m_EmptyMainLightShadowmapTexture.nameID);
225222
return;
226223
}
227224

0 commit comments

Comments
 (0)