Skip to content

Commit b65e6ea

Browse files
axolotoEvergreen
authored andcommitted
Graphics/SRP - RenderGraph Consolidation - Replace RenderGraph ImportBackBuffer by ImportTexture API in HDRP
1 parent e33caa1 commit b65e6ea

3 files changed

Lines changed: 83 additions & 22 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ public TextureHandle ImportTexture(RTHandle rt, ImportResourceParams importParam
726726

727727
/// <summary>
728728
/// Import an external texture to the Render Graph. This overload should be used for RTHandles wrapping a RenderTargetIdentifier.
729-
/// If the RTHandle is wrapping a RenderTargetIdentifer, Rendergrpah can't derive the render texture's properties so the user has to provide this info to the graph through RenderTargetInfo.
729+
/// If the RTHandle is wrapping a RenderTargetIdentifer, Rendergraph can't derive the render texture's properties so the user has to provide this info to the graph through RenderTargetInfo.
730730
///
731731
/// Any pass writing to an imported texture will be considered having side effects and can't be automatically culled.
732732
///

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

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ void RecordRenderGraph(RenderRequest renderRequest,
8383

8484
UpdateParentExposure(m_RenderGraph, hdCamera);
8585

86-
TextureHandle backBuffer = m_RenderGraph.ImportBackbuffer(target.id);
86+
TextureHandle colorBackBuffer = ImportColorBackBuffer(m_RenderGraph, target.colorId);
87+
TextureHandle depthBackBuffer = ImportDepthBackBuffer(m_RenderGraph, target.depthId);
88+
8789
TextureHandle colorBuffer = CreateColorBuffer(m_RenderGraph, hdCamera, msaa, true);
8890
m_NonMSAAColorBuffer = CreateColorBuffer(m_RenderGraph, hdCamera, false);
8991
TextureHandle currentColorPyramid = m_RenderGraph.ImportTexture(hdCamera.GetCurrentFrameRT((int)HDCameraFrameHistoryType.ColorBufferMipChain));
@@ -348,7 +350,7 @@ void RecordRenderGraph(RenderRequest renderRequest,
348350
bool postProcessIsFinalPass = HDUtils.PostProcessIsFinalPass(hdCamera, aovRequest);
349351
TextureHandle afterPostProcessBuffer = RenderAfterPostProcessObjects(m_RenderGraph, hdCamera, pathTracing, cullingResults, prepassOutput);
350352
var postProcessTargetFace = postProcessIsFinalPass ? target.face : CubemapFace.Unknown;
351-
TextureHandle postProcessDest = RenderPostProcess(m_RenderGraph, prepassOutput, colorBuffer, backBuffer, uiBuffer, afterPostProcessBuffer, opticalFogTransmittance, cullingResults, hdCamera, postProcessTargetFace, postProcessIsFinalPass);
353+
TextureHandle postProcessDest = RenderPostProcess(m_RenderGraph, prepassOutput, colorBuffer, colorBackBuffer, uiBuffer, afterPostProcessBuffer, opticalFogTransmittance, cullingResults, hdCamera, postProcessTargetFace, postProcessIsFinalPass);
352354

353355
var xyMapping = GenerateDebugHDRxyMapping(m_RenderGraph, hdCamera, postProcessDest);
354356
GenerateDebugImageHistogram(m_RenderGraph, hdCamera, postProcessDest);
@@ -370,8 +372,8 @@ void RecordRenderGraph(RenderRequest renderRequest,
370372
RenderCustomPass(m_RenderGraph, hdCamera, postProcessDest, prepassOutput, customPassCullingResults, cullingResults, CustomPassInjectionPoint.AfterPostProcess, aovRequest, aovCustomPassBuffers);
371373

372374
// Copy and rescale depth buffer for XR devices
373-
if (hdCamera.xr.enabled && hdCamera.xr.copyDepth)
374-
CopyDepth(m_RenderGraph, hdCamera, prepassOutput.resolvedDepthBuffer, backBuffer, true);
375+
if (hdCamera.xr.enabled && hdCamera.xr.copyDepth && depthBackBuffer.IsValid())
376+
CopyDepth(m_RenderGraph, hdCamera, prepassOutput.resolvedDepthBuffer, depthBackBuffer, true);
375377

376378
if (m_CurrentDebugDisplaySettings.data.historyBuffersView != -1)
377379
{
@@ -408,7 +410,7 @@ void RecordRenderGraph(RenderRequest renderRequest,
408410

409411
for (int viewIndex = 0; viewIndex < hdCamera.viewCount; ++viewIndex)
410412
{
411-
BlitFinalCameraTexture(m_RenderGraph, hdCamera, postProcessDest, backBuffer, uiBuffer, afterPostProcessBuffer, viewIndex, HDROutputActiveForCameraType(hdCamera), target.face);
413+
BlitFinalCameraTexture(m_RenderGraph, hdCamera, postProcessDest, colorBackBuffer, uiBuffer, afterPostProcessBuffer, viewIndex, HDROutputActiveForCameraType(hdCamera), target.face);
412414
}
413415

414416
if (aovRequest.isValid)
@@ -419,25 +421,25 @@ void RecordRenderGraph(RenderRequest renderRequest,
419421
// we need to do this separately.
420422
for (int viewIndex = 0; viewIndex < hdCamera.viewCount; ++viewIndex)
421423
{
422-
if (target.targetDepth != null)
424+
if (target.isProbe && depthBackBuffer.IsValid())
423425
{
424-
BlitFinalCameraTexture(m_RenderGraph, hdCamera, prepassOutput.resolvedDepthBuffer, m_RenderGraph.ImportTexture(target.targetDepth), uiBuffer, afterPostProcessBuffer, viewIndex, outputsToHDR: false, cubemapFace: target.face);
426+
BlitFinalCameraTexture(m_RenderGraph, hdCamera, prepassOutput.resolvedDepthBuffer, depthBackBuffer, uiBuffer, afterPostProcessBuffer, viewIndex, outputsToHDR: false, cubemapFace: target.face);
425427
}
426428
}
427429

428430
SendColorGraphicsBuffer(m_RenderGraph, hdCamera);
429431

430-
SetFinalTarget(m_RenderGraph, hdCamera, prepassOutput.resolvedDepthBuffer, backBuffer, target.face);
432+
SetFinalTarget(m_RenderGraph, hdCamera, prepassOutput.resolvedDepthBuffer, colorBackBuffer, target.face);
431433

432-
RenderWireOverlay(m_RenderGraph, hdCamera, backBuffer);
434+
RenderWireOverlay(m_RenderGraph, hdCamera, colorBackBuffer);
433435

434436
RenderGizmos(m_RenderGraph, hdCamera, GizmoSubset.PostImageEffects);
435437

436438
// Stop XR single pass before rendering screenspace UI
437439
StopXRSinglePass(m_RenderGraph, hdCamera);
438440

439441
if (renderRequest.isLast)
440-
RenderScreenSpaceOverlayUI(m_RenderGraph, hdCamera, backBuffer);
442+
RenderScreenSpaceOverlayUI(m_RenderGraph, hdCamera, colorBackBuffer);
441443
}
442444
}
443445

@@ -2098,6 +2100,51 @@ void RenderDistortion(RenderGraph renderGraph,
20982100
}
20992101
}
21002102

2103+
TextureHandle ImportColorBackBuffer(RenderGraph renderGraph, RenderTargetIdentifier colorBackBufferId)
2104+
{
2105+
if (m_CurrentColorBackBuffer == null)
2106+
{
2107+
m_CurrentColorBackBuffer = RTHandles.Alloc(colorBackBufferId, "Backbuffer color");
2108+
}
2109+
else
2110+
{
2111+
RTHandleStaticHelpers.SetRTHandleUserManagedWrapper(ref m_CurrentColorBackBuffer, colorBackBufferId);
2112+
}
2113+
2114+
// Importing the backbuffer, using dummy information as we used to do with ImportBackbuffer API
2115+
// TODO: Pass the right information using engine APIs and camera settings like in URP
2116+
RenderTargetInfo dummyImportInfo = new RenderTargetInfo();
2117+
dummyImportInfo.width = dummyImportInfo.height = dummyImportInfo.volumeDepth = dummyImportInfo.msaaSamples = 1;
2118+
dummyImportInfo.format = GraphicsFormat.R8G8B8A8_SRGB;
2119+
2120+
return m_RenderGraph.ImportTexture(m_CurrentColorBackBuffer, dummyImportInfo);
2121+
}
2122+
2123+
TextureHandle ImportDepthBackBuffer(RenderGraph renderGraph, RenderTargetIdentifier depthBackBufferId)
2124+
{
2125+
if (m_CurrentDepthBackBuffer == null)
2126+
{
2127+
m_CurrentDepthBackBuffer = RTHandles.Alloc(depthBackBufferId, "Backbuffer depth");
2128+
}
2129+
else
2130+
{
2131+
RTHandleStaticHelpers.SetRTHandleUserManagedWrapper(ref m_CurrentDepthBackBuffer, depthBackBufferId);
2132+
}
2133+
2134+
// We might have requests without depth buffer
2135+
bool noDepthBackBuffer = depthBackBufferId == default(RenderTargetIdentifier);
2136+
if (noDepthBackBuffer)
2137+
return TextureHandle.nullHandle;
2138+
2139+
// Importing the backbuffer, using dummy information as we used to do with ImportBackbuffer API
2140+
// TODO: Pass the right information using engine APIs and camera settings like in URP
2141+
RenderTargetInfo dummyImportInfo = new RenderTargetInfo();
2142+
dummyImportInfo.width = dummyImportInfo.height = dummyImportInfo.volumeDepth = dummyImportInfo.msaaSamples = 1;
2143+
dummyImportInfo.format = CoreUtils.GetDefaultDepthStencilFormat();
2144+
2145+
return m_RenderGraph.ImportTexture(m_CurrentDepthBackBuffer, dummyImportInfo);
2146+
}
2147+
21012148
TextureHandle CreateColorBuffer(RenderGraph renderGraph, HDCamera hdCamera, bool msaa, bool fallbackToBlack = false)
21022149
{
21032150
#if UNITY_2020_2_OR_NEWER

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ internal static HDRenderPipeline currentPipeline
155155
Lazy<RTHandle> m_CustomPassColorBuffer;
156156
Lazy<RTHandle> m_CustomPassDepthBuffer;
157157

158+
RTHandle m_CurrentColorBackBuffer;
159+
RTHandle m_CurrentDepthBackBuffer;
160+
158161
// Constant Buffers
159162
ShaderVariablesGlobal m_ShaderVariablesGlobalCB = new ShaderVariablesGlobal();
160163
ShaderVariablesXR m_ShaderVariablesXRCB = new ShaderVariablesXR();
@@ -1013,6 +1016,9 @@ protected override void Dispose(bool disposing)
10131016
if (m_CustomPassDepthBuffer.IsValueCreated)
10141017
RTHandles.Release(m_CustomPassDepthBuffer.Value);
10151018

1019+
RTHandles.Release(m_CurrentColorBackBuffer);
1020+
RTHandles.Release(m_CurrentDepthBackBuffer);
1021+
10161022
CullingGroupManager.instance.Cleanup();
10171023

10181024
CoreUtils.SafeRelease(m_DepthPyramidMipLevelOffsetsBuffer);
@@ -1411,9 +1417,10 @@ struct RenderRequest
14111417
{
14121418
public struct Target
14131419
{
1414-
public RenderTargetIdentifier id;
1420+
public RenderTargetIdentifier colorId;
1421+
public RenderTargetIdentifier depthId;
14151422
public CubemapFace face;
1416-
public RTHandle targetDepth;
1423+
public bool isProbe;
14171424
}
14181425
public HDCamera hdCamera;
14191426
public bool clearCameraSettings;
@@ -1587,15 +1594,19 @@ bool PrepareAndCullCamera(
15871594
}
15881595

15891596
// Select render target
1590-
RenderTargetIdentifier targetId = camera.targetTexture ?? new RenderTargetIdentifier(BuiltinRenderTextureType.CameraTarget);
1597+
RenderTargetIdentifier targetColorId = camera.targetTexture ?? new RenderTargetIdentifier(BuiltinRenderTextureType.CameraTarget);
1598+
RenderTargetIdentifier targetDepthId = camera.targetTexture ?? new RenderTargetIdentifier(BuiltinRenderTextureType.Depth);
15911599
if (camera.targetTexture != null)
15921600
{
15931601
camera.targetTexture.IncrementUpdateCount(); // Necessary if the texture is used as a cookie.
15941602
}
15951603

15961604
// Render directly to XR render target if active
15971605
if (hdCamera.xr.enabled)
1598-
targetId = hdCamera.xr.renderTarget;
1606+
{
1607+
targetColorId = hdCamera.xr.renderTarget;
1608+
targetDepthId = hdCamera.xr.renderTarget;
1609+
}
15991610

16001611
hdCamera.RequestDynamicResolution(cameraRequestedDynamicRes, DynamicResolutionHandler.instance);
16011612

@@ -1606,7 +1617,8 @@ bool PrepareAndCullCamera(
16061617
cullingResults = cullingResults,
16071618
target = new RenderRequest.Target
16081619
{
1609-
id = targetId,
1620+
colorId = targetColorId,
1621+
depthId = targetDepthId,
16101622
face = cubemapFace
16111623
},
16121624
dependsOnRenderRequestIndices = ListPool<int>.Get(),
@@ -1938,17 +1950,19 @@ ScriptableRenderContext renderContext
19381950
{
19391951
request.target = new RenderRequest.Target
19401952
{
1941-
id = visibleProbe.realtimeTextureRTH,
1942-
face = face
1953+
colorId = visibleProbe.realtimeTextureRTH,
1954+
face = face,
1955+
isProbe = true
19431956
};
19441957
}
19451958
else
19461959
{
19471960
request.target = new RenderRequest.Target
19481961
{
1949-
id = visibleProbe.realtimeTextureRTH,
1950-
targetDepth = visibleProbe.realtimeDepthTextureRTH,
1951-
face = CubemapFace.Unknown
1962+
colorId = visibleProbe.realtimeTextureRTH,
1963+
depthId = visibleProbe.realtimeDepthTextureRTH,
1964+
face = CubemapFace.Unknown,
1965+
isProbe = true
19521966
};
19531967
}
19541968

@@ -2821,7 +2835,7 @@ AOVRequestData aovRequest
28212835

28222836
if (GL.wireframe)
28232837
{
2824-
RenderWireFrame(cullingResults, hdCamera, target.id, renderContext, cmd);
2838+
RenderWireFrame(cullingResults, hdCamera, target.colorId, renderContext, cmd);
28252839
return;
28262840
}
28272841

0 commit comments

Comments
 (0)