#shader #compiler #spirv #glsl #hlsl

shaderc

Rust bindings for shaderc

22 releases

new 0.5.0 Apr 22, 2019
0.3.16 Feb 3, 2019
0.3.13 Jan 15, 2019
0.3.12 Nov 11, 2018
0.2.0 Feb 7, 2017

#36 in Graphics APIs

Download history 272/week @ 2019-01-07 468/week @ 2019-01-14 416/week @ 2019-01-21 505/week @ 2019-01-28 509/week @ 2019-02-04 503/week @ 2019-02-11 346/week @ 2019-02-18 288/week @ 2019-02-25 396/week @ 2019-03-04 893/week @ 2019-03-11 400/week @ 2019-03-18 515/week @ 2019-03-25 432/week @ 2019-04-01 502/week @ 2019-04-08 558/week @ 2019-04-15

2,045 downloads per month
Used in 17 crates (8 directly)

Apache-2.0

16MB
353K SLoC

C++ 288K SLoC // 0.1% comments GLSL 36K SLoC // 0.1% comments Python 13K SLoC // 0.1% comments C# 4K SLoC // 0.0% comments Lua 4K SLoC // 0.0% comments Happy 3.5K SLoC Rust 2K SLoC // 0.2% comments D 1K SLoC // 0.0% comments Shell 615 SLoC // 0.5% comments Batch 126 SLoC // 0.6% comments C 118 SLoC // 0.2% comments Emacs Lisp 8 SLoC // 0.7% comments

lib.rs:

Rust binding for the Shaderc library.

This crate contains the higher-level Rust-friendly interface for the Shaderc library. For the lower-level C interface, please see the shaderc-sys crate.

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.

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

  1. The option --features build-from-source will prevent automatic library detection and force building from source.
  2. If the SHADERC_LIB_DIR environment variable is set to /path/to/shaderc/libs/, it will take precedence and libshaderc_combined.a (and the glsang and SPIRV libraries on Linux) will be searched in the /path/to/shaderc/libs/ directory.
  3. On Linux, /usr/lib/ will be automatically searched for system libraries if none of the above were given.
  4. If no other option was set or succeeded, shaderc-sys will fall back to checking out and compiling a copy of Shaderc. This procedure is quite slow.

NOTE: --no-default-features still works on shaderc-rs, but shaderc-sys implements this behavior in a deprecated manner, and it will be removed in the next release. This method only works with a monolithic libshaderc_combined.a. Refer to pre-0.5 documentation for more information. Prefer SHADERC_LIB_DIR="/path/to/shaderc/libs/".

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

Dependencies