CreateBindGroup validation error on device mismatch (#5596)

* Fix cts_runner command invocation in readme

* Remove assertDeviceMatch from deno_webgpu in createBindGroup

This should be done as verification in wgpu-core.

* Add device mismatched check to create_buffer_binding

* Extract common logic to create_sampler_binding

* Move common logic to create_texture_binding and add device mismatch check
This commit is contained in:
Samson 2024-04-25 12:17:00 +02:00 committed by GitHub
parent 18ceb5183c
commit 5735f85720
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 116 additions and 106 deletions

View File

@ -199,7 +199,7 @@ To run a given set of tests:
``` ```
# Must be inside the `cts` folder we just checked out, else this will fail # Must be inside the `cts` folder we just checked out, else this will fail
cargo run --manifest-path ../Cargo.toml --bin cts_runner -- ./tools/run_deno --verbose "<test string>" cargo run --manifest-path ../Cargo.toml -p cts_runner --bin cts_runner -- ./tools/run_deno --verbose "<test string>"
``` ```
To find the full list of tests, go to the [online cts viewer](https://gpuweb.github.io/cts/standalone/?runnow=0&worker=0&debug=0&q=webgpu:*). To find the full list of tests, go to the [online cts viewer](https://gpuweb.github.io/cts/standalone/?runnow=0&worker=0&debug=0&q=webgpu:*).

View File

@ -1289,11 +1289,6 @@ class GPUDevice extends EventTarget {
const resource = entry.resource; const resource = entry.resource;
if (ObjectPrototypeIsPrototypeOf(GPUSamplerPrototype, resource)) { if (ObjectPrototypeIsPrototypeOf(GPUSamplerPrototype, resource)) {
const rid = assertResource(resource, prefix, context); const rid = assertResource(resource, prefix, context);
assertDeviceMatch(device, resource, {
prefix,
resourceContext: context,
selfContext: "this",
});
return { return {
binding: entry.binding, binding: entry.binding,
kind: "GPUSampler", kind: "GPUSampler",
@ -1304,11 +1299,6 @@ class GPUDevice extends EventTarget {
) { ) {
const rid = assertResource(resource, prefix, context); const rid = assertResource(resource, prefix, context);
assertResource(resource[_texture], prefix, context); assertResource(resource[_texture], prefix, context);
assertDeviceMatch(device, resource[_texture], {
prefix,
resourceContext: context,
selfContext: "this",
});
return { return {
binding: entry.binding, binding: entry.binding,
kind: "GPUTextureView", kind: "GPUTextureView",
@ -1318,11 +1308,6 @@ class GPUDevice extends EventTarget {
// deno-lint-ignore prefer-primordials // deno-lint-ignore prefer-primordials
const rid = assertResource(resource.buffer, prefix, context); const rid = assertResource(resource.buffer, prefix, context);
// deno-lint-ignore prefer-primordials // deno-lint-ignore prefer-primordials
assertDeviceMatch(device, resource.buffer, {
prefix,
resourceContext: context,
selfContext: "this",
});
return { return {
binding: entry.binding, binding: entry.binding,
kind: "GPUBufferBinding", kind: "GPUBufferBinding",

View File

@ -13,6 +13,7 @@ use crate::{
hal_api::HalApi, hal_api::HalApi,
hal_label, hal_label,
hub::Hub, hub::Hub,
id,
init_tracker::{ init_tracker::{
BufferInitTracker, BufferInitTrackerAction, MemoryInitKind, TextureInitRange, BufferInitTracker, BufferInitTrackerAction, MemoryInitKind, TextureInitRange,
TextureInitTracker, TextureInitTrackerAction, TextureInitTracker, TextureInitTrackerAction,
@ -1949,6 +1950,7 @@ impl<A: HalApi> Device<A> {
used: &mut BindGroupStates<A>, used: &mut BindGroupStates<A>,
storage: &'a Storage<Buffer<A>>, storage: &'a Storage<Buffer<A>>,
limits: &wgt::Limits, limits: &wgt::Limits,
device_id: id::Id<id::markers::Device>,
snatch_guard: &'a SnatchGuard<'a>, snatch_guard: &'a SnatchGuard<'a>,
) -> Result<hal::BufferBinding<'a, A>, binding_model::CreateBindGroupError> { ) -> Result<hal::BufferBinding<'a, A>, binding_model::CreateBindGroupError> {
use crate::binding_model::CreateBindGroupError as Error; use crate::binding_model::CreateBindGroupError as Error;
@ -1967,6 +1969,7 @@ impl<A: HalApi> Device<A> {
}) })
} }
}; };
let (pub_usage, internal_use, range_limit) = match binding_ty { let (pub_usage, internal_use, range_limit) = match binding_ty {
wgt::BufferBindingType::Uniform => ( wgt::BufferBindingType::Uniform => (
wgt::BufferUsages::UNIFORM, wgt::BufferUsages::UNIFORM,
@ -1999,6 +2002,10 @@ impl<A: HalApi> Device<A> {
.add_single(storage, bb.buffer_id, internal_use) .add_single(storage, bb.buffer_id, internal_use)
.ok_or(Error::InvalidBuffer(bb.buffer_id))?; .ok_or(Error::InvalidBuffer(bb.buffer_id))?;
if buffer.device.as_info().id() != device_id {
return Err(DeviceError::WrongDevice.into());
}
check_buffer_usage(bb.buffer_id, buffer.usage, pub_usage)?; check_buffer_usage(bb.buffer_id, buffer.usage, pub_usage)?;
let raw_buffer = buffer let raw_buffer = buffer
.raw .raw
@ -2077,13 +2084,53 @@ impl<A: HalApi> Device<A> {
}) })
} }
pub(crate) fn create_texture_binding( fn create_sampler_binding<'a>(
view: &TextureView<A>, used: &BindGroupStates<A>,
internal_use: hal::TextureUses, storage: &'a Storage<Sampler<A>>,
pub_usage: wgt::TextureUsages, id: id::Id<id::markers::Sampler>,
device_id: id::Id<id::markers::Device>,
) -> Result<&'a Sampler<A>, binding_model::CreateBindGroupError> {
use crate::binding_model::CreateBindGroupError as Error;
let sampler = used
.samplers
.add_single(storage, id)
.ok_or(Error::InvalidSampler(id))?;
if sampler.device.as_info().id() != device_id {
return Err(DeviceError::WrongDevice.into());
}
Ok(sampler)
}
pub(crate) fn create_texture_binding<'a>(
self: &Arc<Self>,
binding: u32,
decl: &wgt::BindGroupLayoutEntry,
storage: &'a Storage<TextureView<A>>,
id: id::Id<id::markers::TextureView>,
used: &mut BindGroupStates<A>, used: &mut BindGroupStates<A>,
used_texture_ranges: &mut Vec<TextureInitTrackerAction<A>>, used_texture_ranges: &mut Vec<TextureInitTrackerAction<A>>,
) -> Result<(), binding_model::CreateBindGroupError> { snatch_guard: &'a SnatchGuard<'a>,
) -> Result<hal::TextureBinding<'a, A>, binding_model::CreateBindGroupError> {
use crate::binding_model::CreateBindGroupError as Error;
let view = used
.views
.add_single(storage, id)
.ok_or(Error::InvalidTextureView(id))?;
if view.device.as_info().id() != self.as_info().id() {
return Err(DeviceError::WrongDevice.into());
}
let (pub_usage, internal_use) = self.texture_use_parameters(
binding,
decl,
view,
"SampledTexture, ReadonlyStorageTexture or WriteonlyStorageTexture",
)?;
let texture = &view.parent; let texture = &view.parent;
let texture_id = texture.as_info().id(); let texture_id = texture.as_info().id();
// Careful here: the texture may no longer have its own ref count, // Careful here: the texture may no longer have its own ref count,
@ -2113,7 +2160,12 @@ impl<A: HalApi> Device<A> {
kind: MemoryInitKind::NeedsInitializedMemory, kind: MemoryInitKind::NeedsInitializedMemory,
}); });
Ok(()) Ok(hal::TextureBinding {
view: view
.raw(snatch_guard)
.ok_or(Error::InvalidTextureView(id))?,
usage: internal_use,
})
} }
// This function expects the provided bind group layout to be resolved // This function expects the provided bind group layout to be resolved
@ -2175,6 +2227,7 @@ impl<A: HalApi> Device<A> {
&mut used, &mut used,
&*buffer_guard, &*buffer_guard,
&self.limits, &self.limits,
self.as_info().id(),
&snatch_guard, &snatch_guard,
)?; )?;
@ -2198,31 +2251,27 @@ impl<A: HalApi> Device<A> {
&mut used, &mut used,
&*buffer_guard, &*buffer_guard,
&self.limits, &self.limits,
self.as_info().id(),
&snatch_guard, &snatch_guard,
)?; )?;
hal_buffers.push(bb); hal_buffers.push(bb);
} }
(res_index, num_bindings) (res_index, num_bindings)
} }
Br::Sampler(id) => { Br::Sampler(id) => match decl.ty {
match decl.ty {
wgt::BindingType::Sampler(ty) => { wgt::BindingType::Sampler(ty) => {
let sampler = used let sampler = Self::create_sampler_binding(
.samplers &used,
.add_single(&*sampler_guard, id) &sampler_guard,
.ok_or(Error::InvalidSampler(id))?; id,
self.as_info().id(),
)?;
if sampler.device.as_info().id() != self.as_info().id() {
return Err(DeviceError::WrongDevice.into());
}
// Allowed sampler values for filtering and comparison
let (allowed_filtering, allowed_comparison) = match ty { let (allowed_filtering, allowed_comparison) = match ty {
wgt::SamplerBindingType::Filtering => (None, false), wgt::SamplerBindingType::Filtering => (None, false),
wgt::SamplerBindingType::NonFiltering => (Some(false), false), wgt::SamplerBindingType::NonFiltering => (Some(false), false),
wgt::SamplerBindingType::Comparison => (None, true), wgt::SamplerBindingType::Comparison => (None, true),
}; };
if let Some(allowed_filtering) = allowed_filtering { if let Some(allowed_filtering) = allowed_filtering {
if allowed_filtering != sampler.filtering { if allowed_filtering != sampler.filtering {
return Err(Error::WrongSamplerFiltering { return Err(Error::WrongSamplerFiltering {
@ -2232,7 +2281,6 @@ impl<A: HalApi> Device<A> {
}); });
} }
} }
if allowed_comparison != sampler.comparison { if allowed_comparison != sampler.comparison {
return Err(Error::WrongSamplerComparison { return Err(Error::WrongSamplerComparison {
binding, binding,
@ -2252,51 +2300,37 @@ impl<A: HalApi> Device<A> {
expected: "Sampler", expected: "Sampler",
}) })
} }
} },
}
Br::SamplerArray(ref bindings_array) => { Br::SamplerArray(ref bindings_array) => {
let num_bindings = bindings_array.len(); let num_bindings = bindings_array.len();
Self::check_array_binding(self.features, decl.count, num_bindings)?; Self::check_array_binding(self.features, decl.count, num_bindings)?;
let res_index = hal_samplers.len(); let res_index = hal_samplers.len();
for &id in bindings_array.iter() { for &id in bindings_array.iter() {
let sampler = used let sampler = Self::create_sampler_binding(
.samplers &used,
.add_single(&*sampler_guard, id) &sampler_guard,
.ok_or(Error::InvalidSampler(id))?; id,
if sampler.device.as_info().id() != self.as_info().id() { self.as_info().id(),
return Err(DeviceError::WrongDevice.into()); )?;
}
hal_samplers.push(sampler.raw()); hal_samplers.push(sampler.raw());
} }
(res_index, num_bindings) (res_index, num_bindings)
} }
Br::TextureView(id) => { Br::TextureView(id) => {
let view = used let tb = self.create_texture_binding(
.views
.add_single(&*texture_view_guard, id)
.ok_or(Error::InvalidTextureView(id))?;
let (pub_usage, internal_use) = self.texture_use_parameters(
binding, binding,
decl, decl,
view, &texture_view_guard,
"SampledTexture, ReadonlyStorageTexture or WriteonlyStorageTexture", id,
)?;
Self::create_texture_binding(
view,
internal_use,
pub_usage,
&mut used, &mut used,
&mut used_texture_ranges, &mut used_texture_ranges,
&snatch_guard,
)?; )?;
let res_index = hal_textures.len(); let res_index = hal_textures.len();
hal_textures.push(hal::TextureBinding { hal_textures.push(tb);
view: view
.raw(&snatch_guard)
.ok_or(Error::InvalidTextureView(id))?,
usage: internal_use,
});
(res_index, 1) (res_index, 1)
} }
Br::TextureViewArray(ref bindings_array) => { Br::TextureViewArray(ref bindings_array) => {
@ -2305,26 +2339,17 @@ impl<A: HalApi> Device<A> {
let res_index = hal_textures.len(); let res_index = hal_textures.len();
for &id in bindings_array.iter() { for &id in bindings_array.iter() {
let view = used let tb = self.create_texture_binding(
.views binding,
.add_single(&*texture_view_guard, id) decl,
.ok_or(Error::InvalidTextureView(id))?; &texture_view_guard,
let (pub_usage, internal_use) = id,
self.texture_use_parameters(binding, decl, view,
"SampledTextureArray, ReadonlyStorageTextureArray or WriteonlyStorageTextureArray")?;
Self::create_texture_binding(
view,
internal_use,
pub_usage,
&mut used, &mut used,
&mut used_texture_ranges, &mut used_texture_ranges,
&snatch_guard,
)?; )?;
hal_textures.push(hal::TextureBinding {
view: view hal_textures.push(tb);
.raw(&snatch_guard)
.ok_or(Error::InvalidTextureView(id))?,
usage: internal_use,
});
} }
(res_index, num_bindings) (res_index, num_bindings)