spirv.cr

Pure-Crystal SPIR-V reader, reflection and assembler for Garnet

SPIR-V for Crystal

require "spirv" reads SPIR-V binary modules in pure Crystal, with no native dependency. It decodes the instruction stream and reflects what shader tooling needs: entry points and their interface variables (locations and built-ins), descriptor sets and bindings, and uniform block layouts with member names, offsets, sizes and strides. SPIRV::Assembler writes modules word by word, which is handy for exact test fixtures.

Installation

dependencies:
  spirv:
    github: garnet-engine/spirv.cr

Usage

require "spirv"

mod = SPIRV::Module.parse(File.open("mesh3d.frag.spv", "rb", &.getb_to_end))
entry = mod.entry_points.first           # name, execution model, interface IDs
mod.inputs(entry).map(&.location)        # => [0, 1, 2, nil, nil] (built-ins have no location)
draw = mod.resources.find!(&.name.==("draw"))
draw.kind                                # => SPIRV::Resource::Kind::UniformBlock
draw.binding                             # => 2
block = mod.block(draw.type.id)
block.size                               # => 12272
block.members.first.offset               # => 0
  • Module.parse(bytes) raises SPIRV::Error for a bad header, a zero word count or an instruction that runs past the end.
  • #inputs/#outputs(entry) order variables by location; built-ins follow, with #builtin set.
  • #resources covers uniform and storage blocks, push constants, images, samplers and combined image-samplers, ordered by set and binding.
  • #block(type_id) computes member sizes from Offset, ArrayStride, MatrixStride and RowMajor/ColMajor, including nested structs.
  • #type_name(type) gives readable names such as float4x4, float4[23] and sampler2D.
  • #name, #member_name, #decoration, #member_decoration and #constant expose the underlying tables; #instructions keeps every instruction.

The reader trusts the module's structure beyond those checks. Validate untrusted SPIR-V before relying on its semantics.

Development

crystal spec
crystal tool format --check src spec
ameba

The specs assemble their own modules, so they need no shader compiler.

License

MIT. See LICENSE.

Repository

spirv.cr

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

MIT License

Links
Synced at

Thu, 03 Dec 2026 12:16:36 GMT

Languages