Module librashader::reflect

source ·
Available on crate feature reflect only.
Expand description

Shader reflection and cross-compilation.

The type_alias_impl_trait nightly feature is required. You should choose your target shading language, and a compilation type.


use std::error::Error;
use librashader::preprocess::ShaderSource;
use librashader::presets::ShaderPreset;
use librashader::reflect::{CompileReflectShader, FromCompilation, CompilePresetTarget, ShaderPassArtifact};
use librashader::reflect::targets::SPIRV;
use librashader::reflect::semantics::ShaderSemantics;
use librashader_reflect::front::{ShaderInputCompiler, SpirvCompilation};
use librashader_reflect::reflect::cross::SpirvCross;
type Artifact = impl CompileReflectShader<SPIRV, SpirvCompilation, SpirvCross>;
type ShaderPassMeta = ShaderPassArtifact<Artifact>;

// Compile single shader
pub fn compile_spirv(
        source: &ShaderSource,
    ) -> Result<Artifact, Box<dyn Error>>
    let compilation = SpirvCompilation::compile(&source)?;
    let spirv = SPIRV::from_compilation(compilation)?;

// Compile preset
pub fn compile_preset(preset: ShaderPreset) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), Box<dyn Error>>
    let (passes, semantics) = SPIRV::compile_preset_passes::<SpirvCompilation, SpirvCross, Box<dyn Error>>(
    preset.shaders, &preset.textures)?;
    Ok((passes, semantics))

§What’s with all the traits?

librashader-reflect is designed to be compiler-agnostic. In the future, we will allow usage of naga, a pure-Rust shader compiler, when it has matured enough to support the features librashader needs.

In the meanwhile, the only supported input compiler is SpirvCompilation, which does compilation of GLSL to SPIR-V via glslang.


  • crossreflect-cross
    Reflection via SPIRV-Cross.
  • dxilWindows and reflect-dxil
    DXIL reflection via spirv-to-dxil.
  • nagareflect-naga
    Reflection via Naga
  • Shader semantics and reflection information.
  • Supported shader compiler targets.




  • Trait for target shading languages that can compile output with shader preset metdata.
  • Marker trait for combinations of targets and compilations that can be reflected and compiled successfully.
  • A trait for objects that can be compiled into a shader.
  • A trait for reflectable compilations that can be transformed into an object ready for reflection or compilation.
  • Marker trait for shader compiler targets.
  • A trait for compilation outputs that can provide reflection information.
  • Trait for types that can compile shader sources into a compilation unit.
  • The output of a shader compiler that is reflectable.

Type Aliases§