Skip to content
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

Defined g-buffer index constants to make it easier for users to add their own g-buffers #8106

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public DecalGBufferRenderPass(DecalScreenSpaceSettings settings, DecalDrawGBuffe
m_ShaderTagIdList.Add(new ShaderTagId(DecalShaderPassNames.DecalGBufferMesh));

m_PassData = new PassData();
m_GbufferAttachments = new RTHandle[4];
m_GbufferAttachments = new RTHandle[UniversalRenderer.k_GbufferCountMandatory];

breakGBufferAndDeferredRenderPass = false;
}
Expand Down Expand Up @@ -181,9 +181,9 @@ public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer

if (renderGraph.nativeRenderPassesEnabled)
{
builder.SetInputAttachment(gBufferHandles[4], 0, AccessFlags.Read);
builder.SetInputAttachment(gBufferHandles[UniversalRenderer.k_GbufferCountMandatory], 0, AccessFlags.Read);
if (m_DecalLayers)
builder.SetInputAttachment(gBufferHandles[5], 1, AccessFlags.Read);
builder.SetInputAttachment(gBufferHandles[UniversalRenderer.k_GbufferCountMandatory + 1], 1, AccessFlags.Read);
}
else if (cameraDepthTexture.IsValid())
builder.UseTexture(cameraDepthTexture, AccessFlags.Read);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,26 @@ internal enum StencilDeferredPasses
private static readonly ProfilingSampler m_ProfilingDeferredPass = new ProfilingSampler(k_DeferredPass);
private static readonly ProfilingSampler m_ProfilingSetupLightConstants = new ProfilingSampler(k_SetupLightConstants);

// Mandatory g-buffers
internal int GBufferAlbedoIndex { get { return 0; } }
internal int GBufferSpecularMetallicIndex { get { return 1; } }
internal int GBufferNormalSmoothnessIndex { get { return 2; } }
internal int GBufferLightingIndex { get { return 3; } }

// User-defined g-buffers should go here, after the mandatory and before the lighting buffer & optional g-buffers

// Lighting g-buffer (should be the last mandatory g-buffer because it is not passed along as an attachment, so subsequent attachment indices shift)
internal int GBufferLightingIndex { get { return UniversalRenderer.k_GbufferCountMandatory - 1; } }

// Optional g-buffers
internal int GbufferDepthIndex { get { return UseFramebufferFetch ? GBufferLightingIndex + 1 : -1; } }
internal int GBufferRenderingLayers { get { return UseRenderingLayers ? GBufferLightingIndex + (UseFramebufferFetch ? 1 : 0) + 1 : -1; } }
// Shadow Mask can change at runtime. Because of this it needs to come after the non-changing buffers.
internal int GBufferShadowMask { get { return UseShadowMask ? GBufferLightingIndex + (UseFramebufferFetch ? 1 : 0) + (UseRenderingLayers ? 1 : 0) + 1 : -1; } }

// Color buffer count (not including dephStencil).
internal int GBufferSliceCount { get { return 4 + (UseFramebufferFetch ? 1 : 0) + (UseShadowMask ? 1 : 0) + (UseRenderingLayers ? 1 : 0); } }
internal int GBufferSliceCount { get { return UniversalRenderer.k_GbufferCountMandatory + (UseFramebufferFetch ? 1 : 0) + (UseShadowMask ? 1 : 0) + (UseRenderingLayers ? 1 : 0); } }

internal int GBufferInputAttachmentCount { get { return 4 + (UseShadowMask ? 1 : 0); } }
internal int GBufferInputAttachmentCount { get { return UniversalRenderer.k_GbufferCountMandatory + (UseShadowMask ? 1 : 0); } }

