Skip to content

Commit dbad64a

Browse files
FrancescoC-unitysebastienlagarde
authored andcommitted
Pre warm RT Handle system so that reallocations are reduced. #713
1 parent fda10a8 commit dbad64a

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
@@ -324,7 +324,8 @@ private RenderGraphResourceRegistry()
324324

325325
internal RenderGraphResourceRegistry(bool supportMSAA, MSAASamples initialSampleCount, RenderGraphDebugParams renderGraphDebug, RenderGraphLogger logger)
326326
{
327-
m_RTHandleSystem.Initialize(1, 1, supportMSAA, initialSampleCount);
327+
// We initialize to screen width/height to avoid multiple realloc that can lead to inflated memory usage (as releasing of memory is delayed).
328+
m_RTHandleSystem.Initialize(Screen.width, Screen.height, supportMSAA, initialSampleCount);
328329
m_RenderGraphDebug = renderGraphDebug;
329330
m_Logger = logger;
330331
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
137137
- Fix MaterialBalls having same guid issue
138138
- Fix spelling and grammatical errors in material samples
139139
- Fixed issues with scene view and transparent motion vectors.
140+
- Pre-warm the RTHandle system to reduce the amount of memory allocations and the total memory needed at all points.
140141

141142
### Changed
142143
- Rejecting history for ray traced reflections based on a threshold evaluated on the neighborhood of the sampled history.

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
@@ -477,11 +477,6 @@ internal void Update(FrameSettings currentFrameSettings, HDRenderPipeline hdrp,
477477
cameraFrameCount++;
478478

479479
hdrp.UpdateVolumetricBufferParams(this);
480-
481-
// 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.
482-
// This is necessary because we assume that after post processes, we have the full size render target for debug rendering
483-
// The only point of calling this here is to grow the render targets. The call in BeginRender will setup the current RTHandle viewport size.
484-
RTHandles.SetReferenceSize(nonScaledViewport.x, nonScaledViewport.y, msaaSamples);
485480
}
486481

487482
// Updating RTHandle needs to be done at the beginning of rendering (not during update of HDCamera which happens in batches)

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

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

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

383383
m_XRSystem = new XRSystem(asset.renderPipelineResources.shaders);
384384
m_GPUCopy = new GPUCopy(defaultResources.shaders.copyChannelCS);
@@ -1811,6 +1811,29 @@ ref _cullingResults
18111811

18121812
using (new ProfilingScope(null, ProfilingSampler.Get(HDProfileId.HDRenderPipelineAllRenderRequest)))
18131813
{
1814+
1815+
// Warm up the RTHandle system so that it gets init to the maximum resolution available (avoiding to call multiple resizes
1816+
// that can lead to high memory spike as the memory release is delayed while the creation is immediate).
1817+
{
1818+
Vector2Int maxSize = new Vector2Int(1, 1);
1819+
1820+
for (int i = renderRequestIndicesToRender.Count - 1; i >= 0; --i)
1821+
{
1822+
var renderRequestIndex = renderRequestIndicesToRender[i];
1823+
var renderRequest = renderRequests[renderRequestIndex];
1824+
var hdCamera = renderRequest.hdCamera;
1825+
1826+
maxSize.x = Math.Max((int)hdCamera.finalViewport.size.x, maxSize.x);
1827+
maxSize.y = Math.Max((int)hdCamera.finalViewport.size.y, maxSize.y);
1828+
}
1829+
1830+
// 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.
1831+
// This is necessary because we assume that after post processes, we have the full size render target for debug rendering
1832+
// The only point of calling this here is to grow the render targets. The call in BeginRender will setup the current RTHandle viewport size.
1833+
RTHandles.SetReferenceSize(maxSize.x, maxSize.y, m_MSAASamples);
1834+
}
1835+
1836+
18141837
// Execute render request graph, in reverse order
18151838
for (int i = renderRequestIndicesToRender.Count - 1; i >= 0; --i)
18161839
{

0 commit comments

Comments
 (0)