diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 2a34e4fae..ed05b0264 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -1394,7 +1394,9 @@ made, with no lock, and never rewritten for layouts. is cube-compatible with six layers a cube, and its view is a cube array view (`imageCubeArray`, in the floor). - `Texture::gpu_resource_id` is its index (0 without one) and the translator's `TextureWord` of - its format and swizzle (`docs/RESOURCE-ID.md`), so the guest computes the ID itself. + its format and swizzle (`docs/RESOURCE-ID.md`), so the guest computes the ID itself. An ID of + index 0 that a shader reads from bytes (an argument buffer's texture) reads the null descriptor, + as Metal's nil texture; nothing refuses it there. - `Texture::replace_region` and `Texture::get_bytes` take a Shared or Managed texture (`Refusal::TextureBytesPrivate` otherwise), a region inside a level and slice on the format's block grid (`Refusal::TextureRegion`), and rows and images apart as Metal's calls take them, @@ -1626,8 +1628,10 @@ made, with no lock, and never rewritten for layouts. IOSurface plane (`newTextureWithDescriptor:iosurface:plane:`), each with a `gpuResourceID` of its own, are its views at their own indices, two such textures two views. Copies, `replace_region` and `get_bytes` reach it as they reach any texture. What differs: Metal gives - every texture an index, and this one has none, so its `gpu_resource_id` answers index 0 and a - slot that binds it reads Metal's nil texture. `new_texture` still refuses a texture shaders + every texture an index, and this one has none, so its `gpu_resource_id` answers index 0, which + a shader reads as Metal's nil texture, and where its usage reaches shaders a bind of it to a + function's texture slot (`set_texture`, `set_vertex_texture`, `set_fragment_texture`) is + `Refusal::TextureWithoutIndex`: bind a view. `new_texture` still refuses a texture shaders reach made without an index (`Refusal::TextureIndex`). `Device::new_heap_texture_for_views(descriptor, heap, offset)` is the same texture placed in a heap, as `newTextureWithDescriptor:offset:` places one: its image is bound at `offset` in the @@ -2415,8 +2419,10 @@ while the driver's exit handler waited on a lock, with every runtime thread alre While it is open, the encoder holds the command buffer's recording: another encoder, an encode call, `enqueue` and `commit` are refused (`Refusal::EncoderOpen`). `end_encoding`, or dropping the encoder, gives it back. -- `set_texture` (128 slots, `Refusal::TextureArgumentIndex` past them) and `set_sampler_state` - (16 slots, `Refusal::SamplerArgumentIndex`) bind as Metal's calls do; `use_resource` takes a +- `set_texture` (128 slots, `Refusal::TextureArgumentIndex` past them; + `Refusal::TextureWithoutIndex` for a texture reached through its views alone, which has no + index a slot could push) and `set_sampler_state` (16 slots, `Refusal::SamplerArgumentIndex`) + bind as Metal's calls do; `use_resource` takes a texture as it takes a buffer. A dispatch pushes, for each texture and sampler slot its kernel reads, the bound object's `gpuResourceID` (0 for none), whose index names its elements of set 1; for a texture slot an integer write saturates through, the channel widths of the bound diff --git a/crates/metal2vulkan-runtime/src/encoder.rs b/crates/metal2vulkan-runtime/src/encoder.rs index 64a464004..69aef2876 100644 --- a/crates/metal2vulkan-runtime/src/encoder.rs +++ b/crates/metal2vulkan-runtime/src/encoder.rs @@ -580,8 +580,9 @@ impl ComputeCommandEncoder { /// and the channel widths an integer write saturates to where its kernel takes them. The /// command buffer keeps the texture until it retires. /// - /// Err: [`Refusal::TextureArgumentIndex`] for an index of 128 or more; - /// [`Refusal::OtherDevice`] for another device's texture. + /// Err, binding nothing: [`Refusal::TextureArgumentIndex`] for an index of 128 or more; + /// [`Refusal::OtherDevice`] for another device's texture; [`Refusal::TextureWithoutIndex`] + /// for a texture shaders reach through its views alone. pub fn set_texture(&mut self, texture: Option<&Texture>, index: u32) -> Result<(), Error> { if index >= TEXTURE_SLOTS { return Err(Error::Refused(Refusal::TextureArgumentIndex { index })); @@ -596,6 +597,9 @@ impl ComputeCommandEncoder { if !texture.is_of(building.device.core()) { return Err(Error::Refused(Refusal::OtherDevice)); } + if texture.views_alone() { + return Err(Error::Refused(Refusal::TextureWithoutIndex { index })); + } // SAFETY: the encoder's slot holds the borrow, and it ends before its command // buffer commits. Some(unsafe { building.lend_texture(texture) }) diff --git a/crates/metal2vulkan-runtime/src/error.rs b/crates/metal2vulkan-runtime/src/error.rs index 339363ac0..bb42309a5 100644 --- a/crates/metal2vulkan-runtime/src/error.rs +++ b/crates/metal2vulkan-runtime/src/error.rs @@ -528,17 +528,18 @@ pub enum Refusal { /// The index given. index: Option, }, - /// A texture made without an index bound to a function's texture slot - /// ([`ComputeCommandEncoder::set_texture`](crate::ComputeCommandEncoder::set_texture), - /// [`RenderCommandEncoder::set_vertex_texture`](crate::RenderCommandEncoder::set_vertex_texture), - /// [`RenderCommandEncoder::set_fragment_texture`](crate::RenderCommandEncoder::set_fragment_texture)): - /// one shaders reach through its views alone + /// A texture shaders reach through its views alone, made without an index of its own /// ([`Device::new_texture_for_views`](crate::Device::new_texture_for_views), a surface's or a /// Direct3D 11 surface's plane made with no index, [`PlaneSpec::index`](crate::PlaneSpec)), - /// or one no shader reaches (a depth or stencil target made for neither shader reads nor - /// writes). A slot reads the texture at its index, and such a texture has none: every read - /// would answer the null descriptor's zeros. Bind a view of it, which has an index of its - /// own. Metal has no counterpart: Metal gives every texture a resource ID. + /// bound to a function's texture slot + /// ([`ComputeCommandEncoder::set_texture`](crate::ComputeCommandEncoder::set_texture), + /// [`RenderCommandEncoder::set_vertex_texture`](crate::RenderCommandEncoder::set_vertex_texture), + /// [`RenderCommandEncoder::set_fragment_texture`](crate::RenderCommandEncoder::set_fragment_texture)). + /// A slot reads the texture at its index, and this one has none: every read would answer the + /// null descriptor's zeros. Bind a view of it, which has an index of its own. A texture no + /// shader reaches (a depth or stencil target whose usage asks neither shader reads nor + /// writes) has no index either, and binds, as Metal's does: its usage asks no shader read of + /// it. Metal has no counterpart: Metal gives every texture a resource ID. /// /// Basis: ours (a slot pushes its texture's index, an element of set 1's texture arrays). TextureWithoutIndex { @@ -2565,8 +2566,8 @@ impl fmt::Display for Refusal { ), Refusal::TextureWithoutIndex { index } => write!( f, - "the texture bound at texture index {index} was made without an index, so no \ - slot reaches it; bind a view of it" + "the texture bound at texture index {index} is reached through its views alone, \ + with no index of its own; bind a view of it" ), Refusal::TextureZeroExtent => f.write_str("a texture has a width, height or depth of 0"), Refusal::TextureExtentForType => { diff --git a/crates/metal2vulkan-runtime/src/render_encoder.rs b/crates/metal2vulkan-runtime/src/render_encoder.rs index 43b4876e7..319baba29 100644 --- a/crates/metal2vulkan-runtime/src/render_encoder.rs +++ b/crates/metal2vulkan-runtime/src/render_encoder.rs @@ -1038,8 +1038,9 @@ impl RenderCommandEncoder { /// vertex function reads, and the channel widths an integer write saturates to where the /// function takes them. The command buffer keeps the texture until it retires. /// - /// Err: [`Refusal::TextureArgumentIndex`] for an index of 128 or more; - /// [`Refusal::OtherDevice`] for another device's texture. + /// Err, binding nothing: [`Refusal::TextureArgumentIndex`] for an index of 128 or more; + /// [`Refusal::OtherDevice`] for another device's texture; [`Refusal::TextureWithoutIndex`] + /// for a texture shaders reach through its views alone. pub fn set_vertex_texture( &mut self, texture: Option<&Texture>, @@ -1079,6 +1080,9 @@ impl RenderCommandEncoder { if !texture.is_of(building.device.core()) { return Err(Error::Refused(Refusal::OtherDevice)); } + if texture.views_alone() { + return Err(Error::Refused(Refusal::TextureWithoutIndex { index })); + } // SAFETY: the encoder's table holds the borrow, and it ends before its command // buffer commits. Some(unsafe { building.lend_texture(texture) }) diff --git a/crates/metal2vulkan-runtime/src/tests/bind_without_index.rs b/crates/metal2vulkan-runtime/src/tests/bind_without_index.rs index 676215677..e36d79e5c 100644 --- a/crates/metal2vulkan-runtime/src/tests/bind_without_index.rs +++ b/crates/metal2vulkan-runtime/src/tests/bind_without_index.rs @@ -3,9 +3,9 @@ //! Under a device cfg, on every device of the cfg's kind: a texture reached through its views //! alone (`Device::new_texture_for_views`) bound to a compute encoder's texture slot, or to a //! render encoder's vertex or fragment slot, is `Refusal::TextureWithoutIndex` with the slot's -//! index, and so is a depth target no shader reaches, which is made without an index too. -//! Unbinding the slot (`None`), a view of the texture at an index of its own, and a texture made -//! at an index each bind, and the command buffer completes with no error. +//! index. Unbinding the slot (`None`), a view of the texture at an index of its own, a texture +//! made at an index, and a depth target no shader reaches, which has no index either, each bind, +//! and the command buffer completes with no error. #[cfg(any(m2v_gpu, m2v_cpu_icd))] mod on_device { @@ -27,7 +27,7 @@ mod on_device { } #[test] - fn a_texture_made_without_an_index_binds_to_no_texture_slot() { + fn a_texture_reached_through_its_views_alone_binds_to_no_texture_slot() { each_device("bind_without_index", |name, _, device| { (device.grant_indices(IndexClass::Texture, 4096, 2)) .unwrap_or_else(|error| panic!("{name}: {error}")); @@ -58,14 +58,14 @@ mod on_device { .unwrap_or_else(|error| panic!("{name}: {error}")); let compute_answers = [ answer(compute.set_texture(Some(&unindexed), 3)), - answer(compute.set_texture(Some(&depth), 4)), answer(compute.set_texture(None, 3)), answer(compute.set_texture(Some(&view), 3)), answer(compute.set_texture(Some(&indexed), 4)), + answer(compute.set_texture(Some(&depth), 5)), ]; assert_eq!( compute_answers, - [without(3), without(4), None, None, None], + [without(3), None, None, None, None], "{name}: compute" ); compute.end_encoding(); @@ -80,30 +80,23 @@ mod on_device { let render_answers = [ answer(render.set_vertex_texture(Some(&unindexed), 5)), answer(render.set_fragment_texture(Some(&unindexed), 6)), - answer(render.set_fragment_texture(Some(&depth), 7)), answer(render.set_vertex_texture(None, 5)), answer(render.set_fragment_texture(None, 6)), answer(render.set_vertex_texture(Some(&view), 5)), answer(render.set_fragment_texture(Some(&view), 6)), answer(render.set_fragment_texture(Some(&indexed), 7)), + answer(render.set_fragment_texture(Some(&depth), 8)), ]; assert_eq!( render_answers, - [ - without(5), - without(6), - without(7), - None, - None, - None, - None, - None - ], + [without(5), without(6), None, None, None, None, None, None], "{name}: render" ); render.end_encoding(); run(name, &command_buffer); - println!("{name}: each bind of a texture made without an index refused with its slot"); + println!( + "{name}: each bind of a texture reached through its views refused with its slot" + ); drop(( command_buffer, queue, diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs index a01a1bb00..f7f8e0c71 100644 --- a/crates/metal2vulkan-runtime/src/texture.rs +++ b/crates/metal2vulkan-runtime/src/texture.rs @@ -1079,8 +1079,9 @@ impl Device { /// the consumer makes as its views: the textures Metal makes over one IOSurface plane /// (`newTextureWithDescriptor:iosurface:plane:`), for one. Copies, uploads and readbacks reach /// it as they reach any texture. Where Metal gives every texture an index, this one has none: - /// its [`Texture::gpu_resource_id`] answers index 0, and a slot that binds it reads Metal's - /// nil texture. + /// its [`Texture::gpu_resource_id`] answers index 0, which a shader reads as Metal's nil + /// texture, and where its usage reaches shaders, a bind of it to a function's texture slot is + /// refused ([`Refusal::TextureWithoutIndex`]). /// /// Err: as [`Device::new_texture`], but for [`Refusal::TextureIndex`] and /// [`Refusal::IndicesNotGranted`]. @@ -4156,8 +4157,14 @@ impl Texture { homed } - /// `gpuResourceID`: its index (0 for a texture no shader reaches) and the translator's - /// `TextureWord` of its pixel format and swizzle. + /// `gpuResourceID`: its index and the translator's `TextureWord` of its pixel format and + /// swizzle. The index is 0 for a texture made without one: a texture no shader reaches, and + /// one shaders reach through its views alone ([`Device::new_texture_for_views`]), where Metal + /// gives every texture an index of its own. A shader that reads an ID of index 0, from an + /// argument buffer or any bytes it reads as an ID, reads element 0 of set 1's texture arrays, + /// the null descriptor, as Metal's nil texture: zeros, sizes of 0, and writes dropped. A bind + /// of a texture reached through its views alone to a function's texture slot is refused + /// ([`Refusal::TextureWithoutIndex`]); nothing refuses its ID written into bytes. pub fn gpu_resource_id(&self) -> ResourceId { TextureObject::gpu_resource_id(self) } @@ -4491,6 +4498,14 @@ pub(crate) trait TextureObject { Arc::ptr_eq(&self.object().core, core) } + /// Whether shaders reach it through its views alone: its usage reaches shaders and it has no + /// index of its own ([`Device::new_texture_for_views`]), so no slot reaches it + /// ([`Refusal::TextureWithoutIndex`]). A texture no shader reaches has no index either. + fn views_alone(&self) -> bool { + let d = &self.object().descriptor; + self.object().index.is_none() && reached(d.pixel_format, usage_taken(d)) + } + /// Its image. A texture buffer has none: every call that reaches this refuses one first /// ([`texels`](Self::texels)). fn image(&self) -> vk::Image { @@ -5227,7 +5242,7 @@ pub(crate) trait TextureObject { } } - /// `gpuResourceID`: its index (0 for a texture no shader reaches) and the translator's + /// `gpuResourceID`: its index (0 for a texture made without one) and the translator's /// `TextureWord` of its pixel format and swizzle. fn gpu_resource_id(&self) -> ResourceId { ResourceId { diff --git a/docs/REFUSALS.md b/docs/REFUSALS.md index ca6d835fc..0840bbccf 100644 --- a/docs/REFUSALS.md +++ b/docs/REFUSALS.md @@ -31,7 +31,7 @@ Columns: | Rate maps the compile layer does not compile | 5 | 5 | 0 | 0 | 4 | 1 | 0 | | The translation deadline | 1 | 1 | 0 | 0 | 0 | 1 | 0 | | Devices below the runtime's floor | 8 | 8 | 0 | 0 | 0 | 0 | 0 | -| The runtime's `Refusal` | 86 | 85 | 0 | 0 | 0 | 0 | 0 | +| The runtime's `Refusal` | 86 | 86 | 0 | 0 | 0 | 0 | 0 | | The runtime's `Limit` | 18 | 18 | 0 | 0 | 0 | 0 | 0 | | The runtime's `Gap` | 31 | 31 | 0 | 0 | 0 | 0 | 0 | @@ -266,7 +266,7 @@ Columns: | `IndicesGranted` | A grant of an index already granted (`Device::grant_indices`) | every device (a public call's own check of its arguments) | `ours`: each index has one grant, so each resource ID names one object | 1: `crates/metal2vulkan-runtime/src/arrays.rs (grant)` | `-` | | `IndicesNotGranted` | A texture, a sampler state or a pipeline state's constexpr sampler at an index no grant of its class holds (`Device::new_texture`,... | every device (a public call's own check of its arguments) | `ours`: an object takes an index the consumer was granted | 1: `crates/metal2vulkan-runtime/src/arrays.rs (place)` | `-` | | `TextureIndex` | A texture shaders reach made without an index, or one they do not reach made with one (`Device::new_texture`) | every device (a public call's own check of its arguments) | `ours`: an index is an element of set 1's texture arrays, which only shaders read | 5: `crates/metal2vulkan-runtime/src/texture.rs (new_drawable_texture)`, `crates/metal2vulkan-runtime/src/texture.rs (surface_texture)` (+3) | `-` | -| `TextureWithoutIndex` | A texture made without an index bound to a function's texture slot (`ComputeCommandEncoder::set_texture`,... | every device (a public call's own check of its arguments) | `ours`: a slot pushes its texture's index, an element of set 1's texture arrays | 0 | `-` | +| `TextureWithoutIndex` | A texture shaders reach through its views alone, made without an index of its own (`Device::new_texture_for_views`, a surface's or a Direct3D 11... | every device (a public call's own check of its arguments) | `ours`: a slot pushes its texture's index, an element of set 1's texture arrays | 2: `crates/metal2vulkan-runtime/src/encoder.rs (set_texture)`, `crates/metal2vulkan-runtime/src/render_encoder.rs (set_texture)` | `-` | | `TextureZeroExtent` | A texture with a width, height or depth of 0 | every device (a public call's own check of its arguments) | `metal-measured`: an Apple M3 Max, macOS 27.0.1: plain Metal asserts "MTLTextureDescriptor has width of zero", and the same for a height of zero | 2: `crates/metal2vulkan-runtime/src/texture.rs (shape)`, `crates/metal2vulkan-runtime/src/texture.rs (texel_texture)` | `-` | | `TextureExtentForType` | A texture with a height other than 1 of a 1D type, or a depth other than 1 of a type that is not 3D | every device (a public call's own check of its arguments) | `metal-measured`: an Apple M3 Max, macOS 27.0.1: plain Metal asserts "the texture type MTLTextureType1D requires that height is 1", and the same for a 2D texture of depth 2 | 2: `crates/metal2vulkan-runtime/src/texture.rs (shape)`, `crates/metal2vulkan-runtime/src/texture.rs (texel_texture)` | `-` | | `TextureCubeNotSquare` | A cube texture whose width and height differ | every device (a public call's own check of its arguments) | `metal-measured`: an Apple M3 Max, macOS 27.0.1: plain Metal asserts "MTLTextureDescriptor width must equal height for cube textures" | 1: `crates/metal2vulkan-runtime/src/texture.rs (shape)` | `-` | @@ -561,6 +561,7 @@ Each refusal's sites, with the function that builds it. - `rt:Refusal::IndicesGranted`: `crates/metal2vulkan-runtime/src/arrays.rs (grant)` - `rt:Refusal::IndicesNotGranted`: `crates/metal2vulkan-runtime/src/arrays.rs (place)` - `rt:Refusal::TextureIndex`: `crates/metal2vulkan-runtime/src/texture.rs (new_drawable_texture)`; `crates/metal2vulkan-runtime/src/texture.rs (surface_texture)`; `crates/metal2vulkan-runtime/src/texture.rs (texel_texture)`; `crates/metal2vulkan-runtime/src/texture.rs (texture)`; `crates/metal2vulkan-runtime/src/texture.rs (view)` +- `rt:Refusal::TextureWithoutIndex`: `crates/metal2vulkan-runtime/src/encoder.rs (set_texture)`; `crates/metal2vulkan-runtime/src/render_encoder.rs (set_texture)` - `rt:Refusal::TextureZeroExtent`: `crates/metal2vulkan-runtime/src/texture.rs (shape)`; `crates/metal2vulkan-runtime/src/texture.rs (texel_texture)` - `rt:Refusal::TextureExtentForType`: `crates/metal2vulkan-runtime/src/texture.rs (shape)`; `crates/metal2vulkan-runtime/src/texture.rs (texel_texture)` - `rt:Refusal::TextureCubeNotSquare`: `crates/metal2vulkan-runtime/src/texture.rs (shape)`