lambda/render/
shader.rs

1//! A module for compiling shaders into SPIR-V binary.
2
3// Expose some lower level shader
4pub use lambda_platform::shaderc::{
5  ShaderCompiler,
6  ShaderCompilerBuilder,
7  ShaderKind,
8  VirtualShader,
9};
10
11pub struct ShaderBuilder {
12  compiler: ShaderCompiler,
13}
14
15impl ShaderBuilder {
16  /// Creates a new shader builder that can be reused for compiling shaders.
17  pub fn new() -> Self {
18    let compiler = ShaderCompilerBuilder::new().build();
19    return Self { compiler };
20  }
21
22  /// Compiles the virtual shader into a real shader with SPIR-V binary
23  /// representation.
24  pub fn build(&mut self, virtual_shader: VirtualShader) -> Shader {
25    logging::trace!("Compiling shader: {:?}", virtual_shader);
26    let binary = self.compiler.compile_into_binary(&virtual_shader);
27
28    return Shader {
29      binary,
30      virtual_shader,
31    };
32  }
33}
34
35/// A shader that has been compiled into SPIR-V binary. Contains the binary
36/// representation of the shader as well as the virtual shader that was used
37/// to compile it.
38pub struct Shader {
39  binary: Vec<u32>,
40  virtual_shader: VirtualShader,
41}
42
43impl Shader {
44  /// Returns a copy of the SPIR-V binary representation of the shader.
45  pub fn as_binary(&self) -> Vec<u32> {
46    return self.binary.clone();
47  }
48}