I've spent a little time in this space, and I'm not sure it's a good idea to write shaders in Rust, although it's probably better than GLSL or WGSL.<p>Let me start with the pros:<p>1. Don't have to learn 2 different languages<p>2. Modules, crates, and the easier ability to share code<p>3. Easier sharing between rust structs and shader code.<p>Now the cons, in comparison to Slang [1]<p>1. No autodiff mode
2. Strictly outputs SPIR-V, while Slang can do CPU, CUDA, Pytorch, Optix, and all the major graphics APIs<p>3. Less support - Slang is supported by the Khronos group, and Slang gets use at Nvidia, EA, and Valve.<p>4. Safety isn't very valuable, most GPU code does not use pointers (it's so rare it's considered a feature by Slang!)<p>5. slangc probably runs a lot faster than rustc (although I would like to see a benchmark.)<p>6. Worse debugging experience, slang has better interop with things like NSight Graphics, and their Shader Debugger. Slang recently got support in NSight graphics for shader profiling, for example.<p>7. Slang has support for reflection, and has a C++ api to directly output a JSON file that contains all the reflected aspects.This makes handling the movement between rust <-> gpu much easier. Also, the example shown on the website uses `bytemuck`, but `bytemuck` won't take into consideration the struct alignment rules[2] when using WebGPU. Instead, you have to use a crate like `encase`[3] to handle that. I'm not sure given the example on the website how it would work with WebGPU.<p>8. If you have pre-existing shaders in GLSL or HLSL, you can use slangc directly on them. No need to rewrite.<p>9. In reality, you may not have to learn 2 languages but you have to learn 2 different compute models (CPU vs GPU). This is actually a much harder issue, and AFAICT it is impossible to overcome with a different language. The problem is the programmer needs to understand how the platforms are different.<p>[1] <a href="https://shader-slang.org/" rel="nofollow">https://shader-slang.org/</a>
[2] <a href="https://webgpufundamentals.org/webgpu/lessons/resources/wgsl-offset-computer.html#" rel="nofollow">https://webgpufundamentals.org/webgpu/lessons/resources/wgsl...</a> WGSL struct alignment widget
[3] <a href="https://github.com/teoxoy/encase">https://github.com/teoxoy/encase</a>