top | item 43669562

(no title)

AndrewPGameDev | 10 months ago

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.

Let me start with the pros:

1. Don't have to learn 2 different languages

2. Modules, crates, and the easier ability to share code

3. Easier sharing between rust structs and shader code.

Now the cons, in comparison to Slang [1]

1. No autodiff mode 2. Strictly outputs SPIR-V, while Slang can do CPU, CUDA, Pytorch, Optix, and all the major graphics APIs

3. Less support - Slang is supported by the Khronos group, and Slang gets use at Nvidia, EA, and Valve.

4. Safety isn't very valuable, most GPU code does not use pointers (it's so rare it's considered a feature by Slang!)

5. slangc probably runs a lot faster than rustc (although I would like to see a benchmark.)

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.

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.

8. If you have pre-existing shaders in GLSL or HLSL, you can use slangc directly on them. No need to rewrite.

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.

[1] https://shader-slang.org/ [2] https://webgpufundamentals.org/webgpu/lessons/resources/wgsl... WGSL struct alignment widget [3] https://github.com/teoxoy/encase

discuss

order

wyager|10 months ago

Had not heard of Slang, thanks for sharing.

It's interesting that Slang looks more like Rust than WGSL does, despite WGSL kind of being de facto "owned by" the Rust community.

pcwalton|10 months ago

Not sure why you think WGSL is owned by the Rust community--it's clearly owned by the W3C. (That's part of why it moves so slowly.) WESL [1] is the community-owned set of extensions to WGSL that, while incomplete, promises to move it much closer to Rust.

[1]: https://github.com/wgsl-tooling-wg/wesl-spec

pjmlp|10 months ago

Not sure where you get that from, Slang is an evolution of HLSL with a bit of C# sprinkled on top.

Actually this is a divergence point with HLSL202x evolution, which aims to be more C++ like.

WGSL is the only shading language with Rust influence.

TinkersW|10 months ago

Slang is nice improvement on HLSL, but wish it was more like C++ and not C#(easier to share code).

LegNeato|10 months ago

I have a WIP naga backend that converts glsl and hlsl to rust-gpu shaders.