-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
60 lines (48 loc) · 1.62 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
use std::{
error::Error,
fs::{self, File},
io::Write,
};
const SHADER_DIR: &str = "shaders-generated";
const INCLUDE_PREFIX: &str = "///#include";
fn main() -> Result<(), Box<dyn Error>> {
println!("cargo:rerun-if-changed=shaders/");
let shader_files = ["render.wgsl", "post_process.wgsl"];
std::fs::create_dir_all(SHADER_DIR)?;
for file in shader_files {
generate_shader(file)?;
}
Ok(())
}
fn generate_shader(file_name: &str) -> Result<(), Box<dyn Error>> {
let path = format!("shaders/{}", file_name);
let out_path = SHADER_DIR.to_string() + &format!("/{}", file_name);
let source = match fs::read_to_string(&path) {
Ok(source) => source,
Err(e) => panic!("Failed to read file {}: {}", path, e),
};
let mut result = String::new();
preprocess_shader(&source, &mut result);
let mut file = File::create(out_path)?;
file.write_all(result.as_bytes())?;
Ok(())
}
fn preprocess_shader(source: &str, result: &mut String) {
for line in source.lines() {
if let Some(stripped) = line.strip_prefix(INCLUDE_PREFIX) {
let include_file = stripped.trim().replace('"', "");
let include_source = get_include_source(&include_file);
preprocess_shader(&include_source, result);
} else {
result.push_str(line);
result.push('\n');
}
}
}
fn get_include_source(file_name: &str) -> String {
let path = format!("shaders/{}", file_name);
match fs::read_to_string(&path) {
Ok(source) => source,
Err(e) => panic!("Failed to read file {}: {}", path, e),
}
}