https://github.com/mintlu8/wgsl_ln
Compile time wgsl import, linking and checking.
https://github.com/mintlu8/wgsl_ln
Last synced: about 1 year ago
JSON representation
Compile time wgsl import, linking and checking.
- Host: GitHub
- URL: https://github.com/mintlu8/wgsl_ln
- Owner: mintlu8
- License: apache-2.0
- Created: 2024-07-10T05:34:50.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-13T11:20:19.000Z (almost 2 years ago)
- Last Synced: 2024-10-31T11:56:36.501Z (over 1 year ago)
- Language: Rust
- Size: 20.5 KB
- Stars: 19
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# wgsl_ln
[](https://crates.io/crates/wgsl_ln)
[](https://docs.rs/wgsl_ln/latest/wgsl_ln/)
Experimental crate for writing wgsl in rust!
## The `wgsl!` macro
The `wgsl!` macro converts normal rust tokens into a wgsl `&'static str`, similar to `stringify!`.
This also validates the wgsl string using `naga`. Errors will be reported with
the correct span.
```rust
pub static MANHATTAN_DISTANCE: &str = wgsl!(
fn manhattan_distance(a: vec2, b: vec2) -> f32 {
return abs(a.x - b.x) + abs(a.y - b.y);
}
);
```
Most errors can be caught at compile time.
```rust
pub static MANHATTAN_DISTANCE: &str = wgsl!(
fn manhattan_distance(a: vec2, b: vec2) -> f32 {
// not allowed in wgsl
abs(a.x - b.x) + abs(a.y - b.y)
}
);
```
## The `#[wgsl_export(name)]` macro
Export a wgsl item (function, struct, etc)
via `wgsl_export`. Must have the same `name` as the exported item.
```rust
#[wgsl_export(manhattan_distance)]
pub static MANHATTAN_DISTANCE: &str = wgsl!(
fn manhattan_distance(a: vec2, b: vec2) -> f32 {
return abs(a.x - b.x) + abs(a.y - b.y);
}
);
```
## Using an exported item
```rust
pub static MANHATTAN_DISTANCE_TIMES_FIVE: &str = wgsl!(
fn manhattan_distance_times_five(a: vec2, b: vec2) -> f32 {
return #manhattan_distance(a, b) * 5.0;
}
);
```
`#manhattan_distance` copies the `manhattan_distance` function into the module,
making it usable. You can specify multiple instances of `#manhattan_distance`
or omit the `#` in later usages.
## Ok what's actually going on?
`wgsl_export` creates a `macro_rules!` macro that pastes itself into the `wgsl!` macro.
The macro is `#[doc(hidden)]` and available in the crate root,
i.e. `crate::__wgsl_paste_manhattan_distance!`.
You don't need to import anything to use items defined in your crate, for other crates,
you might want to blanket import the crate root.
```rust
mod my_shaders {
pub use external_shader_defs::*;
pub static MAGIC: &str = wgsl!(
fn magic() -> f32 {
return #magic_number();
}
)
}
pub use my_shaders::MAGIC;
```
## `naga_oil` support
Enable the `naga_oil` feature to enable limited `naga_oil` support:
* Treat `#preprocessor_macro_name` as tokens instead of imports.
* `#define_import_path`
* `#import`
* `#if`
* `#ifdef`
* `#ifndef`
* `#else`
* `#endif`
These values can no longer be imported.
* Checks will be disabled when naga_oil preprocessor macros are detected.
## License
License under either of
Apache License, Version 2.0 (LICENSE-APACHE or )
MIT license (LICENSE-MIT or )
at your option.
## Contribution
Contributions are welcome!
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.