internal GraphicsFormat GetGBufferFormat(int index)
{
Expand Down Expand Up @@ -459,20 +467,24 @@ internal void UpdateDeferredInputAttachments()
this.DeferredInputAttachments[0] = this.GbufferAttachments[0];
this.DeferredInputAttachments[1] = this.GbufferAttachments[1];
this.DeferredInputAttachments[2] = this.GbufferAttachments[2];
this.DeferredInputAttachments[3] = this.GbufferAttachments[4];

// NOTE: Lighting buffer (GbufferAttachments[3]) is skipped. Subsequent attachment indices will be off by 1 compared to their g-buffer indices

// TODO: Can this be this.GbufferAttachments[GbufferDepthIndex] instead of this.GbufferAttachments[4] ?
this.DeferredInputAttachments[UniversalRenderer.k_GbufferCountMandatory - 1] = this.GbufferAttachments[4];

if (UseShadowMask && UseRenderingLayers)
{
this.DeferredInputAttachments[4] = this.GbufferAttachments[GBufferShadowMask];
this.DeferredInputAttachments[5] = this.GbufferAttachments[GBufferRenderingLayers];
this.DeferredInputAttachments[UniversalRenderer.k_GbufferCountMandatory] = this.GbufferAttachments[GBufferShadowMask];
this.DeferredInputAttachments[UniversalRenderer.k_GbufferCountMandatory + 1] = this.GbufferAttachments[GBufferRenderingLayers];
}
else if (UseShadowMask)
{
this.DeferredInputAttachments[4] = this.GbufferAttachments[GBufferShadowMask];
this.DeferredInputAttachments[UniversalRenderer.k_GbufferCountMandatory] = this.GbufferAttachments[GBufferShadowMask];
}
else if (UseRenderingLayers)
{
this.DeferredInputAttachments[4] = this.GbufferAttachments[GBufferRenderingLayers];
this.DeferredInputAttachments[UniversalRenderer.k_GbufferCountMandatory] = this.GbufferAttachments[GBufferRenderingLayers];
}
}

Expand Down Expand Up @@ -503,15 +515,17 @@ public void Setup(
this.GbufferAttachments[this.GBufferLightingIndex] = colorAttachment;
this.DepthAttachment = depthAttachment;

var inputCount = 4 + (UseShadowMask ? 1 : 0) + (UseRenderingLayers ? 1 : 0);
if (this.DeferredInputAttachments == null && this.UseFramebufferFetch && this.GbufferAttachments.Length >= 3 ||
var inputCount = UniversalRenderer.k_GbufferCountMandatory + (UseShadowMask ? 1 : 0) + (UseRenderingLayers ? 1 : 0);
if (this.DeferredInputAttachments == null && this.UseFramebufferFetch && this.GbufferAttachments.Length >= 3 + UniversalRenderer.k_GbufferCountUserDefined ||
(this.DeferredInputAttachments != null && inputCount != this.DeferredInputAttachments.Length))
{
this.DeferredInputAttachments = new RTHandle[inputCount];
this.DeferredInputIsTransient = new bool[inputCount];
int i, j = 0;
for (i = 0; i < inputCount; i++, j++)
{
// Do not include the lighting g-buffer as an attachment. Note that this means that all of the
// optional attachment indices are shifted by 1 compared to their g-buffer indices
if (j == GBufferLightingIndex)
j++;
DeferredInputAttachments[i] = GbufferAttachments[j];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public sealed partial class UniversalRenderer : ScriptableRenderer
const int k_FinalBlitPassQueueOffset = 1;
const int k_AfterFinalBlitPassQueueOffset = k_FinalBlitPassQueueOffset + 1;

// Constants to help make it easier for users to insert a g-buffer after the mandatory g-buffers and before the optional ones
public const int k_GbufferCountBuiltIn = 4;
public const int k_GbufferCountUserDefined = 0;
public const int k_GbufferCountMandatory = k_GbufferCountBuiltIn + k_GbufferCountUserDefined;
public const int k_GbufferCountOptional = 3;
public const int k_GbufferCountMax = k_GbufferCountMandatory + k_GbufferCountOptional;

static readonly List<ShaderTagId> k_DepthNormalsOnly = new List<ShaderTagId> { new ShaderTagId("DepthNormalsOnly") };

private static class Profiling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ static class RenderGraphUtils
{
static private ProfilingSampler s_SetGlobalTextureProfilingSampler = new ProfilingSampler("Set Global Texture");

internal const int GBufferSize = 7;
internal const int GBufferSize = UniversalRenderer.k_GbufferCountMax;
internal const int DBufferSize = 3;
internal const int LightTextureSize = 4;

Expand Down