From 0b358773f2e2fa3463c1704f5527e9161ec0fa0c Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Thu, 4 Jun 2020 16:32:43 +0200 Subject: [PATCH 1/5] temp, gonna revert this, but have to merge staging. --- .../Runtime/RenderPipeline/HDRenderPipeline.cs | 2 ++ 1 file changed, 2 insertions(+) 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 cfe1b11c1b0..e6cf4bd64af 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs @@ -2115,6 +2115,8 @@ AOVRequestData aovRequest hdCamera.BeginRender(cmd); m_CurrentHDCamera = hdCamera; + m_SharedRTManager.GetNormalBuffer().SwitchToFastMemory(cmd); + if (m_RayTracingSupported) { // This call need to happen once per camera From 3268b241e7abd2fe6cb2471871cc6ea9445f17c5 Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Fri, 5 Jun 2020 09:47:26 +0200 Subject: [PATCH 2/5] again, need to switch branch, nothing functional --- .../Runtime/Textures/RTHandle.cs | 19 +++++++++++++++++++ .../RenderPipeline/HDRenderPipeline.cs | 13 ++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/com.unity.render-pipelines.core/Runtime/Textures/RTHandle.cs b/com.unity.render-pipelines.core/Runtime/Textures/RTHandle.cs index 9a87fa0c8d3..a2ddba8aa7e 100644 --- a/com.unity.render-pipelines.core/Runtime/Textures/RTHandle.cs +++ b/com.unity.render-pipelines.core/Runtime/Textures/RTHandle.cs @@ -145,5 +145,24 @@ public Vector2Int GetScaledSize(Vector2Int refSize) ); } } + + public void SwitchToFastMemory(CommandBuffer cmd, + float residencyFraction = 1.0f, + FastMemoryFlags flags = FastMemoryFlags.SpillTop, + bool copyContents = false + ) + { + // TODO: SOME STATE ON RTHANDLE TO CHECK IF WE NEED TO DO IT OR WE ALREADY ARE IN ESRAM. + residencyFraction = Mathf.Clamp01(residencyFraction); // To avoid issues on user side. + cmd.SwitchIntoFastMemory(m_RT, flags, residencyFraction, copyContents); + } + + public void CopyToFastMemory(CommandBuffer cmd, + float residencyFraction = 1.0f, + FastMemoryFlags flags = FastMemoryFlags.SpillTop + ) + { + SwitchToFastMemory(cmd, residencyFraction, flags, copyContents: true); + } } } 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 4e0bf93ada6..62f74748bbf 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs @@ -2120,7 +2120,18 @@ AOVRequestData aovRequest hdCamera.BeginRender(cmd); m_CurrentHDCamera = hdCamera; - m_SharedRTManager.GetNormalBuffer().SwitchToFastMemory(cmd); + //m_SharedRTManager.GetNormalBuffer().SwitchToFastMemory(cmd); + //var rts = m_GbufferManager.GetBuffers(); + //foreach (var gbuff in rts) + //{ + // if (gbuff != null) + // gbuff.SwitchToFastMemory(cmd); + //} + //m_SharedRTManager.GetMotionVectorsBuffer().SwitchToFastMemory(cmd); + //m_SharedRTManager.GetDepthTexture().SwitchToFastMemory(cmd); + //m_SharedRTManager.GetNormalBuffer().SwitchToFastMemory(cmd); + //m_SharedRTManager.GetNormalBuffer().SwitchToFastMemory(cmd); + //m_SharedRTManager.GetNormalBuffer().SwitchToFastMemory(cmd); if (m_RayTracingSupported) { From ec9f1a3aa07168e17c3cbffc04c9652611db74a2 Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Mon, 8 Jun 2020 17:55:40 +0200 Subject: [PATCH 3/5] ESRAM support --- .../RenderGraphResourceRegistry.cs | 21 +++++++++++ .../Runtime/Textures/RTHandle.cs | 27 ++++++++++++-- .../HDRenderPipeline.Prepass.cs | 10 ++++-- .../HDRenderPipeline.RenderGraph.cs | 9 ++++- .../RenderPipeline/HDRenderPipeline.cs | 35 ++++++++++++------- 5 files changed, 85 insertions(+), 17 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..b9fe97e224a 100644 --- a/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResourceRegistry.cs +++ b/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResourceRegistry.cs @@ -118,6 +118,19 @@ public enum TextureSizeMode Functor } + /// + /// Subset of the texture desc containing information for fast memory allocation (when platform supports it) + /// + public struct FastMemoryDesc + { + ///Whether the texture will be in fast memory. + public bool inFastMemory; + ///Flag to determine what parts of the render target is spilled if not fully resident in fast memory. + public FastMemoryFlags flags; + ///How much of the render target is to be switched into fast memory (between 0 and 1). + public float residencyFraction; + } + /// /// Descriptor used to create texture resources /// @@ -169,6 +182,8 @@ public struct TextureDesc public RenderTextureMemoryless memoryless; ///Texture name. public string name; + ///Descriptor to determine how the texture will be in fast memory on platform that supports it. + public FastMemoryDesc fastMemoryDesc; // Initial state. Those should not be used in the hash ///Texture needs to be cleared on first use. @@ -543,6 +558,12 @@ internal void CreateAndClearTexture(RenderGraphContext rgContext, TextureHandle { CreateTextureForPass(ref resource); + var fastMemDesc = resource.desc.fastMemoryDesc; + if(fastMemDesc.inFastMemory) + { + resource.rt.SwitchToFastMemory(rgContext.cmd, fastMemDesc.residencyFraction, fastMemDesc.flags); + } + if (resource.desc.clearBuffer || m_RenderGraphDebug.clearRenderTargetsAtCreation) { bool debugClear = m_RenderGraphDebug.clearRenderTargetsAtCreation && !resource.desc.clearBuffer; diff --git a/com.unity.render-pipelines.core/Runtime/Textures/RTHandle.cs b/com.unity.render-pipelines.core/Runtime/Textures/RTHandle.cs index a2ddba8aa7e..a3962153d21 100644 --- a/com.unity.render-pipelines.core/Runtime/Textures/RTHandle.cs +++ b/com.unity.render-pipelines.core/Runtime/Textures/RTHandle.cs @@ -146,17 +146,30 @@ public Vector2Int GetScaledSize(Vector2Int refSize) } } + /// + /// Switch the render target to fast memory on platform that have it. + /// + /// Command buffer used for rendering. + /// How much of the render target is to be switched into fast memory (between 0 and 1). + /// Flag to determine what parts of the render target is spilled if not fully resident in fast memory. + /// Whether the content of render target are copied or not when switching to fast memory. + public void SwitchToFastMemory(CommandBuffer cmd, float residencyFraction = 1.0f, FastMemoryFlags flags = FastMemoryFlags.SpillTop, bool copyContents = false ) { - // TODO: SOME STATE ON RTHANDLE TO CHECK IF WE NEED TO DO IT OR WE ALREADY ARE IN ESRAM. - residencyFraction = Mathf.Clamp01(residencyFraction); // To avoid issues on user side. + residencyFraction = Mathf.Clamp01(residencyFraction); cmd.SwitchIntoFastMemory(m_RT, flags, residencyFraction, copyContents); } + /// + /// Switch the render target to fast memory on platform that have it and copies the content. + /// + /// Command buffer used for rendering. + /// How much of the render target is to be switched into fast memory (between 0 and 1). + /// Flag to determine what parts of the render target is spilled if not fully resident in fast memory. public void CopyToFastMemory(CommandBuffer cmd, float residencyFraction = 1.0f, FastMemoryFlags flags = FastMemoryFlags.SpillTop @@ -164,5 +177,15 @@ public void CopyToFastMemory(CommandBuffer cmd, { SwitchToFastMemory(cmd, residencyFraction, flags, copyContents: true); } + + /// + /// Switch out the render target from fast memory back to main memory on platforms that have fast memory. + /// + /// Command buffer used for rendering. + /// Whether the content of render target are copied or not when switching out fast memory. + public void SwitchOutFastMemory(CommandBuffer cmd, bool copyContents = true) + { + cmd.SwitchOutOfFastMemory(m_RT, copyContents); + } } } diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.Prepass.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.Prepass.cs index c6bafbc3155..2ae006dcf1f 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.Prepass.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.Prepass.cs @@ -330,12 +330,18 @@ void SetupGBufferTargets(RenderGraph renderGraph, HDCamera hdCamera, GBufferPass passData.depthBuffer = builder.UseDepthBuffer(prepassOutput.depthBuffer, DepthAccess.ReadWrite); passData.gbufferRT[0] = builder.UseColorBuffer(sssBuffer, 0); passData.gbufferRT[1] = builder.UseColorBuffer(prepassOutput.normalBuffer, 1); + + FastMemoryDesc gbufferFastMemDesc; + gbufferFastMemDesc.inFastMemory = true; + gbufferFastMemDesc.residencyFraction = 1.0f; + gbufferFastMemDesc.flags = FastMemoryFlags.SpillTop; + // If we are in deferred mode and the SSR is enabled, we need to make sure that the second gbuffer is cleared given that we are using that information for clear coat selection bool clearGBuffer2 = clearGBuffer || hdCamera.IsSSREnabled(); passData.gbufferRT[2] = builder.UseColorBuffer(renderGraph.CreateTexture( - new TextureDesc(Vector2.one, true, true) { colorFormat = GraphicsFormat.R8G8B8A8_UNorm, clearBuffer = clearGBuffer2, clearColor = Color.clear, name = "GBuffer2" }, HDShaderIDs._GBufferTexture[2]), 2); + new TextureDesc(Vector2.one, true, true) { colorFormat = GraphicsFormat.R8G8B8A8_UNorm, clearBuffer = clearGBuffer2, clearColor = Color.clear, name = "GBuffer2", fastMemoryDesc = gbufferFastMemDesc }, HDShaderIDs._GBufferTexture[2]), 2); passData.gbufferRT[3] = builder.UseColorBuffer(renderGraph.CreateTexture( - new TextureDesc(Vector2.one, true, true) { colorFormat = Builtin.GetLightingBufferFormat(), clearBuffer = clearGBuffer, clearColor = Color.clear, name = "GBuffer3" }, HDShaderIDs._GBufferTexture[3]), 3); + new TextureDesc(Vector2.one, true, true) { colorFormat = Builtin.GetLightingBufferFormat(), clearBuffer = clearGBuffer, clearColor = Color.clear, name = "GBuffer3", fastMemoryDesc = gbufferFastMemDesc }, HDShaderIDs._GBufferTexture[3]), 3); prepassOutput.gbuffer.lightLayersTextureIndex = -1; int currentIndex = 4; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs index 2c0c9a1b3fd..857f805c16f 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs @@ -963,6 +963,11 @@ void RenderDistortion( RenderGraph renderGraph, TextureHandle CreateColorBuffer(RenderGraph renderGraph, HDCamera hdCamera, bool msaa) { + FastMemoryDesc colorFastMemDesc; + colorFastMemDesc.inFastMemory = true; + colorFastMemDesc.residencyFraction = 1.0f; + colorFastMemDesc.flags = FastMemoryFlags.SpillTop; + return renderGraph.CreateTexture( new TextureDesc(Vector2.one, true, true) { @@ -972,7 +977,9 @@ TextureHandle CreateColorBuffer(RenderGraph renderGraph, HDCamera hdCamera, bool enableMSAA = msaa, clearBuffer = NeedClearColorBuffer(hdCamera), clearColor = GetColorBufferClearColor(hdCamera), - name = string.Format("CameraColor{0}", msaa ? "MSAA" : "")}); + name = string.Format("CameraColor{0}", msaa ? "MSAA" : ""), + fastMemoryDesc = colorFastMemDesc + }); } class ResolveColorData 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 62f74748bbf..9812138e8c2 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs @@ -613,6 +613,28 @@ void ValidateResources() #endif + internal void SwitchRenderTargetsToFastMem(CommandBuffer cmd, HDCamera camera) + { + // Color and normal buffer will always be in fast memory + m_CameraColorBuffer.SwitchToFastMemory(cmd, residencyFraction: 1.0f, FastMemoryFlags.SpillTop, copyContents: false); + m_SharedRTManager.GetNormalBuffer().SwitchToFastMemory(cmd, residencyFraction: 1.0f, FastMemoryFlags.SpillTop, copyContents: false); + // Following might need to change depending on context... TODO: Do a deep investigation of projects we have to check what is the most beneficial. + RenderPipelineSettings settings = m_Asset.currentPlatformRenderPipelineSettings; + + if (settings.supportedLitShaderMode != RenderPipelineSettings.SupportedLitShaderMode.ForwardOnly) + { + // Switch gbuffers to fast memory when we are in deferred + var buffers = m_GbufferManager.GetBuffers(); + foreach (var buffer in buffers) + { + buffer.SwitchToFastMemory(cmd, residencyFraction: 1.0f, FastMemoryFlags.SpillTop, copyContents: false); + } + } + + // Trying to fit the depth pyramid + m_SharedRTManager.GetDepthTexture().SwitchToFastMemory(cmd, residencyFraction: 1.0f, FastMemoryFlags.SpillTop, false); + } + /// /// 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. @@ -2120,18 +2142,7 @@ AOVRequestData aovRequest hdCamera.BeginRender(cmd); m_CurrentHDCamera = hdCamera; - //m_SharedRTManager.GetNormalBuffer().SwitchToFastMemory(cmd); - //var rts = m_GbufferManager.GetBuffers(); - //foreach (var gbuff in rts) - //{ - // if (gbuff != null) - // gbuff.SwitchToFastMemory(cmd); - //} - //m_SharedRTManager.GetMotionVectorsBuffer().SwitchToFastMemory(cmd); - //m_SharedRTManager.GetDepthTexture().SwitchToFastMemory(cmd); - //m_SharedRTManager.GetNormalBuffer().SwitchToFastMemory(cmd); - //m_SharedRTManager.GetNormalBuffer().SwitchToFastMemory(cmd); - //m_SharedRTManager.GetNormalBuffer().SwitchToFastMemory(cmd); + SwitchRenderTargetsToFastMem(cmd, hdCamera); if (m_RayTracingSupported) { From 18813c2b544a28fb5961bb86091feff061530c40 Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Mon, 8 Jun 2020 18:04:46 +0200 Subject: [PATCH 4/5] 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 8f24bb9b2c0..76b1eacf949 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -135,6 +135,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Added CustomPassUtils API to simplify Blur, Copy and DrawRenderers custom passes. - Added an API in HDRP to override the camera within the rendering of a frame (mainly for custom pass). - Added more custom pass API functions, mainly to render objects from another camera. +- Added Fast Memory support for platform that support it. ### Fixed - Fix when rescale probe all direction below zero (1219246) From b9311f210ff207a3eb2b16969573d9e4497917da Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Mon, 8 Jun 2020 20:10:40 +0200 Subject: [PATCH 5/5] Add guards --- com.unity.render-pipelines.core/Runtime/Textures/RTHandle.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/com.unity.render-pipelines.core/Runtime/Textures/RTHandle.cs b/com.unity.render-pipelines.core/Runtime/Textures/RTHandle.cs index a3962153d21..68dd42878ef 100644 --- a/com.unity.render-pipelines.core/Runtime/Textures/RTHandle.cs +++ b/com.unity.render-pipelines.core/Runtime/Textures/RTHandle.cs @@ -160,8 +160,10 @@ public void SwitchToFastMemory(CommandBuffer cmd, bool copyContents = false ) { +#if UNITY_2020_2_OR_NEWER residencyFraction = Mathf.Clamp01(residencyFraction); cmd.SwitchIntoFastMemory(m_RT, flags, residencyFraction, copyContents); +#endif } /// @@ -185,7 +187,9 @@ public void CopyToFastMemory(CommandBuffer cmd, /// Whether the content of render target are copied or not when switching out fast memory. public void SwitchOutFastMemory(CommandBuffer cmd, bool copyContents = true) { +#if UNITY_2020_2_OR_NEWER cmd.SwitchOutOfFastMemory(m_RT, copyContents); +#endif } } }