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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#[cfg(feature = "dot-out")]
pub mod dot;
#[cfg(feature = "glsl-out")]
pub mod glsl;
#[cfg(feature = "hlsl-out")]
pub mod hlsl;
#[cfg(feature = "msl-out")]
pub mod msl;
#[cfg(feature = "spv-out")]
pub mod spv;
#[cfg(feature = "wgsl-out")]
pub mod wgsl;
#[allow(dead_code)]
const COMPONENTS: &[char] = &['x', 'y', 'z', 'w'];
#[allow(dead_code)]
const INDENT: &str = " ";
#[allow(dead_code)]
const BAKE_PREFIX: &str = "_e";
#[derive(Clone, Copy)]
#[allow(dead_code)]
struct Level(usize);
#[allow(dead_code)]
impl Level {
fn next(&self) -> Self {
Level(self.0 + 1)
}
}
#[allow(dead_code)]
impl std::fmt::Display for Level {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
(0..self.0).try_for_each(|_| formatter.write_str(INDENT))
}
}
#[allow(dead_code)]
enum FunctionType {
Function(crate::Handle<crate::Function>),
EntryPoint(crate::proc::EntryPointIndex),
}
#[allow(dead_code)]
struct FunctionCtx<'a> {
ty: FunctionType,
info: &'a crate::valid::FunctionInfo,
expressions: &'a crate::Arena<crate::Expression>,
named_expressions: &'a crate::NamedExpressions,
}
#[allow(dead_code)]
impl<'a> FunctionCtx<'_> {
fn name_key(&self, local: crate::Handle<crate::LocalVariable>) -> crate::proc::NameKey {
match self.ty {
FunctionType::Function(handle) => crate::proc::NameKey::FunctionLocal(handle, local),
FunctionType::EntryPoint(idx) => crate::proc::NameKey::EntryPointLocal(idx, local),
}
}
fn argument_key(&self, arg: u32) -> crate::proc::NameKey {
match self.ty {
FunctionType::Function(handle) => crate::proc::NameKey::FunctionArgument(handle, arg),
FunctionType::EntryPoint(ep_index) => {
crate::proc::NameKey::EntryPointArgument(ep_index, arg)
}
}
}
fn is_fixed_function_input(
&self,
mut expression: crate::Handle<crate::Expression>,
module: &crate::Module,
) -> Option<crate::BuiltIn> {
let ep_function = match self.ty {
FunctionType::Function(_) => return None,
FunctionType::EntryPoint(ep_index) => &module.entry_points[ep_index as usize].function,
};
let mut built_in = None;
loop {
match self.expressions[expression] {
crate::Expression::FunctionArgument(arg_index) => {
return match ep_function.arguments[arg_index as usize].binding {
Some(crate::Binding::BuiltIn(bi)) => Some(bi),
_ => built_in,
};
}
crate::Expression::AccessIndex { base, index } => {
match *self.info[base].ty.inner_with(&module.types) {
crate::TypeInner::Struct { ref members, .. } => {
if let Some(crate::Binding::BuiltIn(bi)) =
members[index as usize].binding
{
built_in = Some(bi);
}
}
_ => return None,
}
expression = base;
}
_ => return None,
}
}
}
}
impl crate::Expression {
#[allow(dead_code)]
fn bake_ref_count(&self) -> usize {
match *self {
crate::Expression::Access { .. } | crate::Expression::AccessIndex { .. } => !0,
crate::Expression::ImageSample { .. } | crate::Expression::ImageLoad { .. } => 1,
crate::Expression::Derivative { .. } => 1,
crate::Expression::Load { .. } => 1,
_ => 2,
}
}
}
#[allow(dead_code)]
fn binary_operation_str(op: crate::BinaryOperator) -> &'static str {
use crate::BinaryOperator as Bo;
match op {
Bo::Add => "+",
Bo::Subtract => "-",
Bo::Multiply => "*",
Bo::Divide => "/",
Bo::Modulo => "%",
Bo::Equal => "==",
Bo::NotEqual => "!=",
Bo::Less => "<",
Bo::LessEqual => "<=",
Bo::Greater => ">",
Bo::GreaterEqual => ">=",
Bo::And => "&",
Bo::ExclusiveOr => "^",
Bo::InclusiveOr => "|",
Bo::LogicalAnd => "&&",
Bo::LogicalOr => "||",
Bo::ShiftLeft => "<<",
Bo::ShiftRight => ">>",
}
}
#[allow(dead_code)]
fn vector_size_str(size: crate::VectorSize) -> &'static str {
match size {
crate::VectorSize::Bi => "2",
crate::VectorSize::Tri => "3",
crate::VectorSize::Quad => "4",
}
}
impl crate::TypeInner {
#[allow(unused)]
fn is_handle(&self) -> bool {
match *self {
crate::TypeInner::Image { .. } | crate::TypeInner::Sampler { .. } => true,
_ => false,
}
}
}
impl crate::Statement {
pub fn is_terminator(&self) -> bool {
match *self {
crate::Statement::Break
| crate::Statement::Continue
| crate::Statement::Return { .. }
| crate::Statement::Kill => true,
_ => false,
}
}
}