Skip to content

Commit 9df292f

Browse files
Made conversion from render graph handles to actual resource implicit. (#1034)
* Made conversion from render graph handles to actual resource implicit to reduce boiler plate code. * Post merge fixes * Added an exception when accessing resources while not executing render graph. * Post merge fixes * Fixed error handling of casting handles to resources.
1 parent fa607fd commit 9df292f

File tree

16 files changed

+287
-343
lines changed

16 files changed

+287
-343
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public enum DepthAccess
2020
}
2121

2222
/// <summary>
23-
/// This struct specifies the context given to every render pass.
23+
/// This class specifies the context given to every render pass.
2424
/// </summary>
2525
public class RenderGraphContext
2626
{
@@ -30,8 +30,6 @@ public class RenderGraphContext
3030
public CommandBuffer cmd;
3131
///<summary>Render Graph pooll used for temporary data.</summary>
3232
public RenderGraphObjectPool renderGraphPool;
33-
///<summary>Render Graph Resource Registry used for accessing resources.</summary>
34-
public RenderGraphResourceRegistry resources;
3533
///<summary>Render Graph default resources.</summary>
3634
public RenderGraphDefaultResources defaultResources;
3735
}
@@ -403,7 +401,7 @@ public void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, in
403401
{
404402
m_Logger.Initialize();
405403

406-
m_Resources.BeginRender(parameters.renderingWidth, parameters.renderingHeight, parameters.msaaSamples, parameters.currentFrameIndex);
404+
m_Resources.BeginRender(parameters.currentFrameIndex);
407405

408406
LogFrameInformation(parameters.renderingWidth, parameters.renderingHeight);
409407

@@ -426,6 +424,8 @@ public void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, in
426424

427425
m_DebugParameters.logFrameInformation = false;
428426
m_DebugParameters.logResources = false;
427+
428+
m_Resources.EndRender();
429429
}
430430
}
431431
#endregion
@@ -814,7 +814,6 @@ void ExecuteRenderGraph(ScriptableRenderContext renderContext, CommandBuffer cmd
814814
m_RenderGraphContext.cmd = cmd;
815815
m_RenderGraphContext.renderContext = renderContext;
816816
m_RenderGraphContext.renderGraphPool = m_RenderGraphPool;
817-
m_RenderGraphContext.resources = m_Resources;
818817
m_RenderGraphContext.defaultResources = m_DefaultResources;
819818

820819
for (int passIndex = 0; passIndex < m_CompiledPassInfos.size; ++passIndex)

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

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,30 @@
55

66
namespace UnityEngine.Experimental.Rendering.RenderGraphModule
77
{
8-
/// <summary>
9-
/// The RenderGraphResourceRegistry holds all resource allocated during Render Graph execution.
10-
/// </summary>
11-
public class RenderGraphResourceRegistry
8+
class RenderGraphResourceRegistry
129
{
1310
static readonly ShaderTagId s_EmptyName = new ShaderTagId("");
1411

12+
static RenderGraphResourceRegistry m_CurrentRegistry;
13+
internal static RenderGraphResourceRegistry current
14+
{
15+
get
16+
{
17+
// We assume that it's enough to only check in editor because we don't want to pay the cost at runtime.
18+
#if UNITY_EDITOR
19+
if (m_CurrentRegistry == null)
20+
{
21+
throw new InvalidOperationException("Current Render Graph Resource Registry is not set. You are probably trying to cast a Render Graph handle to a resource outside of a Render Graph Pass.");
22+
}
23+
#endif
24+
return m_CurrentRegistry;
25+
}
26+
set
27+
{
28+
m_CurrentRegistry = value;
29+
}
30+
}
31+
1532
class IRenderGraphResource
1633
{
1734
public bool imported;
@@ -100,46 +117,29 @@ internal RendererListResource(in RendererListDesc desc)
100117

101118
RTHandle m_CurrentBackbuffer;
102119

103-
#region Public Interface
104-
/// <summary>
105-
/// Returns the RTHandle associated with the provided resource handle.
106-
/// </summary>
107-
/// <param name="handle">Handle to a texture resource.</param>
108-
/// <returns>The RTHandle associated with the provided resource handle or null if the handle is invalid.</returns>
109-
public RTHandle GetTexture(in TextureHandle handle)
120+
internal RTHandle GetTexture(in TextureHandle handle)
110121
{
111122
if (!handle.IsValid())
112123
return null;
113124

114125
return GetTextureResource(handle.handle).resource;
115126
}
116127

117-
/// <summary>
118-
/// Returns the RendererList associated with the provided resource handle.
119-
/// </summary>
120-
/// <param name="handle">Handle to a Renderer List resource.</param>
121-
/// <returns>The Renderer List associated with the provided resource handle or an invalid renderer list if the handle is invalid.</returns>
122-
public RendererList GetRendererList(in RendererListHandle handle)
128+
internal RendererList GetRendererList(in RendererListHandle handle)
123129
{
124130
if (!handle.IsValid() || handle >= m_RendererListResources.size)
125131
return RendererList.nullRendererList;
126132

127133
return m_RendererListResources[handle].rendererList;
128134
}
129135

130-
/// <summary>
131-
/// Returns the Compute Buffer associated with the provided resource handle.
132-
/// </summary>
133-
/// <param name="handle">Handle to a Compute Buffer resource.</param>
134-
/// <returns>The Compute Buffer associated with the provided resource handle or a null reference if the handle is invalid.</returns>
135-
public ComputeBuffer GetComputeBuffer(in ComputeBufferHandle handle)
136+
internal ComputeBuffer GetComputeBuffer(in ComputeBufferHandle handle)
136137
{
137138
if (!handle.IsValid())
138139
return null;
139140

140141
return GetComputeBufferResource(handle.handle).resource;
141142
}
142-
#endregion
143143

144144
#region Internal Interface
145145
private RenderGraphResourceRegistry()
@@ -172,9 +172,15 @@ ResType GetResource<DescType, ResType>(DynamicArray<IRenderGraphResource> resour
172172
return res.resource;
173173
}
174174

175-
internal void BeginRender(int width, int height, MSAASamples msaaSamples, int currentFrameIndex)
175+
internal void BeginRender(int currentFrameIndex)
176176
{
177177
m_CurrentFrameIndex = currentFrameIndex;
178+
current = this;
179+
}
180+
181+
internal void EndRender()
182+
{
183+
current = null;
178184
}
179185

180186
void CheckHandleValidity(in ResourceHandle res)

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal enum RenderGraphResourceType
1414
// Can't have a default constructor with handle = -1 hence the ugly IsValid implementation (where m_IsValid will be false by default).
1515
internal struct ResourceHandle
1616
{
17-
bool m_IsValid;
17+
bool m_IsValid;
1818

1919
public int index { get; private set; }
2020
public RenderGraphResourceType type { get; private set; }
@@ -53,6 +53,22 @@ public struct TextureHandle
5353

5454
internal TextureHandle(int handle) { this.handle = new ResourceHandle(handle, RenderGraphResourceType.Texture); }
5555

56+
/// <summary>
57+
/// Cast to RTHandle
58+
/// </summary>
59+
/// <param name="texture">Input TextureHandle.</param>
60+
public static implicit operator RTHandle(TextureHandle texture) => texture.IsValid() ? RenderGraphResourceRegistry.current.GetTexture(texture) : null;
61+
/// <summary>
62+
/// Cast to RenderTargetIdentifier
63+
/// </summary>
64+
/// <param name="texture">Input TextureHandle.</param>
65+
public static implicit operator RenderTargetIdentifier(TextureHandle texture) => texture.IsValid() ? RenderGraphResourceRegistry.current.GetTexture(texture) : null;
66+
/// <summary>
67+
/// Cast to RenderTexture
68+
/// </summary>
69+
/// <param name="texture">Input TextureHandle.</param>
70+
public static implicit operator RenderTexture(TextureHandle texture) => texture.IsValid() ? RenderGraphResourceRegistry.current.GetTexture(texture) : null;
71+
5672
/// <summary>
5773
/// Return true if the handle is valid.
5874
/// </summary>
@@ -70,6 +86,12 @@ public struct ComputeBufferHandle
7086

7187
internal ComputeBufferHandle(int handle) { this.handle = new ResourceHandle(handle, RenderGraphResourceType.ComputeBuffer); }
7288

89+
/// <summary>
90+
/// Cast to ComputeBuffer
91+
/// </summary>
92+
/// <param name="buffer">Input ComputeBufferHandle</param>
93+
public static implicit operator ComputeBuffer(ComputeBufferHandle buffer) => buffer.IsValid() ? RenderGraphResourceRegistry.current.GetComputeBuffer(buffer) : null;
94+
7395
/// <summary>
7496
/// Return true if the handle is valid.
7597
/// </summary>
@@ -93,6 +115,8 @@ public struct RendererListHandle
93115
/// <returns>The integer representation of the handle.</returns>
94116
public static implicit operator int(RendererListHandle handle) { return handle.handle; }
95117

118+
public static implicit operator RendererList(RendererListHandle rendererList) => rendererList.IsValid() ? RenderGraphResourceRegistry.current.GetRendererList(rendererList) : RendererList.nullRendererList;
119+
96120
/// <summary>
97121
/// Return true if the handle is valid.
98122
/// </summary>

com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/AmbientOcclusion.RenderGraph.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ TextureHandle RenderAO(RenderGraph renderGraph, in RenderAOParameters parameters
6868
builder.SetRenderFunc(
6969
(RenderAOPassData data, RenderGraphContext ctx) =>
7070
{
71-
RenderAO(data.parameters, ctx.resources.GetTexture(data.packedData), ctx.resources.GetTexture(data.depthPyramid), ctx.resources.GetTexture(data.normalBuffer), ctx.cmd);
71+
RenderAO(data.parameters, data.packedData, data.depthPyramid, data.normalBuffer, ctx.cmd);
7272
});
7373

7474
return passData.packedData;
@@ -124,13 +124,12 @@ TextureHandle DenoiseAO( RenderGraph renderGraph,
124124
builder.SetRenderFunc(
125125
(DenoiseAOPassData data, RenderGraphContext ctx) =>
126126
{
127-
var res = ctx.resources;
128127
DenoiseAO( data.parameters,
129-
res.GetTexture(data.packedData),
130-
res.GetTexture(data.packedDataBlurred),
131-
res.GetTexture(data.currentHistory),
132-
res.GetTexture(data.outputHistory),
133-
res.GetTexture(data.denoiseOutput),
128+
data.packedData,
129+
data.packedDataBlurred,
130+
data.currentHistory,
131+
data.outputHistory,
132+
data.denoiseOutput,
134133
ctx.cmd);
135134
});
136135

@@ -161,7 +160,7 @@ TextureHandle UpsampleAO(RenderGraph renderGraph, in RenderAOParameters paramete
161160
builder.SetRenderFunc(
162161
(UpsampleAOPassData data, RenderGraphContext ctx) =>
163162
{
164-
UpsampleAO(data.parameters, ctx.resources.GetTexture(data.input), ctx.resources.GetTexture(data.output), ctx.cmd);
163+
UpsampleAO(data.parameters, data.input, data.output, ctx.cmd);
165164
});
166165

167166
return passData.output;

com.unity.render-pipelines.high-definition/Runtime/Lighting/Shadow/HDShadowManager.RenderGraph.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ class BindShadowGlobalResourcesPassData
6262
static void BindAtlasTexture(RenderGraphContext ctx, TextureHandle texture, int shaderId)
6363
{
6464
if (texture.IsValid())
65-
ctx.cmd.SetGlobalTexture(shaderId, ctx.resources.GetTexture(texture));
65+
ctx.cmd.SetGlobalTexture(shaderId, texture);
6666
else
67-
ctx.cmd.SetGlobalTexture(shaderId, ctx.resources.GetTexture(ctx.defaultResources.blackTexture));
67+
ctx.cmd.SetGlobalTexture(shaderId, ctx.defaultResources.blackTexture);
6868
}
6969

7070
void BindShadowGlobalResources(RenderGraph renderGraph, in ShadowResult shadowResult)
@@ -148,26 +148,22 @@ internal TextureHandle RenderShadows(RenderGraph renderGraph, CullingResults cul
148148
builder.SetRenderFunc(
149149
(RenderShadowsPassData data, RenderGraphContext context) =>
150150
{
151-
RTHandle atlasTexture = context.resources.GetTexture(data.atlasTexture);
152151
RenderShadows( data.parameters,
153-
atlasTexture,
152+
data.atlasTexture,
154153
data.shadowDrawSettings,
155154
context.renderContext, context.cmd);
156155

157156
if (data.parameters.blurAlgorithm == BlurAlgorithm.EVSM)
158157
{
159158
RTHandle[] momentTextures = context.renderGraphPool.GetTempArray<RTHandle>(2);
160-
momentTextures[0] = context.resources.GetTexture(data.momentAtlasTexture1);
161-
momentTextures[1] = context.resources.GetTexture(data.momentAtlasTexture2);
159+
momentTextures[0] = data.momentAtlasTexture1;
160+
momentTextures[1] = data.momentAtlasTexture2;
162161

163-
EVSMBlurMoments(data.parameters, atlasTexture, momentTextures, context.cmd);
162+
EVSMBlurMoments(data.parameters, data.atlasTexture, momentTextures, context.cmd);
164163
}
165164
else if (data.parameters.blurAlgorithm == BlurAlgorithm.IM)
166165
{
167-
RTHandle momentAtlas = context.resources.GetTexture(data.momentAtlasTexture1);
168-
RTHandle intermediateSummedArea = context.resources.GetTexture(data.intermediateSummedAreaTexture);
169-
RTHandle summedArea = context.resources.GetTexture(data.summedAreaTexture);
170-
IMBlurMoment(data.parameters, atlasTexture, momentAtlas, intermediateSummedArea, summedArea, context.cmd);
166+
IMBlurMoment(data.parameters, data.atlasTexture, data.momentAtlasTexture1, data.intermediateSummedAreaTexture, data.summedAreaTexture, context.cmd);
171167
}
172168
});
173169

0 commit comments

Comments
 (0)