spirv-cross.cr

Crystal bindings to SPIRV-Cross (vendored) for Garnet's shader pipeline

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
  • Context owns every compiler and string made from it. #close (or the finalizer) frees them; using a compiler afterwards raises SPIRVCross::Error.
  • Compiler#set takes an Option; options for another backend raise ArgumentError.
  • #entry_points, #entry_point=, #cleansed_name, #execution_model, #resources(type), #decoration, #decorate, #name, #rename, #member_name, #member_decoration, #struct_size and #build_combined_image_samplers cover reflection and renaming.
  • SPIRV-Cross failures raise SPIRVCross::Error with 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).

Repository

spirv-cross.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:37 GMT

Languages