Can WGSL share some logic with Rust #5950
-
Can WGSL share some logic with Rust |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Unfortunately, there are not currently any tools (that I am aware of) which will do that on a straightforward basis. rust-gpu is notable in this space, but quite heavyweight (it's an entire new backend for the Rust compiler). However, Rust and WGSL do have a common subset of syntax, so you could share some code simply as text, with some setup. For example, fn luminance(linear_rgb: vec3f) -> f32 {
return dot(linear_rgb, vec3f(0.2126, 0.7152, 0.0722));
} is syntactically valid as both WGSL and Rust, so if you accompany it with suitable Rust item declarations, it will run. You'd use mod from_shader {
type vec3f = [f32; 3]; // or your choice of vector library type
fn vec3f(a: f32, b: f32, c: f32) -> vec3f { [a, b, c] }
fn dot(a: vec3f, b: vec3f) -> f32 {
a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}
include!("shared.wgsl");
// write `pub fn`s that use the included fns here
} The most annoying part of this will be that |
Beta Was this translation helpful? Give feedback.
Unfortunately, there are not currently any tools (that I am aware of) which will do that on a straightforward basis. rust-gpu is notable in this space, but quite heavyweight (it's an entire new backend for the Rust compiler).
However, Rust and WGSL do have a common subset of syntax, so you could share some code simply as text, with some setup. For example,
is syntactically valid as both WGSL and Rust, so if you accompany it with suitable Rust item declarations, it will run. You'd use
include_str!("shared.wgsl")
as usual to load it as a shader, then to load it as Rust, useinclude!
in a mo…