summaryrefslogtreecommitdiffstats
path: root/third_party/rust/glsl/src/transpiler/spirv.rs
blob: a75f3afab6fae9bd841a494e1d99eb6a644fbbce (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! SPIR-V transpiler.
//!
//! The current implementation uses the [shaderc](https://crates.io/crates/shaderc) crate to
//! transpile GLSL to SPIR-V. This is not ideal but will provide a default and starting
//! implementation.

use shaderc;

use crate::syntax;
use crate::transpiler::glsl as glsl_transpiler;

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum ShaderKind {
  TessControl,
  TessEvaluation,
  Vertex,
  Geometry,
  Fragment,
  Compute,
}

impl From<ShaderKind> for shaderc::ShaderKind {
  fn from(kind: ShaderKind) -> Self {
    match kind {
      ShaderKind::TessControl => shaderc::ShaderKind::TessControl,
      ShaderKind::TessEvaluation => shaderc::ShaderKind::TessEvaluation,
      ShaderKind::Vertex => shaderc::ShaderKind::Vertex,
      ShaderKind::Geometry => shaderc::ShaderKind::Geometry,
      ShaderKind::Fragment => shaderc::ShaderKind::Fragment,
      ShaderKind::Compute => shaderc::ShaderKind::Compute,
    }
  }
}

/// Transpile a GLSL AST into a SPIR-V internal buffer and write it to the given buffer.
///
/// The current implementation is highly inefficient as it relies on internal allocations and
/// [shaderc](https://crates.io/crates/shaderc).
///
/// If any error happens while transpiling, they’re returned as an opaque string.
pub fn transpile_translation_unit_to_binary<F>(
  f: &mut F,
  tu: &syntax::TranslationUnit,
  kind: ShaderKind,
) -> Result<(), String>
where
  F: std::io::Write,
{
  // write as GLSL in an intermediate buffer
  let mut glsl_buffer = String::new();
  glsl_transpiler::show_translation_unit(&mut glsl_buffer, tu);

  // pass the GLSL-formatted string to shaderc
  let mut compiler = shaderc::Compiler::new().unwrap();
  let options = shaderc::CompileOptions::new().unwrap();
  let kind = kind.into();
  let output = compiler
    .compile_into_spirv(&glsl_buffer, kind, "glsl input", "main", Some(&options))
    .map_err(|e| format!("{}", e))?;

  let _ = f.write_all(output.as_binary_u8());

  Ok(())
}

/// Transpile a GLSL AST into a SPIR-V internal buffer and write it to the given buffer.
///
/// The current implementation is highly inefficient as it relies on internal allocations and
/// [shaderc](https://crates.io/crates/shaderc).
///
/// If any error happens while transpiling, they’re returned as an opaque string.
pub fn transpile_translation_unit<F>(
  f: &mut F,
  tu: &syntax::TranslationUnit,
  kind: ShaderKind,
) -> Result<(), String>
where
  F: std::fmt::Write,
{
  // write as GLSL in an intermediate buffer
  let mut glsl_buffer = String::new();
  glsl_transpiler::show_translation_unit(&mut glsl_buffer, tu);

  // pass the GLSL-formatted string to shaderc
  let mut compiler = shaderc::Compiler::new().unwrap();
  let options = shaderc::CompileOptions::new().unwrap();
  let kind = kind.into();
  let output = compiler
    .compile_into_spirv_assembly(&glsl_buffer, kind, "glsl input", "main", Some(&options))
    .map_err(|e| format!("{}", e))?;

  let _ = f.write_str(&output.as_text());

  Ok(())
}