-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
140 lines (116 loc) · 3.73 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::collections::hash_map::DefaultHasher;
use std::env;
use std::hash::{Hash, Hasher};
use std::path::PathBuf;
use std::process::Command;
const SHADER_OUTPUT_NAME: &str = "pbr";
fn main() {
generate_rust_types_from_shader_types();
compile_shaders();
}
// xcrun -sdk macosx metal -c shaders.metal -o shaders.air
// xcrun -sdk macosx metallib shaders.air -o shaders.metallib
fn compile_shaders() {
let shader_files = ["pbr", "environment_map", "brdf"];
let shader_air_files: Vec<_> = shader_files
.iter()
.map(|each| format!("./assets/shaders/{each}.air"))
.collect();
println!("cargo:rerun-if-changed=shaders.metal");
println!("cargo:rerun-if-changed=shader_types.h");
shader_files.iter().for_each(|each| {
let output = Command::new("xcrun")
.arg("-sdk")
.arg("macosx")
.arg("metal")
.arg("-frecord-sources")
.args(&["-c", format!("./assets/shaders/{each}.metal").as_str()])
.args(&["-o", format!("./assets/shaders/{each}.air").as_str()])
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
if !output.status.success() {
panic!(
r#"
stdout: {}
stderr: {}
"#,
String::from_utf8(output.stdout).unwrap(),
String::from_utf8(output.stderr).unwrap()
);
}
});
Command::new("xcrun")
.arg("-sdk")
.arg("macosx")
// .arg("metallib")
.arg("metal")
.arg("-frecord-sources")
.args(&[
"-o",
format!("./assets/shaders/{SHADER_OUTPUT_NAME}.metallib").as_str(),
])
.args(&shader_air_files)
.spawn()
.unwrap()
.wait()
.unwrap();
// https://developer.apple.com/documentation/metal/libraries/generating_and_loading_a_metal_library_symbol_file
Command::new("xcrun")
.arg("-sdk")
.arg("macosx")
.arg("metal-dsymutil")
.arg("-flat")
.arg("-remove-source")
.arg(format!("./assets/shaders/{SHADER_OUTPUT_NAME}.metallib").as_str())
.spawn()
.unwrap()
.wait()
.unwrap();
}
fn generate_rust_types_from_shader_types() {
println!("cargo:rerun-if-changed=shader_types");
let current_hash = hash_shader_types();
if let Some(old_hash) = read_cached_shader_types_hash() {
if old_hash == current_hash {
return;
}
}
let bindings = bindgen::Builder::default()
.header("shader_types/wrapper.h")
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate()
.expect("Unable to generate bindings");
let out = PathBuf::from(env::var("OUT_DIR").unwrap());
let out = out.join("shader_bindings.rs");
bindings.write_to_file(out).unwrap();
save_shader_types_hash(current_hash);
}
fn hash_shader_types() -> u64 {
let mut hasher = DefaultHasher::new();
PathBuf::from("shader_types")
.read_dir()
.unwrap()
.for_each(|entry| {
let entry = entry.unwrap();
let file = std::fs::read(entry.path()).unwrap();
file.hash(&mut hasher);
});
hasher.finish()
}
fn read_cached_shader_types_hash() -> Option<u64> {
let hash = shader_types_hash_file();
let hash = match std::fs::read(hash) {
Ok(hash) => Some(hash),
_ => None,
}?;
let hash = String::from_utf8(hash).unwrap().parse::<u64>().unwrap();
Some(hash)
}
fn save_shader_types_hash(hash: u64) {
std::fs::write(shader_types_hash_file(), format!("{}", hash)).unwrap();
}
fn shader_types_hash_file() -> PathBuf {
PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()).join("shader_types_hash")
}