| 1 |
//! SPIR-V transpiler. |
| 2 |
//! |
| 3 |
//! The current implementation uses the [shaderc](https://crates.io/crates/shaderc) crate to |
| 4 |
//! transpile GLSL to SPIR-V. This is not ideal but will provide a default and starting |
| 5 |
//! implementation. |
| 6 |
|
| 7 |
use shaderc; |
| 8 |
|
| 9 |
use crate::syntax; |
| 10 |
use crate::transpiler::glsl as glsl_transpiler; |
| 11 |
|
| 12 |
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] |
| 13 |
pub enum ShaderKind { |
| 14 |
TessControl, |
| 15 |
TessEvaluation, |
| 16 |
Vertex, |
| 17 |
Geometry, |
| 18 |
Fragment, |
| 19 |
Compute, |
| 20 |
} |
| 21 |
|
| 22 |
impl From<ShaderKind> for shaderc::ShaderKind { |
| 23 |
fn from(kind: ShaderKind) -> Self { |
| 24 |
match kind { |
| 25 |
ShaderKind::TessControl => shaderc::ShaderKind::TessControl, |
| 26 |
ShaderKind::TessEvaluation => shaderc::ShaderKind::TessEvaluation, |
| 27 |
ShaderKind::Vertex => shaderc::ShaderKind::Vertex, |
| 28 |
ShaderKind::Geometry => shaderc::ShaderKind::Geometry, |
| 29 |
ShaderKind::Fragment => shaderc::ShaderKind::Fragment, |
| 30 |
ShaderKind::Compute => shaderc::ShaderKind::Compute, |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/// Transpile a GLSL AST into a SPIR-V internal buffer and write it to the given buffer. |
| 36 |
/// |
| 37 |
/// The current implementation is highly inefficient as it relies on internal allocations and |
| 38 |
/// [shaderc](https://crates.io/crates/shaderc). |
| 39 |
/// |
| 40 |
/// If any error happens while transpiling, they’re returned as an opaque string. |
| 41 |
pub fn transpile_translation_unit_to_binary<F>( |
| 42 |
f: &mut F, |
| 43 |
tu: &syntax::TranslationUnit, |
| 44 |
kind: ShaderKind, |
| 45 |
) -> Result<(), String> |
| 46 |
where |
| 47 |
F: std::io::Write, |
| 48 |
{ |
| 49 |
// write as GLSL in an intermediate buffer |
| 50 |
let mut glsl_buffer = String::new(); |
| 51 |
glsl_transpiler::show_translation_unit(&mut glsl_buffer, tu); |
| 52 |
|
| 53 |
// pass the GLSL-formatted string to shaderc |
| 54 |
let mut compiler = shaderc::Compiler::new().unwrap(); |
| 55 |
let options = shaderc::CompileOptions::new().unwrap(); |
| 56 |
let kind = kind.into(); |
| 57 |
let output = compiler |
| 58 |
.compile_into_spirv(&glsl_buffer, kind, "glsl input", "main", Some(&options)) |
| 59 |
.map_err(|e| format!("{}", e))?; |
| 60 |
|
| 61 |
let _ = f.write_all(output.as_binary_u8()); |
| 62 |
|
| 63 |
Ok(()) |
| 64 |
} |
| 65 |
|
| 66 |
/// Transpile a GLSL AST into a SPIR-V internal buffer and write it to the given buffer. |
| 67 |
/// |
| 68 |
/// The current implementation is highly inefficient as it relies on internal allocations and |
| 69 |
/// [shaderc](https://crates.io/crates/shaderc). |
| 70 |
/// |
| 71 |
/// If any error happens while transpiling, they’re returned as an opaque string. |
| 72 |
pub fn transpile_translation_unit<F>( |
| 73 |
f: &mut F, |
| 74 |
tu: &syntax::TranslationUnit, |
| 75 |
kind: ShaderKind, |
| 76 |
) -> Result<(), String> |
| 77 |
where |
| 78 |
F: std::fmt::Write, |
| 79 |
{ |
| 80 |
// write as GLSL in an intermediate buffer |
| 81 |
let mut glsl_buffer = String::new(); |
| 82 |
glsl_transpiler::show_translation_unit(&mut glsl_buffer, tu); |
| 83 |
|
| 84 |
// pass the GLSL-formatted string to shaderc |
| 85 |
let mut compiler = shaderc::Compiler::new().unwrap(); |
| 86 |
let options = shaderc::CompileOptions::new().unwrap(); |
| 87 |
let kind = kind.into(); |
| 88 |
let output = compiler |
| 89 |
.compile_into_spirv_assembly(&glsl_buffer, kind, "glsl input", "main", Some(&options)) |
| 90 |
.map_err(|e| format!("{}", e))?; |
| 91 |
|
| 92 |
let _ = f.write_str(&output.as_text()); |
| 93 |
|
| 94 |
Ok(()) |
| 95 |
} |