This is not an official Google product (experimental or otherwise), it is just
code that happens to be owned by Google.
Usage
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:
[dependencies]
shaderc = "0.10"
Then add to your crate root:
extern crate shaderc;
Documentation
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
for detailed documentation.
Example
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"));
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:
Building from source, if option --features build-from-source is specified.
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.
If the VULKAN_SDK environment variable is set, then $VULKAN_SDK/lib will
be searched for native dynamic or static shaderc library.
If pkg-config is available, use it to find the path to search for libraries.
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.
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/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:
Python (only works with Python 3, on Windows
the executable must be named python.exe)
a C++11 compiler
Additionally:
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.
shaderc-rs
Rust bindings for the shaderc library.
Disclaimer
This is not an official Google product (experimental or otherwise), it is just code that happens to be owned by Google.
Usage
The included shaderc-sys crate uses
build.rsto discover or build a copy of shaderc libraries. See Setup section.First add to your
Cargo.toml:Then add to your crate root:
Documentation
shaderc provides the
Compilerinterface 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 usingCompileOptions. Successful results are kept inCompilationArtifacts.Please see
for detailed documentation.
Example
Compile a shader into SPIR-V binary module and assembly text:
Setup
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:
--features build-from-sourceis specified.SHADERC_LIB_DIRenvironment variable is set to/path/to/shaderc/libs/, that path will be searched for native dynamic or static shaderc library.VULKAN_SDKenvironment variable is set, then$VULKAN_SDK/libwill be searched for native dynamic or static shaderc library.pkg-configis available, use it to find the path to search for libraries./usr/lib/will additionally be searched for native dynamic or shaderc library, if theSHADERC_LIB_DIRis not set.For each library directory, the build script will try to find and link to the dynamic native shaderc library
shaderc/shaderc_sharedfirst and the static native shaderc libraryshaderc_combinednext. To prefer searching for the static library first and the dynamic library next, the option--features prefer-static-linkingmay be used.Building from Source
The shaderc-sys
build.rswill automatically check out and compile a copy of native C++ shaderc and link to the generated artifacts, which requiresgit,cmake, andpythonexisting in thePATH:python.exe)Additionally:
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
rustup default stable-x86_64-pc-windows-msvcPATHenvironment variable.windows-gnu Example Setup
windows-gnu toolchain is not supported but you can instead cross-compile to windows-gnu from windows-msvc.
rustup default stable-x86_64-pc-windows-msvcrustup target install x86_64-pc-windows-gnupacman --noconfirm -Syu mingw-w64-x86_64-cmake mingw-w64-x86_64-make mingw-w64-x86_64-python3 mingw-w64-x86_64-ninja--target x86_64-pc-windows-gnue.g. to run:cargo run --target x86_64-pc-windows-gnuLinux Example Setup
Use your package manager to install the required dev-tools
For example on ubuntu:
On Arch linux, you can directly install the shaderc package.
macOS Example Setup
Assuming Homebrew:
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).