Skip to content

Commit bc7cc75

Browse files
[Backport 7.x.x] Fix various leaks in HDRP (#120)
* Fixed a number of leak in HDRP # Conflicts: # com.unity.render-pipelines.high-definition/Runtime/Lighting/Shadow/HDShadowManager.cs # com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Utility/HDUtils.cs * Update changelog
1 parent aaf7bf5 commit bc7cc75

9 files changed

Lines changed: 48 additions & 12 deletions

File tree

com.unity.render-pipelines.core/Runtime/Common/ComponentSingleton.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,26 @@ public static TType instance
2020
{
2121
if (s_Instance == null)
2222
{
23-
GameObject go = new GameObject("Default " + typeof(TType)) { hideFlags = HideFlags.HideAndDontSave };
23+
GameObject go = new GameObject("Default " + typeof(TType).Name) { hideFlags = HideFlags.HideAndDontSave };
2424
go.SetActive(false);
2525
s_Instance = go.AddComponent<TType>();
2626
}
2727

2828
return s_Instance;
2929
}
3030
}
31+
32+
/// <summary>
33+
/// Release the component singleton.
34+
/// </summary>
35+
public static void Release()
36+
{
37+
if (s_Instance != null)
38+
{
39+
var go = s_Instance.gameObject;
40+
CoreUtils.Destroy(go);
41+
s_Instance = null;
42+
}
43+
}
3144
}
3245
}

com.unity.render-pipelines.high-definition/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
3939
- Fixed an issue where changing the default volume profile from another inspector would not update the default volume editor.
4040
- Fix for range compression factor for probes going negative (now clamped to positive values).
4141
- Fixed path validation when creating new volume profile (case 1229933)
42+
- Fixed various object leaks in HDRP.
4243

4344
### Changed
4445
- Rejecting history for ray traced reflections based on a threshold evaluated on the neighborhood of the sampled history.

com.unity.render-pipelines.high-definition/Runtime/Core/Textures/TextureCacheCubemap.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ public void Release()
189189
CoreUtils.Destroy(m_CubeBlitMaterial);
190190
}
191191

192-
m_Cache.Release();
192+
CoreUtils.Destroy(m_BlitCubemapFaceMaterial);
193+
194+
CoreUtils.Destroy(m_Cache);
193195
}
194196

195197
private bool TransferToPanoCache(CommandBuffer cmd, int sliceIndex, Texture[] textureArray)

com.unity.render-pipelines.high-definition/Runtime/Lighting/Shadow/HDShadowManager.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ partial class HDShadowManager : IDisposable
257257
int m_CascadeCount;
258258
int m_ShadowResolutionRequestCounter;
259259

260+
Material m_ClearShadowMaterial;
261+
260262
private static HDShadowManager s_Instance = new HDShadowManager();
261263

