Skip to content

Commit 7a2f34c

Browse files
Pre warm RT Handle system so that reallocations are reduced. (#713)
* Pre warm rt handle system and initialize to screen width/height * changelog * not all was pushed Co-authored-by: Sebastien Lagarde <sebastien@unity3d.com>
1 parent 3ca86ce commit 7a2f34c

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphResourceRegistry.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@ private RenderGraphResourceRegistry()
449449

450450
internal RenderGraphResourceRegistry(bool supportMSAA, MSAASamples initialSampleCount, RenderGraphDebugParams renderGraphDebug, RenderGraphLogger logger)
451451
{
452-
m_RTHandleSystem.Initialize(1, 1, supportMSAA, initialSampleCount);
452+
// We initialize to screen width/height to avoid multiple realloc that can lead to inflated memory usage (as releasing of memory is delayed).
453+
m_RTHandleSystem.Initialize(Screen.width, Screen.height, supportMSAA, initialSampleCount);
453454
m_RenderGraphDebug = renderGraphDebug;
454455
m_Logger = logger;
455456
}

com.unity.render-pipelines.high-definition/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
775775
- Removed experimental namespace for ray tracing code.
776776
- Increase limit for max numbers of lights in UX
777777
- Removed direct use of BSDFData in the path tracing pass, delegated to the material instead.
778+
- Pre-warm the RTHandle system to reduce the amount of memory allocations and the total memory needed at all points.
778779

779780
## [7.1.1] - 2019-09-05
780781

com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -505,11 +505,6 @@ internal void Update(FrameSettings currentFrameSettings, HDRenderPipeline hdrp,
505505

506506
HDRenderPipeline.UpdateVolumetricBufferParams(this, hdrp.GetFrameCount());
507507
HDRenderPipeline.ResizeVolumetricHistoryBuffers(this, hdrp.GetFrameCount());
508-
509-
// 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.
510-
// This is necessary because we assume that after post processes, we have the full size render target for debug rendering
511-
// The only point of calling this here is to grow the render targets. The call in BeginRender will setup the current RTHandle viewport size.
512-
RTHandles.SetReferenceSize(nonScaledViewport.x, nonScaledViewport.y, msaaSamples);
513508
}
514509

515510
/// <summary>Set the RTHandle scale to the actual camera size (can be scaled)</summary>

com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ public HDRenderPipeline(HDRenderPipelineAsset asset, HDRenderPipelineAsset defau
422422

423423
// Initial state of the RTHandle system.
424424
// Tells the system that we will require MSAA or not so that we can avoid wasteful render texture allocation.
425-
// TODO: Might want to initialize to at least the window resolution to avoid un-necessary re-alloc in the player
426-
RTHandles.Initialize(1, 1, m_Asset.currentPlatformRenderPipelineSettings.supportMSAA, m_Asset.currentPlatformRenderPipelineSettings.msaaSampleCount);
425+
// We initialize to screen width/height to avoid multiple realloc that can lead to inflated memory usage (as releasing of memory is delayed).
426+
RTHandles.Initialize(Screen.width, Screen.height, m_Asset.currentPlatformRenderPipelineSettings.supportMSAA, m_Asset.currentPlatformRenderPipelineSettings.msaaSampleCount);
427427

428428
m_XRSystem = new XRSystem(asset.renderPipelineResources.shaders);
429429
m_GPUCopy = new GPUCopy(defaultResources.shaders.copyChannelCS);
@@ -2007,6 +2007,29 @@ ref _cullingResults
20072007

20082008
using (new ProfilingScope(null, ProfilingSampler.Get(HDProfileId.HDRenderPipelineAllRenderRequest)))
20092009
{
2010+
2011+
// Warm up the RTHandle system so that it gets init to the maximum resolution available (avoiding to call multiple resizes
2012+
// that can lead to high memory spike as the memory release is delayed while the creation is immediate).
2013+
{
2014+
Vector2Int maxSize = new Vector2Int(1, 1);
2015+
2016+
for (int i = renderRequestIndicesToRender.Count - 1; i >= 0; --i)
2017+
{
2018+
var renderRequestIndex = renderRequestIndicesToRender[i];
2019+
var renderRequest = renderRequests[renderRequestIndex];
2020+
var hdCamera = renderRequest.hdCamera;
2021+
2022+
maxSize.x = Math.Max((int)hdCamera.finalViewport.size.x, maxSize.x);
2023+
maxSize.y = Math.Max((int)hdCamera.finalViewport.size.y, maxSize.y);
2024+
}
2025+
2026+
// 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.
2027+
// This is necessary because we assume that after post processes, we have the full size render target for debug rendering
2028+
// The only point of calling this here is to grow the render targets. The call in BeginRender will setup the current RTHandle viewport size.
2029+
RTHandles.SetReferenceSize(maxSize.x, maxSize.y, m_MSAASamples);
2030+
}
2031+
2032+
20102033
// Execute render request graph, in reverse order
20112034
for (int i = renderRequestIndicesToRender.Count - 1; i >= 0; --i)
20122035
{

0 commit comments

Comments
 (0)