/
build.rs
57 lines (43 loc) · 1.71 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
extern crate glsl_to_spirv;
use std::error::Error;
const SRC_LOC: &'static str = "src/shaders";
const DEST_LOC: &'static str = "assets/shaders";
fn main() -> Result<(), Box<Error>> {
use glsl_to_spirv::ShaderType;
// Tell the build script to only run again if we change our source shaders
println!("cargo:rerun-if-changed={}", SRC_LOC);
// Create destination directory
std::fs::create_dir_all(DEST_LOC)?;
// Iterate through shaders and compile them
for entry in std::fs::read_dir(SRC_LOC)? {
let entry = entry?;
if entry.file_type()?.is_file() {
let in_path = entry.path();
if in_path.to_str().unwrap().contains("glium") {
continue;
}
// Pick between vertex and fragment shader
let shader_type = in_path.extension().and_then(|ext| {
match ext.to_string_lossy().as_ref() {
"vert" => Some(ShaderType::Vertex),
"frag" => Some(ShaderType::Fragment),
_ => None,
}
});
if let Some(shader_type) = shader_type {
use std::io::Read;
let source = std::fs::read_to_string(&in_path)?;
let mut compiled_file = glsl_to_spirv::compile(&source, shader_type)?;
let mut compiled_bytes = Vec::new();
compiled_file.read_to_end(&mut compiled_bytes)?;
let out_path = format!(
"{}/{}.spv",
DEST_LOC,
in_path.file_name().unwrap().to_string_lossy()
);
std::fs::write(&out_path, &compiled_bytes)?;
}
}
}
Ok(())
}