From b676eda182d1f9308805109032603fc68ee460ab Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 19 Nov 2026 20:35:15 -0800 Subject: [PATCH] rt: a colour render target takes an index, and a draw reads it as an attachment as it reads a shader-read one Metal fetches any render target, and its header gives every texture a handle with no tie to usage (MTLTexture.h: gpuResourceID, "Handle of the GPU resource suitable for storing in an Argument Buffer"). rt took an index only for shader-read or shader-write usage, so a colour target made for rendering alone had no second element and every fetch from it was Gap::Draw(FetchedAttachment): guest 26's 4,792 refusals, all of that shape. - The index rule: shaders reach a texture made for shader reads or writes, or a colour render target (render-target usage of a pixel format with no depth or stencil), which takes an index exactly where it does (Refusal::TextureIndex), at every maker: new_texture and the heap, buffer and Managed makers, views, drawables and surface planes. A depth or stencil target made for neither is unchanged. - A colour render target not read takes what a shader-read one takes for attachment reads: the sampled usage, and the feedback usage where the image format query offers both beside its usage; the shadow on a device without the loop; neither where the query refuses them, and its draws that read it stay Gap::Draw. Its home stays the attachment layout, so a pass that does not read it records no move of it, and a fetching pass moves it home, feedback, home with the existing move code. - Only its second element is written: its sampled-texture element stays the null one, so a slot that binds it reads nil as before. place_texture takes the view its second element names in place of a flag. - A linear image in place over a buffer takes no sampled usage for a target shaders do not read. - The in-tree consumers take the rule: the replay player's texture class, the harness's and the conformance suite's rt side. rt's own tests make their unindexed colour targets with new_texture_for_views, and give indices where a test needs new_texture's path. - docs/RESOURCE-ID.md, the runtime README and Refusal::TextureIndex's doc say the rule; docs/REFUSALS.md is regenerated. Also rides along, from the planner, in the README's known gaps of memory blocks: a block freed before a submission used it waits on the block-maker thread for its bind, and a ring take that needs a new ring block can wait behind it. Verified: see the check job named in the landing. --- .../metal2vulkan-conformance/src/rt_side.rs | 28 ++-- .../src/rt_side/heap_sizes.rs | 2 +- .../src/rt_side/render.rs | 5 +- .../src/rt_side/states.rs | 8 +- .../src/rt_side/work.rs | 9 +- crates/metal2vulkan-harness/src/rt.rs | 17 +- crates/metal2vulkan-rt/README.md | 45 +++-- crates/metal2vulkan-rt/src/arrays.rs | 12 +- crates/metal2vulkan-rt/src/error.rs | 16 +- crates/metal2vulkan-rt/src/render_encoder.rs | 8 +- crates/metal2vulkan-rt/src/tests/ahead.rs | 2 +- crates/metal2vulkan-rt/src/tests/blit.rs | 5 +- crates/metal2vulkan-rt/src/tests/command.rs | 10 +- crates/metal2vulkan-rt/src/tests/copy_over.rs | 20 ++- crates/metal2vulkan-rt/src/tests/draw.rs | 5 +- crates/metal2vulkan-rt/src/tests/fence.rs | 2 +- crates/metal2vulkan-rt/src/tests/heap.rs | 13 +- crates/metal2vulkan-rt/src/tests/import.rs | 2 +- crates/metal2vulkan-rt/src/tests/managed.rs | 19 ++- crates/metal2vulkan-rt/src/tests/mipmaps.rs | 2 +- crates/metal2vulkan-rt/src/tests/over.rs | 21 ++- crates/metal2vulkan-rt/src/tests/pass.rs | 10 +- crates/metal2vulkan-rt/src/tests/present.rs | 49 +++--- crates/metal2vulkan-rt/src/tests/resolve.rs | 6 +- crates/metal2vulkan-rt/src/tests/sampler.rs | 4 +- crates/metal2vulkan-rt/src/tests/sites.rs | 3 +- crates/metal2vulkan-rt/src/tests/spill.rs | 2 +- .../metal2vulkan-rt/src/tests/take_split.rs | 4 +- crates/metal2vulkan-rt/src/tests/texture.rs | 8 +- crates/metal2vulkan-rt/src/tests/view.rs | 6 +- crates/metal2vulkan-rt/src/texture.rs | 158 ++++++++++++------ .../src/replay/new_runtime/mod.rs | 19 ++- docs/REFUSALS.md | 2 +- docs/RESOURCE-ID.md | 9 +- 34 files changed, 324 insertions(+), 207 deletions(-) diff --git a/crates/metal2vulkan-conformance/src/rt_side.rs b/crates/metal2vulkan-conformance/src/rt_side.rs index 2f2411d3f..3de955dc6 100644 --- a/crates/metal2vulkan-conformance/src/rt_side.rs +++ b/crates/metal2vulkan-conformance/src/rt_side.rs @@ -203,12 +203,15 @@ impl RtDevice { } } - /// The next texture index ([`RtDevice::next_index`]) for a texture of `usage` where a shader - /// reaches it (shader read or write, or no bit, which the runtime takes as every usage), and + /// The next texture index ([`RtDevice::next_index`]) for a texture of `pf` and `usage` where a + /// shader reaches it (shader read or write, no bit, which the runtime takes as every usage, or + /// a colour render target, which a fragment function reads through its attachment), and /// `None` where none does, as the runtime's texture makers take an index. - fn texture_index(&self, usage: rt::TextureUsage) -> Option { + fn texture_index(&self, pf: rt::PixelFormat, usage: rt::TextureUsage) -> Option { let reached = rt::TextureUsage::SHADER_READ | rt::TextureUsage::SHADER_WRITE; - (usage.is_empty() || usage.intersects(reached)) + let color_target = usage.contains(rt::TextureUsage::RENDER_TARGET) + && !metal2vulkan::metal::format::is_depth_stencil(pf); + (usage.is_empty() || usage.intersects(reached) || color_target) .then(|| self.next_index(rt::IndexClass::Texture)) } @@ -772,7 +775,7 @@ impl Side for RtSide<'_> { query: &crate::texture_query::HeapTextureQuery, ) -> Answer> { let descriptor = queried(query); - let index = self.on.texture_index(descriptor.usage); + let index = (self.on).texture_index(descriptor.pixel_format, descriptor.usage); let texture = (self.on.device.new_texture(&descriptor, index)) .map_err(|error| Failure::of_rt(&error))?; let facts = texture_facts(&texture); @@ -791,7 +794,7 @@ impl Side for RtSide<'_> { let (buffer, _blocks) = work::buffer_of(self.on, request.buffer_length, request.buffer_storage)?; let descriptor = queried(&request.descriptor); - let index = self.on.texture_index(descriptor.usage); + let index = (self.on).texture_index(descriptor.pixel_format, descriptor.usage); let made = buffer.new_texture(&descriptor, index, request.offset, request.bytes_per_row); let texture = made.map_err(|error| Failure::of_rt(&error))?; let facts = texture_facts(&texture); @@ -834,7 +837,7 @@ impl Side for RtSide<'_> { /// does not make is its typed error, recorded as the failure it is. fn makes_texture(&mut self, query: &crate::texture_query::HeapTextureQuery) -> Answer { let descriptor = queried(query); - let index = self.on.texture_index(descriptor.usage); + let index = (self.on).texture_index(descriptor.pixel_format, descriptor.usage); let texture = (self.on.device.new_texture(&descriptor, index)) .map_err(|error| Failure::of_rt(&error))?; self.encoder_notes.extend(texture.precision_note()); @@ -1048,8 +1051,8 @@ impl Side for RtSide<'_> { }; let levels = range(request.first_level, request.level_count, "a level")?; let slices = range(request.first_slice, request.slice_count, "a slice")?; - let index = self.on.texture_index(texture.descriptor().usage); let (format, texture_type) = (request.format, request.texture_type); + let index = self.on.texture_index(format, texture.descriptor().usage); let view = match request.call { ViewCall::PixelFormat | ViewCall::LevelsSlices => { texture.new_texture_view(format, texture_type, levels, slices, index) @@ -1190,9 +1193,7 @@ impl Side for RtSide<'_> { ); descriptor.usage = crate::runtime_side::usage_of(spec.role); descriptor.storage_mode = rt::StorageMode::Shared; - let reached = rt::TextureUsage::SHADER_READ | rt::TextureUsage::SHADER_WRITE; - let index = (descriptor.usage.intersects(reached)) - .then(|| self.on.next_index(rt::IndexClass::Texture)); + let index = (self.on).texture_index(descriptor.pixel_format, descriptor.usage); let of_rt = |error: rt::Error| Failure::of_rt(&error); let texture = (self.on.device.new_texture(&descriptor, index)).map_err(of_rt)?; if !bytes.is_empty() { @@ -1235,7 +1236,8 @@ impl Side for RtSide<'_> { false => rt::StorageMode::Shared, }; descriptor.swizzle = request.swizzle; - let index = |usage| self.on.texture_index(usage); + let pf = descriptor.pixel_format; + let index = |usage| self.on.texture_index(pf, usage); let of_rt = |error: rt::Error| Failure::of_rt(&error); let texture = match request.over { None => self @@ -1747,7 +1749,7 @@ impl Side for RtSide<'_> { let mut descriptor = rt::TextureDescriptor::new(rt::TextureType::Type2D, format, width, height); descriptor.usage = rt::TextureUsage::SHADER_READ | rt::TextureUsage::SHADER_WRITE; - let index = self.on.texture_index(descriptor.usage); + let index = (self.on).texture_index(descriptor.pixel_format, descriptor.usage); let (offset, bytes_per_row) = over.planes[0]; let texture = (self .held(buffer)? diff --git a/crates/metal2vulkan-conformance/src/rt_side/heap_sizes.rs b/crates/metal2vulkan-conformance/src/rt_side/heap_sizes.rs index 8cd73c578..4c4d5e950 100644 --- a/crates/metal2vulkan-conformance/src/rt_side/heap_sizes.rs +++ b/crates/metal2vulkan-conformance/src/rt_side/heap_sizes.rs @@ -49,7 +49,7 @@ pub(super) fn hold( ) })?; let (_block, heap) = work::heap_of(on, heap_size, Storage::Private)?; - let index = on.texture_index(descriptor.usage); + let index = on.texture_index(descriptor.pixel_format, descriptor.usage); let texture = (on.device.new_heap_texture(descriptor, index, heap, align)) .map_err(|error| on.failure(&error))?; round_trip(on, queue, descriptor, &texture) diff --git a/crates/metal2vulkan-conformance/src/rt_side/render.rs b/crates/metal2vulkan-conformance/src/rt_side/render.rs index 744699e40..a6d6863b9 100644 --- a/crates/metal2vulkan-conformance/src/rt_side/render.rs +++ b/crates/metal2vulkan-conformance/src/rt_side/render.rs @@ -167,7 +167,7 @@ impl RtSide<'_> { if let Some(why) = over.unsafe_stride() { return Err(Failure::named("WouldEndMetalsProcess", why)); } - let index = self.on.texture_index(usage); + let index = self.on.texture_index(spec.format, usage); let buffer = self.held(over.buffer)?; let texture = (buffer.new_texture(&descriptor, index, over.offset, over.bytes_per_row)) .map_err(of_rt)?; @@ -191,8 +191,7 @@ impl RtSide<'_> { } descriptor.sample_count = spec.samples; } - let index = (spec.shader_read || spec.shader_write || spec.unknown_usage) - .then(|| self.on.next_index(rt::IndexClass::Texture)); + let index = self.on.texture_index(spec.format, usage); let texture = (self.on.device.new_texture(&descriptor, index)).map_err(of_rt)?; Ok(self.keep_texture(texture)) } diff --git a/crates/metal2vulkan-conformance/src/rt_side/states.rs b/crates/metal2vulkan-conformance/src/rt_side/states.rs index d1d54d084..a890f861f 100644 --- a/crates/metal2vulkan-conformance/src/rt_side/states.rs +++ b/crates/metal2vulkan-conformance/src/rt_side/states.rs @@ -138,7 +138,8 @@ impl RtSide<'_> { rt::TextureDescriptor::new(rt::TextureType::Type2D, fill.format, width, height); target.usage = rt::TextureUsage::RENDER_TARGET; target.storage_mode = rt::StorageMode::Private; - let target = on.device.new_texture(&target, None).map_err(of_rt)?; + let index = on.texture_index(target.pixel_format, target.usage); + let target = on.device.new_texture(&target, index).map_err(of_rt)?; let bound = (fill.buffers.iter()) .map(|&handle| self.held(handle).cloned()) .collect::>>()?; @@ -207,17 +208,18 @@ impl RtSide<'_> { } /// [`crate::case::Side::render_target`]: a Shared 2D render target of the mesh family's - /// format, which no shader reaches. + /// format, at an index as the runtime takes one for a colour render target. pub(super) fn mesh_target(&mut self, width: u32, height: u32) -> Answer { let format = rt::PixelFormat::RGBA32Uint; let mut descriptor = rt::TextureDescriptor::new(rt::TextureType::Type2D, format, width, height); descriptor.usage = rt::TextureUsage::RENDER_TARGET; descriptor.storage_mode = rt::StorageMode::Shared; + let index = (self.on).texture_index(descriptor.pixel_format, descriptor.usage); let texture = self .on .device - .new_texture(&descriptor, None) + .new_texture(&descriptor, index) .map_err(of_rt)?; Ok(keep(&mut self.textures, texture)) } diff --git a/crates/metal2vulkan-conformance/src/rt_side/work.rs b/crates/metal2vulkan-conformance/src/rt_side/work.rs index 9e669804a..92339935f 100644 --- a/crates/metal2vulkan-conformance/src/rt_side/work.rs +++ b/crates/metal2vulkan-conformance/src/rt_side/work.rs @@ -310,13 +310,14 @@ fn make(on: &RtDevice, made: &[Made], resource: &Resource) -> Answer { } Resource::Texture { spec } => { let d = descriptor(spec)?; - let texture = (device.new_texture(&d, on.texture_index(d.usage))).map_err(of_rt)?; + let texture = (device.new_texture(&d, on.texture_index(d.pixel_format, d.usage))) + .map_err(of_rt)?; Ok(Made::Texture(texture)) } Resource::HeapTexture { heap, spec, offset } => { let d = descriptor(spec)?; let range = heap_at(*heap)?; - let index = on.texture_index(d.usage); + let index = on.texture_index(d.pixel_format, d.usage); let texture = (device.new_heap_texture(&d, index, range, *offset)).map_err(of_rt)?; Ok(Made::Texture(texture)) } @@ -327,7 +328,7 @@ fn make(on: &RtDevice, made: &[Made], resource: &Resource) -> Answer { bytes_per_row, } => { let d = descriptor(spec)?; - let index = on.texture_index(d.usage); + let index = on.texture_index(d.pixel_format, d.usage); let texture = (buffer_at(*buffer)?.new_texture(&d, index, *offset, *bytes_per_row)) .map_err(of_rt)?; Ok(Made::Texture(texture)) @@ -352,7 +353,7 @@ fn make(on: &RtDevice, made: &[Made], resource: &Resource) -> Answer { let (level, slice) = (narrow(*level, "a level")?, narrow(*slice, "a slice")?); let levels = level..level + narrow(*level_count, "a level count")?; let slices = slice..slice + narrow(*slice_count, "a slice count")?; - let index = on.texture_index(parent.descriptor().usage); + let index = on.texture_index(format, parent.descriptor().usage); let view = (parent.new_texture_view(format, kind, levels, slices, index)).map_err(of_rt)?; Ok(Made::Texture(view)) diff --git a/crates/metal2vulkan-harness/src/rt.rs b/crates/metal2vulkan-harness/src/rt.rs index 1f666fbfd..38d300cb1 100644 --- a/crates/metal2vulkan-harness/src/rt.rs +++ b/crates/metal2vulkan-harness/src/rt.rs @@ -587,7 +587,7 @@ impl crate::ComputeDevice for Device { Storage::Managed => rt::StorageMode::Managed, Storage::Private => rt::StorageMode::Private, }; - match class(texture.usage) { + match class(texture.pixel_format, texture.usage) { Some(class) => self.place(class, |index| { self.device.new_texture(&descriptor, Some(index)) }), @@ -606,7 +606,7 @@ impl crate::ComputeDevice for Device { let view = |index| { parent.new_texture_view(pixel_format, texture_type, 0..1, slices.clone(), index) }; - match class(texture.usage) { + match class(pixel_format, texture.usage) { Some(class) => self.place(class, |index| view(Some(index))), None => view(None).map_err(error), } @@ -620,11 +620,16 @@ impl crate::ComputeDevice for Device { } } -/// The index class of a texture of `usage`, where shaders reach it: unknown usage is every -/// usage, as the runtime takes it; a texture no shader reaches takes no index. -fn class(usage: rt::TextureUsage) -> Option { +/// The index class of a texture of `pf` and `usage`, where shaders reach it, as the runtime takes +/// it: unknown usage is every usage; shader-read or shader-write usage, or a colour render target, +/// which a fragment function reads through its attachment; a depth or stencil target shaders +/// neither read nor write takes no index. +fn class(pf: rt::PixelFormat, usage: rt::TextureUsage) -> Option { let reached = rt::TextureUsage::SHADER_READ | rt::TextureUsage::SHADER_WRITE; - (usage.is_empty() || usage.intersects(reached)).then_some(rt::IndexClass::Texture) + let color_target = usage.contains(rt::TextureUsage::RENDER_TARGET) + && !metal2vulkan::metal::format::is_depth_stencil(pf); + (usage.is_empty() || usage.intersects(reached) || color_target) + .then_some(rt::IndexClass::Texture) } impl crate::Texture for rt::Texture { diff --git a/crates/metal2vulkan-rt/README.md b/crates/metal2vulkan-rt/README.md index d790b347c..871d07ea8 100644 --- a/crates/metal2vulkan-rt/README.md +++ b/crates/metal2vulkan-rt/README.md @@ -779,6 +779,8 @@ planes' first use, so `Surface::wait_until_ready()` returns at once. still fault the device. - An app buffer too large for the consumer's map budget is copied, on drivers whose dma-buf query offers a non-host-visible type first. +- A block freed before a submission used it waits on the block-maker thread for its bind, and a + ring take that needs a new ring block can wait behind it. - rt caps a buffer at 2 GiB (`MAX_BUFFER_LENGTH`), where the M3's Metal allows 38.88 GiB (`maxBufferLength`, 41,747,087,360 bytes), about twenty times as much: shaders reach a buffer's bytes by 32-bit offsets from its bind address, and lifting the cap would put 64-bit offset @@ -905,8 +907,8 @@ made, with no lock, and never rewritten for layouts. elements: element `i + 65,536` is texture `i`'s second element, which a draw pushes in place of the texture's own index while it reads an attachment of its pass through the texture. Through the attachment feedback loop it is the texture's view in the feedback layout, written with - element `i` for a texture whose image a pass may move there (a shader-read colour render target - not in place over a buffer); on a device without the loop it is the texture's view of its image's + element `i` for a texture whose image a pass may move there (a colour render target not in place + over a buffer); on a device without the loop it is the texture's view of its image's shadow (Render encoders), written at the first draw that reads through it where the texture still owns index `i`. It is a null descriptor otherwise, and no grant reaches it. - A shader masks the index of a texture ID it reads from an argument buffer to its low 16 bits, @@ -954,17 +956,22 @@ made, with no lock, and never rewritten for layouts. (`requiresDedicatedAllocation`; preferring one is no reason), where it is larger than a Private block (64 MiB), or where the blocks' memory type does not serve it. A place is free again once the image is freed; one empty texture block is kept, and another that empties goes back to the - pool. `index` is given exactly where shaders reach the texture, shader-read or shader-write - usage (`Refusal::TextureIndex` otherwise), from a live `IndexClass::Texture` grant, whatever + pool. `index` is given exactly where shaders reach the texture (`Refusal::TextureIndex` + otherwise): shader-read or shader-write usage, or render-target usage of a colour pixel format + (one with no depth or stencil), which a fragment function reads through its attachment, as + Metal gives every texture a `gpuResourceID`; from a live `IndexClass::Texture` grant, whatever the usage. `TextureUsage` with no bits is Metal's unknown usage: shader read and write, render target and pixel-format view. -- The image's usage is the descriptor's plus transfer source and destination. Its home layout is - general where it has a storage element, shader-read-only where shaders only read it, and the - attachment layout where only render passes use it. Its elements of set 1 are written before the - call returns, with that layout, and never rewritten. The image moves from undefined to home in - the upload of the next commit, ahead of every command buffer committed after the call, or ahead - of its first copy; until then nothing holds it, so a texture - released first is freed at once, with nothing submitted. +- The image's usage is the descriptor's plus transfer source and destination, and, for a colour + render target where the device reads attachments, sampled, with the feedback usage where it has + the attachment feedback loop (Render encoders). Its home layout is general where it has a storage + element, shader-read-only where shaders only read it, and the attachment layout where only render + passes use it. Its elements of set 1 are written before the call returns, with that layout, and + never rewritten; a colour render target shaders neither read nor write leaves both null, so a + slot that binds it reads Metal's nil texture, and writes only its second element (Set 1). The + image moves from undefined to home in the upload of the next commit, ahead of every command + buffer committed after the call, or ahead of its first copy; until then nothing holds it, so a + texture released first is freed at once, with nothing submitted. - A texture's image lists the formats Metal views the texture as that the device holds in a viewed form of the image format's compatibility class, and is then mutable (`VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT` with a `VkImageFormatListCreateInfo`, its own format @@ -2616,11 +2623,13 @@ or does not check is rebuilt from the generations' names, with young counted ful target, where the device reads attachments (Floor, Attachment reads): through the attachment feedback loop where the device has it, else through a copy. The texture reaches the attachment's level and layer, the fragment function reads it and nothing writes it, and its - image is a shader-read colour render target not in place over a buffer, which one attachment of - the pass renders: its home is shader-read-only, or general where it has a storage view (made for - shader writes, or of an unknown usage, where the device stores its format), and its image takes - the feedback usage where the device's image format query offers it beside the rest, the storage - usage included. Vulkan's feedback layout asks the attachment and sampled usages beside it and + image is a colour render target not in place over a buffer, which one attachment of the pass + renders: its home is general where it has a storage view (made for shader writes, or of an + unknown usage, where the device stores its format), shader-read-only where shaders read it, + else the attachment layout, and its image takes the sampled and feedback usages where the + device's image format query offers them beside the rest, the storage usage included; where it + refuses them, the image is made without them and a draw that reads it is `Gap::Draw`. A pass + that reads no attachment through it records no move of it. Vulkan's feedback layout asks the attachment and sampled usages beside it and bars none (VUID-VkImageMemoryBarrier2-srcQueueFamilyIndex-07006); in it the image is only rendered and sampled. Through the feedback loop, at the pass's first such draw the rendering ends, one barrier moves the whole image to the feedback layout (the attachment's level and layer @@ -2658,8 +2667,8 @@ or does not check is rebuilt from the generations' names, with young counted ful barrier, and it begins again), and through a copy copies what it reads into the shadow again: a pass of N fetching draws records N - 1 such barriers, or N - 1 copies. The texture is a 2D texture (a 2D multisample one in a multisampled pass) whose first level and slice the pass - renders, made with an index, of a shader-read colour render target's image, with a storage view - or not: a texture of its own image, or a surface plane's (Exported surfaces). + renders, made with an index, of a colour render target's image, read by shaders or not, with a + storage view or not: a texture of its own image, or a surface plane's (Exported surfaces). - `memory_barrier` after a draw of the encoder that wrote memory ends the rendering, records one barrier and begins the rendering again, loading what it stored. After draws that only rendered, in a pass that moved an image to the feedback layout, the barrier is owed to the next draw that diff --git a/crates/metal2vulkan-rt/src/arrays.rs b/crates/metal2vulkan-rt/src/arrays.rs index 040523eff..4b512e19b 100644 --- a/crates/metal2vulkan-rt/src/arrays.rs +++ b/crates/metal2vulkan-rt/src/arrays.rs @@ -348,8 +348,9 @@ impl Arrays { /// Places a texture at `index` ([`Grants::place`]) and writes its elements before another /// object can be placed there: the sampled-texture array's with `sampled` and its layout, a /// null descriptor where it is `None`, the storage-texture array's with `storage`, and with - /// `looped` the second element (`index` plus [`TEXTURES`]) with the sampled view in the - /// attachment feedback layout. A texture without a storage view, or without a second element + /// `looped` the second element (`index` plus [`TEXTURES`]) with that view, the texture's + /// sampled view, in the attachment feedback layout: a colour render target shaders do not + /// read has it there alone. A texture without a storage view, or without a second element /// written here, leaves that element null, as every element starts, unless it displaced a /// texture, which may have had one: then it writes that element null, so nothing stale /// answers there. A null second element is written shader-read-only on every device, as @@ -377,7 +378,7 @@ impl Arrays { Option<(vk::ImageView, vk::ImageLayout)>, Option, ), - looped: bool, + looped: Option, ) -> Result { let mut grants = lock(&self.grants); let Placed { owner, displaced } = grants.place(IndexClass::Texture, index)?; @@ -386,9 +387,8 @@ impl Arrays { } let storage = storage.or(displaced.then(vk::ImageView::null)); let second = match looped { - true => sampled - .map(|(view, _)| (view, vk::ImageLayout::ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT)), - false => (displaced && self.seconds).then(|| { + Some(view) => Some((view, vk::ImageLayout::ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT)), + None => (displaced && self.seconds).then(|| { ( vk::ImageView::null(), vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL, diff --git a/crates/metal2vulkan-rt/src/error.rs b/crates/metal2vulkan-rt/src/error.rs index 0c880ae1f..1633cf29c 100644 --- a/crates/metal2vulkan-rt/src/error.rs +++ b/crates/metal2vulkan-rt/src/error.rs @@ -413,9 +413,12 @@ pub enum Refusal { /// The first index of the grant where an object lives. index: u32, }, - /// A texture whose usage shaders reach (read or write) made without an index, or one of no - /// such usage made with one ([`Device::new_texture`](crate::Device::new_texture)). A texture - /// shaders reach only through its views is made without one + /// A texture shaders reach made without an index, or one they do not reach made with one + /// ([`Device::new_texture`](crate::Device::new_texture)). Shaders reach a texture made for + /// shader reads or writes, and a colour render target (a render-target usage of a pixel + /// format with no depth or stencil), which a fragment function reads through its attachment; + /// a depth or stencil target made for neither they do not. A texture shaders reach only + /// through its views is made without one /// ([`Device::new_texture_for_views`](crate::Device::new_texture_for_views)). Metal has no /// counterpart: Metal gives every texture a resource ID itself. /// @@ -1825,9 +1828,10 @@ impl fmt::Display for Refusal { f, "a texture no shader reaches takes no index, and {index} was given" ), - Refusal::TextureIndex { index: None } => { - f.write_str("a texture shaders read or write takes an index, and none was given") - } + Refusal::TextureIndex { index: None } => f.write_str( + "a texture shaders read or write, or a colour render target, takes an index, and \ + none was given", + ), Refusal::TextureZeroExtent => f.write_str("a texture has a width, height or depth of 0"), Refusal::TextureExtentForType => { f.write_str("the texture's height or depth is not 1 where its type needs 1") diff --git a/crates/metal2vulkan-rt/src/render_encoder.rs b/crates/metal2vulkan-rt/src/render_encoder.rs index 1c957534c..06e799ba7 100644 --- a/crates/metal2vulkan-rt/src/render_encoder.rs +++ b/crates/metal2vulkan-rt/src/render_encoder.rs @@ -24,10 +24,10 @@ //! //! A draw may read an attachment of its own pass through a texture, as Metal reads a render target //! (`floor::Capabilities::attachment_reads`): through the attachment feedback loop where the -//! device has it, else through a copy. Its image is a shader-read colour render target not in -//! place over a buffer, whose home is shader-read-only, or general where it has a storage view. -//! An attachment at home general renders there, and a move to the feedback layout and back is -//! from and to general. +//! device has it, else through a copy. Its image is a colour render target not in place over a +//! buffer, whose home is general where it has a storage view, shader-read-only where shaders read +//! it, else the attachment layout. An attachment renders at home general or attachment, and a move +//! to the feedback layout and back is from and to its home. //! //! Through the feedback loop: at the pass's first draw that reads the image, the rendering ends, //! one barrier moves the whole image to the feedback layout (its rendered level and layer from the diff --git a/crates/metal2vulkan-rt/src/tests/ahead.rs b/crates/metal2vulkan-rt/src/tests/ahead.rs index 64724c3c0..581b1fba5 100644 --- a/crates/metal2vulkan-rt/src/tests/ahead.rs +++ b/crates/metal2vulkan-rt/src/tests/ahead.rs @@ -57,7 +57,7 @@ mod on_device { d.usage = TextureUsage::RENDER_TARGET; d.storage_mode = StorageMode::Shared; let texture = - (device.new_texture(&d, None)).unwrap_or_else(|error| panic!("{name}: {error}")); + (device.new_texture_for_views(&d)).unwrap_or_else(|error| panic!("{name}: {error}")); calls.push(( "a copy of a texture's bytes", Box::new(move || { diff --git a/crates/metal2vulkan-rt/src/tests/blit.rs b/crates/metal2vulkan-rt/src/tests/blit.rs index d56ffac22..7cb44949c 100644 --- a/crates/metal2vulkan-rt/src/tests/blit.rs +++ b/crates/metal2vulkan-rt/src/tests/blit.rs @@ -1946,7 +1946,10 @@ pub(crate) mod on_device { bytes_per_row: 256, bytes_per_image: 0, }; - let texture = (device.new_managed_texture(&d, None, &named, &[place(0), place(1024)])) + // A colour render target takes an index. + (device.grant_indices(IndexClass::Texture, 4096, 1)).expect("a grant"); + let places = [place(0), place(1024)]; + let texture = (device.new_managed_texture(&d, Some(4096), &named, &places)) .unwrap_or_else(|error| panic!("{name}: {error}")); let queue = device.new_command_queue(4).expect("a queue"); let counters = device.counters(); diff --git a/crates/metal2vulkan-rt/src/tests/command.rs b/crates/metal2vulkan-rt/src/tests/command.rs index 5e46b107e..49ad19c23 100644 --- a/crates/metal2vulkan-rt/src/tests/command.rs +++ b/crates/metal2vulkan-rt/src/tests/command.rs @@ -2809,7 +2809,7 @@ pub(crate) mod on_device { let a = arena.buffer(); let mut d = TextureDescriptor::new(TextureType::Type2D, PixelFormat::RGBA8Unorm, 4, 4); d.usage = TextureUsage::RENDER_TARGET; - let texture = device.new_texture(&d, None).expect("a texture"); + let texture = device.new_texture_for_views(&d).expect("a texture"); let _grant = (device.grant_indices(IndexClass::Sampler, 1, 1)).expect("an index"); let state = (device.new_sampler_state(&SamplerDescriptor::new(), 1)).expect("a sampler state"); @@ -2912,7 +2912,7 @@ pub(crate) mod on_device { let event = shared_event(&device); let mut d = TextureDescriptor::new(TextureType::Type2D, PixelFormat::RGBA8Unorm, 4, 4); d.usage = TextureUsage::RENDER_TARGET; - let texture = device.new_texture(&d, None).expect("a texture"); + let texture = device.new_texture_for_views(&d).expect("a texture"); let queue = queue(&device); let command_buffer = queue.command_buffer().expect("a command buffer"); fill(&command_buffer, &a, 3); @@ -3002,8 +3002,8 @@ pub(crate) mod on_device { ), ("enqueue", refused(in_later.enqueue())), ( - "new_texture", - refused(in_device.new_texture(&d, None).map(drop)), + "new_texture_for_views", + refused(in_device.new_texture_for_views(&d).map(drop)), ), ]; let _ = answered.send(lines); @@ -3024,7 +3024,7 @@ pub(crate) mod on_device { "new_buffer in a Private block, not zeroed" | "new_buffer zeroed in a Shared block" | "enqueue" - | "new_texture" => "Ok(())".to_string(), + | "new_texture_for_views" => "Ok(())".to_string(), _ => refusal.clone(), }; assert_eq!(*answer, want, "{name}: {call}"); diff --git a/crates/metal2vulkan-rt/src/tests/copy_over.rs b/crates/metal2vulkan-rt/src/tests/copy_over.rs index 97f0f20ce..6f76b16eb 100644 --- a/crates/metal2vulkan-rt/src/tests/copy_over.rs +++ b/crates/metal2vulkan-rt/src/tests/copy_over.rs @@ -28,8 +28,8 @@ mod on_device { use crate::texture::TextureObject; use crate::{ Buffer, BufferLayout, CommandBuffer, CommandQueue, ComputePipelineDescriptor, Device, - DispatchType, FunctionDescriptor, Library, MemoryClass, PixelFormat, Region, Resource, - StageSource, StorageMode, Texture, TextureDescriptor, TexturePlace, TextureType, + DispatchType, FunctionDescriptor, IndexClass, Library, MemoryClass, PixelFormat, Region, + Resource, StageSource, StorageMode, Texture, TextureDescriptor, TexturePlace, TextureType, TextureUsage, }; use metal2vulkan_devtools::{llvm_assemble_text, AssembleError}; @@ -112,7 +112,7 @@ mod on_device { fn plain(name: &str, device: &Device, texels: &[u8]) -> Texture { let mut d = descriptor(); d.storage_mode = StorageMode::Shared; - let texture = (device.new_texture(&d, None)).unwrap_or_else(|e| panic!("{name}: {e}")); + let texture = (device.new_texture_for_views(&d)).unwrap_or_else(|e| panic!("{name}: {e}")); (texture.replace_region(whole(), 0, 0, texels.into(), ROW, 0)) .unwrap_or_else(|e| panic!("{name}: {e}")); texture @@ -166,7 +166,9 @@ mod on_device { write(&buffer, &now); let copied_over = || device.counters().gauges().textures_copied_over; let before = copied_over(); - let texture = (buffer.new_texture(&descriptor(), None, 0, pitch)) + // A colour render target takes an index. + (device.grant_indices(IndexClass::Texture, 4096, 1)).expect("a grant"); + let texture = (buffer.new_texture(&descriptor(), Some(4096), 0, pitch)) .unwrap_or_else(|e| panic!("{name}: {e}")); assert!( texture.copied_over().is_some(), @@ -279,8 +281,12 @@ mod on_device { (crate::tests::managed::on_device::new_managed(&device, cpu.id, offset, LEN, 0)) .unwrap_or_else(|e| panic!("{name}: {e}")) }; + // A colour render target takes an index: each texture over a buffer its own. + (device.grant_indices(IndexClass::Texture, 4096, 4)).expect("a grant"); + let next = std::cell::Cell::new(4096); let over = |buffer: &Buffer| { - let texture = (buffer.new_texture(&descriptor(), None, 0, pitch)) + let index = next.replace(next.get() + 1); + let texture = (buffer.new_texture(&descriptor(), Some(index), 0, pitch)) .unwrap_or_else(|e| panic!("{name}: {e}")); assert!( texture.managed_over().is_some(), @@ -517,7 +523,9 @@ entry: let buffer = (crate::tests::managed::on_device::new_managed(&device, cpu.id, 0, LEN, 0)) .unwrap_or_else(|e| panic!("{name}: {e}")); - let texture = (buffer.new_texture(&descriptor(), None, 0, pitch)) + // A colour render target takes an index. + (device.grant_indices(IndexClass::Texture, 4096, 1)).expect("a grant"); + let texture = (buffer.new_texture(&descriptor(), Some(4096), 0, pitch)) .unwrap_or_else(|e| panic!("{name}: {e}")); assert!( texture.managed_over().is_some(), diff --git a/crates/metal2vulkan-rt/src/tests/draw.rs b/crates/metal2vulkan-rt/src/tests/draw.rs index f4495180d..03967a811 100644 --- a/crates/metal2vulkan-rt/src/tests/draw.rs +++ b/crates/metal2vulkan-rt/src/tests/draw.rs @@ -221,12 +221,13 @@ pub(crate) mod on_device { values.iter().flat_map(|v| v.to_le_bytes()).collect() } - /// A 2D render target of `format`, `SIDE` a side. + /// A 2D render target of `format`, `SIDE` a side, with no index of its own + /// ([`Device::new_texture_for_views`]). pub(crate) fn target(device: &Device, format: PixelFormat, usage: TextureUsage) -> Texture { let mut d = TextureDescriptor::new(TextureType::Type2D, format, SIDE, SIDE); d.usage = usage; d.storage_mode = StorageMode::Shared; - (device.new_texture(&d, None)).unwrap_or_else(|error| panic!("{format:?}: {error}")) + (device.new_texture_for_views(&d)).unwrap_or_else(|error| panic!("{format:?}: {error}")) } /// The texels of `texture`, `unit` bytes each. diff --git a/crates/metal2vulkan-rt/src/tests/fence.rs b/crates/metal2vulkan-rt/src/tests/fence.rs index 8a923a866..3430dd7be 100644 --- a/crates/metal2vulkan-rt/src/tests/fence.rs +++ b/crates/metal2vulkan-rt/src/tests/fence.rs @@ -46,7 +46,7 @@ mod on_device { let out = (device.new_buffer(block.id, 256, 64, None)).expect("a buffer"); let mut d = TextureDescriptor::new(TextureType::Type2D, PixelFormat::RGBA8Unorm, 4, 4); d.usage = TextureUsage::RENDER_TARGET; - let texture = device.new_texture(&d, None).expect("a texture"); + let texture = device.new_texture_for_views(&d).expect("a texture"); let queue = device.new_command_queue(4).expect("a queue"); let layout = BufferLayout { offset: 0, diff --git a/crates/metal2vulkan-rt/src/tests/heap.rs b/crates/metal2vulkan-rt/src/tests/heap.rs index 43fb3190f..7dbd58185 100644 --- a/crates/metal2vulkan-rt/src/tests/heap.rs +++ b/crates/metal2vulkan-rt/src/tests/heap.rs @@ -15,7 +15,7 @@ mod on_device { use crate::tests::command::on_device::each_device; use crate::texture::TextureObject; use crate::{ - DispatchType, Error, Gap, HeapRange, MemoryClass, PixelFormat, Refusal, Region, + DispatchType, Error, Gap, HeapRange, IndexClass, MemoryClass, PixelFormat, Refusal, Region, StorageMode, TextureDescriptor, TextureType, TextureUsage, }; use std::sync::Arc; @@ -69,7 +69,7 @@ mod on_device { let mut textures = Vec::new(); let mut offset = 0; while offset + fit.size <= heap.len { - let texture = (device.new_heap_texture(&d, None, heap, offset)) + let texture = (device.new_heap_texture_for_views(&d, heap, offset)) .unwrap_or_else(|error| panic!("{name}: at {offset}: {error}")); assert_eq!( texture.image_size().next_multiple_of(granularity), @@ -115,12 +115,12 @@ mod on_device { } // Aliasing, as a Metal heap's aliasable resources do. - let alias = (device.new_heap_texture(&d, None, heap, 0)) + let alias = (device.new_heap_texture_for_views(&d, heap, 0)) .unwrap_or_else(|error| panic!("{name}: an alias: {error}")); // Placements that do not lie in the heap. let refused = |heap: HeapRange, offset: u64, what: &str| match device - .new_heap_texture(&d, None, heap, offset) + .new_heap_texture_for_views(&d, heap, offset) { Err(Error::Refused(Refusal::HeapTexturePlace { offset: at, @@ -223,13 +223,14 @@ mod on_device { ); assert_eq!(texture.gpu_resource_id().index, 0, "{name}: no index"); - // Through a view and back. + // Through a view and back, a colour render target's view at an index. + (device.grant_indices(IndexClass::Texture, 4096, 1)).expect("a grant"); let view = (texture.new_texture_view( PixelFormat::RGBA8Unorm, TextureType::Type2D, 0..1, 0..1, - None, + Some(4096), )) .unwrap_or_else(|error| panic!("{name}: a view: {error}")); let whole = Region { diff --git a/crates/metal2vulkan-rt/src/tests/import.rs b/crates/metal2vulkan-rt/src/tests/import.rs index 9baca899e..3bdc9daad 100644 --- a/crates/metal2vulkan-rt/src/tests/import.rs +++ b/crates/metal2vulkan-rt/src/tests/import.rs @@ -350,7 +350,7 @@ pub(crate) mod on_device { let mut plain_d = d; plain_d.usage = TextureUsage::RENDER_TARGET; plain_d.storage_mode = StorageMode::Shared; - let plain = (device.new_texture(&plain_d, None)) + let plain = (device.new_texture_for_views(&plain_d)) .unwrap_or_else(|error| panic!("{name}: {error}")); let source: Vec = (0..row * rows).map(|i| (i % 241) as u8 ^ 0x3c).collect(); let whole = Region { diff --git a/crates/metal2vulkan-rt/src/tests/managed.rs b/crates/metal2vulkan-rt/src/tests/managed.rs index def5bd570..8266b8302 100644 --- a/crates/metal2vulkan-rt/src/tests/managed.rs +++ b/crates/metal2vulkan-rt/src/tests/managed.rs @@ -146,8 +146,8 @@ pub(crate) mod on_device { use crate::tests::compute::on_device::{add, step, WORDS}; use crate::{ Block, BlockId, Buffer, BytesPlace, CommandBuffer, CommandBufferStatus, - ComputePipelineState, Device, DispatchType, Error, MemoryClass, PixelFormat, Refusal, - Region, StorageMode, TextureDescriptor, TextureType, TextureUsage, + ComputePipelineState, Device, DispatchType, Error, IndexClass, MemoryClass, PixelFormat, + Refusal, Region, StorageMode, TextureDescriptor, TextureType, TextureUsage, }; use ash::vk; use std::time::Instant; @@ -1011,7 +1011,9 @@ pub(crate) mod on_device { TextureDescriptor::new(TextureType::Type2D, PixelFormat::RGBA8Unorm, SIDE, SIDE); d.mipmap_level_count = 2; d.usage = TextureUsage::RENDER_TARGET; - let texture = (device.new_managed_texture(&d, None, &named, &places)) + // A colour render target takes an index. + (device.grant_indices(IndexClass::Texture, 4096, 1)).expect("a grant"); + let texture = (device.new_managed_texture(&d, Some(4096), &named, &places)) .unwrap_or_else(|error| panic!("{name}: {error}")); assert_eq!(texture.descriptor().storage_mode, StorageMode::Managed); let image = |level: u32| { @@ -1086,7 +1088,8 @@ pub(crate) mod on_device { // Places a copy does not read, and a texture without named bytes. let refused = |places: &[BytesPlace], bytes: &Buffer, what: &str| -> (u32, u32) { - match device.new_managed_texture(&d, None, bytes, places) { + // Refused before the index is placed. + match device.new_managed_texture(&d, Some(4097), bytes, places) { Err(Error::Refused(Refusal::ManagedTextureBytes { level, slice })) => { (level, slice) } @@ -1117,8 +1120,8 @@ pub(crate) mod on_device { .unwrap_or_else(|error| panic!("{name}: {error}")); assert_eq!(refused(&places, &unmapped, "Private"), (0, 0)); d.storage_mode = StorageMode::Managed; - let plain = - (device.new_texture(&d, None)).unwrap_or_else(|error| panic!("{name}: {error}")); + let plain = (device.new_texture_for_views(&d)) + .unwrap_or_else(|error| panic!("{name}: {error}")); match plain.upload_region(whole(SIDE), 0, 0) { Err(Error::Refused(Refusal::ManagedTextureBytes { level: 0, slice: 0 })) => {} Err(error) => panic!("{name}: an upload without named bytes: {error}"), @@ -1158,7 +1161,9 @@ pub(crate) mod on_device { let mut d = TextureDescriptor::new(TextureType::Type2D, PixelFormat::RGBA8Unorm, SIDE, SIDE); d.usage = TextureUsage::RENDER_TARGET; - let texture = (device.new_managed_texture(&d, None, &named, &places)) + // A colour render target takes an index. + (device.grant_indices(IndexClass::Texture, 4096, 1)).expect("a grant"); + let texture = (device.new_managed_texture(&d, Some(4096), &named, &places)) .unwrap_or_else(|error| panic!("{name}: {error}")); (texture.upload_region(whole(SIDE), 0, 0)) .unwrap_or_else(|error| panic!("{name}: {error}")); diff --git a/crates/metal2vulkan-rt/src/tests/mipmaps.rs b/crates/metal2vulkan-rt/src/tests/mipmaps.rs index 62cdad85f..f8c1c03e2 100644 --- a/crates/metal2vulkan-rt/src/tests/mipmaps.rs +++ b/crates/metal2vulkan-rt/src/tests/mipmaps.rs @@ -50,7 +50,7 @@ mod on_device { d.mipmap_level_count = levels; d.usage = TextureUsage::RENDER_TARGET; d.storage_mode = StorageMode::Shared; - (device.new_texture(&d, None)).unwrap_or_else(|error| panic!("{name}: {error}")) + (device.new_texture_for_views(&d)).unwrap_or_else(|error| panic!("{name}: {error}")) } /// Level `level` of an `RGBA8Unorm` texture, row by row. diff --git a/crates/metal2vulkan-rt/src/tests/over.rs b/crates/metal2vulkan-rt/src/tests/over.rs index fbc13029f..1316f67a7 100644 --- a/crates/metal2vulkan-rt/src/tests/over.rs +++ b/crates/metal2vulkan-rt/src/tests/over.rs @@ -220,7 +220,7 @@ mod on_device { let mut plain_d = d; plain_d.usage = TextureUsage::RENDER_TARGET; plain_d.storage_mode = StorageMode::Shared; - let plain = (device.new_texture(&plain_d, None)) + let plain = (device.new_texture_for_views(&plain_d)) .unwrap_or_else(|error| panic!("{name}: {error}")); // The count sees a texture's allocation: a plain texture's. assert_ne!( @@ -270,13 +270,14 @@ mod on_device { // A render target over the buffer, which a device may not hold in place. let mut r = d; r.usage = TextureUsage::RENDER_TARGET; - let target = buffer.new_texture(&r, None, offset, pitch).map(|_| ()); + // A colour render target takes an index; a refusal below comes before its placement. + let target = buffer.new_texture(&r, Some(3), offset, pitch).map(|_| ()); println!("over: {name}: a render target over a buffer: {target:?}"); // What Metal's layout rules refuse, and what is not held in place. let d = r; let refused = |d: &TextureDescriptor, offset: u64, pitch: u64, what: &str| match buffer - .new_texture(d, None, offset, pitch) + .new_texture(d, Some(4), offset, pitch) { Err(Error::Refused(Refusal::BufferTextureLayout { offset: o, @@ -290,9 +291,12 @@ mod on_device { refused(&d, offset, ROW - 4, "rows closer than a row"); refused(&d, offset, pitch + 2, "rows off a texel"); refused(&d, offset + 2, pitch, "an offset off a texel"); - let gap = |d: &TextureDescriptor, what: &str| match buffer - .new_texture(d, None, offset, pitch) - { + let gap = |d: &TextureDescriptor, what: &str| match buffer.new_texture( + d, + Some(4), + offset, + pitch, + ) { Err(Error::Unsupported(Gap::TextureOverBuffer { pixel_format })) => { assert_eq!(pixel_format, d.pixel_format, "{name}: {what}") } @@ -368,7 +372,8 @@ mod on_device { let counters = device.counters(); let queue = device.new_command_queue(4).expect("a queue"); let first = IndexClass::Texture.range().start; - (device.grant_indices(IndexClass::Texture, first, 1)).expect("a grant"); + // The sampled texture's index, and the render target's: a colour one takes one. + (device.grant_indices(IndexClass::Texture, first, 2)).expect("a grant"); for height in [3, 4] { let rows = u64::from(height) * pitch; let block = (device.take_block(MemoryClass::Shared, AT + rows + TAIL)) @@ -394,7 +399,7 @@ mod on_device { ); d.usage = TextureUsage::RENDER_TARGET; let size = device.linear_subresource_size(&d); - let target = (buffer.new_texture(&d, None, 0, pitch)) + let target = (buffer.new_texture(&d, Some(first + 1), 0, pitch)) .unwrap_or_else(|error| panic!("{name}: {height} rows: {error}")); let copied = target.copied_over().is_some(); let past = size.is_some_and(|size| size > rows); diff --git a/crates/metal2vulkan-rt/src/tests/pass.rs b/crates/metal2vulkan-rt/src/tests/pass.rs index c0694b336..9c4f779f3 100644 --- a/crates/metal2vulkan-rt/src/tests/pass.rs +++ b/crates/metal2vulkan-rt/src/tests/pass.rs @@ -221,7 +221,9 @@ mod on_device { /// Clears and stores land, loads keep, the area cuts, a slice of an array is its own; a pass /// alone is one submission, three barriers and one pass where an attachment moves (a /// shader-read texture's), and one barrier, its command buffer's end, where every attachment - /// renders at home; each view is made once and freed with its texture. + /// renders at home, colour targets made for rendering alone among them (they take an index and + /// the attachment-read usages, and a pass that does not fetch does not move them); each view is + /// made once and freed with its texture. #[test] fn every_device_clears_loads_and_stores_render_attachments() { each_device("render_passes", |name, _, device| { @@ -246,7 +248,7 @@ mod on_device { [4, 2], 1, render, - None, + Some(4097), ); let stencil = texture( &device, @@ -264,7 +266,7 @@ mod on_device { [2, 2], 1, render, - None, + Some(4098), ); let level0 = write(&colors, 0, 0, 4, 1); write(&colors, 1, 0, 4, 2); @@ -664,7 +666,7 @@ mod on_device { [4, 4], 1, TextureUsage::RENDER_TARGET, - None, + Some(4097), ); let sampled = texture( &device, diff --git a/crates/metal2vulkan-rt/src/tests/present.rs b/crates/metal2vulkan-rt/src/tests/present.rs index 43b51cc95..b49379c46 100644 --- a/crates/metal2vulkan-rt/src/tests/present.rs +++ b/crates/metal2vulkan-rt/src/tests/present.rs @@ -17,8 +17,8 @@ //! image stands in for destroying the swapchain, under the same rule (every use complete). //! //! - Under a device cfg, on every device of the cfg's kind, under the validation layer, for a -//! render-target drawable (home: the attachment layout) and a shader-read one at an index (home: -//! shader-read-only): three frames of acquire, a pass, present and the stand-in present. The +//! render-target drawable (home: the attachment layout) and a shader-read one (home: +//! shader-read-only), each at an index: three frames of acquire, a pass, present and the stand-in present. The //! first clears, the second loads and stores what the first left (the move home after a present //! starts in the present layout and keeps the texels), the third clears again. The first and //! third present from the test's thread once `wait_until_scheduled` returns, beside a scheduled @@ -424,14 +424,11 @@ mod on_device { // `BGRA8Unorm`'s bytes: blue, green, red, alpha. let first_texels = [0u8, 153, 51, 255].repeat(32); let third_texels = [102u8, 0, 255, 255].repeat(32); - for (usage, index, barriers) in [ - (TextureUsage::RENDER_TARGET, None, 3), - (read_render, Some(4096), 5), + for (usage, index, barriers, kind) in [ + (TextureUsage::RENDER_TARGET, 4097, 3, "render-target"), + (read_render, 4096, 5, "shader-read"), ] { - let kind = match index { - Some(_) => "shader-read", - None => "render-target", - }; + let index = Some(index); let info = swapchain(USAGE); let stand_in = StandIn::new(&device, &info); let textures = counters.gauges().textures; @@ -710,22 +707,21 @@ mod on_device { ClearColor::new(0.2, 0.6, 0.0, 1.0), ClearColor::new(1.0, 0.0, 0.4, 1.0), ]; - for (usage, image_usage, indexed, kind) in [ - (TextureUsage::RENDER_TARGET, colour, false, "render-target"), + // Each drawable at an index of its own, a colour render target's among them. + for (usage, image_usage, first, kind) in [ + (TextureUsage::RENDER_TARGET, colour, 4096, "render-target"), ( read_render, colour | vk::ImageUsageFlags::SAMPLED, - true, + 8192, "shader-read", ), ] { let chain = headless.swapchain(&device, image_usage); let count = chain.images.len() as u32; - if indexed { - device - .grant_indices(IndexClass::Texture, 4096, count) - .expect("a grant"); - } + device + .grant_indices(IndexClass::Texture, first, count) + .expect("a grant"); let textures = counters.gauges().textures; let mut d = descriptor(usage); d.pixel_format = chain.pixel_format; @@ -733,7 +729,7 @@ mod on_device { d.height = chain.info.image_extent.height; let drawables: Vec = (chain.images.iter().zip(0..)) .map(|(&image, at)| { - let index = indexed.then_some(4096 + at); + let index = Some(first + at); // SAFETY: the image is one of the swapchain's, made with `chain.info` on // the device, not acquired; the swapchain goes as the guide says, below. unsafe { device.new_drawable_texture(&d, index, image, &chain.info) } @@ -888,13 +884,17 @@ mod on_device { let stand_ins = [StandIn::new(&device, &info), StandIn::new(&device, &info)]; let readies = [semaphore(&device), semaphore(&device)]; let outs = [arena.buffer(), arena.buffer()]; + // Each a colour render target, at an index of its own. + (device.grant_indices(IndexClass::Texture, 4096, 2)).expect("a grant"); + let next = std::cell::Cell::new(4096); let drawables = stand_ins.each_ref().map(|stand_in| { + let index = next.replace(next.get() + 1); // SAFETY: the stand-in is made with `info` on the device, in the undefined // layout, and is destroyed as a swapchain may be, below. unsafe { device.new_drawable_texture( &descriptor(TextureUsage::RENDER_TARGET), - None, + Some(index), stand_in.image, &info, ) @@ -1039,7 +1039,7 @@ mod on_device { let unsampled = info.image_usage(USAGE & !vk::ImageUsageFlags::SAMPLED); let read_render = TextureUsage::SHADER_READ | render; device - .grant_indices(IndexClass::Texture, 4096, 1) + .grant_indices(IndexClass::Texture, 4096, 2) .expect("a grant"); assert_eq!( refusal(name, made(&descriptor(read_render), Some(4096), &unsampled)), @@ -1054,10 +1054,15 @@ mod on_device { "{name}: shader-read usage without an index" ); - let drawable = made(&descriptor(render), None, &info) + assert_eq!( + refusal(name, made(&descriptor(render), None, &info)), + Refusal::TextureIndex { index: None }, + "{name}: a colour render target without an index" + ); + let drawable = made(&descriptor(render), Some(4097), &info) .unwrap_or_else(|error| panic!("{name}: a drawable's texture: {error}")); let plain = device - .new_texture(&descriptor(render), None) + .new_texture_for_views(&descriptor(render)) .expect("a texture"); let queue = device.new_command_queue(4).expect("a queue"); let presenting = queue.command_buffer().expect("a command buffer"); diff --git a/crates/metal2vulkan-rt/src/tests/resolve.rs b/crates/metal2vulkan-rt/src/tests/resolve.rs index 5cb2e2941..0aa3251ad 100644 --- a/crates/metal2vulkan-rt/src/tests/resolve.rs +++ b/crates/metal2vulkan-rt/src/tests/resolve.rs @@ -76,7 +76,7 @@ mod on_device { td.sample_count = 4; td.usage = TextureUsage::RENDER_TARGET; td.storage_mode = StorageMode::Private; - let made = device.new_texture(&td, None); + let made = device.new_texture_for_views(&td); if let Err(error) = &made { println!("{name}: no 4-sample {format:?}: {error}"); } @@ -164,8 +164,8 @@ mod on_device { rd.array_length = 2; rd.usage = TextureUsage::RENDER_TARGET; rd.storage_mode = StorageMode::Shared; - let into = - (device.new_texture(&rd, None)).unwrap_or_else(|error| panic!("{name}: {error}")); + let into = (device.new_texture_for_views(&rd)) + .unwrap_or_else(|error| panic!("{name}: {error}")); let mut p = pass(&samples, None); assert_eq!( p.precision_note(), diff --git a/crates/metal2vulkan-rt/src/tests/sampler.rs b/crates/metal2vulkan-rt/src/tests/sampler.rs index a994260b7..a7cbc2c34 100644 --- a/crates/metal2vulkan-rt/src/tests/sampler.rs +++ b/crates/metal2vulkan-rt/src/tests/sampler.rs @@ -198,8 +198,8 @@ mod on_device { compute.end_encoding(); let mut d = TextureDescriptor::new(TextureType::Type2D, PixelFormat::RGBA8Unorm, 4, 4); d.usage = TextureUsage::RENDER_TARGET; - let color = - (device.new_texture(&d, None)).unwrap_or_else(|error| panic!("{name}: {error}")); + let color = (device.new_texture_for_views(&d)) + .unwrap_or_else(|error| panic!("{name}: {error}")); let mut pass = RenderPassDescriptor::default(); pass.color_attachments[0].texture = Some(color); let mut render = (command_buffer.render_command_encoder(&pass)) diff --git a/crates/metal2vulkan-rt/src/tests/sites.rs b/crates/metal2vulkan-rt/src/tests/sites.rs index 5184176c2..9d4c7d8e5 100644 --- a/crates/metal2vulkan-rt/src/tests/sites.rs +++ b/crates/metal2vulkan-rt/src/tests/sites.rs @@ -79,7 +79,8 @@ mod on_device { TextureDescriptor::new(TextureType::Type2D, PixelFormat::RGBA8Unorm, width, height); d.usage = TextureUsage::RENDER_TARGET; d.storage_mode = StorageMode::Shared; - (device.new_texture(&d, None)).unwrap_or_else(|error| panic!("{name}: a texture: {error}")) + (device.new_texture_for_views(&d)) + .unwrap_or_else(|error| panic!("{name}: a texture: {error}")) } /// Writes the whole of `texture`, of `width` by `height`, from the host. diff --git a/crates/metal2vulkan-rt/src/tests/spill.rs b/crates/metal2vulkan-rt/src/tests/spill.rs index 2c1c4b604..95458541a 100644 --- a/crates/metal2vulkan-rt/src/tests/spill.rs +++ b/crates/metal2vulkan-rt/src/tests/spill.rs @@ -81,7 +81,7 @@ mod on_device { let mut d = TextureDescriptor::new(TextureType::Type2D, PixelFormat::RGBA8Unorm, 512, 512); d.usage = TextureUsage::RENDER_TARGET; - let texture = match device.new_texture(&d, None) { + let texture = match device.new_texture_for_views(&d) { Ok(texture) => Some(texture), Err(error) if error.is_out_of_device_memory() => None, Err(error) => panic!("{name}: a texture: {error}"), diff --git a/crates/metal2vulkan-rt/src/tests/take_split.rs b/crates/metal2vulkan-rt/src/tests/take_split.rs index 063e2ed1f..ea1c55d49 100644 --- a/crates/metal2vulkan-rt/src/tests/take_split.rs +++ b/crates/metal2vulkan-rt/src/tests/take_split.rs @@ -125,8 +125,8 @@ mod on_device { TextureDescriptor::new(TextureType::Type2D, PixelFormat::RGBA8Unorm, 64, 64); td.usage = TextureUsage::RENDER_TARGET; td.storage_mode = StorageMode::Private; - let texture = - (device.new_texture(&td, None)).unwrap_or_else(|error| panic!("{name}: {error}")); + let texture = (device.new_texture_for_views(&td)) + .unwrap_or_else(|error| panic!("{name}: {error}")); let placed = counters.gauges().texture_block_bytes - gauged.texture_block_bytes; let own = counters.gauges().texture_own_bytes - gauged.texture_own_bytes; assert!( diff --git a/crates/metal2vulkan-rt/src/tests/texture.rs b/crates/metal2vulkan-rt/src/tests/texture.rs index a1a5c24ea..41e2e1d01 100644 --- a/crates/metal2vulkan-rt/src/tests/texture.rs +++ b/crates/metal2vulkan-rt/src/tests/texture.rs @@ -634,11 +634,11 @@ mod on_device { each_device("host_bytes", |name, _, device| { let mut d = descriptor(TextureType::Type2D, PixelFormat::RGBA8Unorm, [8, 4, 1]); d.storage_mode = StorageMode::Shared; - // No shader usage, so no index: a texture shaders read or write takes one, and no - // usage at all is Metal's unknown usage, which counts as every usage. + // No shader usage, and no index of its own (a colour render target takes one at + // `new_texture`); no usage at all is Metal's unknown usage, which counts as every usage. d.usage = TextureUsage::RENDER_TARGET; - let texture = - (device.new_texture(&d, None)).unwrap_or_else(|error| panic!("{name}: {error}")); + let texture = (device.new_texture_for_views(&d)) + .unwrap_or_else(|error| panic!("{name}: {error}")); let region = Region { origin: [1, 1, 0], size: [5, 3, 1], diff --git a/crates/metal2vulkan-rt/src/tests/view.rs b/crates/metal2vulkan-rt/src/tests/view.rs index 48d63d66e..51c24d097 100644 --- a/crates/metal2vulkan-rt/src/tests/view.rs +++ b/crates/metal2vulkan-rt/src/tests/view.rs @@ -664,14 +664,16 @@ mod on_device { d.array_length = 2; d.mipmap_level_count = 2; d.usage = TextureUsage::RENDER_TARGET; - let texture = (device.new_managed_texture(&d, None, &named, &places)) + // A colour render target takes an index, and so does its view. + (device.grant_indices(IndexClass::Texture, 4096, 2)).expect("a grant"); + let texture = (device.new_managed_texture(&d, Some(4096), &named, &places)) .unwrap_or_else(|error| panic!("{name}: {error}")); let view = (texture.new_texture_view( PixelFormat::RGBA8Unorm, TextureType::Type2D, 1..2, 1..2, - None, + Some(4097), )) .unwrap_or_else(|error| panic!("{name}: {error}")); let region = Region { diff --git a/crates/metal2vulkan-rt/src/texture.rs b/crates/metal2vulkan-rt/src/texture.rs index e0bf4811a..53c307cb5 100644 --- a/crates/metal2vulkan-rt/src/texture.rs +++ b/crates/metal2vulkan-rt/src/texture.rs @@ -360,17 +360,19 @@ pub(crate) struct Image { backing: Backing, /// The memory's bytes. bytes: u64, - /// The view of the sampled-texture array's element, where shaders read the texture. + /// The sampled view: the sampled-texture array's element, where shaders read the texture, and + /// the view a draw reads an attachment through (`attachment_reads`), which a colour render + /// target not read has for that alone. sampled: Option, /// The view of the storage-texture array's element, where shaders write it and the device /// stores its format through a storage image without a format. storage: Option, /// The layout the image is in outside a copy, which its elements name. home: vk::ImageLayout, - /// A draw may read an attachment of its own pass through a texture over the image (a - /// shader-read colour render target not in place over a buffer, its home shader-read-only, or - /// general where it has a storage view, on a device that reads attachments): each such - /// texture has a second element of the sampled-texture array, at its index plus + /// A draw may read an attachment of its own pass through a texture over the image (a colour + /// render target not in place over a buffer, on a device that reads attachments; its home is + /// general where it has a storage view, shader-read-only where shaders read it, else the + /// attachment layout): each such texture has a second element of the sampled-texture array, at its index plus /// [`TEXTURES`](crate::floor::TEXTURES). Through the attachment feedback loop the image has /// the feedback usage and the element names the feedback layout, which a pass moves the whole /// image to from its home, and back, for a draw that reads it; on a device without the loop @@ -855,8 +857,12 @@ impl Device { /// memory, placed in a texture block ([`Texture`]), which reads zero until written: the /// runtime's choice, where Metal does not say what a new texture holds (the module docs). /// `index` is given exactly where shaders reach the texture (shader-read or shader-write - /// usage), and is then an index of a live [`IndexClass::Texture`](crate::IndexClass::Texture) - /// grant, whatever the usage. The texture's elements of set 1 are written before the call + /// usage, or render-target usage of a colour pixel format, which a fragment function reads + /// through its attachment; [`Refusal::TextureIndex`]), and is then an index of a live + /// [`IndexClass::Texture`](crate::IndexClass::Texture) grant, whatever the usage. A colour + /// render target shaders neither read nor write writes only its second element there, the + /// one a draw reads it through as an attachment, and a slot that binds it reads Metal's nil + /// texture. The texture's elements of set 1 are written before the call /// returns, and its image moved to its home layout ahead of every command buffer committed /// after it. Until the submission of the next commit nothing else holds the texture: released /// first, it is freed at once. @@ -1191,8 +1197,7 @@ impl Device { missing: missing.as_raw(), })); } - let reached = usage.intersects(TextureUsage::SHADER_READ | TextureUsage::SHADER_WRITE); - if reached != index.is_some() { + if reached(d.pixel_format, usage) != index.is_some() { return Err(Error::Refused(Refusal::TextureIndex { index })); } // Views of the image's own format take its whole usage, which the swapchain's creation @@ -1200,7 +1205,7 @@ impl Device { let mut made = Made::default(); // SAFETY: the image is the device's and bound (the caller's contract), of the plan's // format, and has the plan's usage. - let views = unsafe { made.views(&core.device, image, &shape, &plan) }; + let views = unsafe { made.views(&core.device, image, &shape, &plan, plan.usage) }; if views.is_err() { // SAFETY: nothing else holds the views; `made` holds no image. unsafe { made.destroy(core) }; @@ -1246,7 +1251,7 @@ impl Device { &core.counters, index, (sampled, storage), - false, + None, ) }?; Some((index, owner)) @@ -1286,9 +1291,8 @@ impl Device { ) -> Result { let d = *descriptor; let (shape, plan, usage) = self.prepare(&d)?; - let reached = usage.intersects(TextureUsage::SHADER_READ | TextureUsage::SHADER_WRITE); let index = match reach { - Reach::Index(index) if reached != index.is_some() => { + Reach::Index(index) if reached(d.pixel_format, usage) != index.is_some() => { return Err(Error::Refused(Refusal::TextureIndex { index })); } Reach::Index(index) => index, @@ -1432,7 +1436,8 @@ impl Device { // image as `image` drops. let index = match index { Some(index) => { - let sampled = sampled.map(|view| (view, home)); + let element = sampled.filter(|_| plan.read).map(|view| (view, home)); + let looped = sampled.filter(|_| image.looped()); // SAFETY: the texture's views are live; its image is in its home layout whenever // work uses it, the upload of the next commit going first. let owner = unsafe { @@ -1440,8 +1445,8 @@ impl Device { &core.device, &core.counters, index, - (sampled, storage), - image.looped(), + (element, storage), + looped, ) }?; Some((index, owner)) @@ -1505,8 +1510,15 @@ struct Plan { format: vk::Format, usage: vk::ImageUsageFlags, samples: vk::SampleCountFlags, - /// The sampled view's component mapping and aspect, where shaders read the texture. + /// The sampled view's component mapping and aspect, where shaders read the texture or, for + /// a colour render target, where a draw may read it as an attachment of its own pass + /// ([`Plan::reads_attachments`]); `plan` drops it from a colour render target whose draws + /// read no attachment through it. sampled: Option<(vk::ComponentMapping, vk::ImageAspectFlags)>, + /// Whether shaders read the texture: its sampled view is its sampled-texture element. A + /// colour render target not read has the view for its second element alone, and its element + /// stays the null one, which a slot that binds it reads as Metal's nil texture. + read: bool, /// Whether the texture has a storage view. storage: bool, /// Whether a storage view of the plan's format reads without a format: the format's @@ -1545,23 +1557,28 @@ struct Plan { impl Plan { /// The usage of a linear image of the plan, over a buffer in place: without the feedback - /// usage, as no pass moves it to the feedback layout. + /// usage, as no pass moves it to the feedback layout, and without the sampled usage where + /// shaders do not read the texture, as a colour render target takes it for attachment reads + /// alone. fn linear_usage(&self) -> vk::ImageUsageFlags { - self.usage & !vk::ImageUsageFlags::ATTACHMENT_FEEDBACK_LOOP_EXT + let mut usage = self.usage & !vk::ImageUsageFlags::ATTACHMENT_FEEDBACK_LOOP_EXT; + if !self.read { + usage &= !vk::ImageUsageFlags::SAMPLED; + } + usage } /// Whether a draw may read an attachment of its own pass through a texture of the plan, where - /// its image takes the feedback usage or a shadow: a shader-read colour render target, with a - /// storage view or not, on a device that reads attachments. Vulkan's feedback layout asks the - /// attachment and sampled usages beside the feedback usage - /// (`VUID-VkImageMemoryBarrier2-srcQueueFamilyIndex-07006`) and bars none, the storage usage - /// included; in it the image is only rendered and sampled. + /// its image takes the feedback usage or a shadow: a colour render target, read by shaders or + /// not, with a storage view or not, on a device that reads attachments, as Metal reads any + /// render target. Vulkan's feedback layout asks the attachment and sampled usages beside the + /// feedback usage (`VUID-VkImageMemoryBarrier2-srcQueueFamilyIndex-07006`) and bars none, the + /// storage usage included; in it the image is only rendered and sampled. A target not read + /// takes the sampled usage for the reads alone (`plan`). fn reads_attachments(&self, core: &Core) -> bool { - let render = - vk::ImageUsageFlags::COLOR_ATTACHMENT | vk::ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT; core.attachment_reads - && self.usage.contains(vk::ImageUsageFlags::SAMPLED) - && self.usage.intersects(render) + && self.sampled.is_some() + && self.usage.contains(vk::ImageUsageFlags::COLOR_ATTACHMENT) && self.aspects == vk::ImageAspectFlags::COLOR } @@ -2001,10 +2018,14 @@ fn plan( }; // A draw may read an attachment of its own pass through such a texture // (`crate::render_encoder`), where the device reads attachments: in the feedback layout where - // it has the attachment feedback loop, else through a copy into the image's shadow. + // it has the attachment feedback loop, else through a copy into the image's shadow. The image + // takes the sampled usage for it where shaders do not read the texture. let mut attachment_reads = plan.reads_attachments(core); let looped = attachment_reads && core.feedback.is_some(); let mut image_usage = plan.usage; + if attachment_reads { + image_usage |= vk::ImageUsageFlags::SAMPLED; + } if looped { image_usage |= vk::ImageUsageFlags::ATTACHMENT_FEEDBACK_LOOP_EXT; } @@ -2026,20 +2047,25 @@ fn plan( info = info.flags(plan.flags(shape)); properties = core.image_format_properties(&info); } - // A format the device makes without the feedback usage alone is made so, and its draws read - // no attachment of their own pass through it. - if properties.is_none() && looped { + // A format the device makes without the usages the attachment reads add alone is made so, + // and its draws read no attachment of their own pass through it. + if properties.is_none() && attachment_reads { attachment_reads = false; - image_usage &= !vk::ImageUsageFlags::ATTACHMENT_FEEDBACK_LOOP_EXT; + image_usage = plan.usage; properties = core.image_format_properties(&info.usage(image_usage)); } - let properties = properties.ok_or_else(gap)?; - // Likewise a format the device makes no shadow of. - if attachment_reads && !looped { + // Likewise a format the device makes no shadow of: a target shaders do not read is then made + // without the sampled usage. + if properties.is_some() && attachment_reads && !looped { attachment_reads = core .image_format_properties(&info.usage(SHADOW_USAGE)) .is_some(); + if !attachment_reads && image_usage != plan.usage { + image_usage = plan.usage; + properties = core.image_format_properties(&info.usage(image_usage)); + } } + let properties = properties.ok_or_else(gap)?; let max = properties.max_extent; let max_extent = [max.width, max.height, max.depth]; if shape.extent.iter().zip(max_extent).any(|(&e, m)| e > m) @@ -2061,6 +2087,9 @@ fn plan( plan.usage = image_usage; plan.attachment_reads = attachment_reads; plan.cube = cube; + if !plan.read && !attachment_reads { + plan.sampled = None; + } Ok(plan) } @@ -2114,6 +2143,16 @@ fn view_formats( list } +/// Whether shaders reach a texture of `pf` and `usage` (Metal's unknown usage already its four +/// bits), which takes an index exactly where they do ([`Refusal::TextureIndex`]): it is read or +/// written, or it is a colour render target (a pixel format with no depth or stencil), which a +/// fragment function reads through its attachment, as Metal gives every texture a +/// `gpuResourceID`. A depth or stencil target shaders neither read nor write is not reached. +fn reached(pf: PixelFormat, usage: TextureUsage) -> bool { + usage.intersects(TextureUsage::SHADER_READ | TextureUsage::SHADER_WRITE) + || (usage.contains(TextureUsage::RENDER_TARGET) && !formats::is_depth_stencil(pf)) +} + /// What a texture of `d` and `usage` holds in its views, as its own image or as a view of /// another's: the format and how it holds Metal's, the aspects, the sampled view's components and /// aspect, whether it has a storage view, the image usage its uses ask, its home layout, the copy @@ -2202,7 +2241,10 @@ fn form(core: &Core, d: &TextureDescriptor, usage: TextureUsage) -> Result Some(( components(mapping.components.into(), d.swizzle).ok_or_else(gap)?, view_aspect, @@ -2214,6 +2256,7 @@ fn form(core: &Core, d: &TextureDescriptor, usage: TextureUsage) -> Result Result<(Option, Option), Error> { let mut view = |components, aspect| { let (view_type, format) = (shape.view_type, plan.format); @@ -2652,8 +2699,10 @@ impl Made { Ok::<_, Error>(view) }; let sampled = match plan.sampled { - Some((components, aspect)) => Some(view(components, aspect)?), - None => None, + Some((components, aspect)) if usage.contains(vk::ImageUsageFlags::SAMPLED) => { + Some(view(components, aspect)?) + } + _ => None, }; let storage = match plan.storage { true => Some(view( @@ -3342,8 +3391,7 @@ impl Texture { if !usable.contains(vk::ImageUsageFlags::SAMPLED) { plan.sampled = None; } - let reached = usage.intersects(TextureUsage::SHADER_READ | TextureUsage::SHADER_WRITE); - if reached != index.is_some() { + if reached(d.pixel_format, usage) != index.is_some() { return Err(Error::Refused(Refusal::TextureIndex { index })); } let core = &inner.core; @@ -3402,7 +3450,8 @@ impl Texture { let shadow = ShadowView::of(&inner.image, &plan, index, shape.view_type, range); let index = match index { Some(index) => { - let sampled = sampled.map(|view| (view, inner.image.home)); + let element = (sampled.filter(|_| plan.read)).map(|view| (view, inner.image.home)); + let looped = sampled.filter(|_| inner.image.looped()); // SAFETY: the view's image views are live; the image is in its home layout // whenever work uses it. let owner = unsafe { @@ -3410,8 +3459,8 @@ impl Texture { &core.device, &core.counters, index, - (sampled, storage), - inner.image.looped(), + (element, storage), + looped, ) }?; Some((index, owner)) @@ -4519,8 +4568,7 @@ impl Device { let d = *descriptor; let checked = self.prepare(&d).and_then(|(shape, plan, usage)| { // With no index, a plane shaders reach is reached through its views alone. - let reached = usage.intersects(TextureUsage::SHADER_READ | TextureUsage::SHADER_WRITE); - if index.is_some() && !reached { + if index.is_some() && !reached(d.pixel_format, usage) { return Err(Error::Refused(Refusal::TextureIndex { index })); } let missing = plan.linear_usage() & !made.usage; @@ -4543,7 +4591,8 @@ impl Device { let mut views = Made::default(); // SAFETY: the image is the device's and bound, of the plan's format or mutable with it // listed, with the plan's usage and more, which the plan's format's features hold. - let made_views = unsafe { views.views(&core.device, made.image, &shape, &plan) }; + let made_views = + unsafe { views.views(&core.device, made.image, &shape, &plan, made.usage) }; let (sampled, storage) = match made_views { Ok(made_views) => made_views, Err(error) => { @@ -4608,7 +4657,8 @@ impl Device { // image as `image` drops. let index = match index { Some(index) => { - let sampled = sampled.map(|view| (view, home)); + let element = sampled.filter(|_| plan.read).map(|view| (view, home)); + let looped = sampled.filter(|_| image.looped()); // SAFETY: the texture's views are live; its image is in its home layout whenever // work uses it: the upload that moves it goes ahead of that work, or it is general // after the other API's access. @@ -4617,8 +4667,8 @@ impl Device { &core.device, &core.counters, index, - (sampled, storage), - image.looped(), + (element, storage), + looped, ) }?; Some((index, owner)) diff --git a/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs b/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs index 9fe36f8a7..8aadc3f6b 100644 --- a/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs +++ b/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs @@ -637,7 +637,6 @@ fn set_texture_descriptor(call: &Call, d: &mut rt::TextureDescriptor) -> Result< Ok(()) } -/// The class of a texture's index: none where no shader reaches it. /// Every slice of a texture of `d`, as a view counts them: a cube's six faces each one. fn whole_slices(d: &rt::TextureDescriptor) -> Range { let faces = match d.texture_type { @@ -650,9 +649,16 @@ fn whole_slices(d: &rt::TextureDescriptor) -> Range { } } -fn texture_class(usage: rt::TextureUsage) -> Option { +/// The class of the index of a texture of `pf` and `usage`, where shaders reach it, as the runtime +/// takes it (`rt::Refusal::TextureIndex`): unknown usage, shader-read or shader-write usage, or a +/// colour render target, which a fragment function reads through its attachment; none for a depth +/// or stencil target shaders neither read nor write. +fn texture_class(pf: rt::PixelFormat, usage: rt::TextureUsage) -> Option { let reached = rt::TextureUsage::SHADER_READ | rt::TextureUsage::SHADER_WRITE; - (usage.is_empty() || usage.intersects(reached)).then_some(rt::IndexClass::Texture) + let color_target = usage.contains(rt::TextureUsage::RENDER_TARGET) + && !metal2vulkan::metal::format::is_depth_stencil(pf); + (usage.is_empty() || usage.intersects(reached) || color_target) + .then_some(rt::IndexClass::Texture) } /// How long the device has, once the backend ends or drops, to finish its GPU work and stop its @@ -1361,7 +1367,10 @@ impl NewRuntime { Some((range, offset)) => device.new_heap_texture(descriptor, index, range, offset), None => device.new_texture(descriptor, index), }; - match (texture_class(descriptor.usage), outside) { + match ( + texture_class(descriptor.pixel_format, descriptor.usage), + outside, + ) { (Some(class), false) => self.place(call, class, None, |index| make(Some(index))), (Some(class), true) => self.place_outside(call, class, |index| make(Some(index))), (None, _) => make(None).map_err(failed(call)), @@ -1896,7 +1905,7 @@ impl NewRuntime { }); made }; - let made = match texture_class(usage) { + let made = match texture_class(descriptor.pixel_format, usage) { Some(class) => self.place(call, class, None, |index| noted(Some(index))), None => noted(None).map_err(failed(call)), }; diff --git a/docs/REFUSALS.md b/docs/REFUSALS.md index 3c49130c6..8d6ea1598 100644 --- a/docs/REFUSALS.md +++ b/docs/REFUSALS.md @@ -650,7 +650,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-rt/src/arrays.rs (grant)` | `-` | | `IndicesNotGranted` | A texture, a sampler state or a pipeline state's constexpr sampler at an index no live 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, and a grant is returned once | 2: `crates/metal2vulkan-rt/src/arrays.rs (place)`, `crates/metal2vulkan-rt/src/arrays.rs (release)` | `-` | | `IndicesInUse` | A release of a grant while a texture, a sampler state or a constexpr sampler holds one of its indices (`Device::release_indices`) | every device (a public call's own check of its arguments) | `ours`: an object's index stays granted while the object holds it | 1: `crates/metal2vulkan-rt/src/arrays.rs (release)` | `-` | -| `TextureIndex` | A texture whose usage shaders reach (read or write) made without an index, or one of no such usage 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 | 4: `crates/metal2vulkan-rt/src/texture.rs (new_drawable_texture)`, `crates/metal2vulkan-rt/src/texture.rs (surface_texture)` (+2) | `-` | +| `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 | 4: `crates/metal2vulkan-rt/src/texture.rs (new_drawable_texture)`, `crates/metal2vulkan-rt/src/texture.rs (surface_texture)` (+2) | `-` | | `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 | 1: `crates/metal2vulkan-rt/src/texture.rs (shape)` | `-` | | `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 | 1: `crates/metal2vulkan-rt/src/texture.rs (shape)` | `-` | | `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-rt/src/texture.rs (shape)` | `-` | diff --git a/docs/RESOURCE-ID.md b/docs/RESOURCE-ID.md index 92e247c09..c222b0c45 100644 --- a/docs/RESOURCE-ID.md +++ b/docs/RESOURCE-ID.md @@ -19,14 +19,17 @@ the same IDs on every run, and a replay reproduces them. | Class | Index | Object | Arrays it is valid in | |---|---|---|---| | nil | 0 | nil (the ID 0) | none | -| texture | 1 to 65,535 | a texture of any usage a shader reaches: `MTLTextureUsageShaderRead`, `MTLTextureUsageShaderWrite`, or `MTLTextureUsageUnknown` | the sampled-image array and the storage-image array | +| texture | 1 to 65,535 | a texture of any usage a shader reaches: `MTLTextureUsageShaderRead`, `MTLTextureUsageShaderWrite`, `MTLTextureUsageUnknown`, or `MTLTextureUsageRenderTarget` of a colour pixel format, which a fragment function reads through its attachment | the sampled-image array and the storage-image array | | sampler | 1 to 3,999 | a sampler | the sampler arrays | A texture of any usage may be placed at any index of the texture class. A texture with a storage 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 texture no -shader reaches has no index. A texture view takes its own index of the texture class. A texture +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 buffer has no index: it is a typed refusal under these domains. An index has one owner, the object placed there last, as Metal's later texture at a resource ID