Skip to content

Commit f55e365

Browse files
cwfitzgeraldkpreid
andcommitted
Convert error scopes to use thread-local guards
Co-authored-by: Kevin Reid <kpreid@switchb.org>
1 parent 5895de8 commit f55e365

File tree

22 files changed

+304
-95
lines changed

22 files changed

+304
-95
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,15 @@ One other breaking change worth noting is that in WGSL `@builtin(view_index)` no
106106

107107
By @SupaMaggie70Incorporated in [#8206](https://github.com/gfx-rs/wgpu/pull/8206).
108108

109-
#### Error Scopes are now thread-local
109+
#### Error scopes now use guards and are thread local.
110+
111+
```diff
112+
- device.push_error_scope(wgpu::ErrorFilter::Validation);
113+
+ let scope = device.push_error_scope(wgpu::ErrorFilter::Validation);
114+
// ... perform operations on the device ...
115+
- let error: Option<Error> = device.pop_error_scope().await;
116+
+ let error: Option<Error> = scope.pop().await;
117+
```
110118

111119
Device error scopes now operate on a per-thread basis. This allows them to be used easily within multithreaded contexts,
112120
without having the error scope capture errors from other threads.

examples/features/src/ray_cube_compute/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,6 @@ impl crate::framework::Example for Example {
420420
}
421421

422422
fn render(&mut self, view: &wgpu::TextureView, device: &wgpu::Device, queue: &wgpu::Queue) {
423-
device.push_error_scope(wgpu::ErrorFilter::Validation);
424-
425423
let anim_time = self.animation_timer.time();
426424

427425
self.tlas[0].as_mut().unwrap().transform =

examples/features/src/ray_cube_fragment/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,6 @@ impl crate::framework::Example for Example {
294294
}
295295

296296
fn render(&mut self, view: &wgpu::TextureView, device: &wgpu::Device, queue: &wgpu::Queue) {
297-
device.push_error_scope(wgpu::ErrorFilter::Validation);
298-
299297
// scene update
300298
{
301299
let dist = 12.0;

examples/features/src/ray_cube_normals/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,6 @@ impl crate::framework::Example for Example {
406406
}
407407

408408
fn render(&mut self, view: &wgpu::TextureView, device: &wgpu::Device, queue: &wgpu::Queue) {
409-
device.push_error_scope(wgpu::ErrorFilter::Validation);
410-
411409
let anim_time = self.animation_timer.time();
412410

413411
self.tlas[0].as_mut().unwrap().transform =

examples/features/src/ray_scene/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,6 @@ impl crate::framework::Example for Example {
464464
}
465465

466466
fn render(&mut self, view: &wgpu::TextureView, device: &wgpu::Device, queue: &wgpu::Queue) {
467-
device.push_error_scope(wgpu::ErrorFilter::Validation);
468-
469467
// scene update
470468
{
471469
let dist = 3.5;

examples/features/src/srgb_blend/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ impl<const SRGB: bool> crate::framework::Example for Example<SRGB> {
173173
}
174174

175175
fn render(&mut self, view: &wgpu::TextureView, device: &wgpu::Device, queue: &wgpu::Queue) {
176-
device.push_error_scope(wgpu::ErrorFilter::Validation);
177176
let mut encoder =
178177
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
179178
{

examples/standalone/custom_backend/src/custom.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,11 @@ impl DeviceInterface for CustomDevice {
251251
unimplemented!()
252252
}
253253

254-
fn push_error_scope(&self, _filter: wgpu::ErrorFilter) {
254+
fn push_error_scope(&self, _filter: wgpu::ErrorFilter) -> u32 {
255255
unimplemented!()
256256
}
257257

258-
fn pop_error_scope(&self) -> Pin<Box<dyn wgpu::custom::PopErrorScopeFuture>> {
258+
fn pop_error_scope(&self, _index: u32) -> Pin<Box<dyn wgpu::custom::PopErrorScopeFuture>> {
259259
unimplemented!()
260260
}
261261

tests/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,5 @@ wasm-bindgen.workspace = true
9494
web-sys = { workspace = true, features = ["CanvasRenderingContext2d", "Blob"] }
9595

9696
[lints.clippy]
97+
bool_assert_comparison = "allow"
9798
disallowed_types = "allow"

tests/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ pub fn fail<T>(
3939
callback: impl FnOnce() -> T,
4040
expected_msg_substring: Option<&str>,
4141
) -> T {
42-
device.push_error_scope(wgpu::ErrorFilter::Validation);
42+
let scope = device.push_error_scope(wgpu::ErrorFilter::Validation);
4343
let result = callback();
44-
let validation_error = pollster::block_on(device.pop_error_scope())
44+
let validation_error = pollster::block_on(scope.pop())
4545
.expect("expected validation error in callback, but no validation error was emitted");
4646
if let Some(expected_msg_substring) = expected_msg_substring {
4747
let lowered_expected = expected_msg_substring.to_lowercase();
@@ -63,9 +63,9 @@ pub fn fail<T>(
6363
/// Run some code in an error scope and assert that validation succeeds.
6464
#[track_caller]
6565
pub fn valid<T>(device: &wgpu::Device, callback: impl FnOnce() -> T) -> T {
66-
device.push_error_scope(wgpu::ErrorFilter::Validation);
66+
let scope = device.push_error_scope(wgpu::ErrorFilter::Validation);
6767
let result = callback();
68-
if let Some(error) = pollster::block_on(device.pop_error_scope()) {
68+
if let Some(error) = pollster::block_on(scope.pop()) {
6969
panic!(
7070
"`valid` block at {} encountered wgpu error:\n{error}",
7171
std::panic::Location::caller()
@@ -95,9 +95,9 @@ fn did_fill_error_scope<T>(
9595
callback: impl FnOnce() -> T,
9696
filter: wgpu::ErrorFilter,
9797
) -> (bool, T) {
98-
device.push_error_scope(filter);
98+
let scope = device.push_error_scope(filter);
9999
let result = callback();
100-
let validation_error = pollster::block_on(device.pop_error_scope());
100+
let validation_error = pollster::block_on(scope.pop());
101101
let failed = validation_error.is_some();
102102

103103
(failed, result)

tests/tests/wgpu-gpu/dispatch_workgroups_indirect.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static RESET_BIND_GROUPS: GpuTestConfiguration = GpuTestConfiguration::new()
7777
}).enable_noop(),
7878
)
7979
.run_async(|ctx| async move {
80-
ctx.device.push_error_scope(wgpu::ErrorFilter::Validation);
80+
let scope = ctx.device.push_error_scope(wgpu::ErrorFilter::Validation);
8181

8282
let test_resources = TestResources::new(&ctx);
8383

@@ -98,7 +98,7 @@ static RESET_BIND_GROUPS: GpuTestConfiguration = GpuTestConfiguration::new()
9898
}
9999
ctx.queue.submit(Some(encoder.finish()));
100100

101-
let error = pollster::block_on(ctx.device.pop_error_scope());
101+
let error = pollster::block_on(scope.pop());
102102
assert!(error.is_some_and(|error| {
103103
format!("{error}").contains("The current set ComputePipeline with '' label expects a BindGroup to be set at index 0")
104104
}));
@@ -120,7 +120,7 @@ static ZERO_SIZED_BUFFER: GpuTestConfiguration = GpuTestConfiguration::new()
120120
.enable_noop(),
121121
)
122122
.run_async(|ctx| async move {
123-
ctx.device.push_error_scope(wgpu::ErrorFilter::Validation);
123+
let scope = ctx.device.push_error_scope(wgpu::ErrorFilter::Validation);
124124

125125
let test_resources = TestResources::new(&ctx);
126126

@@ -141,7 +141,7 @@ static ZERO_SIZED_BUFFER: GpuTestConfiguration = GpuTestConfiguration::new()
141141
}
142142
ctx.queue.submit(Some(encoder.finish()));
143143

144-
let error = pollster::block_on(ctx.device.pop_error_scope());
144+
let error = pollster::block_on(scope.pop());
145145
assert!(error.is_some_and(|error| {
146146
format!("{error}").contains(
147147
"Indirect buffer uses bytes 0..12 which overruns indirect buffer of size 0",

0 commit comments

Comments
 (0)