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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::collections::BTreeMap;
use bevy::render::render_resource::ShaderDefVal;
pub type EntryPointName = &'static str;
pub type EntryPointParameters =
&'static [(&'static [(&'static str, &'static str)], &'static str)];
pub type EntryPointConstants = &'static [&'static str];
pub type EntryPointTypes = Vec<(String, String)>;
pub trait EntryPoint: 'static + Send + Sync {
const NAME: &'static str;
fn parameters() -> EntryPointParameters {
&[]
}
fn constants() -> EntryPointConstants {
&[]
}
fn types() -> EntryPointTypes {
vec![]
}
fn permutation(shader_defs: &Vec<ShaderDefVal>) -> Vec<String> {
let mut permutation = vec![];
for (defined, undefined) in Self::parameters().iter() {
if let Some(mapping) = defined.iter().find_map(|(def, mapping)| {
if shader_defs.contains(&ShaderDefVal::Bool(def.to_string(), true)) {
Some(mapping)
} else {
None
}
}) {
permutation.push(mapping.to_string());
} else {
permutation.push(undefined.to_string())
};
}
permutation
}
fn filter_constants(shader_defs: &Vec<ShaderDefVal>) -> Vec<ShaderDefVal> {
shader_defs
.iter()
.filter(|def| match def {
ShaderDefVal::Bool(key, _)
| ShaderDefVal::Int(key, _)
| ShaderDefVal::UInt(key, _) => Self::constants().contains(&key.as_str()),
})
.cloned()
.collect()
}
fn build(shader_defs: &Vec<ShaderDefVal>) -> String {
let constants = Self::filter_constants(shader_defs)
.into_iter()
.map(|def| {
(
match &def {
ShaderDefVal::Bool(key, _)
| ShaderDefVal::Int(key, _)
| ShaderDefVal::UInt(key, _) => key.clone(),
},
match &def {
ShaderDefVal::Bool(value, _) => value.to_string(),
ShaderDefVal::Int(_, value) => value.to_string(),
ShaderDefVal::UInt(_, value) => value.to_string(),
},
)
})
.collect::<BTreeMap<_, _>>();
std::iter::once(Self::NAME.to_string())
.chain(
Self::permutation(shader_defs)
.into_iter()
.map(|variant| "__".to_string() + &variant),
)
.chain(
constants
.into_iter()
.map(|(key, value)| key + "_" + &value)
.map(|variant| "__".to_string() + &variant),
)
.chain(
Self::types()
.into_iter()
.map(|(key, value)| {
key.to_string().to_lowercase()
+ "_"
+ &value
.to_string()
.replace(" ", "")
.replace("\n", "")
.replace("<", "_")
.replace(">", "_")
.replace("[", "_")
.replace("]", "_")
.replace("(", "_")
.replace(")", "_")
.replace("::", "_")
.replace(",", "_")
.trim_end_matches("_")
.to_lowercase()
})
.map(|variant| "__".to_string() + &variant),
)
.collect::<String>()
}
}
impl EntryPoint for () {
const NAME: &'static str = "";
}