Crate shaderc

source ·
Expand description

Rust binding for the shaderc library.

The shaderc library provides an API for compiling GLSL/HLSL source code to SPIRV modules. It has been shipping in the Android NDK since version r12b.

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 (libshaderc_combined.a on Unix like systems and shaderc_combined.lib on Windows) to a location that is scanned by the linker (e.g., the deps directory within the target directory).

Examples

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"));

Structs

An opaque object containing the results of compilation.
An opaque object managing options to compilation.
An opaque object managing all compiler states.
A representation of a successfully resolved include directive, containing the name of the include and its contents.

Enums

Error.
GLSL profile.
Identifies the type of include directive. Relative is for include directives of the form #include "...", and Standard is for include directives of the form #include <...>.
Resource limit.
Optimization level.
Resource kinds.
Shader kind.
Source language.
Target environment.

Functions

Returns the version and revision of the SPIR-V generated by this library.
Parses the version and profile from the given string.

Type Definitions

Compilation status.