262264
public static HDShadowManager instance { get { return s_Instance; } }
@@ -268,7 +270,7 @@ private HDShadowManager()
268270
public void InitShadowManager(RenderPipelineResources renderPipelineResources, DepthBits directionalShadowDepthBits,
269271
HDShadowInitParameters.HDShadowAtlasInitParams punctualLightAtlasInfo, HDShadowInitParameters.HDShadowAtlasInitParams areaLightAtlasInfo, int maxShadowRequests, Shader clearShader)
270272
{
271-
Material clearMaterial = CoreUtils.CreateEngineMaterial(clearShader);
273+
m_ClearShadowMaterial = CoreUtils.CreateEngineMaterial(clearShader);
272274

273275
// Prevent the list from resizing their internal container when we add shadow requests
274276
m_ShadowDatas.Capacity = Math.Max(maxShadowRequests, m_ShadowDatas.Capacity);
@@ -282,13 +284,13 @@ public void InitShadowManager(RenderPipelineResources renderPipelineResources, D
282284
}
283285

284286
// The cascade atlas will be allocated only if there is a directional light
285-
m_Atlas = new HDShadowAtlas(renderPipelineResources, punctualLightAtlasInfo.shadowAtlasResolution, punctualLightAtlasInfo.shadowAtlasResolution, HDShaderIDs._ShadowmapAtlas, HDShaderIDs._ShadowAtlasSize, clearMaterial, maxShadowRequests, depthBufferBits: punctualLightAtlasInfo.shadowAtlasDepthBits, name: "Shadow Map Atlas");
287+
m_Atlas = new HDShadowAtlas(renderPipelineResources, punctualLightAtlasInfo.shadowAtlasResolution, punctualLightAtlasInfo.shadowAtlasResolution, HDShaderIDs._ShadowmapAtlas, HDShaderIDs._ShadowAtlasSize, m_ClearShadowMaterial, maxShadowRequests, depthBufferBits: punctualLightAtlasInfo.shadowAtlasDepthBits, name: "Shadow Map Atlas");
286288
// Cascade atlas render texture will only be allocated if there is a shadow casting directional light
287289
HDShadowAtlas.BlurAlgorithm cascadeBlur = GetDirectionalShadowAlgorithm() == DirectionalShadowAlgorithm.IMS ? HDShadowAtlas.BlurAlgorithm.IM : HDShadowAtlas.BlurAlgorithm.None;
288-
m_CascadeAtlas = new HDShadowAtlas(renderPipelineResources, 1, 1, HDShaderIDs._ShadowmapCascadeAtlas, HDShaderIDs._CascadeShadowAtlasSize, clearMaterial, maxShadowRequests, cascadeBlur, depthBufferBits: directionalShadowDepthBits, name: "Cascade Shadow Map Atlas");
290+
m_CascadeAtlas = new HDShadowAtlas(renderPipelineResources, 1, 1, HDShaderIDs._ShadowmapCascadeAtlas, HDShaderIDs._CascadeShadowAtlasSize, m_ClearShadowMaterial, maxShadowRequests, cascadeBlur, depthBufferBits: directionalShadowDepthBits, name: "Cascade Shadow Map Atlas");
289291

290292
if (ShaderConfig.s_AreaLights == 1)
291-
m_AreaLightShadowAtlas = new HDShadowAtlas(renderPipelineResources, areaLightAtlasInfo.shadowAtlasResolution, areaLightAtlasInfo.shadowAtlasResolution, HDShaderIDs._AreaLightShadowmapAtlas, HDShaderIDs._AreaShadowAtlasSize, clearMaterial, maxShadowRequests, HDShadowAtlas.BlurAlgorithm.EVSM, depthBufferBits: areaLightAtlasInfo.shadowAtlasDepthBits, name: "Area Light Shadow Map Atlas", momentAtlasShaderID: HDShaderIDs._AreaShadowmapMomentAtlas);
293+
m_AreaLightShadowAtlas = new HDShadowAtlas(renderPipelineResources, areaLightAtlasInfo.shadowAtlasResolution, areaLightAtlasInfo.shadowAtlasResolution, HDShaderIDs._AreaLightShadowmapAtlas, HDShaderIDs._AreaShadowAtlasSize, m_ClearShadowMaterial, maxShadowRequests, HDShadowAtlas.BlurAlgorithm.EVSM, depthBufferBits: areaLightAtlasInfo.shadowAtlasDepthBits, name: "Area Light Shadow Map Atlas", momentAtlasShaderID: HDShaderIDs._AreaShadowmapMomentAtlas);
292294

293295
m_ShadowDataBuffer = new ComputeBuffer(maxShadowRequests, System.Runtime.InteropServices.Marshal.SizeOf(typeof(HDShadowData)));
294296
m_DirectionalShadowDataBuffer = new ComputeBuffer(1, System.Runtime.InteropServices.Marshal.SizeOf(typeof(HDDirectionalShadowData)));
@@ -822,6 +824,8 @@ public void Dispose()
822824
if (ShaderConfig.s_AreaLights == 1)
823825
m_AreaLightShadowAtlas.Release();
824826
m_CascadeAtlas.Release();
827+
828+
CoreUtils.Destroy(m_ClearShadowMaterial);
825829
}
826830
}
827831
}

com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ public void Cleanup()
269269
RTHandles.Release(m_InternalLogLut);
270270
CoreUtils.Destroy(m_FinalPassMaterial);
271271
CoreUtils.Destroy(m_ClearBlackMaterial);
272+
CoreUtils.Destroy(m_SMAAMaterial);
273+
CoreUtils.Destroy(m_TemporalAAMaterial);
272274
CoreUtils.SafeRelease(m_BokehNearKernel);
273275
CoreUtils.SafeRelease(m_BokehFarKernel);
274276
CoreUtils.SafeRelease(m_BokehIndirectCmd);
@@ -285,6 +287,8 @@ public void Cleanup()
285287
m_InternalLogLut = null;
286288
m_FinalPassMaterial = null;
287289
m_ClearBlackMaterial = null;
290+
m_SMAAMaterial = null;
291+
m_TemporalAAMaterial = null;
288292
m_BokehNearKernel = null;
289293
m_BokehFarKernel = null;
290294
m_BokehIndirectCmd = null;

com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,8 @@ void DisposeProbeCameraPool()
940940
}
941941

942942
CameraCaptureBridge.enabled = false;
943+
944+
HDUtils.ReleaseComponentSingletons();
943945
}
944946

