From a010d17c6b785fe4ac8a09ce20993c66aacd7050 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 19 Nov 2026 16:31:48 -0800 Subject: [PATCH] translator: a fetch's element index first among the floor's fragment fields, from the vertex function's end On the floor a fragment function's push fields started at the vertex function's push end rounded up to 8. Where that end is a multiple of 4 and not of 8, the four bytes below the base went unused, and a fetching fragment function placed its four-byte element index (PushField::FetchedColor) after its eight-byte fields: a pair that fits the floor's 256 bytes with those four bytes used was pushed past them. The fragment base is now the vertex function's push end, and FetchedColor sorts after SlotBindOffset and before the slot addresses (SlotGuestBase): a fragment function that pushes no parameter block's address places it in those four bytes. Each field is still placed at the next offset aligned to it, so a fragment function whose first field is eight bytes, the spilled block's address included, starts where it did, and the vertex function's window still ends at 248. The fixed windows read no base: the old runtime's modules are unchanged. The docs that stated the rounding say the base is the vertex function's end: the push module, VERTEX_FLOOR_END, LoweringInputs::push_base, docs/API.md section 5, rt's README, and the render identity test's notes; the spill test reads the base unrounded (its vertex function ends at 248). --- crates/metal2vulkan-rt/README.md | 19 +++++++-------- crates/metal2vulkan-rt/src/render.rs | 2 +- crates/metal2vulkan/src/library/compiled.rs | 11 ++++----- crates/metal2vulkan/src/lower/push.rs | 23 +++++++++++-------- .../metal2vulkan/src/pipeline/lower_inputs.rs | 2 +- crates/metal2vulkan/src/pipeline/sym.rs | 23 +++++++++++-------- .../src/tests/engine_identity_render.rs | 2 +- .../src/tests/engine_identity_spill.rs | 5 +--- docs/API.md | 2 +- 9 files changed, 46 insertions(+), 43 deletions(-) diff --git a/crates/metal2vulkan-rt/README.md b/crates/metal2vulkan-rt/README.md index f57e2d2c5..c104ab014 100644 --- a/crates/metal2vulkan-rt/README.md +++ b/crates/metal2vulkan-rt/README.md @@ -2265,15 +2265,16 @@ or does not check is rebuilt from the generations' names, with young counted ful differ only in a field their compile does not read are two contents, each compiled. - A render pipeline's two functions push their values (each buffer's address, each constexpr sampler's index) in one range of the floor's 256 bytes, dense by link: the vertex function's - from 0, ending by 248, the fragment function's from the vertex function's end rounded up to 8, - so only the values the pair uses take space. Compute and render pipelines share the device's - one pipeline layout and its one push range. A function whose values do not fit its share - pushes the address of a parameter block first and reads the rest from the block, as a kernel - does: a draw writes each such block into the parameter ring where it fills its pushed values - anew (the state, a binding or a pushed value changed since the encoder's last draw), and - `ring_writes` counts it. The values only a checked access - reads (a checked slot's length and the zero range's address) come last, so they are the first - to move to the block. + from 0, ending by 248, the fragment function's from the vertex function's end, each value at + the next offset aligned to it (a framebuffer fetch's element index, four bytes, before the + eight-byte values, where the four bytes below one are free), so only the values the pair uses + take space. Compute and render pipelines share the device's one pipeline layout and its one + push range. A function whose values do not fit its share pushes the address of a parameter + block first and reads the rest from the block, as a kernel does: a draw writes each such block + into the parameter ring where it fills its pushed values anew (the state, a binding or a pushed + value changed since the encoder's last draw), and `ring_writes` counts it. The values only a + checked access reads (a checked slot's length and the zero range's address) come last, so they + are the first to move to the block. - A `VkPipeline` bakes what Vulkan 1.3 cannot leave dynamic: the content and the topology class (line or triangle) of the draws it serves. It takes as dynamic state the viewport, scissor, cull mode, front face, primitive topology within its class, primitive restart, depth test, write and diff --git a/crates/metal2vulkan-rt/src/render.rs b/crates/metal2vulkan-rt/src/render.rs index 62c8d53bf..d266ea84e 100644 --- a/crates/metal2vulkan-rt/src/render.rs +++ b/crates/metal2vulkan-rt/src/render.rs @@ -734,7 +734,7 @@ impl RenderCompiled { lowered.vertex = StageSource::Compiled(function.map_err(Error::Translation)?); } // The fragment function's push fields follow the vertex function's in the floor's one - // range, from the vertex function's end rounded up to 8. + // range, from the vertex function's end. let base = match &lowered.vertex { StageSource::Compiled(vertex) => vertex.fragment_push_base(), StageSource::Function(_) => 0, diff --git a/crates/metal2vulkan/src/library/compiled.rs b/crates/metal2vulkan/src/library/compiled.rs index fde1e7d81..5668ea0e2 100644 --- a/crates/metal2vulkan/src/library/compiled.rs +++ b/crates/metal2vulkan/src/library/compiled.rs @@ -338,13 +338,12 @@ impl CompiledFunction { } /// Where a fragment function linked after this vertex function pushes its fields on - /// `Device::from_queries`' profile (`LoweringInputs::push_base`): this function's push end - /// rounded up to 8, 0 where it pushes nothing. A consumer compiling a render pipeline's - /// fragment function on its own compiles it under this offset. + /// `Device::from_queries`' profile (`LoweringInputs::push_base`): this function's push end, a + /// multiple of 4, 0 where it pushes nothing; each fragment field is placed at the next offset + /// aligned to it, so a four-byte field may take the bytes below an eight-byte one. A consumer + /// compiling a render pipeline's fragment function on its own compiles it under this offset. pub fn fragment_push_base(&self) -> u32 { - (self.0.interface.push.push_range) - .map_or(0, |(offset, size)| offset + size) - .next_multiple_of(8) + (self.0.interface.push.push_range).map_or(0, |(offset, size)| offset + size) } /// Whether the function is a vertex function whose module writes its `[[point_size]]` diff --git a/crates/metal2vulkan/src/lower/push.rs b/crates/metal2vulkan/src/lower/push.rs index 5a51433fa..70cb7c8ca 100644 --- a/crates/metal2vulkan/src/lower/push.rs +++ b/crates/metal2vulkan/src/lower/push.rs @@ -7,13 +7,15 @@ //! compute window is `[0, 256)` (a dispatch region and 28 buffer addresses), and a render //! pipeline's two stages share one range `[0, 256)`, dense by link: the vertex function's fields //! from 0, the fragment function's from its `LoweringInputs::push_base`, the vertex function's push -//! end rounded up to 8, so only the fields the pair places take space. The vertex function's -//! window there is `[0, 248)`, 8 bytes short of the range's end ([`VERTEX_FLOOR_END`]): a vertex -//! function is lowered without knowing its fragment function, and a fragment function whose -//! fields spill must push its parameter block's address (8 bytes, 8-aligned) from the vertex -//! function's end, so the vertex function leaves that room. The reserved 8 bytes cost only a vertex -//! function whose own fields pass 248, which spills where it would have filled them. -//! `select` places the function's own [`PushField`]s in the fixed kind order (the derived `Ord` of [`PushField`]; within a kind, by slot). Fields fill the +//! end, so only the fields the pair places take space (a four-byte field the fragment function +//! places before its eight-byte ones takes the four bytes the vertex function's end may leave). +//! The vertex function's window there is `[0, 248)`, 8 bytes short of the range's end +//! ([`VERTEX_FLOOR_END`]): a vertex function is lowered without knowing its fragment function, and +//! a fragment function whose fields spill must push its parameter block's address (8 bytes, +//! 8-aligned) from the vertex function's end, so the vertex function leaves that room. The +//! reserved 8 bytes cost only a vertex function whose own fields pass 248, which spills where it +//! would have filled them. `select` places the function's own [`PushField`]s in the fixed kind +//! order (the derived `Ord` of [`PushField`]; within a kind, by slot). Fields fill the //! stage window in that order, each at the next offset aligned to the field; from the first field //! that does not fit, every field goes to the stage's `ArgumentTable` uniform buffer //! (`ResourceSlot{stage, Synthetic(ArgumentTable)}`), at offsets fixed by the same order. The @@ -60,8 +62,9 @@ pub const fn window(stage: Stage, w: PushWindow, base: u32) -> (u32, u32) { pub const FLOOR_BYTES: u32 = 256; /// The end of a vertex function's window on the floor: the floor's end less one address, so the -/// fragment function's window, from the vertex function's end rounded up to 8, always holds its -/// parameter block's address (`PushField::ArgumentTableAddress`, which the order places first). +/// fragment function's window, from the vertex function's end, always holds its parameter block's +/// address (`PushField::ArgumentTableAddress`, which the order places first, at the next multiple +/// of 8). pub const VERTEX_FLOOR_END: u32 = FLOOR_BYTES - 8; /// The SPIR-V shape of a push field's value. @@ -495,7 +498,7 @@ mod tests { } /// On the floor a render pipeline's fields are dense by link: a fragment function placed from - /// the vertex function's push end rounded up to 8 starts there, its fields fill the rest of + /// the vertex function's push end starts there, its fields fill the rest of /// the 256 bytes, and from the first that does not fit it spills. A vertex function's window /// ends at 248, so a fragment function from its end can always push its block's address. #[test] diff --git a/crates/metal2vulkan/src/pipeline/lower_inputs.rs b/crates/metal2vulkan/src/pipeline/lower_inputs.rs index 4cde9e6cb..04b727725 100644 --- a/crates/metal2vulkan/src/pipeline/lower_inputs.rs +++ b/crates/metal2vulkan/src/pipeline/lower_inputs.rs @@ -950,7 +950,7 @@ pub struct LoweringInputs { /// Where a fragment function's push fields start on `Device::from_queries`' profile /// (`PushWindow::Floor`), whose render pipeline pushes one range `[0, 256)` for both stages: /// its vertex function's fields first, from 0, then the fragment function's from this offset, - /// the vertex function's push end rounded up to 8 + /// the vertex function's push end /// ([`CompiledFunction::fragment_push_base`](crate::library::CompiledFunction::fragment_push_base)). /// `Device::compile_render_pipeline` sets it for a `Function` fragment stage from the vertex /// function it compiled; a fragment compiled on its own for such a pipeline compiles under the diff --git a/crates/metal2vulkan/src/pipeline/sym.rs b/crates/metal2vulkan/src/pipeline/sym.rs index 815fcb19e..2c1389515 100644 --- a/crates/metal2vulkan/src/pipeline/sym.rs +++ b/crates/metal2vulkan/src/pipeline/sym.rs @@ -1096,6 +1096,19 @@ pub enum PushField { /// Residual bind offset of a descriptor-form slot: the bind offset minus the descriptor base, /// modulo 2^32. SlotBindOffset(SlotRef), + /// Under the identity domains, on a device that orders framebuffer fetch in neither form + /// (`lower::fragment::fetch::fetch_unordered`), the element of the set-1 sampled-texture array + /// through which a fragment function reads color attachment `color` (`[[color(n)]]`): one + /// `u32`, the index of an element holding a 2D view of the attachment's level and slice + /// (multisampled in a multisampled pipeline), which the function fetches as `texels` at the + /// integer part of its `FragCoord` (and its `SampleId`). No descriptor holds the attachment. + /// The function states `RuntimeFact::FramebufferFetch` for the color, and its fetches are + /// ordered after earlier draws only by the consumer's barriers, not after an overlapping + /// earlier fragment of the same draw (`PrecisionNote::UnorderedFetchWithinDraw`). Ordered + /// before the slot addresses, so on the floor (`PushWindow::Floor`) a fragment function that + /// pushes no parameter block's address places it in the four bytes a vertex function's push + /// end may leave below the fragment function's first eight-byte field. + FetchedColor { color: u32, texels: SampledType }, /// Guest base address of a static-root slot. Under the identity domains, a `u64`: a buffer /// slot's device address (its `gpuAddress` plus the bind offset) less 2^31, modulo 2^64 /// (`docs/API.md`, section 5), and a texture or sampler slot's `gpuResourceID` @@ -1261,16 +1274,6 @@ pub enum PushField { /// a device sampler of its own. The use's coordinates are normalized: the function scales a /// pixel sampler's coordinates itself. ConstexprSampler(CombinedSampler), - /// Under the identity domains, on a device that orders framebuffer fetch in neither form - /// (`lower::fragment::fetch::fetch_unordered`), the element of the set-1 sampled-texture array - /// through which a fragment function reads color attachment `color` (`[[color(n)]]`): one - /// `u32`, the index of an element holding a 2D view of the attachment's level and slice - /// (multisampled in a multisampled pipeline), which the function fetches as `texels` at the - /// integer part of its `FragCoord` (and its `SampleId`). No descriptor holds the attachment. - /// The function states `RuntimeFact::FramebufferFetch` for the color, and its fetches are - /// ordered after earlier draws only by the consumer's barriers, not after an overlapping - /// earlier fragment of the same draw (`PrecisionNote::UnorderedFetchWithinDraw`). - FetchedColor { color: u32, texels: SampledType }, /// The value a draw or dispatch binds for specialization key `key`, carried as data rather /// than specialized: one `u32` holding exactly the value the binder computes for the key's /// specialization constant from the bound textures and samplers (a boolean key as 1 or 0; a diff --git a/crates/metal2vulkan/src/tests/engine_identity_render.rs b/crates/metal2vulkan/src/tests/engine_identity_render.rs index 4f633054b..d7719f40b 100644 --- a/crates/metal2vulkan/src/tests/engine_identity_render.rs +++ b/crates/metal2vulkan/src/tests/engine_identity_render.rs @@ -11,7 +11,7 @@ //! (not implemented), as before: its push window is not the floor's. //! - On `Device::from_queries`' profile the two stages push one range, dense by link: with //! `sampled_fragment`, which pushes its constexpr sampler's index, the fragment function's -//! fields start at the vertex function's end rounded up to 8 (32), where the fixed windows put +//! fields start at the vertex function's end (32), where the fixed windows put //! them at 64. A fragment compiled on its own at base 0 and linked after that vertex function is //! `InvalidDescriptor(PushBase)`; compiled under the vertex function's //! `CompiledFunction::fragment_push_base` it links. diff --git a/crates/metal2vulkan/src/tests/engine_identity_spill.rs b/crates/metal2vulkan/src/tests/engine_identity_spill.rs index a639c1404..6a397b117 100644 --- a/crates/metal2vulkan/src/tests/engine_identity_spill.rs +++ b/crates/metal2vulkan/src/tests/engine_identity_spill.rs @@ -329,10 +329,7 @@ fn a_render_pair_past_the_floor_spills_both_functions() { assert_spills(&format!("{stem} vertex"), vertex, 0, true); // The vertex function fills its window to 248: the fragment function's block address // takes the range's last 8 bytes, and its slots' addresses go to its block too. - let base = vertex - .push_range - .map_or(0, |(o, s)| o + s) - .next_multiple_of(8); + let base = vertex.push_range.map_or(0, |(o, s)| o + s); assert_eq!(base, 248, "{stem}"); assert_spills(&format!("{stem} fragment"), fragment, base, false); let end = fragment.push_range.map_or(0, |(o, s)| o + s); diff --git a/docs/API.md b/docs/API.md index 0873208cc..11a20af0d 100644 --- a/docs/API.md +++ b/docs/API.md @@ -134,7 +134,7 @@ compile does. `DevicePolicy::default()` is `DevicePolicy::UNTRUSTED`: (256 on a device made by `Device::from_queries`, whose runtime's floor requires that much `maxPushConstantsSize`: a dispatch region and 28 buffer addresses). On such a device a render pipeline's two functions share one 256-byte range: the vertex function's window is `[0, 248)`, - and the fragment function's runs from the vertex function's end, rounded up to 8, to 256. A + and the fragment function's runs from the vertex function's end to 256. A vertex function is lowered without knowing its fragment function, so it leaves the last 8 bytes, where a fragment function whose fields spill always pushes its block's address; that costs only a vertex function whose own fields pass 248 bytes, which spills where it would have