-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
72 lines (57 loc) · 2.03 KB
/
Copy pathbuild.rs
File metadata and controls
72 lines (57 loc) · 2.03 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use std::path::{Path, PathBuf};
use shaderc::ShaderKind;
const SHADER_DIR: &str = "resources/shaders/";
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("cargo:rerun-if-changed=resources/shaders");
let compiler = Compiler::new(SHADER_DIR, std::env::var_os("OUT_DIR").unwrap());
compiler.compile_shader("fill.vert.glsl");
compiler.compile_shader("fill.frag.glsl");
compiler.compile_shader("image_upload_uint.comp.glsl");
Ok(())
}
struct Compiler {
compiler: shaderc::Compiler,
options: shaderc::CompileOptions<'static>,
src_dir: PathBuf,
dst_dir: PathBuf,
}
impl Compiler {
const SHADER_KINDS: &[(&'static str, ShaderKind)] = &[
("vert.glsl", ShaderKind::Vertex),
("frag.glsl", ShaderKind::Fragment),
("comp.glsl", ShaderKind::Compute),
];
fn new(src_dir: impl AsRef<Path>, dst_dir: impl AsRef<Path>) -> Self {
let mut options = shaderc::CompileOptions::new().unwrap();
options.set_target_env(
shaderc::TargetEnv::Vulkan,
shaderc::EnvVersion::Vulkan1_1 as u32,
);
Self {
compiler: shaderc::Compiler::new().unwrap(),
options,
src_dir: src_dir.as_ref().to_owned(),
dst_dir: dst_dir.as_ref().to_owned(),
}
}
fn compile_shader(&self, name: &str) {
let src_name = self.src_dir.join(name);
let kind = Self::SHADER_KINDS
.iter()
.find_map(|(s, k)| name.ends_with(s).then_some(*k))
.unwrap();
println!("src_name: {:?}", src_name);
let source = std::fs::read_to_string(&src_name).unwrap();
let binary = self
.compiler
.compile_into_spirv(&source, kind, name, "main", Some(&self.options))
.unwrap();
let dst_name = {
// to avoid an extra allocation
let mut p = self.dst_dir.join(name);
p.set_extension("spv");
p
};
std::fs::write(dst_name, binary.as_binary_u8()).unwrap();
}
}