1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! A module for compiling shaders into SPIR-V binary.

// Expose some lower level shader
pub use lambda_platform::shaderc::{
  ShaderCompiler,
  ShaderCompilerBuilder,
  ShaderKind,
  VirtualShader,
};

pub struct ShaderBuilder {
  compiler: ShaderCompiler,
}

impl ShaderBuilder {
  /// Creates a new shader builder that can be reused for compiling shaders.
  pub fn new() -> Self {
    let compiler = ShaderCompilerBuilder::new().build();
    return Self { compiler };
  }

  /// Compiles the virtual shader into a real shader with SPIR-V binary
  /// representation.
  pub fn build(&mut self, virtual_shader: VirtualShader) -> Shader {
    let binary = self.compiler.compile_into_binary(&virtual_shader);

    return Shader {
      binary,
      virtual_shader,
    };
  }
}

/// A shader that has been compiled into SPIR-V binary. Contains the binary
/// representation of the shader as well as the virtual shader that was used
/// to compile it.
pub struct Shader {
  binary: Vec<u32>,
  virtual_shader: VirtualShader,
}

impl Shader {
  /// Returns a copy of the SPIR-V binary representation of the shader.
  pub fn as_binary(&self) -> Vec<u32> {
    return self.binary.clone();
  }
}