Crate sgpu_compute

source ·
Expand description

§SGPU-Compute - Simple GPU-Compute using WebGPU

This crate aims to provide a simple and easy-to-use interface to run compute shaders with WGPU and WGSL. It is designed to be as simple as possible to use, while still providing a lot of flexibility for performance reason.

§Shader buffer

Three buffers are declared by default: - uniform for the uniform buffer - in for the input buffer - out for the output buffer Their types are inferred from the run method. The scratchpad buffer is also available, but it is not required.

§Example

use sgpu_compute::prelude::*;

let my_shader = "
   @group(0) @binding(0) var<uniform> coefficient: u32;
   @group(0) @binding(1) var<storage, read> in: array<u32>;
   @group(0) @binding(2) var<storage, read_write> out: array<u32>;

   @compute
   @workgroup_size(8, 1, 1)
   fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
       out[global_id.x] = coefficient * in[global_id.x];
   }
";

const N_ELEMENT: usize = 64;
const WORKGROUP_SIZE: usize = 8;
const N_WORKGROUP: u32 = (N_ELEMENT / WORKGROUP_SIZE) as u32;

let gpu = GpuCompute::new();
let mut pipeline = gpu.gen_pipeline(
       None,
       [StageDesc {
           name: Some("norm"),
           shader: my_shader,
           entrypoint: "main",
       }],
   );

const COEFFICIENT: u32 = 42;

let input: [u32; N_ELEMENT] = std::array::from_fn(|i| i as u32);
pipeline.write_uniform(&COEFFICIENT);
let result_gpu = pipeline.run(&input, [(N_WORKGROUP, 1, 1)], |vals: &[u32; N_ELEMENT]| *vals);
let result_cpu = input.map(|v| v * COEFFICIENT);
assert_eq!(result_gpu, result_cpu);

Modules§

Structs§

  • This is the main struct of the library. It is used to create pipelines and run them. It requires an async runtime to work. If you want a blocking version, you can use the GpuCompute struct. If you don’t use the blocking version disable default features.
  • This struct represents a pipeline. It is used to run async compute shaders. To build it use the gen_pipeline method of the GpuComputeAsync struct.