diff --git a/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResourceRegistry.cs b/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResourceRegistry.cs
index ad5997e6590..7de11bc5a74 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.
@@ -544,6 +559,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 9a87fa0c8d3..68dd42878ef 100644
--- a/com.unity.render-pipelines.core/Runtime/Textures/RTHandle.cs
+++ b/com.unity.render-pipelines.core/Runtime/Textures/RTHandle.cs
@@ -145,5 +145,51 @@ 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
+ )
+ {
+#if UNITY_2020_2_OR_NEWER
+ residencyFraction = Mathf.Clamp01(residencyFraction);
+ cmd.SwitchIntoFastMemory(m_RT, flags, residencyFraction, copyContents);
+#endif
+ }
+
+ ///
+ /// 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
+ )
+ {
+ 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)
+ {
+#if UNITY_2020_2_OR_NEWER
+ cmd.SwitchOutOfFastMemory(m_RT, copyContents);
+#endif
+ }
}
}
diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md
index f8283a25bdd..9f9eb198424 100644
--- a/com.unity.render-pipelines.high-definition/CHANGELOG.md
+++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md
@@ -143,6 +143,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Added presets quality settings for RTAO and RTGI.
- Added an override for the shadow culling that allows better directional shadow maps in ray tracing effects (RTR, RTGI, RTSSS and RR).
- Added a Cloud Layer volume override.
+- Added Fast Memory support for platform that support it.
### Fixed
- Fix when rescale probe all direction below zero (1219246)
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 05bc44ace30..240e0087e09 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
@@ -331,12 +331,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 2545d5366b8..f1f3a6f4bfa 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
@@ -982,6 +982,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)
{
@@ -991,7 +996,8 @@ TextureHandle CreateColorBuffer(RenderGraph renderGraph, HDCamera hdCamera, bool
enableMSAA = msaa,
clearBuffer = NeedClearColorBuffer(hdCamera),
clearColor = GetColorBufferClearColor(hdCamera),
- name = msaa ? "CameraColorMSAA" : "CameraColor"
+ name = msaa ? "CameraColorMSAA" : "CameraColor",
+ fastMemoryDesc = colorFastMemDesc
});
}
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 a4f08a4cc44..a2d43ca3e3d 100644
--- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs
+++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs
@@ -647,13 +647,35 @@ 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)
+ 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.
+ ///
+ /// 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);
@@ -2176,6 +2198,8 @@ AOVRequestData aovRequest
hdCamera.BeginRender(cmd);
m_CurrentHDCamera = hdCamera;
+ SwitchRenderTargetsToFastMem(cmd, hdCamera);
+
if (m_RayTracingSupported)
{
// This call need to happen once per camera