Skip to content

Commit ca3ed61

Browse files
thomas-zengEvergreen
authored andcommitted
[content automatically redacted] touching PlatformDependent folder
1 parent b9a2759 commit ca3ed61

File tree

5 files changed

+198
-1
lines changed

5 files changed

+198
-1
lines changed

Packages/com.unity.render-pipelines.core/Runtime/RenderGraph/Compiler/PassesData.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,20 @@ static bool CanMergeNativeSubPass(CompilerContextData contextData, ref NativePas
11761176
// Check if we're handling the depth attachment
11771177
if (currRenderGraphPassHasDepth && fragmentIdx == 0)
11781178
{
1179+
#if DEVELOPMENT_BUILD || UNITY_EDITOR
1180+
// If depth input attachment flag is set, depth must be read-only
1181+
if (passToMerge.extendedFeatureFlags.HasFlag(ExtendedFeatureFlags.DepthAttachmentAsInputAttachment))
1182+
{
1183+
if (graphPassFragment.accessFlags.HasFlag(AccessFlags.Write))
1184+
{
1185+
// Depth input attachments cannot have write access
1186+
throw new InvalidOperationException(
1187+
RenderGraph.RenderGraphExceptionMessages.DepthInputAttachmentWithWriteAccess(
1188+
passToMerge.GetName(contextData).name));
1189+
}
1190+
}
1191+
#endif
1192+
11791193
flags = (graphPassFragment.accessFlags.HasFlag(AccessFlags.Write))
11801194
? SubPassFlags.None
11811195
: SubPassFlags.ReadOnlyDepth;
@@ -1227,6 +1241,13 @@ static bool CanMergeNativeSubPass(CompilerContextData contextData, ref NativePas
12271241
inputIndex++;
12281242
}
12291243

1244+
// Check if depth is used as input attachment
1245+
if (passToMerge.extendedFeatureFlags.HasFlag(ExtendedFeatureFlags.DepthAttachmentAsInputAttachment) && nativePass.hasDepth)
1246+
{
1247+
// Depth input attachments are read-only
1248+
flags = SubPassFlags.ReadOnlyDepth;
1249+
}
1250+
12301251
// last check for flags
12311252
return flags == lastPass.flags;
12321253
}
@@ -1278,6 +1299,20 @@ public static void TryMergeNativeSubPass(CompilerContextData contextData, ref Na
12781299
// Check if we're handling the depth attachment
12791300
if (passToMerge.fragmentInfoHasDepth && fragmentIdx == 0)
12801301
{
1302+
#if DEVELOPMENT_BUILD || UNITY_EDITOR
1303+
// If depth input attachment flag is set, depth must be read-only
1304+
if (passToMerge.extendedFeatureFlags.HasFlag(ExtendedFeatureFlags.DepthAttachmentAsInputAttachment))
1305+
{
1306+
if (graphPassFragment.accessFlags.HasFlag(AccessFlags.Write))
1307+
{
1308+
// Depth input attachments cannot have write access
1309+
throw new InvalidOperationException(
1310+
RenderGraph.RenderGraphExceptionMessages.DepthInputAttachmentWithWriteAccess(
1311+
passToMerge.GetName(contextData).name));
1312+
}
1313+
}
1314+
#endif
1315+
12811316
desc.flags = (graphPassFragment.accessFlags.HasFlag(AccessFlags.Write))
12821317
? SubPassFlags.None
12831318
: SubPassFlags.ReadOnlyDepth;
@@ -1331,6 +1366,13 @@ public static void TryMergeNativeSubPass(CompilerContextData contextData, ref Na
13311366
}
13321367
}
13331368

1369+
// Check if depth is used as input attachment
1370+
if (passToMerge.extendedFeatureFlags.HasFlag(ExtendedFeatureFlags.DepthAttachmentAsInputAttachment) && nativePass.hasDepth)
1371+
{
1372+
// Depth input attachments are read-only
1373+
desc.flags = SubPassFlags.ReadOnlyDepth;
1374+
}
1375+
13341376
// Shading rate images
13351377
{
13361378
if (passToMerge.fragmentInfoHasShadingRateImage)

Packages/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraph.ExceptionMessages.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,22 @@ internal static string IncompatibleTextureUVOrigin(TextureUVOriginSelection orig
121121
internal static string IncompatibleTextureUVOriginUseTexture(TextureUVOriginSelection origin) =>
122122
$"UseTexture() of a resource with `{origin}` is not compatible with Unity's standard UV origin for texture reading {TextureUVOrigin.BottomLeft}. Are you trying to UseTexture() on a backbuffer?";
123123

124+
internal static string DepthInputAttachmentNotEnabled(string passName) =>
125+
$"In pass '{passName}' when trying to call SetInputAttachment() with a depth texture - " +
126+
$"This requires calling builder.SetExtendedFeatureFlags(ExtendedFeatureFlags.DepthAttachmentAsInputAttachment) first. " +
127+
$"The extended feature flag was not set before using depth as input attachment.";
128+
129+
internal static string DepthInputAttachmentNotSupported(string passName) =>
130+
$"In pass '{passName}' - " +
131+
$"Read-Only depth attachment as input attachment is not supported on the current platform. " +
132+
$"Platform: {SystemInfo.graphicsDeviceType}. " +
133+
$"Use SystemInfo.supportsDepthAttachmentAsInputAttachment to check support at runtime.";
134+
135+
internal static string DepthInputAttachmentWithWriteAccess(string passName) =>
136+
$"In pass '{passName}' - " +
137+
$"Depth attachment has Write access but ExtendedFeatureFlags.DepthAttachmentAsInputAttachment is set. " +
138+
$"Depth input attachments must be read-only. Remove write access from the depth attachment.";
139+
124140
// RenderGraphPass
125141
internal const string k_MoreThanOneResourceForMRTIndex =
126142
"You can only bind a single texture to a single index in a multiple render texture (MRT). Verify your indexes are correct.";

Packages/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraph.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public enum ExtendedFeatureFlags
6262
MultiviewRenderRegionsCompatible = 1 << 1,
6363
///<summary>On Meta XR, this flag can be set to use MSAA shader resolve in the last subpass of a render pass. </summary>
6464
MultisampledShaderResolve = 1 << 2,
65+
/// <summary>
66+
/// Indicates this pass uses depth texture as input attachment (framebuffer fetch with depth).
67+
/// Required before calling SetInputAttachment() with a depth texture.
68+
/// </summary>
69+
DepthAttachmentAsInputAttachment = 1 << 3,
6570
}
6671

6772
[Flags]

Packages/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphBuilders.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,31 @@ public void Setup(RenderGraphPass renderPass, RenderGraphResourceRegistry resour
7575
#endif
7676
}
7777

78+
[Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")]
79+
private void CheckDepthInputAttachmentFlagEnabled()
80+
{
81+
if (RenderGraph.enableValidityChecks)
82+
{
83+
if (!m_RenderPass.extendedFeatureFlags.HasFlag(ExtendedFeatureFlags.DepthAttachmentAsInputAttachment))
84+
{
85+
throw new InvalidOperationException(
86+
RenderGraphExceptionMessages.DepthInputAttachmentNotEnabled(m_RenderPass.name));
87+
}
88+
}
89+
}
90+
91+
[Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")]
92+
private void CheckDepthInputAttachmentSupported()
93+
{
94+
if (RenderGraph.enableValidityChecks)
95+
{
96+
if (!SystemInfo.supportsDepthAttachmentAsInputAttachment)
97+
{
98+
throw new InvalidOperationException(
99+
RenderGraphExceptionMessages.DepthInputAttachmentNotSupported(m_RenderPass.name));
100+
}
101+
}
102+
}
78103

79104
public void EnableAsyncCompute(bool value)
80105
{
@@ -531,7 +556,20 @@ public void SetInputAttachment(TextureHandle tex, int index, AccessFlags flags,
531556

532557
CheckFrameBufferFetchEmulationIsSupported(tex);
533558

534-
CheckUseFragment(tex, false);
559+
// Check if this is a depth texture - requires DepthAttachmentAsInputAttachment feature flag
560+
m_Resources.GetRenderTargetInfo(tex.handle, out var info);
561+
if (GraphicsFormatUtility.IsDepthFormat(info.format))
562+
{
563+
CheckDepthInputAttachmentFlagEnabled();
564+
CheckDepthInputAttachmentSupported();
565+
CheckUseFragment(tex, true);
566+
}
567+
else
568+
{
569+
// Note: Regular color input attachments don't require extended feature flags - supported everywhere
570+
CheckUseFragment(tex, false);
571+
}
572+
535573
var versionedTextureHandle = new TextureHandle(UseResource(tex.handle, flags));
536574
m_RenderPass.SetFragmentInputRaw(versionedTextureHandle, index, flags, mipLevel, depthSlice);
537575
}

Packages/com.unity.render-pipelines.core/Tests/Editor/RenderGraphTests.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2767,5 +2767,101 @@ public void CanMergeBackBufferAndCustomRenderTargetsDepth()
27672767

27682768
m_RenderGraph.Cleanup();
27692769
}
2770+
2771+
class DepthInputAttachmentPassData
2772+
{
2773+
public TextureHandle depthTexture;
2774+
public TextureHandle colorTexture;
2775+
}
2776+
2777+
[Test]
2778+
public void SetInputAttachment_WithDepthTarget_Works()
2779+
{
2780+
const int kWidth = 4;
2781+
const int kHeight = 4;
2782+
2783+
m_RenderGraphTestPipeline.recordRenderGraphBody = (context, camera, cmd) =>
2784+
{
2785+
if (!SystemInfo.supportsDepthAttachmentAsInputAttachment)
2786+
{
2787+
return; // Skip the test if the platform does not support multisampled shader resolve
2788+
}
2789+
2790+
TextureHandle depthTexture = m_RenderGraph.CreateTexture(new TextureDesc(kWidth, kHeight) { colorFormat = GraphicsFormat.D24_UNorm });
2791+
TextureHandle colorTexture = m_RenderGraph.CreateTexture(new TextureDesc(kWidth, kHeight) { colorFormat = GraphicsFormat.R8G8B8A8_UNorm });
2792+
2793+
// First pass: Write to depth buffer
2794+
using (var builder = m_RenderGraph.AddRasterRenderPass<DepthInputAttachmentPassData>("DepthWritePass", out var passData))
2795+
{
2796+
builder.SetRenderAttachment(colorTexture, 0, AccessFlags.Write);
2797+
builder.SetRenderAttachmentDepth(depthTexture, AccessFlags.Write);
2798+
builder.AllowPassCulling(false);
2799+
builder.SetRenderFunc((DepthInputAttachmentPassData data, RasterGraphContext context) => { });
2800+
}
2801+
2802+
// Second pass: Read depth buffer as input attachment
2803+
using (var builder = m_RenderGraph.AddRasterRenderPass<DepthInputAttachmentPassData>("DepthInputAttachmentPass", out var passData))
2804+
{
2805+
passData.depthTexture = depthTexture;
2806+
passData.colorTexture = colorTexture;
2807+
2808+
builder.SetRenderAttachment(colorTexture, 0, AccessFlags.Write);
2809+
// Set extended feature flag for depth input attachment
2810+
builder.SetExtendedFeatureFlags(ExtendedFeatureFlags.DepthAttachmentAsInputAttachment);
2811+
builder.SetInputAttachment(depthTexture, 0, AccessFlags.Read);
2812+
2813+
builder.AllowPassCulling(false);
2814+
builder.SetRenderFunc((DepthInputAttachmentPassData data, RasterGraphContext context) =>
2815+
{
2816+
// Depth texture is used as input attachment here
2817+
Assert.IsTrue(data.depthTexture.IsValid());
2818+
});
2819+
}
2820+
};
2821+
2822+
m_Camera.Render();
2823+
}
2824+
2825+
[Test]
2826+
public void SetInputAttachment_WithDepthTarget_ThrowsException_WhenExtendedFeatureFlagNotSet()
2827+
{
2828+
const int kWidth = 4;
2829+
const int kHeight = 4;
2830+
2831+
m_RenderGraphTestPipeline.recordRenderGraphBody = (context, camera, cmd) =>
2832+
{
2833+
if (!SystemInfo.supportsDepthAttachmentAsInputAttachment)
2834+
{
2835+
return; // Skip the test if the platform does not support multisampled shader resolve
2836+
}
2837+
2838+
TextureHandle depthTexture = m_RenderGraph.CreateTexture(new TextureDesc(kWidth, kHeight) { colorFormat = GraphicsFormat.D24_UNorm });
2839+
TextureHandle colorTexture = m_RenderGraph.CreateTexture(new TextureDesc(kWidth, kHeight) { colorFormat = GraphicsFormat.R8G8B8A8_UNorm });
2840+
2841+
// First pass: Write to depth buffer
2842+
using (var builder = m_RenderGraph.AddRasterRenderPass<DepthInputAttachmentPassData>("DepthWritePass", out var passData))
2843+
{
2844+
builder.SetRenderAttachment(colorTexture, 0, AccessFlags.Write);
2845+
builder.SetRenderAttachmentDepth(depthTexture, AccessFlags.Write);
2846+
builder.AllowPassCulling(false);
2847+
builder.SetRenderFunc((DepthInputAttachmentPassData data, RasterGraphContext context) => { });
2848+
}
2849+
2850+
// Second pass: Using depth buffer as input attachment without setting the feature flag (not allowed and should throw)
2851+
Assert.Throws<System.InvalidOperationException>(() =>
2852+
{
2853+
using (var builder = m_RenderGraph.AddRasterRenderPass<DepthInputAttachmentPassData>("DepthInputAttachmentPass", out var passData))
2854+
{
2855+
// NOT calling builder.SetExtendedFeatureFlags(ExtendedFeatureFlags.DepthAttachmentAsInputAttachment)
2856+
builder.SetRenderAttachment(colorTexture, 0, AccessFlags.Write);
2857+
builder.SetInputAttachment(depthTexture, 0, AccessFlags.Read); // This should throw
2858+
builder.AllowPassCulling(false);
2859+
builder.SetRenderFunc((DepthInputAttachmentPassData data, RasterGraphContext context) => { });
2860+
}
2861+
});
2862+
};
2863+
2864+
m_Camera.Render();
2865+
}
27702866
}
27712867
}

0 commit comments

Comments
 (0)