Source code

Revision control

Other Tools

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
extern crate webrender_build;
6
7
use std::borrow::Cow;
8
use std::env;
9
use std::fs::{canonicalize, read_dir, File};
10
use std::io::prelude::*;
11
use std::path::{Path, PathBuf};
12
use webrender_build::shader::*;
13
14
fn write_shaders(glsl_files: Vec<PathBuf>, shader_file_path: &Path) {
15
let mut shader_file = File::create(shader_file_path).unwrap();
16
17
write!(shader_file, "/// AUTO GENERATED BY build.rs\n\n").unwrap();
18
write!(shader_file, "use std::collections::HashMap;\n\n").unwrap();
19
write!(shader_file, "pub struct SourceWithDigest {{ pub source: &'static str, pub digest: &'static str }}\n\n")
20
.unwrap();
21
write!(shader_file, "lazy_static! {{\n").unwrap();
22
write!(
23
shader_file,
24
" pub static ref SHADERS: HashMap<&'static str, SourceWithDigest> = {{\n"
25
).unwrap();
26
write!(shader_file, " let mut h = HashMap::new();\n").unwrap();
27
for glsl in glsl_files {
28
// Compute the shader name.
29
assert!(glsl.is_file());
30
let shader_name = glsl.file_name().unwrap().to_str().unwrap();
31
let shader_name = shader_name.replace(".glsl", "");
32
33
// Compute a digest of the #include-expanded shader source. We store
34
// this as a literal alongside the source string so that we don't need
35
// to hash large strings at runtime.
36
let mut hasher = Sha256::new();
37
let base = glsl.parent().unwrap();
38
assert!(base.is_dir());
39
parse_shader_source(
40
Cow::Owned(shader_source_from_file(&glsl)),
41
&|f| Cow::Owned(shader_source_from_file(&base.join(&format!("{}.glsl", f)))),
42
&mut |s| hasher.input(s.as_bytes()),
43
);
44
let digest: ProgramSourceDigest = hasher.into();
45
46
// Compute the shader path for insertion into the include_str!() macro.
47
// This makes for more compact generated code than inserting the literal
48
// shader source into the generated file.
49
//
50
// If someone is building on a network share, I'm sorry.
51
let full_path = canonicalize(&glsl).unwrap();
52
let full_name = full_path.as_os_str().to_str().unwrap();
53
let full_name = full_name.replace("\\\\?\\", "");
54
let full_name = full_name.replace("\\", "/");
55
56
write!(
57
shader_file,
58
" h.insert(\"{}\", SourceWithDigest {{ source: include_str!(\"{}\"), digest: \"{}\"}});\n",
59
shader_name,
60
full_name,
61
digest,
62
).unwrap();
63
}
64
write!(shader_file, " h\n").unwrap();
65
write!(shader_file, " }};\n").unwrap();
66
write!(shader_file, "}}\n").unwrap();
67
}
68
69
fn main() {
70
let out_dir = env::var("OUT_DIR").unwrap_or("out".to_owned());
71
72
let shaders_file = Path::new(&out_dir).join("shaders.rs");
73
let mut glsl_files = vec![];
74
75
println!("cargo:rerun-if-changed=res");
76
let res_dir = Path::new("res");
77
for entry in read_dir(res_dir).unwrap() {
78
let entry = entry.unwrap();
79
let path = entry.path();
80
81
if entry.file_name().to_str().unwrap().ends_with(".glsl") {
82
println!("cargo:rerun-if-changed={}", path.display());
83
glsl_files.push(path.to_owned());
84
}
85
}
86
87
// Sort the file list so that the shaders.rs file is filled
88
// deterministically.
89
glsl_files.sort_by(|a, b| a.file_name().cmp(&b.file_name()));
90
91
write_shaders(glsl_files, &shaders_file);
92
}