Menu

Testing WebGPU data layouts with Facet

www.mattkeeter.com·about 1 month ago
#v4MkzUZl
#webgpu#wgsl#rust#shaders#zerocopy
Reading 0:00
15s threshold

When working on GPU compute shaders, it's common to want to share snippets of configuration data between the host and the shader. Here's a toy example, adapted from fidget-wgpu :

// WGSL struct Config { /// Screen-to-model transform matrix mat: mat3x3f,

/// Image size, in pixels image_size: vec2u,

/// Z position at which to render the image z: f32, }

// Rust #[derive( zerocopy::IntoBytes, zerocopy::Immutable, zerocopy::FromBytes, zerocopy::KnownLayout )] #[repr(C)] struct Config { mat: [[f32; 3]; 3], image_size: [u32; 2], z: f32, }

Thanks to the zerocopy annotations, we can call as_bytes() and write the configuration data directly into a WebGPU buffer, which is very convenient!

Unfortunately, there are often subtle difference between Rust and WebGPU's layout rules.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.