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.
This library uses build.rs
to 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
.
To turn off this feature, specify --no-default-features
when building.
But then you will need to place a copy of the shaderc_combined
library
(on Windows) or the shaderc_shared
library (on Linux and macOS) to a location
that is scanned by the linker (e.g., the deps
directory within the target
directory).
First add to your Cargo.toml
:
toml
[dependencies]
shaderc = "0.3"
Then add to your crate root:
rust
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
CompilationArtifact
s.
Please see
for detailed documentation.
Compile a shader into SPIR-V binary module and assembly text:
```rust 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.addmacrodefinition("EP", Some("main")); let binaryresult = compiler.compileinto_spirv( source, shaderc::ShaderKind::Vertex, "shader.glsl", "main", Some(&options)).unwrap();
asserteq!(Some(&0x07230203), binaryresult.as_binary().first());
let textresult = compiler.compileintospirvassembly( source, shaderc::ShaderKind::Vertex, "shader.glsl", "main", Some(&options)).unwrap();
assert!(textresult.astext().starts_with("; SPIR-V\n")); ```
To build the shaderc-rs crate, the following tools must be installed and available on PATH
:
- CMake
- Python (works with both Python 2.x and 3.x)
- a C++11 compiler
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.
rustup default stable-x86_64-pc-windows-msvc
pacman --noconfirm -Syu mingw-w64-x86_64-cmake mingw-w64-x86_64-python3
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.
rustup default stable-x86_64-pc-windows-msvc
rustup target install x86_64-pc-windows-gnu
pacman --noconfirm -Syu mingw-w64-x86_64-cmake mingw-w64-x86_64-make mingw-w64-x86_64-python3
--target x86_64-pc-windows-gnu
e.g. to run: cargo run --target x86_64-pc-windows-gnu
Use your package manager to install the required dev-tools
For example on ubuntu:
sudo apt-get install build-essential git python cmake
Assuming Homebrew:
brew install cmake
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).