1 #include <msp/core/algorithm.h>
2 #include <msp/core/maputils.h>
3 #include <msp/core/raii.h>
13 const SpirVGenerator::BuiltinFunctionInfo SpirVGenerator::builtin_functions[] =
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 }
134 SpirVGenerator::SpirVGenerator():
138 void SpirVGenerator::apply(Module &module, const Features &f)
141 use_capability(CAP_SHADER);
143 for(Stage &s: module.stages)
146 interface_layouts.clear();
147 s.content.visit(*this);
150 writer.finalize(SPIRV_GENERATOR_MSP, next_id);
153 SpirVGenerator::StorageClass SpirVGenerator::get_interface_storage(const string &iface, bool block)
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;
164 throw invalid_argument("SpirVGenerator::get_interface_storage");
167 SpirVGenerator::BuiltinSemantic SpirVGenerator::get_builtin_semantic(const string &name)
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;
198 throw invalid_argument("SpirVGenerator::get_builtin_semantic");
201 void SpirVGenerator::use_capability(Capability cap)
203 if(used_capabilities.count(cap))
206 used_capabilities.insert(cap);
207 writer.write_op(content.capabilities, OP_CAPABILITY, cap);
210 SpirVGenerator::Id SpirVGenerator::import_extension(const string &name)
212 Id &ext_id = imported_extension_ids[name];
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);
224 SpirVGenerator::Id SpirVGenerator::get_id(Node &node) const
226 return get_item(declared_ids, &node).id;
229 SpirVGenerator::Id SpirVGenerator::allocate_id(Node &node, Id type_id)
231 auto i = declared_ids.find(&node);
232 if(i!=declared_ids.end())
234 if(i->second.type_id)
235 throw key_error(&node);
236 i->second.type_id = type_id;
241 declared_ids.insert(make_pair(&node, Declaration(id, type_id)));
245 SpirVGenerator::Id SpirVGenerator::allocate_forward_id(Node &node)
247 auto i = declared_ids.find(&node);
248 if(i!=declared_ids.end())
252 declared_ids.insert(make_pair(&node, Declaration(id, 0)));
256 SpirVGenerator::Id SpirVGenerator::write_constant(Id type_id, Word value, bool spec)
258 Id const_id = next_id++;
259 if(is_scalar_type(type_id, BasicTypeDeclaration::BOOL))
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);
267 Opcode opcode = (spec ? OP_SPEC_CONSTANT : OP_CONSTANT);
268 writer.write_op(content.globals, opcode, type_id, const_id, value);
273 SpirVGenerator::ConstantKey SpirVGenerator::get_constant_key(Id type_id, const Variant &value)
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>());
284 throw invalid_argument("SpirVGenerator::get_constant_key");
287 SpirVGenerator::Id SpirVGenerator::get_constant_id(Id type_id, const Variant &value)
289 ConstantKey key = get_constant_key(type_id, value);
290 Id &const_id = constant_ids[key];
292 const_id = write_constant(type_id, key.int_value, false);
296 SpirVGenerator::Id SpirVGenerator::get_vector_constant_id(Id type_id, unsigned size, Id scalar_id)
298 Id &const_id = constant_ids[get_constant_key(type_id, static_cast<int>(scalar_id))];
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);
312 SpirVGenerator::Id SpirVGenerator::get_standard_type_id(BasicTypeDeclaration::Kind kind, unsigned size, bool sign)
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)];
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);
330 throw invalid_argument("SpirVGenerator::get_standard_type_id");
335 bool SpirVGenerator::is_scalar_type(Id type_id, BasicTypeDeclaration::Kind kind) const
337 auto i = standard_type_ids.find(TypeKey(kind, true));
338 return (i!=standard_type_ids.end() && i->second==type_id);
341 SpirVGenerator::Id SpirVGenerator::get_array_type_id(TypeDeclaration &base_type, Id size_id)
343 Id base_type_id = get_id(base_type);
344 Id &array_type_id = array_type_ids[TypeKey(base_type_id, size_id)];
347 array_type_id = next_id++;
349 writer.write_op(content.globals, OP_TYPE_ARRAY, array_type_id, base_type_id, size_id);
351 writer.write_op(content.globals, OP_TYPE_RUNTIME_ARRAY, array_type_id, base_type_id);
353 unsigned stride = MemoryRequirementsCalculator().apply(base_type).stride;
354 writer.write_op_decorate(array_type_id, DECO_ARRAY_STRIDE, stride);
357 return array_type_id;
360 SpirVGenerator::Id SpirVGenerator::get_pointer_type_id(Id type_id, StorageClass storage)
362 Id &ptr_type_id = pointer_type_ids[TypeKey(type_id, storage)];
365 ptr_type_id = next_id++;
366 writer.write_op(content.globals, OP_TYPE_POINTER, ptr_type_id, storage, type_id);
371 SpirVGenerator::Id SpirVGenerator::get_variable_type_id(const VariableDeclaration &var)
373 if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(var.type_declaration))
374 if(basic->kind==BasicTypeDeclaration::ARRAY)
379 SetFlag set_const(constant_expression);
380 r_expression_result_id = 0;
381 var.array_size->visit(*this);
382 size_id = r_expression_result_id;
385 size_id = get_constant_id(get_standard_type_id(BasicTypeDeclaration::INT, 1), 1);
386 return get_array_type_id(*basic->base_type, size_id);
389 return get_id(*var.type_declaration);
392 SpirVGenerator::Id SpirVGenerator::get_load_id(VariableDeclaration &var)
394 Id &load_result_id = variable_load_ids[&var];
397 load_result_id = next_id++;
398 writer.write_op(content.function_body, OP_LOAD, get_variable_type_id(var), load_result_id, get_id(var));
400 return load_result_id;
403 void SpirVGenerator::prune_loads(Id min_id)
405 for(auto i=variable_load_ids.begin(); i!=variable_load_ids.end(); )
407 if(i->second>=min_id)
408 variable_load_ids.erase(i++);
414 SpirVGenerator::Id SpirVGenerator::begin_expression(Opcode opcode, Id type_id, unsigned n_args)
416 bool has_result = (opcode==OP_FUNCTION_CALL || !is_scalar_type(type_id, BasicTypeDeclaration::VOID));
417 if(!constant_expression)
419 if(!current_function)
420 throw internal_error("non-constant expression outside a function");
422 writer.begin_op(content.function_body, opcode, (n_args ? 1+has_result*2+n_args : 0));
424 else if(opcode==OP_COMPOSITE_CONSTRUCT)
425 writer.begin_op(content.globals, (spec_constant ? OP_SPEC_CONSTANT_COMPOSITE : OP_CONSTANT_COMPOSITE),
426 (n_args ? 1+has_result*2+n_args : 0));
427 else if(!spec_constant)
428 throw internal_error("invalid non-specialization constant expression");
430 writer.begin_op(content.globals, OP_SPEC_CONSTANT_OP, (n_args ? 2+has_result*2+n_args : 0));
432 Id result_id = next_id++;
435 writer.write(type_id);
436 writer.write(result_id);
438 if(spec_constant && opcode!=OP_COMPOSITE_CONSTRUCT)
439 writer.write(opcode);
444 void SpirVGenerator::end_expression(Opcode opcode)
446 if(constant_expression)
447 opcode = (opcode==OP_COMPOSITE_CONSTRUCT ? spec_constant ? OP_SPEC_CONSTANT_COMPOSITE : OP_CONSTANT_COMPOSITE : OP_SPEC_CONSTANT_OP);
448 writer.end_op(opcode);
451 SpirVGenerator::Id SpirVGenerator::write_expression(Opcode opcode, Id type_id, Id arg_id)
453 Id result_id = begin_expression(opcode, type_id, 1);
454 writer.write(arg_id);
455 end_expression(opcode);
459 SpirVGenerator::Id SpirVGenerator::write_expression(Opcode opcode, Id type_id, Id left_id, Id right_id)
461 Id result_id = begin_expression(opcode, type_id, 2);
462 writer.write(left_id);
463 writer.write(right_id);
464 end_expression(opcode);
468 void SpirVGenerator::write_deconstruct(Id elem_type_id, Id composite_id, Id *elem_ids, unsigned n_elems)
470 for(unsigned i=0; i<n_elems; ++i)
472 elem_ids[i] = begin_expression(OP_COMPOSITE_EXTRACT, elem_type_id, 2);
473 writer.write(composite_id);
475 end_expression(OP_COMPOSITE_EXTRACT);
479 SpirVGenerator::Id SpirVGenerator::write_construct(Id type_id, const Id *elem_ids, unsigned n_elems)
481 Id result_id = begin_expression(OP_COMPOSITE_CONSTRUCT, type_id, n_elems);
482 for(unsigned i=0; i<n_elems; ++i)
483 writer.write(elem_ids[i]);
484 end_expression(OP_COMPOSITE_CONSTRUCT);
489 void SpirVGenerator::visit(Block &block)
491 for(const RefPtr<Statement> &s: block.body)
495 void SpirVGenerator::visit(Literal &literal)
497 Id type_id = get_id(*literal.type);
499 r_expression_result_id = write_constant(type_id, get_constant_key(type_id, literal.value).int_value, true);
501 r_expression_result_id = get_constant_id(type_id, literal.value);
502 r_constant_result = true;
505 void SpirVGenerator::visit(VariableReference &var)
507 if(constant_expression || var.declaration->constant)
509 if(!var.declaration->constant)
510 throw internal_error("reference to non-constant variable in constant context");
512 r_expression_result_id = get_id(*var.declaration);
513 r_constant_result = true;
516 else if(!current_function)
517 throw internal_error("non-constant context outside a function");
519 r_constant_result = false;
522 r_expression_result_id = 0;
523 if(!assignment_source_id)
525 auto i = variable_load_ids.find(var.declaration);
526 if(i!=variable_load_ids.end())
527 r_expression_result_id = i->second;
529 if(!r_expression_result_id)
530 r_composite_base = var.declaration;
532 else if(assignment_source_id)
534 writer.write_op(content.function_body, OP_STORE, get_id(*var.declaration), assignment_source_id);
535 variable_load_ids[var.declaration] = assignment_source_id;
536 r_expression_result_id = assignment_source_id;
539 r_expression_result_id = get_load_id(*var.declaration);
542 void SpirVGenerator::visit(InterfaceBlockReference &iface)
544 if(!composite_access || !current_function)
545 throw internal_error("invalid interface block reference");
547 r_composite_base = iface.declaration;
548 r_expression_result_id = 0;
549 r_constant_result = false;
552 void SpirVGenerator::generate_composite_access(TypeDeclaration &result_type)
555 Id result_type_id = get_id(result_type);
556 Id access_type_id = result_type_id;
559 if(constant_expression)
560 throw internal_error("composite access through pointer in constant context");
562 Id int32_type_id = get_standard_type_id(BasicTypeDeclaration::INT, 1);
563 for(unsigned &i: r_composite_chain)
564 i = (i<0x400000 ? get_constant_id(int32_type_id, static_cast<int>(i)) : i&0x3FFFFF);
566 /* Find the storage class of the base and obtain appropriate pointer type
568 const Declaration &base_decl = get_item(declared_ids, r_composite_base);
569 auto i = pointer_type_ids.begin();
570 for(; (i!=pointer_type_ids.end() && i->second!=base_decl.type_id); ++i) ;
571 if(i==pointer_type_ids.end())
572 throw internal_error("could not find storage class");
573 access_type_id = get_pointer_type_id(result_type_id, static_cast<StorageClass>(i->first.detail));
575 opcode = OP_ACCESS_CHAIN;
577 else if(assignment_source_id)
578 throw internal_error("assignment to temporary composite");
581 for(unsigned &i: r_composite_chain)
582 for(auto j=constant_ids.begin(); (i>=0x400000 && j!=constant_ids.end()); ++j)
583 if(j->second==(i&0x3FFFFF))
584 i = j->first.int_value;
586 opcode = OP_COMPOSITE_EXTRACT;
589 Id access_id = begin_expression(opcode, access_type_id, 1+r_composite_chain.size());
590 writer.write(r_composite_base_id);
591 for(unsigned i: r_composite_chain)
593 end_expression(opcode);
595 r_constant_result = false;
598 if(assignment_source_id)
600 writer.write_op(content.function_body, OP_STORE, access_id, assignment_source_id);
601 r_expression_result_id = assignment_source_id;
604 r_expression_result_id = write_expression(OP_LOAD, result_type_id, access_id);
607 r_expression_result_id = access_id;
610 void SpirVGenerator::visit_composite(Expression &base_expr, unsigned index, TypeDeclaration &type)
612 if(!composite_access)
614 r_composite_base = 0;
615 r_composite_base_id = 0;
616 r_composite_chain.clear();
620 SetFlag set_composite(composite_access);
621 base_expr.visit(*this);
624 if(!r_composite_base_id)
625 r_composite_base_id = (r_composite_base ? get_id(*r_composite_base) : r_expression_result_id);
627 r_composite_chain.push_back(index);
628 if(!composite_access)
629 generate_composite_access(type);
631 r_expression_result_id = 0;
634 void SpirVGenerator::visit_isolated(Expression &expr)
636 SetForScope<Id> clear_assign(assignment_source_id, 0);
637 SetFlag clear_composite(composite_access, false);
638 SetForScope<Node *> clear_base(r_composite_base, 0);
639 SetForScope<Id> clear_base_id(r_composite_base_id, 0);
640 vector<unsigned> saved_chain;
641 swap(saved_chain, r_composite_chain);
643 swap(saved_chain, r_composite_chain);
646 void SpirVGenerator::visit(MemberAccess &memacc)
648 visit_composite(*memacc.left, memacc.index, *memacc.type);
651 void SpirVGenerator::visit(Swizzle &swizzle)
654 visit_composite(*swizzle.left, swizzle.components[0], *swizzle.type);
655 else if(assignment_source_id)
657 const BasicTypeDeclaration &basic = dynamic_cast<const BasicTypeDeclaration &>(*swizzle.left->type);
660 for(unsigned i=0; i<swizzle.count; ++i)
661 mask |= 1<<swizzle.components[i];
663 visit_isolated(*swizzle.left);
665 Id combined_id = begin_expression(OP_VECTOR_SHUFFLE, get_id(*swizzle.left->type), 2+basic.size);
666 writer.write(r_expression_result_id);
667 writer.write(assignment_source_id);
668 for(unsigned i=0; i<basic.size; ++i)
669 writer.write(i+((mask>>i)&1)*basic.size);
670 end_expression(OP_VECTOR_SHUFFLE);
672 SetForScope<Id> set_assign(assignment_source_id, combined_id);
673 swizzle.left->visit(*this);
675 r_expression_result_id = combined_id;
679 swizzle.left->visit(*this);
680 Id left_id = r_expression_result_id;
682 r_expression_result_id = begin_expression(OP_VECTOR_SHUFFLE, get_id(*swizzle.type), 2+swizzle.count);
683 writer.write(left_id);
684 writer.write(left_id);
685 for(unsigned i=0; i<swizzle.count; ++i)
686 writer.write(swizzle.components[i]);
687 end_expression(OP_VECTOR_SHUFFLE);
689 r_constant_result = false;
692 void SpirVGenerator::visit(UnaryExpression &unary)
695 return visit_isolated(unary);
697 unary.expression->visit(*this);
699 char oper = unary.oper->token[0];
700 char oper2 = unary.oper->token[1];
701 if(oper=='+' && !oper2)
704 BasicTypeDeclaration &basic = dynamic_cast<BasicTypeDeclaration &>(*unary.expression->type);
705 BasicTypeDeclaration &elem = *get_element_type(basic);
707 if(constant_expression && elem.kind!=BasicTypeDeclaration::BOOL && elem.kind!=BasicTypeDeclaration::INT)
708 /* SPIR-V allows constant operations on floating-point values only for
710 throw internal_error("invalid operands for constant unary expression");
712 Id result_type_id = get_id(*unary.type);
713 Opcode opcode = OP_NOP;
715 r_constant_result = false;
717 opcode = OP_LOGICAL_NOT;
720 else if(oper=='-' && !oper2)
722 opcode = (elem.kind==BasicTypeDeclaration::INT ? OP_S_NEGATE : OP_F_NEGATE);
724 if(basic.kind==BasicTypeDeclaration::MATRIX)
726 Id column_type_id = get_id(*basic.base_type);
727 unsigned n_columns = basic.size&0xFFFF;
729 write_deconstruct(column_type_id, r_expression_result_id, column_ids, n_columns);
730 for(unsigned i=0; i<n_columns; ++i)
731 column_ids[i] = write_expression(opcode, column_type_id, column_ids[i]);
732 r_expression_result_id = write_construct(result_type_id, column_ids, n_columns);
736 else if((oper=='+' || oper=='-') && oper2==oper)
738 if(constant_expression)
739 throw internal_error("increment or decrement in constant expression");
742 if(elem.kind==BasicTypeDeclaration::INT)
744 opcode = (oper=='+' ? OP_I_ADD : OP_I_SUB);
745 one_id = get_constant_id(get_id(elem), 1);
747 else if(elem.kind==BasicTypeDeclaration::FLOAT)
749 opcode = (oper=='+' ? OP_F_ADD : OP_F_SUB);
750 one_id = get_constant_id(get_id(elem), 1.0f);
753 throw internal_error("invalid increment/decrement");
755 if(basic.kind==BasicTypeDeclaration::VECTOR)
756 one_id = get_vector_constant_id(result_type_id, basic.size, one_id);
758 Id post_value_id = write_expression(opcode, result_type_id, r_expression_result_id, one_id);
760 SetForScope<Id> set_assign(assignment_source_id, post_value_id);
761 unary.expression->visit(*this);
763 r_expression_result_id = (unary.oper->type==Operator::POSTFIX ? r_expression_result_id : post_value_id);
768 throw internal_error("unknown unary operator");
770 r_expression_result_id = write_expression(opcode, result_type_id, r_expression_result_id);
773 void SpirVGenerator::visit(BinaryExpression &binary)
775 char oper = binary.oper->token[0];
778 visit_isolated(*binary.right);
779 return visit_composite(*binary.left, 0x400000|r_expression_result_id, *binary.type);
781 else if(composite_access)
782 return visit_isolated(binary);
784 if(assignment_source_id)
785 throw internal_error("invalid binary expression in assignment target");
787 BasicTypeDeclaration &basic_left = dynamic_cast<BasicTypeDeclaration &>(*binary.left->type);
788 BasicTypeDeclaration &basic_right = dynamic_cast<BasicTypeDeclaration &>(*binary.right->type);
789 // Expression resolver ensures that element types are the same
790 BasicTypeDeclaration &elem = *get_element_type(basic_left);
792 if(constant_expression && elem.kind!=BasicTypeDeclaration::BOOL && elem.kind!=BasicTypeDeclaration::INT)
793 /* SPIR-V allows constant operations on floating-point values only for
795 throw internal_error("invalid operands for constant binary expression");
797 binary.left->visit(*this);
798 Id left_id = r_expression_result_id;
799 binary.right->visit(*this);
800 Id right_id = r_expression_result_id;
802 Id result_type_id = get_id(*binary.type);
803 Opcode opcode = OP_NOP;
804 bool swap_operands = false;
806 r_constant_result = false;
808 char oper2 = binary.oper->token[1];
809 if((oper=='<' || oper=='>') && oper2!=oper)
811 if(basic_left.kind==BasicTypeDeclaration::INT)
814 opcode = (oper=='<' ? (oper2=='=' ? OP_S_LESS_THAN_EQUAL : OP_S_LESS_THAN) :
815 (oper2=='=' ? OP_S_GREATER_THAN_EQUAL : OP_S_GREATER_THAN));
817 opcode = (oper=='<' ? (oper2=='=' ? OP_U_LESS_THAN_EQUAL : OP_U_LESS_THAN) :
818 (oper2=='=' ? OP_U_GREATER_THAN_EQUAL : OP_U_GREATER_THAN));
820 else if(basic_left.kind==BasicTypeDeclaration::FLOAT)
821 opcode = (oper=='<' ? (oper2=='=' ? OP_F_ORD_LESS_THAN_EQUAL : OP_F_ORD_LESS_THAN) :
822 (oper2=='=' ? OP_F_ORD_GREATER_THAN_EQUAL : OP_F_ORD_GREATER_THAN));
824 else if((oper=='=' || oper=='!') && oper2=='=')
826 if(elem.kind==BasicTypeDeclaration::BOOL)
827 opcode = (oper=='=' ? OP_LOGICAL_EQUAL : OP_LOGICAL_NOT_EQUAL);
828 else if(elem.kind==BasicTypeDeclaration::INT)
829 opcode = (oper=='=' ? OP_I_EQUAL : OP_I_NOT_EQUAL);
830 else if(elem.kind==BasicTypeDeclaration::FLOAT)
831 opcode = (oper=='=' ? OP_F_ORD_EQUAL : OP_F_ORD_NOT_EQUAL);
833 if(opcode!=OP_NOP && basic_left.base_type)
835 /* The SPIR-V equality operations produce component-wise results, but
836 GLSL operators return a single boolean. Use the any/all operations to
837 combine the results. */
838 Opcode combine_op = (oper=='!' ? OP_ANY : OP_ALL);
839 unsigned n_elems = basic_left.size&0xFFFF;
840 Id bool_vec_type_id = get_standard_type_id(BasicTypeDeclaration::BOOL, n_elems);
843 if(basic_left.kind==BasicTypeDeclaration::VECTOR)
844 compare_id = write_expression(opcode, bool_vec_type_id, left_id, right_id);
845 else if(basic_left.kind==BasicTypeDeclaration::MATRIX)
847 Id column_type_id = get_id(*basic_left.base_type);
849 write_deconstruct(column_type_id, left_id, column_ids, n_elems);
850 write_deconstruct(column_type_id, right_id, column_ids+4, n_elems);
852 Id column_bvec_type_id = get_standard_type_id(BasicTypeDeclaration::BOOL, basic_left.size>>16);
853 for(unsigned i=0; i<n_elems; ++i)
855 compare_id = write_expression(opcode, column_bvec_type_id, column_ids[i], column_ids[4+i]);
856 column_ids[i] = write_expression(combine_op, result_type_id, compare_id);;
859 compare_id = write_construct(bool_vec_type_id, column_ids, n_elems);
863 r_expression_result_id = write_expression(combine_op, result_type_id, compare_id);
867 else if(oper2=='&' && elem.kind==BasicTypeDeclaration::BOOL)
868 opcode = OP_LOGICAL_AND;
869 else if(oper2=='|' && elem.kind==BasicTypeDeclaration::BOOL)
870 opcode = OP_LOGICAL_OR;
871 else if(oper2=='^' && elem.kind==BasicTypeDeclaration::BOOL)
872 opcode = OP_LOGICAL_NOT_EQUAL;
873 else if(oper=='&' && elem.kind==BasicTypeDeclaration::INT)
874 opcode = OP_BITWISE_AND;
875 else if(oper=='|' && elem.kind==BasicTypeDeclaration::INT)
876 opcode = OP_BITWISE_OR;
877 else if(oper=='^' && elem.kind==BasicTypeDeclaration::INT)
878 opcode = OP_BITWISE_XOR;
879 else if(oper=='<' && oper2==oper && elem.kind==BasicTypeDeclaration::INT)
880 opcode = OP_SHIFT_LEFT_LOGICAL;
881 else if(oper=='>' && oper2==oper && elem.kind==BasicTypeDeclaration::INT)
882 opcode = OP_SHIFT_RIGHT_ARITHMETIC;
883 else if(oper=='%' && elem.kind==BasicTypeDeclaration::INT)
884 opcode = (elem.sign ? OP_S_MOD : OP_U_MOD);
885 else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')
887 Opcode elem_op = OP_NOP;
888 if(elem.kind==BasicTypeDeclaration::INT)
891 elem_op = (elem.sign ? OP_S_DIV : OP_U_DIV);
893 elem_op = (oper=='+' ? OP_I_ADD : oper=='-' ? OP_I_SUB : OP_I_MUL);
895 else if(elem.kind==BasicTypeDeclaration::FLOAT)
896 elem_op = (oper=='+' ? OP_F_ADD : oper=='-' ? OP_F_SUB : oper=='*' ? OP_F_MUL : OP_F_DIV);
898 if(oper=='*' && (basic_left.base_type || basic_right.base_type) && elem.kind==BasicTypeDeclaration::FLOAT)
900 /* Multiplication between floating-point vectors and matrices has
901 dedicated operations. */
902 if(basic_left.kind==BasicTypeDeclaration::MATRIX && basic_right.kind==BasicTypeDeclaration::MATRIX)
903 opcode = OP_MATRIX_TIMES_MATRIX;
904 else if(basic_left.kind==BasicTypeDeclaration::MATRIX || basic_right.kind==BasicTypeDeclaration::MATRIX)
906 if(basic_left.kind==BasicTypeDeclaration::VECTOR)
907 opcode = OP_VECTOR_TIMES_MATRIX;
908 else if(basic_right.kind==BasicTypeDeclaration::VECTOR)
909 opcode = OP_MATRIX_TIMES_VECTOR;
912 opcode = OP_MATRIX_TIMES_SCALAR;
913 swap_operands = (basic_right.kind==BasicTypeDeclaration::MATRIX);
916 else if(basic_left.kind==BasicTypeDeclaration::VECTOR && basic_right.kind==BasicTypeDeclaration::VECTOR)
920 opcode = OP_VECTOR_TIMES_SCALAR;
921 swap_operands = (basic_right.kind==BasicTypeDeclaration::VECTOR);
924 else if((basic_left.base_type!=0)!=(basic_right.base_type!=0))
926 /* One operand is scalar and the other is a vector or a matrix.
927 Expand the scalar to a vector of appropriate size. */
928 Id &scalar_id = (basic_left.base_type ? right_id : left_id);
929 BasicTypeDeclaration *vector_type = (basic_left.base_type ? &basic_left : &basic_right);
930 if(vector_type->kind==BasicTypeDeclaration::MATRIX)
931 vector_type = dynamic_cast<BasicTypeDeclaration *>(vector_type->base_type);
932 Id vector_type_id = get_id(*vector_type);
934 Id expanded_id = begin_expression(OP_COMPOSITE_CONSTRUCT, vector_type_id, vector_type->size);
935 for(unsigned i=0; i<vector_type->size; ++i)
936 writer.write(scalar_id);
937 end_expression(OP_COMPOSITE_CONSTRUCT);
939 scalar_id = expanded_id;
941 if(basic_left.kind==BasicTypeDeclaration::MATRIX || basic_right.kind==BasicTypeDeclaration::MATRIX)
943 // Apply matrix operation column-wise.
944 Id matrix_id = (basic_left.base_type ? left_id : right_id);
947 unsigned n_columns = (basic_left.base_type ? basic_left.size : basic_right.size)&0xFFFF;
948 write_deconstruct(vector_type_id, matrix_id, column_ids, n_columns);
950 for(unsigned i=0; i<n_columns; ++i)
951 column_ids[i] = write_expression(elem_op, vector_type_id, column_ids[i], expanded_id);
953 r_expression_result_id = write_construct(result_type_id, column_ids, n_columns);
959 else if(basic_left.kind==BasicTypeDeclaration::MATRIX && basic_right.kind==BasicTypeDeclaration::MATRIX)
962 throw internal_error("non-float matrix multiplication");
964 /* Other operations involving matrices need to be performed
966 Id column_type_id = get_id(*basic_left.base_type);
969 unsigned n_columns = basic_left.size&0xFFFF;
970 write_deconstruct(column_type_id, left_id, column_ids, n_columns);
971 write_deconstruct(column_type_id, right_id, column_ids+4, n_columns);
973 for(unsigned i=0; i<n_columns; ++i)
974 column_ids[i] = write_expression(elem_op, column_type_id, column_ids[i], column_ids[4+i]);
976 r_expression_result_id = write_construct(result_type_id, column_ids, n_columns);
979 else if(basic_left.kind==basic_right.kind)
980 // Both operands are either scalars or vectors.
985 throw internal_error("unknown binary operator");
988 swap(left_id, right_id);
990 r_expression_result_id = write_expression(opcode, result_type_id, left_id, right_id);
993 void SpirVGenerator::visit(Assignment &assign)
995 if(assign.oper->token[0]!='=')
996 visit(static_cast<BinaryExpression &>(assign));
998 assign.right->visit(*this);
1000 SetForScope<Id> set_assign(assignment_source_id, r_expression_result_id);
1001 assign.left->visit(*this);
1002 r_constant_result = false;
1005 void SpirVGenerator::visit(TernaryExpression &ternary)
1007 if(composite_access)
1008 return visit_isolated(ternary);
1009 if(constant_expression)
1011 ternary.condition->visit(*this);
1012 Id condition_id = r_expression_result_id;
1013 ternary.true_expr->visit(*this);
1014 Id true_result_id = r_expression_result_id;
1015 ternary.false_expr->visit(*this);
1016 Id false_result_id = r_expression_result_id;
1018 r_expression_result_id = begin_expression(OP_SELECT, get_id(*ternary.type), 3);
1019 writer.write(condition_id);
1020 writer.write(true_result_id);
1021 writer.write(false_result_id);
1022 end_expression(OP_SELECT);
1027 ternary.condition->visit(*this);
1028 Id condition_id = r_expression_result_id;
1030 Id true_label_id = next_id++;
1031 Id false_label_id = next_id++;
1032 Id merge_block_id = next_id++;
1033 writer.write_op(content.function_body, OP_SELECTION_MERGE, merge_block_id, 0); // Selection control (none)
1034 writer.write_op(content.function_body, OP_BRANCH_CONDITIONAL, condition_id, true_label_id, false_label_id);
1036 writer.write_op_label(true_label_id);
1037 ternary.true_expr->visit(*this);
1038 Id true_result_id = r_expression_result_id;
1039 true_label_id = writer.get_current_block();
1040 writer.write_op(content.function_body, OP_BRANCH, merge_block_id);
1042 writer.write_op_label(false_label_id);
1043 ternary.false_expr->visit(*this);
1044 Id false_result_id = r_expression_result_id;
1045 false_label_id = writer.get_current_block();
1047 writer.write_op_label(merge_block_id);
1048 r_expression_result_id = begin_expression(OP_PHI, get_id(*ternary.type), 4);
1049 writer.write(true_result_id);
1050 writer.write(true_label_id);
1051 writer.write(false_result_id);
1052 writer.write(false_label_id);
1053 end_expression(OP_PHI);
1055 r_constant_result = false;
1058 void SpirVGenerator::visit(FunctionCall &call)
1060 if(assignment_source_id)
1061 throw internal_error("assignment to function call");
1062 else if(composite_access)
1063 return visit_isolated(call);
1064 else if(call.constructor && call.arguments.size()==1 && call.arguments[0]->type==call.type)
1065 return call.arguments[0]->visit(*this);
1067 vector<Id> argument_ids;
1068 argument_ids.reserve(call.arguments.size());
1069 bool all_args_const = true;
1070 for(const RefPtr<Expression> &a: call.arguments)
1073 argument_ids.push_back(r_expression_result_id);
1074 all_args_const &= r_constant_result;
1077 if(constant_expression && (!call.constructor || !all_args_const))
1078 throw internal_error("function call in constant expression");
1080 Id result_type_id = get_id(*call.type);
1081 r_constant_result = false;
1083 if(call.constructor)
1084 visit_constructor(call, argument_ids, all_args_const);
1085 else if(call.declaration->source==BUILTIN_SOURCE)
1088 for(const RefPtr<Expression> &a: call.arguments)
1089 if(BasicTypeDeclaration *basic_arg = dynamic_cast<BasicTypeDeclaration *>(a->type))
1091 BasicTypeDeclaration &elem_arg = *get_element_type(*basic_arg);
1092 switch(elem_arg.kind)
1094 case BasicTypeDeclaration::BOOL: arg_types += 'b'; break;
1095 case BasicTypeDeclaration::INT: arg_types += (elem_arg.sign ? 'i' : 'u'); break;
1096 case BasicTypeDeclaration::FLOAT: arg_types += 'f'; break;
1097 default: arg_types += '?';
1101 const BuiltinFunctionInfo *builtin_info;
1102 for(builtin_info=builtin_functions; builtin_info->function[0]; ++builtin_info)
1103 if(builtin_info->function==call.name && (!builtin_info->arg_types[0] || builtin_info->arg_types==arg_types))
1106 if(builtin_info->capability)
1107 use_capability(static_cast<Capability>(builtin_info->capability));
1109 if(builtin_info->opcode)
1112 if(builtin_info->extension[0])
1114 opcode = OP_EXT_INST;
1115 Id ext_id = import_extension(builtin_info->extension);
1117 r_expression_result_id = begin_expression(opcode, result_type_id);
1118 writer.write(ext_id);
1119 writer.write(builtin_info->opcode);
1123 opcode = static_cast<Opcode>(builtin_info->opcode);
1124 r_expression_result_id = begin_expression(opcode, result_type_id);
1127 for(unsigned i=0; i<call.arguments.size(); ++i)
1129 if(!builtin_info->arg_order[i] || builtin_info->arg_order[i]>argument_ids.size())
1130 throw internal_error("invalid builtin function info");
1131 writer.write(argument_ids[builtin_info->arg_order[i]-1]);
1134 end_expression(opcode);
1136 else if(builtin_info->handler)
1137 (this->*(builtin_info->handler))(call, argument_ids);
1139 throw internal_error("unknown builtin function "+call.name);
1143 r_expression_result_id = begin_expression(OP_FUNCTION_CALL, result_type_id, 1+call.arguments.size());
1144 writer.write(get_id(*call.declaration->definition));
1145 for(Id i: argument_ids)
1147 end_expression(OP_FUNCTION_CALL);
1149 // Any global variables the called function uses might have changed value
1150 set<Node *> dependencies = DependencyCollector().apply(*call.declaration->definition);
1151 for(Node *n: dependencies)
1152 if(const VariableDeclaration *var = dynamic_cast<const VariableDeclaration *>(n))
1153 variable_load_ids.erase(var);
1157 void SpirVGenerator::visit_constructor(FunctionCall &call, const vector<Id> &argument_ids, bool all_args_const)
1159 Id result_type_id = get_id(*call.type);
1161 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(call.type);
1164 if(dynamic_cast<const StructDeclaration *>(call.type))
1165 r_expression_result_id = write_construct(result_type_id, &argument_ids[0], argument_ids.size());
1167 throw internal_error("unconstructable type "+call.name);
1171 SetFlag set_const(constant_expression, constant_expression || all_args_const);
1173 BasicTypeDeclaration &elem = *get_element_type(*basic);
1174 BasicTypeDeclaration &basic_arg0 = dynamic_cast<BasicTypeDeclaration &>(*call.arguments[0]->type);
1175 BasicTypeDeclaration &elem_arg0 = *get_element_type(basic_arg0);
1177 if(basic->kind==BasicTypeDeclaration::MATRIX)
1179 Id col_type_id = get_id(*basic->base_type);
1180 unsigned n_columns = basic->size&0xFFFF;
1181 unsigned n_rows = basic->size>>16;
1184 if(call.arguments.size()==1)
1186 // Construct diagonal matrix from a single scalar.
1187 Id zero_id = get_constant_id(get_id(elem), 0.0f);
1188 for(unsigned i=0; i<n_columns; ++i)
1190 column_ids[i] = begin_expression(OP_COMPOSITE_CONSTRUCT, col_type_id, n_rows);
1191 for(unsigned j=0; j<n_rows; ++j)
1192 writer.write(j==i ? argument_ids[0] : zero_id);
1193 end_expression(OP_COMPOSITE_CONSTRUCT);
1197 // Construct a matrix from column vectors
1198 copy(argument_ids.begin(), argument_ids.begin()+n_columns, column_ids);
1200 r_expression_result_id = write_construct(result_type_id, column_ids, n_columns);
1202 else if(basic->kind==BasicTypeDeclaration::VECTOR && (call.arguments.size()>1 || basic_arg0.kind!=BasicTypeDeclaration::VECTOR))
1204 /* There's either a single scalar argument or multiple arguments
1205 which make up the vector's components. */
1206 if(call.arguments.size()==1)
1208 r_expression_result_id = begin_expression(OP_COMPOSITE_CONSTRUCT, result_type_id);
1209 for(unsigned i=0; i<basic->size; ++i)
1210 writer.write(argument_ids[0]);
1211 end_expression(OP_COMPOSITE_CONSTRUCT);
1214 r_expression_result_id = write_construct(result_type_id, &argument_ids[0], argument_ids.size());
1216 else if(elem.kind==BasicTypeDeclaration::BOOL)
1218 if(constant_expression)
1219 throw internal_error("unconverted constant");
1221 // Conversion to boolean is implemented as comparing against zero.
1222 Id number_type_id = get_id(elem_arg0);
1223 Id zero_id = (elem_arg0.kind==BasicTypeDeclaration::FLOAT ?
1224 get_constant_id(number_type_id, 0.0f) : get_constant_id(number_type_id, 0));
1225 if(basic_arg0.kind==BasicTypeDeclaration::VECTOR)
1226 zero_id = get_vector_constant_id(get_id(basic_arg0), basic_arg0.size, zero_id);
1228 Opcode opcode = (elem_arg0.kind==BasicTypeDeclaration::FLOAT ? OP_F_ORD_NOT_EQUAL : OP_I_NOT_EQUAL);
1229 r_expression_result_id = write_expression(opcode, result_type_id, argument_ids[0], zero_id);
1231 else if(elem_arg0.kind==BasicTypeDeclaration::BOOL)
1233 if(constant_expression)
1234 throw internal_error("unconverted constant");
1236 /* Conversion from boolean is implemented as selecting from zero
1238 Id number_type_id = get_id(elem);
1239 Id zero_id = (elem.kind==BasicTypeDeclaration::FLOAT ?
1240 get_constant_id(number_type_id, 0.0f) : get_constant_id(number_type_id, 0));
1241 Id one_id = (elem.kind==BasicTypeDeclaration::FLOAT ?
1242 get_constant_id(number_type_id, 1.0f) : get_constant_id(number_type_id, 1));
1243 if(basic->kind==BasicTypeDeclaration::VECTOR)
1245 zero_id = get_vector_constant_id(get_id(*basic), basic->size, zero_id);
1246 one_id = get_vector_constant_id(get_id(*basic), basic->size, one_id);
1249 r_expression_result_id = begin_expression(OP_SELECT, result_type_id, 3);
1250 writer.write(argument_ids[0]);
1251 writer.write(zero_id);
1252 writer.write(one_id);
1253 end_expression(OP_SELECT);
1257 if(constant_expression)
1258 throw internal_error("unconverted constant");
1260 // Scalar or vector conversion between types of equal size.
1262 if(elem.kind==BasicTypeDeclaration::INT && elem_arg0.kind==BasicTypeDeclaration::FLOAT)
1263 opcode = (elem.sign ? OP_CONVERT_F_TO_S : OP_CONVERT_F_TO_U);
1264 else if(elem.kind==BasicTypeDeclaration::FLOAT && elem_arg0.kind==BasicTypeDeclaration::INT)
1265 opcode = (elem_arg0.sign ? OP_CONVERT_S_TO_F : OP_CONVERT_U_TO_F);
1266 else if(elem.kind==BasicTypeDeclaration::INT && elem_arg0.kind==BasicTypeDeclaration::INT)
1267 opcode = OP_BITCAST;
1269 throw internal_error("invalid conversion");
1271 r_expression_result_id = write_expression(opcode, result_type_id, argument_ids[0]);
1275 void SpirVGenerator::visit_builtin_matrix_comp_mult(FunctionCall &call, const vector<Id> &argument_ids)
1277 if(argument_ids.size()!=2)
1278 throw internal_error("invalid matrixCompMult call");
1280 const BasicTypeDeclaration &basic_arg0 = dynamic_cast<const BasicTypeDeclaration &>(*call.arguments[0]->type);
1281 Id column_type_id = get_id(*basic_arg0.base_type);
1284 unsigned n_columns = basic_arg0.size&0xFFFF;
1285 write_deconstruct(column_type_id, argument_ids[0], column_ids, n_columns);
1286 write_deconstruct(column_type_id, argument_ids[1], column_ids+4, n_columns);
1288 for(unsigned i=0; i<n_columns; ++i)
1289 column_ids[i] = write_expression(OP_F_MUL, column_type_id, column_ids[i], column_ids[4+i]);
1291 r_expression_result_id = write_construct(get_id(*call.type), column_ids, n_columns);
1294 void SpirVGenerator::visit_builtin_texture_query(FunctionCall &call, const vector<Id> &argument_ids)
1296 if(argument_ids.size()<1)
1297 throw internal_error("invalid texture query call");
1300 if(call.name=="textureSize")
1301 opcode = OP_IMAGE_QUERY_SIZE_LOD;
1302 else if(call.name=="textureQueryLod")
1303 opcode = OP_IMAGE_QUERY_LOD;
1304 else if(call.name=="textureQueryLevels")
1305 opcode = OP_IMAGE_QUERY_LEVELS;
1307 throw internal_error("invalid texture query call");
1309 ImageTypeDeclaration &image_arg0 = dynamic_cast<ImageTypeDeclaration &>(*call.arguments[0]->type);
1312 if(image_arg0.sampled)
1314 Id image_type_id = get_item(image_type_ids, get_id(image_arg0));
1315 image_id = write_expression(OP_IMAGE, image_type_id, argument_ids[0]);
1318 image_id = argument_ids[0];
1320 Id result_type_id = get_id(*call.type);
1321 r_expression_result_id = begin_expression(opcode, result_type_id, argument_ids.size());
1322 writer.write(image_id);
1323 for(unsigned i=1; i<argument_ids.size(); ++i)
1324 writer.write(argument_ids[i]);
1325 end_expression(opcode);
1328 void SpirVGenerator::visit_builtin_texture(FunctionCall &call, const vector<Id> &argument_ids)
1330 if(argument_ids.size()<2)
1331 throw internal_error("invalid texture sampling call");
1333 bool explicit_lod = (stage->type!=Stage::FRAGMENT || call.name=="textureLod");
1334 Id lod_id = (!explicit_lod ? 0 : call.name=="textureLod" ? argument_ids.back() :
1335 get_constant_id(get_standard_type_id(BasicTypeDeclaration::FLOAT, 1), 0.0f));
1337 const ImageTypeDeclaration &image = dynamic_cast<const ImageTypeDeclaration &>(*call.arguments[0]->type);
1340 Id result_type_id = get_id(*call.type);
1344 if(argument_ids.size()==2)
1346 const BasicTypeDeclaration &basic_arg1 = dynamic_cast<const BasicTypeDeclaration &>(*call.arguments[1]->type);
1347 dref_id = begin_expression(OP_COMPOSITE_EXTRACT, get_id(*basic_arg1.base_type), 2);
1348 writer.write(argument_ids.back());
1349 writer.write(basic_arg1.size-1);
1350 end_expression(OP_COMPOSITE_EXTRACT);
1353 dref_id = argument_ids[2];
1355 opcode = (explicit_lod ? OP_IMAGE_SAMPLE_DREF_EXPLICIT_LOD : OP_IMAGE_SAMPLE_DREF_IMPLICIT_LOD);
1356 r_expression_result_id = begin_expression(opcode, result_type_id, 3+explicit_lod*2);
1360 opcode = (explicit_lod ? OP_IMAGE_SAMPLE_EXPLICIT_LOD : OP_IMAGE_SAMPLE_IMPLICIT_LOD);
1361 r_expression_result_id = begin_expression(opcode, result_type_id, 2+explicit_lod*2);
1364 for(unsigned i=0; i<2; ++i)
1365 writer.write(argument_ids[i]);
1367 writer.write(dref_id);
1370 writer.write(2); // Lod
1371 writer.write(lod_id);
1374 end_expression(opcode);
1377 void SpirVGenerator::visit_builtin_texel_fetch(FunctionCall &call, const vector<Id> &argument_ids)
1379 if(argument_ids.size()!=3)
1380 throw internal_error("invalid texelFetch call");
1382 r_expression_result_id = begin_expression(OP_IMAGE_FETCH, get_id(*call.type), 4);
1383 for(unsigned i=0; i<2; ++i)
1384 writer.write(argument_ids[i]);
1385 writer.write(2); // Lod
1386 writer.write(argument_ids.back());
1387 end_expression(OP_IMAGE_FETCH);
1390 void SpirVGenerator::visit_builtin_interpolate(FunctionCall &call, const vector<Id> &argument_ids)
1392 if(argument_ids.size()<1)
1393 throw internal_error("invalid interpolate call");
1394 const VariableReference *var = dynamic_cast<const VariableReference *>(call.arguments[0].get());
1395 if(!var || !var->declaration || var->declaration->interface!="in")
1396 throw internal_error("invalid interpolate call");
1398 SpirVGlslStd450Opcode opcode;
1399 if(call.name=="interpolateAtCentroid")
1400 opcode = GLSL450_INTERPOLATE_AT_CENTROID;
1401 else if(call.name=="interpolateAtSample")
1402 opcode = GLSL450_INTERPOLATE_AT_SAMPLE;
1403 else if(call.name=="interpolateAtOffset")
1404 opcode = GLSL450_INTERPOLATE_AT_OFFSET;
1406 throw internal_error("invalid interpolate call");
1408 Id ext_id = import_extension("GLSL.std.450");
1409 r_expression_result_id = begin_expression(OP_EXT_INST, get_id(*call.type));
1410 writer.write(ext_id);
1411 writer.write(opcode);
1412 writer.write(get_id(*var->declaration));
1413 for(auto i=argument_ids.begin(); ++i!=argument_ids.end(); )
1415 end_expression(OP_EXT_INST);
1418 void SpirVGenerator::visit(ExpressionStatement &expr)
1420 expr.expression->visit(*this);
1423 void SpirVGenerator::visit(InterfaceLayout &layout)
1425 interface_layouts.push_back(&layout);
1428 bool SpirVGenerator::check_duplicate_type(TypeDeclaration &type)
1430 for(const auto &kvp: declared_ids)
1431 if(TypeDeclaration *type2 = dynamic_cast<TypeDeclaration *>(kvp.first))
1432 if(TypeComparer().apply(type, *type2))
1434 insert_unique(declared_ids, &type, kvp.second);
1441 bool SpirVGenerator::check_standard_type(BasicTypeDeclaration &basic)
1443 const BasicTypeDeclaration *elem = (basic.kind==BasicTypeDeclaration::VECTOR ?
1444 dynamic_cast<const BasicTypeDeclaration *>(basic.base_type) : &basic);
1445 if(!elem || elem->base_type)
1447 if((elem->kind==BasicTypeDeclaration::INT || elem->kind==BasicTypeDeclaration::FLOAT) && elem->size!=32)
1450 Id standard_id = get_standard_type_id(elem->kind, (basic.kind==BasicTypeDeclaration::VECTOR ? basic.size : 1), elem->sign);
1451 insert_unique(declared_ids, &basic, Declaration(standard_id, 0));
1452 writer.write_op_name(standard_id, basic.name);
1457 void SpirVGenerator::visit(BasicTypeDeclaration &basic)
1459 if(check_standard_type(basic))
1461 if(check_duplicate_type(basic))
1463 // Alias types shouldn't exist at this point and arrays are handled elsewhere
1464 if(basic.kind==BasicTypeDeclaration::ALIAS || basic.kind==BasicTypeDeclaration::ARRAY)
1467 Id type_id = allocate_id(basic, 0);
1468 writer.write_op_name(type_id, basic.name);
1472 case BasicTypeDeclaration::INT:
1473 writer.write_op(content.globals, OP_TYPE_INT, type_id, basic.size, basic.sign);
1475 case BasicTypeDeclaration::FLOAT:
1476 writer.write_op(content.globals, OP_TYPE_FLOAT, type_id, basic.size);
1478 case BasicTypeDeclaration::VECTOR:
1479 writer.write_op(content.globals, OP_TYPE_VECTOR, type_id, get_id(*basic.base_type), basic.size);
1481 case BasicTypeDeclaration::MATRIX:
1482 writer.write_op(content.globals, OP_TYPE_MATRIX, type_id, get_id(*basic.base_type), basic.size&0xFFFF);
1485 throw internal_error("unknown basic type");
1489 void SpirVGenerator::visit(ImageTypeDeclaration &image)
1491 if(check_duplicate_type(image))
1494 Id type_id = allocate_id(image, 0);
1496 Id image_id = (image.sampled ? next_id++ : type_id);
1497 writer.begin_op(content.globals, OP_TYPE_IMAGE, 9);
1498 writer.write(image_id);
1499 writer.write(get_id(*image.base_type));
1500 writer.write(image.dimensions-1);
1501 writer.write(image.shadow);
1502 writer.write(image.array);
1503 writer.write(false); // Multisample
1504 writer.write(image.sampled ? 1 : 2);
1505 writer.write(0); // Format (unknown)
1506 writer.end_op(OP_TYPE_IMAGE);
1510 writer.write_op_name(type_id, image.name);
1511 writer.write_op(content.globals, OP_TYPE_SAMPLED_IMAGE, type_id, image_id);
1512 insert_unique(image_type_ids, type_id, image_id);
1515 if(image.dimensions==ImageTypeDeclaration::ONE)
1516 use_capability(image.sampled ? CAP_SAMPLED_1D : CAP_IMAGE_1D);
1517 else if(image.dimensions==ImageTypeDeclaration::CUBE && image.array)
1518 use_capability(image.sampled ? CAP_SAMPLED_CUBE_ARRAY : CAP_IMAGE_CUBE_ARRAY);
1521 void SpirVGenerator::visit(StructDeclaration &strct)
1523 if(check_duplicate_type(strct))
1526 Id type_id = allocate_id(strct, 0);
1527 writer.write_op_name(type_id, strct.name);
1529 if(strct.interface_block)
1530 writer.write_op_decorate(type_id, DECO_BLOCK);
1532 bool builtin = (strct.interface_block && !strct.interface_block->block_name.compare(0, 3, "gl_"));
1533 vector<Id> member_type_ids;
1534 member_type_ids.reserve(strct.members.body.size());
1535 for(const RefPtr<Statement> &s: strct.members.body)
1537 const VariableDeclaration *var = dynamic_cast<const VariableDeclaration *>(s.get());
1541 unsigned index = member_type_ids.size();
1542 member_type_ids.push_back(get_variable_type_id(*var));
1544 writer.write_op_member_name(type_id, index, var->name);
1548 BuiltinSemantic semantic = get_builtin_semantic(var->name);
1549 writer.write_op_member_decorate(type_id, index, DECO_BUILTIN, semantic);
1555 for(const Layout::Qualifier &q: var->layout->qualifiers)
1557 if(q.name=="offset")
1558 writer.write_op_member_decorate(type_id, index, DECO_OFFSET, q.value);
1559 else if(q.name=="column_major")
1560 writer.write_op_member_decorate(type_id, index, DECO_COL_MAJOR);
1561 else if(q.name=="row_major")
1562 writer.write_op_member_decorate(type_id, index, DECO_ROW_MAJOR);
1566 const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(var->type_declaration);
1567 while(basic && basic->kind==BasicTypeDeclaration::ARRAY)
1568 basic = dynamic_cast<const BasicTypeDeclaration *>(basic->base_type);
1569 if(basic && basic->kind==BasicTypeDeclaration::MATRIX)
1571 unsigned stride = MemoryRequirementsCalculator().apply(*basic->base_type).stride;
1572 writer.write_op_member_decorate(type_id, index, DECO_MATRIX_STRIDE, stride);
1577 writer.begin_op(content.globals, OP_TYPE_STRUCT);
1578 writer.write(type_id);
1579 for(Id i: member_type_ids)
1581 writer.end_op(OP_TYPE_STRUCT);
1584 void SpirVGenerator::visit(VariableDeclaration &var)
1586 Id type_id = get_variable_type_id(var);
1591 if(!var.init_expression)
1592 throw internal_error("const variable without initializer");
1594 int spec_id = get_layout_value(var.layout.get(), "constant_id");
1595 Id *spec_var_id = (spec_id>=0 ? &declared_spec_ids[spec_id] : 0);
1596 if(spec_id>=0 && *spec_var_id)
1598 insert_unique(declared_ids, &var, Declaration(*spec_var_id, type_id));
1602 SetFlag set_const(constant_expression);
1603 SetFlag set_spec(spec_constant, spec_id>=0);
1604 r_expression_result_id = 0;
1605 var.init_expression->visit(*this);
1606 var_id = r_expression_result_id;
1607 insert_unique(declared_ids, &var, Declaration(var_id, type_id));
1610 writer.write_op_decorate(var_id, DECO_SPEC_ID, spec_id);
1611 *spec_var_id = var_id;
1616 StorageClass storage = (current_function ? STORAGE_FUNCTION : get_interface_storage(var.interface, false));
1617 Id ptr_type_id = get_pointer_type_id(type_id, storage);
1618 if(var.interface=="uniform")
1620 Id &uni_id = declared_uniform_ids["v"+var.name];
1623 insert_unique(declared_ids, &var, Declaration(uni_id, ptr_type_id));
1627 uni_id = var_id = allocate_id(var, ptr_type_id);
1630 var_id = allocate_id(var, (var.constant ? type_id : ptr_type_id));
1633 if(var.init_expression)
1635 SetFlag set_const(constant_expression, !current_function);
1636 r_expression_result_id = 0;
1637 r_constant_result = false;
1638 var.init_expression->visit(*this);
1639 init_id = r_expression_result_id;
1642 vector<Word> &target = (current_function ? content.locals : content.globals);
1643 writer.begin_op(target, OP_VARIABLE, 4+(init_id && !current_function));
1644 writer.write(ptr_type_id);
1645 writer.write(var_id);
1646 writer.write(storage);
1647 if(init_id && !current_function)
1648 writer.write(init_id);
1649 writer.end_op(OP_VARIABLE);
1653 for(const Layout::Qualifier &q: var.layout->qualifiers)
1655 if(q.name=="location")
1656 writer.write_op_decorate(var_id, DECO_LOCATION, q.value);
1657 else if(q.name=="set")
1658 writer.write_op_decorate(var_id, DECO_DESCRIPTOR_SET, q.value);
1659 else if(q.name=="binding")
1660 writer.write_op_decorate(var_id, DECO_BINDING, q.value);
1663 if(!var.name.compare(0, 3, "gl_"))
1665 BuiltinSemantic semantic = get_builtin_semantic(var.name);
1666 writer.write_op_decorate(var_id, DECO_BUILTIN, semantic);
1669 if(init_id && current_function)
1671 writer.write_op(content.function_body, OP_STORE, var_id, init_id);
1672 variable_load_ids[&var] = init_id;
1676 writer.write_op_name(var_id, var.name);
1679 void SpirVGenerator::visit(InterfaceBlock &iface)
1681 bool push_const = has_layout_qualifier(iface.layout.get(), "push_constant");
1683 StorageClass storage = (push_const ? STORAGE_PUSH_CONSTANT : get_interface_storage(iface.interface, true));
1686 type_id = get_array_type_id(*iface.struct_declaration, 0);
1688 type_id = get_id(*iface.struct_declaration);
1689 Id ptr_type_id = get_pointer_type_id(type_id, storage);
1692 if(iface.interface=="uniform")
1694 Id &uni_id = declared_uniform_ids["b"+iface.block_name];
1697 insert_unique(declared_ids, &iface, Declaration(uni_id, ptr_type_id));
1701 uni_id = block_id = allocate_id(iface, ptr_type_id);
1704 block_id = allocate_id(iface, ptr_type_id);
1705 writer.write_op_name(block_id, iface.instance_name);
1707 writer.write_op(content.globals, OP_VARIABLE, ptr_type_id, block_id, storage);
1711 for(const Layout::Qualifier &q: iface.layout->qualifiers)
1714 writer.write_op_decorate(block_id, DECO_DESCRIPTOR_SET, q.value);
1715 else if(q.name=="binding")
1716 writer.write_op_decorate(block_id, DECO_BINDING, q.value);
1721 void SpirVGenerator::visit_entry_point(FunctionDeclaration &func, Id func_id)
1723 writer.begin_op(content.entry_points, OP_ENTRY_POINT);
1726 case Stage::VERTEX: writer.write(0); break;
1727 case Stage::GEOMETRY: writer.write(3); break;
1728 case Stage::FRAGMENT: writer.write(4); break;
1729 default: throw internal_error("unknown stage");
1731 writer.write(func_id);
1732 writer.write_string(func.name);
1734 set<Node *> dependencies = DependencyCollector().apply(func);
1735 for(Node *n: dependencies)
1737 if(const VariableDeclaration *var = dynamic_cast<const VariableDeclaration *>(n))
1739 if(!var->interface.empty())
1740 writer.write(get_id(*n));
1742 else if(dynamic_cast<InterfaceBlock *>(n))
1743 writer.write(get_id(*n));
1746 writer.end_op(OP_ENTRY_POINT);
1748 if(stage->type==Stage::FRAGMENT)
1750 SpirVExecutionMode origin = (features.target_api==VULKAN ? EXEC_ORIGIN_UPPER_LEFT : EXEC_ORIGIN_LOWER_LEFT);
1751 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, origin);
1753 else if(stage->type==Stage::GEOMETRY)
1754 use_capability(CAP_GEOMETRY);
1756 for(const InterfaceLayout *i: interface_layouts)
1758 for(const Layout::Qualifier &q: i->layout.qualifiers)
1761 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id,
1762 (i->interface=="in" ? EXEC_INPUT_POINTS : EXEC_OUTPUT_POINTS));
1763 else if(q.name=="lines")
1764 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_INPUT_LINES);
1765 else if(q.name=="lines_adjacency")
1766 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_INPUT_LINES_ADJACENCY);
1767 else if(q.name=="triangles")
1768 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_TRIANGLES);
1769 else if(q.name=="triangles_adjacency")
1770 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_INPUT_TRIANGLES_ADJACENCY);
1771 else if(q.name=="line_strip")
1772 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_OUTPUT_LINE_STRIP);
1773 else if(q.name=="triangle_strip")
1774 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_OUTPUT_TRIANGLE_STRIP);
1775 else if(q.name=="max_vertices")
1776 writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_OUTPUT_VERTICES, q.value);
1781 void SpirVGenerator::visit(FunctionDeclaration &func)
1783 if(func.source==BUILTIN_SOURCE)
1785 else if(func.definition!=&func)
1788 allocate_forward_id(*func.definition);
1792 Id return_type_id = get_id(*func.return_type_declaration);
1793 vector<unsigned> param_type_ids;
1794 param_type_ids.reserve(func.parameters.size());
1795 for(const RefPtr<VariableDeclaration> &p: func.parameters)
1796 param_type_ids.push_back(get_variable_type_id(*p));
1798 string sig_with_return = func.return_type+func.signature;
1799 Id &type_id = function_type_ids[sig_with_return];
1802 type_id = next_id++;
1803 writer.begin_op(content.globals, OP_TYPE_FUNCTION);
1804 writer.write(type_id);
1805 writer.write(return_type_id);
1806 for(unsigned i: param_type_ids)
1808 writer.end_op(OP_TYPE_FUNCTION);
1810 writer.write_op_name(type_id, sig_with_return);
1813 Id func_id = allocate_id(func, type_id);
1814 writer.write_op_name(func_id, func.name+func.signature);
1816 if(func.name=="main")
1817 visit_entry_point(func, func_id);
1819 writer.begin_op(content.functions, OP_FUNCTION, 5);
1820 writer.write(return_type_id);
1821 writer.write(func_id);
1822 writer.write(0); // Function control flags (none)
1823 writer.write(type_id);
1824 writer.end_op(OP_FUNCTION);
1826 for(unsigned i=0; i<func.parameters.size(); ++i)
1828 Id param_id = allocate_id(*func.parameters[i], param_type_ids[i]);
1829 writer.write_op(content.functions, OP_FUNCTION_PARAMETER, param_type_ids[i], param_id);
1830 // TODO This is probably incorrect if the parameter is assigned to.
1831 variable_load_ids[func.parameters[i].get()] = param_id;
1835 writer.begin_function_body(next_id++);
1836 SetForScope<FunctionDeclaration *> set_func(current_function, &func);
1837 func.body.visit(*this);
1839 if(writer.get_current_block())
1842 writer.write_op(content.function_body, OP_UNREACHABLE);
1845 const BasicTypeDeclaration *basic_return = dynamic_cast<const BasicTypeDeclaration *>(func.return_type_declaration);
1846 if(basic_return && basic_return->kind==BasicTypeDeclaration::VOID)
1847 writer.write_op(content.function_body, OP_RETURN);
1849 throw internal_error("missing return in non-void function");
1852 writer.end_function_body();
1853 variable_load_ids.clear();
1856 void SpirVGenerator::visit(Conditional &cond)
1858 cond.condition->visit(*this);
1860 Id true_label_id = next_id++;
1861 Id merge_block_id = next_id++;
1862 Id false_label_id = (cond.else_body.body.empty() ? merge_block_id : next_id++);
1863 writer.write_op(content.function_body, OP_SELECTION_MERGE, merge_block_id, 0); // Selection control (none)
1864 writer.write_op(content.function_body, OP_BRANCH_CONDITIONAL, r_expression_result_id, true_label_id, false_label_id);
1866 std::map<const VariableDeclaration *, Id> saved_load_ids = variable_load_ids;
1868 writer.write_op_label(true_label_id);
1869 cond.body.visit(*this);
1870 if(writer.get_current_block())
1871 writer.write_op(content.function_body, OP_BRANCH, merge_block_id);
1873 bool reachable_if_true = reachable;
1876 if(!cond.else_body.body.empty())
1878 swap(saved_load_ids, variable_load_ids);
1879 writer.write_op_label(false_label_id);
1880 cond.else_body.visit(*this);
1881 reachable |= reachable_if_true;
1884 writer.write_op_label(merge_block_id);
1885 prune_loads(true_label_id);
1888 void SpirVGenerator::visit(Iteration &iter)
1890 if(iter.init_statement)
1891 iter.init_statement->visit(*this);
1893 for(Node *n: AssignmentCollector().apply(iter))
1894 if(VariableDeclaration *var = dynamic_cast<VariableDeclaration *>(n))
1895 variable_load_ids.erase(var);
1897 Id header_id = next_id++;
1898 Id continue_id = next_id++;
1899 Id merge_block_id = next_id++;
1901 SetForScope<Id> set_merge(loop_merge_block_id, merge_block_id);
1902 SetForScope<Id> set_continue(loop_continue_target_id, continue_id);
1904 writer.write_op_label(header_id);
1905 writer.write_op(content.function_body, OP_LOOP_MERGE, merge_block_id, continue_id, 0); // Loop control (none)
1907 Id body_id = next_id++;
1910 writer.write_op_label(next_id++);
1911 iter.condition->visit(*this);
1912 writer.write_op(content.function_body, OP_BRANCH_CONDITIONAL, r_expression_result_id, body_id, merge_block_id);
1915 writer.write_op_label(body_id);
1916 iter.body.visit(*this);
1918 writer.write_op_label(continue_id);
1919 if(iter.loop_expression)
1920 iter.loop_expression->visit(*this);
1921 writer.write_op(content.function_body, OP_BRANCH, header_id);
1923 writer.write_op_label(merge_block_id);
1924 prune_loads(header_id);
1928 void SpirVGenerator::visit(Return &ret)
1932 ret.expression->visit(*this);
1933 writer.write_op(content.function_body, OP_RETURN_VALUE, r_expression_result_id);
1936 writer.write_op(content.function_body, OP_RETURN);
1940 void SpirVGenerator::visit(Jump &jump)
1942 if(jump.keyword=="discard")
1943 writer.write_op(content.function_body, OP_KILL);
1944 else if(jump.keyword=="break")
1945 writer.write_op(content.function_body, OP_BRANCH, loop_merge_block_id);
1946 else if(jump.keyword=="continue")
1947 writer.write_op(content.function_body, OP_BRANCH, loop_continue_target_id);
1949 throw internal_error("unknown jump");
1954 SpirVGenerator::TypeKey::TypeKey(BasicTypeDeclaration::Kind kind, bool sign):
1959 case BasicTypeDeclaration::VOID: detail = 'v'; break;
1960 case BasicTypeDeclaration::BOOL: detail = 'b'; break;
1961 case BasicTypeDeclaration::INT: detail = (sign ? 'i' : 'u'); break;
1962 case BasicTypeDeclaration::FLOAT: detail = 'f'; break;
1963 default: throw invalid_argument("TypeKey::TypeKey");
1967 bool SpirVGenerator::TypeKey::operator<(const TypeKey &other) const
1969 if(type_id!=other.type_id)
1970 return type_id<other.type_id;
1971 return detail<other.detail;
1975 bool SpirVGenerator::ConstantKey::operator<(const ConstantKey &other) const
1977 if(type_id!=other.type_id)
1978 return type_id<other.type_id;
1979 return int_value<other.int_value;