feat(metal, msl-out) Support for SHADER_EARLY_DEPTH_TEST on Metal (#10625)

This commit is contained in:
Douglas Dwyer authored and GitHub committed 2026-10-30 17:22:35 +00:00
1 parent 14231a863b
commit 2375b1cd83
24 files changed
+431 -5

No files matched your search

+1
View File
@@ -155,6 +155,7 @@ By @sagudev in [#10307](https://github.com/gfx-rs/wgpu/pull/10307).
#### Metal
- Support `Features::SHADER_EARLY_DEPTH_TEST` on Metal. Naga's MSL backend now emits `[[early_fragment_tests]]` for `@early_depth_test(force)`, `depth(greater)`/`depth(less)` for `@early_depth_test(greater_equal/less_equal)`, and discards the depth output for `@early_depth_test(unchanged)` and `@early_depth_test(force)`. By @DouglasDwyer in [#10625](https://github.com/gfx-rs/wgpu/pull/10625).
- Fix Naga's Metal backend crashing when a storage texture was used as a function argument. By @ErichDonGubler in [#9867](https://github.com/gfx-rs/wgpu/pull/9867).
- Fix `max_task_workgroup_count` being misreported on pre-Apple7 devices. By @inner-daemons in [#10065](https://github.com/gfx-rs/wgpu/pull/10065).
- The value of the `max_binding_array_sampler_elements_per_shader_stage` on Apple7, Apple8, and Mac2 hardware is reduced from 1024 to 996, to match the latest Metal documentation. By @andyleiserson in [#10649](https://github.com/gfx-rs/wgpu/pull/10649).
+13 -1
View File
@@ -182,6 +182,9 @@ enum ResolvedBinding {
},
Resource(BindTarget),
Payload,
/// A fragment shader's depth output, qualified by how it may differ from
/// the depth calculated by the rasterizer.
ConservativeDepth(crate::ConservativeDepth),
}
#[derive(Copy, Clone)]
@@ -714,6 +717,15 @@ impl ResolvedBinding {
write!(out, "{name}")?;
}
Self::Attribute(index) => write!(out, "attribute({index})")?,
Self::ConservativeDepth(conservative) => {
use crate::ConservativeDepth as Cd;
let qualifier = match conservative {
Cd::GreaterEqual => "greater",
Cd::LessEqual => "less",
Cd::Unchanged => "any",
};
write!(out, "depth({qualifier})")?
}
Self::Color {
location,
blend_src,
@@ -847,7 +859,7 @@ pub fn supported_capabilities() -> crate::valid::Capabilities {
// No CULL_DISTANCE
| Caps::STORAGE_TEXTURE_16BIT_NORM_FORMATS
| Caps::MULTIVIEW
// No EARLY_DEPTH_TEST
| Caps::EARLY_DEPTH_TEST
| Caps::MULTISAMPLED_SHADING
| Caps::RAY_QUERY
| Caps::DUAL_SOURCE_BLENDING
+93 -2
View File
@@ -630,6 +630,55 @@ impl crate::Scalar {
}
}
/// Returns `true` if `binding` is the depth that a fragment shader writes.
const fn is_frag_depth(binding: Option<&crate::Binding>) -> bool {
matches!(
binding,
Some(crate::Binding::BuiltIn(crate::BuiltIn::FragDepth))
)
}
/// Returns `true` if the depth that a fragment shader writes is the only thing that
/// `entry_point` returns.
fn only_writes_frag_depth(module: &crate::ir::Module, entry_point: &crate::ir::EntryPoint) -> bool {
entry_point
.function
.result
.as_ref()
.is_some_and(|result| match module.types[result.ty].inner {
crate::TypeInner::Struct { ref members, .. } if result.binding.is_none() => members
.iter()
.all(|member| is_frag_depth(member.binding.as_ref())),
_ => is_frag_depth(result.binding.as_ref()),
})
}
/// Returns `true` if the depth that `entry_point` writes must be discarded.
///
/// MSL has no way to specify that depth is unchanged, and it does not allow functions that
/// force early fragment tests to output depth. Discarding the depth has the same effect
/// as either of those, and keeps early depth tests enabled.
fn discards_frag_depth(entry_point: &crate::ir::EntryPoint) -> bool {
entry_point.stage == crate::ShaderStage::Fragment
&& matches!(
entry_point.early_depth_test,
Some(
crate::EarlyDepthTest::Force
| crate::EarlyDepthTest::Allow {
conservative: crate::ConservativeDepth::Unchanged
}
)
)
}
/// Returns `true` if `entry_point` has nothing to return once its depth is discarded.
fn only_writes_discarded_depth(
module: &crate::ir::Module,
entry_point: &crate::ir::EntryPoint,
) -> bool {
discards_frag_depth(entry_point) && only_writes_frag_depth(module, entry_point)
}
const fn separate(need_separator: bool) -> &'static str {
if need_separator {
","
@@ -3661,6 +3710,15 @@ impl<W: Write> Writer<W> {
result_struct: Option<&str>,
context: &ExpressionContext,
) -> BackendResult {
let entry_point = match context.origin {
FunctionOrigin::EntryPoint(index) => Some(&context.module.entry_points[index as usize]),
FunctionOrigin::Handle(_) => None,
};
if entry_point.is_some_and(|ep| only_writes_discarded_depth(context.module, ep)) {
writeln!(self.out, "{level}return;")?;
return Ok(());
}
match result_struct {
Some(struct_name) => {
let mut has_point_size = false;
@@ -3685,6 +3743,12 @@ impl<W: Write> Writer<W> {
}
}
if entry_point.is_some_and(discards_frag_depth)
&& is_frag_depth(member.binding.as_ref())
{
continue;
}
let comma = if is_first { "" } else { "," };
is_first = false;
let name = &self.names[&NameKey::StructMember(result_ty, index as u32)];
@@ -7523,7 +7587,10 @@ template <typename A>
let stage_out_name = self.namer.call(&format!("{fun_name}Output"));
let result_member_name = self.namer.call("member");
let result_type_name = match fun.result {
Some(ref result) if ep.stage != crate::ShaderStage::Task => {
Some(ref result)
if ep.stage != crate::ShaderStage::Task
&& !only_writes_discarded_depth(module, ep) =>
{
let mut result_members = Vec::new();
if let crate::TypeInner::Struct { ref members, .. } =
module.types[result.ty].inner
@@ -7564,6 +7631,10 @@ template <typename A>
}
}
if discards_frag_depth(ep) && is_frag_depth(Some(binding)) {
continue;
}
let array_len = match module.types[ty].inner {
crate::TypeInner::Array {
size: crate::ArraySize::Constant(size),
@@ -7571,7 +7642,16 @@ template <typename A>
} => Some(size),
_ => None,
};
let resolved = options.resolve_local_binding(binding, out_mode)?;
let resolved = match (
options.resolve_local_binding(binding, out_mode)?,
ep.early_depth_test,
) {
(
super::ResolvedBinding::BuiltIn(crate::BuiltIn::FragDepth),
Some(crate::EarlyDepthTest::Allow { conservative }),
) => super::ResolvedBinding::ConservativeDepth(conservative),
(resolved, _) => resolved,
};
write!(self.out, "{}{} {}", back::INDENT, ty_name, name)?;
resolved.try_fmt(&mut self.out)?;
if let Some(array_len) = array_len {
@@ -7658,6 +7738,17 @@ template <typename A>
)?;
}
if ep.stage == crate::ShaderStage::Fragment
&& ep.early_depth_test == Some(crate::EarlyDepthTest::Force)
{
if options.lang_version < (1, 2) {
return Err(Error::UnsupportedAttribute(
"early_fragment_tests".to_string(),
));
}
write!(self.out, "[[early_fragment_tests]] ")?;
}
// Write the entry point function's name, and begin its argument list.
if let Some(em_str) = em_str {
write!(self.out, "{em_str} ")?;
@@ -0,0 +1,2 @@
capabilities = "EARLY_DEPTH_TEST"
targets = "METAL"
@@ -0,0 +1,5 @@
@fragment
@early_depth_test(greater_equal)
fn main(@builtin(position) pos: vec4<f32>) -> @builtin(frag_depth) f32 {
return pos.z + 0.1;
}
@@ -0,0 +1,2 @@
capabilities = "EARLY_DEPTH_TEST"
targets = "METAL"
@@ -0,0 +1,32 @@
struct Output {
@location(0) color: vec4<f32>,
@builtin(frag_depth) depth: f32,
}
@fragment
@early_depth_test(less_equal)
fn agree_first(@builtin(position) pos: vec4<f32>) -> Output {
return Output(vec4<f32>(0.4, 0.3, 0.2, 0.1), pos.z - 0.1);
}
@fragment
@early_depth_test(less_equal)
fn agree_second(@builtin(position) pos: vec4<f32>) -> Output {
return Output(vec4<f32>(0.1, 0.2, 0.3, 0.4), pos.z - 0.2);
}
struct DisagreeingOutput {
@location(0) color: vec4<f32>,
@builtin(frag_depth) depth: f32,
}
@fragment
@early_depth_test(greater_equal)
fn disagree_conservative(@builtin(position) pos: vec4<f32>) -> DisagreeingOutput {
return DisagreeingOutput(vec4<f32>(0.4, 0.3, 0.2, 0.1), pos.z + 0.1);
}
@fragment
fn disagree_unrestricted(@builtin(position) pos: vec4<f32>) -> DisagreeingOutput {
return DisagreeingOutput(vec4<f32>(0.1, 0.2, 0.3, 0.4), pos.z * 2.0);
}
@@ -0,0 +1,2 @@
capabilities = "EARLY_DEPTH_TEST"
targets = "METAL"
@@ -0,0 +1,5 @@
@fragment
@early_depth_test(unchanged)
fn main(@builtin(position) pos: vec4<f32>) -> @builtin(frag_depth) f32 {
return pos.z;
}
@@ -1,5 +1,5 @@
capabilities = "EARLY_DEPTH_TEST"
targets = "SPIRV | GLSL"
targets = "SPIRV | GLSL | METAL"
[glsl]
version.Desktop = 420
@@ -0,0 +1,5 @@
capabilities = "EARLY_DEPTH_TEST"
targets = "METAL"
[msl]
lang_version = [1, 2]
@@ -0,0 +1,16 @@
struct ColorAndDepth {
@location(0) color: vec4<f32>,
@builtin(frag_depth) depth: f32,
}
@fragment
@early_depth_test(force)
fn only_depth(@builtin(position) pos: vec4<f32>) -> @builtin(frag_depth) f32 {
return pos.z;
}
@fragment
@early_depth_test(force)
fn color_and_depth(@builtin(position) pos: vec4<f32>) -> ColorAndDepth {
return ColorAndDepth(vec4<f32>(0.4, 0.3, 0.2, 0.1), pos.z);
}
@@ -1,2 +1,5 @@
capabilities = "EARLY_DEPTH_TEST"
targets = "SPIRV | GLSL"
targets = "SPIRV | GLSL | METAL"
[msl]
lang_version = [1, 2]
@@ -0,0 +1,2 @@
capabilities = "EARLY_DEPTH_TEST"
targets = "METAL"
@@ -0,0 +1,25 @@
struct ColorAndDepth {
@location(0) color: vec4<f32>,
@builtin(frag_depth) depth: f32,
}
struct OnlyDepth {
@builtin(frag_depth) depth: f32,
}
@fragment
@early_depth_test(unchanged)
fn color_and_depth(@builtin(position) pos: vec4<f32>) -> ColorAndDepth {
return ColorAndDepth(vec4<f32>(0.4, 0.3, 0.2, 0.1), pos.z);
}
@fragment
@early_depth_test(unchanged)
fn only_depth(@builtin(position) pos: vec4<f32>) -> OnlyDepth {
return OnlyDepth(pos.z);
}
@fragment
fn unrestricted(@builtin(position) pos: vec4<f32>) -> ColorAndDepth {
return ColorAndDepth(vec4<f32>(0.1, 0.2, 0.3, 0.4), pos.z * 2.0);
}
@@ -0,0 +1,17 @@
// language: metal1.0
#include <metal_stdlib>
#include <simd/simd.h>
using metal::uint;
struct main_Input {
};
struct main_Output {
float member [[depth(greater)]];
};
fragment main_Output main_(
metal::float4 pos [[position]]
) {
return main_Output { pos.z + 0.1 };
}
@@ -0,0 +1,71 @@
// language: metal1.0
#include <metal_stdlib>
#include <simd/simd.h>
using metal::uint;
struct Output {
metal::float4 color;
float depth;
char _pad2[12];
};
struct DisagreeingOutput {
metal::float4 color;
float depth;
char _pad2[12];
};
struct agree_firstInput {
};
struct agree_firstOutput {
metal::float4 color [[color(0)]];
float depth [[depth(less)]];
};
fragment agree_firstOutput agree_first(
metal::float4 pos [[position]]
) {
const auto _tmp = Output {metal::float4(0.4, 0.3, 0.2, 0.1), pos.z - 0.1};
return agree_firstOutput { _tmp.color, _tmp.depth };
}
struct agree_secondInput {
};
struct agree_secondOutput {
metal::float4 color [[color(0)]];
float depth [[depth(less)]];
};
fragment agree_secondOutput agree_second(
metal::float4 pos_1 [[position]]
) {
const auto _tmp_1 = Output {metal::float4(0.1, 0.2, 0.3, 0.4), pos_1.z - 0.2};
return agree_secondOutput { _tmp_1.color, _tmp_1.depth };
}
struct disagree_conservativeInput {
};
struct disagree_conservativeOutput {
metal::float4 color [[color(0)]];
float depth [[depth(greater)]];
};
fragment disagree_conservativeOutput disagree_conservative(
metal::float4 pos_2 [[position]]
) {
const auto _tmp_2 = DisagreeingOutput {metal::float4(0.4, 0.3, 0.2, 0.1), pos_2.z + 0.1};
return disagree_conservativeOutput { _tmp_2.color, _tmp_2.depth };
}
struct disagree_unrestrictedInput {
};
struct disagree_unrestrictedOutput {
metal::float4 color [[color(0)]];
float depth [[depth(any)]];
};
fragment disagree_unrestrictedOutput disagree_unrestricted(
metal::float4 pos_3 [[position]]
) {
const auto _tmp_3 = DisagreeingOutput {metal::float4(0.1, 0.2, 0.3, 0.4), pos_3.z * 2.0};
return disagree_unrestrictedOutput { _tmp_3.color, _tmp_3.depth };
}
@@ -0,0 +1,14 @@
// language: metal1.0
#include <metal_stdlib>
#include <simd/simd.h>
using metal::uint;
struct main_Input {
};
fragment void main_(
metal::float4 pos [[position]]
) {
return;
}
@@ -0,0 +1,17 @@
// language: metal1.0
#include <metal_stdlib>
#include <simd/simd.h>
using metal::uint;
struct main_Input {
};
struct main_Output {
float member [[depth(less)]];
};
fragment main_Output main_(
metal::float4 pos [[position]]
) {
return main_Output { pos.z - 0.1 };
}
@@ -0,0 +1,32 @@
// language: metal1.2
#include <metal_stdlib>
#include <simd/simd.h>
using metal::uint;
struct ColorAndDepth {
metal::float4 color;
float depth;
char _pad2[12];
};
struct only_depthInput {
};
[[early_fragment_tests]] fragment void only_depth(
metal::float4 pos [[position]]
) {
return;
}
struct color_and_depthInput {
};
struct color_and_depthOutput {
metal::float4 color [[color(0)]];
};
[[early_fragment_tests]] fragment color_and_depthOutput color_and_depth(
metal::float4 pos_1 [[position]]
) {
const auto _tmp = ColorAndDepth {metal::float4(0.4, 0.3, 0.2, 0.1), pos_1.z};
return color_and_depthOutput { _tmp.color };
}
@@ -0,0 +1,14 @@
// language: metal1.2
#include <metal_stdlib>
#include <simd/simd.h>
using metal::uint;
struct main_Output {
metal::float4 member [[color(0)]];
};
[[early_fragment_tests]] fragment main_Output main_(
) {
return main_Output { metal::float4(0.4, 0.3, 0.2, 0.1) };
}
@@ -0,0 +1,49 @@
// language: metal1.0
#include <metal_stdlib>
#include <simd/simd.h>
using metal::uint;
struct ColorAndDepth {
metal::float4 color;
float depth;
char _pad2[12];
};
struct OnlyDepth {
float depth;
};
struct color_and_depthInput {
};
struct color_and_depthOutput {
metal::float4 color [[color(0)]];
};
fragment color_and_depthOutput color_and_depth(
metal::float4 pos [[position]]
) {
const auto _tmp = ColorAndDepth {metal::float4(0.4, 0.3, 0.2, 0.1), pos.z};
return color_and_depthOutput { _tmp.color };
}
struct only_depthInput {
};
fragment void only_depth(
metal::float4 pos_1 [[position]]
) {
return;
}
struct unrestrictedInput {
};
struct unrestrictedOutput {
metal::float4 color [[color(0)]];
float depth [[depth(any)]];
};
fragment unrestrictedOutput unrestricted(
metal::float4 pos_2 [[position]]
) {
const auto _tmp_1 = ColorAndDepth {metal::float4(0.1, 0.2, 0.3, 0.4), pos_2.z * 2.0};
return unrestrictedOutput { _tmp_1.color, _tmp_1.depth };
}
+4
View File
@@ -1286,6 +1286,10 @@ impl super::CapabilitiesQuery {
F::DUAL_SOURCE_BLENDING,
self.msl_version >= MTLLanguageVersion::Version1_2 && self.dual_source_blending,
);
features.set(
F::SHADER_EARLY_DEPTH_TEST,
MTLLanguageVersion::Version1_2 <= self.msl_version,
);
features.set(F::TEXTURE_COMPRESSION_ASTC, self.format_astc);
features.set(F::TEXTURE_COMPRESSION_ASTC_HDR, self.format_astc_hdr);
features.set(F::TEXTURE_COMPRESSION_ASTC_SLICED_3D, self.format_astc_3d);
+5
View File
@@ -807,6 +807,8 @@ features! {
///
/// - `layout(early_fragment_tests) in;` (GLSL)
///
/// - `[[early_fragment_tests]]` (MSL 1.2+)
///
/// 2. Provide a conservative depth specifier that allows an additional early
/// depth test under certain conditions:
///
@@ -814,10 +816,13 @@ features! {
///
/// - `layout(depth_<greater/less/unchanged>) out float gl_FragDepth;` (GLSL)
///
/// - `[[depth(greater/less)]]` (MSL, any version)
///
/// See [`EarlyDepthTest`] for more details.
///
/// Supported platforms:
/// - Vulkan
/// - Metal (with MSL 1.2+)
/// - GLES 3.1+
///
/// This is a native only feature.