From 313bc97b649dcafe8eb99a8ba450f8b30e57b02e Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Thu, 29 Oct 2026 17:51:19 +0000 Subject: [PATCH] feat(metal): report barycentric and pull-model interpolation features --- .../src/gpu/compiler/metal_compiler.c | 7 ++++- .../src/gpu/compiler/metal_compiler.h | 5 +++- .../src/gpu/compiler/tests/metal_compiler.c | 28 +++++++++++++++++++ .../stela-drivers/src/gpu/metal/compiler.rs | 11 ++++++-- .../src/gpu/metal/compiler/ffi.rs | 2 ++ .../src/gpu/metal/compiler/tests.rs | 24 ++++++++++++++++ 6 files changed, 73 insertions(+), 4 deletions(-) diff --git a/crates/stela-drivers/src/gpu/compiler/metal_compiler.c b/crates/stela-drivers/src/gpu/compiler/metal_compiler.c index 5aa5a6f37f..a36fdbcc08 100644 --- a/crates/stela-drivers/src/gpu/compiler/metal_compiler.c +++ b/crates/stela-drivers/src/gpu/compiler/metal_compiler.c @@ -201,7 +201,12 @@ add_input(struct reflecting *r, const struct stela_air_stage_io *io) case STELA_AIR_STAGE_BUFFER: return add_buffer(r, &io->buffer, io->argument_buffer); case STELA_AIR_STAGE_TEXTURE: return add_texture(r, &io->texture); case STELA_AIR_STAGE_SAMPLER: return add_sampler(r, &io->sampler); - case STELA_AIR_VARYING: return add_varying(r, io); + case STELA_AIR_VARYING: + out->interpolants |= io->interpolant; + return add_varying(r, io); + case STELA_AIR_BARYCENTRIC: + out->barycentrics = 1; + return true; case STELA_AIR_VERTEX_INPUT: return add_attribute(r, io->location, io->primitive, io->components); case STELA_AIR_CONTROL_POINTS: diff --git a/crates/stela-drivers/src/gpu/compiler/metal_compiler.h b/crates/stela-drivers/src/gpu/compiler/metal_compiler.h index 47ecae768e..825e360288 100644 --- a/crates/stela-drivers/src/gpu/compiler/metal_compiler.h +++ b/crates/stela-drivers/src/gpu/compiler/metal_compiler.h @@ -73,6 +73,9 @@ struct stela_metal_reflection { /* A post-tessellation vertex function's enum stela_air_patch and the * control points it declares, zero when it declares none. */ uint32_t patch, patch_control_points; + /* Whether a fragment function reads its barycentric coordinates, and + * interpolates an interpolant. */ + uint32_t barycentrics, interpolants; struct stela_metal_buffer_use buffers[STELA_METAL_BUFFERS]; struct stela_metal_texture_use textures[STELA_METAL_TEXTURES]; uint32_t samplers[STELA_METAL_SAMPLERS]; @@ -83,7 +86,7 @@ struct stela_metal_reflection { struct stela_metal_function_constant function_constants[STELA_METAL_FUNCTION_CONSTANTS]; char names[STELA_METAL_NAME_BYTES]; }; -#define STELA_METAL_REFLECTION_BYTES 18280u +#define STELA_METAL_REFLECTION_BYTES 18288u /* The target's facts, fixed for a device. nir is the Vulkan driver's * compiler options, which must outlive every shader compiled with them. */ diff --git a/crates/stela-drivers/src/gpu/compiler/tests/metal_compiler.c b/crates/stela-drivers/src/gpu/compiler/tests/metal_compiler.c index e2aa37cf8c..8f5ee4c809 100644 --- a/crates/stela-drivers/src/gpu/compiler/tests/metal_compiler.c +++ b/crates/stela-drivers/src/gpu/compiler/tests/metal_compiler.c @@ -165,6 +165,26 @@ static const char *fragment = "!40 = !{i32 0, !\"air.fragment_input\", !\"user(uv)\", !\"air.center\", !\"air.perspective\"," " !\"air.arg_type_name\", !\"float2\", !\"air.arg_name\", !\"uv\"}\n"; +/* Reads its barycentric coordinates and interpolates its uv at the + * centroid. */ +static const char *pulled = + "declare <2 x float> @air.interpolate_centroid_perspective.v2f32(ptr addrspace(1))\n" + "define <4 x float> @pulled(ptr addrspace(1) %uv, <3 x float> %bary) {\n" + "%c = call <2 x float> @air.interpolate_centroid_perspective.v2f32(ptr addrspace(1) %uv)\n" + "%x = extractelement <2 x float> %c, i64 0\n" + "%v = insertelement <3 x float> %bary, float %x, i64 0\n" + "%r = shufflevector <3 x float> %v, <3 x float> %v, <4 x i32> \n" + "ret <4 x float> %r\n}\n" + "!air.fragment = !{!2}\n" + "!2 = !{ptr @pulled, !3, !4}\n!3 = !{!30}\n" + "!30 = !{!\"air.render_target\", i32 0, i32 0, !\"air.arg_type_name\", !\"float4\"," + " !\"air.arg_name\", !\"color\"}\n" + "!4 = !{!40, !41}\n" + "!40 = !{i32 0, !\"air.fragment_input\", !\"user(uv)\", !\"air.interpolation_function\"," + " !\"air.perspective\", !\"air.arg_type_name\", !\"float2\", !\"air.arg_name\", !\"uv\"}\n" + "!41 = !{i32 1, !\"air.barycentric_coord\", !\"air.center\", !\"air.perspective\"," + " !\"air.arg_type_name\", !\"float3\", !\"air.arg_name\", !\"bary\"}\n"; + static const char *object = "declare void @air.set_threadgroups_per_grid_mesh_properties(ptr addrspace(3), <3 x i32>)\n" "define void @object(ptr addrspace(6) %payload, ptr addrspace(3) %grid) {\n" @@ -327,6 +347,14 @@ render_stages_compile_at_their_linked_locations(void) for (unsigned i = 0; i < 2; i++) stela_metal_release(shaders[i]); + assert(!reflection.barycentrics && !reflection.interpolants); + /* Interpolants link as varyings. */ + assert(reflect(pulled, "pulled", &reflection, &error)); + assert(reflection.barycentrics && reflection.interpolants && reflection.varying_count == 1); + nir_shader *pulling = compile(pulled, "pulled", &specialization, &error); + assert(pulling && varying(pulling, nir_var_shader_in, VARYING_SLOT_VAR0 + 1)); + stela_metal_release(pulling); + /* A varying the pipeline did not link is refused. */ const struct stela_metal_specialization unlinked = {.varyings = linked, .varying_count = 1}; assert(!compile(fragment, "fragment", &unlinked, &error) && error.kind == STELA_AIR_ARGUMENT); diff --git a/crates/stela-drivers/src/gpu/metal/compiler.rs b/crates/stela-drivers/src/gpu/metal/compiler.rs index 3a4b33ba85..716f2d9f76 100644 --- a/crates/stela-drivers/src/gpu/metal/compiler.rs +++ b/crates/stela-drivers/src/gpu/metal/compiler.rs @@ -61,7 +61,9 @@ const FEATURES: ShaderFeatures = ShaderFeatures::COMPUTE .union(ShaderFeatures::ARGUMENT_BUFFERS) .union(ShaderFeatures::LAYERED_RENDERING) .union(ShaderFeatures::VERTEX_AMPLIFICATION) - .union(ShaderFeatures::TESSELLATION); + .union(ShaderFeatures::TESSELLATION) + .union(ShaderFeatures::BARYCENTRICS) + .union(ShaderFeatures::PULL_MODEL_INTERPOLATION); /// LLVM instructions one compilation may lower, legalization included. Far /// beyond the functions Metal compiles, it bounds the work of hostile input. @@ -728,7 +730,12 @@ fn interface(reflection: &ffi::Reflection) -> Result { buffers.iter().any(|buffer| buffer.argument_buffer), ) .with(ShaderFeatures::VERTEX_AMPLIFICATION, r.amplification != 0) - .with(ShaderFeatures::TESSELLATION, patch.is_some()); + .with(ShaderFeatures::TESSELLATION, patch.is_some()) + .with(ShaderFeatures::BARYCENTRICS, r.barycentrics != 0) + .with( + ShaderFeatures::PULL_MODEL_INTERPOLATION, + r.interpolants != 0, + ); let interface = Interface { stage, patch, diff --git a/crates/stela-drivers/src/gpu/metal/compiler/ffi.rs b/crates/stela-drivers/src/gpu/metal/compiler/ffi.rs index 61d1bbb2d7..d0f8c0893c 100644 --- a/crates/stela-drivers/src/gpu/metal/compiler/ffi.rs +++ b/crates/stela-drivers/src/gpu/metal/compiler/ffi.rs @@ -149,6 +149,8 @@ pub struct Reflection { /// `enum stela_air_patch`. pub(super) patch: u32, pub(super) patch_control_points: u32, + pub(super) barycentrics: u32, + pub(super) interpolants: u32, pub(super) buffers: [BufferUse; MAX_BUFFER_BINDINGS as usize], pub(super) textures: [TextureUse; MAX_TEXTURE_BINDINGS as usize], pub(super) samplers: [u32; MAX_SAMPLER_BINDINGS as usize], diff --git a/crates/stela-drivers/src/gpu/metal/compiler/tests.rs b/crates/stela-drivers/src/gpu/metal/compiler/tests.rs index 628ebf4304..90e1b83ecd 100644 --- a/crates/stela-drivers/src/gpu/metal/compiler/tests.rs +++ b/crates/stela-drivers/src/gpu/metal/compiler/tests.rs @@ -401,6 +401,25 @@ fn reflections_become_interfaces_with_their_provenance() { ); } +#[test] +fn fragment_interpolation_is_a_feature_of_its_function() { + reflecting(|r| { + r.stage = 2; + r.barycentrics = 1; + r.interpolants = 1; + }); + let (mut compiler, library) = with_library(); + let interface = compiler + .reflect(library, "kernel", &Specialization::default()) + .unwrap(); + assert!( + interface + .features + .contains(ShaderFeatures::BARYCENTRICS.union(ShaderFeatures::PULL_MODEL_INTERPOLATION)) + ); + assert!(compiler.features().contains(interface.features)); +} + #[test] fn compilations_carry_their_specialization_and_release_through_the_service() { use ffi::primitive::{FLOAT, HALF}; @@ -433,6 +452,11 @@ fn compilations_carry_their_specialization_and_release_through_the_service() { .compile(library, "kernel", &specialization) .unwrap(); assert!(fragment.interface.discards); + assert!( + !fragment.interface.features.intersects( + ShaderFeatures::BARYCENTRICS.union(ShaderFeatures::PULL_MODEL_INTERPOLATION) + ) + ); assert_eq!( fragment .interface