Skip to content

Can WGSL share some logic with Rust #5950

Answered by kpreid
songhuaixu asked this question in Q&A
Discussion options

You must be logged in to vote

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 include_str!("shared.wgsl") as usual to load it as a shader, then to load it as Rust, use include! in a mo…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by songhuaixu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants