Shades, a shader EDSL in Rust

This crate provides an [EDSL] to build [shaders], leveraging the Rust compiler (rustc) and its type system to ensure soundness and typing. Because shaders are written in Rust, this crate is completely language agnostic: it can in theory target any shading language – the current tier-1 language being [GLSL]. The EDSL allows to statically type shaders while still generating the actual shading code at runtime.

Motivation

In typical graphics libraries and engines, shaders are opaque strings – either hard-coded in the program, read from a file at runtime, constructed via fragments of strings concatenated with each others, etc. The strings are passed to the graphics drivers, which will compile and link the code at runtime. It is the responsibility of the runtime (i.e. the graphics library, engine or the application) to check for errors and react correctly. Shading languages can also be compiled off-line, and their bytecode is then used at runtime (c.f. SPIR-V).

For a lot of people, this has proven okay for decades and even allowed live coding: because the shading code is loaded at runtime, it is possible to re-load, re-compile and re-link it every time a change happens. However, this comes with a non-negligible drawbacks:

The author ([@phaazon]) of this crate thinks that shading code is still code, and that it should be treated as such. It’s easy to see the power of live-coding / reloading, but it’s more important to provide a shading code that is statically proven sound and with less bugs that without the static check. Also, as stated above, using a compiled approach doesn’t prevent from writing a relocatable object, compiled isolated and reload this object, providing roughly the same functionality as live-coding.

Important note: this crate does its best to catch semantic bugs at compile-time via rustc. However, it might still lack opportunities to catch all semantic bugs. If you find such a case, please feel free to open an issue to as that is considered a bug / regression.

Another important point is the choice of using an EDSL. Some people would argue that Rust has other interesting and powerful ways to achieve the same goal. It is important to notice that this crate doesn’t provide a compiler to compile Rust code to a shading language. Instead, it provides a Rust crate that will still generate the shading code at runtime. Other alternatives would be using a [proc-macro]. Several crates who do this:

Influences

Why you would love this

If you like type systems, languages and basically hacking compilers (writing code for your compiler to generate the runtime code!), then it’s likely you’ll like this crate. Among all the features you will find:

Why you wouldn’t love this

The crate is, as of nowadays, still very experimental. Here’s a list of things you might dislike about the crate: