@group(0) @binding(0) var<storage,read> firstArray: array<f32>;
@group(0) @binding(1) var<storage,read> secondArray: array<f32>;
@group(0) @binding(2) var<storage,read_write> resultArray: array<f32>;
@compute @workgroup_size(1, 1, 1)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
let index: u32 = global_id.x;
resultArray[index] = firstArray[index] + secondArray[index];
}
fn calc_gpu_things(mut compute_worker: ResMut<AppComputeWorker<SimpleComputeWorker>>) {
if !compute_worker.ready() {
return;
};
compute_worker.write_slice("firstArray", &[2.0, 3.0, 5.0, 6.0]);
//compute_worker.write("secondArray", &[2.0, 3.0, 4.0, 5.0]);
let result: [f32; 4] = compute_worker.read("resultArray");
println!("got {:?}", result);
}
#[derive(TypeUuid)]
#[uuid = "2545ae14-a9bc-4f03-9ea4-4eb43d1075a7"]
struct SimpleShader;
impl ComputeShader for SimpleShader {
fn shader() -> ShaderRef {
"compute1.wgsl".into()
}
}
#[derive(Resource)]
struct SimpleComputeWorker;
impl ComputeWorker for SimpleComputeWorker {
fn build(world: &mut World) -> AppComputeWorker<Self> {
let worker = AppComputeWorkerBuilder::new(world)
// Add a staging buffer, it will be available from
// both CPU and GPU land.
.add_staging("firstArray", &[2.0, 3.0, 4.0, 5.0])
.add_staging("secondArray", &[2.0, 3.0, 4.0, 5.0])
.add_staging("resultArray", &[0.0, 0.0, 0.0, 0.0])
// Create a compute pass from your compute shader
// and define used variables
.add_pass::<SimpleShader>([4, 1, 1], &["firstArray", "secondArray", "resultArray"])
.build();
worker
}
}
thread 'Compute Task Pool (4)' panicked at C:\Users\bramb\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-0.17.2\src\backend\direct.rs:3056:5:
wgpu error: Validation Error
Caused by:
In Queue::write_buffer
Copy of 0..32 would end up overrunning the bounds of the Destination buffer of size 16