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