-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
74 lines (63 loc) · 1.81 KB
/
build.rs
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
use anyhow::*;
use glob::glob;
use shaderc::{Compiler, ShaderKind};
use std::{
fs::{read_to_string, write},
path::PathBuf,
};
struct ShaderData {
src: String,
src_path: PathBuf,
spv_path: PathBuf,
kind: ShaderKind,
}
impl ShaderData {
fn load(src_path: PathBuf) -> Result<Self> {
let extension = src_path
.extension()
.context("File has no extension")?
.to_str()
.context("Extension cannot be converted to &str")?;
let kind = match extension {
"vert" => ShaderKind::Vertex,
"frag" => ShaderKind::Fragment,
"comp" => ShaderKind::Compute,
_ => bail!("Unsupported shader: {}", src_path.display()),
};
let src = read_to_string(src_path.clone())?;
let spv_path = src_path.with_extension(format!("{}.spv", extension));
Ok(Self {
src,
src_path,
spv_path,
kind,
})
}
}
fn main() -> Result<()> {
let mut shader_paths = [
glob("shaders/**/*.vert")?,
glob("shaders/**/*.frag")?,
glob("shaders/**/*.comp")?,
];
let shaders = shader_paths
.iter_mut()
.flatten()
.map(|glob_result| ShaderData::load(glob_result?))
.collect::<Vec<Result<_>>>()
.into_iter()
.collect::<Result<Vec<_>>>()?;
let mut compiler = Compiler::new().context("unable to create shader compiler")?;
for shader in shaders {
println!("cargo:rerun-if-changed={:?}", shader.src_path);
let compiled = compiler.compile_into_spirv(
&shader.src,
shader.kind,
&shader.src_path.to_str().unwrap(),
"main",
None,
)?;
write(shader.spv_path, compiled.as_binary_u8())?;
}
Ok(())
}