diff --git a/CHANGELOG.md b/CHANGELOG.md index f96e2cf07..79bc8e249 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -114,6 +114,7 @@ By @andyleiserson in [#9321](https://github.com/gfx-rs/wgpu/pull/9321). #### Vulkan +- Add `vulkan::Queue::add_wait_semaphore` and `vulkan::Queue::remove_wait_semaphore`. Lets external producers (CUDA / OpenCL / D3D12 imported via `VK_KHR_external_semaphore_*`) be waited on at the next `Queue::submit` call without a CPU block. By @AdrianEddy in [#9461](https://github.com/gfx-rs/wgpu/pull/9461). - Add `vulkan::Device::texture_from_dmabuf_fd()` for importing DMA-buf textures on Linux, with `VULKAN_EXTERNAL_MEMORY_FD` and `VULKAN_EXTERNAL_MEMORY_DMA_BUF` feature flags. By @TODO in [#9412](https://github.com/gfx-rs/wgpu/pull/9412). - Add support for RawWindowHandle::Drm on unix, conditional on the `"drm"` feature. - DRM support by @rectalogic in [#9182](https://github.com/gfx-rs/wgpu/pull/9182). diff --git a/wgpu-hal/src/vulkan/adapter.rs b/wgpu-hal/src/vulkan/adapter.rs index 3b0364fa5..211e53e37 100644 --- a/wgpu-hal/src/vulkan/adapter.rs +++ b/wgpu-hal/src/vulkan/adapter.rs @@ -2817,6 +2817,7 @@ impl super::Adapter { family_index, relay_semaphores: Mutex::new(relay_semaphores), signal_semaphores: Mutex::new(SemaphoreList::new(SemaphoreListMode::Signal)), + wait_semaphores: Mutex::new(SemaphoreList::new(SemaphoreListMode::Wait)), }; let allocation_sizes = AllocationSizes::from_memory_hints(memory_hints).into(); diff --git a/wgpu-hal/src/vulkan/mod.rs b/wgpu-hal/src/vulkan/mod.rs index 5c26dedbc..e147cd190 100644 --- a/wgpu-hal/src/vulkan/mod.rs +++ b/wgpu-hal/src/vulkan/mod.rs @@ -618,6 +618,7 @@ pub struct Queue { family_index: u32, relay_semaphores: Mutex, signal_semaphores: Mutex, + wait_semaphores: Mutex, } impl Queue { @@ -1295,6 +1296,11 @@ impl crate::Queue for Queue { signal_semaphores.append(&mut guard); } + let mut wait_guard = self.wait_semaphores.lock(); + if !wait_guard.is_empty() { + wait_semaphores.append(&mut wait_guard); + } + // 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)?; @@ -1397,6 +1403,39 @@ impl Queue { pub fn remove_signal_semaphore(&self, semaphore: vk::Semaphore) -> bool { self.signal_semaphores.lock().remove(semaphore) } + + /// Stage a semaphore wait on the next [`crate::Queue::submit`] call. + /// + /// `semaphore_value` selects the kind of payload the wait targets: + /// + /// - `Some(value)` - wait until `semaphore` (a timeline semaphore) has been signalled to at least `value`. + /// - `None` - wait on a binary semaphore signal. + /// + /// `stage` is the pipeline stage at which the wait blocks downstream + /// work (e.g. `vk::PipelineStageFlags::TOP_OF_PIPE` to gate the + /// entire submission, or a more specific stage when only that stage + /// reads the synchronised resource). + pub fn add_wait_semaphore( + &self, + semaphore: vk::Semaphore, + semaphore_value: Option, + stage: vk::PipelineStageFlags, + ) { + let mut guard = self.wait_semaphores.lock(); + if let Some(value) = semaphore_value { + guard.push_wait(SemaphoreType::Timeline(semaphore, value), stage); + } else { + guard.push_wait(SemaphoreType::Binary(semaphore), stage); + } + } + + /// Remove `semaphore` from the pending wait list if it is still present. + /// + /// Returns `true` if the semaphore was found and removed. If the submit + /// already consumed it, this is a no-op that returns `false`. + pub fn remove_wait_semaphore(&self, semaphore: vk::Semaphore) -> bool { + self.wait_semaphores.lock().remove(semaphore) + } } /// Maps