Skip to content

Made conversion from render graph handles to actual resource implicit. #1034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 8, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public enum DepthAccess
}

/// <summary>
/// This struct specifies the context given to every render pass.
/// This class specifies the context given to every render pass.
/// </summary>
public class RenderGraphContext
{
Expand All @@ -30,8 +30,6 @@ public class RenderGraphContext
public CommandBuffer cmd;
///<summary>Render Graph pooll used for temporary data.</summary>
public RenderGraphObjectPool renderGraphPool;
///<summary>Render Graph Resource Registry used for accessing resources.</summary>
public RenderGraphResourceRegistry resources;
///<summary>Render Graph default resources.</summary>
public RenderGraphDefaultResources defaultResources;
}
Expand Down Expand Up @@ -403,7 +401,7 @@ public void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, in
{
m_Logger.Initialize();

m_Resources.BeginRender(parameters.renderingWidth, parameters.renderingHeight, parameters.msaaSamples, parameters.currentFrameIndex);
m_Resources.BeginRender(parameters.currentFrameIndex);

LogFrameInformation(parameters.renderingWidth, parameters.renderingHeight);

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

m_DebugParameters.logFrameInformation = false;
m_DebugParameters.logResources = false;

m_Resources.EndRender();
}
}
#endregion
Expand Down Expand Up @@ -814,7 +814,6 @@ void ExecuteRenderGraph(ScriptableRenderContext renderContext, CommandBuffer cmd
m_RenderGraphContext.cmd = cmd;
m_RenderGraphContext.renderContext = renderContext;
m_RenderGraphContext.renderGraphPool = m_RenderGraphPool;
m_RenderGraphContext.resources = m_Resources;
m_RenderGraphContext.defaultResources = m_DefaultResources;

for (int passIndex = 0; passIndex < m_CompiledPassInfos.size; ++passIndex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,30 @@

namespace UnityEngine.Experimental.Rendering.RenderGraphModule
{
/// <summary>
/// The RenderGraphResourceRegistry holds all resource allocated during Render Graph execution.
/// </summary>
public class RenderGraphResourceRegistry
class RenderGraphResourceRegistry
{
static readonly ShaderTagId s_EmptyName = new ShaderTagId("");

static RenderGraphResourceRegistry m_CurrentRegistry;
internal static RenderGraphResourceRegistry current
{
get
{
// We assume that it's enough to only check in editor because we don't want to pay the cost at runtime.
#if UNITY_EDITOR
if (m_CurrentRegistry == null)
{
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.");
}
#endif
return m_CurrentRegistry;
}
set
{
m_CurrentRegistry = value;
}
}

class IRenderGraphResource
{
public bool imported;
Expand Down Expand Up @@ -100,46 +117,29 @@ internal RendererListResource(in RendererListDesc desc)

RTHandle m_CurrentBackbuffer;

#region Public Interface
/// <summary>
/// Returns the RTHandle associated with the provided resource handle.
/// </summary>
/// <param name="handle">Handle to a texture resource.</param>
/// <returns>The RTHandle associated with the provided resource handle or null if the handle is invalid.</returns>
public RTHandle GetTexture(in TextureHandle handle)
internal RTHandle GetTexture(in TextureHandle handle)
{
if (!handle.IsValid())
return null;

return GetTextureResource(handle.handle).resource;
}

/// <summary>
/// Returns the RendererList associated with the provided resource handle.
/// </summary>
/// <param name="handle">Handle to a Renderer List resource.</param>
/// <returns>The Renderer List associated with the provided resource handle or an invalid renderer list if the handle is invalid.</returns>
public RendererList GetRendererList(in RendererListHandle handle)
internal RendererList GetRendererList(in RendererListHandle handle)
{
if (!handle.IsValid() || handle >= m_RendererListResources.size)
return RendererList.nullRendererList;

return m_RendererListResources[handle].rendererList;
}

/// <summary>
/// Returns the Compute Buffer associated with the provided resource handle.
/// </summary>
/// <param name="handle">Handle to a Compute Buffer resource.</param>
/// <returns>The Compute Buffer associated with the provided resource handle or a null reference if the handle is invalid.</returns>
public ComputeBuffer GetComputeBuffer(in ComputeBufferHandle handle)
internal ComputeBuffer GetComputeBuffer(in ComputeBufferHandle handle)
{
if (!handle.IsValid())
return null;

return GetComputeBufferResource(handle.handle).resource;
}
#endregion

#region Internal Interface
private RenderGraphResourceRegistry()
Expand Down Expand Up @@ -172,9 +172,15 @@ ResType GetResource<DescType, ResType>(DynamicArray<IRenderGraphResource> resour
return res.resource;
}

internal void BeginRender(int width, int height, MSAASamples msaaSamples, int currentFrameIndex)
internal void BeginRender(int currentFrameIndex)
{
m_CurrentFrameIndex = currentFrameIndex;
current = this;
}

internal void EndRender()
{
current = null;
}

void CheckHandleValidity(in ResourceHandle res)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal enum RenderGraphResourceType
// Can't have a default constructor with handle = -1 hence the ugly IsValid implementation (where m_IsValid will be false by default).
internal struct ResourceHandle
{
bool m_IsValid;
bool m_IsValid;

public int index { get; private set; }
public RenderGraphResourceType type { get; private set; }
Expand Down Expand Up @@ -53,6 +53,22 @@ public struct TextureHandle

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

/// <summary>
/// Cast to RTHandle
/// </summary>
/// <param name="texture">Input TextureHandle.</param>
public static implicit operator RTHandle(TextureHandle texture) => texture.IsValid() ? RenderGraphResourceRegistry.current.GetTexture(texture) : null;
/// <summary>
/// Cast to RenderTargetIdentifier
/// </summary>
/// <param name="texture">Input TextureHandle.</param>
public static implicit operator RenderTargetIdentifier(TextureHandle texture) => texture.IsValid() ? RenderGraphResourceRegistry.current.GetTexture(texture) : null;
/// <summary>
/// Cast to RenderTexture
/// </summary>
/// <param name="texture">Input TextureHandle.</param>
public static implicit operator RenderTexture(TextureHandle texture) => texture.IsValid() ? RenderGraphResourceRegistry.current.GetTexture(texture) : null;

/// <summary>
/// Return true if the handle is valid.
/// </summary>
Expand All @@ -70,6 +86,12 @@ public struct ComputeBufferHandle

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

/// <summary>
/// Cast to ComputeBuffer
/// </summary>
/// <param name="buffer">Input ComputeBufferHandle</param>
public static implicit operator ComputeBuffer(ComputeBufferHandle buffer) => buffer.IsValid() ? RenderGraphResourceRegistry.current.GetComputeBuffer(buffer) : null;

/// <summary>
/// Return true if the handle is valid.
/// </summary>
Expand All @@ -93,6 +115,8 @@ public struct RendererListHandle
/// <returns>The integer representation of the handle.</returns>
public static implicit operator int(RendererListHandle handle) { return handle.handle; }

public static implicit operator RendererList(RendererListHandle rendererList) => rendererList.IsValid() ? RenderGraphResourceRegistry.current.GetRendererList(rendererList) : RendererList.nullRendererList;

/// <summary>
/// Return true if the handle is valid.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ TextureHandle RenderAO(RenderGraph renderGraph, in RenderAOParameters parameters
builder.SetRenderFunc(
(RenderAOPassData data, RenderGraphContext ctx) =>
{
RenderAO(data.parameters, ctx.resources.GetTexture(data.packedData), ctx.resources.GetTexture(data.depthPyramid), ctx.resources.GetTexture(data.normalBuffer), ctx.cmd);
RenderAO(data.parameters, data.packedData, data.depthPyramid, data.normalBuffer, ctx.cmd);
});

return passData.packedData;
Expand Down Expand Up @@ -124,13 +124,12 @@ TextureHandle DenoiseAO( RenderGraph renderGraph,
builder.SetRenderFunc(
(DenoiseAOPassData data, RenderGraphContext ctx) =>
{
var res = ctx.resources;
DenoiseAO( data.parameters,
res.GetTexture(data.packedData),
res.GetTexture(data.packedDataBlurred),
res.GetTexture(data.currentHistory),
res.GetTexture(data.outputHistory),
res.GetTexture(data.denoiseOutput),
data.packedData,
data.packedDataBlurred,
data.currentHistory,
data.outputHistory,
data.denoiseOutput,
ctx.cmd);
});

Expand Down Expand Up @@ -161,7 +160,7 @@ TextureHandle UpsampleAO(RenderGraph renderGraph, in RenderAOParameters paramete
builder.SetRenderFunc(
(UpsampleAOPassData data, RenderGraphContext ctx) =>
{
UpsampleAO(data.parameters, ctx.resources.GetTexture(data.input), ctx.resources.GetTexture(data.output), ctx.cmd);
UpsampleAO(data.parameters, data.input, data.output, ctx.cmd);
});

return passData.output;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ class BindShadowGlobalResourcesPassData
static void BindAtlasTexture(RenderGraphContext ctx, TextureHandle texture, int shaderId)
{
if (texture.IsValid())
ctx.cmd.SetGlobalTexture(shaderId, ctx.resources.GetTexture(texture));
ctx.cmd.SetGlobalTexture(shaderId, texture);
else
ctx.cmd.SetGlobalTexture(shaderId, ctx.resources.GetTexture(ctx.defaultResources.blackTexture));
ctx.cmd.SetGlobalTexture(shaderId, ctx.defaultResources.blackTexture);
}

void BindShadowGlobalResources(RenderGraph renderGraph, in ShadowResult shadowResult)
Expand Down Expand Up @@ -148,26 +148,22 @@ internal TextureHandle RenderShadows(RenderGraph renderGraph, CullingResults cul
builder.SetRenderFunc(
(RenderShadowsPassData data, RenderGraphContext context) =>
{
RTHandle atlasTexture = context.resources.GetTexture(data.atlasTexture);
RenderShadows( data.parameters,
atlasTexture,
data.atlasTexture,
data.shadowDrawSettings,
context.renderContext, context.cmd);

if (data.parameters.blurAlgorithm == BlurAlgorithm.EVSM)
{
RTHandle[] momentTextures = context.renderGraphPool.GetTempArray<RTHandle>(2);
momentTextures[0] = context.resources.GetTexture(data.momentAtlasTexture1);
momentTextures[1] = context.resources.GetTexture(data.momentAtlasTexture2);
momentTextures[0] = data.momentAtlasTexture1;
momentTextures[1] = data.momentAtlasTexture2;

EVSMBlurMoments(data.parameters, atlasTexture, momentTextures, context.cmd);
EVSMBlurMoments(data.parameters, data.atlasTexture, momentTextures, context.cmd);
}
else if (data.parameters.blurAlgorithm == BlurAlgorithm.IM)
{
RTHandle momentAtlas = context.resources.GetTexture(data.momentAtlasTexture1);
RTHandle intermediateSummedArea = context.resources.GetTexture(data.intermediateSummedAreaTexture);
RTHandle summedArea = context.resources.GetTexture(data.summedAreaTexture);
IMBlurMoment(data.parameters, atlasTexture, momentAtlas, intermediateSummedArea, summedArea, context.cmd);
IMBlurMoment(data.parameters, data.atlasTexture, data.momentAtlasTexture1, data.intermediateSummedAreaTexture, data.summedAreaTexture, context.cmd);
}
});

Expand Down
Loading