Skip to content

Commit

Permalink
Add check to ensure vulkan::CommandEncoder::discard_encoding is not…
Browse files Browse the repository at this point in the history
… called multiple times in a row (#5557)

Document that `wgpu_hal::CommandEncoder::discard_encoding` must not be called multiple times.

Assert in `wgpu_hal::vulkan::CommandEncoder::discard_encoding` that encoding is actually in progress.

Fixes #5255.
  • Loading branch information
villuna authored Apr 22, 2024
1 parent 0d9e11d commit 7840e75
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ Bottom level categories:
#### Vulkan

- Set object labels when the DEBUG flag is set, even if the VALIDATION flag is disabled. By @DJMcNab in [#5345](https://github.com/gfx-rs/wgpu/pull/5345).
- Add safety check to `wgpu_hal::vulkan::CommandEncoder` to make sure `discard_encoding` is not called in the closed state. By @villuna in [#5557](https://github.com/gfx-rs/wgpu/pull/5557)

#### Metal

Expand Down
6 changes: 5 additions & 1 deletion wgpu-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ pub trait CommandEncoder: WasmNotSendSync + fmt::Debug {
/// This `CommandEncoder` must be in the "closed" state.
unsafe fn begin_encoding(&mut self, label: Label) -> Result<(), DeviceError>;

/// Discard the command list under construction, if any.
/// Discard the command list under construction.
///
/// If an error has occurred while recording commands, this
/// is the only safe thing to do with the encoder.
Expand All @@ -655,6 +655,10 @@ pub trait CommandEncoder: WasmNotSendSync + fmt::Debug {
/// # Safety
///
/// This `CommandEncoder` must be in the "recording" state.
///
/// Callers must not assume that implementations of this
/// function are idempotent, and thus should not call it
/// multiple times in a row.
unsafe fn discard_encoding(&mut self);

/// Return a fresh [`CommandBuffer`] holding the recorded commands.
Expand Down
5 changes: 5 additions & 0 deletions wgpu-hal/src/vulkan/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ impl crate::CommandEncoder for super::CommandEncoder {
}

unsafe fn discard_encoding(&mut self) {
// Safe use requires this is not called in the "closed" state, so the buffer
// shouldn't be null. Assert this to make sure we're not pushing null
// buffers to the discard pile.
assert_ne!(self.active, vk::CommandBuffer::null());

self.discarded.push(self.active);
self.active = vk::CommandBuffer::null();
}
Expand Down

0 comments on commit 7840e75

Please sign in to comment.