slang.cr

Crystal bindings to the Slang shader compiler for Garnet

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-compiler dynamically with an rpath to the release's lib/.
  • Windows: build from a Visual Studio developer environment (so cl and lib are 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's bin/ (holding slang-compiler.dll) on PATH or beside the executable. CI runs this on windows-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#program loads, 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; #diagnostics holds Slang's messages. Warnings are in Program#warnings.
  • Slang::RELEASE is the pinned release; Compiler#version reports 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.

Repository

slang.cr

Owner
Statistic
  • 0
  • 0
  • 0
  • 1
  • 1
  • 2 days ago
  • December 1, 2026
License

MIT License

Links
Synced at

Thu, 03 Dec 2026 12:16:34 GMT

Languages