-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
64 lines (51 loc) · 2.11 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
//! Minify WGSL shaders.
use std::path::Path;
use naga::{
back::wgsl::WriterFlags,
valid::{Capabilities, ValidationFlags, Validator},
};
/// Compile a WGSL shader into Spir-V bytes and write it to file.
fn minify_wgsl(source: impl AsRef<Path>, target: impl AsRef<Path>, include_base: bool) {
// Read the source WGSL
let mut source = std::fs::read_to_string(source).expect("Error reading WGSL shader file");
// Include the base if set
if include_base {
source.push_str(include_str!("shaders/custom_shader_base.wgsl"));
}
// Parse into NAGA module
let mut module = naga::front::wgsl::parse_str(&source).expect("Error compiling WGSL shader");
// Create the validator
let info = Validator::new(ValidationFlags::all(), Capabilities::all())
.validate(&module)
.expect("Error while validating WGSL shader");
// Optimize shader, removing unused stuff
naga::compact::compact(&mut module);
// Compile back into WGSL
let output = naga::back::wgsl::write_string(&module, &info, WriterFlags::empty())
.expect("Error converting WGSL module back to WGSL code");
// Minify the WGSL
let output = wgsl_minifier::minify_wgsl_source(&output);
// Convert to bytes
std::fs::write(target, output).expect("Error writing minified WGSL shader to file");
}
fn main() {
// Rerun build script if shaders changed
println!("cargo::rerun-if-changed=shaders/downscale.wgsl");
println!("cargo::rerun-if-changed=shaders/rotation.wgsl");
println!("cargo::rerun-if-changed=shaders/nearest_neighbor.wgsl");
println!("cargo::rerun-if-changed=shaders/custom_shader_base.wgsl");
let out_dir_str = std::env::var_os("OUT_DIR").unwrap();
let out_dir = Path::new(&out_dir_str);
// Compile the shaders into binaries placed in the OUT_DIR
minify_wgsl(
"shaders/downscale.wgsl",
out_dir.join("downscale.wgsl"),
false,
);
minify_wgsl("shaders/rotation.wgsl", out_dir.join("rotation.wgsl"), true);
minify_wgsl(
"shaders/nearest_neighbor.wgsl",
out_dir.join("nearest_neighbor.wgsl"),
true,
);
}