-
Notifications
You must be signed in to change notification settings - Fork 936
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
by injecting a compute shader that validates the content of the indirect buffer
- Loading branch information
Showing
10 changed files
with
508 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use wgpu_test::{gpu_test, GpuTestConfiguration, TestParameters}; | ||
|
||
const SHADER_SRC: &str = " | ||
@group(0) @binding(0) | ||
var<storage, read_write> out: u32; | ||
@compute @workgroup_size(1) | ||
fn main() { | ||
out = 1u; | ||
} | ||
"; | ||
|
||
#[gpu_test] | ||
static CHECK_NUM_WORKGROUPS: GpuTestConfiguration = GpuTestConfiguration::new() | ||
.parameters(TestParameters::default().test_features_limits()) | ||
.run_async(|ctx| async move { | ||
let module = ctx | ||
.device | ||
.create_shader_module(wgpu::ShaderModuleDescriptor { | ||
label: None, | ||
source: wgpu::ShaderSource::Wgsl(SHADER_SRC.into()), | ||
}); | ||
|
||
let pipeline = ctx | ||
.device | ||
.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { | ||
label: None, | ||
layout: None, | ||
module: &module, | ||
entry_point: "main", | ||
compilation_options: Default::default(), | ||
cache: None, | ||
}); | ||
|
||
let out_buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor { | ||
label: None, | ||
size: 4, | ||
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC, | ||
mapped_at_creation: false, | ||
}); | ||
|
||
let readback_buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor { | ||
label: None, | ||
size: 4, | ||
usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, | ||
mapped_at_creation: false, | ||
}); | ||
|
||
let indirect_buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor { | ||
label: None, | ||
size: 12, | ||
usage: wgpu::BufferUsages::INDIRECT | ||
| wgpu::BufferUsages::COPY_DST | ||
| wgpu::BufferUsages::UNIFORM, | ||
mapped_at_creation: false, | ||
}); | ||
|
||
let max = ctx.adapter.limits().max_compute_workgroups_per_dimension; | ||
ctx.queue | ||
.write_buffer(&indirect_buffer, 0, bytemuck::bytes_of(&[max + 1, 1, 1])); | ||
|
||
let bind_group = ctx.device.create_bind_group(&wgpu::BindGroupDescriptor { | ||
label: None, | ||
layout: &pipeline.get_bind_group_layout(0), | ||
entries: &[wgpu::BindGroupEntry { | ||
binding: 0, | ||
resource: out_buffer.as_entire_binding(), | ||
}], | ||
}); | ||
|
||
let mut encoder = ctx | ||
.device | ||
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default()); | ||
|
||
{ | ||
let mut compute_pass = | ||
encoder.begin_compute_pass(&wgpu::ComputePassDescriptor::default()); | ||
compute_pass.set_pipeline(&pipeline); | ||
compute_pass.set_bind_group(0, &bind_group, &[]); | ||
compute_pass.dispatch_workgroups_indirect(&indirect_buffer, 0); | ||
} | ||
|
||
encoder.copy_buffer_to_buffer(&out_buffer, 0, &readback_buffer, 0, 4); | ||
|
||
ctx.queue.submit(Some(encoder.finish())); | ||
|
||
readback_buffer | ||
.slice(..) | ||
.map_async(wgpu::MapMode::Read, |_| {}); | ||
|
||
ctx.async_poll(wgpu::Maintain::wait()) | ||
.await | ||
.panic_on_timeout(); | ||
|
||
let view = readback_buffer.slice(..).get_mapped_range(); | ||
// Make sure the dispatch was discarded | ||
assert!(view.iter().all(|v| *v == 0)); | ||
|
||
// Test that unsetting the bind group works properly | ||
{ | ||
ctx.device.push_error_scope(wgpu::ErrorFilter::Validation); | ||
let mut encoder = ctx | ||
.device | ||
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default()); | ||
{ | ||
let mut compute_pass = | ||
encoder.begin_compute_pass(&wgpu::ComputePassDescriptor::default()); | ||
compute_pass.set_pipeline(&pipeline); | ||
compute_pass.dispatch_workgroups_indirect(&indirect_buffer, 0); | ||
} | ||
let _ = encoder.finish(); | ||
let error = pollster::block_on(ctx.device.pop_error_scope()); | ||
assert!(error.map_or(false, |error| format!("{error}") | ||
.contains("Expected bind group is missing"))); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.