#shader #compiler #spirv #glsl #hlsl

sys shaderc-sys

Shaderc system library FFI, building, and linking

15 releases

Uses old Rust 2015

0.8.3 Nov 30, 2023
0.8.2 Jan 20, 2023
0.8.1 Dec 10, 2022
0.8.0 Apr 14, 2022
0.4.0 Mar 30, 2019

#696 in Graphics APIs

Download history 4595/week @ 2023-11-01 4353/week @ 2023-11-08 4337/week @ 2023-11-15 3843/week @ 2023-11-22 3690/week @ 2023-11-29 3403/week @ 2023-12-06 4474/week @ 2023-12-13 4032/week @ 2023-12-20 2739/week @ 2023-12-27 3702/week @ 2024-01-03 4583/week @ 2024-01-10 4342/week @ 2024-01-17 3739/week @ 2024-01-24 3789/week @ 2024-01-31 4786/week @ 2024-02-07 4269/week @ 2024-02-14

17,193 downloads per month
Used in shaderc


603K SLoC

C++ 478K SLoC // 0.1% comments GLSL 53K SLoC // 0.0% comments Go 30K SLoC // 0.1% comments Python 12K SLoC // 0.3% comments JavaScript 6.5K SLoC // 0.0% comments C# 4.5K SLoC // 0.0% comments Lua 4.5K SLoC // 0.0% comments M4 4K SLoC // 0.0% comments Happy 4K SLoC D 2K SLoC // 0.0% comments C 1.5K SLoC // 0.2% comments Shell 1K SLoC // 0.6% comments Rust 538 SLoC // 0.1% comments AsciiDoc 533 SLoC Forge Config 288 SLoC // 0.8% comments Bitbake 245 SLoC Batch 171 SLoC // 0.6% comments Ruby 88 SLoC // 0.1% comments HLSL 45 SLoC // 0.1% comments TypeScript 39 SLoC // 0.2% comments GNU Style Assembly 18 SLoC Emacs Lisp 9 SLoC // 0.7% comments


Version Documentation

Rust bindings for the shaderc library.


This is not an official Google product (experimental or otherwise), it is just code that happens to be owned by Google.


The included shaderc-sys crate uses build.rs to discover or build a copy of shaderc libraries. See Setup section.

First add to your Cargo.toml:

shaderc = "0.8"

Then add to your crate root:

extern crate shaderc;


shaderc 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.

Please see Documentation for detailed documentation.


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

use shaderc;

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

let mut compiler = shaderc::Compiler::new().unwrap();
let mut options = shaderc::CompileOptions::new().unwrap();
options.add_macro_definition("EP", Some("main"));
let binary_result = compiler.compile_into_spirv(
    source, shaderc::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::ShaderKind::Vertex,
    "shader.glsl", "main", Some(&options)).unwrap();

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


shaderc-rs needs the C++ shaderc library. It's shipped inside the Vulkan SDK. You may be able to install it directly on some Linux distro's using the package manager. The C++ shaderc project provides artifacts downloads. You can also build it from source.

The order of preference in which the build script attempts to obtain native shaderc can be controlled by several options, which are passed through to shaderc-sys when building shaderc-rs:

  1. Building from source, if option --features build-from-source is specified.
  2. If the SHADERC_LIB_DIR environment variable is set to /path/to/shaderc/libs/, that path will be searched for native dynamic or static shaderc library.
  3. If the VULKAN_SDK environment variable is set, then $VULKAN_SDK/lib will be searched for native dynamic or static shaderc library.
  4. On Linux, system library paths like /usr/lib/ will additionally be searched for native dynamic or shaderc library, if the SHADERC_LIB_DIR is not set.
  5. Building from source, if the native shaderc library is not found via the above steps.

For each library directory, the build script will try to find and link to the dynamic native shaderc library shaderc_shared first and the static native shaderc library shaderc_combined next. To prefer searching for the static library first and the dynamic library next, the option --features prefer-static-linking may be used.

Building from Source

The shaderc-sys build.rs will automatically check out and compile a copy of native C++ shaderc and link to the generated artifacts, which requires git, cmake, and python existing in the PATH:

  • CMake
  • Git
  • Python (only works with Python 3, on Windows the executable must be named python.exe)
  • a C++11 compiler


  • Ninja is required on windows-msvc, but optional on all other platforms.

These requirements can be either installed with your favourite package manager or with installers from the projects' websites. Below are some example ways to get setup.

windows-msvc Example Setup

  1. rustup default stable-x86_64-pc-windows-msvc
  2. Install Build Tools for Visual Studio 2017. If you have already been using this toolchain then its probably already installed.
  3. Install the necessary tools as listed in the above and add their paths to the PATH environment variable.

windows-gnu Example Setup

windows-gnu toolchain is not supported but you can instead cross-compile to windows-gnu from windows-msvc.

Steps 1 and 2 are to workaround https://github.com/rust-lang/rust/issues/49078 by using the same mingw that rust uses.

  1. Download and extract https://s3-us-west-1.amazonaws.com/rust-lang-ci2/rust-ci-mirror/x86_64-6.3.0-release-posix-seh-rt_v5-rev2.7z
  2. Add the absolute path to mingw64\bin to your PATH environment variable.
  3. Run the command: rustup default stable-x86_64-pc-windows-msvc
  4. Run the command: rustup target install x86_64-pc-windows-gnu
  5. Install Build Tools for Visual Studio 2017. If you have already been using this toolchain then its probably already installed.
  6. Install msys2, following ALL of the instructions.
  7. Then in the msys2 terminal run: pacman --noconfirm -Syu mingw-w64-x86_64-cmake mingw-w64-x86_64-make mingw-w64-x86_64-python3 mingw-w64-x86_64-ninja
  8. Add the msys2 mingw64 binary path to the PATH environment variable.
  9. Any cargo command that builds the project needs to include --target x86_64-pc-windows-gnu e.g. to run: cargo run --target x86_64-pc-windows-gnu

Linux Example Setup

Use your package manager to install the required dev-tools

For example on ubuntu:

sudo apt-get install build-essential cmake git ninja python3

On Arch linux, you can directly install the shaderc package.

macOS Example Setup

Assuming Homebrew:

brew install git cmake ninja python@3.8


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


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