diff --git a/Packages/com.unity.render-pipelines.universal/Runtime/Decal/ScreenSpace/DecalGBufferRenderPass.cs b/Packages/com.unity.render-pipelines.universal/Runtime/Decal/ScreenSpace/DecalGBufferRenderPass.cs index 1b39d9d2fb5..19f09ee1861 100644 --- a/Packages/com.unity.render-pipelines.universal/Runtime/Decal/ScreenSpace/DecalGBufferRenderPass.cs +++ b/Packages/com.unity.render-pipelines.universal/Runtime/Decal/ScreenSpace/DecalGBufferRenderPass.cs @@ -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; } @@ -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); diff --git a/Packages/com.unity.render-pipelines.universal/Runtime/DeferredLights.cs b/Packages/com.unity.render-pipelines.universal/Runtime/DeferredLights.cs index 34543812269..ce82518bad1 100644 --- a/Packages/com.unity.render-pipelines.universal/Runtime/DeferredLights.cs +++ b/Packages/com.unity.render-pipelines.universal/Runtime/DeferredLights.cs @@ -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) { @@ -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]; } } @@ -503,8 +515,8 @@ 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]; @@ -512,6 +524,8 @@ public void Setup( 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]; diff --git a/Packages/com.unity.render-pipelines.universal/Runtime/UniversalRenderer.cs b/Packages/com.unity.render-pipelines.universal/Runtime/UniversalRenderer.cs index 249f38e9774..cb7d15e080e 100644 --- a/Packages/com.unity.render-pipelines.universal/Runtime/UniversalRenderer.cs +++ b/Packages/com.unity.render-pipelines.universal/Runtime/UniversalRenderer.cs @@ -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 k_DepthNormalsOnly = new List { new ShaderTagId("DepthNormalsOnly") }; private static class Profiling diff --git a/Packages/com.unity.render-pipelines.universal/Runtime/UniversalRendererRenderGraph.cs b/Packages/com.unity.render-pipelines.universal/Runtime/UniversalRendererRenderGraph.cs index 3045fcb00c1..75a74401e22 100644 --- a/Packages/com.unity.render-pipelines.universal/Runtime/UniversalRendererRenderGraph.cs +++ b/Packages/com.unity.render-pipelines.universal/Runtime/UniversalRendererRenderGraph.cs @@ -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;