From 2bc328c46f3f9e1205769b5e180863ac51d80ec4 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Tue, 16 Jul 2024 18:06:22 -0700 Subject: [PATCH] [hal, core] Introduce `wgpu_hal::AtomicFenceValue`, and use it. Introduce the new type alias `wgpu_hal::AtomicFenceValue`, which is the atomic version of `wgpu_hal::FenceValue`. Use this type alias in `wgpu_core`. Remove `as` conversions made unnecessary since we're not conflating `usize` with `u64` any more. --- wgpu-core/src/device/resource.rs | 3 +-- wgpu-core/src/resource.rs | 14 +++++--------- wgpu-hal/src/lib.rs | 1 + 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index c364711f5d..7030f3c6fe 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -88,8 +88,7 @@ pub struct Device { label: String, pub(crate) command_allocator: command::CommandAllocator, - //Note: The submission index here corresponds to the last submission that is done. - pub(crate) active_submission_index: AtomicU64, //SubmissionIndex, + pub(crate) active_submission_index: hal::AtomicFenceValue, // NOTE: if both are needed, the `snatchable_lock` must be consistently acquired before the // `fence` lock to avoid deadlocks. pub(crate) fence: RwLock>, diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index ced9edbb56..1929b9f8f7 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -28,10 +28,7 @@ use std::{ mem::{self, ManuallyDrop}, ops::Range, ptr::NonNull, - sync::{ - atomic::{AtomicUsize, Ordering}, - Arc, Weak, - }, + sync::{atomic::Ordering, Arc, Weak}, }; /// Information about the wgpu-core resource. @@ -64,7 +61,7 @@ pub(crate) struct TrackingData { /// sequentially. Thus, when a queue submission completes, we know any /// resources used in that submission and any lower-numbered submissions are /// no longer in use by the GPU. - submission_index: AtomicUsize, + submission_index: hal::AtomicFenceValue, } impl Drop for TrackingData { @@ -78,7 +75,7 @@ impl TrackingData { Self { tracker_index: tracker_indices.alloc(), tracker_indices, - submission_index: AtomicUsize::new(0), + submission_index: hal::AtomicFenceValue::new(0), } } @@ -89,12 +86,11 @@ impl TrackingData { /// Record that this resource will be used by the queue submission with the /// given index. pub(crate) fn use_at(&self, submit_index: SubmissionIndex) { - self.submission_index - .store(submit_index as _, Ordering::Release); + self.submission_index.store(submit_index, Ordering::Release); } pub(crate) fn submission_index(&self) -> SubmissionIndex { - self.submission_index.load(Ordering::Acquire) as _ + self.submission_index.load(Ordering::Acquire) } } diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index 2dd09934df..b28a005a7a 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -294,6 +294,7 @@ pub const QUERY_SIZE: wgt::BufferAddress = 8; pub type Label<'a> = Option<&'a str>; pub type MemoryRange = Range; pub type FenceValue = u64; +pub type AtomicFenceValue = std::sync::atomic::AtomicU64; /// Drop guard to signal wgpu-hal is no longer using an externally created object. pub type DropGuard = Box;