From ebfd1b311dbf093ba569822b73d91fb402cf90bd Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 14 Nov 2026 17:15:37 -0800 Subject: [PATCH] SEMANTICS 2119: A merged draw replay's vertex function draws regions several at a time A rate-mapped pass draws its draws region by region through a multi-draw, each region's viewport at the draw's index (VariantKey::region_viewports). A merged draw replay's vertex function now takes that variant too. Its corner is its VertexIndex and InstanceIndex's, which each draw of a multi-draw gives from its own first vertex. Its viewport is its DrawIndex's, which the merged read does not touch. Only a replay of one draw per command (replay_bindings without merged_draws) is still NotImplemented(RasterizationRateMap). A host step that draws a GPU-decoded merged replay's commands region by region needs this, as it needs the region copies (SEMANTICS 2118). tests::engine_region_fan_out::a_merged_draw_s_variant_fans_out now also compiles the merged region-viewports variant on lavapipe, the Iris Xe and the Pascal. It checks L3 on every module, both facts stated, the viewport index stored, and the plan settling on itself. --- crates/metal2vulkan/src/cache/mod.rs | 2 +- .../metal2vulkan/src/lower/stage_io/plan.rs | 15 ++++--- .../metal2vulkan/src/pipeline/lower_inputs.rs | 10 +++-- .../src/tests/engine_region_fan_out.rs | 45 ++++++++++++++++++- 4 files changed, 58 insertions(+), 14 deletions(-) diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 21577c321..06bec835a 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -61,7 +61,7 @@ mod tests; /// unchanged version, and its bless records the version. Independent of the crate's version and of /// [`ARTIFACT_FORMAT_VERSION`]. The commit that bumps it says what changed; this comment keeps no /// history. -pub const SEMANTICS_VERSION: u32 = 2118; +pub const SEMANTICS_VERSION: u32 = 2119; /// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the /// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another diff --git a/crates/metal2vulkan/src/lower/stage_io/plan.rs b/crates/metal2vulkan/src/lower/stage_io/plan.rs index 05e7fe6af..395584560 100644 --- a/crates/metal2vulkan/src/lower/stage_io/plan.rs +++ b/crates/metal2vulkan/src/lower/stage_io/plan.rs @@ -1240,11 +1240,13 @@ fn amplified_output(cx: &mut SelectCx<'_>, index: AmplifiedIndex) -> Result<(), /// the draw" of a `vkCmdDrawMultiEXT` or `vkCmdDrawMultiIndexedEXT` (the specification's /// `DrawIndex`), and so its region's among the command's ([`super::RegionViewport`]). `DrawIndex` /// is dynamically uniform, so every vertex of a primitive stores the same index, as Vulkan's -/// `ViewportIndex` asks. A function that writes its own viewport or slice index, and one under -/// another field that moves its indices or position (`amplification_draws`, -/// `viewport_compensation`), reads its draw from a replay (`replay_bindings`, `merged_draws`) or -/// runs behind the tessellator, is `NotImplemented(RasterizationRateMap)`: the runtime draws -/// those region by region. A device that cannot store `ViewportIndex` from a vertex function, +/// `ViewportIndex` asks. A merged draw's vertex function (`merged_draws`) takes it too: its corner +/// is its `VertexIndex` and `InstanceIndex`'s, which a multi-draw's draws each give from their own +/// first vertex. A function that writes its own viewport or slice index, and one under another +/// field that moves its indices or position (`amplification_draws`, `viewport_compensation`), +/// reads its draw from a replay of one draw per command (`replay_bindings` without +/// `merged_draws`) or runs behind the tessellator, is `NotImplemented(RasterizationRateMap)`: the +/// runtime draws those region by region. A device that cannot store `ViewportIndex` from a vertex function, /// read `DrawIndex` or take several viewports refuses it, naming the three features. A function /// without outputs rasterizes nothing and stores no index. fn region_viewport( @@ -1264,8 +1266,7 @@ fn region_viewport( || tessellated || i.amplification_draws.is_some() || i.viewport_compensation.is_some() - || i.replay_bindings - || i.merged_draws + || (i.replay_bindings && !i.merged_draws) { return Err(Unsupported::not_implemented(Feature::RasterizationRateMap).into()); } diff --git a/crates/metal2vulkan/src/pipeline/lower_inputs.rs b/crates/metal2vulkan/src/pipeline/lower_inputs.rs index 8be1453d7..20fb04ee6 100644 --- a/crates/metal2vulkan/src/pipeline/lower_inputs.rs +++ b/crates/metal2vulkan/src/pipeline/lower_inputs.rs @@ -875,11 +875,13 @@ pub struct LoweringInputs { /// index is the same at every vertex of a draw, so of a primitive. It needs the device to /// store `ViewportIndex` from a vertex function (`shaderOutputViewportIndex`), to read /// `DrawIndex` (`shaderDrawParameters`) and to take several viewports (`multiViewport`): - /// `Unsupported(RasterizationRateMap)` names them otherwise. A function that writes its own `[[viewport_array_index]]` or + /// `Unsupported(RasterizationRateMap)` names them otherwise. A merged draw's vertex function + /// (`merged_draws`) takes it too, its corner from its `VertexIndex` and `InstanceIndex` as + /// without it. A function that writes its own `[[viewport_array_index]]` or /// `[[render_target_array_index]]`, one compiled under `amplification_draws`, - /// `viewport_compensation`, `replay_bindings` or `merged_draws`, and a post-tessellation - /// vertex function, is `NotImplemented(RasterizationRateMap)` under it: the runtime draws - /// those region by region. The vertex function states `RuntimeFact::RegionViewports`. A + /// `viewport_compensation`, or `replay_bindings` without `merged_draws`, and a + /// post-tessellation vertex function, is `NotImplemented(RasterizationRateMap)` under it: the + /// runtime draws those region by region. The vertex function states `RuntimeFact::RegionViewports`. A /// fragment function compiled under it (behind such a vertex function, where it reads /// `[[viewport_array_index]]`) reads 0 there, the index Metal's reads behind a vertex /// function that writes none. Any other stage compiles as without it. diff --git a/crates/metal2vulkan/src/tests/engine_region_fan_out.rs b/crates/metal2vulkan/src/tests/engine_region_fan_out.rs index d644bf34a..39afea4ba 100644 --- a/crates/metal2vulkan/src/tests/engine_region_fan_out.rs +++ b/crates/metal2vulkan/src/tests/engine_region_fan_out.rs @@ -14,7 +14,9 @@ //! in every mode as a draw's does: its modules pass L3, its vertex function pushes the header's //! address, and its plan settles on itself; its region-copy variant draws no geometry stage, its //! vertex function storing its region's viewport index and pushing its first region beside the -//! header's address. +//! header's address. So does its variant that draws a pass's regions several at a time +//! (`VariantKey::region_viewports`), its vertex function storing the draw's index as its +//! viewport's. //! - Drawn amplified, the plan keeps the plain variant, and draws the regions one at a time. //! //! `llvm-as` assembles the fixtures; a missing tool skips visibly. No GPU is used. @@ -195,7 +197,8 @@ fn a_fanned_out_draw_runs_its_variant() { /// A merged draw replay's variant fans out in every mode: its modules pass L3, its graphics state /// states the fan-out, its vertex function pushes the header's address (and a region-copy one its -/// first region too, storing its region's viewport index), and its plan settles on itself. +/// first region too, storing its region's viewport index), and its plan settles on itself; and so +/// does its variant that draws regions several at a time. #[test] fn a_merged_draw_s_variant_fans_out() { use crate::pipeline::sym::PushField; @@ -279,6 +282,44 @@ fn a_merged_draw_s_variant_fans_out() { assert_eq!(vertex[0], plain[0] + 2 * 8, "{stem}"); assert_eq!(vertex[2], plain[2] + 1, "{stem}"); } + // Its regions several at a time, where the pass's draws are drawn region by region. + let state = BindState { + region_viewports: true, + ..Default::default() + }; + let plan = base + .plan(&dev, &state.shape()) + .unwrap_or_else(|e| panic!("{stem}: {e:?}")); + assert!(plan.variant.region_viewports, "{stem}"); + let mut key = plan.variant.clone(); + key.replay_bindings = true; + key.merged_draws = true; + let p = dev + .compile_render_variant(&d, &key, &budget) + .unwrap_or_else(|e| panic!("{stem} region viewports: {e}")); + for m in &p.stages { + check_words_valid(&m.spirv, &p.requirements, dev.profile()) + .unwrap_or_else(|e| panic!("{stem} region viewports {:?}: L3: {e}", m.stage)); + } + for fact in [RuntimeFact::RegionViewports, RuntimeFact::MergedDraws] { + assert!( + (p.dependencies.iter()).any(|x| x.fact == fact), + "{stem}: {fact:?}" + ); + } + let vertex = (p.stages.iter()) + .find(|m| m.stage == vk::ShaderStage::VERTEX) + .map(|m| decorated(&m.spirv)) + .expect("a vertex module"); + assert_eq!( + vertex[2], + plain[2] + 1, + "{stem}: the draw's index as its viewport's" + ); + let settled = p + .plan(&dev, &state.shape()) + .unwrap_or_else(|e| panic!("{stem} region viewports: {e:?}")); + assert_eq!(settled.variant, key, "{stem} region viewports"); } }