945947

com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MipGenerator.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public void Release()
3939
RTHandles.Release(m_TempDownsamplePyramid[i]);
4040
m_TempDownsamplePyramid[i] = null;
4141
}
42+
43+
CoreUtils.Destroy(m_ColorPyramidPSMat);
4244
}
4345

4446
private int tmpTargetCount
@@ -222,4 +224,4 @@ public int RenderColorGaussianPyramid(CommandBuffer cmd, Vector2Int size, Textur
222224
return srcMipLevel + 1;
223225
}
224226
}
225-
}
227+
}

com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Utility/HDUtils.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class HDUtils
2424
static internal HDAdditionalLightData s_DefaultHDAdditionalLightData { get { return ComponentSingleton<HDAdditionalLightData>.instance; } }
2525
/// <summary>Default HDAdditionalCameraData</summary>
2626
static internal HDAdditionalCameraData s_DefaultHDAdditionalCameraData { get { return ComponentSingleton<HDAdditionalCameraData>.instance; } }
27-
27+
2828
static List<CustomPassVolume> m_TempCustomPassVolumeList = new List<CustomPassVolume>();
2929

3030
static Texture3D m_ClearTexture3D;
@@ -473,7 +473,7 @@ internal static RenderPipelineAsset SwitchToBuiltinRenderPipeline(out bool asset
473473
}
474474

475475
// Set the renderPipelineAsset, either on the quality settings if it was unset from there or in GraphicsSettings.
476-
// IMPORTANT: RenderPipelineManager.currentPipeline won't be HDRP until a camera.Render() call is made.
476+
// IMPORTANT: RenderPipelineManager.currentPipeline won't be HDRP until a camera.Render() call is made.
477477
internal static void RestoreRenderPipelineAsset(bool wasUnsetFromQuality, RenderPipelineAsset renderPipelineAsset)
478478
{
479479
if(wasUnsetFromQuality)
@@ -1002,6 +1002,13 @@ internal static void DisplayUnsupportedAPIMessage(string graphicAPI = null)
10021002
DisplayUnsupportedMessage(msg);
10031003
}
10041004

1005+
internal static void ReleaseComponentSingletons()
1006+
{
1007+
ComponentSingleton<HDAdditionalReflectionData>.Release();
1008+
ComponentSingleton<HDAdditionalLightData>.Release();
1009+
ComponentSingleton<HDAdditionalCameraData>.Release();
1010+
}
1011+
10051012
internal static void DisplayUnsupportedXRMessage()
10061013
{
10071014
string msg = "AR/VR devices are not supported, no rendering will occur";

com.unity.render-pipelines.high-definition/Runtime/Sky/PhysicallyBasedSky/PhysicallyBasedSkyRenderer.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public enum PbrSkyConfig
3232

3333
static ComputeShader s_GroundIrradiancePrecomputationCS;
3434
static ComputeShader s_InScatteredRadiancePrecomputationCS;
35-
static Material s_PbrSkyMaterial;
35+
Material s_PbrSkyMaterial;
3636
static MaterialPropertyBlock s_PbrSkyMaterialProperties;
3737

3838
static GraphicsFormat s_ColorFormat = GraphicsFormat.R16G16B16A16_SFloat;
@@ -80,8 +80,7 @@ public override void Build()
8080
s_InScatteredRadiancePrecomputationCS = hdrpResources.shaders.inScatteredRadiancePrecomputationCS;
8181
s_PbrSkyMaterialProperties = new MaterialPropertyBlock();
8282

83-
if (s_PbrSkyMaterial == null) // Material instance is static.
84-
s_PbrSkyMaterial = CoreUtils.CreateEngineMaterial(hdrpResources.shaders.physicallyBasedSkyPS);
83+
s_PbrSkyMaterial = CoreUtils.CreateEngineMaterial(hdrpResources.shaders.physicallyBasedSkyPS);
8584

8685
Debug.Assert(s_GroundIrradiancePrecomputationCS != null);
8786
Debug.Assert(s_InScatteredRadiancePrecomputationCS != null);
@@ -126,6 +125,8 @@ public override void Cleanup()
126125
RTHandles.Release(m_InScatteredRadianceTables[3]); m_InScatteredRadianceTables[3] = null;
127126
RTHandles.Release(m_InScatteredRadianceTables[4]); m_InScatteredRadianceTables[4] = null;
128127

128+
CoreUtils.Destroy(s_PbrSkyMaterial);
129+
129130
m_LastPrecomputedBounce = 0;
130131
}
131132

0 commit comments

Comments
 (0)