From 73bb5eb2049dfec5e3dc7fc040d74e0e277e76b0 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 2 Dec 2026 06:45:15 -0800 Subject: [PATCH] rt: a dispatch or draw whose texture lacks the usage its slot's access asks is TextureUsageAtSlot A function's texture slot bound to a texture whose usage lacks what the function's access there asks reached no element of the texture's and ran: it read element 0 of set 1 (nil's null descriptor) or a null element as zeros and dropped its writes, with no refusal and no count. The textures: one no shader reaches (no index: a Depth32Float target of RenderTarget usage alone, a texture of PixelFormatView usage alone), a colour target of RenderTarget usage alone (null elements), a texture not made for shader writes at a written slot (no storage element), and a view made with a swizzle at a written slot (written unswizzled, where plain Metal writes through the swizzle). Each dispatch and draw now checks, once after the pipeline state or a binding changes and before it records anything, each texture its functions' slots reach against the usage Metal's validation asks by the access the function declares there (pipeline::slot_usage): ShaderRead for a sample or read slot, a size query alone included; ShaderWrite for a write or read_write slot; both for a read_write slot it reads. The texture's usage is Metal's answer (TextureObject::metal_usage): the descriptor's, Metal's unknown usage as 0x17, and a texture reading through a swizzle other than the identity ShaderRead and PixelFormatView alone. A texture short of it refuses the command, Refusal::TextureUsageAtSlot { stage, index, usage }, binding and recording nothing. The binds take every texture, as Metal's do. Gap::SwizzledReadWrite stays checked first for a read_write slot it reads. The check reads facts the interface and the texture already hold; nothing runs per draw. Basis (the previous commit's measurement, an Apple M3 Max, macOS 27.0.1, jobs m3-max-78392 and m3-max-78422): plain Metal reads and writes the texels; Metal's validation asserts "Compute Function(k_read): reads texture (t[0]) whose usage (0x04) doesn't specify MTLTextureUsageShaderRead (0x01)", "writes texture (t[0]) whose usage (0x01) doesn't specify MTLTextureUsageShaderWrite (0x02)" (an access::write argument asked only its width included), and "Fragment Function(f_first): Shader reads texture (t[0]) whose usage (0x04) doesn't specify MTLTextureUsageShaderRead (0x01)". Docs: REFUSALS.md regenerated (the refusal's site); the runtime README's Textures section states the rule (Texture slots and usage) and its compute encoder, render encoder and swizzle entries name the refusal; RESOURCE-ID.md's sentence that a slot binding a colour target of neither usage reads nil now says what a runtime refuses; the doc comments of set_texture, set_vertex_texture, dispatch_threadgroups, draw_primitives, gpu_resource_id and new_texture_view_with_swizzle name it. The test literals of the kernel interface and of a content's draws (tests/compute.rs, tests/indirect.rs) take the new field. Verified: cargo fmt. The red and green runs, clippy and the runtime suite on lavapipe follow as jobs. --- crates/metal2vulkan-runtime/README.md | 32 +++++++++++++--- crates/metal2vulkan-runtime/src/encoder.rs | 37 +++++++++++++++++-- crates/metal2vulkan-runtime/src/pipeline.rs | 26 ++++++++++++- crates/metal2vulkan-runtime/src/render.rs | 12 ++++-- .../src/render_encoder.rs | 26 ++++++++++--- .../metal2vulkan-runtime/src/tests/compute.rs | 1 + .../src/tests/indirect.rs | 1 + crates/metal2vulkan-runtime/src/texture.rs | 29 +++++++++++++-- docs/REFUSALS.md | 5 ++- docs/RESOURCE-ID.md | 17 +++++++-- 10 files changed, 156 insertions(+), 30 deletions(-) diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index b10830276..78952d5fe 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -2045,6 +2045,20 @@ made, with no lock, and never rewritten for layouts. and its index is free at once; the reaper frees its image once the last upload that moves or copies it has retired, and a view's image views once the view is gone. `Gauges::textures` counts images and views until then, `Gauges::descriptor_slots` the indices held. +- Texture slots and usage. A function's access at a texture slot asks a usage of the texture + bound there, as Metal's validation asks it, by the access the function declares: `ShaderRead` + for a `sample` or `read` slot, whatever the function does there (a size query alone included), + `ShaderWrite` for a `write` or `read_write` slot, and both for a `read_write` slot it reads. The + usage is Metal's answer: its unknown usage takes every access, a view's is its texture's, and a + view made with a swizzle answers `ShaderRead` and `PixelFormatView` alone. Any texture binds, as + Metal's does; a dispatch or a draw whose function uses a slot whose texture lacks the usage + is `Refusal::TextureUsageAtSlot` (the stage, the slot and the usage), found when the pipeline + state or a binding changes, and records nothing. Without the usage the slot reaches no element + of the texture's: a texture no shader reaches has no index, a colour target made for neither + shader reads nor writes has null elements, and one not made for shader writes has no storage + element. Plain Metal reads and writes such a texture all the same; its validation asserts + (M3 Max, macOS 27.0.1, jobs m3-max-78392 and m3-max-78422). A `read_write` slot the function + only writes asks `ShaderWrite` alone, as its write reaches the storage element alone. ### Known gaps of textures @@ -2248,10 +2262,12 @@ made, with no lock, and never rewritten for layouts. own descriptor takes a swizzle only without shader-write and render-target usage (`Refusal::TextureSwizzleWithWrites`), so this reaches views alone. A view's descriptor reports its base texture's usage, which is what the view may be used with, so a draw into such a view - and a kernel's write through it run, where Metal reports only `ShaderRead` and - `PixelFormatView` of a base of those, `ShaderWrite` and `RenderTarget`, for a view made with a - swizzle other than the identity and for every view of such a view (M3 Max, macOS 27.0.1, - `metal2vulkan-conformance/probes/swizzled_view_stores.m`). + runs, where Metal reports only `ShaderRead` and `PixelFormatView` of a base of those, + `ShaderWrite` and `RenderTarget`, for a view made with a swizzle other than the identity and + for every view of such a view (M3 Max, macOS 27.0.1, + `metal2vulkan-conformance/probes/swizzled_view_stores.m`). A function's write of such a view at + a texture slot asks the usage Metal reports, and is `Refusal::TextureUsageAtSlot` (Texture + slots and usage, below), where plain Metal writes the texels through the swizzle. - A kernel, a vertex function or a fragment function samples a 2D texture slot through a bicubic constexpr sampler, at normalized or at pixel coordinates, by the arm its slot's pushed word selects, which the runtime writes from the bound texture's format and swizzle @@ -2925,7 +2941,9 @@ while the driver's exit handler waited on a lock, with every runtime thread alre - `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 + bind as Metal's calls do; a dispatch whose kernel uses a texture slot whose texture lacks the + usage the kernel's access there asks is `Refusal::TextureUsageAtSlot` (Textures: Texture slots + and usage); `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 @@ -4793,7 +4811,9 @@ cache is answered compile-required on both. the floor's `maxColorAttachments` is 8. - An encoder holds Metal's render state: a pipeline state, the vertex and fragment functions' buffers (`set_vertex_buffer`, `set_vertex_bytes` and their offsets, the fragment's alike), - textures and sampler states, up to 16 viewports and scissor rectangles, the cull mode, the + textures (a draw whose function uses a texture slot whose texture lacks the usage its access + there asks is `Refusal::TextureUsageAtSlot`, Textures: Texture slots and usage) and sampler + states, up to 16 viewports and scissor rectangles, the cull mode, the front-facing winding, a depth-stencil state (`Device::new_depth_stencil_state`), the stencil references, the depth bias and the blend color, Metal's defaults where it set none. A vertex buffer slot serves the vertex function's buffer argument of its index and the vertex descriptor's layout of its diff --git a/crates/metal2vulkan-runtime/src/encoder.rs b/crates/metal2vulkan-runtime/src/encoder.rs index 39911cec5..8f98201e6 100644 --- a/crates/metal2vulkan-runtime/src/encoder.rs +++ b/crates/metal2vulkan-runtime/src/encoder.rs @@ -27,8 +27,9 @@ use crate::sampler::SamplerState; use crate::texture::{Region, StorageGap, Texture, TextureObject}; use ash::vk; use metal2vulkan::metal::format::{integer_channels_word, BICUBIC_TEXELS_UNBOUND}; +use metal2vulkan::metal::TextureUsage; use metal2vulkan::pipeline::{ - grid_tile, nonuniform_region_bytes, region_bytes, DEEP_THREADGROUP, GRID_TILE_THREADS, + grid_tile, nonuniform_region_bytes, region_bytes, Stage, DEEP_THREADGROUP, GRID_TILE_THREADS, }; use std::ops::BitOr; use std::sync::Arc; @@ -111,6 +112,26 @@ enum Slot { Bytes(Placed, u32), } +/// [`Refusal::TextureUsageAtSlot`] where a texture of `usage` (its usage as Metal answers it) is +/// bound at texture slot `index`, which a function of `stage` uses, and lacks the usage `asked` +/// that the function's access there asks (`crate::pipeline::slot_usage`). A dispatch and a draw +/// check each texture their functions' slots reach before they record anything. +pub(crate) fn check_usage( + stage: Stage, + index: u32, + asked: TextureUsage, + usage: TextureUsage, +) -> Result<(), Error> { + match usage.contains(asked) { + true => Ok(()), + false => Err(Error::Refused(Refusal::TextureUsageAtSlot { + stage, + index, + usage, + })), + } +} + /// [`Refusal::BufferOffsetAlignment`] where `buffer`, bound at `offset` to buffer argument /// `slot`, is at an address (its `gpuAddress` plus the offset) that is not a multiple of the /// alignment the module takes the argument to have, `mask + 1` (`FieldValue::BufferAddress`'s @@ -598,7 +619,9 @@ impl ComputeCommandEncoder { /// /// 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. + /// for a texture shaders reach through its views alone. A texture whose usage lacks what the + /// kernel's access at the slot asks binds, as Metal's does, and the dispatch is + /// [`Refusal::TextureUsageAtSlot`]. pub fn set_texture(&mut self, texture: Option<&Texture>, index: u32) -> Result<(), Error> { if index >= TEXTURE_SLOTS { return Err(Error::Refused(Refusal::TextureArgumentIndex { index })); @@ -980,7 +1003,9 @@ impl ComputeCommandEncoder { /// [`Refusal::BufferOffsetAlignment`] for a buffer the kernel reaches bound off the alignment /// its function holds it to, which nothing records (a function kernel's argument of 8 or 16 /// bound at a multiple of 4 runs on its kernel's variant that holds it to 4); - /// [`Gap::SwizzledReadWrite`]; [`Gap::OpenClLineTexture`]; [`Gap::BicubicTexels`]; + /// [`Gap::SwizzledReadWrite`]; [`Gap::OpenClLineTexture`]; [`Refusal::TextureUsageAtSlot`] + /// for a texture the kernel's slot reaches whose usage lacks what the kernel's access there + /// asks; [`Gap::BicubicTexels`]; /// [`Limit::LevelElements`] where no run of level elements holds a texture of several levels /// the kernel writes or reads at a level other than the constant 0; /// [`Limit::ThreadgroupMemory`], the imageblock's cells at the size @@ -1100,6 +1125,12 @@ impl ComputeCommandEncoder { return Err(Error::Unsupported(Gap::SwizzledReadWrite { slot })); } } + // Each texture the kernel's slots reach, of the usage its access there asks. + for &(slot, asked) in &inner.whole.interface.usages { + if let Some(Some(texture)) = self.textures.get(slot as usize) { + check_usage(Stage::Kernel, slot, asked, texture.metal_usage())?; + } + } let interface = &inner.whole.interface; // A 1D texture bound for a kernel of OpenCL mode, which reads each 1D image as the 2D // texture Apple's OpenCL binds for it and never a 1D one. diff --git a/crates/metal2vulkan-runtime/src/pipeline.rs b/crates/metal2vulkan-runtime/src/pipeline.rs index e76a9e6a5..5a3830797 100644 --- a/crates/metal2vulkan-runtime/src/pipeline.rs +++ b/crates/metal2vulkan-runtime/src/pipeline.rs @@ -78,13 +78,13 @@ use metal2vulkan::cache::Hash256; use metal2vulkan::device::{VkLimit, KERNEL_BUFFER_BINDINGS}; use metal2vulkan::error::{Capability, Error as TranslationError, Feature, Needs, Unsupported}; use metal2vulkan::library::{CompiledFunction, FunctionCompileKey}; -use metal2vulkan::metal::{BindingAccess, BindingType}; +use metal2vulkan::metal::{BindingAccess, BindingType, TextureUsage}; use metal2vulkan::pipeline::{ arena_bytes, threadgroup_constants, AccessSet, BindingSource, BufferKind, CombinedSampler, ComputePipeline, ComputePipelineDescriptor, DescriptorSetLayout, FunctionDescriptor, KernelVariant, LinkedFunctions, LoweringInputs, NumberedSampler, PrecisionNote, PushField, PushLoc, RuntimeFact, SamplerRef, SlotClass, SlotInterface, SlotRef, SpecKey, Stage, - StageEntries, StageSource, TextureAccess, GRID_TILE_THREADS, + StageEntries, StageSource, TextureAccess, TextureSlot, GRID_TILE_THREADS, }; use metal2vulkan::Budget; use std::ffi::CString; @@ -713,6 +713,9 @@ pub(crate) struct Interface { pub(crate) views: Vec, /// Each texture slot the kernel uses, with its access. pub(crate) textures: Vec<(u32, Access)>, + /// Each texture slot the kernel uses, with the usage Metal asks of the texture bound there + /// ([`slot_usage`]). + pub(crate) usages: Vec<(u32, TextureUsage)>, /// Each texture slot the kernel reads through its storage image (`TextureSlot::storage`, a /// `read_write` slot it reads), which takes no swizzle: a swizzled texture bound there is /// `Gap::SwizzledReadWrite`. @@ -1763,6 +1766,7 @@ pub(crate) fn interface( } let mut buffers = Vec::new(); let mut textures = Vec::new(); + let mut usages = Vec::new(); let mut storage_reads = Vec::new(); let mut formatless = Vec::new(); for (slot, used) in &translated.slots { @@ -1778,6 +1782,7 @@ pub(crate) fn interface( if buffer.kind == BufferKind::VisibleFunctionTable => {} (Stage::Kernel, SlotClass::Texture, SlotInterface::Texture(texture)) => { textures.push((slot.index, texture_access(texture.access))); + usages.push((slot.index, slot_usage(texture))); if texture.storage && texture.access.read { storage_reads.push(slot.index); } @@ -1996,6 +2001,7 @@ pub(crate) fn interface( buffers, views, textures, + usages, storage_reads, open_cl_mode, formatless, @@ -2095,6 +2101,22 @@ pub(crate) fn texture_access(used: TextureAccess) -> Access { } } +/// The usage Metal asks of the texture bound at a texture slot a function uses, by the access the +/// function declares there, whatever it does there (a size query alone included): `ShaderRead` +/// for a `sample` or `read` slot, `ShaderWrite` for a `write` or `read_write` slot +/// (`TextureSlot::storage`), and both for one it reads. Metal's validation stops a dispatch or a +/// draw whose texture lacks it (measured on an Apple M3 Max, macOS 27.0.1, jobs m3-max-78392 and +/// m3-max-78422), as [`Refusal::TextureUsageAtSlot`] does. A `read_write` slot the function only +/// writes asks `ShaderWrite` alone: the write reaches the texture through its storage element +/// alone. +pub(crate) fn slot_usage(slot: &TextureSlot) -> TextureUsage { + match (slot.storage, slot.access.read) { + (false, _) => TextureUsage::SHADER_READ, + (true, false) => TextureUsage::SHADER_WRITE, + (true, true) => TextureUsage::SHADER_READ | TextureUsage::SHADER_WRITE, + } +} + /// The hazard table's access of a slot a function reads, writes, or both. pub(crate) fn access(used: AccessSet) -> Access { match (used.read, used.write) { diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs index 427dbda2a..9dd066add 100644 --- a/crates/metal2vulkan-runtime/src/render.rs +++ b/crates/metal2vulkan-runtime/src/render.rs @@ -82,8 +82,8 @@ use crate::hazard::Access; use crate::kernel_map::KernelKey; use crate::pass::Pass; use crate::pipeline::{ - access, loaded, lower, made_through_cache, number_samplers, of_the_device, texture_access, - translating, FieldValue, Numbered, + access, loaded, lower, made_through_cache, number_samplers, of_the_device, slot_usage, + texture_access, translating, FieldValue, Numbered, }; use crate::reaper::{Free, Job}; use crate::ring::CHUNK; @@ -92,7 +92,7 @@ use crate::times::host_time; use ash::vk; use metal2vulkan::cache::{Hash256, VariantKey}; use metal2vulkan::device::EnabledCaps; -use metal2vulkan::metal::{BindingAccess, PrimitiveTopologyClass}; +use metal2vulkan::metal::{BindingAccess, PrimitiveTopologyClass, TextureUsage}; use metal2vulkan::pipeline::{ specialize, BufferKind, CombinedSampler, NumberedSampler, PrecisionNote, PushField, PushLoc, RenderPipeline, RenderPipelineDescriptor, RenderPipelineKeys, RuntimeFact, SamplerRef, @@ -231,6 +231,9 @@ pub(crate) struct Draws { pub(crate) buffers: Vec<(usize, u32, Access)>, /// Each texture slot a function uses, likewise. pub(crate) textures: Vec<(usize, u32, Access)>, + /// Each texture slot a function uses, with its function and the usage Metal asks of the + /// texture bound there (`crate::pipeline::slot_usage`). + pub(crate) usages: Vec<(usize, u32, TextureUsage)>, /// Each texture slot a function reads through its storage image (`TextureSlot::storage`, a /// `read_write` slot it reads), with its function, which takes no swizzle: a swizzled texture /// bound there is `Gap::SwizzledReadWrite`. @@ -838,6 +841,7 @@ fn draws( } let mut buffers = Vec::new(); let mut textures = Vec::new(); + let mut usages = Vec::new(); let mut storage_reads = Vec::new(); let mut formatless = Vec::new(); for (slot, used) in &translated.slots { @@ -850,6 +854,7 @@ fn draws( } (SlotClass::Texture, SlotInterface::Texture(texture)) => { textures.push((at, slot.index, texture_access(texture.access))); + usages.push((at, slot.index, slot_usage(texture))); if texture.storage && texture.access.read { storage_reads.push((at, slot.index)); } @@ -1060,6 +1065,7 @@ fn draws( let draws = Draws { buffers, textures, + usages, storage_reads, formatless, own_pixel, diff --git a/crates/metal2vulkan-runtime/src/render_encoder.rs b/crates/metal2vulkan-runtime/src/render_encoder.rs index 28225d653..fb4bdfe6e 100644 --- a/crates/metal2vulkan-runtime/src/render_encoder.rs +++ b/crates/metal2vulkan-runtime/src/render_encoder.rs @@ -91,8 +91,8 @@ use crate::counters::Metric; use crate::depth_stencil::{BitDraws, DepthStencilState, Face, Tests, EVERY_FRAGMENT}; use crate::device::Core; use crate::encoder::{ - check_alignment, merge, Declared, Resource, ResourceUsage, BUFFER_SLOTS, SAMPLER_SLOTS, - TEXTURE_SLOTS, + check_alignment, check_usage, merge, Declared, Resource, ResourceUsage, BUFFER_SLOTS, + SAMPLER_SLOTS, TEXTURE_SLOTS, }; use crate::error::{DrawKind, Error, Gap, IndirectCommandsKind, Limit, Refusal, RenderPassKind}; use crate::fence::Fence; @@ -122,7 +122,7 @@ use metal2vulkan::metal::state::{self, input_assembly, DepthStencilDescriptor}; use metal2vulkan::metal::{ CullMode, DepthClipMode, IndexType, PrimitiveType, TriangleFillMode, Winding, }; -use metal2vulkan::pipeline::PrecisionNote; +use metal2vulkan::pipeline::{PrecisionNote, Stage}; use std::sync::Arc; /// `MTLViewport`: an origin and size in pixels, and a depth range. @@ -294,6 +294,7 @@ impl Facts { /// /// Err: [`Refusal::VertexBufferOffset`]; [`Refusal::NoBufferAtIndex`] for a vertex buffer the /// vertex descriptor reads that is not bound; [`Gap::SwizzledReadWrite`]; + /// [`Refusal::TextureUsageAtSlot`]; /// [`Gap::BicubicTexels`]; [`Gap::StorageElement`]; [`Limit::LevelElements`]. fn find( &mut self, @@ -318,6 +319,16 @@ impl Facts { return Err(Error::Unsupported(Gap::SwizzledReadWrite { slot })); } } + // Each texture the functions' slots reach, of the usage their access there asks. + for &(table, slot, asked) in &draws.usages { + if let Some(texture) = tables[table].texture(slot) { + let stage = match table { + VERTEX => Stage::Vertex, + _ => Stage::Fragment, + }; + check_usage(stage, slot, asked, texture.metal_usage())?; + } + } // A bicubic sample's texture, of a format the filter's treatment is known for; a content // with no bicubic slot has none to look at. for &(table, slot) in &draws.bicubic { @@ -1339,7 +1350,9 @@ impl RenderCommandEncoder { /// /// 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. + /// for a texture shaders reach through its views alone. A texture whose usage lacks what the + /// function's access at the slot asks binds, as Metal's does, and the draw is + /// [`Refusal::TextureUsageAtSlot`]. pub fn set_vertex_texture( &mut self, texture: Option<&Texture>, @@ -2394,8 +2407,9 @@ impl RenderCommandEncoder { /// [`Refusal::BufferOffsetAlignment`] for a buffer a function reaches bound off its argument's /// alignment, checked before anything is recorded; [`Refusal::StencilAttachment`]; /// [`Gap::Draw`] for what the runtime does not build, naming - /// it; [`Gap::SwizzledReadWrite`]; [`Error::Vulkan`] where a pipeline cannot be made or the - /// command buffer cannot record. + /// it; [`Gap::SwizzledReadWrite`]; [`Refusal::TextureUsageAtSlot`] for a texture a + /// function's slot reaches whose usage lacks what the function's access there asks; + /// [`Error::Vulkan`] where a pipeline cannot be made or the command buffer cannot record. pub fn draw_primitives( &mut self, primitive_type: PrimitiveType, diff --git a/crates/metal2vulkan-runtime/src/tests/compute.rs b/crates/metal2vulkan-runtime/src/tests/compute.rs index 081142653..9cc6f0b29 100644 --- a/crates/metal2vulkan-runtime/src/tests/compute.rs +++ b/crates/metal2vulkan-runtime/src/tests/compute.rs @@ -43,6 +43,7 @@ pub(crate) mod on_device { buffers, views: Vec::new(), textures: Vec::new(), + usages: Vec::new(), storage_reads: Vec::new(), open_cl_mode: false, formatless: Vec::new(), diff --git a/crates/metal2vulkan-runtime/src/tests/indirect.rs b/crates/metal2vulkan-runtime/src/tests/indirect.rs index 696e73549..177b4a0fb 100644 --- a/crates/metal2vulkan-runtime/src/tests/indirect.rs +++ b/crates/metal2vulkan-runtime/src/tests/indirect.rs @@ -70,6 +70,7 @@ fn draws(fields: Vec<(DrawPlace, usize, FieldValue)>) -> Draws { Draws { buffers: Vec::new(), textures: Vec::new(), + usages: Vec::new(), storage_reads: Vec::new(), formatless: Vec::new(), own_pixel: Vec::new(), diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs index bb2b91f2f..a53b862c5 100644 --- a/crates/metal2vulkan-runtime/src/texture.rs +++ b/crates/metal2vulkan-runtime/src/texture.rs @@ -4592,9 +4592,13 @@ impl Texture { /// Apple defines the swizzle for sampling and reading. The view's sampled element (`sample`, /// and `read` of an `access::read` texture) takes the combined swizzle. Its storage element /// and its attachment views map each channel to itself, as Vulkan requires of a storage image - /// and of an attachment: a render pass and a shader's write store the texels unswizzled. A - /// slot that reads through the storage element, `access::read_write`, would read them - /// unswizzled: a draw or dispatch that binds the view there is [`Gap::SwizzledReadWrite`]. + /// and of an attachment: a render pass stores the texels unswizzled. A slot that reads + /// through the storage element, `access::read_write`, would read them unswizzled: a draw or + /// dispatch that binds the view there is [`Gap::SwizzledReadWrite`]. Metal answers the view's + /// usage as `ShaderRead` and `PixelFormatView` alone, and its validation stops a function's + /// write of it, where plain Metal writes the texels through the swizzle (measured on an Apple + /// M3 Max, macOS 27.0.1, job m3-max-78422): a draw or dispatch whose function writes the view + /// at a slot is [`Refusal::TextureUsageAtSlot`]. /// /// Err: as [`Texture::new_texture_view`]; [`Gap::TextureFormat`] for a swizzle source this /// crate does not know. @@ -4941,7 +4945,9 @@ impl Texture { /// 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. + /// ([`Refusal::TextureWithoutIndex`]); a texture no shader reaches binds, and a dispatch or + /// draw whose function uses its slot is [`Refusal::TextureUsageAtSlot`]; nothing refuses its + /// ID written into bytes. pub fn gpu_resource_id(&self) -> ResourceId { TextureObject::gpu_resource_id(self) } @@ -6086,6 +6092,21 @@ pub(crate) trait TextureObject { TextureInner::reads(self.object()) != TextureSwizzleChannels::DEFAULT } + /// The usage Metal answers of it, which a function's use of it at a texture slot asks + /// (`crate::pipeline::slot_usage`, [`Refusal::TextureUsageAtSlot`]): its descriptor's, Metal's + /// unknown usage as the four uses Metal answers for it, and of one that reads through a + /// swizzle other than each channel's own, its `ShaderRead` and `PixelFormatView` alone, as + /// Metal answers a view made with a swizzle (measured on an Apple M3 Max, macOS 27.0.1, job + /// m3-max-78422: `0x11` of such a view of a texture of `0x13` and of one of unknown usage; + /// a texture's own swizzle is made only without shader-write and render-target usage). + fn metal_usage(&self) -> TextureUsage { + let usage = usage_taken(&self.object().descriptor); + match self.swizzled() { + true => usage & (TextureUsage::SHADER_READ | TextureUsage::PIXEL_FORMAT_VIEW), + false => usage, + } + } + /// Its level count as a function's `get_num_mip_levels` of a storage texture and the bounds of /// its levels read it (`PushField::TextureLevels`): its own where shaders reach it at an /// index, else 0, as nil's, whose resource ID it pushes. diff --git a/docs/REFUSALS.md b/docs/REFUSALS.md index bb455b711..a30e5548c 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` | 88 | 87 | 0 | 0 | 0 | 0 | 0 | +| The runtime's `Refusal` | 88 | 88 | 0 | 0 | 0 | 0 | 0 | | The runtime's `Limit` | 21 | 21 | 0 | 0 | 0 | 0 | 0 | | The runtime's `Gap` | 31 | 31 | 0 | 0 | 0 | 0 | 0 | @@ -267,7 +267,7 @@ Columns: | `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 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)` | `-` | -| `TextureUsageAtSlot` | A dispatch or draw whose function uses a texture slot where the bound texture's usage lacks what the function's access there asks | every device (a public call's own check of its arguments) | `metal-measured`: an Apple M3 Max, macOS 27.0.1, jobs m3-max-78392 and m3-max-78422: `setTexture:atIndex:` binds the texture and plain Metal reads and writes its texels; Metal's validation asserts at the dispatch "Compute Function(k_read): reads texture (t[0]) whose usage (0x04) doesn't specify MTLTextureUsageShaderRead (0x01)", "writes texture (t[0]) whose usage (0x01) doesn't specify MTLTextureUsageShaderWrite (0x02)" for an `access::write` argument the function only asks the width of, and at the draw "Fragment Function(f_first): Shader reads texture (t[0]) whose usage (0x04) doesn't specify MTLTextureUsageShaderRead (0x01)" | 0 | `-` | +| `TextureUsageAtSlot` | A dispatch or draw whose function uses a texture slot where the bound texture's usage lacks what the function's access there asks | every device (a public call's own check of its arguments) | `metal-measured`: an Apple M3 Max, macOS 27.0.1, jobs m3-max-78392 and m3-max-78422: `setTexture:atIndex:` binds the texture and plain Metal reads and writes its texels; Metal's validation asserts at the dispatch "Compute Function(k_read): reads texture (t[0]) whose usage (0x04) doesn't specify MTLTextureUsageShaderRead (0x01)", "writes texture (t[0]) whose usage (0x01) doesn't specify MTLTextureUsageShaderWrite (0x02)" for an `access::write` argument the function only asks the width of, and at the draw "Fragment Function(f_first): Shader reads texture (t[0]) whose usage (0x04) doesn't specify MTLTextureUsageShaderRead (0x01)" | 1: `crates/metal2vulkan-runtime/src/encoder.rs (check_usage)` | `-` | | `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)` | `-` | @@ -567,6 +567,7 @@ Each refusal's sites, with the function that builds it. - `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::TextureUsageAtSlot`: `crates/metal2vulkan-runtime/src/encoder.rs (check_usage)` - `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)` diff --git a/docs/RESOURCE-ID.md b/docs/RESOURCE-ID.md index bcac37ce7..5efd8204a 100644 --- a/docs/RESOURCE-ID.md +++ b/docs/RESOURCE-ID.md @@ -26,12 +26,21 @@ A texture of any usage may be placed at any index of the texture class. A textur element (shader-write or unknown usage, of a format the device stores through a storage image declared without a format) writes the element of both image arrays at its index; any other texture writes its sampled-image element and leaves its storage-image element null. A colour -render target made for neither shader reads nor writes leaves both null, so a slot that binds it -reads nil; a draw that reads it as an attachment of its own pass reads the runtime's second element -at its index plus 65,536, which no resource ID reaches. A texture no shader reaches, a depth or -stencil target made for neither, has no index. A texture view takes its own index of the texture class. A texture +render target made for neither shader reads nor writes leaves both null; a draw that reads it as an +attachment of its own pass (`[[color(n)]]`) reads the runtime's second element at its index plus +65,536, which no resource ID reaches. A texture no shader reaches, a depth or stencil target made +for neither, has no index. A texture view takes its own index of the texture class. A texture buffer has no index: it is a typed refusal under these domains. +A slot reaches its texture's elements only where the texture's usage holds what the function's +access there asks, as Metal's validation asks it: `ShaderRead` for a `sample` or `read` slot, a +size query alone included, `ShaderWrite` for a `write` or `read_write` slot, and both for a +`read_write` slot the function reads (Metal's unknown usage takes every access; a view made with +a swizzle answers `ShaderRead` and `PixelFormatView` alone). A runtime refuses a dispatch or draw +whose function uses a slot whose texture lacks it (the runtime's +`Refusal::TextureUsageAtSlot`): such a slot would read element 0's or a null element's zeros and +drop its writes, where plain Metal reads and writes the texture. + An index has one owner, the object placed there last, as Metal's later texture at a resource ID takes the ID. A texture or a sampler placed at an index another object holds, a live one or one the program released that work in flight still holds, writes its elements and owns the index at