diff --git a/crates/metal2vulkan/src/lower/simd/mod.rs b/crates/metal2vulkan/src/lower/simd/mod.rs index fe27275c8..258bf764c 100644 --- a/crates/metal2vulkan/src/lower/simd/mod.rs +++ b/crates/metal2vulkan/src/lower/simd/mod.rs @@ -303,9 +303,9 @@ pub struct SimdPlan { /// The fragment entry declares `RequireFullQuadsKHR`, so every quad starts with its four /// invocations and the helpers take part in the group operations, as Metal's do. pub full_quads: bool, - /// A fragment's `[[thread_index_in_quadgroup]]` on a profile without the fragment quad tier: - /// the stage interface's `FragCoord` object the lane is derived from ([`select_param`]). `None` - /// where the lane is the subgroup lane's low two bits. + /// A fragment's `[[thread_index_in_quadgroup]]` on a profile without fragment subgroups that + /// hold whole quads: the stage interface's `FragCoord` object the lane is derived from + /// ([`select_param`]). `None` where the lane is the subgroup lane's low two bits. pub quad_lane_coord: Option, /// A fragment's `[[thread_index_in_simdgroup]]` is the lane of the device's grouping /// ([`select_param`]): the function records `PrecisionNote::FragmentSimdgroupGrouping`, and @@ -710,9 +710,9 @@ pub(super) fn partly_live_at(cx: &EmitCx<'_>, at: InstId) -> bool { /// /// - `[[thread_index_in_quadgroup]]`. Vulkan's quad scope is four adjacent subgroup lanes from a /// multiple of 4, which in a fragment shader are the 2x2 pixels in Metal's order (top left, top -/// right, bottom left, bottom right; the quad operations' model), so where the profile has the -/// fragment quad tier the value is `SubgroupLocalInvocationId & 3`, and the class is raised with -/// the quad class. Where it has none (no fragment subgroups, none with quad operations, or one +/// right, bottom left, bottom right; the quad operations' model), so where the profile has +/// fragment subgroups that hold whole quads the value is `SubgroupLocalInvocationId & 3`, and the +/// class is raised with the basic class alone. Where it has none (no fragment subgroups, or ones /// narrower than a quad), the lane is the pixel's place in its aligned 2x2 block, `(x & 1) + 2 * /// (y & 1)` of the pixel `FragCoord` lies in, and raises no class. That is what Metal gives: our /// probe read the lane of every written pixel equal to it on the M3 Max, single-sampled, @@ -763,13 +763,19 @@ pub fn select_param(cx: &mut SelectCx<'_>, b: &crate::air::abi::ParamBinding) -> OpBits::BASIC } _ => { - let quad = SimdClass::CrossLane(OpBits::QUAD); - if subgroup::tier(&quad, Stage::Fragment, &cx.profile.fields().subgroup).is_err() { + // A quad is four adjacent subgroup lanes from a multiple of 4 wherever the stage has + // subgroups (Vulkan 9.27.9), so the lane needs subgroups holding whole quads, of the + // basic class alone. + let model = &cx.profile.fields().subgroup; + let lane = subgroup::tier(&SimdClass::LaneArithmetic, Stage::Fragment, model); + let whole = + lane != Ok(Tier::PartialFragment) || model.fragment.api_size.is_multiple_of(4); + if lane.is_err() || !whole { let coord = crate::lower::stage_io::frag_coord(cx); cx.families.simd.quad_lane_coord = Some(coord); return Ok(()); } - OpBits::QUAD + OpBits::BASIC } }; cx.simd = raise(cx.simd, ops); @@ -782,8 +788,8 @@ pub fn select_param(cx: &mut SelectCx<'_>, b: &crate::air::abi::ParamBinding) -> /// The value of a fragment lane parameter ([`select_param`]), a `u32`: the simdgroup width 32; /// the simdgroup lane, the subgroup's Metal lane; or the quad lane, the subgroup lane's low two -/// bits, or, without the fragment quad tier, `(x & 1) | ((y & 1) << 1)` of the pixel `FragCoord` -/// lies in. +/// bits, or, without fragment subgroups that hold whole quads, `(x & 1) | ((y & 1) << 1)` of the +/// pixel `FragCoord` lies in. pub fn param_value( cx: &mut EmitCx<'_>, fb: &mut FunctionBuilder<'_>, diff --git a/crates/metal2vulkan/src/tests/engine_simd.rs b/crates/metal2vulkan/src/tests/engine_simd.rs index 0cd81e0eb..e0dad837c 100644 --- a/crates/metal2vulkan/src/tests/engine_simd.rs +++ b/crates/metal2vulkan/src/tests/engine_simd.rs @@ -1153,11 +1153,12 @@ fn quad_ballots_and_simd_active_masks_run_in_every_subgroup_tier() { } /// A fragment's `[[thread_index_in_quadgroup]]` is its subgroup lane's low two bits where the -/// profile has the fragment quad tier (Vulkan's quad scope: four adjacent lanes from a multiple of -/// 4, in a fragment shader the 2x2 pixels in Metal's order), and the pixel's place in its aligned -/// 2x2 block, read from `FragCoord`, where it has none. On every committed profile the function -/// compiles and passes L3: through `SubgroupLocalInvocationId` exactly where a fragment quad -/// operation's tier exists, and elsewhere through `FragCoord` with no subgroup capability. +/// profile has fragment subgroups that hold whole quads (Vulkan's quad scope: four adjacent lanes +/// from a multiple of 4, in a fragment shader the 2x2 pixels in Metal's order), and the pixel's +/// place in its aligned 2x2 block, read from `FragCoord`, where it has none. On every committed +/// profile the function compiles and passes L3: through `SubgroupLocalInvocationId` exactly where +/// the fragment stage's lane has a tier, and elsewhere through `FragCoord` with no subgroup +/// capability. #[test] fn a_fragment_quad_lane_is_its_subgroup_lane_or_its_place_in_the_pixel_quad() { let Some(bc) = assemble("simd_masks") else { @@ -1166,7 +1167,7 @@ fn a_fragment_quad_lane_is_its_subgroup_lane_or_its_place_in_the_pixel_quad() { let (mut by_lane, mut by_position) = (0, 0); for (stem, p) in committed_profiles().expect("profiles") { let quad = subgroup::tier( - &SimdClass::CrossLane(OpBits::QUAD), + &SimdClass::LaneArithmetic, Stage::Fragment, &p.fields().subgroup, ); @@ -1197,6 +1198,39 @@ fn a_fragment_quad_lane_is_its_subgroup_lane_or_its_place_in_the_pixel_quad() { assert!(by_lane > 0 && by_position > 0, "{by_lane} {by_position}"); } +/// A fragment's `[[thread_index_in_quadgroup]]` asks for fragment subgroups and not the quad +/// class: Vulkan's quad scope is four adjacent subgroup lanes from a multiple of 4 wherever the +/// stage has subgroups (9.27.9), whatever operation classes it lists. On MoltenVK's description +/// without the quad class, `quad_lane_f` reads `SubgroupLocalInvocationId` and declares no quad +/// capability; with the fragment stage cleared from `subgroupSupportedStages`, it reads the pixel's +/// place in its 2x2 block from `FragCoord` and declares no subgroup capability. +#[test] +fn a_fragment_quad_lane_needs_fragment_subgroups_and_no_quad_class() { + let Some(bc) = assemble("simd_masks") else { + return; + }; + let text = |p: &DeviceProfile| { + let a = compile(&bc, "quad_lane_f", p).expect("compile"); + let (_, words) = link_and_validate(&a, p).expect("valid"); + spirv_dis(&words) + }; + let lane = text(&moltenvk_without_quads()); + assert!(lane.contains("SubgroupLocalInvocationId"), "the lane"); + assert!(!lane.contains("GroupNonUniformQuad"), "no quad class"); + let mut desc = crate::pipeline::testing::description("moltenvk-m3").expect("description"); + desc.subgroup.supported_stages = crate::vk::ShaderStageFlags::COMPUTE; + let p = Device::from_queries(desc, DevicePolicy::RESIDENT_TABLES) + .expect("device") + .profile() + .clone(); + let parity = text(&p); + assert!(parity.contains("OpShiftLeftLogical"), "the row's bit"); + assert!( + !parity.contains("SubgroupLocalInvocationId") && !parity.contains("GroupNonUniform"), + "no subgroup" + ); +} + /// The owned fixture `gsm2_fragment_lanes`. A fragment's `[[thread_index_in_simdgroup]]`, read as a /// `uint` or `ushort` parameter or as a Metal 3.1 program-scope `uint`, and beside the shuffles and /// ballot of `fragment_lane_shuffles`, is the lane of its tier's 32-lane simdgroup, over every