]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/spirv.cpp
Fix incorrect lookup of GLSL interface variables
[libs/gl.git] / source / glsl / spirv.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/core/maputils.h>
3 #include <msp/core/raii.h>
4 #include "reflect.h"
5 #include "spirv.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11 namespace SL {
12
13 const SpirVGenerator::BuiltinFunctionInfo SpirVGenerator::builtin_functions[] =
14 {
15         { "radians", "f", "GLSL.std.450", GLSL450_RADIANS, { 1 }, 0, 0 },
16         { "degrees", "f", "GLSL.std.450", GLSL450_DEGREES, { 1 }, 0, 0 },
17         { "sin", "f", "GLSL.std.450", GLSL450_SIN, { 1 }, 0, 0 },
18         { "cos", "f", "GLSL.std.450", GLSL450_COS, { 1 }, 0, 0 },
19         { "tan", "f", "GLSL.std.450", GLSL450_TAN, { 1 }, 0, 0 },
20         { "asin", "f", "GLSL.std.450", GLSL450_ASIN, { 1 }, 0, 0 },
21         { "acos", "f", "GLSL.std.450", GLSL450_ACOS, { 1 }, 0, 0 },
22         { "atan", "f", "GLSL.std.450", GLSL450_ATAN, { 1 }, 0, 0 },
23         { "atan", "ff", "GLSL.std.450", GLSL450_ATAN2, { 1, 2 }, 0, 0 },
24         { "sinh", "f", "GLSL.std.450", GLSL450_SINH, { 1 }, 0, 0 },
25         { "cosh", "f", "GLSL.std.450", GLSL450_COSH, { 1 }, 0, 0 },
26         { "tanh", "f", "GLSL.std.450", GLSL450_TANH, { 1 }, 0, 0 },
27         { "asinh", "f", "GLSL.std.450", GLSL450_ASINH, { 1 }, 0, 0 },
28         { "acosh", "f", "GLSL.std.450", GLSL450_ACOSH, { 1 }, 0, 0 },
29         { "atanh", "f", "GLSL.std.450", GLSL450_ATANH, { 1 }, 0, 0 },
30         { "pow", "ff", "GLSL.std.450", GLSL450_POW, { 1, 2 }, 0, 0 },
31         { "exp", "f", "GLSL.std.450", GLSL450_EXP, { 1 }, 0, 0 },
32         { "log", "f", "GLSL.std.450", GLSL450_LOG, { 1 }, 0, 0 },
33         { "exp2", "f", "GLSL.std.450", GLSL450_EXP2, { 1 }, 0, 0 },
34         { "log2", "f", "GLSL.std.450", GLSL450_LOG2, { 1 }, 0, 0 },
35         { "sqrt", "f", "GLSL.std.450", GLSL450_SQRT, { 1 }, 0, 0 },
36         { "inversesqrt", "f", "GLSL.std.450", GLSL450_INVERSE_SQRT, { 1 }, 0, 0 },
37         { "abs", "f", "GLSL.std.450", GLSL450_F_ABS, { 1 }, 0, 0 },
38         { "abs", "i", "GLSL.std.450", GLSL450_S_ABS, { 1 }, 0, 0 },
39         { "sign", "f", "GLSL.std.450", GLSL450_F_SIGN, { 1 }, 0, 0 },
40         { "sign", "i", "GLSL.std.450", GLSL450_S_SIGN, { 1 }, 0, 0 },
41         { "floor", "f", "GLSL.std.450", GLSL450_FLOOR, { 1 }, 0, 0 },
42         { "trunc", "f", "GLSL.std.450", GLSL450_TRUNC, { 1 }, 0, 0 },
43         { "round", "f", "GLSL.std.450", GLSL450_ROUND, { 1 }, 0, 0 },
44         { "roundEven", "f", "GLSL.std.450", GLSL450_ROUND_EVEN, { 1 }, 0, 0 },
45         { "ceil", "f", "GLSL.std.450", GLSL450_CEIL, { 1 }, 0, 0 },
46         { "fract", "f", "GLSL.std.450", GLSL450_FRACT, { 1 }, 0, 0 },
47         { "mod", "f", "", OP_F_MOD, { 1, 2 }, 0, 0 },
48         { "min", "ff", "GLSL.std.450", GLSL450_F_MIN, { 1, 2 }, 0, 0 },
49         { "min", "ii", "GLSL.std.450", GLSL450_S_MIN, { 1, 2 }, 0, 0 },
50         { "min", "uu", "GLSL.std.450", GLSL450_U_MIN, { 1, 2 }, 0, 0 },
51         { "max", "ff", "GLSL.std.450", GLSL450_F_MAX, { 1, 2 }, 0, 0 },
52         { "max", "ii", "GLSL.std.450", GLSL450_S_MAX, { 1, 2 }, 0, 0 },
53         { "max", "uu", "GLSL.std.450", GLSL450_U_MAX, { 1, 2 }, 0, 0 },
54         { "clamp", "fff", "GLSL.std.450", GLSL450_F_CLAMP, { 1, 2, 3 }, 0, 0 },
55         { "clamp", "iii", "GLSL.std.450", GLSL450_S_CLAMP, { 1, 2, 3 }, 0, 0 },
56         { "clamp", "uuu", "GLSL.std.450", GLSL450_U_CLAMP, { 1, 2, 3 }, 0, 0 },
57         { "mix", "fff", "GLSL.std.450", GLSL450_F_MIX, { 1, 2, 3 }, 0, 0 },
58         { "mix", "ffb", "", OP_SELECT, { 3, 2, 1 }, 0, 0 },
59         { "mix", "iib", "", OP_SELECT, { 3, 2, 1 }, 0, 0 },
60         { "mix", "uub", "", OP_SELECT, { 3, 2, 1 }, 0, 0 },
61         { "step", "ff", "GLSL.std.450", GLSL450_F_STEP, { 1, 2 }, 0, 0 },
62         { "smoothstep", "fff", "GLSL.std.450", GLSL450_F_SMOOTH_STEP, { 1, 2, 3 }, 0, 0 },
63         { "isnan", "f", "", OP_IS_NAN, { 1 }, 0, 0 },
64         { "isinf", "f", "", OP_IS_INF, { 1 }, 0, 0 },
65         { "fma", "fff", "GLSL.std.450", GLSL450_F_FMA, { 1, 2, 3 }, 0, 0 },
66         { "length", "f", "GLSL.std.450", GLSL450_LENGTH, { 1 }, 0, 0 },
67         { "distance", "ff", "GLSL.std.450", GLSL450_DISTANCE, { 1, 2 }, 0, 0 },
68         { "dot", "ff", "", OP_DOT, { 1, 2 }, 0, 0 },
69         { "cross", "ff", "GLSL.std.450", GLSL450_CROSS, { 1, 2 }, 0, 0 },
70         { "normalize", "f", "GLSL.std.450", GLSL450_NORMALIZE, { 1 }, 0, 0 },
71         { "faceforward", "fff", "GLSL.std.450", GLSL450_FACE_FORWARD, { 1, 2, 3 }, 0, 0 },
72         { "reflect", "ff", "GLSL.std.450", GLSL450_REFLECT, { 1, 2 }, 0, 0 },
73         { "refract", "fff", "GLSL.std.450", GLSL450_REFRACT, { 1, 2, 3 }, 0, 0 },
74         { "matrixCompMult", "ff", "", 0, { 0 }, 0, &SpirVGenerator::visit_builtin_matrix_comp_mult },
75         { "outerProduct", "ff", "", OP_OUTER_PRODUCT, { 1, 2 }, 0, 0 },
76         { "transpose", "f", "", OP_TRANSPOSE, { 1 }, 0, 0 },
77         { "determinant", "f", "GLSL.std.450", GLSL450_DETERMINANT, { 1 }, 0, 0 },
78         { "inverse", "f", "GLSL.std.450", GLSL450_MATRIX_INVERSE, { 1 }, 0, 0 },
79         { "lessThan", "ff", "", OP_F_ORD_LESS_THAN, { 1, 2 }, 0, 0 },
80         { "lessThan", "ii", "", OP_S_LESS_THAN, { 1, 2 }, 0, 0 },
81         { "lessThan", "uu", "", OP_U_LESS_THAN, { 1, 2 }, 0, 0 },
82         { "lessThanEqual", "ff", "", OP_F_ORD_LESS_THAN_EQUAL, { 1, 2 }, 0, 0 },
83         { "lessThanEqual", "ii", "", OP_S_LESS_THAN_EQUAL, { 1, 2 }, 0, 0 },
84         { "lessThanEqual", "uu", "", OP_U_LESS_THAN_EQUAL, { 1, 2 }, 0, 0 },
85         { "greaterThan", "ff", "", OP_F_ORD_GREATER_THAN, { 1, 2 }, 0, 0 },
86         { "greaterThan", "ii", "", OP_S_GREATER_THAN, { 1, 2 }, 0, 0 },
87         { "greaterThan", "uu", "", OP_U_GREATER_THAN, { 1, 2 }, 0, 0 },
88         { "greaterThanEqual", "ff", "", OP_F_ORD_GREATER_THAN_EQUAL, { 1, 2 }, 0, 0 },
89         { "greaterThanEqual", "ii", "", OP_S_GREATER_THAN_EQUAL, { 1, 2 }, 0, 0 },
90         { "greaterThanEqual", "uu", "", OP_U_GREATER_THAN_EQUAL, { 1, 2 }, 0, 0 },
91         { "equal", "ff", "", OP_F_ORD_EQUAL, { 1, 2 }, 0, 0 },
92         { "equal", "ii", "", OP_I_EQUAL, { 1, 2 }, 0, 0 },
93         { "equal", "uu", "", OP_I_EQUAL, { 1, 2 }, 0, 0 },
94         { "notEqual", "ff", "", OP_F_ORD_NOT_EQUAL, { 1, 2 }, 0, 0 },
95         { "notEqual", "ii", "", OP_I_NOT_EQUAL, { 1, 2 }, 0, 0 },
96         { "notEqual", "uu", "", OP_I_NOT_EQUAL, { 1, 2 }, 0, 0 },
97         { "any", "b", "", OP_ANY, { 1 }, 0, 0 },
98         { "all", "b", "", OP_ALL, { 1 }, 0, 0 },
99         { "not", "b", "", OP_LOGICAL_NOT, { 1 }, 0, 0 },
100         { "bitfieldExtract", "iii", "", OP_BIT_FIELD_S_EXTRACT, { 1, 2, 3 }, 0, 0 },
101         { "bitfieldExtract", "uii", "", OP_BIT_FIELD_U_EXTRACT, { 1, 2, 3 }, 0, 0 },
102         { "bitfieldInsert", "iiii", "", OP_BIT_FIELD_INSERT, { 1, 2, 3, 4 }, 0, 0 },
103         { "bitfieldInsert", "uuii", "", OP_BIT_FIELD_INSERT, { 1, 2, 3, 4 }, 0, 0 },
104         { "bitfieldReverse", "i", "", OP_BIT_REVERSE, { 1 }, 0, 0 },
105         { "bitfieldReverse", "u", "", OP_BIT_REVERSE, { 1 }, 0, 0 },
106         { "bitCount", "i", "", OP_BIT_COUNT, { 1 }, 0, 0 },
107         { "findLSB", "i", "GLSL.std.450", GLSL450_FIND_I_LSB, { 1 }, 0, 0 },
108         { "findLSB", "u", "GLSL.std.450", GLSL450_FIND_I_LSB, { 1 }, 0, 0 },
109         { "findMSB", "i", "GLSL.std.450", GLSL450_FIND_S_MSB, { 1 }, 0, 0 },
110         { "findMSB", "u", "GLSL.std.450", GLSL450_FIND_U_MSB, { 1 }, 0, 0 },
111         { "textureSize", "", "", 0, { }, CAP_IMAGE_QUERY, &SpirVGenerator::visit_builtin_texture_query },
112         { "textureQueryLod", "", "", 0, { }, CAP_IMAGE_QUERY, &SpirVGenerator::visit_builtin_texture_query },
113         { "textureQueryLevels", "", "", 0, { }, CAP_IMAGE_QUERY, &SpirVGenerator::visit_builtin_texture_query },
114         { "texture", "", "", 0, { }, 0, &SpirVGenerator::visit_builtin_texture },
115         { "textureLod", "", "", 0, { }, 0, &SpirVGenerator::visit_builtin_texture },
116         { "texelFetch", "", "", 0, { }, 0, &SpirVGenerator::visit_builtin_texel_fetch },
117         { "EmitVertex", "", "", OP_EMIT_VERTEX, { }, 0, 0 },
118         { "EndPrimitive", "", "", OP_END_PRIMITIVE, { }, 0, 0 },
119         { "dFdx", "f", "", OP_DP_DX, { 1 }, 0, 0 },
120         { "dFdy", "f", "", OP_DP_DY, { 1 }, 0, 0 },
121         { "dFdxFine", "f", "", OP_DP_DX_FINE, { 1 }, CAP_DERIVATIVE_CONTROL, 0 },
122         { "dFdyFine", "f", "", OP_DP_DY_FINE, { 1 }, CAP_DERIVATIVE_CONTROL, 0 },
123         { "dFdxCoarse", "f", "", OP_DP_DX_COARSE, { 1 }, CAP_DERIVATIVE_CONTROL, 0 },
124         { "dFdyCoarse", "f", "", OP_DP_DY_COARSE, { 1 }, CAP_DERIVATIVE_CONTROL, 0 },
125         { "fwidth", "f", "", OP_FWIDTH, { 1 }, 0, 0 },
126         { "fwidthFine", "f", "", OP_FWIDTH_FINE, { 1 }, CAP_DERIVATIVE_CONTROL, 0 },
127         { "fwidthCoarse", "f", "", OP_FWIDTH_COARSE, { 1 }, CAP_DERIVATIVE_CONTROL, 0 },
128         { "interpolateAtCentroid", "", "", 0, { }, CAP_INTERPOLATION_FUNCTION, &SpirVGenerator::visit_builtin_interpolate },
129         { "interpolateAtSample", "", "", 0, { }, CAP_INTERPOLATION_FUNCTION, &SpirVGenerator::visit_builtin_interpolate },
130         { "interpolateAtOffset", "", "", 0, { }, CAP_INTERPOLATION_FUNCTION, &SpirVGenerator::visit_builtin_interpolate },
131         { "", "", "", 0, { }, 0, 0 }
132 };
133
134 SpirVGenerator::SpirVGenerator():
135         writer(content)
136 { }
137
138 void SpirVGenerator::apply(Module &module, const Features &f)
139 {
140         features = f;
141         use_capability(CAP_SHADER);
142
143         for(Stage &s: module.stages)
144         {
145                 stage = &s;
146                 interface_layouts.clear();
147                 s.content.visit(*this);
148         }
149
150         writer.finalize(SPIRV_GENERATOR_MSP, next_id);
151 }
152
153 SpirVGenerator::StorageClass SpirVGenerator::get_interface_storage(const string &iface, bool block)
154 {
155         if(iface=="in")
156                 return STORAGE_INPUT;
157         else if(iface=="out")
158                 return STORAGE_OUTPUT;
159         else if(iface=="uniform")
160                 return (block ? STORAGE_UNIFORM : STORAGE_UNIFORM_CONSTANT);
161         else if(iface.empty())
162                 return STORAGE_PRIVATE;
163         else
164                 throw invalid_argument("SpirVGenerator::get_interface_storage");
165 }
166
167 SpirVGenerator::BuiltinSemantic SpirVGenerator::get_builtin_semantic(const string &name)
168 {
169         if(name=="gl_Position")
170                 return BUILTIN_POSITION;
171         else if(name=="gl_PointSize")
172                 return BUILTIN_POINT_SIZE;
173         else if(name=="gl_ClipDistance")
174                 return BUILTIN_CLIP_DISTANCE;
175         else if(name=="gl_VertexID")
176                 return BUILTIN_VERTEX_ID;
177         else if(name=="gl_InstanceID")
178                 return BUILTIN_INSTANCE_ID;
179         else if(name=="gl_PrimitiveID" || name=="gl_PrimitiveIDIn")
180                 return BUILTIN_PRIMITIVE_ID;
181         else if(name=="gl_InvocationID")
182                 return BUILTIN_INVOCATION_ID;
183         else if(name=="gl_Layer")
184                 return BUILTIN_LAYER;
185         else if(name=="gl_FragCoord")
186                 return BUILTIN_FRAG_COORD;
187         else if(name=="gl_PointCoord")
188                 return BUILTIN_POINT_COORD;
189         else if(name=="gl_FrontFacing")
190                 return BUILTIN_FRONT_FACING;
191         else if(name=="gl_SampleId")
192                 return BUILTIN_SAMPLE_ID;
193         else if(name=="gl_SamplePosition")
194                 return BUILTIN_SAMPLE_POSITION;
195         else if(name=="gl_FragDepth")
196                 return BUILTIN_FRAG_DEPTH;
197         else
198                 throw invalid_argument("SpirVGenerator::get_builtin_semantic");
199 }
200
201 void SpirVGenerator::use_capability(Capability cap)
202 {
203         if(used_capabilities.count(cap))
204                 return;
205
206         used_capabilities.insert(cap);
207         writer.write_op(content.capabilities, OP_CAPABILITY, cap);
208 }
209
210 SpirVGenerator::Id SpirVGenerator::import_extension(const string &name)
211 {
212         Id &ext_id = imported_extension_ids[name];
213         if(!ext_id)
214         {
215                 ext_id = next_id++;
216                 writer.begin_op(content.extensions, OP_EXT_INST_IMPORT);
217                 writer.write(ext_id);
218                 writer.write_string(name);
219                 writer.end_op(OP_EXT_INST_IMPORT);
220         }
221         return ext_id;
222 }
223
224 SpirVGenerator::Id SpirVGenerator::get_id(Node &node) const
225 {
226         return get_item(declared_ids, &node).id;
227 }
228
229 SpirVGenerator::Id SpirVGenerator::allocate_id(Node &node, Id type_id)
230 {
231         auto i = declared_ids.find(&node);
232         if(i!=declared_ids.end())
233         {
234                 if(i->second.type_id)
235                         throw key_error(&node);
236                 i->second.type_id = type_id;
237                 return i->second.id;
238         }
239
240         Id id = next_id++;
241         declared_ids.insert(make_pair(&node, Declaration(id, type_id)));
242         return id;
243 }
244
245 SpirVGenerator::Id SpirVGenerator::allocate_forward_id(Node &node)
246 {
247         auto i = declared_ids.find(&node);
248         if(i!=declared_ids.end())
249                 return i->second.id;
250
251         Id id = next_id++;
252         declared_ids.insert(make_pair(&node, Declaration(id, 0)));
253         return id;
254 }
255
256 SpirVGenerator::Id SpirVGenerator::write_constant(Id type_id, Word value, bool spec)
257 {
258         Id const_id = next_id++;
259         if(is_scalar_type(type_id, BasicTypeDeclaration::BOOL))
260         {
261                 Opcode opcode = (value ? (spec ? OP_SPEC_CONSTANT_TRUE : OP_CONSTANT_TRUE) :
262                         (spec ? OP_SPEC_CONSTANT_FALSE : OP_CONSTANT_FALSE));
263                 writer.write_op(content.globals, opcode, type_id, const_id);
264         }
265         else
266         {
267                 Opcode opcode = (spec ? OP_SPEC_CONSTANT : OP_CONSTANT);
268                 writer.write_op(content.globals, opcode, type_id, const_id, value);
269         }
270         return const_id;
271 }
272
273 SpirVGenerator::ConstantKey SpirVGenerator::get_constant_key(Id type_id, const Variant &value)
274 {
275         if(value.check_type<bool>())
276                 return ConstantKey(type_id, value.value<bool>());
277         else if(value.check_type<int>())
278                 return ConstantKey(type_id, value.value<int>());
279         else if(value.check_type<unsigned>())
280                 return ConstantKey(type_id, value.value<unsigned>());
281         else if(value.check_type<float>())
282                 return ConstantKey(type_id, value.value<float>());
283         else
284                 throw invalid_argument("SpirVGenerator::get_constant_key");
285 }
286
287 SpirVGenerator::Id SpirVGenerator::get_constant_id(Id type_id, const Variant &value)
288 {
289         ConstantKey key = get_constant_key(type_id, value);
290         Id &const_id = constant_ids[key];
291         if(!const_id)
292                 const_id = write_constant(type_id, key.int_value, false);
293         return const_id;
294 }
295
296 SpirVGenerator::Id SpirVGenerator::get_vector_constant_id(Id type_id, unsigned size, Id scalar_id)
297 {
298         Id &const_id = constant_ids[get_constant_key(type_id, static_cast<int>(scalar_id))];
299         if(!const_id)
300         {
301                 const_id = next_id++;
302                 writer.begin_op(content.globals, OP_CONSTANT_COMPOSITE, 3+size);
303                 writer.write(type_id);
304                 writer.write(const_id);
305                 for(unsigned i=0; i<size; ++i)
306                         writer.write(scalar_id);
307                 writer.end_op(OP_CONSTANT_COMPOSITE);
308         }
309         return const_id;
310 }
311
312 SpirVGenerator::Id SpirVGenerator::get_standard_type_id(BasicTypeDeclaration::Kind kind, unsigned size, bool sign)
313 {
314         Id base_id = (size>1 ? get_standard_type_id(kind, 1, sign) : 0);
315         Id &type_id = standard_type_ids[base_id ? TypeKey(base_id, size) : TypeKey(kind, sign)];
316         if(!type_id)
317         {
318                 type_id = next_id++;
319                 if(size>1)
320                         writer.write_op(content.globals, OP_TYPE_VECTOR, type_id, base_id, size);
321                 else if(kind==BasicTypeDeclaration::VOID)
322                         writer.write_op(content.globals, OP_TYPE_VOID, type_id);
323                 else if(kind==BasicTypeDeclaration::BOOL)
324                         writer.write_op(content.globals, OP_TYPE_BOOL, type_id);
325                 else if(kind==BasicTypeDeclaration::INT)
326                         writer.write_op(content.globals, OP_TYPE_INT, type_id, 32, sign);
327                 else if(kind==BasicTypeDeclaration::FLOAT)
328                         writer.write_op(content.globals, OP_TYPE_FLOAT, type_id, 32);
329                 else
330                         throw invalid_argument("SpirVGenerator::get_standard_type_id");
331         }
332         return type_id;
333 }
334
335 bool SpirVGenerator::is_scalar_type(Id type_id, BasicTypeDeclaration::Kind kind) const
336 {
337         auto i = standard_type_ids.find(TypeKey(kind, true));
338         return (i!=standard_type_ids.end() && i->second==type_id);
339 }
340
341 SpirVGenerator::Id SpirVGenerator::get_array_type_id(TypeDeclaration &base_type, Id size_id, bool extended_align)
342 {
343         Id base_type_id = get_id(base_type);
344         Id &array_type_id = array_type_ids[TypeKey(base_type_id, extended_align*0x400000 | size_id)];
345         if(!array_type_id)
346         {
347                 array_type_id = next_id++;
348                 if(size_id)
349                         writer.write_op(content.globals, OP_TYPE_ARRAY, array_type_id, base_type_id, size_id);
350                 else
351                         writer.write_op(content.globals, OP_TYPE_RUNTIME_ARRAY, array_type_id, base_type_id);
352
353                 unsigned stride = MemoryRequirementsCalculator().apply(base_type).stride;
354                 if(extended_align)
355                         stride = (stride+15)&~15U;
356                 writer.write_op_decorate(array_type_id, DECO_ARRAY_STRIDE, stride);
357         }
358
359         return array_type_id;
360 }
361
362 SpirVGenerator::Id SpirVGenerator::get_pointer_type_id(Id type_id, StorageClass storage)
363 {
364         Id &ptr_type_id = pointer_type_ids[TypeKey(type_id, storage)];
365         if(!ptr_type_id)
366         {
367                 ptr_type_id = next_id++;
368                 writer.write_op(content.globals, OP_TYPE_POINTER, ptr_type_id, storage, type_id);
369         }
370         return ptr_type_id;
371 }
372
373 SpirVGenerator::Id SpirVGenerator::get_variable_type_id(const VariableDeclaration &var)
374 {
375         if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(var.type_declaration))
376                 if(basic->kind==BasicTypeDeclaration::ARRAY)
377                 {
378                         if(!var.array_size)
379                                 throw logic_error("array without size");
380
381                         SetFlag set_const(constant_expression);
382                         r_expression_result_id = 0;
383                         var.array_size->visit(*this);
384                         return get_array_type_id(*basic->base_type, r_expression_result_id, basic->extended_alignment);
385                 }
386
387         return get_id(*var.type_declaration);
388 }
389
390 SpirVGenerator::Id SpirVGenerator::get_load_id(VariableDeclaration &var)
391 {
392         Id &load_result_id = variable_load_ids[&var];
393         if(!load_result_id)
394         {
395                 load_result_id = next_id++;
396                 writer.write_op(content.function_body, OP_LOAD, get_variable_type_id(var), load_result_id, get_id(var));
397         }
398         return load_result_id;
399 }
400
401 void SpirVGenerator::prune_loads(Id min_id)
402 {
403         for(auto i=variable_load_ids.begin(); i!=variable_load_ids.end(); )
404         {
405                 if(i->second>=min_id)
406                         variable_load_ids.erase(i++);
407                 else
408                         ++i;
409         }
410 }
411
412 SpirVGenerator::Id SpirVGenerator::begin_expression(Opcode opcode, Id type_id, unsigned n_args)
413 {
414         bool has_result = (opcode==OP_FUNCTION_CALL || !is_scalar_type(type_id, BasicTypeDeclaration::VOID));
415         if(!constant_expression)
416         {
417                 if(!current_function)
418                         throw internal_error("non-constant expression outside a function");
419
420                 writer.begin_op(content.function_body, opcode, (n_args ? 1+has_result*2+n_args : 0));
421         }
422         else if(opcode==OP_COMPOSITE_CONSTRUCT)
423                 writer.begin_op(content.globals, (spec_constant ? OP_SPEC_CONSTANT_COMPOSITE : OP_CONSTANT_COMPOSITE),
424                         (n_args ? 1+has_result*2+n_args : 0));
425         else if(!spec_constant)
426                 throw internal_error("invalid non-specialization constant expression");
427         else
428                 writer.begin_op(content.globals, OP_SPEC_CONSTANT_OP, (n_args ? 2+has_result*2+n_args : 0));
429
430         Id result_id = next_id++;
431         if(has_result)
432         {
433                 writer.write(type_id);
434                 writer.write(result_id);
435         }
436         if(spec_constant && opcode!=OP_COMPOSITE_CONSTRUCT)
437                 writer.write(opcode);
438
439         return result_id;
440 }
441
442 void SpirVGenerator::end_expression(Opcode opcode)
443 {
444         if(constant_expression)
445                 opcode = (opcode==OP_COMPOSITE_CONSTRUCT ? spec_constant ? OP_SPEC_CONSTANT_COMPOSITE : OP_CONSTANT_COMPOSITE : OP_SPEC_CONSTANT_OP);
446         writer.end_op(opcode);
447 }
448
449 SpirVGenerator::Id SpirVGenerator::write_expression(Opcode opcode, Id type_id, Id arg_id)
450 {
451         Id result_id = begin_expression(opcode, type_id, 1);
452         writer.write(arg_id);
453         end_expression(opcode);
454         return result_id;
455 }
456
457 SpirVGenerator::Id SpirVGenerator::write_expression(Opcode opcode, Id type_id, Id left_id, Id right_id)
458 {
459         Id result_id = begin_expression(opcode, type_id, 2);
460         writer.write(left_id);
461         writer.write(right_id);
462         end_expression(opcode);
463         return result_id;
464 }
465
466 void SpirVGenerator::write_deconstruct(Id elem_type_id, Id composite_id, Id *elem_ids, unsigned n_elems)
467 {
468         for(unsigned i=0; i<n_elems; ++i)
469         {
470                 elem_ids[i] = begin_expression(OP_COMPOSITE_EXTRACT, elem_type_id, 2);
471                 writer.write(composite_id);
472                 writer.write(i);
473                 end_expression(OP_COMPOSITE_EXTRACT);
474         }
475 }
476
477 SpirVGenerator::Id SpirVGenerator::write_construct(Id type_id, const Id *elem_ids, unsigned n_elems)
478 {
479         Id result_id = begin_expression(OP_COMPOSITE_CONSTRUCT, type_id, n_elems);
480         for(unsigned i=0; i<n_elems; ++i)
481                 writer.write(elem_ids[i]);
482         end_expression(OP_COMPOSITE_CONSTRUCT);
483
484         return result_id;
485 }
486
487 void SpirVGenerator::visit(Block &block)
488 {
489         for(const RefPtr<Statement> &s: block.body)
490                 s->visit(*this);
491 }
492
493 void SpirVGenerator::visit(Literal &literal)
494 {
495         Id type_id = get_id(*literal.type);
496         if(spec_constant)
497                 r_expression_result_id = write_constant(type_id, get_constant_key(type_id, literal.value).int_value, true);
498         else
499                 r_expression_result_id = get_constant_id(type_id, literal.value);
500         r_constant_result = true;
501 }
502
503 void SpirVGenerator::visit(VariableReference &var)
504 {
505         if(constant_expression || var.declaration->constant)
506         {
507                 if(!var.declaration->constant)
508                         throw internal_error("reference to non-constant variable in constant context");
509
510                 r_expression_result_id = get_id(*var.declaration);
511                 r_constant_result = true;
512                 return;
513         }
514         else if(!current_function)
515                 throw internal_error("non-constant context outside a function");
516
517         r_constant_result = false;
518         if(composite_access)
519         {
520                 r_expression_result_id = 0;
521                 if(!assignment_source_id)
522                 {
523                         auto i = variable_load_ids.find(var.declaration);
524                         if(i!=variable_load_ids.end())
525                                 r_expression_result_id = i->second;
526                 }
527                 if(!r_expression_result_id)
528                         r_composite_base = var.declaration;
529         }
530         else if(assignment_source_id)
531         {
532                 writer.write_op(content.function_body, OP_STORE, get_id(*var.declaration), assignment_source_id);
533                 variable_load_ids[var.declaration] = assignment_source_id;
534                 r_expression_result_id = assignment_source_id;
535         }
536         else
537                 r_expression_result_id = get_load_id(*var.declaration);
538 }
539
540 void SpirVGenerator::generate_composite_access(TypeDeclaration &result_type)
541 {
542         Opcode opcode;
543         Id result_type_id = get_id(result_type);
544         Id access_type_id = result_type_id;
545         if(r_composite_base)
546         {
547                 if(constant_expression)
548                         throw internal_error("composite access through pointer in constant context");
549
550                 Id int32_type_id = get_standard_type_id(BasicTypeDeclaration::INT, 1);
551                 for(unsigned &i: r_composite_chain)
552                         i = (i<0x400000 ? get_constant_id(int32_type_id, static_cast<int>(i)) : i&0x3FFFFF);
553
554                 /* Find the storage class of the base and obtain appropriate pointer type
555                 for the result. */
556                 const Declaration &base_decl = get_item(declared_ids, r_composite_base);
557                 auto i = pointer_type_ids.begin();
558                 for(; (i!=pointer_type_ids.end() && i->second!=base_decl.type_id); ++i) ;
559                 if(i==pointer_type_ids.end())
560                         throw internal_error("could not find storage class");
561                 access_type_id = get_pointer_type_id(result_type_id, static_cast<StorageClass>(i->first.detail));
562
563                 opcode = OP_ACCESS_CHAIN;
564         }
565         else if(assignment_source_id)
566                 throw internal_error("assignment to temporary composite");
567         else
568         {
569                 for(unsigned &i: r_composite_chain)
570                         for(auto j=constant_ids.begin(); (i>=0x400000 && j!=constant_ids.end()); ++j)
571                                 if(j->second==(i&0x3FFFFF))
572                                         i = j->first.int_value;
573
574                 opcode = OP_COMPOSITE_EXTRACT;
575         }
576
577         Id access_id = begin_expression(opcode, access_type_id, 1+r_composite_chain.size());
578         writer.write(r_composite_base_id);
579         for(unsigned i: r_composite_chain)
580                 writer.write(i);
581         end_expression(opcode);
582
583         r_constant_result = false;
584         if(r_composite_base)
585         {
586                 if(assignment_source_id)
587                 {
588                         writer.write_op(content.function_body, OP_STORE, access_id, assignment_source_id);
589                         r_expression_result_id = assignment_source_id;
590                 }
591                 else
592                         r_expression_result_id = write_expression(OP_LOAD, result_type_id, access_id);
593         }
594         else
595                 r_expression_result_id = access_id;
596 }
597
598 void SpirVGenerator::visit_composite(Expression &base_expr, unsigned index, TypeDeclaration &type)
599 {
600         if(!composite_access)
601         {
602                 r_composite_base = 0;
603                 r_composite_base_id = 0;
604                 r_composite_chain.clear();
605         }
606
607         {
608                 SetFlag set_composite(composite_access);
609                 base_expr.visit(*this);
610         }
611
612         if(!r_composite_base_id)
613                 r_composite_base_id = (r_composite_base ? get_id(*r_composite_base) : r_expression_result_id);
614
615         r_composite_chain.push_back(index);
616         if(!composite_access)
617                 generate_composite_access(type);
618         else
619                 r_expression_result_id = 0;
620 }
621
622 void SpirVGenerator::visit_isolated(Expression &expr)
623 {
624         SetForScope<Id> clear_assign(assignment_source_id, 0);
625         SetFlag clear_composite(composite_access, false);
626         SetForScope<Node *> clear_base(r_composite_base, 0);
627         SetForScope<Id> clear_base_id(r_composite_base_id, 0);
628         vector<unsigned> saved_chain;
629         swap(saved_chain, r_composite_chain);
630         expr.visit(*this);
631         swap(saved_chain, r_composite_chain);
632 }
633
634 void SpirVGenerator::visit(MemberAccess &memacc)
635 {
636         visit_composite(*memacc.left, memacc.index, *memacc.type);
637 }
638
639 void SpirVGenerator::visit(Swizzle &swizzle)
640 {
641         if(swizzle.count==1)
642                 visit_composite(*swizzle.left, swizzle.components[0], *swizzle.type);
643         else if(assignment_source_id)
644         {
645                 const BasicTypeDeclaration &basic = dynamic_cast<const BasicTypeDeclaration &>(*swizzle.left->type);
646
647                 unsigned mask = 0;
648                 for(unsigned i=0; i<swizzle.count; ++i)
649                         mask |= 1<<swizzle.components[i];
650
651                 visit_isolated(*swizzle.left);
652
653                 Id combined_id = begin_expression(OP_VECTOR_SHUFFLE, get_id(*swizzle.left->type), 2+basic.size);
654                 writer.write(r_expression_result_id);
655                 writer.write(assignment_source_id);
656                 for(unsigned i=0; i<basic.size; ++i)
657                         writer.write(i+((mask>>i)&1)*basic.size);
658                 end_expression(OP_VECTOR_SHUFFLE);
659
660                 SetForScope<Id> set_assign(assignment_source_id, combined_id);
661                 swizzle.left->visit(*this);
662
663                 r_expression_result_id = combined_id;
664         }
665         else
666         {
667                 swizzle.left->visit(*this);
668                 Id left_id = r_expression_result_id;
669
670                 r_expression_result_id = begin_expression(OP_VECTOR_SHUFFLE, get_id(*swizzle.type), 2+swizzle.count);
671                 writer.write(left_id);
672                 writer.write(left_id);
673                 for(unsigned i=0; i<swizzle.count; ++i)
674                         writer.write(swizzle.components[i]);
675                 end_expression(OP_VECTOR_SHUFFLE);
676         }
677         r_constant_result = false;
678 }
679
680 void SpirVGenerator::visit(UnaryExpression &unary)
681 {
682         if(composite_access)
683                 return visit_isolated(unary);
684
685         unary.expression->visit(*this);
686
687         char oper = unary.oper->token[0];
688         char oper2 = unary.oper->token[1];
689         if(oper=='+' && !oper2)
690                 return;
691
692         BasicTypeDeclaration &basic = dynamic_cast<BasicTypeDeclaration &>(*unary.expression->type);
693         BasicTypeDeclaration &elem = *get_element_type(basic);
694
695         if(constant_expression && elem.kind!=BasicTypeDeclaration::BOOL && elem.kind!=BasicTypeDeclaration::INT)
696                 /* SPIR-V allows constant operations on floating-point values only for
697                 OpenGL kernels. */
698                 throw internal_error("invalid operands for constant unary expression");
699
700         Id result_type_id = get_id(*unary.type);
701         Opcode opcode = OP_NOP;
702
703         r_constant_result = false;
704         if(oper=='!')
705                 opcode = OP_LOGICAL_NOT;
706         else if(oper=='~')
707                 opcode = OP_NOT;
708         else if(oper=='-' && !oper2)
709         {
710                 opcode = (elem.kind==BasicTypeDeclaration::INT ? OP_S_NEGATE : OP_F_NEGATE);
711
712                 if(basic.kind==BasicTypeDeclaration::MATRIX)
713                 {
714                         Id column_type_id = get_id(*basic.base_type);
715                         unsigned n_columns = basic.size&0xFFFF;
716                         Id column_ids[4];
717                         write_deconstruct(column_type_id, r_expression_result_id, column_ids, n_columns);
718                         for(unsigned i=0; i<n_columns; ++i)
719                                 column_ids[i] = write_expression(opcode, column_type_id, column_ids[i]);
720                         r_expression_result_id = write_construct(result_type_id, column_ids, n_columns);
721                         return;
722                 }
723         }
724         else if((oper=='+' || oper=='-') && oper2==oper)
725         {
726                 if(constant_expression)
727                         throw internal_error("increment or decrement in constant expression");
728
729                 Id one_id = 0;
730                 if(elem.kind==BasicTypeDeclaration::INT)
731                 {
732                         opcode = (oper=='+' ? OP_I_ADD : OP_I_SUB);
733                         one_id = get_constant_id(get_id(elem), 1);
734                 }
735                 else if(elem.kind==BasicTypeDeclaration::FLOAT)
736                 {
737                         opcode = (oper=='+' ? OP_F_ADD : OP_F_SUB);
738                         one_id = get_constant_id(get_id(elem), 1.0f);
739                 }
740                 else
741                         throw internal_error("invalid increment/decrement");
742
743                 if(basic.kind==BasicTypeDeclaration::VECTOR)
744                         one_id = get_vector_constant_id(result_type_id, basic.size, one_id);
745
746                 Id post_value_id = write_expression(opcode, result_type_id, r_expression_result_id, one_id);
747
748                 SetForScope<Id> set_assign(assignment_source_id, post_value_id);
749                 unary.expression->visit(*this);
750
751                 r_expression_result_id = (unary.oper->type==Operator::POSTFIX ? r_expression_result_id : post_value_id);
752                 return;
753         }
754
755         if(opcode==OP_NOP)
756                 throw internal_error("unknown unary operator");
757
758         r_expression_result_id = write_expression(opcode, result_type_id, r_expression_result_id);
759 }
760
761 void SpirVGenerator::visit(BinaryExpression &binary)
762 {
763         char oper = binary.oper->token[0];
764         if(oper=='[')
765         {
766                 visit_isolated(*binary.right);
767                 return visit_composite(*binary.left, 0x400000|r_expression_result_id, *binary.type);
768         }
769         else if(composite_access)
770                 return visit_isolated(binary);
771
772         if(assignment_source_id)
773                 throw internal_error("invalid binary expression in assignment target");
774
775         BasicTypeDeclaration &basic_left = dynamic_cast<BasicTypeDeclaration &>(*binary.left->type);
776         BasicTypeDeclaration &basic_right = dynamic_cast<BasicTypeDeclaration &>(*binary.right->type);
777         // Expression resolver ensures that element types are the same
778         BasicTypeDeclaration &elem = *get_element_type(basic_left);
779
780         if(constant_expression && elem.kind!=BasicTypeDeclaration::BOOL && elem.kind!=BasicTypeDeclaration::INT)
781                 /* SPIR-V allows constant operations on floating-point values only for
782                 OpenGL kernels. */
783                 throw internal_error("invalid operands for constant binary expression");
784
785         binary.left->visit(*this);
786         Id left_id = r_expression_result_id;
787         binary.right->visit(*this);
788         Id right_id = r_expression_result_id;
789
790         Id result_type_id = get_id(*binary.type);
791         Opcode opcode = OP_NOP;
792         bool swap_operands = false;
793
794         r_constant_result = false;
795
796         char oper2 = binary.oper->token[1];
797         if((oper=='<' || oper=='>') && oper2!=oper)
798         {
799                 if(basic_left.kind==BasicTypeDeclaration::INT)
800                 {
801                         if(basic_left.sign)
802                                 opcode = (oper=='<' ? (oper2=='=' ? OP_S_LESS_THAN_EQUAL : OP_S_LESS_THAN) :
803                                         (oper2=='=' ? OP_S_GREATER_THAN_EQUAL : OP_S_GREATER_THAN));
804                         else
805                                 opcode = (oper=='<' ? (oper2=='=' ? OP_U_LESS_THAN_EQUAL : OP_U_LESS_THAN) :
806                                         (oper2=='=' ? OP_U_GREATER_THAN_EQUAL : OP_U_GREATER_THAN));
807                 }
808                 else if(basic_left.kind==BasicTypeDeclaration::FLOAT)
809                         opcode = (oper=='<' ? (oper2=='=' ? OP_F_ORD_LESS_THAN_EQUAL : OP_F_ORD_LESS_THAN) :
810                                 (oper2=='=' ? OP_F_ORD_GREATER_THAN_EQUAL : OP_F_ORD_GREATER_THAN));
811         }
812         else if((oper=='=' || oper=='!') && oper2=='=')
813         {
814                 if(elem.kind==BasicTypeDeclaration::BOOL)
815                         opcode = (oper=='=' ? OP_LOGICAL_EQUAL : OP_LOGICAL_NOT_EQUAL);
816                 else if(elem.kind==BasicTypeDeclaration::INT)
817                         opcode = (oper=='=' ? OP_I_EQUAL : OP_I_NOT_EQUAL);
818                 else if(elem.kind==BasicTypeDeclaration::FLOAT)
819                         opcode = (oper=='=' ? OP_F_ORD_EQUAL : OP_F_ORD_NOT_EQUAL);
820
821                 if(opcode!=OP_NOP && basic_left.base_type)
822                 {
823                         /* The SPIR-V equality operations produce component-wise results, but
824                         GLSL operators return a single boolean.  Use the any/all operations to
825                         combine the results. */
826                         Opcode combine_op = (oper=='!' ? OP_ANY : OP_ALL);
827                         unsigned n_elems = basic_left.size&0xFFFF;
828                         Id bool_vec_type_id = get_standard_type_id(BasicTypeDeclaration::BOOL, n_elems);
829
830                         Id compare_id = 0;
831                         if(basic_left.kind==BasicTypeDeclaration::VECTOR)
832                                 compare_id = write_expression(opcode, bool_vec_type_id, left_id, right_id);
833                         else if(basic_left.kind==BasicTypeDeclaration::MATRIX)
834                         {
835                                 Id column_type_id = get_id(*basic_left.base_type);
836                                 Id column_ids[8];
837                                 write_deconstruct(column_type_id, left_id, column_ids, n_elems);
838                                 write_deconstruct(column_type_id, right_id, column_ids+4, n_elems);
839
840                                 Id column_bvec_type_id = get_standard_type_id(BasicTypeDeclaration::BOOL, basic_left.size>>16);
841                                 for(unsigned i=0; i<n_elems; ++i)
842                                 {
843                                         compare_id = write_expression(opcode, column_bvec_type_id, column_ids[i], column_ids[4+i]);
844                                         column_ids[i] = write_expression(combine_op, result_type_id, compare_id);;
845                                 }
846
847                                 compare_id = write_construct(bool_vec_type_id, column_ids, n_elems);
848                         }
849
850                         if(compare_id)
851                                 r_expression_result_id = write_expression(combine_op, result_type_id, compare_id);
852                         return;
853                 }
854         }
855         else if(oper2=='&' && elem.kind==BasicTypeDeclaration::BOOL)
856                 opcode = OP_LOGICAL_AND;
857         else if(oper2=='|' && elem.kind==BasicTypeDeclaration::BOOL)
858                 opcode = OP_LOGICAL_OR;
859         else if(oper2=='^' && elem.kind==BasicTypeDeclaration::BOOL)
860                 opcode = OP_LOGICAL_NOT_EQUAL;
861         else if(oper=='&' && elem.kind==BasicTypeDeclaration::INT)
862                 opcode = OP_BITWISE_AND;
863         else if(oper=='|' && elem.kind==BasicTypeDeclaration::INT)
864                 opcode = OP_BITWISE_OR;
865         else if(oper=='^' && elem.kind==BasicTypeDeclaration::INT)
866                 opcode = OP_BITWISE_XOR;
867         else if(oper=='<' && oper2==oper && elem.kind==BasicTypeDeclaration::INT)
868                 opcode = OP_SHIFT_LEFT_LOGICAL;
869         else if(oper=='>' && oper2==oper && elem.kind==BasicTypeDeclaration::INT)
870                 opcode = OP_SHIFT_RIGHT_ARITHMETIC;
871         else if(oper=='%' && elem.kind==BasicTypeDeclaration::INT)
872                 opcode = (elem.sign ? OP_S_MOD : OP_U_MOD);
873         else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')
874         {
875                 Opcode elem_op = OP_NOP;
876                 if(elem.kind==BasicTypeDeclaration::INT)
877                 {
878                         if(oper=='/')
879                                 elem_op = (elem.sign ? OP_S_DIV : OP_U_DIV);
880                         else
881                                 elem_op = (oper=='+' ? OP_I_ADD : oper=='-' ? OP_I_SUB : OP_I_MUL);
882                 }
883                 else if(elem.kind==BasicTypeDeclaration::FLOAT)
884                         elem_op = (oper=='+' ? OP_F_ADD : oper=='-' ? OP_F_SUB : oper=='*' ? OP_F_MUL : OP_F_DIV);
885
886                 if(oper=='*' && (basic_left.base_type || basic_right.base_type) && elem.kind==BasicTypeDeclaration::FLOAT)
887                 {
888                         /* Multiplication between floating-point vectors and matrices has
889                         dedicated operations. */
890                         if(basic_left.kind==BasicTypeDeclaration::MATRIX && basic_right.kind==BasicTypeDeclaration::MATRIX)
891                                 opcode = OP_MATRIX_TIMES_MATRIX;
892                         else if(basic_left.kind==BasicTypeDeclaration::MATRIX || basic_right.kind==BasicTypeDeclaration::MATRIX)
893                         {
894                                 if(basic_left.kind==BasicTypeDeclaration::VECTOR)
895                                         opcode = OP_VECTOR_TIMES_MATRIX;
896                                 else if(basic_right.kind==BasicTypeDeclaration::VECTOR)
897                                         opcode = OP_MATRIX_TIMES_VECTOR;
898                                 else
899                                 {
900                                         opcode = OP_MATRIX_TIMES_SCALAR;
901                                         swap_operands = (basic_right.kind==BasicTypeDeclaration::MATRIX);
902                                 }
903                         }
904                         else if(basic_left.kind==BasicTypeDeclaration::VECTOR && basic_right.kind==BasicTypeDeclaration::VECTOR)
905                                 opcode = elem_op;
906                         else
907                         {
908                                 opcode = OP_VECTOR_TIMES_SCALAR;
909                                 swap_operands = (basic_right.kind==BasicTypeDeclaration::VECTOR);
910                         }
911                 }
912                 else if((basic_left.base_type!=0)!=(basic_right.base_type!=0))
913                 {
914                         /* One operand is scalar and the other is a vector or a matrix.
915                         Expand the scalar to a vector of appropriate size. */
916                         Id &scalar_id = (basic_left.base_type ? right_id : left_id);
917                         BasicTypeDeclaration *vector_type = (basic_left.base_type ? &basic_left : &basic_right);
918                         if(vector_type->kind==BasicTypeDeclaration::MATRIX)
919                                 vector_type = dynamic_cast<BasicTypeDeclaration *>(vector_type->base_type);
920                         Id vector_type_id = get_id(*vector_type);
921
922                         Id expanded_id = begin_expression(OP_COMPOSITE_CONSTRUCT, vector_type_id, vector_type->size);
923                         for(unsigned i=0; i<vector_type->size; ++i)
924                                 writer.write(scalar_id);
925                         end_expression(OP_COMPOSITE_CONSTRUCT);
926
927                         scalar_id = expanded_id;
928
929                         if(basic_left.kind==BasicTypeDeclaration::MATRIX || basic_right.kind==BasicTypeDeclaration::MATRIX)
930                         {
931                                 // Apply matrix operation column-wise.
932                                 Id matrix_id = (basic_left.base_type ? left_id : right_id);
933
934                                 Id column_ids[4];
935                                 unsigned n_columns = (basic_left.base_type ? basic_left.size : basic_right.size)&0xFFFF;
936                                 write_deconstruct(vector_type_id, matrix_id, column_ids, n_columns);
937
938                                 for(unsigned i=0; i<n_columns; ++i)
939                                         column_ids[i] = write_expression(elem_op, vector_type_id, column_ids[i], expanded_id);
940
941                                 r_expression_result_id = write_construct(result_type_id, column_ids, n_columns);
942                                 return;
943                         }
944                         else
945                                 opcode = elem_op;
946                 }
947                 else if(basic_left.kind==BasicTypeDeclaration::MATRIX && basic_right.kind==BasicTypeDeclaration::MATRIX)
948                 {
949                         if(oper=='*')
950                                 throw internal_error("non-float matrix multiplication");
951
952                         /* Other operations involving matrices need to be performed
953                         column-wise. */
954                         Id column_type_id = get_id(*basic_left.base_type);
955                         Id column_ids[8];
956
957                         unsigned n_columns = basic_left.size&0xFFFF;
958                         write_deconstruct(column_type_id, left_id, column_ids, n_columns);
959                         write_deconstruct(column_type_id, right_id, column_ids+4, n_columns);
960
961                         for(unsigned i=0; i<n_columns; ++i)
962                                 column_ids[i] = write_expression(elem_op, column_type_id, column_ids[i], column_ids[4+i]);
963
964                         r_expression_result_id = write_construct(result_type_id, column_ids, n_columns);
965                         return;
966                 }
967                 else if(basic_left.kind==basic_right.kind)
968                         // Both operands are either scalars or vectors.
969                         opcode = elem_op;
970         }
971
972         if(opcode==OP_NOP)
973                 throw internal_error("unknown binary operator");
974
975         if(swap_operands)
976                 swap(left_id, right_id);
977
978         r_expression_result_id = write_expression(opcode, result_type_id, left_id, right_id);
979 }
980
981 void SpirVGenerator::visit(Assignment &assign)
982 {
983         if(assign.oper->token[0]!='=')
984                 visit(static_cast<BinaryExpression &>(assign));
985         else
986                 assign.right->visit(*this);
987
988         SetForScope<Id> set_assign(assignment_source_id, r_expression_result_id);
989         assign.left->visit(*this);
990         r_constant_result = false;
991 }
992
993 void SpirVGenerator::visit(TernaryExpression &ternary)
994 {
995         if(composite_access)
996                 return visit_isolated(ternary);
997         if(constant_expression)
998         {
999                 ternary.condition->visit(*this);
1000                 Id condition_id = r_expression_result_id;
1001                 ternary.true_expr->visit(*this);
1002                 Id true_result_id = r_expression_result_id;
1003                 ternary.false_expr->visit(*this);
1004                 Id false_result_id = r_expression_result_id;
1005
1006                 r_expression_result_id = begin_expression(OP_SELECT, get_id(*ternary.type), 3);
1007                 writer.write(condition_id);
1008                 writer.write(true_result_id);
1009                 writer.write(false_result_id);
1010                 end_expression(OP_SELECT);
1011
1012                 return;
1013         }
1014
1015         ternary.condition->visit(*this);
1016         Id condition_id = r_expression_result_id;
1017
1018         Id true_label_id = next_id++;
1019         Id false_label_id = next_id++;
1020         Id merge_block_id = next_id++;
1021         writer.write_op(content.function_body, OP_SELECTION_MERGE, merge_block_id, 0);  // Selection control (none)
1022         writer.write_op(content.function_body, OP_BRANCH_CONDITIONAL, condition_id, true_label_id, false_label_id);
1023
1024         writer.write_op_label(true_label_id);
1025         ternary.true_expr->visit(*this);
1026         Id true_result_id = r_expression_result_id;
1027         true_label_id = writer.get_current_block();
1028         writer.write_op(content.function_body, OP_BRANCH, merge_block_id);
1029
1030         writer.write_op_label(false_label_id);
1031         ternary.false_expr->visit(*this);
1032         Id false_result_id = r_expression_result_id;
1033         false_label_id = writer.get_current_block();
1034
1035         writer.write_op_label(merge_block_id);
1036         r_expression_result_id = begin_expression(OP_PHI, get_id(*ternary.type), 4);
1037         writer.write(true_result_id);
1038         writer.write(true_label_id);
1039         writer.write(false_result_id);
1040         writer.write(false_label_id);
1041         end_expression(OP_PHI);
1042
1043         r_constant_result = false;
1044 }
1045
1046 void SpirVGenerator::visit(FunctionCall &call)
1047 {
1048         if(assignment_source_id)
1049                 throw internal_error("assignment to function call");
1050         else if(composite_access)
1051                 return visit_isolated(call);
1052         else if(call.constructor && call.arguments.size()==1 && call.arguments[0]->type==call.type)
1053                 return call.arguments[0]->visit(*this);
1054
1055         vector<Id> argument_ids;
1056         argument_ids.reserve(call.arguments.size());
1057         bool all_args_const = true;
1058         for(const RefPtr<Expression> &a: call.arguments)
1059         {
1060                 a->visit(*this);
1061                 argument_ids.push_back(r_expression_result_id);
1062                 all_args_const &= r_constant_result;
1063         }
1064
1065         if(constant_expression && (!call.constructor || !all_args_const))
1066                 throw internal_error("function call in constant expression");
1067
1068         Id result_type_id = get_id(*call.type);
1069         r_constant_result = false;
1070
1071         if(call.constructor)
1072                 visit_constructor(call, argument_ids, all_args_const);
1073         else if(call.declaration->source==BUILTIN_SOURCE)
1074         {
1075                 string arg_types;
1076                 for(const RefPtr<Expression> &a: call.arguments)
1077                         if(BasicTypeDeclaration *basic_arg = dynamic_cast<BasicTypeDeclaration *>(a->type))
1078                         {
1079                                 BasicTypeDeclaration &elem_arg = *get_element_type(*basic_arg);
1080                                 switch(elem_arg.kind)
1081                                 {
1082                                 case BasicTypeDeclaration::BOOL: arg_types += 'b'; break;
1083                                 case BasicTypeDeclaration::INT: arg_types += (elem_arg.sign ? 'i' : 'u'); break;
1084                                 case BasicTypeDeclaration::FLOAT: arg_types += 'f'; break;
1085                                 default: arg_types += '?';
1086                                 }
1087                         }
1088
1089                 const BuiltinFunctionInfo *builtin_info;
1090                 for(builtin_info=builtin_functions; builtin_info->function[0]; ++builtin_info)
1091                         if(builtin_info->function==call.name && (!builtin_info->arg_types[0] || builtin_info->arg_types==arg_types))
1092                                 break;
1093
1094                 if(builtin_info->capability)
1095                         use_capability(static_cast<Capability>(builtin_info->capability));
1096
1097                 if(builtin_info->opcode)
1098                 {
1099                         Opcode opcode;
1100                         if(builtin_info->extension[0])
1101                         {
1102                                 opcode = OP_EXT_INST;
1103                                 Id ext_id = import_extension(builtin_info->extension);
1104
1105                                 r_expression_result_id = begin_expression(opcode, result_type_id);
1106                                 writer.write(ext_id);
1107                                 writer.write(builtin_info->opcode);
1108                         }
1109                         else
1110                         {
1111                                 opcode = static_cast<Opcode>(builtin_info->opcode);
1112                                 r_expression_result_id = begin_expression(opcode, result_type_id);
1113                         }
1114
1115                         for(unsigned i=0; i<call.arguments.size(); ++i)
1116                         {
1117                                 if(!builtin_info->arg_order[i] || builtin_info->arg_order[i]>argument_ids.size())
1118                                         throw internal_error("invalid builtin function info");
1119                                 writer.write(argument_ids[builtin_info->arg_order[i]-1]);
1120                         }
1121
1122                         end_expression(opcode);
1123                 }
1124                 else if(builtin_info->handler)
1125                         (this->*(builtin_info->handler))(call, argument_ids);
1126                 else
1127                         throw internal_error("unknown builtin function "+call.name);
1128         }
1129         else
1130         {
1131                 r_expression_result_id = begin_expression(OP_FUNCTION_CALL, result_type_id, 1+call.arguments.size());
1132                 writer.write(get_id(*call.declaration->definition));
1133                 for(Id i: argument_ids)
1134                         writer.write(i);
1135                 end_expression(OP_FUNCTION_CALL);
1136
1137                 // Any global variables the called function uses might have changed value
1138                 set<Node *> dependencies = DependencyCollector().apply(*call.declaration->definition);
1139                 for(Node *n: dependencies)
1140                         if(const VariableDeclaration *var = dynamic_cast<const VariableDeclaration *>(n))
1141                                 variable_load_ids.erase(var);
1142         }
1143 }
1144
1145 void SpirVGenerator::visit_constructor(FunctionCall &call, const vector<Id> &argument_ids, bool all_args_const)
1146 {
1147         Id result_type_id = get_id(*call.type);
1148
1149         BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(call.type);
1150         if(!basic)
1151         {
1152                 if(dynamic_cast<const StructDeclaration *>(call.type))
1153                         r_expression_result_id = write_construct(result_type_id, &argument_ids[0], argument_ids.size());
1154                 else
1155                         throw internal_error("unconstructable type "+call.name);
1156                 return;
1157         }
1158
1159         SetFlag set_const(constant_expression, constant_expression || all_args_const);
1160
1161         BasicTypeDeclaration &elem = *get_element_type(*basic);
1162         BasicTypeDeclaration &basic_arg0 = dynamic_cast<BasicTypeDeclaration &>(*call.arguments[0]->type);
1163         BasicTypeDeclaration &elem_arg0 = *get_element_type(basic_arg0);
1164
1165         if(basic->kind==BasicTypeDeclaration::MATRIX)
1166         {
1167                 Id col_type_id = get_id(*basic->base_type);
1168                 unsigned n_columns = basic->size&0xFFFF;
1169                 unsigned n_rows = basic->size>>16;
1170
1171                 Id column_ids[4];
1172                 if(call.arguments.size()==1)
1173                 {
1174                         // Construct diagonal matrix from a single scalar.
1175                         Id zero_id = get_constant_id(get_id(elem), 0.0f);
1176                         for(unsigned i=0; i<n_columns; ++i)
1177                         {
1178                                 column_ids[i] = begin_expression(OP_COMPOSITE_CONSTRUCT, col_type_id, n_rows);
1179                                 for(unsigned j=0; j<n_rows; ++j)
1180                                         writer.write(j==i ? argument_ids[0] : zero_id);
1181                                 end_expression(OP_COMPOSITE_CONSTRUCT);
1182                         }
1183                 }
1184                 else
1185                         // Construct a matrix from column vectors
1186                         copy(argument_ids.begin(), argument_ids.begin()+n_columns, column_ids);
1187
1188                 r_expression_result_id = write_construct(result_type_id, column_ids, n_columns);
1189         }
1190         else if(basic->kind==BasicTypeDeclaration::VECTOR && (call.arguments.size()>1 || basic_arg0.kind!=BasicTypeDeclaration::VECTOR))
1191         {
1192                 /* There's either a single scalar argument or multiple arguments
1193                 which make up the vector's components. */
1194                 if(call.arguments.size()==1)
1195                 {
1196                         r_expression_result_id = begin_expression(OP_COMPOSITE_CONSTRUCT, result_type_id);
1197                         for(unsigned i=0; i<basic->size; ++i)
1198                                 writer.write(argument_ids[0]);
1199                         end_expression(OP_COMPOSITE_CONSTRUCT);
1200                 }
1201                 else
1202                         r_expression_result_id = write_construct(result_type_id, &argument_ids[0], argument_ids.size());
1203         }
1204         else if(elem.kind==BasicTypeDeclaration::BOOL)
1205         {
1206                 if(constant_expression)
1207                         throw internal_error("unconverted constant");
1208
1209                 // Conversion to boolean is implemented as comparing against zero.
1210                 Id number_type_id = get_id(elem_arg0);
1211                 Id zero_id = (elem_arg0.kind==BasicTypeDeclaration::FLOAT ?
1212                         get_constant_id(number_type_id, 0.0f) : get_constant_id(number_type_id, 0));
1213                 if(basic_arg0.kind==BasicTypeDeclaration::VECTOR)
1214                         zero_id = get_vector_constant_id(get_id(basic_arg0), basic_arg0.size, zero_id);
1215
1216                 Opcode opcode = (elem_arg0.kind==BasicTypeDeclaration::FLOAT ? OP_F_ORD_NOT_EQUAL : OP_I_NOT_EQUAL);
1217                 r_expression_result_id = write_expression(opcode, result_type_id, argument_ids[0], zero_id);
1218         }
1219         else if(elem_arg0.kind==BasicTypeDeclaration::BOOL)
1220         {
1221                 if(constant_expression)
1222                         throw internal_error("unconverted constant");
1223
1224                 /* Conversion from boolean is implemented as selecting from zero
1225                 or one. */
1226                 Id number_type_id = get_id(elem);
1227                 Id zero_id = (elem.kind==BasicTypeDeclaration::FLOAT ?
1228                         get_constant_id(number_type_id, 0.0f) : get_constant_id(number_type_id, 0));
1229                 Id one_id = (elem.kind==BasicTypeDeclaration::FLOAT ?
1230                         get_constant_id(number_type_id, 1.0f) : get_constant_id(number_type_id, 1));
1231                 if(basic->kind==BasicTypeDeclaration::VECTOR)
1232                 {
1233                         zero_id = get_vector_constant_id(get_id(*basic), basic->size, zero_id);
1234                         one_id = get_vector_constant_id(get_id(*basic), basic->size, one_id);
1235                 }
1236
1237                 r_expression_result_id = begin_expression(OP_SELECT, result_type_id, 3);
1238                 writer.write(argument_ids[0]);
1239                 writer.write(zero_id);
1240                 writer.write(one_id);
1241                 end_expression(OP_SELECT);
1242         }
1243         else
1244         {
1245                 if(constant_expression)
1246                         throw internal_error("unconverted constant");
1247
1248                 // Scalar or vector conversion between types of equal size.
1249                 Opcode opcode;
1250                 if(elem.kind==BasicTypeDeclaration::INT && elem_arg0.kind==BasicTypeDeclaration::FLOAT)
1251                         opcode = (elem.sign ? OP_CONVERT_F_TO_S : OP_CONVERT_F_TO_U);
1252                 else if(elem.kind==BasicTypeDeclaration::FLOAT && elem_arg0.kind==BasicTypeDeclaration::INT)
1253                         opcode = (elem_arg0.sign ? OP_CONVERT_S_TO_F : OP_CONVERT_U_TO_F);
1254                 else if(elem.kind==BasicTypeDeclaration::INT && elem_arg0.kind==BasicTypeDeclaration::INT)
1255                         opcode = OP_BITCAST;
1256                 else
1257                         throw internal_error("invalid conversion");
1258
1259                 r_expression_result_id = write_expression(opcode, result_type_id, argument_ids[0]);
1260         }
1261 }
1262
1263 void SpirVGenerator::visit_builtin_matrix_comp_mult(FunctionCall &call, const vector<Id> &argument_ids)
1264 {
1265         if(argument_ids.size()!=2)
1266                 throw internal_error("invalid matrixCompMult call");
1267
1268         const BasicTypeDeclaration &basic_arg0 = dynamic_cast<const BasicTypeDeclaration &>(*call.arguments[0]->type);
1269         Id column_type_id = get_id(*basic_arg0.base_type);
1270         Id column_ids[8];
1271
1272         unsigned n_columns = basic_arg0.size&0xFFFF;
1273         write_deconstruct(column_type_id, argument_ids[0], column_ids, n_columns);
1274         write_deconstruct(column_type_id, argument_ids[1], column_ids+4, n_columns);
1275
1276         for(unsigned i=0; i<n_columns; ++i)
1277                 column_ids[i] = write_expression(OP_F_MUL, column_type_id, column_ids[i], column_ids[4+i]);
1278
1279         r_expression_result_id = write_construct(get_id(*call.type), column_ids, n_columns);
1280 }
1281
1282 void SpirVGenerator::visit_builtin_texture_query(FunctionCall &call, const vector<Id> &argument_ids)
1283 {
1284         if(argument_ids.size()<1)
1285                 throw internal_error("invalid texture query call");
1286
1287         Opcode opcode;
1288         if(call.name=="textureSize")
1289                 opcode = OP_IMAGE_QUERY_SIZE_LOD;
1290         else if(call.name=="textureQueryLod")
1291                 opcode = OP_IMAGE_QUERY_LOD;
1292         else if(call.name=="textureQueryLevels")
1293                 opcode = OP_IMAGE_QUERY_LEVELS;
1294         else
1295                 throw internal_error("invalid texture query call");
1296
1297         ImageTypeDeclaration &image_arg0 = dynamic_cast<ImageTypeDeclaration &>(*call.arguments[0]->type);
1298
1299         Id image_id;
1300         if(image_arg0.sampled)
1301         {
1302                 Id image_type_id = get_item(image_type_ids, get_id(image_arg0));
1303                 image_id = write_expression(OP_IMAGE, image_type_id, argument_ids[0]);
1304         }
1305         else
1306                 image_id = argument_ids[0];
1307
1308         Id result_type_id = get_id(*call.type);
1309         r_expression_result_id = begin_expression(opcode, result_type_id, argument_ids.size());
1310         writer.write(image_id);
1311         for(unsigned i=1; i<argument_ids.size(); ++i)
1312                 writer.write(argument_ids[i]);
1313         end_expression(opcode);
1314 }
1315
1316 void SpirVGenerator::visit_builtin_texture(FunctionCall &call, const vector<Id> &argument_ids)
1317 {
1318         if(argument_ids.size()<2)
1319                 throw internal_error("invalid texture sampling call");
1320
1321         bool explicit_lod = (stage->type!=Stage::FRAGMENT || call.name=="textureLod");
1322         Id lod_id = (!explicit_lod ? 0 : call.name=="textureLod" ? argument_ids.back() :
1323                 get_constant_id(get_standard_type_id(BasicTypeDeclaration::FLOAT, 1), 0.0f));
1324
1325         const ImageTypeDeclaration &image = dynamic_cast<const ImageTypeDeclaration &>(*call.arguments[0]->type);
1326
1327         Opcode opcode;
1328         Id result_type_id = get_id(*call.type);
1329         Id dref_id = 0;
1330         if(image.shadow)
1331         {
1332                 if(argument_ids.size()==2)
1333                 {
1334                         const BasicTypeDeclaration &basic_arg1 = dynamic_cast<const BasicTypeDeclaration &>(*call.arguments[1]->type);
1335                         dref_id = begin_expression(OP_COMPOSITE_EXTRACT, get_id(*basic_arg1.base_type), 2);
1336                         writer.write(argument_ids.back());
1337                         writer.write(basic_arg1.size-1);
1338                         end_expression(OP_COMPOSITE_EXTRACT);
1339                 }
1340                 else
1341                         dref_id = argument_ids[2];
1342
1343                 opcode = (explicit_lod ? OP_IMAGE_SAMPLE_DREF_EXPLICIT_LOD : OP_IMAGE_SAMPLE_DREF_IMPLICIT_LOD);
1344                 r_expression_result_id = begin_expression(opcode, result_type_id, 3+explicit_lod*2);
1345         }
1346         else
1347         {
1348                 opcode = (explicit_lod ? OP_IMAGE_SAMPLE_EXPLICIT_LOD : OP_IMAGE_SAMPLE_IMPLICIT_LOD);
1349                 r_expression_result_id = begin_expression(opcode, result_type_id, 2+explicit_lod*2);
1350         }
1351
1352         for(unsigned i=0; i<2; ++i)
1353                 writer.write(argument_ids[i]);
1354         if(dref_id)
1355                 writer.write(dref_id);
1356         if(explicit_lod)
1357         {
1358                 writer.write(2);  // Lod
1359                 writer.write(lod_id);
1360         }
1361
1362         end_expression(opcode);
1363 }
1364
1365 void SpirVGenerator::visit_builtin_texel_fetch(FunctionCall &call, const vector<Id> &argument_ids)
1366 {
1367         if(argument_ids.size()!=3)
1368                 throw internal_error("invalid texelFetch call");
1369
1370         r_expression_result_id = begin_expression(OP_IMAGE_FETCH, get_id(*call.type), 4);
1371         for(unsigned i=0; i<2; ++i)
1372                 writer.write(argument_ids[i]);
1373         writer.write(2);  // Lod
1374         writer.write(argument_ids.back());
1375         end_expression(OP_IMAGE_FETCH);
1376 }
1377
1378 void SpirVGenerator::visit_builtin_interpolate(FunctionCall &call, const vector<Id> &argument_ids)
1379 {
1380         if(argument_ids.size()<1)
1381                 throw internal_error("invalid interpolate call");
1382         const VariableReference *var = dynamic_cast<const VariableReference *>(call.arguments[0].get());
1383         if(!var || !var->declaration || var->declaration->interface!="in")
1384                 throw internal_error("invalid interpolate call");
1385
1386         SpirVGlslStd450Opcode opcode;
1387         if(call.name=="interpolateAtCentroid")
1388                 opcode = GLSL450_INTERPOLATE_AT_CENTROID;
1389         else if(call.name=="interpolateAtSample")
1390                 opcode = GLSL450_INTERPOLATE_AT_SAMPLE;
1391         else if(call.name=="interpolateAtOffset")
1392                 opcode = GLSL450_INTERPOLATE_AT_OFFSET;
1393         else
1394                 throw internal_error("invalid interpolate call");
1395
1396         Id ext_id = import_extension("GLSL.std.450");
1397         r_expression_result_id = begin_expression(OP_EXT_INST, get_id(*call.type));
1398         writer.write(ext_id);
1399         writer.write(opcode);
1400         writer.write(get_id(*var->declaration));
1401         for(auto i=argument_ids.begin(); ++i!=argument_ids.end(); )
1402                 writer.write(*i);
1403         end_expression(OP_EXT_INST);
1404 }
1405
1406 void SpirVGenerator::visit(ExpressionStatement &expr)
1407 {
1408         expr.expression->visit(*this);
1409 }
1410
1411 void SpirVGenerator::visit(InterfaceLayout &layout)
1412 {
1413         interface_layouts.push_back(&layout);
1414 }
1415
1416 bool SpirVGenerator::check_duplicate_type(TypeDeclaration &type)
1417 {
1418         for(const auto &kvp: declared_ids)
1419                 if(TypeDeclaration *type2 = dynamic_cast<TypeDeclaration *>(kvp.first))
1420                         if(TypeComparer().apply(type, *type2))
1421                         {
1422                                 insert_unique(declared_ids, &type, kvp.second);
1423                                 return true;
1424                         }
1425
1426         return false;
1427 }
1428
1429 bool SpirVGenerator::check_standard_type(BasicTypeDeclaration &basic)
1430 {
1431         const BasicTypeDeclaration *elem = (basic.kind==BasicTypeDeclaration::VECTOR ?
1432                 dynamic_cast<const BasicTypeDeclaration *>(basic.base_type) : &basic);
1433         if(!elem || elem->base_type)
1434                 return false;
1435         if((elem->kind==BasicTypeDeclaration::INT || elem->kind==BasicTypeDeclaration::FLOAT) && elem->size!=32)
1436                 return false;
1437
1438         Id standard_id = get_standard_type_id(elem->kind, (basic.kind==BasicTypeDeclaration::VECTOR ? basic.size : 1), elem->sign);
1439         insert_unique(declared_ids, &basic, Declaration(standard_id, 0));
1440         writer.write_op_name(standard_id, basic.name);
1441
1442         return true;
1443 }
1444
1445 void SpirVGenerator::visit(BasicTypeDeclaration &basic)
1446 {
1447         if(check_standard_type(basic))
1448                 return;
1449         if(check_duplicate_type(basic))
1450                 return;
1451         // Alias types shouldn't exist at this point and arrays are handled elsewhere
1452         if(basic.kind==BasicTypeDeclaration::ALIAS || basic.kind==BasicTypeDeclaration::ARRAY)
1453                 return;
1454
1455         Id type_id = allocate_id(basic, 0);
1456         writer.write_op_name(type_id, basic.name);
1457
1458         switch(basic.kind)
1459         {
1460         case BasicTypeDeclaration::INT:
1461                 writer.write_op(content.globals, OP_TYPE_INT, type_id, basic.size, basic.sign);
1462                 break;
1463         case BasicTypeDeclaration::FLOAT:
1464                 writer.write_op(content.globals, OP_TYPE_FLOAT, type_id, basic.size);
1465                 break;
1466         case BasicTypeDeclaration::VECTOR:
1467                 writer.write_op(content.globals, OP_TYPE_VECTOR, type_id, get_id(*basic.base_type), basic.size);
1468                 break;
1469         case BasicTypeDeclaration::MATRIX:
1470                 writer.write_op(content.globals, OP_TYPE_MATRIX, type_id, get_id(*basic.base_type), basic.size&0xFFFF);
1471                 break;
1472         default:
1473                 throw internal_error("unknown basic type");
1474         }
1475 }
1476
1477 void SpirVGenerator::visit(ImageTypeDeclaration &image)
1478 {
1479         if(check_duplicate_type(image))
1480                 return;
1481
1482         Id type_id = allocate_id(image, 0);
1483
1484         Id image_id = (image.sampled ? next_id++ : type_id);
1485         writer.begin_op(content.globals, OP_TYPE_IMAGE, 9);
1486         writer.write(image_id);
1487         writer.write(get_id(*image.base_type));
1488         writer.write(image.dimensions-1);
1489         writer.write(image.shadow);
1490         writer.write(image.array);
1491         writer.write(false);  // Multisample
1492         writer.write(image.sampled ? 1 : 2);
1493         writer.write(0);  // Format (unknown)
1494         writer.end_op(OP_TYPE_IMAGE);
1495
1496         if(image.sampled)
1497         {
1498                 writer.write_op_name(type_id, image.name);
1499                 writer.write_op(content.globals, OP_TYPE_SAMPLED_IMAGE, type_id, image_id);
1500                 insert_unique(image_type_ids, type_id, image_id);
1501         }
1502
1503         if(image.dimensions==ImageTypeDeclaration::ONE)
1504                 use_capability(image.sampled ? CAP_SAMPLED_1D : CAP_IMAGE_1D);
1505         else if(image.dimensions==ImageTypeDeclaration::CUBE && image.array)
1506                 use_capability(image.sampled ? CAP_SAMPLED_CUBE_ARRAY : CAP_IMAGE_CUBE_ARRAY);
1507 }
1508
1509 void SpirVGenerator::visit(StructDeclaration &strct)
1510 {
1511         if(check_duplicate_type(strct))
1512                 return;
1513
1514         Id type_id = allocate_id(strct, 0);
1515         writer.write_op_name(type_id, (strct.block_name.empty() ? strct.name : strct.block_name));
1516
1517         if(!strct.block_name.empty())
1518                 writer.write_op_decorate(type_id, DECO_BLOCK);
1519
1520         bool builtin = !strct.block_name.compare(0, 3, "gl_");
1521         vector<Id> member_type_ids;
1522         member_type_ids.reserve(strct.members.body.size());
1523         for(const RefPtr<Statement> &s: strct.members.body)
1524         {
1525                 const VariableDeclaration *var = dynamic_cast<const VariableDeclaration *>(s.get());
1526                 if(!var)
1527                         continue;
1528
1529                 unsigned index = member_type_ids.size();
1530                 member_type_ids.push_back(get_variable_type_id(*var));
1531
1532                 writer.write_op_member_name(type_id, index, var->name);
1533
1534                 if(builtin)
1535                 {
1536                         BuiltinSemantic semantic = get_builtin_semantic(var->name);
1537                         writer.write_op_member_decorate(type_id, index, DECO_BUILTIN, semantic);
1538                 }
1539                 else
1540                 {
1541                         if(var->layout)
1542                         {
1543                                 for(const Layout::Qualifier &q: var->layout->qualifiers)
1544                                 {
1545                                         if(q.name=="offset")
1546                                                 writer.write_op_member_decorate(type_id, index, DECO_OFFSET, q.value);
1547                                         else if(q.name=="column_major")
1548                                                 writer.write_op_member_decorate(type_id, index, DECO_COL_MAJOR);
1549                                         else if(q.name=="row_major")
1550                                                 writer.write_op_member_decorate(type_id, index, DECO_ROW_MAJOR);
1551                                 }
1552                         }
1553
1554                         const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(var->type_declaration);
1555                         while(basic && basic->kind==BasicTypeDeclaration::ARRAY)
1556                                 basic = dynamic_cast<const BasicTypeDeclaration *>(basic->base_type);
1557                         if(basic && basic->kind==BasicTypeDeclaration::MATRIX)
1558                         {
1559                                 unsigned stride = MemoryRequirementsCalculator().apply(*basic->base_type).stride;
1560                                 writer.write_op_member_decorate(type_id, index, DECO_MATRIX_STRIDE, stride);
1561                         }
1562                 }
1563         }
1564
1565         writer.begin_op(content.globals, OP_TYPE_STRUCT);
1566         writer.write(type_id);
1567         for(Id i: member_type_ids)
1568                 writer.write(i);
1569         writer.end_op(OP_TYPE_STRUCT);
1570 }
1571
1572 void SpirVGenerator::visit(VariableDeclaration &var)
1573 {
1574         Id type_id = get_variable_type_id(var);
1575         Id var_id;
1576
1577         if(var.constant)
1578         {
1579                 if(!var.init_expression)
1580                         throw internal_error("const variable without initializer");
1581
1582                 int spec_id = get_layout_value(var.layout.get(), "constant_id");
1583                 Id *spec_var_id = (spec_id>=0 ? &declared_spec_ids[spec_id] : 0);
1584                 if(spec_id>=0 && *spec_var_id)
1585                 {
1586                         insert_unique(declared_ids, &var, Declaration(*spec_var_id, type_id));
1587                         return;
1588                 }
1589
1590                 SetFlag set_const(constant_expression);
1591                 SetFlag set_spec(spec_constant, spec_id>=0);
1592                 r_expression_result_id = 0;
1593                 var.init_expression->visit(*this);
1594                 var_id = r_expression_result_id;
1595                 insert_unique(declared_ids, &var, Declaration(var_id, type_id));
1596                 if(spec_id>=0)
1597                 {
1598                         writer.write_op_decorate(var_id, DECO_SPEC_ID, spec_id);
1599                         *spec_var_id = var_id;
1600                 }
1601         }
1602         else
1603         {
1604                 bool push_const = has_layout_qualifier(var.layout.get(), "push_constant");
1605
1606                 StorageClass storage;
1607                 if(current_function)
1608                         storage = STORAGE_FUNCTION;
1609                 else if(push_const)
1610                         storage = STORAGE_PUSH_CONSTANT;
1611                 else
1612                         storage = get_interface_storage(var.interface, var.block_declaration);
1613
1614                 Id ptr_type_id = get_pointer_type_id(type_id, storage);
1615                 if(var.interface=="uniform")
1616                 {
1617                         Id &uni_id = declared_uniform_ids[var.block_declaration ? "b"+var.block_declaration->block_name : "v"+var.name];
1618                         if(uni_id)
1619                         {
1620                                 insert_unique(declared_ids, &var, Declaration(uni_id, ptr_type_id));
1621                                 return;
1622                         }
1623
1624                         uni_id = var_id = allocate_id(var, ptr_type_id);
1625                 }
1626                 else
1627                         var_id = allocate_id(var, (var.constant ? type_id : ptr_type_id));
1628
1629                 Id init_id = 0;
1630                 if(var.init_expression)
1631                 {
1632                         SetFlag set_const(constant_expression, !current_function);
1633                         r_expression_result_id = 0;
1634                         r_constant_result = false;
1635                         var.init_expression->visit(*this);
1636                         init_id = r_expression_result_id;
1637                 }
1638
1639                 vector<Word> &target = (current_function ? content.locals : content.globals);
1640                 writer.begin_op(target, OP_VARIABLE, 4+(init_id && !current_function));
1641                 writer.write(ptr_type_id);
1642                 writer.write(var_id);
1643                 writer.write(storage);
1644                 if(init_id && !current_function)
1645                         writer.write(init_id);
1646                 writer.end_op(OP_VARIABLE);
1647
1648                 if(var.layout)
1649                 {
1650                         for(const Layout::Qualifier &q: var.layout->qualifiers)
1651                         {
1652                                 if(q.name=="location")
1653                                         writer.write_op_decorate(var_id, DECO_LOCATION, q.value);
1654                                 else if(q.name=="set")
1655                                         writer.write_op_decorate(var_id, DECO_DESCRIPTOR_SET, q.value);
1656                                 else if(q.name=="binding")
1657                                         writer.write_op_decorate(var_id, DECO_BINDING, q.value);
1658                         }
1659                 }
1660                 if(!var.block_declaration && !var.name.compare(0, 3, "gl_"))
1661                 {
1662                         BuiltinSemantic semantic = get_builtin_semantic(var.name);
1663                         writer.write_op_decorate(var_id, DECO_BUILTIN, semantic);
1664                 }
1665
1666                 if(init_id && current_function)
1667                 {
1668                         writer.write_op(content.function_body, OP_STORE, var_id, init_id);
1669                         variable_load_ids[&var] = init_id;
1670                 }
1671         }
1672
1673         if(var.name.find(' ')==string::npos)
1674                 writer.write_op_name(var_id, var.name);
1675 }
1676
1677 void SpirVGenerator::visit_entry_point(FunctionDeclaration &func, Id func_id)
1678 {
1679         writer.begin_op(content.entry_points, OP_ENTRY_POINT);
1680         switch(stage->type)
1681         {
1682         case Stage::VERTEX: writer.write(0); break;
1683         case Stage::GEOMETRY: writer.write(3); break;
1684         case Stage::FRAGMENT: writer.write(4); break;
1685         default: throw internal_error("unknown stage");
1686         }
1687         writer.write(func_id);
1688         writer.write_string(func.name);
1689
1690         set<Node *> dependencies = DependencyCollector().apply(func);
1691         for(Node *n: dependencies)
1692                 if(const VariableDeclaration *var = dynamic_cast<const VariableDeclaration *>(n))
1693                         if(!var->interface.empty())
1694                                 writer.write(get_id(*n));
1695
1696         writer.end_op(OP_ENTRY_POINT);
1697
1698         if(stage->type==Stage::FRAGMENT)
1699         {
1700                 SpirVExecutionMode origin = (features.target_api==VULKAN ? EXEC_ORIGIN_UPPER_LEFT : EXEC_ORIGIN_LOWER_LEFT);
1701                 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, origin);
1702         }
1703         else if(stage->type==Stage::GEOMETRY)
1704         {
1705                 use_capability(CAP_GEOMETRY);
1706                 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_INVOCATIONS, 1);
1707         }
1708
1709         for(const InterfaceLayout *i: interface_layouts)
1710         {
1711                 for(const Layout::Qualifier &q: i->layout.qualifiers)
1712                 {
1713                         if(q.name=="point")
1714                                 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id,
1715                                         (i->interface=="in" ? EXEC_INPUT_POINTS : EXEC_OUTPUT_POINTS));
1716                         else if(q.name=="lines")
1717                                 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_INPUT_LINES);
1718                         else if(q.name=="lines_adjacency")
1719                                 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_INPUT_LINES_ADJACENCY);
1720                         else if(q.name=="triangles")
1721                                 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_TRIANGLES);
1722                         else if(q.name=="triangles_adjacency")
1723                                 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_INPUT_TRIANGLES_ADJACENCY);
1724                         else if(q.name=="line_strip")
1725                                 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_OUTPUT_LINE_STRIP);
1726                         else if(q.name=="triangle_strip")
1727                                 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_OUTPUT_TRIANGLE_STRIP);
1728                         else if(q.name=="max_vertices")
1729                                 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_OUTPUT_VERTICES, q.value);
1730                 }
1731         }
1732 }
1733
1734 void SpirVGenerator::visit(FunctionDeclaration &func)
1735 {
1736         if(func.source==BUILTIN_SOURCE)
1737                 return;
1738         else if(func.definition!=&func)
1739         {
1740                 if(func.definition)
1741                         allocate_forward_id(*func.definition);
1742                 return;
1743         }
1744
1745         Id return_type_id = get_id(*func.return_type_declaration);
1746         vector<unsigned> param_type_ids;
1747         param_type_ids.reserve(func.parameters.size());
1748         for(const RefPtr<VariableDeclaration> &p: func.parameters)
1749                 param_type_ids.push_back(get_variable_type_id(*p));
1750
1751         string sig_with_return = func.return_type+func.signature;
1752         Id &type_id = function_type_ids[sig_with_return];
1753         if(!type_id)
1754         {
1755                 type_id = next_id++;
1756                 writer.begin_op(content.globals, OP_TYPE_FUNCTION);
1757                 writer.write(type_id);
1758                 writer.write(return_type_id);
1759                 for(unsigned i: param_type_ids)
1760                         writer.write(i);
1761                 writer.end_op(OP_TYPE_FUNCTION);
1762
1763                 writer.write_op_name(type_id, sig_with_return);
1764         }
1765
1766         Id func_id = allocate_id(func, type_id);
1767         writer.write_op_name(func_id, func.name+func.signature);
1768
1769         if(func.name=="main")
1770                 visit_entry_point(func, func_id);
1771
1772         writer.begin_op(content.functions, OP_FUNCTION, 5);
1773         writer.write(return_type_id);
1774         writer.write(func_id);
1775         writer.write(0);  // Function control flags (none)
1776         writer.write(type_id);
1777         writer.end_op(OP_FUNCTION);
1778
1779         for(unsigned i=0; i<func.parameters.size(); ++i)
1780         {
1781                 Id param_id = allocate_id(*func.parameters[i], param_type_ids[i]);
1782                 writer.write_op(content.functions, OP_FUNCTION_PARAMETER, param_type_ids[i], param_id);
1783                 writer.write_op_name(param_id, func.parameters[i]->name);
1784                 // TODO This is probably incorrect if the parameter is assigned to.
1785                 variable_load_ids[func.parameters[i].get()] = param_id;
1786         }
1787
1788         reachable = true;
1789         writer.begin_function_body(next_id++);
1790         SetForScope<FunctionDeclaration *> set_func(current_function, &func);
1791         func.body.visit(*this);
1792
1793         if(writer.get_current_block())
1794         {
1795                 if(!reachable)
1796                         writer.write_op(content.function_body, OP_UNREACHABLE);
1797                 else
1798                 {
1799                         const BasicTypeDeclaration *basic_return = dynamic_cast<const BasicTypeDeclaration *>(func.return_type_declaration);
1800                         if(basic_return && basic_return->kind==BasicTypeDeclaration::VOID)
1801                                 writer.write_op(content.function_body, OP_RETURN);
1802                         else
1803                                 throw internal_error("missing return in non-void function");
1804                 }
1805         }
1806         writer.end_function_body();
1807         variable_load_ids.clear();
1808 }
1809
1810 void SpirVGenerator::visit(Conditional &cond)
1811 {
1812         cond.condition->visit(*this);
1813
1814         Id true_label_id = next_id++;
1815         Id merge_block_id = next_id++;
1816         Id false_label_id = (cond.else_body.body.empty() ? merge_block_id : next_id++);
1817         writer.write_op(content.function_body, OP_SELECTION_MERGE, merge_block_id, 0);  // Selection control (none)
1818         writer.write_op(content.function_body, OP_BRANCH_CONDITIONAL, r_expression_result_id, true_label_id, false_label_id);
1819
1820         std::map<const VariableDeclaration *, Id> saved_load_ids = variable_load_ids;
1821
1822         writer.write_op_label(true_label_id);
1823         cond.body.visit(*this);
1824         if(writer.get_current_block())
1825                 writer.write_op(content.function_body, OP_BRANCH, merge_block_id);
1826
1827         bool reachable_if_true = reachable;
1828
1829         reachable = true;
1830         if(!cond.else_body.body.empty())
1831         {
1832                 swap(saved_load_ids, variable_load_ids);
1833                 writer.write_op_label(false_label_id);
1834                 cond.else_body.visit(*this);
1835                 reachable |= reachable_if_true;
1836         }
1837
1838         writer.write_op_label(merge_block_id);
1839         prune_loads(true_label_id);
1840 }
1841
1842 void SpirVGenerator::visit(Iteration &iter)
1843 {
1844         if(iter.init_statement)
1845                 iter.init_statement->visit(*this);
1846
1847         for(Node *n: AssignmentCollector().apply(iter))
1848                 if(VariableDeclaration *var = dynamic_cast<VariableDeclaration *>(n))
1849                         variable_load_ids.erase(var);
1850
1851         Id header_id = next_id++;
1852         Id continue_id = next_id++;
1853         Id merge_block_id = next_id++;
1854
1855         SetForScope<Id> set_merge(loop_merge_block_id, merge_block_id);
1856         SetForScope<Id> set_continue(loop_continue_target_id, continue_id);
1857
1858         writer.write_op_label(header_id);
1859         writer.write_op(content.function_body, OP_LOOP_MERGE, merge_block_id, continue_id, 0);  // Loop control (none)
1860
1861         Id body_id = next_id++;
1862         if(iter.condition)
1863         {
1864                 writer.write_op_label(next_id++);
1865                 iter.condition->visit(*this);
1866                 writer.write_op(content.function_body, OP_BRANCH_CONDITIONAL, r_expression_result_id, body_id, merge_block_id);
1867         }
1868
1869         writer.write_op_label(body_id);
1870         iter.body.visit(*this);
1871
1872         writer.write_op_label(continue_id);
1873         if(iter.loop_expression)
1874                 iter.loop_expression->visit(*this);
1875         writer.write_op(content.function_body, OP_BRANCH, header_id);
1876
1877         writer.write_op_label(merge_block_id);
1878         prune_loads(header_id);
1879         reachable = true;
1880 }
1881
1882 void SpirVGenerator::visit(Return &ret)
1883 {
1884         if(ret.expression)
1885         {
1886                 ret.expression->visit(*this);
1887                 writer.write_op(content.function_body, OP_RETURN_VALUE, r_expression_result_id);
1888         }
1889         else
1890                 writer.write_op(content.function_body, OP_RETURN);
1891         reachable = false;
1892 }
1893
1894 void SpirVGenerator::visit(Jump &jump)
1895 {
1896         if(jump.keyword=="discard")
1897                 writer.write_op(content.function_body, OP_KILL);
1898         else if(jump.keyword=="break")
1899                 writer.write_op(content.function_body, OP_BRANCH, loop_merge_block_id);
1900         else if(jump.keyword=="continue")
1901                 writer.write_op(content.function_body, OP_BRANCH, loop_continue_target_id);
1902         else
1903                 throw internal_error("unknown jump");
1904         reachable = false;
1905 }
1906
1907
1908 SpirVGenerator::TypeKey::TypeKey(BasicTypeDeclaration::Kind kind, bool sign):
1909         type_id(0)
1910 {
1911         switch(kind)
1912         {
1913         case BasicTypeDeclaration::VOID: detail = 'v'; break;
1914         case BasicTypeDeclaration::BOOL: detail = 'b'; break;
1915         case BasicTypeDeclaration::INT: detail = (sign ? 'i' : 'u'); break;
1916         case BasicTypeDeclaration::FLOAT: detail = 'f'; break;
1917         default: throw invalid_argument("TypeKey::TypeKey");
1918         }
1919 }
1920
1921 bool SpirVGenerator::TypeKey::operator<(const TypeKey &other) const
1922 {
1923         if(type_id!=other.type_id)
1924                 return type_id<other.type_id;
1925         return detail<other.detail;
1926 }
1927
1928
1929 bool SpirVGenerator::ConstantKey::operator<(const ConstantKey &other) const
1930 {
1931         if(type_id!=other.type_id)
1932                 return type_id<other.type_id;
1933         return int_value<other.int_value;
1934 }
1935
1936 } // namespace SL
1937 } // namespace GL
1938 } // namespace Msp