Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ public enum TextureSizeMode
Functor
}

/// <summary>
/// Subset of the texture desc containing information for fast memory allocation (when platform supports it)
/// </summary>
public struct FastMemoryDesc
{
///<summary>Whether the texture will be in fast memory.</summary>
public bool inFastMemory;
///<summary>Flag to determine what parts of the render target is spilled if not fully resident in fast memory.</summary>
public FastMemoryFlags flags;
///<summary>How much of the render target is to be switched into fast memory (between 0 and 1).</summary>
public float residencyFraction;
}

/// <summary>
/// Descriptor used to create texture resources
/// </summary>
Expand Down Expand Up @@ -169,6 +182,8 @@ public struct TextureDesc
public RenderTextureMemoryless memoryless;
///<summary>Texture name.</summary>
public string name;
///<summary>Descriptor to determine how the texture will be in fast memory on platform that supports it.</summary>
public FastMemoryDesc fastMemoryDesc;

// Initial state. Those should not be used in the hash
///<summary>Texture needs to be cleared on first use.</summary>
Expand Down Expand Up @@ -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;
Expand Down
46 changes: 46 additions & 0 deletions com.unity.render-pipelines.core/Runtime/Textures/RTHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,51 @@ public Vector2Int GetScaledSize(Vector2Int refSize)
);
}
}

/// <summary>
/// Switch the render target to fast memory on platform that have it.
/// </summary>
/// <param name="cmd">Command buffer used for rendering.</param>
/// <param name="residencyFraction">How much of the render target is to be switched into fast memory (between 0 and 1).</param>
/// <param name="flags">Flag to determine what parts of the render target is spilled if not fully resident in fast memory.</param>
/// <param name="copyContents">Whether the content of render target are copied or not when switching to fast memory.</param>

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
}

/// <summary>
/// Switch the render target to fast memory on platform that have it and copies the content.
/// </summary>
/// <param name="cmd">Command buffer used for rendering.</param>
/// <param name="residencyFraction">How much of the render target is to be switched into fast memory (between 0 and 1).</param>
/// <param name="flags">Flag to determine what parts of the render target is spilled if not fully resident in fast memory.</param>
public void CopyToFastMemory(CommandBuffer cmd,
float residencyFraction = 1.0f,
FastMemoryFlags flags = FastMemoryFlags.SpillTop
)
{
SwitchToFastMemory(cmd, residencyFraction, flags, copyContents: true);
}

/// <summary>
/// Switch out the render target from fast memory back to main memory on platforms that have fast memory.
/// </summary>
/// <param name="cmd">Command buffer used for rendering.</param>
/// <param name="copyContents">Whether the content of render target are copied or not when switching out fast memory.</param>
public void SwitchOutFastMemory(CommandBuffer cmd, bool copyContents = true)
{
#if UNITY_2020_2_OR_NEWER
cmd.SwitchOutOfFastMemory(m_RT, copyContents);
#endif
}
}
}
1 change: 1 addition & 0 deletions com.unity.render-pipelines.high-definition/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,13 +647,35 @@ void ValidateResources()

#endif

/// <summary>
/// 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.
/// </summary>
/// <param name="width">New width of the internal RTHandle System.</param>
/// <param name="height">New height of the internal RTHandle System.</param>
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);
}

/// <summary>
/// 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.
/// </summary>
/// <param name="width">New width of the internal RTHandle System.</param>
/// <param name="height">New height of the internal RTHandle System.</param>
public void ResetRTHandleReferenceSize(int width, int height)
{
RTHandles.ResetReferenceSize(width, height);
HDCamera.ResetAllHistoryRTHandleSystems(width, height);
Expand Down Expand Up @@ -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
Expand Down