Skip to content

Universal/depth copy as color #5987

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

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 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
4deded8
Merge branch 'master' into universal/depth-copy-as-color
jonuuukas Oct 6, 2021
f95fafb
Merge branch 'master' into universal/depth-copy-as-color
jonuuukas Oct 11, 2021
159580e
fixing DBuffer Decals CopyDepth pass not copying the depth and failin…
jonuuukas Oct 11, 2021
f9152cf
adding fb fetch to Gbuffer decals and some other depth related fixes …
jonuuukas Oct 12, 2021
b5a042f
Merge branch 'universal/depth-copy-as-color' of ssh://github.com/Unit…
jonuuukas Oct 12, 2021
62ca66d
fixing shader compilation error and renaming define for fb fetch idx
jonuuukas Oct 12, 2021
fcb7d7f
fix for RTIs non render pass platforms
jonuuukas Oct 12, 2021
427e0cb
another DeferredLights null check
jonuuukas Oct 13, 2021
850428a
null exception fix
jonuuukas Oct 13, 2021
70ad6ee
adding some editor-specific code to avoid a bug with editor setting i…
jonuuukas Oct 14, 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 @@ -63,7 +63,10 @@ Pass
#ifdef SCENEPICKINGPASS
float4 _SelectionID;
#endif

#if _RENDER_PASS_ENABLED
#define GBUFFER3 0
FRAMEBUFFER_INPUT_HALF(GBUFFER3);
#endif
// Includes
$splice(PreGraphIncludes)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,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 @@ -181,12 +181,20 @@ void Frag(PackedVaryings packedInput,

#if defined(DECAL_PROJECTOR)
#if UNITY_REVERSED_Z
#if _RENDER_PASS_ENABLED
float depth = LOAD_FRAMEBUFFER_INPUT(GBUFFER3, input.positionCS.xy);
#else
float depth = LoadSceneDepth(input.positionCS.xy);
#endif
#else
#if _RENDER_PASS_ENABLED
float depth = lerp(UNITY_NEAR_CLIP_VALUE, 1, LOAD_FRAMEBUFFER_INPUT(GBUFFER3, input.positionCS.xy));
#else
// Adjust z to match NDC for OpenGL
float depth = lerp(UNITY_NEAR_CLIP_VALUE, 1, LoadSceneDepth(input.positionCS.xy));
#endif
#endif
#endif

#if defined(DECAL_RECONSTRUCT_NORMAL)
#if defined(_DECAL_NORMAL_BLEND_HIGH)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,7 @@ static class Descriptors
{ CoreKeywordDescriptors.MixedLightingSubtractive },
{ Descriptors.DecalsNormalBlend },
{ CoreKeywordDescriptors.GBufferNormalsOct },
{ CoreKeywordDescriptors.RenderPassEnabled },
{ Descriptors.LodCrossFade, new FieldCondition(Fields.LodCrossFade, true) },
};

Expand All @@ -986,6 +987,7 @@ static class Descriptors
{ CoreKeywordDescriptors.ShadowsSoft },
{ Descriptors.DecalsNormalBlend },
{ CoreKeywordDescriptors.GBufferNormalsOct },
{ CoreKeywordDescriptors.RenderPassEnabled },
};
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal class DecalGBufferRenderPass : ScriptableRenderPass
private DecalDrawGBufferSystem m_DrawSystem;
private DecalScreenSpaceSettings m_Settings;
private DeferredLights m_DeferredLights;
private RenderTargetIdentifier[] m_GbufferAttachments;

public DecalGBufferRenderPass(DecalScreenSpaceSettings settings, DecalDrawGBufferSystem drawSystem)
{
Expand All @@ -41,7 +42,20 @@ internal void Setup(DeferredLights deferredLights)

public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
{
ConfigureTarget(m_DeferredLights.GbufferAttachmentIdentifiers, m_DeferredLights.DepthAttachmentIdentifier);
if (m_DeferredLights != null && m_DeferredLights.UseRenderPass)
{
if (m_GbufferAttachments == null)
m_GbufferAttachments = new RenderTargetIdentifier[]
{
m_DeferredLights.GbufferAttachmentIdentifiers[0], m_DeferredLights.GbufferAttachmentIdentifiers[1],
m_DeferredLights.GbufferAttachmentIdentifiers[2], m_DeferredLights.GbufferAttachmentIdentifiers[3]
};
ConfigureInputAttachments(m_DeferredLights.DepthCopyTextureIdentifier, false);
}
else
m_GbufferAttachments = m_DeferredLights.GbufferAttachmentIdentifiers;

ConfigureTarget(m_GbufferAttachments, m_DeferredLights.DepthAttachmentIdentifier);
}

public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
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 @@ -188,6 +188,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 @@ -402,12 +403,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,6 +18,9 @@ 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;

internal bool m_CopyResolvedDepth;
Expand All @@ -26,6 +29,7 @@ public CopyDepthPass(RenderPassEvent evt, Material copyDepthMaterial)
{
base.profilingSampler = new ProfilingSampler(nameof(CopyDepthPass));
AllocateRT = true;
CopyToDepth = false;
m_CopyDepthMaterial = copyDepthMaterial;
renderPassEvent = evt;
m_CopyResolvedDepth = false;
Expand All @@ -47,14 +51,25 @@ 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;
#if UNITY_EDITOR
descriptor.depthBufferBits = 16;

#else
descriptor.depthBufferBits = 0;

#endif
descriptor.msaaSamples = 1;
if (this.AllocateRT)
cmd.GetTemporaryRT(destination.id, descriptor, FilterMode.Point);

var target = new RenderTargetIdentifier(destination.Identifier(), 0, CubemapFace.Unknown, -1);
#if UNITY_EDITOR
ConfigureTarget(target, target, GraphicsFormat.R32_SFloat, descriptor.width, descriptor.height, descriptor.msaaSamples);
#else
// 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(target, GraphicsFormat.R32_SFloat, descriptor.width, descriptor.height, descriptor.msaaSamples, false);
#endif
ConfigureClear(ClearFlag.None, Color.black);
}

Expand Down Expand Up @@ -112,6 +127,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;
}

/// <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 @@ -109,7 +109,7 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
cameraTarget,
RenderBufferLoadAction.Load,
RenderBufferStoreAction.Store,
ClearFlag.None,
ClearFlag.Depth,
Color.black);

Camera camera = cameraData.camera;
Expand Down
Loading