From cceefacfa7b7b9ea021a28dc9aac7bd3f9be4f09 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 15 Nov 2026 21:23:17 -0800 Subject: [PATCH] RUNTIME 2420: A storage twin's neutral shares the device's zero buffer: the plan says which buffer writes are read-only What: a new public BindingPlan::read_only_buffer(&DescriptorWrite): true for a uniform buffer and for a uniform view's storage twin (DescriptorView::UniformWithStorageTwin, PlannedBuffer::twin), which no function stores through. The runtime asks it where it used to read the descriptor type alone (binding::descriptor's neutral arm, make_neutrals, binds_own_zeros), and makes the device's zero buffer a storage buffer too, so a twin bound to nothing binds it. Why: on the Iris Xe RPL-P profile (DriverWorkaround::PushReadsLengthenThreadPayload) RUNTIME 2413 gave a kernel's constant slot a storage twin. Left unbound, the plan writes the twin from the ZeroBuffer neutral at a STORAGE_BUFFER binding, which the runtime took for a writable binding: every dispatch took a fresh 64 KiB upload region, a dedicated VkBuffer allocated and zeroed, held until its command buffer completed. Round 5974320352's Iris leg: neutral_zeros_shared::dispatches_share_the_devices_one_zero_buffer, 8 dispatches made 8 VkBuffers and 16 made 16. Translation output and plans are unchanged (no SEMANTICS bump); the API addition is blessed in api_signatures.tsv, and docs/API.md section 5 says it. Test: engine_access::an_unbound_constant_slot_s_storage_twin_is_a_read_only_write plans buffer_end_byte_load_constant with nothing bound on intel-irisxe-rplp-anv (the twin's STORAGE_BUFFER write read-only, the written slot's not) and, as the negative control, on four other profiles (no twin; read-only exactly at uniform buffers). The device check is the neutral test itself on the Iris, which fails at the base. Also, test premises on the Iris profile (round 5974320352's iris:runtime reds): - a20c0a26d8: the compute creation test counts the forms its profile makes (1 + keyed storage views since RUNTIME 2410) and holds the pool only where completion is bounded (under Exact the second form runs on the pool). - 88b8d31a32: set 0 at offsets counts the pipeline binds (draws past offset 0 take the residual form) and the push after each where the driver forgets pushed descriptors after a pipeline bind. Verified in the lane (msi): m3-max-35051 clippy -D warnings -p metal2vulkan -p metal2vulkan-runtime --all-targets --all-features exit 0, translator tests 71/71 (api_signatures 3/3); m3-max-35053 lavapipe (cpuicd) deferred_calls, neutral_zeros_shared, set_zero_at_offsets, uniform_bind_offsets 43/43. Still to run: clippy gpucfg and cpuicd, rustdoc, ONE Iris run of the three tests. New Degradation kinds: none. --- crates/metal2vulkan-runtime/src/binding.rs | 73 ++++++++---------- crates/metal2vulkan-runtime/src/neutral.rs | 21 +++-- .../src/tests/deferred_calls.rs | 21 +++-- .../src/tests/neutral_zeros_shared.rs | 11 ++- .../src/tests/set_zero_at_offsets.rs | 34 ++++++++- crates/metal2vulkan/src/bind/plan.rs | 20 +++++ crates/metal2vulkan/src/cache/mod.rs | 2 +- .../metal2vulkan/src/tests/engine_access.rs | 76 +++++++++++++++++++ crates/metal2vulkan/tests/api_signatures.tsv | 1 + docs/API.md | 5 +- 10 files changed, 198 insertions(+), 66 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/binding.rs b/crates/metal2vulkan-runtime/src/binding.rs index 6a8401b7c..3a8af7258 100644 --- a/crates/metal2vulkan-runtime/src/binding.rs +++ b/crates/metal2vulkan-runtime/src/binding.rs @@ -1302,7 +1302,7 @@ pub(super) fn bind_into( call, device, state, - (command, tables), + (plan, command, tables), (write, element), kept, )?; @@ -1328,7 +1328,7 @@ pub(super) fn bind_into( call, device, state, - (command, tables), + (plan, command, tables), (write, element), kept, )?; @@ -1469,13 +1469,13 @@ fn written_at( } } -/// The write of `write`, one of its plan's descriptor writes, at `element`, for `command` over +/// The write of `write`, one of `plan`'s descriptor writes, at `element`, for `command` over /// `tables` ([`bind_into`]): its image descriptor's sampler kept in `samplers`, through `memo`. fn written( call: &'static str, device: &Device, state: &mut State, - (command, tables): (&Command<'_>, &Tables<'_>), + (plan, command, tables): (&BindingPlan, &Command<'_>, &Tables<'_>), (write, element): (&DescriptorWrite, u32), (samplers, mut memo): (&mut Vec>, Option<&mut BindMemo>), ) -> Result { @@ -1487,7 +1487,12 @@ fn written( (_, ty) => match ty { vk::DescriptorType::UNIFORM_BUFFER | vk::DescriptorType::STORAGE_BUFFER => { let spaces = memo.as_deref_mut().map(|memo| &mut memo.spaces); - Info::Buffer(descriptor(device, state, command, tables, (write, spaces))?) + Info::Buffer(descriptor( + device, + state, + (plan, command, tables), + (write, spaces), + )?) } vk::DescriptorType::UNIFORM_TEXEL_BUFFER | vk::DescriptorType::STORAGE_TEXEL_BUFFER => { let storage = ty == vk::DescriptorType::STORAGE_TEXEL_BUFFER; @@ -1543,7 +1548,7 @@ pub(super) fn make_neutrals(device: &Device, plan: &BindingPlan) -> Result<(), E // (the module docs' "no wait"), as it finds the images and samplers made. WriteSource::Neutral( NeutralResource::ZeroBuffer { min_bytes } | NeutralResource::Sink { min_bytes }, - ) if read_only_buffer(ty) => { + ) if plan.read_only_buffer(write) => { device.neutral_zero_buffer(padded(*min_bytes as usize))?; } WriteSource::Neutral(kind @ NeutralResource::Image { .. }) => { @@ -2774,29 +2779,15 @@ fn unexpected(source: &WriteSource) -> Error { } } -/// The buffer descriptor of `write`. -/// Whether a buffer descriptor of `ty` is read-only to every shader that binds it: a uniform -/// buffer, whose block Vulkan and SPIR-V make read-only, so no store can reach it. A storage -/// buffer is not, whatever a particular shader does with it. -/// -/// It decides which bindings may share the device's one neutral zero buffer (`neutral.rs`): a -/// writable binding keeps a region of its own, so nothing a shader stores is ever seen by another -/// command. Structural: the descriptor type alone, never a function or shader name. -fn read_only_buffer(ty: vk::DescriptorType) -> bool { - matches!( - ty, - vk::DescriptorType::UNIFORM_BUFFER | vk::DescriptorType::UNIFORM_BUFFER_DYNAMIC - ) -} - /// Whether `plan` binds a zero region of each command's own: at a writable buffer binding that -/// nothing is bound to ([`read_only_buffer`]), a fresh region of the command buffer's upload -/// arena at every draw (`write_info`). A draw drawn again as it was kept, or rebound, keeps the -/// kept draw's set-0 writes at every slot but those that changed (`rebind_into`), so it would -/// bind the kept draw's region, which that draw may have written: such a draw is not kept -/// (`render/draw_again.rs`), counted as one that may write (`WorkCounts::draws_unkept_writing`). -/// It walks every write of the plan, one for each resource its heap reaches in each heap array, -/// so a draw asks its plan's facts (`render/plan_facts.rs`), which hold the answer. +/// nothing is bound to (not `BindingPlan::read_only_buffer`), a fresh region of the command +/// buffer's upload arena at every draw (`write_info`). A draw drawn again as it was kept, or +/// rebound, keeps the kept draw's set-0 writes at every slot but those that changed +/// (`rebind_into`), so it would bind the kept draw's region, which that draw may have written: +/// such a draw is not kept (`render/draw_again.rs`), counted as one that may write +/// (`WorkCounts::draws_unkept_writing`). It walks every write of the plan, one for each resource +/// its heap reaches in each heap array, so a draw asks its plan's facts (`render/plan_facts.rs`), +/// which hold the answer. pub(super) fn binds_own_zeros(plan: &BindingPlan) -> bool { #[cfg(all(test, debug_assertions, any(m2v_gpu, m2v_cpu_icd)))] PLAN_WALKS.with(|walks| walks.set(walks.get() + plan.descriptors.len() as u64)); @@ -2805,7 +2796,7 @@ pub(super) fn binds_own_zeros(plan: &BindingPlan) -> bool { write.source, WriteSource::Neutral(NeutralResource::ZeroBuffer { .. } | NeutralResource::Sink { .. }) ); - neutral && !read_only_buffer(write.ty.into()) + neutral && !plan.read_only_buffer(write) }) } @@ -2870,15 +2861,15 @@ fn slot_descriptor( })) } +/// The buffer descriptor of `write`, one of `plan`'s descriptor writes, for `command` over +/// `tables`. fn descriptor( device: &Device, state: &mut State, - command: &Command<'_>, - tables: &Tables<'_>, + (plan, command, tables): (&BindingPlan, &Command<'_>, &Tables<'_>), (write, spaces): (&DescriptorWrite, Option<&mut KeptSpaces>), ) -> Result { let key = (write.set, write.binding); - let ty: vk::DescriptorType = write.ty.into(); let bytes = || { (command.descriptor_bytes.iter()) .chain(command.heap_tables) @@ -3016,18 +3007,20 @@ fn descriptor( // The command buffer's status words, the ones every command of it writes, on a device // that does not address buffers (`status_address` elsewhere). WriteSource::ExecutionStatus => whole(state.status_words(device)?), - // A read-only binding takes the device's kept zero buffer, made once for its lifetime - // (`neutral.rs`): zeros of a length the device's own limits fix cannot have changed since - // it was made, and before this every draw and dispatch that bound one took a fresh - // zero-filled region, each past half the upload arena's chunk and so a dedicated 64 KiB - // `VkBuffer` allocated and written to zero. A WRITABLE binding keeps its own region, as - // every binding did: the one buffer is shared by every command of the device, so a - // shader that could store through the binding would write zeros another command reads. + // A read-only binding (a uniform buffer, or a uniform view's storage twin: + // `BindingPlan::read_only_buffer`) takes the device's kept zero buffer, made once for its + // lifetime (`neutral.rs`): zeros of a length the device's own limits fix cannot have + // changed since it was made, and before this every draw and dispatch that bound one took + // a fresh zero-filled region, each past half the upload arena's chunk and so a dedicated + // 64 KiB `VkBuffer` allocated and written to zero. A WRITABLE binding keeps its own + // region, as every binding did: the one buffer is shared by every command of the device, + // so a shader that could store through the binding would write zeros another command + // reads. WriteSource::Neutral( NeutralResource::ZeroBuffer { min_bytes } | NeutralResource::Sink { min_bytes }, ) => { let bytes = padded(*min_bytes as usize); - match read_only_buffer(ty) { + match plan.read_only_buffer(write) { true => vk::DescriptorBufferInfo::default() .buffer(device.neutral_zero_buffer(bytes)?) .offset(0) diff --git a/crates/metal2vulkan-runtime/src/neutral.rs b/crates/metal2vulkan-runtime/src/neutral.rs index 526e32ebb..f2eaad495 100644 --- a/crates/metal2vulkan-runtime/src/neutral.rs +++ b/crates/metal2vulkan-runtime/src/neutral.rs @@ -304,8 +304,10 @@ impl Device { /// Only a **read-only** binding may be served from it: every draw and dispatch of the device /// binds the one buffer, so a shader that could store through the binding would be writing /// zeros another command reads. A Vulkan uniform-buffer descriptor is read-only by - /// construction, which is the binding `binding::descriptor` serves from here; a writable one - /// keeps its own region, as before. + /// construction, and so is a uniform view's storage twin, a storage buffer its kernel only + /// loads from (`BindingPlan::read_only_buffer`): those are the bindings `binding::descriptor` + /// serves from here, so the buffer is a storage buffer too. A writable one keeps its own + /// region, as before. pub(super) fn neutral_zero_buffer(&self, bytes: u64) -> Result { let shared = self.shared(); let mut neutrals = lock(&shared.textures.neutrals); @@ -328,15 +330,18 @@ impl Device { /// A new zero buffer of `bytes` bytes: a `Private` buffer filled with zeros, as /// [`Device::new_neutral_texels`] makes its one texel of zeros, so the fill is the device's - /// next submission's first work and waits for nothing. + /// next submission's first work and waits for nothing. A uniform and a storage buffer + /// ([`neutral_zero_buffer`](Device::neutral_zero_buffer)). fn new_neutral_zeros(&self, bytes: u64) -> Result, Error> { let shared = self.shared(); let raw_device = shared.vk(); - let info = shared.sharing().buffer( - vk::BufferCreateInfo::default() - .size(bytes) - .usage(vk::BufferUsageFlags::UNIFORM_BUFFER | vk::BufferUsageFlags::TRANSFER_DST), - ); + let info = shared + .sharing() + .buffer(vk::BufferCreateInfo::default().size(bytes).usage( + vk::BufferUsageFlags::UNIFORM_BUFFER + | vk::BufferUsageFlags::STORAGE_BUFFER + | vk::BufferUsageFlags::TRANSFER_DST, + )); // SAFETY: the device is live and `info` valid. let buffer = unsafe { crate::creation::buffer(raw_device, &info) }.map_err(vulkan)?; // SAFETY: `buffer` is a live buffer of this device. diff --git a/crates/metal2vulkan-runtime/src/tests/deferred_calls.rs b/crates/metal2vulkan-runtime/src/tests/deferred_calls.rs index 28e5fd0c4..1b9ac929a 100644 --- a/crates/metal2vulkan-runtime/src/tests/deferred_calls.rs +++ b/crates/metal2vulkan-runtime/src/tests/deferred_calls.rs @@ -1685,11 +1685,14 @@ fn a_dispatch_whose_pipeline_is_still_being_made_past_its_bound_runs_once_made() } /// A compute state whose descriptor requires a threadgroup size predicts its first dispatch's -/// region at its creation (`pipeline/dispatch_making.rs`). Under `PipelineWaitPolicy::Bounded` -/// (the default) the creation hands that making to the device's pool and, the pool held -/// (`pool::held`), returns at its bound with the making still waiting -/// (`WorkCounts::creations_left_making`), never past the 10 s a guest's FIFO check allows; under -/// `Exact` it makes the region on its own thread before it returns, the pool held or not. +/// region at its creation (`pipeline/dispatch_making.rs`), and on a profile whose storage views +/// declare `SpecKey::RangeAtBindOffset` that region's residual form beside it +/// (`uniform_bind_offsets::storage_views_keyed`: `scale_and_offset` reads two storage views), its +/// forms. Under `PipelineWaitPolicy::Bounded` (the default) the creation hands each form's making +/// to the device's pool and, the pool held (`pool::held`), returns at its bound with every one +/// still waiting (`WorkCounts::creations_left_making`), never past the 10 s a guest's FIFO check +/// allows; under `Exact` every form is made before it returns, the first on its own thread and the +/// other on the pool, which the test does not hold there. #[test] fn a_compute_state_s_creation_waits_its_bound_for_its_predicted_making() { use metal2vulkan::pipeline::{ComputePipelineDescriptor, StageSource}; @@ -1708,8 +1711,9 @@ fn a_compute_state_s_creation_waits_its_bound_for_its_predicted_making() { let mut descriptor = ComputePipelineDescriptor::new(StageSource::Function(function.function_descriptor())); descriptor.required_threads_per_threadgroup = Some([32, 1, 1]); + let forms = 1 + u64::from(super::uniform_bind_offsets::storage_views_keyed(&device)); let counts = device.work_counts(); - let hold = held::hold(); + let hold = (waits == PipelineWaitPolicy::Bounded).then(held::hold); let creating = Instant::now(); let _state = device .new_compute_pipeline_state(&descriptor, &crate::PipelineOptions::default()) @@ -1719,15 +1723,16 @@ fn a_compute_state_s_creation_waits_its_bound_for_its_predicted_making() { drop(hold); let left = after.creations_left_making - counts.creations_left_making; let made = after.creation_dispatch_pipelines_made - counts.creation_dispatch_pipelines_made; + println!("CREATION-MAKINGS: {waits:?}: {forms} forms, {left} left making, {made} made"); match waits { PipelineWaitPolicy::Bounded => { - assert_eq!((left, made), (1, 0), "the creation's making, held"); + assert_eq!((left, made), (forms, 0), "the creation's makings, held"); assert!( (CREATION_WAIT..COMMIT_WAIT).contains(&created), "the creation took {created:?}, not its own bound" ); } - _ => assert_eq!((left, made), (0, 1), "the creation's making, made"), + _ => assert_eq!((left, made), (0, forms), "the creation's makings, made"), } } } diff --git a/crates/metal2vulkan-runtime/src/tests/neutral_zeros_shared.rs b/crates/metal2vulkan-runtime/src/tests/neutral_zeros_shared.rs index ec30f01b5..009499668 100644 --- a/crates/metal2vulkan-runtime/src/tests/neutral_zeros_shared.rs +++ b/crates/metal2vulkan-runtime/src/tests/neutral_zeros_shared.rs @@ -19,10 +19,13 @@ //! # Why only a read-only binding //! //! One buffer shared by every command of the device is sound only where no shader can store -//! through the binding. A Vulkan uniform-buffer descriptor is read-only by construction, so that -//! is the binding served from it (`binding::read_only_buffer`, decided from the descriptor type -//! alone). A writable binding keeps a region of its own, exactly as every binding did, so nothing -//! a shader stores is ever read by another command. +//! through the binding. A Vulkan uniform-buffer descriptor is read-only by construction, and so is +//! a uniform view's storage twin, a storage buffer its kernel only loads from (a kernel's +//! `constant` slot on a profile that takes `DriverWorkaround::PushReadsLengthenThreadPayload`, +//! where the unbound slot below binds both): those are the bindings served from it +//! (`BindingPlan::read_only_buffer`, decided from the plan's layout alone, never a function or +//! shader name). A writable binding keeps a region of its own, exactly as every binding did, so +//! nothing a shader stores is ever read by another command. //! //! # Counts, not times //! diff --git a/crates/metal2vulkan-runtime/src/tests/set_zero_at_offsets.rs b/crates/metal2vulkan-runtime/src/tests/set_zero_at_offsets.rs index 36b21bd14..4187a992b 100644 --- a/crates/metal2vulkan-runtime/src/tests/set_zero_at_offsets.rs +++ b/crates/metal2vulkan-runtime/src/tests/set_zero_at_offsets.rs @@ -7,7 +7,13 @@ //! (`WorkCounts::draws_pushing_set_zero`: a device of Vulkan 1.4 or `VK_KHR_push_descriptor` //! whose set 0 a push descriptor set layout holds), the first draw's push carries its writes and //! no later draw pushes, since set 0 holds them (`render/bound.rs`). They draw what the same draws -//! draw from buffers of their own each, which write a set each. +//! draw from buffers of their own each, which write a set each. Where storage views declare +//! `SpecKey::RangeAtBindOffset` (a profile that takes +//! `DriverWorkaround::PushReadsLengthenThreadPayload`) a draw of a buffer at offset 0 takes its own +//! key's pipeline and a draw past it the residual form's, and where the driver's pushed +//! descriptors go stale across a pipeline bind +//! (`RuntimeDriverDefect::PushedDescriptorsStaleAfterPipelineBinds`) the push after that bind +//! carries one write set 0 holds already: a push more, its descriptors unchanged. //! //! Device test: compiled only under the GPU safety cfgs (`m2v_gpu`, `m2v_cpu_icd`). The functions //! are `colored_vertex` and `colored_fragment` of the owned fixture @@ -72,7 +78,7 @@ fn buffer_of(device: &Device, values: &[f32]) -> Buffer { /// What a run drew and counted: the target's words, its `set_zero_sets_reused`, /// `draws_rebound` and `draws_pushing_set_zero`, and its `vkCmdBindDescriptorSets`, -/// `vkCmdPushDescriptorSet` and `vkCmdDraw` calls. +/// `vkCmdPushDescriptorSet`, `vkCmdDraw` and `vkCmdBindPipeline` calls. struct Ran { words: Vec, reused: u64, @@ -81,6 +87,7 @@ struct Ran { binds: u64, pushes: u64, draws: u64, + pipelines: u64, } /// Encode `encode` with `state` set in one pass into a new cleared `RGBA8Unorm` target, commit and @@ -134,6 +141,7 @@ fn run( binds: grew(|counts| counts.commands_recorded.cmd_bind_descriptor_sets), pushes: grew(|counts| counts.commands_recorded.cmd_push_descriptor_set), draws: grew(|counts| counts.commands_recorded.cmd_draw), + pipelines: grew(|counts| counts.commands_recorded.cmd_bind_pipeline), } } @@ -232,14 +240,32 @@ fn draws_that_move_only_a_device_buffer_s_offset_bind_one_set_zero() { "the draws of their own buffers" ); assert_eq!((at_offsets.draws, own_buffers.draws), (DRAWS, DRAWS)); + // Where storage views declare their key, the first draw, of both buffers at offset 0, takes + // its own key's pipeline (`pipeline/render/residual.rs`), and the others their residual form's. + let keyed = super::uniform_bind_offsets::storage_views_keyed(&device); + let switches = u64::from(keyed); + // Where the driver's pushed descriptors go stale across a pipeline bind, the push after it + // carries one write set 0 holds already (`render/bound.rs`). + let stale = device.shared().pushes_after_pipeline_binds; + println!( + "SET-ZERO-AT-OFFSETS: {} pipeline binds, {} pushes; storage views keyed: {keyed}, \ + pushes after pipeline binds: {stale}", + at_offsets.pipelines, at_offsets.pushes + ); + assert_eq!( + at_offsets.pipelines, + 1 + switches, + "the draws at offsets bind their pipelines" + ); let (once, each) = match pushes_set_zero(&[&at_offsets, &own_buffers]) { false => ((DRAWS - 1, 1, 0), (0, DRAWS, 0)), - true => ((0, 0, 1), (0, 0, DRAWS)), + true => ((0, 0, 1 + switches * u64::from(stale)), (0, 0, DRAWS)), }; assert_eq!( set_zero(&at_offsets), once, - "the draws at offsets record their first draw's set 0, once" + "the draws at offsets record their first draw's set 0, once, and push it again only after \ + a pipeline bind where the driver's pushed descriptors go stale across one" ); assert_eq!( set_zero(&own_buffers), diff --git a/crates/metal2vulkan/src/bind/plan.rs b/crates/metal2vulkan/src/bind/plan.rs index 509c60f8f..d84380f6f 100644 --- a/crates/metal2vulkan/src/bind/plan.rs +++ b/crates/metal2vulkan/src/bind/plan.rs @@ -78,6 +78,26 @@ impl BindingPlan { .find(|(k, _)| *k == key) .map(|(_, v)| u64::from(*v)) } + + /// Whether no function of the plan's pipeline can store through the buffer binding `write` + /// (one of [`descriptors`](Self::descriptors)) writes: a uniform buffer, whose block Vulkan + /// makes read-only, or a uniform view's storage twin (`DescriptorView::UniformWithStorageTwin`), + /// a storage buffer the function only loads from. A consumer may bind there one buffer that + /// every command shares, such as the zeros of a `NeutralResource::ZeroBuffer` it keeps for the + /// device's lifetime; through any other storage buffer a function may store, so such a binding + /// takes a buffer of its command's own. + pub fn read_only_buffer(&self, write: &DescriptorWrite) -> bool { + let at = (write.set, write.binding); + let twin = |buffers: &[PlannedBuffer]| buffers.iter().any(|b| b.twin == Some(at)); + match write.ty { + vk::DescriptorType::UNIFORM_BUFFER | vk::DescriptorType::UNIFORM_BUFFER_DYNAMIC => true, + vk::DescriptorType::STORAGE_BUFFER => { + (self.compute.as_ref()).is_some_and(|c| twin(&c.buffers)) + || (self.graphics.as_ref()).is_some_and(|g| twin(&g.buffers)) + } + _ => false, + } + } } // Adding a field to a plan type: a plan's content digest omits a field whose value is zero (`0`, diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 51d24077e..1fe3b8e13 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 523; /// changes no behaviour, and then updates that hash alone with the commit saying why, or changes /// behaviour and also bumps this version. The commit that bumps it says what changed; this comment /// keeps no history. -pub const RUNTIME_VERSION: u32 = 2419; +pub const RUNTIME_VERSION: u32 = 2420; /// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex, /// of the manifest, the build script, and exactly the files the crate roots' module tree compiles diff --git a/crates/metal2vulkan/src/tests/engine_access.rs b/crates/metal2vulkan/src/tests/engine_access.rs index 9cc2e373e..aa7dbe847 100644 --- a/crates/metal2vulkan/src/tests/engine_access.rs +++ b/crates/metal2vulkan/src/tests/engine_access.rs @@ -690,6 +690,82 @@ fn a_storage_twin_is_a_kernels_on_its_profile_within_the_limits() { link_and_validate(&a, &p).unwrap_or_else(|e| panic!("room: L3: {e}")); } +/// A plan states which of its buffer writes no function stores through +/// (`BindingPlan::read_only_buffer`), so a consumer may bind one buffer every command shares +/// there. On the profile that takes `DriverWorkaround::PushReadsLengthenThreadPayload`, a kernel's +/// `constant` slot bound to nothing writes its uniform binding and its storage twin from the +/// `ZeroBuffer` neutral, and both are read-only: the twin's is a storage buffer, so its descriptor +/// type alone would call it writable. The written `device` slot's storage view is not read-only. On +/// every other tested profile, the negative control, the slot has no twin, and a write is +/// read-only exactly where it is a uniform buffer's. +#[test] +fn an_unbound_constant_slot_s_storage_twin_is_a_read_only_write() { + use crate::bind::{BindState, DescriptorWrite, NeutralResource, WriteSource}; + use crate::pipeline::layout::BindingSource; + use crate::pipeline::{ComputePipelineDescriptor, FunctionDescriptor, StageSource}; + let entry = "buffer_end_byte_load_constant"; + let Some(bc) = assemble_file(entry) else { + return; + }; + let library = crate::library::Library::from_air(&bc).expect("library"); + for stem in [AT_BIND_PROFILE].into_iter().chain(PROFILES) { + let device = super::engine_argument_tables::device(stem); + let descriptor = ComputePipelineDescriptor::new(StageSource::Function( + FunctionDescriptor::new(library.function(entry).expect("entry")), + )); + let pipeline = device + .compile_compute_pipeline(&descriptor, &crate::budget::Budget::standard()) + .unwrap_or_else(|e| panic!("{stem}: {e}")); + let plan = pipeline + .plan(&device, &BindState::default().shape()) + .unwrap_or_else(|e| panic!("{stem}: {e}")); + let source = |write: &DescriptorWrite| { + (pipeline.layout.sets.iter()) + .filter(|set| set.set == write.set) + .flat_map(|set| &set.bindings) + .find(|b| b.binding == write.binding) + .map(|b| b.source) + }; + let mut writes = BTreeMap::new(); + for write in &plan.descriptors { + let Some(at @ (BindingSource::Buffer(_) | BindingSource::StorageTwin(_))) = + source(write) + else { + continue; + }; + assert!( + matches!( + write.source, + WriteSource::Neutral(NeutralResource::ZeroBuffer { .. }) + ), + "{stem} {at:?}: {:?}", + write.source + ); + writes.insert(at, (write.ty, plan.read_only_buffer(write))); + } + let uniform = crate::vk::DescriptorType::UNIFORM_BUFFER; + let storage = crate::vk::DescriptorType::STORAGE_BUFFER; + let twin = BindingSource::StorageTwin(sref(0)); + match stem == AT_BIND_PROFILE { + true => assert_eq!( + writes, + BTreeMap::from([ + (BindingSource::Buffer(sref(0)), (uniform, true)), + (BindingSource::Buffer(sref(1)), (storage, false)), + (twin, (storage, true)), + ]), + "{stem}" + ), + false => { + assert!(!writes.contains_key(&twin), "{stem}"); + for (at, (ty, read_only)) in &writes { + assert_eq!(*read_only, *ty == uniform, "{stem} {at:?}"); + } + } + } + } +} + /// A `constant` struct of 6 bytes whose first and last bytes are read: its window starts at 0. const CONSTANT_ENDS: &str = r#"define void @constant_ends(ptr addrspace(2) noundef "air-buffer-no-alias" %0, ptr addrspace(1) noundef "air-buffer-no-alias" %1) { entry: diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index d7617a542..a11c7ff31 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -9487,6 +9487,7 @@ method metal2vulkan::bind::BindingPlan::instantiate_draw pub fn instantiate_draw method metal2vulkan::bind::BindingPlan::instantiate_draw_apart_into pub fn instantiate_draw_apart_into(&self, state: &BindState, draw: &DrawCall, commands: &mut DrawCommands) -> Result<(), BindError> method metal2vulkan::bind::BindingPlan::instantiate_draw_into pub fn instantiate_draw_into(&self, state: &BindState, draw: &DrawCall, commands: &mut DrawCommands) -> Result<(), BindError> method metal2vulkan::bind::BindingPlan::instantiate_replay pub fn instantiate_replay(&self, state: &BindState, threads_per_group: [u32; 3]) -> Result +method metal2vulkan::bind::BindingPlan::read_only_buffer pub fn read_only_buffer(&self, write: &DescriptorWrite) -> bool method metal2vulkan::bind::BindingPlan::rebind_draw_slots_into pub fn rebind_draw_slots_into(&self, state: &BindState, slots: &[SlotRef], commands: &mut DrawCommands) -> Result method metal2vulkan::bind::BindingPlan::rebind_draw_slots_with pub fn rebind_draw_slots_with(&self, rebinds: &SlotRebinds, state: &BindState, commands: &mut DrawCommands) -> Result method metal2vulkan::bind::BindingPlan::redraw_apart_into pub fn redraw_apart_into(&self, state: &BindState, (from, draw): (&DrawCall, &DrawCall), commands: &mut DrawCommands) -> Result diff --git a/docs/API.md b/docs/API.md index 8cdc89c35..f113f8b26 100644 --- a/docs/API.md +++ b/docs/API.md @@ -1513,7 +1513,10 @@ for dependency in &pipeline.dependencies { buffer, offset and range, from the same bind offset: a plan writes both from the slot's own source (`WriteSource::Buffer`, `InlineBytes` or `Neutral`), an instance's `DescriptorRange`s give both bindings one range, and the plan's `uses` hold a `Storage` read of the slot beside the - `Uniform` one. Where the + `Uniform` one. No function stores through the twin: `BindingPlan::read_only_buffer` holds for + both of the slot's writes, as for every uniform buffer's, so a slot bound to nothing may bind + one zero buffer that every command shares at both (the runtime's, made as a uniform and a + storage buffer); a write it does not hold for may be stored through. Where the slot's `RangeAtBindOffset` holds, the kernel reads the twin at its bind offset alone; elsewhere the uniform view at its residual, so the same specialization and range serve either. The key is a uniform view's (a bind offset the view base alignment divides, at a slot whose table tells it