top | item 42244472

The Slang Shading Language

142 points| lmariscal | 1 year ago |shader-slang.com

44 comments

order

bartwr|1 year ago

One unique and new feature of Slang that sets it apart from existing shading languages is support for differentiation and gradient computation/propagation - while still cross-compiling generated forward and backward passes to other, platform-specific shading languages. Before, the only way to backpropagate through shader code (such as material BRDF or lighting computation) was to either manually differentiate every function and chain them together, or rewrite it in another language or framework - such as PyTorch or a specialized language/framework like as Dr.Jit, and keeping both versions in sync after any changes. Game developers typically don't use those, programming models are different (SIMT kernels vs array computations), it's a maintenance headache, and it was a significant blocker for a wider adoption of data-driven techniques and ML in existing renderers or game engines.

mpartel|1 year ago

The design of generics in this one seems rather well balanced in simplicity vs power.

The part about interface-typed values [1] is interesting. They do dynamic dispatch as a fallback when the function is too polymorphic to be be specialized.

In Rust terms it's as if it picks between `dyn` and `impl` automatically. It looks convenient, but also a bit of a non-obvious performance pitfall.

[1] https://shader-slang.com/slang/user-guide/interfaces-generic...

hajile|1 year ago

It seems like the type checker could call out polymorphic functions both when compiling and even highlighting them in the editor when checking for type errors.

Conscat|1 year ago

I've been very impressed by Slang as an open source project. The development team is quick to respond to issues and review pull requests. It's clear from their repository and their presentations that they take Slang seriously as a user-facing project, not a research demo.

pjmlp|1 year ago

Several commercial products are already using Slang, it has moved beyond research demo for quite some time now.

The main difference, with something like Cg, is that now it isn't NVidia only.

machinekob|1 year ago

It isn't weird it started inside NV and folks from there are still working on the project.

coffeeaddict1|1 year ago

I hope this becomes the standard way of writing shaders across all platforms. There is no compelling reason for having to use WGSL, HLSL, GLSL or Metal C++. Having one single feature-complete language is a huge QoL improvement.

incrudible|1 year ago

Nobody is using all these languages at the same time, anyone who targets all these platforms already has some mechanism to turn HLSL or GLSL (or whatever) into what the target platform needs. That's why Slang will have difficulty finding adoption, it's the N+1th solution to a problem that already has been solved N times.

mistercow|1 year ago

I think there is a compelling reason to have WGSL as a separate language (but maybe not sufficiently compelling): WebGPU is a lowest common denominator which can only have features which are in the intersection of its target platforms, and so WGSL has no “dead syntax” to memorize. If the language spec says it’s there, you can use it, and you won’t find examples online of code that just won’t work in WebGPU because of missing features.

Of course, the f16 extension immediately weakens that, and demonstrates that this situation probably won’t last.

CyberDildonics|1 year ago

What is missing from GLSL?

Cotterzz|1 year ago

I have to wonder how this fits in with other shader technologies that seem to have overlapping purposes. (There's Khronos' own Spir-V, SDL's SDLSL, Three's TSL, and WebGPU's WGSL) The reflection and automatic differentiation look interesting, I just wish I understood better what problems they're trying to solve. I actually think WebGPU/WGSL will and should become ubiquitous, though it's a shame it's not more C/GLSL-like, Slang may yet be a missing part of that solution. I wonder if this is Khronos trying to fix the shader lang debacle with Apple that led to WGSL being what it is? Thoughts?

flohofwoe|1 year ago

IMHO Slang fixes two problems in one tool:

- the frontend language is mostly compatible with HLSL, which is the defacto shader authoring standard in the gamedev industry

- it compiles to the 'shader language/bytecode zoo' of the different 3D APIs (since recently even WGSL/WebGPU)

So it lets you author your shaders in a single language that's already accepted by most of the industry, and target the various 3D APIs (D3D, Metal, GL, WebGPU, ...).

It is already possible to build such a tool yourself by glueing together existing libraries (like glslang, SPIRVTools, SPIRVCross and Tint), but it requires quite some work and there are little annoying details like SPIRVCross never supporting WGSL output for some non-technical reasons (so that you need a separate library like Tint just for the SPIRV-to-WGSL conversion). In fact there are tons of such engine-specific shader cross-compilers, all built around the same few Khronos libraries and doing more or less the same thing. Slang looks like it could be the standard replacement for all those custom solutions.

For my own needs, the main thing I'll need to investigate is how easy it is to add engine-specific custom annotations to Slang resources and then output those as part of the reflection information.

jacobgorm|1 year ago

Will Slang support compute shaders too, or is it just for graphics.

bartwr|1 year ago

It does! Both platform-specific compute shaders as well as cross-compilation to CUDA. The authors even provide some basic PyTorch bindings to help use existing shader code for gradient computation and backpropagation in ML and differentiable programming of graphics-adjacent tasks: https://github.com/shader-slang/slang-torch (Disclaimer: this is the work of my colleagues, and I helped test-drive differentiable Slang and wrote one of the example applications/use-cases)

Cotterzz|1 year ago

It's compute also, though that may depend on the target language.

koolala|1 year ago

I wish it supported WGSL. WebGPU seems 5 years away from being standard on devices and browsers.

LegNeato|1 year ago

Also check out Rust GPU, which has similar features and uses Rust rather than a new language:

https://github.com/Rust-GPU/rust-gpu

Cotterzz|1 year ago

Don't we already have a shader language with Rust-syntax in WGSL? It also works everywhere, given that WebGPU works wherever Vulkan, Metal & DirectX do...

ImHereToVote|1 year ago

Will it provide unified screen space derivatives?

darkest_ruby|1 year ago

Slang is GS internal database language

kseistrup|1 year ago

[deleted]

flohofwoe|1 year ago

...which is only a problem on Linux though (turns out that system-wide shared locations for headers and linker libraries are not a great idea). Most Slang users will probably be on Windows, and apart from that there's also most likely not much overlap with S-Lang users.

otteromkram|1 year ago

Will the real Slang Shady please compile up?

We're gonna have a problem here...