-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
53 lines (44 loc) · 2.01 KB
/
build.rs
File metadata and controls
53 lines (44 loc) · 2.01 KB
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
use std::fs::{read_to_string, write};
const ENV_WITH_DEFAULT: &[(&str, &str)] = &[("DATADIR", "dist"), ("PAM_SERVICE", "system-auth")];
const RESOURCES: &str = "resources";
const VERTEX_SHADER_GLOB: &str = "*.vert";
const VERTEX_SPIRV_EXTENSION: &str = "vert.spv";
const FRAGMENT_SHADER_GLOB: &str = "*.frag";
const FRAGMENT_SPIRV_EXTENSION: &str = "frag.spv";
fn vertex_shader_files() -> impl Iterator<Item = std::path::PathBuf> {
glob::glob(&format!("{}/{}", RESOURCES, VERTEX_SHADER_GLOB))
.expect("Failed to parse shader file glob")
.map(|r| r.expect("Failed to read path"))
}
fn fragment_shader_files() -> impl Iterator<Item = std::path::PathBuf> {
glob::glob(&format!("{}/{}", RESOURCES, FRAGMENT_SHADER_GLOB))
.expect("Failed to parse shader file glob")
.map(|r| r.expect("Failed to read path"))
}
fn compile(compiler: &mut shaderc::Compiler, file: &std::path::Path, kind: shaderc::ShaderKind) {
println!("cargo:rerun-if-changed={:?}", file);
let data = read_to_string(file).expect("Failed to read shader");
let compiled = compiler
.compile_into_spirv(&data, kind, &file.to_string_lossy(), "main", None)
.expect("Failed to compile shader");
let output = match kind {
shaderc::ShaderKind::Vertex => file.with_extension(VERTEX_SPIRV_EXTENSION),
shaderc::ShaderKind::Fragment => file.with_extension(FRAGMENT_SPIRV_EXTENSION),
_ => unreachable!(),
};
write(output, compiled.as_binary_u8()).expect("Failed to write Spirv");
}
fn main() {
let mut compiler = shaderc::Compiler::new().expect("Failed to create compiler");
for file in vertex_shader_files() {
compile(&mut compiler, &file, shaderc::ShaderKind::Vertex);
}
for file in fragment_shader_files() {
compile(&mut compiler, &file, shaderc::ShaderKind::Fragment);
}
if std::env::var("PROFILE").unwrap() != "release" {
for (var, default) in ENV_WITH_DEFAULT {
println!("cargo:rustc-env={}={}", var, default);
}
}
}