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 25, 2022
1 parent be13840 commit eba248a
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 @@ -263,6 +263,15 @@ impl RenderBundleEncoder {
} => {
let scope = PassErrorScope::SetBindGroup(bind_group_id);

let bind_group: &binding_model::BindGroup<A> = state
.trackers
.bind_groups
.add_single(&*bind_group_guard, bind_group_id)
.ok_or(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 @@ -279,12 +288,6 @@ impl RenderBundleEncoder {
next_dynamic_offset = offsets_range.end;
let offsets = &base.dynamic_offsets[offsets_range.clone()];

let bind_group: &binding_model::BindGroup<A> = state
.trackers
.bind_groups
.add_single(&*bind_group_guard, bind_group_id)
.ok_or(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 @@ -333,6 +336,8 @@ impl RenderBundleEncoder {
.add_single(&*pipeline_guard, pipeline_id)
.ok_or(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 @@ -372,6 +377,8 @@ impl RenderBundleEncoder {
.buffers
.merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDEX)
.map_pass_err(scope)?;
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 @@ -399,6 +406,8 @@ impl RenderBundleEncoder {
.buffers
.merge_single(&*buffer_guard, buffer_id, hal::BufferUses::VERTEX)
.map_pass_err(scope)?;
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 @@ -524,6 +533,8 @@ impl RenderBundleEncoder {
.buffers
.merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDIRECT)
.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 @@ -557,6 +568,8 @@ impl RenderBundleEncoder {
.buffers
.merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDIRECT)
.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 @@ -608,6 +621,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 @@ -1282,6 +1306,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 @@ -1329,6 +1355,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 eba248a

Please sign in to comment.