Skip to content

[URP] DepthCopy texture as a color #5369

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 18 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
a2e4eb6
switching _CameraDepthTexture to R32Float and accommodating some stuf…
jonuuukas Jul 29, 2021
80af9be
some changes to the Foundation Project for Repro project (to be rever…
jonuuukas Jul 29, 2021
d1cef12
Revert "some changes to the Foundation Project for Repro project (to …
jonuuukas Jul 29, 2021
d39e96d
disable fbfetch on DbufferDeferred decals
jonuuukas Aug 3, 2021
c149ec9
adding IsTransientAttachment to ScriptableRenderPass/NativeRenderPass…
jonuuukas Aug 5, 2021
933f089
making depthOnly pass compatible to render color texture and changing…
jonuuukas Aug 12, 2021
624f0a1
Merge branch 'master' into universal/depth-copy-as-color
jonuuukas Aug 12, 2021
d887392
fix after updating trunk
jonuuukas Aug 13, 2021
6accb1c
merge fix - change descriptor for DepthOnly configureTarget to the co…
jonuuukas Aug 13, 2021
ee6e7d4
enabling depthbuffer for depth copy and adding additional override fo…
jonuuukas Aug 16, 2021
399ee75
formatting fixes
jonuuukas Aug 16, 2021
68d3938
terrain shader passes depthonly pass fix to output clippos
jonuuukas Aug 17, 2021
4f9cbd7
adding depth copy for renderpass as well and changing how fake depth …
jonuuukas Aug 17, 2021
165f4d5
some cleanup and refactoring and enabling Renderpass for SS forward d…
jonuuukas Aug 17, 2021
fd4df44
adding _OUTPUT_DEPTH keyword to CopyDepthPass to select correct outpu…
jonuuukas Aug 19, 2021
2510a18
Merge branch 'master' into universal/depth-copy-as-color
jonuuukas Aug 23, 2021
5dec840
adding depth output for VFXGraph depth only passes
jonuuukas Aug 24, 2021
c4ddd12
changing the semantic for outDepthColor in VFXPasses.template
jonuuukas Aug 25, 2021
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 @@ -13,7 +13,7 @@ Material:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: 2000
m_CustomRenderQueue: 1999
stringTagMap:
RenderType: Opaque
disabledShaderPasses: []
Expand Down Expand Up @@ -95,7 +95,7 @@ Material:
- _Metallic: 0
- _OcclusionStrength: 0.387
- _Parallax: 0.0477
- _QueueOffset: 0
- _QueueOffset: -1
- _ReceiveShadows: 1
- _Smoothness: 0.742
- _SmoothnessTextureChannel: 0
Expand Down
3 changes: 1 addition & 2 deletions com.unity.render-pipelines.core/ShaderLibrary/API/Metal.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@
#define GATHER_GREEN_TEXTURE2D(textureName, samplerName, coord2) textureName.GatherGreen(samplerName, coord2)
#define GATHER_BLUE_TEXTURE2D(textureName, samplerName, coord2) textureName.GatherBlue(samplerName, coord2)
#define GATHER_ALPHA_TEXTURE2D(textureName, samplerName, coord2) textureName.GatherAlpha(samplerName, coord2)

#if defined(SHADER_API_MOBILE)
#if defined(UNITY_FRAMEBUFFER_FETCH_AVAILABLE)
#define PLATFORM_SUPPORTS_NATIVE_RENDERPASS
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ half4 frag(PackedVaryings packedInput) : SV_TARGET
clip(surfaceDescription.Alpha - surfaceDescription.AlphaClipThreshold);
#endif

return 0;
return packedInput.positionCS.z;
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ public static RenderStateCollection DepthOnly(UniversalTarget target)
{ RenderState.ZTest(ZTest.LEqual) },
{ RenderState.ZWrite(ZWrite.On) },
{ UberSwitchedCullRenderState(target) },
{ RenderState.ColorMask("ColorMask 0") },
{ RenderState.ColorMask("ColorMask R") },
};

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ void frag(ps_input i
#if VFX_PASSDEPTH == VFX_PASSDEPTH_ACTUAL
#if defined(WRITE_NORMAL_BUFFER)
, out float4 outNormalBuffer : SV_Target0
, out float4 outDepthColor : SV_Target1
#else
, out float4 outDepthColor : SV_Target0
#endif
#elif VFX_PASSDEPTH == VFX_PASSDEPTH_SELECTION
, out float4 outColor : SV_Target0
Expand Down Expand Up @@ -130,7 +133,7 @@ void frag(ps_input i
// We use depth prepass for scene selection in the editor, this code allow to output the outline correctly
outColor = float4(_ObjectId, _PassValue, 1.0, 1.0);
#elif VFX_PASSDEPTH == VFX_PASSDEPTH_ACTUAL
//void
outDepthColor = float4(i.VFX_VARYING_POSCS.z, 0,0,0);
#elif VFX_PASSDEPTH == VFX_PASSDEPTH_SHADOW
//void
#else
Expand Down
10 changes: 8 additions & 2 deletions com.unity.render-pipelines.universal/Runtime/DeferredLights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ internal bool AccurateGbufferNormals
// Output lighting result.
internal RenderTargetHandle[] GbufferAttachments { get; set; }
internal RenderTargetIdentifier[] DeferredInputAttachments { get; set; }
internal bool[] DeferredInputIsTransient { get; set; }
// Input depth texture, also bound as read-only RT
internal RenderTargetHandle DepthAttachment { get; set; }
//
Expand Down Expand Up @@ -817,12 +818,17 @@ public void Setup(ref RenderingData renderingData,
this.GbufferAttachmentIdentifiers[i] = this.GbufferAttachments[i].Identifier();
this.GbufferFormats[i] = this.GetGBufferFormat(i);
}
if (this.DeferredInputAttachments == null && this.UseRenderPass && this.GbufferAttachments.Length >= 5)
if (this.DeferredInputAttachments == null && this.UseRenderPass && this.GbufferAttachments.Length >= 3)
{
this.DeferredInputAttachments = new RenderTargetIdentifier[4]
{
this.GbufferAttachmentIdentifiers[0], this.GbufferAttachmentIdentifiers[1],
this.GbufferAttachmentIdentifiers[2], this.GbufferAttachmentIdentifiers[4]
this.GbufferAttachmentIdentifiers[2], this.DepthCopyTextureIdentifier
};

this.DeferredInputIsTransient = new bool[4]
{
true, true, true, false
};
}
this.DepthAttachmentIdentifier = depthAttachment.Identifier();
Expand Down
37 changes: 32 additions & 5 deletions com.unity.render-pipelines.universal/Runtime/NativeRenderPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public partial class ScriptableRenderer
};
AttachmentDescriptor m_ActiveDepthAttachmentDescriptor;

bool[] m_IsActiveColorAttachmentTransient = new bool[]
{
false, false, false, false, false, false, false, false
};

internal RenderBufferStoreAction[] m_FinalColorStoreAction = new RenderBufferStoreAction[]
{
RenderBufferStoreAction.Store, RenderBufferStoreAction.Store, RenderBufferStoreAction.Store, RenderBufferStoreAction.Store,
Expand Down Expand Up @@ -200,6 +205,7 @@ internal void SetNativeRenderPassMRTAttachmentList(ScriptableRenderPass renderPa
UpdateFinalStoreActions(currentMergeablePasses, cameraData);

int currentAttachmentIdx = 0;
bool hasInput = false;
foreach (var passIdx in currentMergeablePasses)
{
if (passIdx == -1)
Expand Down Expand Up @@ -248,7 +254,10 @@ internal void SetNativeRenderPassMRTAttachmentList(ScriptableRenderPass renderPa
}

if (PassHasInputAttachments(pass))
{
hasInput = true;
SetupInputAttachmentIndices(pass);
}

// TODO: this is redundant and is being setup for each attachment. Needs to be done only once per mergeable pass list (we need to make sure mergeable passes use the same depth!)
m_ActiveDepthAttachmentDescriptor = new AttachmentDescriptor(SystemInfo.GetGraphicsFormat(DefaultFormat.DepthStencil));
Expand All @@ -260,6 +269,9 @@ internal void SetNativeRenderPassMRTAttachmentList(ScriptableRenderPass renderPa
if (m_UseOptimizedStoreActions)
m_ActiveDepthAttachmentDescriptor.storeAction = m_FinalDepthStoreAction;
}

if (hasInput)
SetupTransientInputAttachments(m_RenderPassesAttachmentCount[currentPassHash]);
}
}

Expand Down Expand Up @@ -504,6 +516,7 @@ internal void ExecuteNativeRenderPass(ScriptableRenderContext context, Scriptabl
for (int i = 0; i < m_ActiveColorAttachmentDescriptors.Length; ++i)
{
m_ActiveColorAttachmentDescriptors[i] = RenderingUtils.emptyAttachment;
m_IsActiveColorAttachmentTransient[i] = false;
}

m_ActiveDepthAttachmentDescriptor = RenderingUtils.emptyAttachment;
Expand All @@ -523,11 +536,25 @@ internal void SetupInputAttachmentIndices(ScriptableRenderPass pass)
continue;
}

// Assume input attachment has to be transient as ScriptableRenderPass currently has only setters for StoreAction
// We also change the target of the descriptor for it to be initialized engine-side as a transient resource.
m_ActiveColorAttachmentDescriptors[pass.m_InputAttachmentIndices[i]].loadAction = RenderBufferLoadAction.DontCare;
m_ActiveColorAttachmentDescriptors[pass.m_InputAttachmentIndices[i]].storeAction = RenderBufferStoreAction.DontCare;
m_ActiveColorAttachmentDescriptors[pass.m_InputAttachmentIndices[i]].loadStoreTarget = BuiltinRenderTextureType.None;
// Only update it as long as it has default value - if it was changed once, we assume it'll be memoryless in the whole RenderPass
if (!m_IsActiveColorAttachmentTransient[pass.m_InputAttachmentIndices[i]])
{
m_IsActiveColorAttachmentTransient[pass.m_InputAttachmentIndices[i]] = pass.IsInputAttachmentTransient(i);
}
}
}

