From 014ecaa5f7b4f4b0d3fdcf362e8a5a2a5903c61b Mon Sep 17 00:00:00 2001 From: Pavlos Mavridis Date: Fri, 15 Jan 2021 17:26:54 +0100 Subject: [PATCH 1/3] Fix gc alloc errors --- .../Runtime/Compositor/CompositionManager.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Compositor/CompositionManager.cs b/com.unity.render-pipelines.high-definition/Runtime/Compositor/CompositionManager.cs index a475ebe798a..3f6caac093f 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Compositor/CompositionManager.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Compositor/CompositionManager.cs @@ -929,14 +929,12 @@ static internal void RegisterCustomPasses(HDRenderPipeline hdPipeline) } // If custom post processes are not registered in the HDRP asset, they are never executed so we have to add them manually - int indx = hdPipeline.asset.beforePostProcessCustomPostProcesses.FindIndex(x => x == typeof(ChromaKeying).AssemblyQualifiedName); - if (indx < 0) + if (!hdPipeline.asset.beforePostProcessCustomPostProcesses.Contains(typeof(ChromaKeying).AssemblyQualifiedName)) { hdPipeline.asset.beforePostProcessCustomPostProcesses.Add(typeof(ChromaKeying).AssemblyQualifiedName); } - indx = hdPipeline.asset.beforePostProcessCustomPostProcesses.FindIndex(x => x == typeof(AlphaInjection).AssemblyQualifiedName); - if (indx < 0) + if (!hdPipeline.asset.beforePostProcessCustomPostProcesses.Contains(typeof(AlphaInjection).AssemblyQualifiedName)) { hdPipeline.asset.beforePostProcessCustomPostProcesses.Add(typeof(AlphaInjection).AssemblyQualifiedName); } @@ -950,14 +948,12 @@ static internal void UnRegisterCustomPasses(HDRenderPipeline hdPipeline) return; } - int indx = hdPipeline.asset.beforePostProcessCustomPostProcesses.FindIndex(x => x == typeof(ChromaKeying).AssemblyQualifiedName); - if (indx >= 0) + if (hdPipeline.asset.beforePostProcessCustomPostProcesses.Contains(typeof(ChromaKeying).AssemblyQualifiedName)) { hdPipeline.asset.beforePostProcessCustomPostProcesses.Remove(typeof(ChromaKeying).AssemblyQualifiedName); } - indx = hdPipeline.asset.beforePostProcessCustomPostProcesses.FindIndex(x => x == typeof(AlphaInjection).AssemblyQualifiedName); - if (indx >= 0) + if (hdPipeline.asset.beforePostProcessCustomPostProcesses.Contains(typeof(AlphaInjection).AssemblyQualifiedName)) { hdPipeline.asset.beforePostProcessCustomPostProcesses.Remove(typeof(AlphaInjection).AssemblyQualifiedName); } From 3d3df6f38d477a156107890e6ec00edfba949b27 Mon Sep 17 00:00:00 2001 From: Pavlos Mavridis Date: Fri, 15 Jan 2021 17:50:36 +0100 Subject: [PATCH 2/3] Avoid setting the name of an object during render (gc alloc) --- .../Runtime/Compositor/AlphaInjection.cs | 9 +++++++-- .../Runtime/Compositor/ChromaKeying.cs | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Compositor/AlphaInjection.cs b/com.unity.render-pipelines.high-definition/Runtime/Compositor/AlphaInjection.cs index 4eed8e821fb..2b3ae53d13e 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Compositor/AlphaInjection.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Compositor/AlphaInjection.cs @@ -18,6 +18,13 @@ internal class ShaderIDs Material m_Material; + protected override void OnEnable() + { + // Needed to get a scope name in RenderDoc captures + name = "AlphaInjection"; + base.OnEnable(); + } + public bool IsActive() => m_Material != null; public override CustomPostProcessInjectionPoint injectionPoint => CustomPostProcessInjectionPoint.BeforePostProcess; @@ -27,8 +34,6 @@ public override void Setup() var hdrpAsset = HDRenderPipeline.defaultAsset; if (hdrpAsset != null) m_Material = CoreUtils.CreateEngineMaterial(hdrpAsset.renderPipelineResources.shaders.alphaInjectionPS); - - name = "AlphaInjection"; // Needed to get a scope name in RenderDoc captures } public override void Render(CommandBuffer cmd, HDCamera camera, RTHandle source, RTHandle destination) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Compositor/ChromaKeying.cs b/com.unity.render-pipelines.high-definition/Runtime/Compositor/ChromaKeying.cs index 03e13a689e1..e1194e002a7 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Compositor/ChromaKeying.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Compositor/ChromaKeying.cs @@ -21,6 +21,13 @@ internal class ShaderIDs public BoolParameter activate = new BoolParameter(false); Material m_Material; + protected override void OnEnable() + { + // Needed to get a scope name in RenderDoc captures + name = "ChromaKeying"; + base.OnEnable(); + } + public bool IsActive() => m_Material != null; public override CustomPostProcessInjectionPoint injectionPoint => CustomPostProcessInjectionPoint.BeforePostProcess; @@ -30,8 +37,6 @@ public override void Setup() var hdrpAsset = HDRenderPipeline.defaultAsset; if (hdrpAsset != null) m_Material = CoreUtils.CreateEngineMaterial(hdrpAsset.renderPipelineResources.shaders.chromaKeyingPS); - - name = "ChromaKeying"; // Needed to get a scope name in RenderDoc captures } public override void Render(CommandBuffer cmd, HDCamera camera, RTHandle source, RTHandle destination) From 143fb4d424fc0d5dd041fd99793d63803ba44cba Mon Sep 17 00:00:00 2001 From: Pavlos Mavridis Date: Fri, 15 Jan 2021 18:06:50 +0100 Subject: [PATCH 3/3] Avoid gc allocs (for real this time) --- .../Runtime/Compositor/AlphaInjection.cs | 7 ------- .../Runtime/Compositor/ChromaKeying.cs | 7 ------- .../Runtime/Compositor/CompositionManager.cs | 4 ++++ 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Compositor/AlphaInjection.cs b/com.unity.render-pipelines.high-definition/Runtime/Compositor/AlphaInjection.cs index 2b3ae53d13e..8e7ee09e4eb 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Compositor/AlphaInjection.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Compositor/AlphaInjection.cs @@ -18,13 +18,6 @@ internal class ShaderIDs Material m_Material; - protected override void OnEnable() - { - // Needed to get a scope name in RenderDoc captures - name = "AlphaInjection"; - base.OnEnable(); - } - public bool IsActive() => m_Material != null; public override CustomPostProcessInjectionPoint injectionPoint => CustomPostProcessInjectionPoint.BeforePostProcess; diff --git a/com.unity.render-pipelines.high-definition/Runtime/Compositor/ChromaKeying.cs b/com.unity.render-pipelines.high-definition/Runtime/Compositor/ChromaKeying.cs index e1194e002a7..42241172924 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Compositor/ChromaKeying.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Compositor/ChromaKeying.cs @@ -21,13 +21,6 @@ internal class ShaderIDs public BoolParameter activate = new BoolParameter(false); Material m_Material; - protected override void OnEnable() - { - // Needed to get a scope name in RenderDoc captures - name = "ChromaKeying"; - base.OnEnable(); - } - public bool IsActive() => m_Material != null; public override CustomPostProcessInjectionPoint injectionPoint => CustomPostProcessInjectionPoint.BeforePostProcess; diff --git a/com.unity.render-pipelines.high-definition/Runtime/Compositor/CompositionManager.cs b/com.unity.render-pipelines.high-definition/Runtime/Compositor/CompositionManager.cs index 3f6caac093f..df1272d3647 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Compositor/CompositionManager.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Compositor/CompositionManager.cs @@ -551,6 +551,10 @@ void OnDestroy() CoreUtils.Destroy(volume); } } + + // We don't need the custom passes anymore + var hdPipeline = RenderPipelineManager.currentPipeline as HDRenderPipeline; + UnRegisterCustomPasses(hdPipeline); } public void AddInputFilterAtLayer(CompositionFilter filter, int index)