1 unstable release
0.1.0 | Feb 7, 2021 |
---|
#1068 in Graphics APIs
24 stars & 3 watchers
27KB
661 lines
ShaderRoy
ShaderToy clone in Rust, currently supporting MacOS.
Features
cargo run <rust project dir>
displays a single macOS window filled with a Metal framework fragment shader.- You can edit and save the Rust project source code (in VS code or any other editor) to change the fragment shader output and the window will update in real time.
- You write the shader in Rust but it is compiled to Metal Shading Language (a variation of C++)
- In the shader source you can reference the
const
INPUT
struct which provides inputs for each frame, similarly to Input Uniforms in ShaderToy. You don't need to thread these values through your functions as arguments, despite Metal having no concept of global uniforms like WebGL does. - You can split the shader across multiple files using
mod <name>
anduse <name>::*
. - You can pause, restart and even run another shader file from the command line while the window is open.
Instructions
- clone this repo
- run
cargo run raymarching_eyes
- edit
examples/raymarching_eyes.rs
Why Rust for the shader source? Better syntax, better editor integration and because it's a fun hack. It should feel exactly like writing Rust (which feels awesome!). Unlike in ShaderToy the Rust typechecker warns immediately about most errors one might make.
Metal Shading Rust Language
In general you will write Rust that closely resembles the C++ Metal Shading Language API, except for a few differences.
The INPUT
struct is documented in shader_roy_metal_sl_interface.
API | MSL (C++) | Rust |
Scalar Types |
|
Use standard Rust types that correspond to the Metal types.
|
Vector Types |
|
Use the generic
|
Constructors |
vector constructors (
|
In Rust, these follow the type names. You need to call these as methods.
|
Access Constructors |
vector component selection constructors (
|
In Rust you need to call these as methods:
|
Functions |
|
Math, geometric and common functions need to be called as methods
|
Renamed functions |
|
Names follow vek
|
Methods with different argument order |
|
When one argument is special from the others it is used as the receiver of the method call.
|
Limitations
Modules
Right now only modules in the same directory as the main file will be watched.
Only files in the same directory are supported for mod <name>
s, <name>/mod.rs
is not supported.
There is no support for path
attribute on mod
s.
Let bindings
Variables cannot be redeclared. (for now)
Constructors
In Rust we could use a single generic vec2
constructor for all Vec2
s. But this would require actually using rustc
to compile the constructors to the concrete Metal C++ constructors. To keep things simpler, the Rust bindings here require specifying the constructors directly (vec2bool
-> bool2
).
Development
Print the compiled shader without opening the window:
cargo test -- --nocapture
Dependencies
~5–14MB
~169K SLoC