Skip to content

Commit

Permalink
Add a way to notify with Queue::submit() to Vulkan's vk::Semaphore al…
Browse files Browse the repository at this point in the history
…located outside of wgpu
  • Loading branch information
Sotaro Ikeda committed Dec 23, 2024
1 parent ee3ae0e commit 97aa154
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
21 changes: 20 additions & 1 deletion wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use crate::{
},
global::Global,
hal_api::HalApi,
id::{AdapterId, BufferId, CommandEncoderId, DeviceId, SurfaceId, TextureId, TextureViewId},
id::{
AdapterId, BufferId, CommandEncoderId, DeviceId, QueueId, SurfaceId, TextureId,
TextureViewId,
},
init_tracker::{BufferInitTracker, TextureInitTracker},
lock::{rank, Mutex, RwLock},
resource_log,
Expand Down Expand Up @@ -1391,6 +1394,22 @@ impl Global {
hal_command_encoder_callback(None)
}
}

/// # Safety
///
/// - The raw queue handle must not be manually destroyed
pub unsafe fn queue_as_hal<A: HalApi, F: FnOnce(Option<&A::Queue>) -> R, R>(
&self,
id: QueueId,
hal_queue_callback: F,
) -> R {
profiling::scope!("Queue::as_hal");

let queue = self.hub.queues.get(id);
let hal_queue = queue.raw().as_any().downcast_ref();

hal_queue_callback(hal_queue)
}
}

/// A texture that has been marked as destroyed and is staged for actual deletion soon.
Expand Down
1 change: 1 addition & 0 deletions wgpu-hal/src/vulkan/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,7 @@ impl super::Adapter {
device: Arc::clone(&shared),
family_index,
relay_semaphores: Mutex::new(relay_semaphores),
signal_semaphores: Mutex::new(Vec::new()),
};

let mem_allocator = {
Expand Down
17 changes: 17 additions & 0 deletions wgpu-hal/src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@ pub struct Queue {
device: Arc<DeviceShared>,
family_index: u32,
relay_semaphores: Mutex<RelaySemaphores>,
signal_semaphores: Mutex<Vec<vk::Semaphore>>,
}

impl Drop for Queue {
Expand Down Expand Up @@ -1210,6 +1211,11 @@ impl crate::Queue for Queue {
signal_values.push(!0);
}

let mut pending_signal_semaphores = self.signal_semaphores.lock();
if !pending_signal_semaphores.is_empty() {
signal_semaphores.append(&mut pending_signal_semaphores);
}

// In order for submissions to be strictly ordered, we encode a dependency between each submission
// using a pair of semaphores. This adds a wait if it is needed, and signals the next semaphore.
let semaphore_state = self.relay_semaphores.lock().advance(&self.device)?;
Expand Down Expand Up @@ -1338,6 +1344,17 @@ impl crate::Queue for Queue {
}
}

impl Queue {
pub fn raw_device(&self) -> &ash::Device {
&self.device.raw
}

pub fn add_signal_semahore(&self, semaphore: vk::Semaphore) {
let mut signal_semaphores = self.signal_semaphores.lock();
signal_semaphores.push(semaphore);
}
}

/// Maps
///
/// - VK_ERROR_OUT_OF_HOST_MEMORY
Expand Down

0 comments on commit 97aa154

Please sign in to comment.