Skip to content

Commit

Permalink
MissingBufferUsageError now has an id prop
Browse files Browse the repository at this point in the history
  • Loading branch information
andristarr committed Feb 13, 2024
1 parent 84ba4e5 commit 24bbf1d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 20 deletions.
8 changes: 4 additions & 4 deletions wgpu-core/src/command/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ impl RenderBundleEncoder {
.map_pass_err(scope)?;
self.check_valid_to_use(buffer.device.info.id())
.map_pass_err(scope)?;
check_buffer_usage(buffer.usage, wgt::BufferUsages::INDEX)
check_buffer_usage(buffer_id, buffer.usage, wgt::BufferUsages::INDEX)
.map_pass_err(scope)?;

let end = match size {
Expand Down Expand Up @@ -535,7 +535,7 @@ impl RenderBundleEncoder {
.map_pass_err(scope)?;
self.check_valid_to_use(buffer.device.info.id())
.map_pass_err(scope)?;
check_buffer_usage(buffer.usage, wgt::BufferUsages::VERTEX)
check_buffer_usage(buffer_id, buffer.usage, wgt::BufferUsages::VERTEX)
.map_pass_err(scope)?;

let end = match size {
Expand Down Expand Up @@ -657,7 +657,7 @@ impl RenderBundleEncoder {
.map_pass_err(scope)?;
self.check_valid_to_use(buffer.device.info.id())
.map_pass_err(scope)?;
check_buffer_usage(buffer.usage, wgt::BufferUsages::INDIRECT)
check_buffer_usage(buffer_id, buffer.usage, wgt::BufferUsages::INDIRECT)
.map_pass_err(scope)?;

buffer_memory_init_actions.extend(buffer.initialization_status.read().create_action(
Expand Down Expand Up @@ -696,7 +696,7 @@ impl RenderBundleEncoder {
.map_pass_err(scope)?;
self.check_valid_to_use(buffer.device.info.id())
.map_pass_err(scope)?;
check_buffer_usage(buffer.usage, wgt::BufferUsages::INDIRECT)
check_buffer_usage(buffer_id, buffer.usage, wgt::BufferUsages::INDIRECT)
.map_pass_err(scope)?;

buffer_memory_init_actions.extend(buffer.initialization_status.read().create_action(
Expand Down
8 changes: 6 additions & 2 deletions wgpu-core/src/command/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,12 @@ impl Global {
.buffers
.merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDIRECT)
.map_pass_err(scope)?;
check_buffer_usage(indirect_buffer.usage, wgt::BufferUsages::INDIRECT)
.map_pass_err(scope)?;
check_buffer_usage(
buffer_id,
indirect_buffer.usage,
wgt::BufferUsages::INDIRECT,
)
.map_pass_err(scope)?;

let end_offset = offset + mem::size_of::<wgt::DispatchIndirectArgs>() as u64;
if end_offset > indirect_buffer.size {
Expand Down
22 changes: 15 additions & 7 deletions wgpu-core/src/command/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1675,7 +1675,7 @@ impl Global {
return Err(DeviceError::WrongDevice).map_pass_err(scope);
}

check_buffer_usage(buffer.usage, BufferUsages::INDEX)
check_buffer_usage(buffer_id, buffer.usage, BufferUsages::INDEX)
.map_pass_err(scope)?;
let buf_raw = buffer
.raw
Expand Down Expand Up @@ -1737,7 +1737,7 @@ impl Global {
.map_pass_err(scope);
}

check_buffer_usage(buffer.usage, BufferUsages::VERTEX)
check_buffer_usage(buffer_id, buffer.usage, BufferUsages::VERTEX)
.map_pass_err(scope)?;
let buf_raw = buffer
.raw
Expand Down Expand Up @@ -2034,8 +2034,12 @@ impl Global {
.buffers
.merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDIRECT)
.map_pass_err(scope)?;
check_buffer_usage(indirect_buffer.usage, BufferUsages::INDIRECT)
.map_pass_err(scope)?;
check_buffer_usage(
buffer_id,
indirect_buffer.usage,
BufferUsages::INDIRECT,
)
.map_pass_err(scope)?;
let indirect_raw = indirect_buffer
.raw
.get(&snatch_guard)
Expand Down Expand Up @@ -2106,8 +2110,12 @@ impl Global {
.buffers
.merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDIRECT)
.map_pass_err(scope)?;
check_buffer_usage(indirect_buffer.usage, BufferUsages::INDIRECT)
.map_pass_err(scope)?;
check_buffer_usage(
buffer_id,
indirect_buffer.usage,
BufferUsages::INDIRECT,
)
.map_pass_err(scope)?;
let indirect_raw = indirect_buffer
.raw
.get(&snatch_guard)
Expand All @@ -2123,7 +2131,7 @@ impl Global {
hal::BufferUses::INDIRECT,
)
.map_pass_err(scope)?;
check_buffer_usage(count_buffer.usage, BufferUsages::INDIRECT)
check_buffer_usage(buffer_id, count_buffer.usage, BufferUsages::INDIRECT)
.map_pass_err(scope)?;
let count_raw = count_buffer
.raw
Expand Down
6 changes: 3 additions & 3 deletions wgpu-core/src/device/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ impl Global {
.buffers
.get(buffer_id)
.map_err(|_| BufferAccessError::Invalid)?;
check_buffer_usage(buffer.usage, wgt::BufferUsages::MAP_WRITE)?;
check_buffer_usage(buffer_id, buffer.usage, wgt::BufferUsages::MAP_WRITE)?;
//assert!(buffer isn't used by the GPU);

#[cfg(feature = "trace")]
Expand Down Expand Up @@ -446,7 +446,7 @@ impl Global {
.buffers
.get(buffer_id)
.map_err(|_| BufferAccessError::Invalid)?;
check_buffer_usage(buffer.usage, wgt::BufferUsages::MAP_READ)?;
check_buffer_usage(buffer_id, buffer.usage, wgt::BufferUsages::MAP_READ)?;
//assert!(buffer isn't used by the GPU);

let raw_buf = buffer
Expand Down Expand Up @@ -2394,7 +2394,7 @@ impl Global {
return Err((op, DeviceError::Lost.into()));
}

if let Err(e) = check_buffer_usage(buffer.usage, pub_usage) {
if let Err(e) = check_buffer_usage(buffer.info.id(), buffer.usage, pub_usage) {
return Err((op, e.into()));
}

Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/src/device/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1837,7 +1837,7 @@ impl<A: HalApi> Device<A> {
.add_single(storage, bb.buffer_id, internal_use)
.ok_or(Error::InvalidBuffer(bb.buffer_id))?;

check_buffer_usage(buffer.usage, pub_usage)?;
check_buffer_usage(bb.buffer_id, buffer.usage, pub_usage)?;
let raw_buffer = buffer
.raw
.get(snatch_guard)
Expand Down
16 changes: 13 additions & 3 deletions wgpu-core/src/validation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{device::bgl, FastHashMap, FastHashSet};
use crate::{
device::bgl,
id::{markers::Buffer, Id},
FastHashMap, FastHashSet,
};
use arrayvec::ArrayVec;
use std::{collections::hash_map::Entry, fmt};
use thiserror::Error;
Expand Down Expand Up @@ -134,20 +138,26 @@ pub struct Interface {
}

#[derive(Clone, Debug, Error)]
#[error("Buffer usage is {actual:?} which does not contain required usage {expected:?}")]
#[error("Usage flags {actual:?} for buffer {id:?} do not contain required usage flags {expected:?}")]
pub struct MissingBufferUsageError {
pub(crate) id: Id<Buffer>,
pub(crate) actual: wgt::BufferUsages,
pub(crate) expected: wgt::BufferUsages,
}

/// Checks that the given buffer usage contains the required buffer usage,
/// returns an error otherwise.
pub fn check_buffer_usage(
id: Id<Buffer>,
actual: wgt::BufferUsages,
expected: wgt::BufferUsages,
) -> Result<(), MissingBufferUsageError> {
if !actual.contains(expected) {
Err(MissingBufferUsageError { actual, expected })
Err(MissingBufferUsageError {
id,
actual,
expected,
})
} else {
Ok(())
}
Expand Down

0 comments on commit 24bbf1d

Please sign in to comment.