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
use std::borrow::Cow;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::collections::HashSet;
use std::collections::hash_map::DefaultHasher;
pub use crate::shader_features::*;
#[derive(PartialEq, Eq, Hash, Debug, Clone, Default)]
#[cfg_attr(feature = "serialize_program", derive(Deserialize, Serialize))]
pub struct ProgramSourceDigest(u64);
impl ::std::fmt::Display for ProgramSourceDigest {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{:02x}", self.0)
}
}
impl From<DefaultHasher> for ProgramSourceDigest {
fn from(hasher: DefaultHasher) -> Self {
use std::hash::Hasher;
ProgramSourceDigest(hasher.finish())
}
}
const SHADER_IMPORT: &str = "#include ";
pub struct ShaderSourceParser {
included: HashSet<String>,
}
impl ShaderSourceParser {
pub fn new() -> Self {
ShaderSourceParser {
included: HashSet::new(),
}
}
pub fn parse<F: FnMut(&str), G: Fn(&str) -> Cow<'static, str>>(
&mut self,
source: Cow<'static, str>,
get_source: &G,
output: &mut F,
) {
for line in source.lines() {
if line.starts_with(SHADER_IMPORT) {
let imports = line[SHADER_IMPORT.len() ..].split(',');
for import in imports {
if self.included.insert(import.into()) {
let include = get_source(import);
self.parse(include, get_source, output);
} else {
output(&format!("// {} is already included\n", import));
}
}
} else {
output(line);
output("\n");
}
}
}
}
pub fn shader_source_from_file(shader_path: &Path) -> String {
assert!(shader_path.exists(), "Shader not found {:?}", shader_path);
let mut source = String::new();
File::open(&shader_path)
.expect("Shader not found")
.read_to_string(&mut source)
.unwrap();
source
}