slang.cr
Slang for Crystal
require "slang" compiles Slang shader modules to SPIR-V in process and returns Slang's reflection, user attributes included. A small C shim (native/shim.cpp, native/garnet_slang.h) wraps Slang's C++ COM API; LibSlang binds the shim and Slang wraps it.
Installation
dependencies:
slang:
github: garnet-engine/slang.cr
shards install runs make -C native. native/fetch.sh downloads the pinned prebuilt Slang release (native/SLANG_VERSION, currently 2026.21) for macOS (arm64, x86_64), Linux (x86_64, aarch64) or Windows (x86_64, arm64), checks its SHA-256 against the digest recorded in the script, and unpacks it into native/vendor/. The shim is then compiled against its headers. A C++17 compiler, make, curl and tar are required.
- macOS and Linux: programs link
libslang-compilerdynamically with an rpath to the release'slib/. - Windows: build from a Visual Studio developer environment (so
clandlibare on PATH) with GNU make and Git Bash; the shim is compiled with MSVC against the dynamic CRT. Windows has no rpath, so put the release'sbin/(holdingslang-compiler.dll) on PATH or beside the executable. CI runs this onwindows-latest.
Usage
require "slang"
compiler = Slang::Compiler.new # owns a Slang global session; reuse it
program = compiler.program("shaders/mesh3d.slang",
search_paths: ["shaders"], macros: {"GARNET_VULKAN" => "1"})
program.entry_points # => [Slang::EntryPoint(name: "mesh3dVertex", stage: Vertex), ...]
spirv = program.spirv("mesh3dVertex")
json = program.reflection # bindings, uniform offsets and [UserAttribute]s
Compiler#programloads, checks and links the module with every[shader(...)]entry point. Imports resolve beside the module and through search_paths.- Defaults: SPIR-V 1.3, column-major matrices (GLSL conventions) and entry point names kept in SPIR-V.
- Failures raise
Slang::Error;#diagnosticsholds Slang's messages. Warnings are inProgram#warnings. Slang::RELEASEis the pinned release;Compiler#versionreports the linked one.
Compilers and programs are not thread-safe.
Development
make -C native
crystal spec
crystal tool format --check src spec
ameba
The specs check generated SPIR-V with spirv.cr (a development dependency).
License
The Crystal bindings and shim are MIT (LICENSE). Slang is Apache-2.0 with LLVM exception; its licence and notices are unpacked with the release. See native/UPSTREAM.
slang.cr
- 0
- 0
- 0
- 1
- 1
- 2 days ago
- December 1, 2026
MIT License
Thu, 03 Dec 2026 12:16:34 GMT