From b09e9df2d43e4d1bf976cedd12ff31340219ad9f Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Wed, 3 Jun 2020 12:08:16 +0200 Subject: [PATCH 1/3] Pre warm rt handle system and initialize to screen width/height --- .../RenderGraphResourceRegistry.cs | 3 ++- .../Runtime/RenderPipeline/Camera/HDCamera.cs | 5 ---- .../RenderPipeline/HDRenderPipeline.cs | 25 ++++++++++++++++++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResourceRegistry.cs b/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResourceRegistry.cs index aba42596776..349848b6df0 100644 --- a/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResourceRegistry.cs +++ b/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResourceRegistry.cs @@ -449,7 +449,8 @@ private RenderGraphResourceRegistry() internal RenderGraphResourceRegistry(bool supportMSAA, MSAASamples initialSampleCount, RenderGraphDebugParams renderGraphDebug, RenderGraphLogger logger) { - m_RTHandleSystem.Initialize(1, 1, supportMSAA, initialSampleCount); + // We initialize to screen width/height to avoid multiple realloc that can lead to inflated memory usage (as releasing of memory is delayed). + m_RTHandleSystem.Initialize(Screen.width, Screen.height, supportMSAA, initialSampleCount); m_RenderGraphDebug = renderGraphDebug; m_Logger = logger; } diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs index e77c0825e69..5783b994550 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs @@ -499,11 +499,6 @@ internal void Update(FrameSettings currentFrameSettings, HDRenderPipeline hdrp, HDRenderPipeline.UpdateVolumetricBufferParams(this, hdrp.GetFrameCount()); HDRenderPipeline.ResizeVolumetricHistoryBuffers(this, hdrp.GetFrameCount()); - - // Here we use the non scaled resolution for the RTHandleSystem ref size because we assume that at some point we will need full resolution anyway. - // This is necessary because we assume that after post processes, we have the full size render target for debug rendering - // The only point of calling this here is to grow the render targets. The call in BeginRender will setup the current RTHandle viewport size. - RTHandles.SetReferenceSize(nonScaledViewport.x, nonScaledViewport.y, msaaSamples); } /// Set the RTHandle scale to the actual camera size (can be scaled) 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 a95a9afcc82..6a864662d3d 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs @@ -400,7 +400,7 @@ public HDRenderPipeline(HDRenderPipelineAsset asset, HDRenderPipelineAsset defau // Initial state of the RTHandle system. // Tells the system that we will require MSAA or not so that we can avoid wasteful render texture allocation. - // TODO: Might want to initialize to at least the window resolution to avoid un-necessary re-alloc in the player + // We initialize to screen width/height to avoid multiple realloc that can lead to inflated memory usage (as releasing of memory is delayed). RTHandles.Initialize(1, 1, m_Asset.currentPlatformRenderPipelineSettings.supportMSAA, m_Asset.currentPlatformRenderPipelineSettings.msaaSampleCount); m_XRSystem = new XRSystem(asset.renderPipelineResources.shaders); @@ -1980,6 +1980,29 @@ ref _cullingResults using (new ProfilingScope(null, ProfilingSampler.Get(HDProfileId.HDRenderPipelineAllRenderRequest))) { + + // Warm up the RTHandle system so that it gets init to the maximum resolution available (avoiding to call multiple resizes + // that can lead to high memory spike as the memory release is delayed while the creation is immediate). + { + Vector2Int maxSize = new Vector2Int(1, 1); + + for (int i = renderRequestIndicesToRender.Count - 1; i >= 0; --i) + { + var renderRequestIndex = renderRequestIndicesToRender[i]; + var renderRequest = renderRequests[renderRequestIndex]; + var hdCamera = renderRequest.hdCamera; + + maxSize.x = Math.Max((int)hdCamera.finalViewport.size.x, maxSize.x); + maxSize.y = Math.Max((int)hdCamera.finalViewport.size.y, maxSize.y); + } + + // Here we use the non scaled resolution for the RTHandleSystem ref size because we assume that at some point we will need full resolution anyway. + // This is necessary because we assume that after post processes, we have the full size render target for debug rendering + // The only point of calling this here is to grow the render targets. The call in BeginRender will setup the current RTHandle viewport size. + RTHandles.SetReferenceSize(maxSize.x, maxSize.y, m_MSAASamples); + } + + // Execute render request graph, in reverse order for (int i = renderRequestIndicesToRender.Count - 1; i >= 0; --i) { From 4adea085bbe0296bf614320f7617a72325c0957e Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Wed, 3 Jun 2020 12:11:47 +0200 Subject: [PATCH 2/3] changelog --- com.unity.render-pipelines.high-definition/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index 69b3a8bb1cd..932163fee82 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -763,6 +763,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Refactored shadow caching system. - Removed experimental namespace for ray tracing code. - Increase limit for max numbers of lights in UX +- Pre-warm the RTHandle system to reduce the amount of memory allocations and the total memory needed at all points. ## [7.1.1] - 2019-09-05 From 698a3d73ee69a8f94a07615f650f977c7b701b02 Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Wed, 3 Jun 2020 12:19:07 +0200 Subject: [PATCH 3/3] not all was pushed --- .../Runtime/RenderPipeline/HDRenderPipeline.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 6a864662d3d..d3238bd3613 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs @@ -401,7 +401,7 @@ public HDRenderPipeline(HDRenderPipelineAsset asset, HDRenderPipelineAsset defau // Initial state of the RTHandle system. // Tells the system that we will require MSAA or not so that we can avoid wasteful render texture allocation. // We initialize to screen width/height to avoid multiple realloc that can lead to inflated memory usage (as releasing of memory is delayed). - RTHandles.Initialize(1, 1, m_Asset.currentPlatformRenderPipelineSettings.supportMSAA, m_Asset.currentPlatformRenderPipelineSettings.msaaSampleCount); + RTHandles.Initialize(Screen.width, Screen.height, m_Asset.currentPlatformRenderPipelineSettings.supportMSAA, m_Asset.currentPlatformRenderPipelineSettings.msaaSampleCount); m_XRSystem = new XRSystem(asset.renderPipelineResources.shaders); m_GPUCopy = new GPUCopy(defaultResources.shaders.copyChannelCS);