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/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index d3c3433fe8d..edc031712eb 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -775,6 +775,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Removed experimental namespace for ray tracing code. - Increase limit for max numbers of lights in UX - Removed direct use of BSDFData in the path tracing pass, delegated to the material instead. +- 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 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 ea75fec6d2e..95b53beba04 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 @@ -505,11 +505,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 13c4c04d5df..b32cb87c693 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs @@ -422,8 +422,8 @@ 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 - RTHandles.Initialize(1, 1, m_Asset.currentPlatformRenderPipelineSettings.supportMSAA, m_Asset.currentPlatformRenderPipelineSettings.msaaSampleCount); + // 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(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); @@ -2007,6 +2007,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) {