Skip to content

A tiny utility macro to verify WGSL at compile time

Notifications You must be signed in to change notification settings

ashpil/include_wgsl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 

Repository files navigation

include_wgsl

Crates.io Docs.rs

A tiny proc macro to include a WGSL file in your binary, and verify that it is valid at compile time.

Example

This is how you might create a wgpu shader module:

let shader_str = include_wgsl!("shader.wgsl");
device.create_shader_module(&ShaderModuleDescriptor {
    source: ShaderSource::Wgsl(Cow::Borrowed(&shader_str)),
    flags: ShaderFlags::default(),
    label: None,
})

This functions exactly as it would if you had used include_str!("shader.wgsl"), but it also makes sure at compile time that your WGSL is valid using naga.

If your WGSL code is valid, compliation continues on, but if your WGSL is invalid, you will get a friendly naga error, and compliation will halt:

error: Unable to parse shader.wgsl

error: invalid field accessor `world_positoon`
   ┌─ wgsl:33:933out.world_positoon = world_position.xyz;
   │         ^^^^^^^^^^^^^^ invalid accessor

The syntax errors are typically pretty understandable, but at the moment naga validation errors are a bit dense. I do not consider improving them part of the scope of this project, but will happily update this crate once naga has made them more approachable.

Nightly

This crate is currently nightly-only. If I have to choose between nightly or fewer dependencies, I choose nightly, and I have adhered to that philosophy in this crate.

About

A tiny utility macro to verify WGSL at compile time

Resources

Stars

Watchers

Forks

Languages