WGSL

Some people may find it advantageous to use raw WGSL code in their project. We support these projects, but recommend for ease of use that you use a different workload type if you are running a common workload.

Feature support

All language features are supported. There are some limitations on the way you can use a user's device:

  • All code must be thoroughly tested for GPU crashes (you can use your own computer, and we also have containers we can provide for testing)

  • You may need Entitlements depending on how you use the user's computer

Example

A basic function to take the exponent of a base and a number repeatedly. BUFFER_SIZE is typically set to the maximum of 134217728 bytes, or 128MB to prevent crashes. It is replaced at runtime.

@group(0) @binding(0)
var<storage, read_write> output: array<f32>;

@compute @workgroup_size(64)
fn main(
  @builtin(global_invocation_id)
  global_id : vec3u,

  @builtin(local_invocation_id)
  local_id : vec3u,
) {
  // Avoid accessing the buffer out of bounds
  if (global_id.x >= ${BUFFER_SIZE}) {
    return;
  }

  output[global_id.x] =
    ldexp(f32(global_id.x), i32(local_id.x));
}

Last updated