shaderc-dyn 0.10.1

Rust bindings for shaderc with runtime dynamic loading
Documentation

shaderc-dyn-rs

Rust bindings for the shaderc library.

Note: This is a fork of google/shaderc-rs that loads the shaderc shared library at runtime using libloading, instead of statically linking or building from source at compile time. A pre-built libshaderc_shared shared library is required to be available at runtime. Pls ping me if the fork is outdated.

Usage

First add to your Cargo.toml:

[dependencies]
shaderc-dyn = "0.10"

Then add to your crate root:

extern crate shaderc_dyn;

Documentation

shaderc-dyn provides the Compiler interface to compile GLSL/HLSL source code into SPIR-V binary modules or assembly code. It can also assemble SPIR-V assembly into binary module. Default compilation behavior can be adjusted using CompileOptions. Successful results are kept in CompilationArtifacts.

Example

Compile a shader into SPIR-V binary module and assembly text:

use shaderc_dyn;

let source = "#version 310 es\n void EP() {}";

let mut compiler = shaderc_dyn::Compiler::new().unwrap();
let mut options = shaderc_dyn::CompileOptions::new().unwrap();
options.add_macro_definition("EP", Some("main"));
let binary_result = compiler.compile_into_spirv(
    source, shaderc_dyn::ShaderKind::Vertex,
    "shader.glsl", "main", Some(&options)).unwrap();

assert_eq!(Some(&0x07230203), binary_result.as_binary().first());

let text_result = compiler.compile_into_spirv_assembly(
    source, shaderc_dyn::ShaderKind::Vertex,
    "shader.glsl", "main", Some(&options)).unwrap();

assert!(text_result.as_text().starts_with("; SPIR-V\n"));

Setup

This fork requires a pre-built shaderc shared library to be installed and available on the system library search path at runtime.

Required shared library

Platform Library name
Windows shaderc_shared.dll
macOS libshaderc_shared.dylib
Linux libshaderc_shared.so

Environment variable

Set SHADERC_LIB_PATH to the full path of the shared library to override automatic search:

export SHADERC_LIB_PATH=/path/to/libshaderc_shared.dylib

Installation

The shared library is included in the Vulkan SDK or can be installed via package managers:

  • macOS (Homebrew): brew install shaderc
  • Ubuntu/Debian: sudo apt install libshaderc-dev
  • Arch Linux: pacman -S shaderc
  • Windows: Install the Vulkan SDK and ensure the SDK's Bin directory is on PATH

Contributions

This project is licensed under the Apache 2 license. Please see CONTRIBUTING before contributing.

Authors

This project is initialized and mainly developed by Lei Zhang (@antiagainst).