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
use crate::{
proc::{ensure_block_returns, Typifier},
Block, Expression, Function, MathFunction, SampleLevel, TypeInner,
};
use super::{ast::*, error::ErrorKind};
impl Program {
pub fn function_call(&mut self, fc: FunctionCall) -> Result<ExpressionRule, ErrorKind> {
match fc.kind {
FunctionCallKind::TypeConstructor(ty) => {
let h = if fc.args.len() == 1 {
let kind = self.module.types[ty].inner.scalar_kind().ok_or_else(|| {
ErrorKind::SemanticError("Can only cast to scalar or vector".into())
})?;
self.context.expressions.append(Expression::As {
kind,
expr: fc.args[0].expression,
convert: true,
})
} else {
self.context.expressions.append(Expression::Compose {
ty,
components: fc.args.iter().map(|a| a.expression).collect(),
})
};
Ok(ExpressionRule {
expression: h,
statements: fc
.args
.into_iter()
.map(|a| a.statements)
.flatten()
.collect(),
sampler: None,
})
}
FunctionCallKind::Function(name) => {
match name.as_str() {
"sampler2D" => {
if fc.args.len() != 2 {
return Err(ErrorKind::WrongNumberArgs(name, 2, fc.args.len()));
}
Ok(ExpressionRule {
expression: fc.args[0].expression,
sampler: Some(fc.args[1].expression),
statements: fc
.args
.into_iter()
.map(|a| a.statements)
.flatten()
.collect(),
})
}
"texture" => {
if fc.args.len() != 2 {
return Err(ErrorKind::WrongNumberArgs(name, 2, fc.args.len()));
}
if let Some(sampler) = fc.args[0].sampler {
Ok(ExpressionRule {
expression: self.context.expressions.append(
Expression::ImageSample {
image: fc.args[0].expression,
sampler,
coordinate: fc.args[1].expression,
array_index: None, //TODO
offset: None, //TODO
level: SampleLevel::Auto,
depth_ref: None,
},
),
sampler: None,
statements: fc
.args
.into_iter()
.map(|a| a.statements)
.flatten()
.collect(),
})
} else {
Err(ErrorKind::SemanticError("Bad call to texture".into()))
}
}
"ceil" | "round" | "floor" | "fract" | "trunc" => {
if fc.args.len() != 1 {
return Err(ErrorKind::WrongNumberArgs(name, 1, fc.args.len()));
}
Ok(ExpressionRule {
expression: self.context.expressions.append(Expression::Math {
fun: match name.as_str() {
"ceil" => MathFunction::Ceil,
"round" => MathFunction::Round,
"floor" => MathFunction::Floor,
"fract" => MathFunction::Fract,
"trunc" => MathFunction::Trunc,
_ => unreachable!(),
},
arg: fc.args[0].expression,
arg1: None,
arg2: None,
}),
sampler: None,
statements: fc.args.into_iter().flat_map(|a| a.statements).collect(),
})
}
func_name => {
let function = *self.lookup_function.get(func_name).ok_or_else(|| {
ErrorKind::SemanticError(
format!("Unknown function: {}", func_name).into(),
)
})?;
Ok(ExpressionRule {
expression: self.context.expressions.append(Expression::Call {
function,
arguments: fc.args.iter().map(|a| a.expression).collect(),
}),
sampler: None,
statements: fc.args.into_iter().flat_map(|a| a.statements).collect(),
})
}
}
}
}
}
pub fn add_function_prelude(&mut self) {
for (var_handle, var) in self.module.global_variables.iter() {
if let Some(name) = var.name.as_ref() {
let expr = self
.context
.expressions
.append(Expression::GlobalVariable(var_handle));
self.context
.lookup_global_var_exps
.insert(name.clone(), expr);
} else {
let ty = &self.module.types[var.ty];
// anonymous structs
if let TypeInner::Struct {
block: _,
ref members,
} = ty.inner
{
let base = self
.context
.expressions
.append(Expression::GlobalVariable(var_handle));
for (idx, member) in members.iter().enumerate() {
if let Some(name) = member.name.as_ref() {
let exp = self.context.expressions.append(Expression::AccessIndex {
base,
index: idx as u32,
});
self.context
.lookup_global_var_exps
.insert(name.clone(), exp);
}
}
}
}
}
}
pub fn function_definition(&mut self, mut f: Function, mut block: Block) -> Function {
std::mem::swap(&mut f.expressions, &mut self.context.expressions);
std::mem::swap(&mut f.local_variables, &mut self.context.local_variables);
self.context.clear_scopes();
self.context.lookup_global_var_exps.clear();
self.context.typifier = Typifier::new();
ensure_block_returns(&mut block);
f.body = block;
f.fill_global_use(&self.module.global_variables);
f
}
}