From d12e67ceab3aa2c5ab06e7b61cd2ef044a145fdd Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Fri, 30 Oct 2026 00:58:15 +0000 Subject: [PATCH] feat(compiler): refuse programmable blending by name as an Apple GPU family feature --- .../stela-drivers/src/gpu/compiler/air_metadata.c | 6 ++++++ .../src/gpu/compiler/tests/metal_compiler.c | 14 ++++++++++++++ .../src/gpu/metal/fixtures/corpus/render.metal | 11 ++++++++--- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/crates/stela-drivers/src/gpu/compiler/air_metadata.c b/crates/stela-drivers/src/gpu/compiler/air_metadata.c index aab212cd16..13bf23a794 100644 --- a/crates/stela-drivers/src/gpu/compiler/air_metadata.c +++ b/crates/stela-drivers/src/gpu/compiler/air_metadata.c @@ -1615,6 +1615,12 @@ metadata_arguments(struct stela_air_metadata *t, LLVMValueRef fn, LLVMValueRef a bool payload = (object || mesh_stage) && stela_air_md_string(fields[1], "air.payload"); bool mesh = mesh_stage && stela_air_md_string(fields[1], "air.mesh"); bool grid = object && stela_air_md_string(fields[1], "air.mesh_grid_properties"); + /* A fragment input of a render target's color is programmable + * blending, which reads the target in raster order. */ + if (render && render->entry.stage == STELA_AIR_FRAGMENT && + stela_air_md_string(fields[1], "air.render_target")) + return fail(t, STELA_AIR_ARGUMENT, + "programmable blending ([[color(n)]] inputs) needs an Apple GPU family"); if (!buffer && !builtin && !interpolated && !vertex_input && !texture && !sampler && !payload && !mesh && !grid && !control_points && !in_patch) { unsigned length; 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 acd555b4ad..75df906878 100644 --- a/crates/stela-drivers/src/gpu/compiler/tests/metal_compiler.c +++ b/crates/stela-drivers/src/gpu/compiler/tests/metal_compiler.c @@ -457,6 +457,17 @@ 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"; +/* Returns the color its render target holds: programmable blending. */ +static const char *blended = + "define <4 x float> @blended(<4 x float> %previous) {\nret <4 x float> %previous\n}\n" + "!air.fragment = !{!2}\n" + "!2 = !{ptr @blended, !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}\n" + "!40 = !{i32 0, !\"air.render_target\", i32 0, !\"air.arg_type_name\", !\"float4\"," + " !\"air.arg_name\", !\"previous\"}\n"; + /* Reads its barycentric coordinates and interpolates its uv at the * centroid. */ static const char *pulled = @@ -662,6 +673,9 @@ render_stages_compile_at_their_linked_locations(void) assert(!compile(fragment, "fragment", &unlinked, &error) && error.kind == STELA_AIR_ARGUMENT); /* As are entries no AIR list names. */ assert(!reflect(vertex, "missing", &reflection, &error) && error.kind == STELA_AIR_ARGUMENT); + /* Only Apple GPU families blend programmably. */ + assert(!reflect(blended, "blended", &reflection, &error) && error.kind == STELA_AIR_ARGUMENT && + strstr(error.detail, "programmable blending")); } static void diff --git a/crates/stela-drivers/src/gpu/metal/fixtures/corpus/render.metal b/crates/stela-drivers/src/gpu/metal/fixtures/corpus/render.metal index 8559d0c470..4467b07eea 100644 --- a/crates/stela-drivers/src/gpu/metal/fixtures/corpus/render.metal +++ b/crates/stela-drivers/src/gpu/metal/fixtures/corpus/render.metal @@ -75,13 +75,12 @@ struct FragmentOut { fragment FragmentOut fragment_main(FragmentIn in [[stage_in]], texture2d tex [[texture(0)]], sampler s [[sampler(0)]], bool front [[front_facing]], uint sample [[sample_id]], float2 point [[point_coord]], - uint primitive [[primitive_id]], uint coverage [[sample_mask]], - float4 previous [[color(0)]]) + uint primitive [[primitive_id]], uint coverage [[sample_mask]]) { FragmentOut out; float4 c = tex.sample(s, in.uv) * in.color; c += float4(dfdx(in.uv), dfdy(in.uv)) + float4(fwidth(in.sample_value)); - c += float4(front, sample, primitive, in.flat_id) + float4(point, in.position.xy) + previous; + c += float4(front, sample, primitive, in.flat_id) + float4(point, in.position.xy); if (c.a < 0.01f) discard_fragment(); out.color0 = c; @@ -92,6 +91,12 @@ fragment FragmentOut fragment_main(FragmentIn in [[stage_in]], texture2d return out; } +// Programmable blending, which only Apple GPU families have: refused. +fragment float4 fragment_blended(FragmentIn in [[stage_in]], float4 previous [[color(0)]]) +{ + return previous * in.color; +} + fragment float4 fragment_interpolated(FragmentIn in [[stage_in]], float3 bary [[barycentric_coord]]) {