diff --git a/com.unity.render-pipelines.core/Runtime/Common/ComponentSingleton.cs b/com.unity.render-pipelines.core/Runtime/Common/ComponentSingleton.cs index fb02eab90cb..993c1283d03 100644 --- a/com.unity.render-pipelines.core/Runtime/Common/ComponentSingleton.cs +++ b/com.unity.render-pipelines.core/Runtime/Common/ComponentSingleton.cs @@ -20,7 +20,7 @@ public static TType instance { if (s_Instance == null) { - GameObject go = new GameObject("Default " + typeof(TType)) { hideFlags = HideFlags.HideAndDontSave }; + GameObject go = new GameObject("Default " + typeof(TType).Name) { hideFlags = HideFlags.HideAndDontSave }; go.SetActive(false); s_Instance = go.AddComponent(); } @@ -28,5 +28,18 @@ public static TType instance return s_Instance; } } + + /// + /// Release the component singleton. + /// + public static void Release() + { + if (s_Instance != null) + { + var go = s_Instance.gameObject; + CoreUtils.Destroy(go); + s_Instance = null; + } + } } } diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index ce982cb32dc..ce7366ecc9c 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -526,6 +526,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Fix precise fresnel for delta lights for SVBRDF in AxF. - Fixed the debug exposure mode for display sky reflection and debug view baked lighting - Fixed MSAA depth resolve when there is no motion vectors +- Fixed various object leaks in HDRP. ### Changed - Color buffer pyramid is not allocated anymore if neither refraction nor distortion are enabled diff --git a/com.unity.render-pipelines.high-definition/Runtime/Core/Textures/TextureCacheCubemap.cs b/com.unity.render-pipelines.high-definition/Runtime/Core/Textures/TextureCacheCubemap.cs index bb2ca768c52..82c52aec49a 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Core/Textures/TextureCacheCubemap.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Core/Textures/TextureCacheCubemap.cs @@ -189,7 +189,9 @@ public void Release() CoreUtils.Destroy(m_CubeBlitMaterial); } - m_Cache.Release(); + CoreUtils.Destroy(m_BlitCubemapFaceMaterial); + + CoreUtils.Destroy(m_Cache); } private bool TransferToPanoCache(CommandBuffer cmd, int sliceIndex, Texture[] textureArray) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/Shadow/HDShadowManager.cs b/com.unity.render-pipelines.high-definition/Runtime/Lighting/Shadow/HDShadowManager.cs index aa4a98c0dcc..258e010ab2d 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/Shadow/HDShadowManager.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/Shadow/HDShadowManager.cs @@ -257,6 +257,8 @@ partial class HDShadowManager : IDisposable int m_CascadeCount; int m_ShadowResolutionRequestCounter; + Material m_ClearShadowMaterial; + private static HDShadowManager s_Instance = new HDShadowManager(); public static HDShadowManager instance { get { return s_Instance; } } @@ -268,7 +270,7 @@ private HDShadowManager() public void InitShadowManager(RenderPipelineResources renderPipelineResources, DepthBits directionalShadowDepthBits, HDShadowInitParameters.HDShadowAtlasInitParams punctualLightAtlasInfo, HDShadowInitParameters.HDShadowAtlasInitParams areaLightAtlasInfo, int maxShadowRequests, Shader clearShader) { - Material clearMaterial = CoreUtils.CreateEngineMaterial(clearShader); + m_ClearShadowMaterial = CoreUtils.CreateEngineMaterial(clearShader); // Prevent the list from resizing their internal container when we add shadow requests m_ShadowDatas.Capacity = Math.Max(maxShadowRequests, m_ShadowDatas.Capacity); @@ -282,13 +284,13 @@ public void InitShadowManager(RenderPipelineResources renderPipelineResources, D } // The cascade atlas will be allocated only if there is a directional light - m_Atlas = new HDShadowAtlas(renderPipelineResources, punctualLightAtlasInfo.shadowAtlasResolution, punctualLightAtlasInfo.shadowAtlasResolution, HDShaderIDs._ShadowmapAtlas, clearMaterial, maxShadowRequests, depthBufferBits: punctualLightAtlasInfo.shadowAtlasDepthBits, name: "Shadow Map Atlas"); + m_Atlas = new HDShadowAtlas(renderPipelineResources, punctualLightAtlasInfo.shadowAtlasResolution, punctualLightAtlasInfo.shadowAtlasResolution, HDShaderIDs._ShadowmapAtlas, m_ClearShadowMaterial, maxShadowRequests, depthBufferBits: punctualLightAtlasInfo.shadowAtlasDepthBits, name: "Shadow Map Atlas"); // Cascade atlas render texture will only be allocated if there is a shadow casting directional light HDShadowAtlas.BlurAlgorithm cascadeBlur = GetDirectionalShadowAlgorithm() == DirectionalShadowAlgorithm.IMS ? HDShadowAtlas.BlurAlgorithm.IM : HDShadowAtlas.BlurAlgorithm.None; - m_CascadeAtlas = new HDShadowAtlas(renderPipelineResources, 1, 1, HDShaderIDs._ShadowmapCascadeAtlas, clearMaterial, maxShadowRequests, cascadeBlur, depthBufferBits: directionalShadowDepthBits, name: "Cascade Shadow Map Atlas"); + m_CascadeAtlas = new HDShadowAtlas(renderPipelineResources, 1, 1, HDShaderIDs._ShadowmapCascadeAtlas, m_ClearShadowMaterial, maxShadowRequests, cascadeBlur, depthBufferBits: directionalShadowDepthBits, name: "Cascade Shadow Map Atlas"); if (ShaderConfig.s_AreaLights == 1) - m_AreaLightShadowAtlas = new HDShadowAtlas(renderPipelineResources, areaLightAtlasInfo.shadowAtlasResolution, areaLightAtlasInfo.shadowAtlasResolution, HDShaderIDs._AreaLightShadowmapAtlas, clearMaterial, maxShadowRequests, HDShadowAtlas.BlurAlgorithm.EVSM, depthBufferBits: areaLightAtlasInfo.shadowAtlasDepthBits, name: "Area Light Shadow Map Atlas", momentAtlasShaderID: HDShaderIDs._AreaShadowmapMomentAtlas); + m_AreaLightShadowAtlas = new HDShadowAtlas(renderPipelineResources, areaLightAtlasInfo.shadowAtlasResolution, areaLightAtlasInfo.shadowAtlasResolution, HDShaderIDs._AreaLightShadowmapAtlas, m_ClearShadowMaterial, maxShadowRequests, HDShadowAtlas.BlurAlgorithm.EVSM, depthBufferBits: areaLightAtlasInfo.shadowAtlasDepthBits, name: "Area Light Shadow Map Atlas", momentAtlasShaderID: HDShaderIDs._AreaShadowmapMomentAtlas); m_ShadowDataBuffer = new ComputeBuffer(maxShadowRequests, System.Runtime.InteropServices.Marshal.SizeOf(typeof(HDShadowData))); m_DirectionalShadowDataBuffer = new ComputeBuffer(1, System.Runtime.InteropServices.Marshal.SizeOf(typeof(HDDirectionalShadowData))); @@ -830,6 +832,8 @@ public void Dispose() if (ShaderConfig.s_AreaLights == 1) m_AreaLightShadowAtlas.Release(); m_CascadeAtlas.Release(); + + CoreUtils.Destroy(m_ClearShadowMaterial); } } } diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs index 397eae6ba11..a206f5acb40 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs @@ -246,6 +246,8 @@ public void Cleanup() RTHandles.Release(m_InternalLogLut); CoreUtils.Destroy(m_FinalPassMaterial); CoreUtils.Destroy(m_ClearBlackMaterial); + CoreUtils.Destroy(m_SMAAMaterial); + CoreUtils.Destroy(m_TemporalAAMaterial); CoreUtils.SafeRelease(m_BokehNearKernel); CoreUtils.SafeRelease(m_BokehFarKernel); CoreUtils.SafeRelease(m_BokehIndirectCmd); @@ -262,6 +264,8 @@ public void Cleanup() m_InternalLogLut = null; m_FinalPassMaterial = null; m_ClearBlackMaterial = null; + m_SMAAMaterial = null; + m_TemporalAAMaterial = null; m_BokehNearKernel = null; m_BokehFarKernel = null; m_BokehIndirectCmd = null; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs index 3c489e118fd..211325b3b38 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs @@ -962,6 +962,8 @@ void DisposeProbeCameraPool() ConstantBuffer.ReleaseAll(); CameraCaptureBridge.enabled = false; + + HDUtils.ReleaseComponentSingletons(); } diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MipGenerator.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MipGenerator.cs index d16186a2d52..f413e1df6ad 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MipGenerator.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/MipGenerator.cs @@ -39,6 +39,8 @@ public void Release() RTHandles.Release(m_TempDownsamplePyramid[i]); m_TempDownsamplePyramid[i] = null; } + + CoreUtils.Destroy(m_ColorPyramidPSMat); } private int tmpTargetCount @@ -222,4 +224,4 @@ public int RenderColorGaussianPyramid(CommandBuffer cmd, Vector2Int size, Textur return srcMipLevel + 1; } } -} \ No newline at end of file +} diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Utility/HDUtils.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Utility/HDUtils.cs index ce92adf6263..f09c5257339 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Utility/HDUtils.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Utility/HDUtils.cs @@ -24,7 +24,7 @@ public class HDUtils static internal HDAdditionalLightData s_DefaultHDAdditionalLightData { get { return ComponentSingleton.instance; } } /// Default HDAdditionalCameraData static internal HDAdditionalCameraData s_DefaultHDAdditionalCameraData { get { return ComponentSingleton.instance; } } - + static List m_TempCustomPassVolumeList = new List(); static Texture3D m_ClearTexture3D; @@ -470,7 +470,7 @@ internal static RenderPipelineAsset SwitchToBuiltinRenderPipeline(out bool asset } // Set the renderPipelineAsset, either on the quality settings if it was unset from there or in GraphicsSettings. - // IMPORTANT: RenderPipelineManager.currentPipeline won't be HDRP until a camera.Render() call is made. + // IMPORTANT: RenderPipelineManager.currentPipeline won't be HDRP until a camera.Render() call is made. internal static void RestoreRenderPipelineAsset(bool wasUnsetFromQuality, RenderPipelineAsset renderPipelineAsset) { if(wasUnsetFromQuality) @@ -998,5 +998,12 @@ internal static void DisplayUnsupportedAPIMessage(string graphicAPI = null) string msg = "Platform " + currentPlatform + " with device " + graphicAPI + " is not supported with High Definition Render Pipeline, no rendering will occur"; DisplayUnsupportedMessage(msg); } + + internal static void ReleaseComponentSingletons() + { + ComponentSingleton.Release(); + ComponentSingleton.Release(); + ComponentSingleton.Release(); + } } } diff --git a/com.unity.render-pipelines.high-definition/Runtime/Sky/PhysicallyBasedSky/PhysicallyBasedSkyRenderer.cs b/com.unity.render-pipelines.high-definition/Runtime/Sky/PhysicallyBasedSky/PhysicallyBasedSkyRenderer.cs index 15cc2560985..b91015b7943 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Sky/PhysicallyBasedSky/PhysicallyBasedSkyRenderer.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Sky/PhysicallyBasedSky/PhysicallyBasedSkyRenderer.cs @@ -73,7 +73,7 @@ unsafe struct ShaderVariablesPhysicallyBasedSky static ComputeShader s_GroundIrradiancePrecomputationCS; static ComputeShader s_InScatteredRadiancePrecomputationCS; - static Material s_PbrSkyMaterial; + Material s_PbrSkyMaterial; static MaterialPropertyBlock s_PbrSkyMaterialProperties; ShaderVariablesPhysicallyBasedSky m_ConstantBuffer; @@ -124,8 +124,7 @@ public override void Build() s_InScatteredRadiancePrecomputationCS = hdrpResources.shaders.inScatteredRadiancePrecomputationCS; s_PbrSkyMaterialProperties = new MaterialPropertyBlock(); - if (s_PbrSkyMaterial == null) // Material instance is static. - s_PbrSkyMaterial = CoreUtils.CreateEngineMaterial(hdrpResources.shaders.physicallyBasedSkyPS); + s_PbrSkyMaterial = CoreUtils.CreateEngineMaterial(hdrpResources.shaders.physicallyBasedSkyPS); Debug.Assert(s_GroundIrradiancePrecomputationCS != null); Debug.Assert(s_InScatteredRadiancePrecomputationCS != null); @@ -170,6 +169,8 @@ public override void Cleanup() RTHandles.Release(m_InScatteredRadianceTables[3]); m_InScatteredRadianceTables[3] = null; RTHandles.Release(m_InScatteredRadianceTables[4]); m_InScatteredRadianceTables[4] = null; + CoreUtils.Destroy(s_PbrSkyMaterial); + m_LastPrecomputedBounce = 0; }