spirv-cross.cr
SPIRV-Cross for Crystal
require "spirv_cross" translates SPIR-V into GLSL, ESSL, MSL (Metal) and HLSL in process, through typed bindings to the SPIRV-Cross C API. LibSPIRVCross is the raw FFI; SPIRVCross wraps it without exposing native pointers.
Installation
dependencies:
spirv_cross:
github: garnet-engine/spirv-cross.cr
shards install runs make -C native, which compiles the vendored, unmodified SPIRV-Cross sources (tag vulkan-sdk-1.4.363.0, C API 0.68) into native/libspirv-cross.a. It needs a C++17 compiler, make and ar. On Windows, run it from a Visual Studio developer environment with GNU make and Git Bash: the sources are compiled with MSVC (cl, dynamic CRT) into native/spirv-cross.lib. CI builds and tests macOS, Linux and Windows. See native/UPSTREAM for provenance.
Usage
require "spirv_cross"
context = SPIRVCross::Context.new
compiler = context.compiler(spirv, :msl)
compiler.set(:msl_version, SPIRVCross.msl_version(2, 1))
compiler.set(:msl_platform, SPIRVCross::MSLPlatform::MacOS)
compiler.set(:msl_enable_decoration_binding, true) # Metal indices = SPIR-V bindings
metal = compiler.compile
gl = context.compiler(spirv, :glsl)
gl.set(:glsl_version, 330).set(:glsl_es, false).set(:fixup_depth_convention, true)
gl.resources(:stage_output).each { |output| gl.rename(output.id, "out#{gl.decoration(output.id, :location)}") }
glsl = gl.compile
Contextowns every compiler and string made from it.#close(or the finalizer) frees them; using a compiler afterwards raisesSPIRVCross::Error.Compiler#settakes anOption; options for another backend raiseArgumentError.#entry_points,#entry_point=,#cleansed_name,#execution_model,#resources(type),#decoration,#decorate,#name,#rename,#member_name,#member_decoration,#struct_sizeand#build_combined_image_samplerscover reflection and renaming.- SPIRV-Cross failures raise
SPIRVCross::Errorwith the library's message.
Contexts and compilers are not thread-safe.
Development
make -C native
crystal spec # with the workspace CRYSTAL_PATH, or after shards install
crystal tool format --check src spec
ameba
The specs build their SPIR-V with spirv.cr's assembler (a development dependency), so no shader compiler is needed.
License
The Crystal bindings are MIT (LICENSE). SPIRV-Cross is Apache-2.0 (native/vendor/LICENSE).
spirv-cross.cr
- 0
- 0
- 0
- 1
- 1
- 2 days ago
- December 1, 2026
MIT License
Thu, 03 Dec 2026 12:16:37 GMT