internal void SetupTransientInputAttachments(int attachmentCount)
{
for (int i = 0; i < attachmentCount; ++i)
{
if (!m_IsActiveColorAttachmentTransient[i])
continue;

m_ActiveColorAttachmentDescriptors[i].loadAction = RenderBufferLoadAction.DontCare;
m_ActiveColorAttachmentDescriptors[i].storeAction = RenderBufferStoreAction.DontCare;
// We change the target of the descriptor for it to be initialized engine-side as a transient resource.
m_ActiveColorAttachmentDescriptors[i].loadStoreTarget = BuiltinRenderTextureType.None;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ public class CopyDepthPass : ScriptableRenderPass
private RenderTargetHandle destination { get; set; }
internal bool AllocateRT { get; set; }
internal int MssaSamples { get; set; }
// In some cases (Scene view, XR and etc.) we actually want to output to depth buffer
// So this variable needs to be set to true to enable the correct copy shader semantic
internal bool CopyToDepth { get; set; }
Material m_CopyDepthMaterial;
public CopyDepthPass(RenderPassEvent evt, Material copyDepthMaterial)
{
base.profilingSampler = new ProfilingSampler(nameof(CopyDepthPass));
AllocateRT = true;
CopyToDepth = false;
m_CopyDepthMaterial = copyDepthMaterial;
renderPassEvent = evt;
}
Expand All @@ -43,14 +47,14 @@ public void Setup(RenderTargetHandle source, RenderTargetHandle destination)
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
{
var descriptor = renderingData.cameraData.cameraTargetDescriptor;
descriptor.colorFormat = RenderTextureFormat.Depth;
descriptor.depthBufferBits = 32; //TODO: do we really need this. double check;
descriptor.graphicsFormat = GraphicsFormat.R32_SFloat;
descriptor.depthBufferBits = 0;
descriptor.msaaSamples = 1;
if (this.AllocateRT)
cmd.GetTemporaryRT(destination.id, descriptor, FilterMode.Point);

// On Metal iOS, prevent camera attachments to be bound and cleared during this pass.
ConfigureTarget(new RenderTargetIdentifier(destination.Identifier(), 0, CubemapFace.Unknown, -1), descriptor.depthStencilFormat, descriptor.width, descriptor.height, descriptor.msaaSamples, true);
ConfigureTarget(new RenderTargetIdentifier(destination.Identifier(), 0, CubemapFace.Unknown, -1), GraphicsFormat.R32_SFloat, descriptor.width, descriptor.height, descriptor.msaaSamples, false);
ConfigureClear(ClearFlag.None, Color.black);
}

Expand Down Expand Up @@ -108,6 +112,11 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
break;
}

if (CopyToDepth)
cmd.EnableShaderKeyword("_OUTPUT_DEPTH");
else
cmd.DisableShaderKeyword("_OUTPUT_DEPTH");

cmd.SetGlobalTexture("_CameraDepthAttachment", source.Identifier());


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override void Configure(CommandBuffer cmd, RenderTextureDescriptor camera
RenderTargetIdentifier lightingAttachmentId = m_DeferredLights.GbufferAttachmentIdentifiers[m_DeferredLights.GBufferLightingIndex];
RenderTargetIdentifier depthAttachmentId = m_DeferredLights.DepthAttachmentIdentifier;
if (m_DeferredLights.UseRenderPass)
ConfigureInputAttachments(m_DeferredLights.DeferredInputAttachments);
ConfigureInputAttachments(m_DeferredLights.DeferredInputAttachments, m_DeferredLights.DeferredInputIsTransient);

// TODO: change to m_DeferredLights.GetGBufferFormat(m_DeferredLights.GBufferLightingIndex) when it's not GraphicsFormat.None
// TODO: Cannot currently bind depth texture as read-only!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public DepthOnlyPass(RenderPassEvent evt, RenderQueueRange renderQueueRange, Lay
base.profilingSampler = new ProfilingSampler(nameof(DepthOnlyPass));
m_FilteringSettings = new FilteringSettings(renderQueueRange, layerMask);
renderPassEvent = evt;
useNativeRenderPass = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this pass not RP compatible anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this actually get's set later as there is a possibility that with depth prepass we are going to have the additional depth as depth. however, if we can use it as a color texture - it will get set to true

}

/// <summary>
Expand All @@ -41,7 +42,8 @@ public void Setup(
RenderTargetHandle depthAttachmentHandle)
{
this.depthAttachmentHandle = depthAttachmentHandle;
baseDescriptor.colorFormat = RenderTextureFormat.Depth;
baseDescriptor.graphicsFormat = GraphicsFormat.R32_SFloat;
// Even though this texture is going to be a color texture, we need depth buffer to correctly render it (ZTest and all)
baseDescriptor.depthBufferBits = k_DepthBufferBits;

// Depth-Only pass don't use MSAA
Expand All @@ -61,16 +63,18 @@ public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderin
// When depth priming is in use the camera target should not be overridden so the Camera's MSAA depth attachment is used.
if (renderingData.cameraData.renderer.useDepthPriming && (renderingData.cameraData.renderType == CameraRenderType.Base || renderingData.cameraData.clearDepth))
{
ConfigureTarget(renderingData.cameraData.renderer.cameraDepthTarget, descriptor.depthStencilFormat, desc.width, desc.height, 1, true);
ConfigureTarget(renderingData.cameraData.renderer.cameraDepthTarget, desc.depthStencilFormat, desc.width, desc.height, 1, true);
// Only clear depth here so we don't clear any bound color target. It might be unused by this pass but that doesn't mean we can just clear it. (e.g. in case of overlay cameras + depth priming)
ConfigureClear(ClearFlag.Depth, Color.black);
}
// When not using depth priming the camera target should be set to our non MSAA depth target.
else
{
ConfigureTarget(new RenderTargetIdentifier(depthAttachmentHandle.Identifier(), 0, CubemapFace.Unknown, -1), descriptor.depthStencilFormat, desc.width, desc.height, 1, true);
useNativeRenderPass = true;
var target = new RenderTargetIdentifier(depthAttachmentHandle.Identifier(), 0, CubemapFace.Unknown, -1);
ConfigureTarget(target, target, GraphicsFormat.R32_SFloat, desc.width, desc.height, 1);
ConfigureClear(ClearFlag.All, Color.black);
}

// Only clear depth here so we don't clear any bound color target. It might be unused by this pass but that doesn't mean we can just clear it. (e.g. in case of overlay cameras + depth priming)
ConfigureClear(ClearFlag.Depth, Color.black);
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
cameraTarget,
RenderBufferLoadAction.Load,
RenderBufferStoreAction.Store,
ClearFlag.None,
ClearFlag.Depth,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't follow why we are changing this. Could this maybe introduce regression for users that might read depend on depth after final blit pass?

Color.black);

Camera camera = cameraData.camera;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public override void Configure(CommandBuffer cmd, RenderTextureDescriptor camera

if (cmd != null)
{
if (m_DeferredLights.UseRenderPass)
{
m_DeferredLights.GbufferAttachments[m_DeferredLights.GbufferDepthIndex] = m_DeferredLights.DepthCopyTexture;
m_DeferredLights.GbufferAttachmentIdentifiers[m_DeferredLights.GbufferDepthIndex] = m_DeferredLights.DepthCopyTextureIdentifier;
}
// Create and declare the render targets used in the pass
for (int i = 0; i < gbufferAttachments.Length; ++i)
{
Expand All @@ -70,14 +75,15 @@ public override void Configure(CommandBuffer cmd, RenderTextureDescriptor camera
continue;

// No need to setup temporaryRTs if we are using input attachments as they will be Memoryless
if (m_DeferredLights.UseRenderPass && i != m_DeferredLights.GBufferShadowMask && i != m_DeferredLights.GBufferRenderingLayers)
if (m_DeferredLights.UseRenderPass && i != m_DeferredLights.GBufferShadowMask && i != m_DeferredLights.GBufferRenderingLayers && (i != m_DeferredLights.GbufferDepthIndex && !m_DeferredLights.HasDepthPrepass))
continue;

RenderTextureDescriptor gbufferSlice = cameraTextureDescriptor;
gbufferSlice.depthBufferBits = 0; // make sure no depth surface is actually created
gbufferSlice.stencilFormat = GraphicsFormat.None;
gbufferSlice.graphicsFormat = m_DeferredLights.GetGBufferFormat(i);
cmd.GetTemporaryRT(m_DeferredLights.GbufferAttachments[i].id, gbufferSlice);

cmd.GetTemporaryRT(m_DeferredLights.GbufferAttachments[i].id, gbufferSlice, FilterMode.Point);
}
}

Expand Down
Loading