-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.rs
105 lines (97 loc) · 3.28 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
#[cfg(feature = "shaderc")]
extern crate shaderc;
#[allow(unused_imports)]
use std::fs::File;
#[allow(unused_imports)]
use std::io::{Read, Write};
#[allow(unused_imports)]
use std::path::Path;
#[cfg(feature = "shaderc")]
use shaderc::{CompileOptions, EnvVersion, TargetEnv};
#[cfg(feature = "shaderc")]
fn load_file(path: &Path) -> String {
let mut out = String::new();
File::open(path).unwrap().read_to_string(&mut out).unwrap();
out
}
#[cfg(feature = "shaderc")]
fn save_file(path: &Path, binary: &[u8]) {
File::create(path).unwrap().write_all(binary).unwrap();
}
#[cfg(feature = "shaderc")]
fn compile_shader(path: &Path, kind: shaderc::ShaderKind, output: &Path) {
let compiler = shaderc::Compiler::new().unwrap();
let mut options = CompileOptions::new().unwrap();
options.set_target_env(TargetEnv::Vulkan, EnvVersion::Vulkan1_2 as u32);
let binary = compiler
.compile_into_spirv(
&load_file(path),
kind,
path.as_os_str().to_str().unwrap(),
"main",
Some(&options),
)
.unwrap();
save_file(output, binary.as_binary_u8());
}
#[cfg(feature = "shaderc")]
fn compile_shaders() {
println!("cargo:rerun-if-changed=examples/data/vert.glsl");
println!("cargo:rerun-if-changed=examples/data/frag.glsl");
println!("cargo:rerun-if-changed=examples/data/blue.glsl");
println!("cargo:rerun-if-changed=examples/data/compute.glsl");
println!("cargo:rerun-if-changed=examples/data/raygen.rgen");
println!("cargo:rerun-if-changed=examples/data/rayhit.rchit");
println!("cargo:rerun-if-changed=examples/data/raymiss.rmiss");
println!("cargo:rerun-if-changed=examples/data/fsr_render_frag.glsl");
println!("cargo:rerun-if-changed=examples/data/fsr_render_vert.glsl");
compile_shader(
Path::new("examples/data/vert.glsl"),
shaderc::ShaderKind::Vertex,
Path::new("examples/data/vert.spv"),
);
compile_shader(
Path::new("examples/data/frag.glsl"),
shaderc::ShaderKind::Fragment,
Path::new("examples/data/frag.spv"),
);
compile_shader(
Path::new("examples/data/blue.glsl"),
shaderc::ShaderKind::Fragment,
Path::new("examples/data/blue.spv"),
);
compile_shader(
Path::new("examples/data/compute.glsl"),
shaderc::ShaderKind::Compute,
Path::new("examples/data/compute.spv"),
);
compile_shader(
Path::new("examples/data/raygen.rgen"),
shaderc::ShaderKind::RayGeneration,
Path::new("examples/data/raygen.spv"),
);
compile_shader(
Path::new("examples/data/rayhit.rchit"),
shaderc::ShaderKind::ClosestHit,
Path::new("examples/data/rayhit.spv"),
);
compile_shader(
Path::new("examples/data/raymiss.rmiss"),
shaderc::ShaderKind::Miss,
Path::new("examples/data/raymiss.spv"),
);
compile_shader(
Path::new("examples/data/fsr_render_frag.glsl"),
shaderc::ShaderKind::Fragment,
Path::new("examples/data/fsr_render_frag.spv"),
);
compile_shader(
Path::new("examples/data/fsr_render_vert.glsl"),
shaderc::ShaderKind::Vertex,
Path::new("examples/data/fsr_render_vert.spv"),
);
}
fn main() {
#[cfg(feature = "shaderc")]
compile_shaders();
}