tycho_shader

    GLSL (glslangValidator) and Slang shader compilation, pure-MoonBit SPIR-V reflection, and PPM/BMP image loading helpers for Tycho

    vulkan
    shader
    spirv
    glslang
    slang
    tycho
    Download zip
    Version
    0.9.0
    License
    Apache-2.0
    Last updated
    3 days ago
    Downloads
    87

    No dependencies

    #laplaceliu/tycho_shader

    Optional shader compilation and asset loading helpers for the Tycho graphics stack:

    • GLSL → SPIR-V compilation by invoking glslangValidator (compile_shader, compile_shader_file, compile_shader_env, compile_shader_file_env), with ShaderStage / ShaderTarget enums instead of raw UInt32 magic numbers
    • Slang → SPIR-V compilation by invoking slangc (compile_slang, compile_slang_file)
    • *_checked variants raise ShaderError (CompileFailed(stage, diagnostics) / SlangFailed(entry, diagnostics)): the C shim keeps the last tool output, so last_error() and the raised payload carry the real compiler message instead of an empty array
    • Binary .spv file loader (load_spirv)
    • Pure-MoonBit SPIR-V bytecode reflector (reflect_spirv — extracts stage, vertex inputs, descriptor bindings, push constant size, MRT output count with zero external C deps)
    • PPM (P6) / BMP image decoding to RGBA8 (load_image)
    • PNG encoding of RGBA8 readback buffers (save_png); rows are filtered per row (None/Sub/Up/Average/Paeth, chosen by minimum absolute sum) and compressed with a real deflate stream, so a 480x320 screenshot is a few KB instead of w*h*4 + ~400 bytes. bgra=true swaps the red/blue channels first, which Vulkan swapchain readbacks on Windows need
    • Radiance .hdr decoding to linear Float RGB pixels (load_hdr_image)

    Zero C link dependencies: compilers are resolved from TYCHO_GLSLANG / TYCHO_SLANGC, beside the executable, or on PATH; PNG encoding uses bundled stb_image_write.h (public domain, compiled into the shim), while general image decoding lives in pure MoonBit inside laplaceliu/tycho_image. Decoupled from laplaceliu/tycho_vulkan, so core Vulkan users need neither libshaderc-dev nor libstb-dev.

    Runtime tools (only when you actually compile at runtime): glslangValidator (glslang-tools on Debian/Ubuntu) and, for Slang, slangc.

    ShaderError

    pub suberror ShaderError {
    CompileFailed(ShaderStage, String)
    SlangFailed(String, String)
    LoadFailed(String, String)
    }

    Shader compilation failure. diagnostics carries the compiler output, so the caller sees the real error instead of an empty SPIR-V array.

    ReflectedBinding

    pub(all) struct ReflectedBinding {
    set : Int
    binding : Int
    kind : ReflectedDescriptorKind
    count : Int
    name : String
    } derive(Eq,
    Debug
    )

    ReflectedDescriptorKind

    pub(all) enum ReflectedDescriptorKind {
    UniformBuffer
    StorageBuffer
    CombinedImageSampler
    StorageImage
    SampledImage
    Sampler
    AccelerationStructure
    } derive(Eq,
    Debug
    )

    ReflectedStage

    pub(all) enum ReflectedStage {
    Vertex
    Fragment
    Compute
    Unknown
    } derive(Eq,
    Debug
    )

    ReflectedVertexFormat

    pub(all) enum ReflectedVertexFormat {
    Float1
    Float2
    Float3
    Float4
    Int1
    Uint1
    Unknown
    } derive(Eq,
    Debug
    )

    ReflectedVertexInput

    pub(all) struct ReflectedVertexInput {
    location : Int
    format : ReflectedVertexFormat
    name : String
    } derive(Eq,
    Debug
    )

    ShaderStage

    pub(all) enum ShaderStage {
    Vertex
    Fragment
    Compute
    Task
    Mesh
    RayGen
    AnyHit
    ClosestHit
    Miss
    Intersection
    Callable
    } derive(Eq,
    Debug
    )

    Shader stage identity. code() / from_code() bridge the integer argument glslangValidator -S expects; the enum keeps call sites self-describing and the compiler from accepting an unrelated number.

    ShaderStage::code

    fn ShaderStage::code(self : ShaderStage) -> UInt

    ShaderStage::from_code

    fn ShaderStage::from_code(code : UInt) -> ShaderStage

    ShaderStage::name

    fn ShaderStage::name(self : ShaderStage) -> String

    ShaderTarget

    pub(all) enum ShaderTarget {
    Vulkan10
    Vulkan11
    Vulkan12
    Vulkan13
    } derive(Eq,
    Debug
    )

    --target-env selection for glslangValidator.

    ShaderTarget::code

    fn ShaderTarget::code(self : ShaderTarget) -> UInt

    ShaderTarget::from_code

    fn ShaderTarget::from_code(code : UInt) -> ShaderTarget

    SpirvReflection

    pub(all) struct SpirvReflection {
    stage : ReflectedStage
    entry_point : String
    vertex_inputs : Array[ReflectedVertexInput]
    bindings : Array[ReflectedBinding]
    push_constant_size : Int
    color_output_count : Int
    local_size : (Int, Int, Int)
    }

    compile_shader

    fn compile_shader(source : String, stage : ShaderStage) -> FixedArray[UInt]

    compile_shader_checked

    fn compile_shader_checked(source : String, stage : ShaderStage) -> FixedArray[UInt] raise ShaderError

    compile_shader that raises ShaderError::CompileFailed with the compiler diagnostics instead of returning an empty array.

    compile_shader_env

    fn compile_shader_env(source : String, stage : ShaderStage, target : ShaderTarget) -> FixedArray[UInt]

    compile_shader_file

    fn compile_shader_file(path : String, stage : ShaderStage) -> FixedArray[UInt]

    compile_shader_file_checked

    fn compile_shader_file_checked(path : String, stage : ShaderStage) -> FixedArray[UInt] raise ShaderError

    compile_shader_file_env

    fn compile_shader_file_env(path : String, stage : ShaderStage, target : ShaderTarget) -> FixedArray[UInt]

    compile_shader_file_env_checked

    fn compile_shader_file_env_checked(path : String, stage : ShaderStage, target : ShaderTarget) -> FixedArray[UInt] raise ShaderError

    compile_slang

    fn compile_slang(source : String, entry : String) -> FixedArray[UInt]

    compile_slang_checked

    fn compile_slang_checked(source : String, entry : String) -> FixedArray[UInt] raise ShaderError

    compile_slang_file

    fn compile_slang_file(path : String, entry : String) -> FixedArray[UInt]

    last_error

    fn last_error() -> String

    Compiler output from the most recent failed compile (empty on success).

    load_hdr_image

    fn load_hdr_image(path : String) -> (FixedArray[Float], Int, Int)

    load_image

    fn load_image(path : String) -> (FixedArray[Byte], Int, Int)

    load_spirv

    fn load_spirv(path : String) -> FixedArray[UInt]

    Load a .spv file. Returns an empty array on failure; use load_spirv_checked to get a reason instead.

    load_spirv_checked

    fn load_spirv_checked(path : String) -> FixedArray[UInt] raise ShaderError

    Load a .spv file, raising instead of silently returning no code.

    read_file_bytes

    fn read_file_bytes(path : String) -> Bytes

    reflect_spirv

    fn reflect_spirv(spv : FixedArray[UInt]) -> SpirvReflection

    save_png

    fn save_png(path : String, width : Int, height : Int, rgba : FixedArray[Byte], bgra? : Bool) -> Bool