Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check to ensure vulkan::CommandEncoder::discard_encoding is not called multiple times in a row #5557

Merged
merged 7 commits into from
Apr 22, 2024
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ Bottom level categories:
- Implement the `device_set_device_lost_callback` method for `ContextWebGpu`. By @suti in [#5438](https://github.com/gfx-rs/wgpu/pull/5438)
- Add support for storage texture access modes `ReadOnly` and `ReadWrite`. By @JolifantoBambla in [#5434](https://github.com/gfx-rs/wgpu/pull/5434)

#### Vulkan

- Make `CommandEncoder::discard_encoding` do nothing if called multiple times, and implement `Drop` for `CommandEncoder` to call `discard_encoding`. By @villuna

### Bug Fixes

#### General
Expand Down
8 changes: 8 additions & 0 deletions wgpu-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,10 @@ pub trait Queue: WasmNotSendSync {
/// live `CommandBuffers` built from it. All the `CommandBuffer`s
/// are destroyed, and their resources are freed.
///
/// You may want to implement the [core::ops::Drop] trait to discard
/// the current commands before the encoder is dropped (e.g. using
/// [CommandEncoder::discard_encoding]).
///
/// # Safety
///
/// - The `CommandEncoder` must be in the states described above to
Expand Down Expand Up @@ -652,6 +656,10 @@ pub trait CommandEncoder: WasmNotSendSync + fmt::Debug {
///
/// This puts this `CommandEncoder` in the "closed" state.
///
/// Implementations of this function must be idempotent, i.e.
/// if the function has just been called, calling it again should
/// not do anything.
///
/// # Safety
///
/// This `CommandEncoder` must be in the "recording" state.
Expand Down
13 changes: 11 additions & 2 deletions wgpu-hal/src/vulkan/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ impl super::DeviceShared {
}
}

impl Drop for super::CommandEncoder {
fn drop(&mut self) {
use crate::CommandEncoder;
unsafe { self.discard_encoding() }
}
}

impl super::CommandEncoder {
fn write_pass_end_timestamp_if_requested(&mut self) {
if let Some((query_set, index)) = self.end_of_pass_timer_query.take() {
Expand Down Expand Up @@ -104,8 +111,10 @@ impl crate::CommandEncoder for super::CommandEncoder {
}

unsafe fn discard_encoding(&mut self) {
self.discarded.push(self.active);
self.active = vk::CommandBuffer::null();
if self.active != vk::CommandBuffer::null() {
self.discarded.push(self.active);
self.active = vk::CommandBuffer::null();
}
}

unsafe fn reset_all<I>(&mut self, cmd_bufs: I)
Expand Down
Loading