-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
65 lines (58 loc) · 1.96 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
use std::path::Path;
use std::{
fs::{read_to_string, write},
path::PathBuf,
};
fn main() {
let shader_paths = vec!["shaders/shader.vert", "shaders/shader.frag"];
let mut compiler = shaderc::Compiler::new().expect("Able to create shader compiler");
// This can't be parallelized. The [shaderc::Compiler] is not
// thread safe. Also, it creates a lot of resources. You could
// spawn multiple processes to handle this, but it would probably
// be better just to only compile shaders that have been changed
// recently.
for shader_path in shader_paths {
let shader = ShaderData::load(shader_path);
let compiled = compiler
.compile_into_spirv(
&shader.src,
shader.kind,
&shader.src_path.to_str().unwrap(),
"main",
None,
)
.unwrap();
write(shader.spv_path, compiled.as_binary_u8()).unwrap();
println!("cargo:rerun-if-changed={}", shader_path);
}
}
struct ShaderData {
src: String,
src_path: PathBuf,
spv_path: PathBuf,
kind: shaderc::ShaderKind,
}
impl ShaderData {
pub fn load(src_path: impl AsRef<Path>) -> Self {
let src_path: &Path = src_path.as_ref();
let extension = src_path
.extension()
.expect("File has extension")
.to_str()
.expect("Extension can be converted to &str");
let kind = match extension {
"vert" => shaderc::ShaderKind::Vertex,
"frag" => shaderc::ShaderKind::Fragment,
"comp" => shaderc::ShaderKind::Compute,
_ => panic!("Unsupported shader: {}", src_path.display()),
};
let src = read_to_string(src_path).unwrap();
let spv_path = src_path.with_extension(format!("{}.spv", extension));
Self {
src,
src_path: src_path.to_path_buf(),
spv_path,
kind,
}
}
}