From c73b48fde7b56a42b57488b386f5ccb51f48894d Mon Sep 17 00:00:00 2001 From: Julien Ignace Date: Tue, 28 Apr 2020 14:18:04 +0200 Subject: [PATCH 1/2] Added a function (HDRenderPipeline.ResetRTHandleReferenceSize) to reset the reference size of RTHandle systems. --- .../Runtime/RenderGraph/RenderGraph.cs | 11 +++++++++++ .../RenderGraph/RenderGraphResourceRegistry.cs | 5 +++++ .../Runtime/Textures/BufferedRTHandleSystem.cs | 11 +++++++++++ .../Runtime/Textures/RTHandleSystem.cs | 16 ++++++++++++++-- .../Runtime/Textures/RTHandles.cs | 10 ++++++++++ .../Runtime/RenderPipeline/Camera/HDCamera.cs | 14 ++++++++++++++ .../Runtime/RenderPipeline/HDRenderPipeline.cs | 18 ++++++++++++++++-- 7 files changed, 81 insertions(+), 4 deletions(-) diff --git a/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraph.cs b/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraph.cs index 34f7b894800..c8a1b3a32ae 100644 --- a/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraph.cs +++ b/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraph.cs @@ -242,6 +242,17 @@ public void UnRegisterDebug() //m_DebugParameters.UnRegisterDebug(); } + /// + /// Resets the reference size of the internal RTHandle System. + /// This allows users to reduce the memory footprint of render textures after doing a super sampled rendering pass for example. + /// + /// New width of the internal RTHandle System. + /// New height of the internal RTHandle System. + public void ResetRTHandleReferenceSize(int width, int height) + { + m_Resources.ResetRTHandleReferenceSize(width, height); + } + /// /// Import an external texture to the Render Graph. /// diff --git a/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResourceRegistry.cs b/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResourceRegistry.cs index 51c76b3bedc..3b8a66efc34 100644 --- a/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResourceRegistry.cs +++ b/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResourceRegistry.cs @@ -771,6 +771,11 @@ internal void Clear() #endif } + internal void ResetRTHandleReferenceSize(int width, int height) + { + m_RTHandleSystem.ResetReferenceSize(width, height); + } + internal void Cleanup() { foreach (var value in m_TexturePool) diff --git a/com.unity.render-pipelines.core/Runtime/Textures/BufferedRTHandleSystem.cs b/com.unity.render-pipelines.core/Runtime/Textures/BufferedRTHandleSystem.cs index 7d10f4fe881..51888e2e4aa 100644 --- a/com.unity.render-pipelines.core/Runtime/Textures/BufferedRTHandleSystem.cs +++ b/com.unity.render-pipelines.core/Runtime/Textures/BufferedRTHandleSystem.cs @@ -134,6 +134,17 @@ public void SwapAndSetReferenceSize(int width, int height, MSAASamples msaaSampl m_RTHandleSystem.SetReferenceSize(width, height, msaaSamples); } + /// + /// Reset the reference size of the system and reallocate all textures. + /// + /// New width. + /// New height. + public void ResetReferenceSize(int width, int height) + { + m_RTHandleSystem.ResetReferenceSize(width, height); + } + + void Swap() { foreach (var item in m_RTHandles) diff --git a/com.unity.render-pipelines.core/Runtime/Textures/RTHandleSystem.cs b/com.unity.render-pipelines.core/Runtime/Textures/RTHandleSystem.cs index e5ce6686619..77e517d38b2 100644 --- a/com.unity.render-pipelines.core/Runtime/Textures/RTHandleSystem.cs +++ b/com.unity.render-pipelines.core/Runtime/Textures/RTHandleSystem.cs @@ -125,13 +125,25 @@ internal void Remove(RTHandle rth) m_AutoSizedRTs.Remove(rth); } + /// + /// Reset the reference size of the system and reallocate all textures. + /// + /// New width. + /// New height. + public void ResetReferenceSize(int width, int height) + { + m_MaxWidths = width; + m_MaxHeights = height; + SetReferenceSize(width, height, m_ScaledRTCurrentMSAASamples, reset: true); + } + /// /// Sets the reference rendering size for subsequent rendering for the RTHandle System /// /// Reference rendering width for subsequent rendering. /// Reference rendering height for subsequent rendering. /// Number of MSAA samples for multisampled textures for subsequent rendering. - public void SetReferenceSize(int width, int height, MSAASamples msaaSamples) + public void SetReferenceSize(int width, int height, MSAASamples msaaSamples, bool reset = false) { m_RTHandleProperties.previousViewportSize = m_RTHandleProperties.currentViewportSize; m_RTHandleProperties.previousRenderTargetSize = m_RTHandleProperties.currentRenderTargetSize; @@ -140,7 +152,7 @@ public void SetReferenceSize(int width, int height, MSAASamples msaaSamples) width = Mathf.Max(width, 1); height = Mathf.Max(height, 1); - bool sizeChanged = width > GetMaxWidth() || height > GetMaxHeight(); + bool sizeChanged = width > GetMaxWidth() || height > GetMaxHeight() || reset; bool msaaSamplesChanged = (msaaSamples != m_ScaledRTCurrentMSAASamples); if (sizeChanged || msaaSamplesChanged) diff --git a/com.unity.render-pipelines.core/Runtime/Textures/RTHandles.cs b/com.unity.render-pipelines.core/Runtime/Textures/RTHandles.cs index a2d7c95c04b..eaeb4c2a483 100644 --- a/com.unity.render-pipelines.core/Runtime/Textures/RTHandles.cs +++ b/com.unity.render-pipelines.core/Runtime/Textures/RTHandles.cs @@ -304,5 +304,15 @@ MSAASamples msaaSamples msaaSamples ); } + + /// + /// Reset the reference size of the system and reallocate all textures. + /// + /// New width. + /// New height. + public static void ResetReferenceSize(int width, int height) + { + s_DefaultInstance.ResetReferenceSize(width, height); + } } } 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 74ad3623b32..ef686eaddb1 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 @@ -569,6 +569,20 @@ internal static void CleanUnused() s_Cleanup.Clear(); } + internal static void ResetAllHistoryRTHandleSystems(int width, int height) + { + foreach (var kvp in s_Cameras) + { + var hdCamera = kvp.Value; + var currentHistorySize = hdCamera.m_HistoryRTSystem.rtHandleProperties.currentRenderTargetSize; + // We only reset if the new size if smaller than current reference (otherwise we might increase the size of off screen camera with lower resolution than the new reference. + if (width < currentHistorySize.x || height < currentHistorySize.y) + { + hdCamera.m_HistoryRTSystem.ResetReferenceSize(width, height); + } + } + } + unsafe internal void UpdateShaderVariablesGlobalCB(ref ShaderVariablesGlobal cb, int frameCount) { bool taaEnabled = frameSettings.IsEnabled(FrameSettingsField.Postprocess) 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 ba05ca5c744..f6a6a5e6410 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs @@ -577,6 +577,20 @@ void ValidateResources() #endif + /// + /// Resets the reference size of the internal RTHandle System. + /// This allows users to reduce the memory footprint of render textures after doing a super sampled rendering pass for example. + /// + /// New width of the internal RTHandle System. + /// New height of the internal RTHandle System. + public void ResetRTHandleReferenceSize(int width, int height) + { + RTHandles.ResetReferenceSize(width, height); + HDCamera.ResetAllHistoryRTHandleSystems(width, height); + if (m_RenderGraph != null) + m_RenderGraph.ResetRTHandleReferenceSize(width, height); + } + void InitializeRenderTextures() { RenderPipelineSettings settings = m_Asset.currentPlatformRenderPipelineSettings; @@ -1004,7 +1018,7 @@ void DisposeProbeCameraPool() // Dispose of Render Pipeline can be call either by OnValidate() or by OnDisable(). // Inside an OnValidate() call we can't call a DestroyImmediate(). // Here we are releasing our singleton to not leak while doing a domain reload. - // However this is doing a call to DestroyImmediate(). + // However this is doing a call to DestroyImmediate(). // To workaround this, and was we only leak with Singleton while doing domain reload (and not in OnValidate) // we are detecting if we are in an OnValidate call and releasing the Singleton only if it is not the case. if (!m_Asset.isInOnValidateCall) @@ -3215,7 +3229,7 @@ void RenderDBuffer(HDCamera hdCamera, CommandBuffer cmd, ScriptableRenderContext { // We still bind black textures to make sure that something is bound (can be a problem on some platforms) m_DbufferManager.BindBlackTextures(cmd); - + // Bind buffer to make sure that something is bound . cmd.SetGlobalBuffer(HDShaderIDs._DecalPropertyMaskBufferSRV, m_DbufferManager.propertyMaskBuffer); From 72763ce9af578a847b2c4da33110636ea10ea249 Mon Sep 17 00:00:00 2001 From: Julien Ignace Date: Tue, 28 Apr 2020 14:18:32 +0200 Subject: [PATCH 2/2] Update 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 d2ab7d1072e..d343f80fe94 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -118,6 +118,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Added a DisplayInfo attribute to specify a name override and a display order for Volume Component fields (used only in default inspector for now). - Added Min distance to contact shadows. - Added support for Depth of Field in path tracing (by sampling the lens aperture). +- Added a function (HDRenderPipeline.ResetRTHandleReferenceSize) to reset the reference size of RTHandle systems. ### Fixed - Fix when rescale probe all direction below zero (1219246)