Skip to content

Commit

Permalink
Perform "valid to use with" checks when recording render bundles.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimblandy committed May 23, 2022
1 parent 0734e1b commit 4c6d246
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
44 changes: 38 additions & 6 deletions wgpu-core/src/command/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ impl RenderBundleEncoder {
} => {
let scope = PassErrorScope::SetBindGroup(bind_group_id);

let bind_group: &crate::binding_model::BindGroup<A> = state
.trackers
.bind_groups
.use_extend(&*bind_group_guard, bind_group_id, (), ())
.map_err(|_| RenderCommandError::InvalidBindGroup(bind_group_id))
.map_pass_err(scope)?;
self.check_valid_to_use(bind_group.device_id.value)
.map_pass_err(scope)?;

let max_bind_groups = device.limits.max_bind_groups;
if (index as u32) >= max_bind_groups {
return Err(RenderCommandError::BindGroupIndexOutOfRange {
Expand All @@ -266,12 +275,6 @@ impl RenderBundleEncoder {
next_dynamic_offset = offsets_range.end;
let offsets = &base.dynamic_offsets[offsets_range.clone()];

let bind_group = state
.trackers
.bind_groups
.use_extend(&*bind_group_guard, bind_group_id, (), ())
.map_err(|_| RenderCommandError::InvalidBindGroup(bind_group_id))
.map_pass_err(scope)?;
if bind_group.dynamic_binding_info.len() != offsets.len() {
return Err(RenderCommandError::InvalidDynamicOffsetCount {
actual: offsets.len(),
Expand Down Expand Up @@ -318,6 +321,8 @@ impl RenderBundleEncoder {
.use_extend(&*pipeline_guard, pipeline_id, (), ())
.map_err(|_| RenderCommandError::InvalidPipeline(pipeline_id))
.map_pass_err(scope)?;
self.check_valid_to_use(pipeline.device_id.value)
.map_pass_err(scope)?;

self.context
.check_compatible(&pipeline.pass_context)
Expand Down Expand Up @@ -357,6 +362,8 @@ impl RenderBundleEncoder {
.buffers
.use_extend(&*buffer_guard, buffer_id, (), hal::BufferUses::INDEX)
.unwrap();
self.check_valid_to_use(buffer.device_id.value)
.map_pass_err(scope)?;
check_buffer_usage(buffer.usage, wgt::BufferUsages::INDEX)
.map_pass_err(scope)?;

Expand Down Expand Up @@ -384,6 +391,8 @@ impl RenderBundleEncoder {
.buffers
.use_extend(&*buffer_guard, buffer_id, (), hal::BufferUses::VERTEX)
.unwrap();
self.check_valid_to_use(buffer.device_id.value)
.map_pass_err(scope)?;
check_buffer_usage(buffer.usage, wgt::BufferUsages::VERTEX)
.map_pass_err(scope)?;

Expand Down Expand Up @@ -509,6 +518,8 @@ impl RenderBundleEncoder {
.buffers
.use_extend(&*buffer_guard, buffer_id, (), hal::BufferUses::INDIRECT)
.unwrap();
self.check_valid_to_use(buffer.device_id.value)
.map_pass_err(scope)?;
check_buffer_usage(buffer.usage, wgt::BufferUsages::INDIRECT)
.map_pass_err(scope)?;

Expand Down Expand Up @@ -543,6 +554,8 @@ impl RenderBundleEncoder {
.use_extend(&*buffer_guard, buffer_id, (), hal::BufferUses::INDIRECT)
.map_err(|err| RenderCommandError::Buffer(buffer_id, err))
.map_pass_err(scope)?;
self.check_valid_to_use(buffer.device_id.value)
.map_pass_err(scope)?;
check_buffer_usage(buffer.usage, wgt::BufferUsages::INDIRECT)
.map_pass_err(scope)?;

Expand Down Expand Up @@ -594,6 +607,17 @@ impl RenderBundleEncoder {
})
}

pub(crate) fn check_valid_to_use(
&self,
device_id: id::Valid<id::DeviceId>,
) -> Result<(), crate::validation::ValidToUseError> {
if device_id.0 != self.parent_id {
return Err(crate::validation::ValidToUseError);
}

Ok(())
}

pub fn set_index_buffer(
&mut self,
buffer_id: id::BufferId,
Expand Down Expand Up @@ -1272,6 +1296,8 @@ pub(super) enum RenderBundleErrorInner {
Draw(#[from] DrawError),
#[error(transparent)]
MissingDownlevelFlags(#[from] MissingDownlevelFlags),
#[error(transparent)]
ValidToUse(crate::validation::ValidToUseError),
}

impl<T> From<T> for RenderBundleErrorInner
Expand Down Expand Up @@ -1319,6 +1345,12 @@ where
}
}

impl From<crate::validation::ValidToUseError> for RenderBundleErrorInner {
fn from(err: crate::validation::ValidToUseError) -> Self {
RenderBundleErrorInner::ValidToUse(err)
}
}

pub mod bundle_ffi {
use super::{RenderBundleEncoder, RenderCommand};
use crate::{id, RawString};
Expand Down
4 changes: 4 additions & 0 deletions wgpu-core/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,3 +1186,7 @@ impl Interface {
Ok(outputs)
}
}

#[derive(Clone, Debug, Error)]
#[error("GPUObject is not valid to use with method target")]
pub(crate) struct ValidToUseError;

0 comments on commit 4c6d246

Please sign in to comment.