From ab8651ae384832aee6f569d1e533d93f4ece0168 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 7 Nov 2026 22:16:59 -0800 Subject: [PATCH] Run a device call again on the serving generation where its own was lost under it tests::device_recovery::each_recovery_announces_its_own_wait failed on the M3 in 10 of 30 runs at 2fed3c2453 (m3-max-7120): a waiter thread's `device.new_buffer(..)` returned NotImplemented(MadeBeforeRecovery) while another thread recovered the device. A public call of a root handle took the serving generation (Device::current -> Line::current), the recovery marked it lost, and the call's own check of the loss (new_buffer_placed's is_lost) then refused it through lost_refusal, whose line had gone past the generation. The call came from the device the program holds, which Metal keeps across a recovery: it must not be refused because the generation it took was lost while it ran (rule 2). The device's public calls now run through Device::at_serving: a run refused for its generation's loss (DeviceLost, or MadeBeforeRecovery as lost_refusal answers it) where the line went past that generation runs again on the one serving next. A run refused so kept nothing, so the call runs whole again. Each run is on a later generation than the one before, after its recovery, which Line::current waits for, announced past RECOVERY_NOTICE as before; a line that cannot recover still answers the loss, and a thread command buffers' completion depends on, whose calls reach the lost generation, is answered as before (no retry). A call refused for an object of an earlier generation while its own generation serves is answered as before. The calls that consume arguments a refused run could not hand to a second one (host memory and its deallocator or keep-alive, a range token, a buffer descriptor's contents) run through Device::at_serving_taking: they go to the generation once it is not lost, else to the next, and from there a loss of it follows the call: the thread's RunsOn scope makes Device::is_lost answer false for that generation to the call's own checks, so the call completes on it as one the loss came after, its object made before the recovery. The two texture calls with a range token make the texture through at_serving and keep the token on it after (their _now forms, used by nothing else, are gone). The calls that wrap the consumer's own Vulkan objects (new_texture_with_vk_image, _plane, new_buffer_with_vk_buffer) are unchanged: those objects belong to the VkDevice the caller made them on. Tests: device_recovery gains a call whose first run meets a recovery and runs again on the next generation, and a taking call that completes on the generation lost after it took its arguments. Docs: the runtime README's lost device section, API.md's device-recovered notice, line.rs's module docs. (cherry picked from commit 3af6741dcf79b99a608ea0a464b98734dac52ba5) --- crates/metal2vulkan-runtime/README.md | 6 +- .../metal2vulkan-runtime/src/address_pool.rs | 2 +- .../src/argument_encoder.rs | 6 +- .../src/binary_archive/mod.rs | 2 +- crates/metal2vulkan-runtime/src/buffer.rs | 2 +- .../src/buffer_descriptor.rs | 17 +-- .../metal2vulkan-runtime/src/depth_stencil.rs | 2 +- .../metal2vulkan-runtime/src/device/line.rs | 110 +++++++++++++++++- crates/metal2vulkan-runtime/src/device/mod.rs | 6 +- .../src/dynamic_library.rs | 4 +- crates/metal2vulkan-runtime/src/event.rs | 6 +- .../src/exported_memory.rs | 2 +- .../src/exported_semaphore.rs | 2 +- crates/metal2vulkan-runtime/src/heap.rs | 35 +++--- crates/metal2vulkan-runtime/src/host_ram.rs | 2 +- .../src/host_shared_pointers.rs | 2 +- .../src/indirect_command_buffer.rs | 5 +- crates/metal2vulkan-runtime/src/library.rs | 4 +- .../metal2vulkan-runtime/src/pipeline/mod.rs | 18 +-- .../src/pipeline/render.rs | 16 +-- .../metal2vulkan-runtime/src/pipeline/tile.rs | 8 +- crates/metal2vulkan-runtime/src/queue.rs | 9 +- crates/metal2vulkan-runtime/src/rate_map.rs | 2 +- .../metal2vulkan-runtime/src/resource_ids.rs | 2 +- crates/metal2vulkan-runtime/src/sampler.rs | 5 +- crates/metal2vulkan-runtime/src/sparse/mod.rs | 14 ++- .../src/tests/device_recovery.rs | 47 ++++++++ crates/metal2vulkan-runtime/src/texture.rs | 14 +-- .../metal2vulkan-runtime/src/texture/heap.rs | 5 +- .../src/texture/shared.rs | 18 +-- docs/API.md | 2 +- 31 files changed, 255 insertions(+), 120 deletions(-) diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 07769d370..00e2fe0ce 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -631,7 +631,11 @@ meanwhile wait for it. Metal's does: a command buffer committed on it from then on, before the recovery too, fails with `AccessRevoked`. - The `Device` and its `CommandQueue`s go on: what they make from then on is made on the new - `VkDevice`. + `VkDevice`. A `Device` call that the loss meets while it runs is not refused for it: it runs + again on the new `VkDevice`. One that takes host memory, a deallocator or a range token, which a + second run could not have, runs on the new `VkDevice` where the loss came before it took them, + and completes on the lost one where the loss came after, its object then one made before the + recovery. - An object made before the recovery is refused where it meets the new `VkDevice` (an encoder's bind, a pipeline made from its functions) and in its own calls that need its lost one: `Error::NotImplemented(RuntimeGap::MadeBeforeRecovery)`. Make it again. Metal's objects go on. diff --git a/crates/metal2vulkan-runtime/src/address_pool.rs b/crates/metal2vulkan-runtime/src/address_pool.rs index d09191f58..c48af1d24 100644 --- a/crates/metal2vulkan-runtime/src/address_pool.rs +++ b/crates/metal2vulkan-runtime/src/address_pool.rs @@ -70,7 +70,7 @@ impl Device { placement: AddressPlacement, length: u64, ) -> Result { - self.current().reserve_address_pool_now(placement, length) + self.at_serving(|device| device.reserve_address_pool_now(placement, length)) } /// [`Device::reserve_address_pool`] on the generation that serves (`device/line.rs`). diff --git a/crates/metal2vulkan-runtime/src/argument_encoder.rs b/crates/metal2vulkan-runtime/src/argument_encoder.rs index 1b1053911..6d318b6f1 100644 --- a/crates/metal2vulkan-runtime/src/argument_encoder.rs +++ b/crates/metal2vulkan-runtime/src/argument_encoder.rs @@ -182,8 +182,7 @@ impl Device { &self, arguments: &[ArgumentDescriptor], ) -> Result { - self.current() - .new_argument_encoder_with_arguments_now(arguments) + self.at_serving(|device| device.new_argument_encoder_with_arguments_now(arguments)) } /// [`Device::new_argument_encoder_with_arguments`] on the generation that serves @@ -214,8 +213,7 @@ impl Device { &self, binding: &BufferBinding, ) -> Result { - self.current() - .new_argument_encoder_with_buffer_binding_now(binding) + self.at_serving(|device| device.new_argument_encoder_with_buffer_binding_now(binding)) } /// [`Device::new_argument_encoder_with_buffer_binding`] on the generation that serves diff --git a/crates/metal2vulkan-runtime/src/binary_archive/mod.rs b/crates/metal2vulkan-runtime/src/binary_archive/mod.rs index eac8af89f..0f84650fc 100644 --- a/crates/metal2vulkan-runtime/src/binary_archive/mod.rs +++ b/crates/metal2vulkan-runtime/src/binary_archive/mod.rs @@ -110,7 +110,7 @@ impl Device { &self, descriptor: &BinaryArchiveDescriptor, ) -> Result { - self.current().new_binary_archive_now(descriptor) + self.at_serving(|device| device.new_binary_archive_now(descriptor)) } /// [`Device::new_binary_archive`] on the generation that serves (`device/line.rs`). diff --git a/crates/metal2vulkan-runtime/src/buffer.rs b/crates/metal2vulkan-runtime/src/buffer.rs index d3d7d79d8..d1a7fdfc7 100644 --- a/crates/metal2vulkan-runtime/src/buffer.rs +++ b/crates/metal2vulkan-runtime/src/buffer.rs @@ -2074,7 +2074,7 @@ impl Device { /// answers nil, its validation stopping it), `Memoryless` storage /// (`Invalid`). pub fn new_buffer(&self, length: u64, options: ResourceOptions) -> Result { - self.current().new_buffer_now(length, options) + self.at_serving(|device| device.new_buffer_now(length, options)) } /// [`Device::new_buffer`] on the generation that serves (`device/line.rs`). diff --git a/crates/metal2vulkan-runtime/src/buffer_descriptor.rs b/crates/metal2vulkan-runtime/src/buffer_descriptor.rs index 33289adfb..bd9dd7834 100644 --- a/crates/metal2vulkan-runtime/src/buffer_descriptor.rs +++ b/crates/metal2vulkan-runtime/src/buffer_descriptor.rs @@ -201,7 +201,9 @@ impl Device { &self, descriptor: BufferDescriptor<'_>, ) -> Result { - self.current().new_buffer_with_descriptor_now(descriptor) + self.at_serving_taking(descriptor, |device, descriptor| { + device.new_buffer_with_descriptor_now(descriptor) + }) } /// [`Device::new_buffer_with_descriptor`] on the generation that serves (`device/line.rs`). @@ -289,7 +291,7 @@ impl Device { bytes: &[u8], options: ResourceOptions, ) -> Result { - self.current().new_buffer_with_bytes_now(bytes, options) + self.at_serving(|device| device.new_buffer_with_bytes_now(bytes, options)) } /// [`Device::new_buffer_with_bytes`] on the generation that serves (`device/line.rs`). @@ -359,11 +361,12 @@ impl Device { options: ResourceOptions, deallocator: Option, ) -> Result { - // SAFETY: the caller's contract. - unsafe { - self.current() - .new_buffer_with_bytes_no_copy_now(memory, length, options, deallocator) - } + self.at_serving_taking((memory, deallocator), |device, (memory, deallocator)| { + // SAFETY: the caller's contract. + unsafe { + device.new_buffer_with_bytes_no_copy_now(memory, length, options, deallocator) + } + }) } /// [`Device::new_buffer_with_bytes_no_copy`] on the generation that serves (`device/line.rs`). diff --git a/crates/metal2vulkan-runtime/src/depth_stencil.rs b/crates/metal2vulkan-runtime/src/depth_stencil.rs index 97b086858..4f50ea4dc 100644 --- a/crates/metal2vulkan-runtime/src/depth_stencil.rs +++ b/crates/metal2vulkan-runtime/src/depth_stencil.rs @@ -56,7 +56,7 @@ impl Device { &self, descriptor: &DepthStencilDescriptor, ) -> Result { - self.current().new_depth_stencil_state_now(descriptor) + self.at_serving(|device| device.new_depth_stencil_state_now(descriptor)) } /// [`Device::new_depth_stencil_state`] on the generation that serves (`device/line.rs`). diff --git a/crates/metal2vulkan-runtime/src/device/line.rs b/crates/metal2vulkan-runtime/src/device/line.rs index 47d76a28c..85d37bb59 100644 --- a/crates/metal2vulkan-runtime/src/device/line.rs +++ b/crates/metal2vulkan-runtime/src/device/line.rs @@ -10,11 +10,14 @@ //! - **Two kinds of handle.** A *root* handle (the device the program made, `Device::system_default` //! and the rest, and every clone of it) holds the line and no generation: every call made //! through it goes to the generation serving when it is made (the public calls' -//! [`Device::current`]). A *generation* handle holds one generation: the device of every object -//! made on it (a buffer's, a pipeline state's), through which the object's Vulkan calls reach -//! its own `VkDevice` alone, its drop included. So no object's Vulkan handle ever reaches -//! another generation's `VkDevice`, and an object made before a recovery is refused where it -//! meets the new generation (`RuntimeGap::MadeBeforeRecovery`, `error.rs`). +//! [`Device::current`]), and again to the one serving next where that one was lost under the +//! call and refused it for that ([`Device::at_serving`]; a call that takes host memory or a +//! range token, which it cannot take twice, is not refused for a loss once it took them, +//! [`Device::at_serving_taking`]). A *generation* handle holds one generation: the device of +//! every object made on it (a buffer's, a pipeline state's), through which the object's Vulkan +//! calls reach its own `VkDevice` alone, its drop included. So no object's Vulkan handle ever +//! reaches another generation's `VkDevice`, and an object made before a recovery is refused +//! where it meets the new generation (`RuntimeGap::MadeBeforeRecovery`, `error.rs`). //! - **The states.** [`State`]: the line serves one generation (`Live`); once it is lost, the //! first call that needs the device recovers it (`Lost`, then claimed), while every other call //! waits for that recovery, the first to wait past [`RECOVERY_NOTICE`] announcing it and waiting @@ -58,6 +61,7 @@ use crate::unique_identifier::UniqueIdentifiers; use crate::work_counts::Counters; use ash::vk; use metal2vulkan::device::EnableSet; +use std::cell::Cell; use std::ffi::CString; use std::path::PathBuf; use std::sync::atomic::{AtomicU64, Ordering}; @@ -539,6 +543,37 @@ impl Line { } } +thread_local! { + /// The generation the thread's public call runs on past its check of the generation's loss, + /// its arguments taken ([`Device::at_serving_taking`]): the address of its `DeviceShared`. + static RUNS_ON: Cell = const { Cell::new(0) }; +} + +/// The thread's public call running on a generation past its check of the generation's loss +/// ([`Device::at_serving_taking`]): while it lives, the generation is not lost to the call's own +/// checks ([`runs_on`]). What it replaced comes back as it drops. +struct RunsOn(usize); + +impl RunsOn { + fn enter(shared: &DeviceShared) -> RunsOn { + let address = std::ptr::from_ref(shared).addr(); + RunsOn(RUNS_ON.with(|runs| runs.replace(address))) + } +} + +impl Drop for RunsOn { + fn drop(&mut self) { + RUNS_ON.with(|runs| runs.set(self.0)); + } +} + +/// Whether the thread's public call runs on `shared` past its check of its loss ([`RunsOn`]): +/// a loss of it since follows the call, which its checks do not refuse (`Device::is_lost`). +#[cold] +pub(super) fn runs_on(shared: &DeviceShared) -> bool { + RUNS_ON.with(|runs| runs.get() == std::ptr::from_ref(shared).addr()) +} + /// The device a call goes to ([`Device::current`]): the caller's own handle, or one of the /// generation that serves. pub(crate) enum Current<'a> { @@ -600,6 +635,71 @@ impl Device { } } + /// Run `call`, a public call of this handle, on the generation it goes to + /// ([`current`](Self::current)), and again on the one serving next wherever the generation it + /// ran on was lost under it and refused it for that ([`lost_under`](Self::lost_under)): a + /// call of the device Metal keeps across a recovery is not refused because the generation it + /// took was lost while it ran. Refused so, a run kept nothing it made, so the call runs whole + /// again. Bounded as the line's waits are: each run is on a later generation than the one + /// before, after its recovery, which [`Line::current`] waits for (announced past + /// [`RECOVERY_NOTICE`]); a line that cannot recover answers the loss. + pub(crate) fn at_serving( + &self, + mut call: impl FnMut(&Device) -> Result, + ) -> Result { + loop { + let device = self.current(); + match call(&*device) { + Err(refused) if device.lost_under(&refused) => continue, + answer => return answer, + } + } + } + + /// [`at_serving`](Self::at_serving) of a call that takes `taken`, arguments it consumes (host + /// memory and what gives it back, a range token), which a run refused for a loss could not + /// give a later one: they go to the generation the call goes to once it is not lost, else to + /// the one serving next, and the call then runs on it whole, a loss of it from then on + /// following the call, as one after the call returned would. Its checks of the generation's + /// loss answer so ([`RunsOn`]): the call is not refused for a loss once it took its + /// arguments, and answers as on a generation lost after it (its objects of that generation, + /// refused where they meet the next: `RuntimeGap::MadeBeforeRecovery`). A line that cannot + /// recover refuses it as before. + pub(crate) fn at_serving_taking( + &self, + taken: A, + call: impl FnOnce(&Device, A) -> Result, + ) -> Result { + loop { + let device = self.current(); + if device.shared().lost() { + let refused = device.lost_refusal(); + if device.lost_under(&refused) { + continue; + } + return Err(refused); + } + let _runs = RunsOn::enter(device.shared()); + return call(&*device, taken); + } + } + + /// Whether `refused`, a call's answer on this generation, is the refusal of its loss + /// (`DeviceLost`, or `RuntimeGap::MadeBeforeRecovery` as [`lost_refusal`](Self::lost_refusal) + /// answers it) and the line went past it: the call goes to the generation serving next + /// ([`at_serving`](Self::at_serving)). A call refused for an object of an earlier generation + /// while its own still serves is answered so, as is one of a thread command buffers' + /// completion depends on, whose calls reach the lost generation ([`Line::current`]). + #[cold] + fn lost_under(&self, refused: &Error) -> bool { + let loss = matches!( + refused, + Error::DeviceLost | Error::NotImplemented(RuntimeGap::MadeBeforeRecovery) + ); + loss && !crate::exit_guard::serving_completion() + && self.line().recovers_past(self.shared().generation) + } + /// The refusal of a call that reaches this generation after its loss: where the device's line /// recovers past it, the call's object was made before the recovery /// (`RuntimeGap::MadeBeforeRecovery`); else the lost device's (`DeviceLost`). diff --git a/crates/metal2vulkan-runtime/src/device/mod.rs b/crates/metal2vulkan-runtime/src/device/mod.rs index 3fd7ec6d4..315115074 100644 --- a/crates/metal2vulkan-runtime/src/device/mod.rs +++ b/crates/metal2vulkan-runtime/src/device/mod.rs @@ -1300,9 +1300,11 @@ impl Device { Arc::ptr_eq(self.line(), other.line()) } - /// Whether the device was lost. + /// Whether the device was lost: not to a public call that took its arguments on it before + /// its loss, which the loss follows (`Device::at_serving_taking`, `line.rs`). pub(super) fn is_lost(&self) -> bool { - self.shared().lost() + let shared = self.shared(); + shared.lost() && !line::runs_on(shared) } /// Record that the device was lost: its line recovers at the next call that needs the device, diff --git a/crates/metal2vulkan-runtime/src/dynamic_library.rs b/crates/metal2vulkan-runtime/src/dynamic_library.rs index 4f3655010..f14409113 100644 --- a/crates/metal2vulkan-runtime/src/dynamic_library.rs +++ b/crates/metal2vulkan-runtime/src/dynamic_library.rs @@ -45,7 +45,7 @@ impl Device { /// code reads a function constant is refused (`Compile(InvalidDescriptor(DynamicLibrary))`), /// as Metal refuses each. A library of another device is refused (`Invalid(ForeignObject)`). pub fn new_dynamic_library(&self, library: &Library) -> Result { - self.current().new_dynamic_library_now(library) + self.at_serving(|device| device.new_dynamic_library_now(library)) } /// [`Device::new_dynamic_library`] on the generation that serves (`device/line.rs`). @@ -69,7 +69,7 @@ impl Device { /// `new_dynamic_library` takes is refused as it refuses it, where Metal's answer for a file is /// not measured. pub fn new_dynamic_library_with_url(&self, url: &Path) -> Result { - self.current().new_dynamic_library_with_url_now(url) + self.at_serving(|device| device.new_dynamic_library_with_url_now(url)) } /// [`Device::new_dynamic_library_with_url`] on the generation that serves (`device/line.rs`). diff --git a/crates/metal2vulkan-runtime/src/event.rs b/crates/metal2vulkan-runtime/src/event.rs index 5c790eb2f..484567e13 100644 --- a/crates/metal2vulkan-runtime/src/event.rs +++ b/crates/metal2vulkan-runtime/src/event.rs @@ -596,7 +596,7 @@ fn due(state: &mut EventState) -> Vec { impl Device { /// `newEvent`. pub fn new_event(&self) -> Result { - self.current().new_event_now() + self.at_serving(|device| device.new_event_now()) } /// [`Device::new_event`] on the generation that serves (`device/line.rs`). @@ -606,7 +606,7 @@ impl Device { /// `newSharedEvent`. pub fn new_shared_event(&self) -> Result { - self.current().new_shared_event_now() + self.at_serving(|device| device.new_shared_event_now()) } /// [`Device::new_shared_event`] on the generation that serves (`device/line.rs`). @@ -632,7 +632,7 @@ impl Device { &self, handle: &SharedEventHandle, ) -> Result { - self.current().new_shared_event_with_handle_now(handle) + self.at_serving(|device| device.new_shared_event_with_handle_now(handle)) } /// [`Device::new_shared_event_with_handle`] on the generation that serves (`device/line.rs`). diff --git a/crates/metal2vulkan-runtime/src/exported_memory.rs b/crates/metal2vulkan-runtime/src/exported_memory.rs index 7586661d6..f878b6caf 100644 --- a/crates/metal2vulkan-runtime/src/exported_memory.rs +++ b/crates/metal2vulkan-runtime/src/exported_memory.rs @@ -139,7 +139,7 @@ impl Device { length: u64, options: ResourceOptions, ) -> Result { - self.current().new_exportable_buffer_now(length, options) + self.at_serving(|device| device.new_exportable_buffer_now(length, options)) } /// [`Device::new_exportable_buffer`] on the generation that serves (`device/line.rs`). diff --git a/crates/metal2vulkan-runtime/src/exported_semaphore.rs b/crates/metal2vulkan-runtime/src/exported_semaphore.rs index 726c885ab..65df51d24 100644 --- a/crates/metal2vulkan-runtime/src/exported_semaphore.rs +++ b/crates/metal2vulkan-runtime/src/exported_semaphore.rs @@ -95,7 +95,7 @@ impl Device { /// `VK_KHR_external_semaphore_fd` or that exports no timeline semaphore as an opaque file /// descriptor. pub fn new_exportable_shared_event(&self) -> Result { - self.current().new_exportable_shared_event_now() + self.at_serving(|device| device.new_exportable_shared_event_now()) } /// [`Device::new_exportable_shared_event`] on the generation that serves (`device/line.rs`). diff --git a/crates/metal2vulkan-runtime/src/heap.rs b/crates/metal2vulkan-runtime/src/heap.rs index 109037c36..2839a2830 100644 --- a/crates/metal2vulkan-runtime/src/heap.rs +++ b/crates/metal2vulkan-runtime/src/heap.rs @@ -489,7 +489,7 @@ impl Device { /// `maxBufferSize` (`NotImplemented(HeapPastMaxBufferSize)`); a pinned address whose range /// meets another resource's bytes or leaves the space (`PlacementConflict`). pub fn new_heap(&self, descriptor: &HeapDescriptor) -> Result { - self.current().new_heap_now(descriptor) + self.at_serving(|device| device.new_heap_now(descriptor)) } /// [`Device::new_heap`] on the generation that serves (`device/line.rs`). @@ -509,8 +509,9 @@ impl Device { descriptor: &HeapDescriptor, token: RangeToken, ) -> Result { - self.current() - .new_heap_with_range_token_now(descriptor, token) + self.at_serving_taking(token, |device, token| { + device.new_heap_with_range_token_now(descriptor, token) + }) } /// [`Device::new_heap_with_range_token`] on the generation that serves (`device/line.rs`). @@ -608,15 +609,13 @@ impl Device { deallocator: Option, keep_alive: Option, ) -> Result { - // SAFETY: the caller's contract. - unsafe { - self.current().new_heap_with_bytes_no_copy_now( - memory, - descriptor, - deallocator, - keep_alive, - ) - } + let taken = (memory, deallocator, keep_alive); + self.at_serving_taking(taken, |device, (memory, deallocator, keep_alive)| { + // SAFETY: the caller's contract. + unsafe { + device.new_heap_with_bytes_no_copy_now(memory, descriptor, deallocator, keep_alive) + } + }) } /// [`Device::new_heap_with_bytes_no_copy`] on the generation that serves (`device/line.rs`). @@ -647,17 +646,19 @@ impl Device { keep_alive: Option, token: RangeToken, ) -> Result { - // SAFETY: the caller's contract. - unsafe { - self.current() - .new_heap_with_bytes_no_copy_and_range_token_now( + let taken = (memory, deallocator, keep_alive, token); + self.at_serving_taking(taken, |device, (memory, deallocator, keep_alive, token)| { + // SAFETY: the caller's contract. + unsafe { + device.new_heap_with_bytes_no_copy_and_range_token_now( memory, descriptor, deallocator, keep_alive, token, ) - } + } + }) } /// [`Device::new_heap_with_bytes_no_copy_and_range_token`] on the generation that serves diff --git a/crates/metal2vulkan-runtime/src/host_ram.rs b/crates/metal2vulkan-runtime/src/host_ram.rs index d92bc58be..9ac27b3b7 100644 --- a/crates/metal2vulkan-runtime/src/host_ram.rs +++ b/crates/metal2vulkan-runtime/src/host_ram.rs @@ -1561,7 +1561,7 @@ impl Device { /// whose ranges meet those of a RAM the device uses already, itself among them (`Invalid`, /// `Problem::HostRam`); a lost device. pub fn use_host_ram(&self, ram: &Arc) -> Result { - self.current().use_host_ram_now(ram) + self.at_serving(|device| device.use_host_ram_now(ram)) } /// [`Device::use_host_ram`] on the generation that serves (`device/line.rs`). diff --git a/crates/metal2vulkan-runtime/src/host_shared_pointers.rs b/crates/metal2vulkan-runtime/src/host_shared_pointers.rs index e90e203b3..06390fb85 100644 --- a/crates/metal2vulkan-runtime/src/host_shared_pointers.rs +++ b/crates/metal2vulkan-runtime/src/host_shared_pointers.rs @@ -36,7 +36,7 @@ impl Device { /// Fails only where the answer cannot be found: out of host memory for the page, or the /// device lost. The device's refusal of the pointer is no failure but the answer, false. pub fn imports_shared_host_pointers(&self) -> Result { - self.current().imports_shared_host_pointers_now() + self.at_serving(|device| device.imports_shared_host_pointers_now()) } /// [`Device::imports_shared_host_pointers`] on the generation that serves (`device/line.rs`). diff --git a/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs b/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs index 69fcd7cd7..8ac56b5f7 100644 --- a/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs +++ b/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs @@ -536,8 +536,9 @@ impl Device { max_count: u64, options: ResourceOptions, ) -> Result { - self.current() - .new_indirect_command_buffer_now(descriptor, max_count, options) + self.at_serving(|device| { + device.new_indirect_command_buffer_now(descriptor, max_count, options) + }) } /// [`Device::new_indirect_command_buffer`] on the generation that serves (`device/line.rs`). diff --git a/crates/metal2vulkan-runtime/src/library.rs b/crates/metal2vulkan-runtime/src/library.rs index 4fbe841ba..8f4a05025 100644 --- a/crates/metal2vulkan-runtime/src/library.rs +++ b/crates/metal2vulkan-runtime/src/library.rs @@ -147,7 +147,7 @@ impl Device { /// bytes that are no metallib, on an Apple M4. AIR goes in through the translator /// ([`Device::new_library_with_compile_library`]). pub fn new_library_with_data(&self, data: &[u8]) -> Result { - self.current().new_library_with_data_now(data) + self.at_serving(|device| device.new_library_with_data_now(data)) } /// [`Device::new_library_with_data`] on the generation that serves (`device/line.rs`). @@ -164,7 +164,7 @@ impl Device { /// ahead of time with Apple's `metal` tool and load the metallib with /// [`new_library_with_data`](Self::new_library_with_data). pub fn new_library_with_source(&self, _source: &str) -> Result { - self.current().new_library_with_source_now(_source) + self.at_serving(|device| device.new_library_with_source_now(_source)) } /// [`Device::new_library_with_source`] on the generation that serves (`device/line.rs`). diff --git a/crates/metal2vulkan-runtime/src/pipeline/mod.rs b/crates/metal2vulkan-runtime/src/pipeline/mod.rs index 4b5a50591..c3b91e512 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/mod.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/mod.rs @@ -559,8 +559,9 @@ impl Device { function: &Function, options: &PipelineOptions, ) -> Result { - self.current() - .new_compute_pipeline_state_with_function_now(function, options) + self.at_serving(|device| { + device.new_compute_pipeline_state_with_function_now(function, options) + }) } /// [`Device::new_compute_pipeline_state_with_function`] on the generation that serves @@ -622,8 +623,7 @@ impl Device { descriptor: &ComputePipelineDescriptor, options: &PipelineOptions, ) -> Result { - self.current() - .new_compute_pipeline_state_now(descriptor, options) + self.at_serving(|device| device.new_compute_pipeline_state_now(descriptor, options)) } /// [`Device::new_compute_pipeline_state`] on the generation that serves (`device/line.rs`). @@ -646,8 +646,9 @@ impl Device { descriptor: &ComputePipelineDescriptor, options: &PipelineOptions, ) -> Result<(ComputePipelineState, Option), Error> { - self.current() - .new_compute_pipeline_state_with_reflection_now(descriptor, options) + self.at_serving(|device| { + device.new_compute_pipeline_state_with_reflection_now(descriptor, options) + }) } /// [`Device::new_compute_pipeline_state_with_reflection`] on the generation that serves @@ -680,8 +681,9 @@ impl Device { function: &Function, options: &PipelineOptions, ) -> Result<(ComputePipelineState, Option), Error> { - self.current() - .new_compute_pipeline_state_with_function_and_reflection_now(function, options) + self.at_serving(|device| { + device.new_compute_pipeline_state_with_function_and_reflection_now(function, options) + }) } /// [`Device::new_compute_pipeline_state_with_function_and_reflection`] on the generation diff --git a/crates/metal2vulkan-runtime/src/pipeline/render.rs b/crates/metal2vulkan-runtime/src/pipeline/render.rs index 7b21b4846..5ef16082e 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/render.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/render.rs @@ -594,8 +594,7 @@ impl Device { descriptor: &RenderPipelineDescriptor, options: &PipelineOptions, ) -> Result { - self.current() - .new_render_pipeline_state_now(descriptor, options) + self.at_serving(|device| device.new_render_pipeline_state_now(descriptor, options)) } /// [`Device::new_render_pipeline_state`] on the generation that serves (`device/line.rs`). @@ -619,8 +618,9 @@ impl Device { descriptor: &RenderPipelineDescriptor, options: &PipelineOptions, ) -> Result<(RenderPipelineState, Option), Error> { - self.current() - .new_render_pipeline_state_with_reflection_now(descriptor, options) + self.at_serving(|device| { + device.new_render_pipeline_state_with_reflection_now(descriptor, options) + }) } /// [`Device::new_render_pipeline_state_with_reflection`] on the generation that serves @@ -694,8 +694,7 @@ impl Device { descriptor: &MeshPipelineDescriptor, options: &PipelineOptions, ) -> Result { - self.current() - .new_mesh_render_pipeline_state_now(descriptor, options) + self.at_serving(|device| device.new_mesh_render_pipeline_state_now(descriptor, options)) } /// [`Device::new_mesh_render_pipeline_state`] on the generation that serves (`device/line.rs`). @@ -723,8 +722,9 @@ impl Device { descriptor: &MeshPipelineDescriptor, options: &PipelineOptions, ) -> Result<(RenderPipelineState, Option), Error> { - self.current() - .new_mesh_render_pipeline_state_with_reflection_now(descriptor, options) + self.at_serving(|device| { + device.new_mesh_render_pipeline_state_with_reflection_now(descriptor, options) + }) } /// [`Device::new_mesh_render_pipeline_state_with_reflection`] on the generation that serves diff --git a/crates/metal2vulkan-runtime/src/pipeline/tile.rs b/crates/metal2vulkan-runtime/src/pipeline/tile.rs index 515d2d56c..377dd6079 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/tile.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/tile.rs @@ -67,8 +67,7 @@ impl Device { descriptor: &TileRenderPipelineDescriptor, options: &PipelineOptions, ) -> Result { - self.current() - .new_tile_render_pipeline_state_now(descriptor, options) + self.at_serving(|device| device.new_tile_render_pipeline_state_now(descriptor, options)) } /// [`Device::new_tile_render_pipeline_state`] on the generation that serves (`device/line.rs`). @@ -91,8 +90,9 @@ impl Device { descriptor: &TileRenderPipelineDescriptor, options: &PipelineOptions, ) -> Result<(RenderPipelineState, Option), Error> { - self.current() - .new_tile_render_pipeline_state_with_reflection_now(descriptor, options) + self.at_serving(|device| { + device.new_tile_render_pipeline_state_with_reflection_now(descriptor, options) + }) } /// [`Device::new_tile_render_pipeline_state_with_reflection`] on the generation that serves diff --git a/crates/metal2vulkan-runtime/src/queue.rs b/crates/metal2vulkan-runtime/src/queue.rs index 9c11d07b9..0cb2a04ab 100644 --- a/crates/metal2vulkan-runtime/src/queue.rs +++ b/crates/metal2vulkan-runtime/src/queue.rs @@ -306,7 +306,7 @@ impl Drop for Slot { impl Device { /// `newCommandQueue`: a queue of at most 64 uncompleted command buffers, as Metal's. pub fn new_command_queue(&self) -> Result { - self.current().new_command_queue_now() + self.at_serving(|device| device.new_command_queue_now()) } /// [`Device::new_command_queue`] on the generation that serves (`device/line.rs`). @@ -321,8 +321,7 @@ impl Device { &self, count: usize, ) -> Result { - self.current() - .new_command_queue_with_max_command_buffer_count_now(count) + self.at_serving(|device| device.new_command_queue_with_max_command_buffer_count_now(count)) } /// [`Device::new_command_queue_with_max_command_buffer_count`] on the generation that serves @@ -374,7 +373,7 @@ impl Device { /// Refused as that call is: a count of 0 (`Invalid`, `MaxCommandBufferCount`), and a lost /// device (`DeviceLost`). pub fn new_command_queue_unshared(&self, count: usize) -> Result { - self.current().new_command_queue_unshared_now(count) + self.at_serving(|device| device.new_command_queue_unshared_now(count)) } /// [`Device::new_command_queue_unshared`] on the generation that serves (`device/line.rs`). @@ -412,7 +411,7 @@ impl Device { /// `Device::from_raw`) gives the queue its `VkQueue` round robin, as `new_command_queue` /// does, and announces `Degradation::SharedPresentQueue` (`shared-present-queue`) once. pub fn new_command_queue_for_presenting(&self) -> Result { - self.current().new_command_queue_for_presenting_now() + self.at_serving(|device| device.new_command_queue_for_presenting_now()) } /// [`Device::new_command_queue_for_presenting`] on the generation that serves diff --git a/crates/metal2vulkan-runtime/src/rate_map.rs b/crates/metal2vulkan-runtime/src/rate_map.rs index 3a8feb4f9..fed270db1 100644 --- a/crates/metal2vulkan-runtime/src/rate_map.rs +++ b/crates/metal2vulkan-runtime/src/rate_map.rs @@ -368,7 +368,7 @@ impl Device { &self, descriptor: &RateMapDescriptor, ) -> Result { - self.current().new_rasterization_rate_map_now(descriptor) + self.at_serving(|device| device.new_rasterization_rate_map_now(descriptor)) } /// [`Device::new_rasterization_rate_map`] on the generation that serves (`device/line.rs`). diff --git a/crates/metal2vulkan-runtime/src/resource_ids.rs b/crates/metal2vulkan-runtime/src/resource_ids.rs index 0fcecfe4c..ccdc40f74 100644 --- a/crates/metal2vulkan-runtime/src/resource_ids.rs +++ b/crates/metal2vulkan-runtime/src/resource_ids.rs @@ -1356,7 +1356,7 @@ impl Device { kind: ResourceIdKind, ids: &[ResourceId], ) -> Result<(), Error> { - self.current().reserve_resource_ids_now(kind, ids) + self.at_serving(|device| device.reserve_resource_ids_now(kind, ids)) } /// [`Device::reserve_resource_ids`] on the generation that serves (`device/line.rs`). diff --git a/crates/metal2vulkan-runtime/src/sampler.rs b/crates/metal2vulkan-runtime/src/sampler.rs index 4fb0f00f2..9c1238d1a 100644 --- a/crates/metal2vulkan-runtime/src/sampler.rs +++ b/crates/metal2vulkan-runtime/src/sampler.rs @@ -600,7 +600,7 @@ impl Device { /// pixel-coordinate one's included). A minimum or maximum reduction reads the weighted /// average. pub fn new_sampler_state(&self, descriptor: &SamplerDescriptor) -> Result { - self.current().new_sampler_state_now(descriptor) + self.at_serving(|device| device.new_sampler_state_now(descriptor)) } /// [`Device::new_sampler_state`] on the generation that serves (`device/line.rs`). @@ -629,8 +629,7 @@ impl Device { descriptor: &SamplerDescriptor, options: SamplerStateOptions, ) -> Result { - self.current() - .new_sampler_state_with_options_now(descriptor, options) + self.at_serving(|device| device.new_sampler_state_with_options_now(descriptor, options)) } /// [`Device::new_sampler_state_with_options`] on the generation that serves (`device/line.rs`). diff --git a/crates/metal2vulkan-runtime/src/sparse/mod.rs b/crates/metal2vulkan-runtime/src/sparse/mod.rs index 7cada2d63..981fd3985 100644 --- a/crates/metal2vulkan-runtime/src/sparse/mod.rs +++ b/crates/metal2vulkan-runtime/src/sparse/mod.rs @@ -445,12 +445,14 @@ impl Device { tile_size: Size, mode: SparseTextureRegionAlignmentMode, ) -> Result<(), Error> { - self.current().convert_sparse_pixel_regions_now( - pixel_regions, - tile_regions, - tile_size, - mode, - ) + self.at_serving(|device| { + device.convert_sparse_pixel_regions_now( + pixel_regions, + &mut *tile_regions, + tile_size, + mode, + ) + }) } /// [`Device::convert_sparse_pixel_regions`] on the generation that serves (`device/line.rs`). diff --git a/crates/metal2vulkan-runtime/src/tests/device_recovery.rs b/crates/metal2vulkan-runtime/src/tests/device_recovery.rs index 75093f7ca..2e81185b1 100644 --- a/crates/metal2vulkan-runtime/src/tests/device_recovery.rs +++ b/crates/metal2vulkan-runtime/src/tests/device_recovery.rs @@ -366,3 +366,50 @@ fn a_later_recovery_announces_its_failure() { )); assert_eq!(device.work_counts().device_recoveries, 1); } + +/// A device call whose generation is lost under it, refused for that, runs again on the generation +/// serving next (`Device::at_serving`): the loss here comes inside the call's first run, as a +/// recovery on another thread may, and the call answers with a buffer of the new generation. +#[test] +fn a_call_refused_for_a_loss_under_it_runs_again_on_the_next_generation() { + let scratch = Scratch::new(); + let device = device(&scratch.0); + let mut runs = 0; + let buffer = device + .at_serving(|serving| { + runs += 1; + if runs == 1 { + serving.recover_for_test().unwrap(); + } + serving.new_buffer_now(4096, ResourceOptions::shared()) + }) + .unwrap(); + assert_eq!(runs, 2, "the call ran once more, on the next generation"); + assert!(buffer.device().generation_serves()); + assert_eq!(device.work_counts().device_recoveries, 1); +} + +/// A device call that took its arguments (`Device::at_serving_taking`) before its generation's +/// loss is not refused for it: it completes on that generation, as one the loss followed, and +/// its buffer is one made before the recovery. +#[test] +fn a_call_that_took_its_arguments_completes_on_a_generation_lost_after() { + let scratch = Scratch::new(); + let device = device(&scratch.0); + let buffer = device + .at_serving_taking(4096, |serving, length| { + serving.recover_for_test().unwrap(); + serving.new_buffer_now(length, ResourceOptions::shared()) + }) + .unwrap(); + assert!(!buffer.device().generation_serves()); + assert!(matches!( + buffer + .device() + .new_buffer_now(4096, ResourceOptions::shared()), + Err(Error::NotImplemented(crate::RuntimeGap::MadeBeforeRecovery)) + )); + // The device itself goes on, on the new generation. + let next = device.new_buffer(4096, ResourceOptions::shared()).unwrap(); + assert!(next.device().generation_serves()); +} diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs index 7db8ccf83..11dbde1e5 100644 --- a/crates/metal2vulkan-runtime/src/texture.rs +++ b/crates/metal2vulkan-runtime/src/texture.rs @@ -1695,7 +1695,7 @@ impl Device { /// holds no slot, and counts in no plain allocation or reservation, otherwise made as here (a /// runtime extension, for a caller's helper texture). pub fn new_texture(&self, descriptor: &TextureDescriptor) -> Result { - self.current().new_texture_now(descriptor) + self.at_serving(|device| device.new_texture_now(descriptor)) } /// [`Device::new_texture`] on the generation that serves (`device/line.rs`). @@ -1728,17 +1728,7 @@ impl Device { descriptor: &TextureDescriptor, token: RangeToken, ) -> Result { - self.current() - .new_texture_with_range_token_now(descriptor, token) - } - - /// [`Device::new_texture_with_range_token`] on the generation that serves (`device/line.rs`). - pub(crate) fn new_texture_with_range_token_now( - &self, - descriptor: &TextureDescriptor, - token: RangeToken, - ) -> Result { - let texture = self.new_texture_now(descriptor)?; + let texture = self.at_serving(|device| device.new_texture_now(descriptor))?; texture.keep_range_token(token)?; Ok(texture) } diff --git a/crates/metal2vulkan-runtime/src/texture/heap.rs b/crates/metal2vulkan-runtime/src/texture/heap.rs index 4c76de04b..cc07e4e8d 100644 --- a/crates/metal2vulkan-runtime/src/texture/heap.rs +++ b/crates/metal2vulkan-runtime/src/texture/heap.rs @@ -1647,7 +1647,7 @@ impl Device { &self, descriptor: &TextureDescriptor, ) -> Result { - self.current().heap_texture_size_and_align_now(descriptor) + self.at_serving(|device| device.heap_texture_size_and_align_now(descriptor)) } /// [`Device::heap_texture_size_and_align`] on the generation that serves (`device/line.rs`). @@ -1726,8 +1726,7 @@ impl Device { &self, descriptor: &TextureDescriptor, ) -> Result, Error> { - self.current() - .metal_heap_texture_size_and_align_now(descriptor) + self.at_serving(|device| device.metal_heap_texture_size_and_align_now(descriptor)) } /// [`Device::metal_heap_texture_size_and_align`] on the generation that serves diff --git a/crates/metal2vulkan-runtime/src/texture/shared.rs b/crates/metal2vulkan-runtime/src/texture/shared.rs index ebd1b4455..6d2dacc1d 100644 --- a/crates/metal2vulkan-runtime/src/texture/shared.rs +++ b/crates/metal2vulkan-runtime/src/texture/shared.rs @@ -68,7 +68,7 @@ impl Device { /// multisample sample count's rule; a swizzle with writes under `write_swizzle_enabled` too /// (`Invalid`, `SwizzleWithWrites`), which Metal's validation stops here alone. pub fn new_shared_texture(&self, descriptor: &TextureDescriptor) -> Result { - self.current().new_shared_texture_now(descriptor) + self.at_serving(|device| device.new_shared_texture_now(descriptor)) } /// [`Device::new_shared_texture`] on the generation that serves (`device/line.rs`). @@ -148,18 +148,7 @@ impl Device { descriptor: &TextureDescriptor, token: RangeToken, ) -> Result { - self.current() - .new_shared_texture_with_range_token_now(descriptor, token) - } - - /// [`Device::new_shared_texture_with_range_token`] on the generation that serves - /// (`device/line.rs`). - pub(crate) fn new_shared_texture_with_range_token_now( - &self, - descriptor: &TextureDescriptor, - token: RangeToken, - ) -> Result { - let texture = self.new_shared_texture_now(descriptor)?; + let texture = self.at_serving(|device| device.new_shared_texture_now(descriptor))?; texture.keep_range_token(token)?; Ok(texture) } @@ -178,8 +167,7 @@ impl Device { handle: &SharedTextureHandle, placement: IdPlacement, ) -> Result { - self.current() - .new_shared_texture_with_handle_now(handle, placement) + self.at_serving(|device| device.new_shared_texture_with_handle_now(handle, placement)) } /// [`Device::new_shared_texture_with_handle`] on the generation that serves (`device/line.rs`). diff --git a/docs/API.md b/docs/API.md index 85f561224..4a2b0fcee 100644 --- a/docs/API.md +++ b/docs/API.md @@ -3120,7 +3120,7 @@ bound. command buffers in flight took past 10 seconds to fail, and where its new `VkDevice` could not be made, after which the device answers as a lost one. `Device::recover_for_test` runs the same recovery with no GPU fault. Cost: the objects made before the recovery are refused (`MadeBeforeRecovery`), where Metal's go on; the calls during it - wait for it. + wait for it, and a device call the loss met while it ran runs again after it. - `metal2vulkan-degrade-private-properties-unknown` (precision). `PrivatePropertiesUnknown`: Apple's private device properties the device has no answer for: no document describes them and nothing the device reports maps to them. Cost: the device gives no answer for them (`PrivateAnswer::value` is