This crate provides a library for performing similar actions to what is expected from a preprocessor in WGSL. Since WGSL will not have a preprocessor at least for version 1.0, this crate provides solutions to some common problems like including shader files and defining constants from Rust code.
Here are the contents of the three shader files in this example (there are blank lines at the end of each included file):
test_shaders/main.wgsl
:
wgsl
//!include test_shaders/included.wgsl test_shaders/included2.wgsl
test_shaders/included.wgsl
:
```wgsl
struct TestStruct {
test_data: vec4
`test_shaders/included2.wgsl`:
wgsl
struct AnotherTestStruct {
anothertestdata: vec3
After building `main.wgsl`, it's compiled contents would be identical to:
wgsl
struct TestStruct {
testdata: vec4
It is important to note that `test_shaders/main.wgsl` could also contain:
wgsl
//!include testshaders/included.wgsl
//!include testshaders/included2.wgsl
```
The result would be the same.
Let's say some color constants are calculated before shader compile time and should be injected into the
code for performance reasons.
main.wgsl
would contain:
wgsl
//!define STRUCT_ARRAY
In the Rust code, Struct
is defined, and given an implementation of [WGSLType
] it can be translated to
a WGSL struct with a single vec4<f32>
member named data
.
The Rust code building and compiling the shaders will contain:
```rust
struct Struct {
pub data: [f32; 4],
}
impl WGSLType for Struct {
fn typename() -> String {
"Struct".tostring()
}
fn string_definition(&self) -> String {
format!("{}(vec4<f32>({:?}))", Self::type_name(), self.data)
.replace(&['[', ']'], "")
}
}
After building and compiling `main.wgsl` with the following array definition:
rust
ShaderBuilder::new("main.wgsl")
.unwrap()
.putarraydefinition(
"STRUCTARRAY",
&vec![
&Struct {
data: [1.0, 2.0, 3.0, 4.0]
},
&Struct {
data: [1.5, 2.1, 3.7, 4.9]
}
]
)
.build()
The compiled contents would be identical to:
wgsl
var
By default, none of the following features are enabled.
* arrayvectors -
When enabled, implementations of [WGSLType
] are compiled for all array types of suitable lengths and scalar types.
This feature forces the translation of (for example) [f32; 4]
to the WGSL type vec4<f32>
in methods like [ShaderBuilder::put_array_definition
].
* cgmathvectors -
This feature is similar to array_vectors but with [cgmath
] vector objects like [cgmath::Vector3<u32>
]
which would be translated to vec3<u32>
.