diff --git a/crates/metal2vulkan-runtime/src/argument_table.rs b/crates/metal2vulkan-runtime/src/argument_table.rs index 8a1121ebf..19e4aef48 100644 --- a/crates/metal2vulkan-runtime/src/argument_table.rs +++ b/crates/metal2vulkan-runtime/src/argument_table.rs @@ -25,13 +25,12 @@ //! core, whichever slot binds it: a bind before each draw, and a draw that binds objects an //! earlier draw of the encoder bound, retain nothing of their own. //! -//! A compute encoder's table differs in two answers. As plain Metal's compute encoder gave it on an -//! M4 where its API validation asserts, it holds a buffer bound past its length, which a dispatch +//! A compute encoder's table differs in one answer, as plain Metal's compute encoder gave it on an +//! M4 where its API validation asserts: it holds a buffer bound past its length, which a dispatch //! refuses only where the kernel reads the slot (a read there is past the buffer: memory decides -//! it); a render stage's table refuses that at the call, as Metal's validation does, and plain -//! Metal's render form is not measured. And its stride forms hold `MTLAttributeStrideStatic` as -//! the stride its low 32 bits name, `2^32 - 1`, where a render stage's table holds no stride for -//! it ([`per_bind_stride`]). +//! it). A render stage's table refuses that at the call, as Metal's validation does; plain Metal's +//! render form is not measured. Both tables' stride forms hold a stride as its low 32 bits, +//! `MTLAttributeStrideStatic`'s included ([`per_bind_stride`]). //! //! An offset set at an index with no buffer bound is no change in either, as plain Metal's render //! and compute encoders both take it ([`ArgumentTable::take_buffer_offset`]). @@ -70,10 +69,6 @@ pub(super) const BUFFER_SLOTS: u32 = 31; pub(super) const TEXTURE_SLOTS: u32 = 128; /// Entries of a sampler table (`setSamplerState:atIndex:` and its stage forms). pub(super) const SAMPLER_SLOTS: u32 = 16; -/// `MTLAttributeStrideStatic`: the stride argument of the stride forms that a render stage's -/// table holds as no per-bind stride, as the plain setters bind, and a compute table as the -/// stride `2^32 - 1` ([`per_bind_stride`]). -pub(super) const ATTRIBUTE_STRIDE_STATIC: u64 = u64::MAX; /// The most bytes one `setBytes:length:atIndex:` (or a stage form) takes: Metal's documented /// limit for inline bytes, 4 KiB. pub(super) const INLINE_BYTES_LIMIT: u64 = 4096; @@ -453,23 +448,24 @@ pub(super) fn check_bound_buffer( Ok(()) } -/// The per-bind stride a stride form's `stride` names in a compute table (`compute`) or a render -/// stage's: its low 32 bits. Plain Metal reads a stride past 32 bits so, and its validation takes -/// it (an M4 under macOS 27.0.1 and an M3 Max under 26.7, compute stage-in and vertex fetch, both -/// binding forms: `2^32 + 8` and `2^63 + 8` read at 8, `2^33 + 4` at 4, `2^32` at 0). +/// The per-bind stride a stride form's `stride` names in a compute table or a render stage's: its +/// low 32 bits. Plain Metal reads a stride past 32 bits so, and its validation takes it (an M4 +/// under macOS 27.0.1 and an M3 Max under 26.7, compute stage-in and vertex fetch, both binding +/// forms: `2^32 + 8` and `2^63 + 8` read at 8, `2^33 + 4` at 4, `2^32` at 0). /// -/// `MTLAttributeStrideStatic` (`u64::MAX`) is no exception in a compute table: its low 32 bits, -/// `2^32 - 1`, are the stride a kernel's stage-in layout of `MTLBufferLayoutStrideDynamic` reads. -/// Plain Metal on an Apple M3 Max under macOS 27.0.1 answered each of the four stride forms bound -/// so with the element at the bind offset for the first element and zero for every element past -/// it, the bind offset reaching the first (`metal2vulkan-conformance`'s `attribute-stride-*` -/// recordings, a buffer of 512 bytes), which is a fetch at that stride: element 1 lies `2^32 - 1` -/// bytes on, past the buffer (an indirect command buffer's command read the byte there in a buffer -/// that holds it, `RuntimeGap::IndirectCommandUnstridedBuffer`). A render stage's table holds no -/// stride for it, as the plain setters bind (`render/vertex_strides.rs`). -pub(super) fn per_bind_stride(stride: u64, compute: bool) -> Option { +/// `MTLAttributeStrideStatic` (`u64::MAX`) is no exception: its low 32 bits, `2^32 - 1`, are the +/// stride a layout of `MTLBufferLayoutStrideDynamic` reads, a kernel's stage-in layout or a vertex +/// descriptor's. Plain Metal on an Apple M3 Max under macOS 27.0.1 answered each of the compute +/// encoder's four stride forms, and the render encoder's `setVertexBuffer:offset:attributeStride: +/// atIndex:`, bound so with the element at the bind offset for the first element and zero for +/// every element past it, the bind offset reaching the first (`metal2vulkan-conformance`'s +/// `attribute-stride-*` recordings, buffers of 512 and 64 bytes), which is a fetch at that stride: +/// element 1 lies `2^32 - 1` bytes on, past the buffer (an indirect command buffer's command read +/// the byte there in a buffer that holds it, `RuntimeGap::IndirectCommandUnstridedBuffer`). +/// Metal's API validation stops a draw bound so. +pub(super) fn per_bind_stride(stride: u64) -> u32 { // Truncation is the measured answer. - (compute || stride != ATTRIBUTE_STRIDE_STATIC).then_some(stride as u32) + stride as u32 } /// Bind `value` at `index` of `table`, or unbind the index when `value` is `None`: what was @@ -705,8 +701,8 @@ impl ArgumentTable { stride: u64, index: u32, ) -> Result<(), Error> { - let stride = per_bind_stride(stride, self.compute); - self.bind_buffer(core, call, buffer, offset, stride, index) + let stride = per_bind_stride(stride); + self.bind_buffer(core, call, buffer, offset, Some(stride), index) } /// [`set_buffer_with_stride`](Self::set_buffer_with_stride) at the per-bind stride `stride`, @@ -774,9 +770,7 @@ impl ArgumentTable { self.check_buffer(core, call, buffer, offset)?; } } - let compute = self.compute; - let strides = - (0..buffers.len()).map(|i| strides.and_then(|s| per_bind_stride(s[i], compute))); + let strides = (0..buffers.len()).map(|i| strides.map(|s| per_bind_stride(s[i]))); for (((buffer, &offset), stride), index) in buffers.iter().zip(offsets).zip(strides).zip(first..) { @@ -821,8 +815,7 @@ impl ArgumentTable { } /// `setBufferOffset:attributeStride:atIndex:` with the per-bind stride `stride` names - /// ([`per_bind_stride`]: `MTLAttributeStrideStatic` clears the index's stride in a render - /// stage's table). Refused as [`set_buffer_offset`](Self::set_buffer_offset) is. + /// ([`per_bind_stride`]). Refused as [`set_buffer_offset`](Self::set_buffer_offset) is. pub(super) fn set_buffer_offset_with_stride( &mut self, core: &mut EncoderCore, @@ -831,8 +824,8 @@ impl ArgumentTable { stride: u64, index: u32, ) -> Result<(), Error> { - let stride = per_bind_stride(stride, self.compute); - self.move_buffer_offset(core, call, offset, stride, index) + let stride = per_bind_stride(stride); + self.move_buffer_offset(core, call, offset, Some(stride), index) } /// [`set_buffer_offset_with_stride`](Self::set_buffer_offset_with_stride) at the per-bind diff --git a/crates/metal2vulkan-runtime/src/fetch_reach.rs b/crates/metal2vulkan-runtime/src/fetch_reach.rs index 184a2d7ff..9abf02ef6 100644 --- a/crates/metal2vulkan-runtime/src/fetch_reach.rs +++ b/crates/metal2vulkan-runtime/src/fetch_reach.rs @@ -2,11 +2,10 @@ //! attribute's (`render/vertex_reach.rs`) and a kernel `[[stage_in]]` attribute's (here). //! //! - **What differs.** Metal fetches an attribute at the displacement `element · stride + -//! offset` from its buffer's bind offset in 64 bits. The compile layer fetches a vertex attribute -//! it pulls at that displacement in wrapping 32-bit arithmetic, each byte at the displacement -//! plus its place, and a kernel stage-in attribute whose bytes lie past `2^32` as zero bytes +//! offset` from its buffer's bind offset in 64 bits. The compile layer fetches an attribute it +//! pulls (a vertex attribute or a kernel stage-in one) whose bytes lie past `2^32` as zero bytes //! (`lower::stage_io::fetch`). So where the buffer holds more than `2^32` bytes past its bind -//! offset, a byte past `2^32` reads another place of the buffer than Metal's, or zero, silently. +//! offset, a byte past `2^32` reads zero where Metal reads the buffer, silently. //! - **The refusal.** Until the fetch takes 64-bit displacements, a fetch that may read past `2^32` //! bytes of its buffer's bind offset, inside a buffer that runs past them, is refused, naming the //! buffer index and the furthest byte it may read; one that may not runs as before, and so does diff --git a/crates/metal2vulkan-runtime/src/render/vertex_reach.rs b/crates/metal2vulkan-runtime/src/render/vertex_reach.rs index 82f966fe4..90806d80e 100644 --- a/crates/metal2vulkan-runtime/src/render/vertex_reach.rs +++ b/crates/metal2vulkan-runtime/src/render/vertex_reach.rs @@ -3,9 +3,10 @@ //! //! - **What differs.** The compile layer fetches an attribute it pulls //! (`VertexInputState::pulled`: a format or step fixed-function vertex input does not take, or -//! every attribute where the device bounds no fixed-function fetch) at a 32-bit displacement -//! from the buffer's bind offset, where Metal's is 64 bits (in a buffer of 4 GiB + 1 MiB, element -//! 1 at stride `2^32 - 1` read the bytes at `2^32 - 1` and `2^32 + 3`). +//! every attribute where the device bounds no fixed-function fetch) whose bytes lie past `2^32` +//! from the buffer's bind offset as zero bytes, where Metal's displacement is 64 bits (in a +//! buffer of 4 GiB + 1 MiB, element 1 at stride `2^32 - 1` read the bytes at `2^32 - 1` and +//! `2^32 + 3`). //! - **The refusal.** A draw (the encoder's, or an indirect command buffer's command, //! `render/executions.rs`) that may read a pulled attribute past `2^32` bytes of its buffer's //! bind offset, inside a buffer that runs past them, is refused diff --git a/crates/metal2vulkan-runtime/src/render/vertex_strides.rs b/crates/metal2vulkan-runtime/src/render/vertex_strides.rs index 72b7e88ca..cc1ea94ce 100644 --- a/crates/metal2vulkan-runtime/src/render/vertex_strides.rs +++ b/crates/metal2vulkan-runtime/src/render/vertex_strides.rs @@ -8,8 +8,12 @@ //! `VERTEX_INPUT_BINDING_STRIDE`, or the pipeline's static one), or pushes where the pipeline //! pulls the buffer. It is held in the encoder's vertex table beside the buffer //! (`argument_table.rs`), as the compute encoder holds a stage-in buffer's. -//! - **`MTLAttributeStrideStatic`** (`u64::MAX`) sets no stride, as the plain setters do; a layout -//! of a fixed stride reads its own, whatever the bind's. +//! - **`MTLAttributeStrideStatic`** (`u64::MAX`) is a stride of its low 32 bits, `2^32 - 1`, as +//! any other stride past 32 bits: a dynamic layout reads the element at the bind offset for +//! vertex 0 and zero past it (every later element lies past `2^32` bytes, which the pulled +//! fetch reads as zero), as plain Metal on an M3 Max reads it (`metal2vulkan-conformance`'s +//! `attribute-stride-vertex-buffer`); Metal's API validation stops such a draw. A layout of a +//! fixed stride reads its own, whatever the bind's. //! - **The plain setters** bind without a stride (`set_vertex_buffer`, `set_vertex_buffers`), and //! so does `set_vertex_buffer_offset`: it clears the stride bound at the index. A draw whose //! pipeline's layout is dynamic at a buffer bound without a stride reads it at stride 0, as @@ -114,9 +118,10 @@ impl RenderLowering { impl RenderCommandEncoder { /// `setVertexBuffer:offset:attributeStride:atIndex:`: as /// [`set_vertex_buffer`](Self::set_vertex_buffer), the buffer read at `stride` by a layout of - /// `MTLBufferLayoutStrideDynamic` (the module docs); `MTLAttributeStrideStatic` (`u64::MAX`) - /// sets no stride; any other stride past 32 bits is taken modulo `2^32`, as plain Metal - /// reads it and its validation takes it (an M4). Refused as `set_vertex_buffer` is. + /// `MTLBufferLayoutStrideDynamic` (the module docs); a stride past 32 bits is taken modulo + /// `2^32`, as plain Metal reads it and its validation takes it (an M4), and + /// `MTLAttributeStrideStatic` (`u64::MAX`) with it: the layout reads vertex 0's element and + /// zero past it (an M3 Max). Refused as `set_vertex_buffer` is. pub fn set_vertex_buffer_with_stride( &mut self, buffer: Option<&Buffer>, @@ -137,10 +142,9 @@ impl RenderCommandEncoder { /// `setVertexBytes:length:attributeStride:atIndex:`: as /// [`set_vertex_bytes`](Self::set_vertex_bytes), the bytes read at `stride`, its low 32 bits, - /// by a layout of `MTLBufferLayoutStrideDynamic`. At `MTLAttributeStrideStatic` (`u64::MAX`) - /// a draw whose layout reads them so is refused (`Bind(MissingVertexStride)`): an M4 read - /// element 0 of the bytes and zeros past it there, which the fetch does not emulate. Refused as - /// `set_vertex_bytes` is. + /// by a layout of `MTLBufferLayoutStrideDynamic`; at `MTLAttributeStrideStatic` (`u64::MAX`), + /// `2^32 - 1`, it reads element 0 of the bytes and zeros past it, as an M4 read them. Refused + /// as `set_vertex_bytes` is. pub fn set_vertex_bytes_with_stride( &mut self, bytes: &[u8], diff --git a/crates/metal2vulkan-runtime/src/tests/argument_table.rs b/crates/metal2vulkan-runtime/src/tests/argument_table.rs index 8d66d6e20..82f31c8a8 100644 --- a/crates/metal2vulkan-runtime/src/tests/argument_table.rs +++ b/crates/metal2vulkan-runtime/src/tests/argument_table.rs @@ -6,32 +6,24 @@ use super::super::argument_table::{per_bind_stride, same_as_printed, ArgumentTable}; use metal2vulkan::bind::{BoundSampler, SamplerDescriptor, StageTable}; -/// A stride form's stride is its low 32 bits in both tables, and `MTLAttributeStrideStatic`'s -/// (`u64::MAX`) too in a compute table, where a kernel's stage-in layout of -/// `MTLBufferLayoutStrideDynamic` reads `2^32 - 1` and so the element at the bind offset and zero -/// past it, as plain Metal on an Apple M3 Max under macOS 27.0.1 answered all four compute stride -/// forms (`metal2vulkan-conformance`'s `attribute-stride-*` recordings). A render stage's table -/// holds no stride for it, as the plain setters bind. +/// A stride form's stride is its low 32 bits, and `MTLAttributeStrideStatic`'s (`u64::MAX`) too: +/// a layout of `MTLBufferLayoutStrideDynamic` reads `2^32 - 1` and so the element at the bind +/// offset and zero past it, as plain Metal on an Apple M3 Max under macOS 27.0.1 answered the +/// compute encoder's four stride forms and the render encoder's vertex form +/// (`metal2vulkan-conformance`'s `attribute-stride-*` recordings). #[test] -fn the_static_constant_is_a_compute_tables_widest_stride() { - for compute in [false, true] { - for (stride, held) in [ - (0u64, 0u32), - (8, 8), - (1 << 32, 0), - ((1 << 32) + 8, 8), - ((1 << 63) + 8, 8), - (u64::from(u32::MAX), u32::MAX), - ] { - assert_eq!( - per_bind_stride(stride, compute), - Some(held), - "{stride} {compute}" - ); - } +fn the_static_constant_is_the_widest_stride() { + for (stride, held) in [ + (0u64, 0u32), + (8, 8), + (1 << 32, 0), + ((1 << 32) + 8, 8), + ((1 << 63) + 8, 8), + (u64::from(u32::MAX), u32::MAX), + (u64::MAX, u32::MAX), + ] { + assert_eq!(per_bind_stride(stride), held, "{stride}"); } - assert_eq!(per_bind_stride(u64::MAX, true), Some(u32::MAX)); - assert_eq!(per_bind_stride(u64::MAX, false), None); } /// `setBufferOffset:atIndex:`, and its stride form, at an index with no buffer bound: a render diff --git a/crates/metal2vulkan-validation/src/runtime_lane/inline_bytes.rs b/crates/metal2vulkan-validation/src/runtime_lane/inline_bytes.rs index ed30f41fe..c8a1d5e43 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/inline_bytes.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/inline_bytes.rs @@ -12,21 +12,20 @@ //! - A vertex function's attribute reads them as its vertex buffer (the owned fixture //! `ob3_vertex_input`, `vertex_strides.rs`): at the layout's own stride, and under //! `MTLBufferLayoutStrideDynamic` at stride 0 after the plain setter and at the stride form's -//! stride, as plain Metal reads them; at `MTLAttributeStrideStatic` the -//! draw is refused (`MissingVertexStride`). +//! stride, as plain Metal reads them; at `MTLAttributeStrideStatic`, `2^32 - 1`, vertex 0 reads +//! the first element and every vertex past it zero, as an M4 read them. //! - A fragment function reads them through a `const device` pointer and a `constant` //! reference (`cfb_ts_read_bytes` of the owned fixture `cfb_tile_shadows`). //! //! The object stage's form runs `mesh_draws`' object cases (`mesh_draws.rs`). -use super::vertex_strides::{drawn, pipeline, read_at}; +use super::vertex_strides::{drawn, pipeline, read_at, read_static}; use super::{ buffer_with, layer_reported_nothing_at_all, read_buffer, validated_device, Messages, ScratchCache, WAIT, }; use crate::air_library::AirLibrary; use crate::gpu::TEST_DEVICE; -use metal2vulkan::error::BindError; use metal2vulkan::metal::{PixelFormat, PrimitiveType, TextureUsage}; use metal2vulkan::pipeline::{ ColorAttachmentDescriptor, ComputePipelineDescriptor, RenderPipelineDescriptor, StageSource, @@ -217,11 +216,9 @@ fn vertex_attributes_read_inline_bytes() { Ok(read_at(0, 3)), "a stride of 2^32 + 12" ); - assert!( - matches!( - draw(&|e| e.set_vertex_bytes_with_stride(&bytes, u64::MAX, 0)), - Err(Error::Bind(BindError::MissingVertexStride { .. })) - ), + assert_eq!( + draw(&|e| e.set_vertex_bytes_with_stride(&bytes, u64::MAX, 0)), + Ok(read_static()), "MTLAttributeStrideStatic" ); } diff --git a/crates/metal2vulkan-validation/src/runtime_lane/vertex_strides.rs b/crates/metal2vulkan-validation/src/runtime_lane/vertex_strides.rs index ad9ca01d8..378c7dd19 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/vertex_strides.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/vertex_strides.rs @@ -7,10 +7,11 @@ //! //! - The stride form reads vertex `v` at `offset + v * stride`; so do the range form and the //! offset form, which sets both. -//! - A buffer bound without a stride (the plain setters, the plain offset form after a stride -//! form, or `MTLAttributeStrideStatic`) under a dynamic layout reads at stride 0, as plain -//! Metal does (an M4; an M3 Max and an M4), where Metal's debug layer stops the draw; a stride -//! past 32 bits is refused at the setter. +//! - A buffer bound without a stride (the plain setters, or the plain offset form after a stride +//! form) under a dynamic layout reads at stride 0, as plain Metal does (an M4; an M3 Max and an +//! M4), where Metal's debug layer stops the draw; a stride past 32 bits reads at its low 32 +//! bits, and `MTLAttributeStrideStatic` at `2^32 - 1`: vertex 0's element and zero past it, as +//! plain Metal on an M3 Max reads it. //! - An executed indirect render command's vertex buffer set without a stride under a dynamic //! layout, on a command buffer whose descriptor supports dynamic attribute strides, refuses the //! commit the runtime's own gap (`IndirectCommandUnstridedBuffer`): plain Metal takes such a @@ -112,6 +113,14 @@ pub(super) fn read_at(first: u32, step: u32) -> Vec { (0..VERTICES).map(|v| 100 + first + v * step).collect() } +/// The words a layout bound at `MTLAttributeStrideStatic` reads: word 0 for vertex 0, and zero for +/// every vertex past it, whose element lies `2^32 - 1` bytes on. +pub(super) fn read_static() -> Vec { + (0..VERTICES) + .map(|v| if v == 0 { 100 } else { 0 }) + .collect() +} + /// Each stride form binds the stride a dynamic layout reads; a buffer bound without one reads at /// stride 0, as plain Metal's does; a stride past 32 bits binds its low 32 bits. #[test] @@ -168,7 +177,7 @@ fn vertex_buffers_bind_the_strides_a_dynamic_layout_reads() { ); assert_eq!( draw(&|e, b| e.set_vertex_buffer_with_stride(Some(b), 0, u64::MAX, 0)), - Ok(read_at(0, 0)), + Ok(read_static()), "MTLAttributeStrideStatic" ); // A stride past 32 bits reads at its low 32 bits, as plain Metal reads it (an M4; Metal's diff --git a/crates/metal2vulkan/src/bind/graphics.rs b/crates/metal2vulkan/src/bind/graphics.rs index bd5cdc62d..2f05a47f4 100644 --- a/crates/metal2vulkan/src/bind/graphics.rs +++ b/crates/metal2vulkan/src/bind/graphics.rs @@ -22,9 +22,9 @@ //! - **Vertex buffers.** One [`VertexBufferBind`] per fixed-function binding of the pipeline's //! `VertexInputState`; the slot must be bound (`UnboundVertexBuffer`), and a `PerBind` stride //! is the bind's `attributeStride`: 0 for a buffer or inline bytes bound without one, as plain -//! Metal's encoder reads them (its debug layer stops the draw); inline bytes -//! bound at `MTLAttributeStrideStatic`, which an M4 read as element 0 and zeros past it, are -//! `MissingVertexStride`. A binding the shape binds where fixed-function input cannot fetch it (a +//! Metal's encoder reads them (its debug layer stops the draw); `MTLAttributeStrideStatic`'s is +//! its low 32 bits, `2^32 - 1`, which the pulled fetch reads as element 0 and zeros past it, as +//! plain Metal does (an M4 over inline bytes, an M3 Max over a buffer). A binding the shape binds where fixed-function input cannot fetch it (a //! bind offset whose alignment class misses an attribute's Vulkan alignment; a per-bind stride //! past `maxVertexInputBindingStride`, not a multiple of the alignment, breaking the portability //! subset's stride rule, or as dynamic state below an element's attributes) names its layout in @@ -1788,13 +1788,9 @@ fn plan(p: &RenderPipeline, device: &Device, shape: &BindShape) -> Result {} Some(VertexStride::PerBind) => { - // A buffer's or inline bytes' stride is its bind's, 0 without one; - // inline bytes at `MTLAttributeStrideStatic` are not emulated. + // A buffer's or inline bytes' stride is its bind's, 0 without one. match stage_shape(Stage::Vertex).and_then(|s| s.buffers.get(&layout)) { - Some(BufferShape::Buffer { .. }) => {} - Some(&BufferShape::Bytes { attribute_stride }) => { - bytes_stride(attribute_stride, vertex_buffer(layout))?; - } + Some(BufferShape::Buffer { .. } | BufferShape::Bytes { .. }) => {} _ => { return Err(internal(format!( "the stride of vertex buffer {layout} bound by nothing" @@ -2201,7 +2197,7 @@ fn plan(p: &RenderPipeline, device: &Device, shape: &BindShape) -> Result { let per_bind = match stride { - VertexStride::PerBind => Some(bytes_stride(attribute_stride, slot)?), + VertexStride::PerBind => Some(attribute_stride.unwrap_or(0)), VertexStride::Static(_) => None, }; fetchable( @@ -4628,7 +4624,7 @@ fn instantiate( let (stride, per_bind) = match vb.stride { VertexStride::Static(s) => (s, None), VertexStride::PerBind => { - let s = bytes_stride(attribute_stride, vb.slot)?; + let s = attribute_stride.unwrap_or(0); (s, Some(s)) } }; @@ -4784,7 +4780,7 @@ fn pulled_stride( Some(&BoundBuffer::Bytes { attribute_stride, .. }), - ) => bytes_stride(attribute_stride, vertex_buffer(layout))?, + ) => attribute_stride.unwrap_or(0), _ => { return Err(internal(format!( "the bind state's shape differs from the plan's at vertex buffer {layout}" @@ -5007,17 +5003,6 @@ fn amplification_writes(t: &GraphicsTemplate) -> Result, slot: SlotRef) -> Result { - match attribute_stride { - Some(u32::MAX) => Err(BindError::MissingVertexStride { slot }), - stride => Ok(stride.unwrap_or(0)), - } -} - /// Whether fixed-function vertex input can fetch vertex buffer `vb` bound at an offset of /// alignment class `offset_alignment` ([`offset_alignment`]) with the per-bind stride /// `per_bind` (`None` for a static stride, which link already made fetchable) under `rules`: diff --git a/crates/metal2vulkan/src/bind/graphics/tests.rs b/crates/metal2vulkan/src/bind/graphics/tests.rs index 578188034..1ebe66b41 100644 --- a/crates/metal2vulkan/src/bind/graphics/tests.rs +++ b/crates/metal2vulkan/src/bind/graphics/tests.rs @@ -890,9 +890,9 @@ fn draws_bind_vertex_buffers_at_aligned_offsets_with_their_windows() { /// Inline vertex bytes at a per-bind layout read at their bind's stride, 0 without one, as plain /// Metal reads them (`setVertexBytes:length:atIndex:` read vertex 0 for every -/// vertex, the stride form at 0, 4 and 8 its stride); at `MTLAttributeStrideStatic` -/// (`u32::MAX`), which an M4 read as element 0 and zeros past it, they are refused -/// `MissingVertexStride`; and a stride fixed function cannot fetch is the plan's pulled variant. +/// vertex, the stride form at 0, 4 and 8 its stride); and a stride fixed function cannot fetch is +/// the plan's pulled variant, `MTLAttributeStrideStatic`'s (`u32::MAX`) among them, which the +/// pulled fetch reads as element 0 and zeros past it, as an M4 read them. #[test] fn inline_vertex_bytes_bind_the_strides_a_dynamic_layout_reads() { let mut template = GraphicsTemplate { @@ -943,14 +943,15 @@ fn inline_vertex_bytes_bind_the_strides_a_dynamic_layout_reads() { // Dynamic strides list every binding's, the static one included (`pStrides`). assert_eq!(cmds.vertex_strides, vec![(0, 24), (1, read)], "{stride:?}"); } - assert_eq!( - instantiate(&template, &state(Some(u32::MAX)), &draw).err(), - Some(BindError::MissingVertexStride { slot: vslot(2) }) - ); - // A stride fixed function cannot fetch (past the attributes' alignment, or below the bytes - // an element reads) is the plan's pulled variant: a draw of this plan with it is an internal - // error. + // A stride fixed function cannot fetch (past the attributes' alignment, below the bytes an + // element reads, or past `maxVertexInputBindingStride`) is the plan's pulled variant: a draw of + // this plan with it is an internal error. let pulled = |r: Result| matches!(r, Err(BindError::Internal(_))); + assert!(pulled(instantiate( + &template, + &state(Some(u32::MAX)), + &draw + ))); assert!(pulled(instantiate(&template, &state(Some(7)), &draw))); assert!(pulled(instantiate(&template, &state(Some(6)), &draw))); template.vertex_strides = rules(None, false); diff --git a/crates/metal2vulkan/src/bind/state.rs b/crates/metal2vulkan/src/bind/state.rs index 47a60a9e4..0a0d7caf2 100644 --- a/crates/metal2vulkan/src/bind/state.rs +++ b/crates/metal2vulkan/src/bind/state.rs @@ -1330,10 +1330,9 @@ pub enum BoundBuffer { /// `MTLBufferLayoutStrideDynamic` reads the bytes at; `None` for the plain setters, which /// such a layout reads at stride 0, as plain Metal reads them. /// `MTLAttributeStrideStatic` is `Some(u32::MAX)`, its low 32 bits: an M4 read element 0 - /// of inline bytes bound so and zeros past it, which a kernel's stage-in fetch reads at - /// that stride (every element past the first lies past the bytes); the binder refuses a - /// vertex layout that reads inline bytes at it - /// ([`BindError::MissingVertexStride`](crate::error::BindError::MissingVertexStride)). + /// of inline bytes bound so and zeros past it, which the pulled fetch, a vertex + /// attribute's or a kernel stage-in's, reads at that stride (every element past the first + /// lies past the bytes). #[cfg_attr(feature = "serde", serde(default))] attribute_stride: Option, }, diff --git a/crates/metal2vulkan/src/error/mod.rs b/crates/metal2vulkan/src/error/mod.rs index 5ba585b6a..bf9de1a56 100644 --- a/crates/metal2vulkan/src/error/mod.rs +++ b/crates/metal2vulkan/src/error/mod.rs @@ -2080,13 +2080,6 @@ pub enum BindError { InvalidDescriptor(DescriptorError), /// A render pipeline fetches a fixed-function vertex buffer that nothing is bound to. UnboundVertexBuffer { slot: SlotRef }, - /// A vertex buffer layout with a per-bind stride (`MTLBufferLayoutStrideDynamic`) reads - /// inline bytes (`setVertexBytes`) bound at `MTLAttributeStrideStatic` - /// (`BoundBuffer::Bytes::attribute_stride` `Some(u32::MAX)`), which an M4 read as element 0 - /// and zeros past it (Metal's debug layer stops the draw): not emulated. - /// Inline bytes and a buffer bound without a stride read at stride 0, as plain Metal's encoder - /// reads them, and at the stride bound with one. - MissingVertexStride { slot: SlotRef }, /// The set-1 heap's elements (one per resident texture a handle can name, plus the null /// element) exceed the pipeline's heap capacity: the heap arrays' count, at most the handle /// table's reach. diff --git a/crates/metal2vulkan/src/lower/stage_io/fetch.rs b/crates/metal2vulkan/src/lower/stage_io/fetch.rs index ce9cdf778..469d1b027 100644 --- a/crates/metal2vulkan/src/lower/stage_io/fetch.rs +++ b/crates/metal2vulkan/src/lower/stage_io/fetch.rs @@ -15,8 +15,15 @@ //! element · VertexBufferStride(buffer) + offset //! ``` //! -//! from the bind offset (a view adds the bind residual `SlotBindOffset(buffer)`), in wrapping -//! `u32` arithmetic, where `element` is the step's element index: +//! from the bind offset (a view adds the bind residual `SlotBindOffset(buffer)`), in `u32` +//! arithmetic, where `element` is the step's element index. Metal places an element in 64 bits, +//! so where an element's bytes run past the first `2^32` bytes from the bind offset +//! (`element · stride + offset + size > 2^32`, decided exactly, where the `u32` displacement would +//! wrap back into the buffer) the read lies outside the buffer and yields zero bytes on every +//! device (`within_reach`, `access::pulled::prepare_within`), as plain Metal on an M3 Max reads a +//! vertex buffer bound at a stride of `2^31 + 4` (`metal2vulkan-conformance`'s +//! `attribute-stride-vertex-buffer`): the runtime refuses a draw that may fetch there from a buffer +//! that holds those bytes. The steps: //! //! - `PerVertex`: `VertexIndex` (it includes the base vertex in Vulkan as `[[vertex_id]]` does in //! Metal); in the de-indexed and private list forms, whose draws have no indices, the vertex @@ -60,15 +67,12 @@ //! descriptor names, at `element · stride + offset` with the layout's stride: a fixed one compiled //! in, or for a layout of `MTLBufferLayoutStrideDynamic` the one the encoder binds the buffer with, //! from `PushField::StageInStride(buffer_index)`, where `element` is the step's ([`Step::Grid`], -//! [`Step::Indexed`], [`Step::Constant`]; `super::stage_in` plans them). Metal places an element in -//! 64 bits, so where an element's bytes run past the first `2^32` bytes from the bind offset -//! (`e · stride + offset + size > 2^32`, decided exactly, where the `u32` displacement would wrap -//! back into the buffer) the read lies outside the buffer and yields zero bytes on every device -//! (`within_reach`, `access::pulled::prepare_within`): the runtime refuses a dispatch that may -//! fetch there from a buffer that holds those bytes. A dynamic layout bound with -//! `MTLAttributeStrideStatic` reads at its low 32 bits, `2^32 - 1`, so its first element is the -//! one at the bind offset and every element past it reads zero, as plain Metal on an M3 Max -//! answers it. +//! [`Step::Indexed`], [`Step::Constant`]; `super::stage_in` plans them), with the same reach as a +//! vertex attribute's: the runtime refuses a dispatch that may fetch past `2^32` bytes from a +//! buffer that holds those bytes. A dynamic layout bound with `MTLAttributeStrideStatic`, a vertex +//! layout's or a kernel's, reads at its low 32 bits, `2^32 - 1`, so its first element is the one +//! at the bind offset and every element past it reads zero, as plain Metal on an M3 Max answers +//! both. //! //! **Source buffers**. This family records which Metal buffer indices the fetch reads //! ([`super::StageIoPlan::buffers`]); their slots are the access family's (`lower::access::pulled`, @@ -438,17 +442,15 @@ impl PulledRead { }); // A kernel step derives the thread position in the grid (the region arithmetic), clamps it // to the stage-in region, reads its index the way it reads the attribute, and adds the - // region's origin. + // region's origin. Every step with an element checks its reach. let element = match self.step { Step::Grid { .. } => GRID_POSITION_WORDS + REGION_AXIS_WORDS + REACH_WORDS + 16, Step::Indexed { index, .. } => { GRID_POSITION_WORDS + REGION_AXIS_WORDS + REACH_WORDS + index.leaf().cost() + 176 } // The index read is the plan's, counted once (`super::CAPTURED_INDEX_WORDS`). - Step::PerVertex { .. } - | Step::PerInstance { .. } - | Step::Constant - | Step::CapturedVertex => 0, + Step::PerVertex { .. } | Step::PerInstance { .. } | Step::CapturedVertex => REACH_WORDS, + Step::Constant => 0, }; self.encoding.leaf().cost() + 4 * lane + 160 + element } @@ -459,7 +461,7 @@ impl PulledRead { /// threadgroup, unflattened in the flattened tiers (`dispatch::KernelDecl::value`). const GRID_POSITION_WORDS: u32 = 400; -/// An upper bound of the words of a kernel stage-in read's reach (`within_reach` and +/// An upper bound of the words of a pulled read's reach (`within_reach` and /// `pulled::prepare_within`): the division, the comparisons and their conjunctions, the /// selects, and their constants. const REACH_WORDS: u32 = 96; @@ -1008,13 +1010,10 @@ pub(super) fn emit( (None, _) => None, }; let n = r.encoding.bytes(); - // A kernel stage-in element reads Metal's 64-bit displacement, which the `u32` one below - // wraps. - let fits = match (r.step, element) { - (Step::Grid { .. } | Step::Indexed { .. }, Some((e, s))) => { - within_reach(fb, e, s, r.stride, r.offset, n)? - } - _ => None, + // An element reads Metal's 64-bit displacement, which the `u32` one below wraps. + let fits = match element { + Some((e, s)) => within_reach(fb, e, s, r.stride, r.offset, n)?, + None => None, }; let m = displacement(fb, element, r.offset)?; let read = match fits { @@ -1024,7 +1023,7 @@ pub(super) fn emit( fetch_from(fb, read, r.encoding, r.form) } -/// Whether the `n` bytes of a kernel stage-in read at element `e` of stride `s` (the value of +/// Whether the `n` bytes of a pulled read at element `e` of stride `s` (the value of /// `stride`) and attribute offset `offset` lie in the first `2^32` bytes from the bind offset, /// as Metal's 64-bit displacement `e · s + offset` places them: `e · s + offset + n <= 2^32`, /// exactly, which the `u32` displacement's wrap cannot say. `None` where every element's do (a diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 280e4aa8d..a89cd2ad8 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -13281,7 +13281,6 @@ variant metal2vulkan::error::BindError::MissingGuestAddress MissingGuestAddress variant metal2vulkan::error::BindError::MissingPrimitiveType MissingPrimitiveType variant metal2vulkan::error::BindError::MissingStageInRegion MissingStageInRegion variant metal2vulkan::error::BindError::MissingThreadgroupMemory MissingThreadgroupMemory { index: u32 } -variant metal2vulkan::error::BindError::MissingVertexStride MissingVertexStride { slot: SlotRef } variant metal2vulkan::error::BindError::NeedsVariant NeedsVariant { variant: Box } variant metal2vulkan::error::BindError::PatchControlPoints PatchControlPoints { given: u32, expected: u32 } variant metal2vulkan::error::BindError::PatchIndexRange PatchIndexRange { needed: u64, limit: u64 }