--- /dev/null
+#include <msp/core/maputils.h>
+#include <msp/core/raii.h>
+#include "reflect.h"
+#include "spirv.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GL {
+namespace SL {
+
+const SpirVGenerator::BuiltinFunctionInfo SpirVGenerator::builtin_functions[] =
+{
+ { "radians", "f", "GLSL.std.450", GLSL450_RADIANS, { 1 }, 0 },
+ { "degrees", "f", "GLSL.std.450", GLSL450_DEGREES, { 1 }, 0 },
+ { "sin", "f", "GLSL.std.450", GLSL450_SIN, { 1 }, 0 },
+ { "cos", "f", "GLSL.std.450", GLSL450_COS, { 1 }, 0 },
+ { "tan", "f", "GLSL.std.450", GLSL450_TAN, { 1 }, 0 },
+ { "asin", "f", "GLSL.std.450", GLSL450_ASIN, { 1 }, 0 },
+ { "acos", "f", "GLSL.std.450", GLSL450_ACOS, { 1 }, 0 },
+ { "atan", "f", "GLSL.std.450", GLSL450_ATAN, { 1 }, 0 },
+ { "atan", "ff", "GLSL.std.450", GLSL450_ATAN2, { 1, 2 }, 0 },
+ { "sinh", "f", "GLSL.std.450", GLSL450_SINH, { 1 }, 0 },
+ { "cosh", "f", "GLSL.std.450", GLSL450_COSH, { 1 }, 0 },
+ { "tanh", "f", "GLSL.std.450", GLSL450_TANH, { 1 }, 0 },
+ { "asinh", "f", "GLSL.std.450", GLSL450_ASINH, { 1 }, 0 },
+ { "acosh", "f", "GLSL.std.450", GLSL450_ACOSH, { 1 }, 0 },
+ { "atanh", "f", "GLSL.std.450", GLSL450_ATANH, { 1 }, 0 },
+ { "pow", "ff", "GLSL.std.450", GLSL450_POW, { 1, 2 }, 0 },
+ { "exp", "f", "GLSL.std.450", GLSL450_EXP, { 1 }, 0 },
+ { "log", "f", "GLSL.std.450", GLSL450_LOG, { 1 }, 0 },
+ { "exp2", "f", "GLSL.std.450", GLSL450_EXP2, { 1 }, 0 },
+ { "log2", "f", "GLSL.std.450", GLSL450_LOG2, { 1 }, 0 },
+ { "sqrt", "f", "GLSL.std.450", GLSL450_SQRT, { 1 }, 0 },
+ { "inversesqrt", "f", "GLSL.std.450", GLSL450_INVERSE_SQRT, { 1 }, 0 },
+ { "abs", "f", "GLSL.std.450", GLSL450_F_ABS, { 1 }, 0 },
+ { "abs", "i", "GLSL.std.450", GLSL450_S_ABS, { 1 }, 0 },
+ { "sign", "f", "GLSL.std.450", GLSL450_F_SIGN, { 1 }, 0 },
+ { "sign", "i", "GLSL.std.450", GLSL450_S_SIGN, { 1 }, 0 },
+ { "floor", "f", "GLSL.std.450", GLSL450_FLOOR, { 1 }, 0 },
+ { "trunc", "f", "GLSL.std.450", GLSL450_TRUNC, { 1 }, 0 },
+ { "round", "f", "GLSL.std.450", GLSL450_ROUND, { 1 }, 0 },
+ { "roundEven", "f", "GLSL.std.450", GLSL450_ROUND_EVEN, { 1 }, 0 },
+ { "ceil", "f", "GLSL.std.450", GLSL450_CEIL, { 1 }, 0 },
+ { "fract", "f", "GLSL.std.450", GLSL450_FRACT, { 1 }, 0 },
+ { "mod", "f", "", OP_F_MOD, { 1, 2 }, 0 },
+ { "min", "ff", "GLSL.std.450", GLSL450_F_MIN, { 1, 2 }, 0 },
+ { "min", "ii", "GLSL.std.450", GLSL450_S_MIN, { 1, 2 }, 0 },
+ { "max", "ff", "GLSL.std.450", GLSL450_F_MAX, { 1, 2 }, 0 },
+ { "max", "ii", "GLSL.std.450", GLSL450_S_MAX, { 1, 2 }, 0 },
+ { "clamp", "fff", "GLSL.std.450", GLSL450_F_CLAMP, { 1, 2, 3 }, 0 },
+ { "clamp", "iii", "GLSL.std.450", GLSL450_S_CLAMP, { 1, 2, 3 }, 0 },
+ { "mix", "fff", "GLSL.std.450", GLSL450_F_MIX, { 1, 2, 3 }, 0 },
+ { "mix", "ffb", "", OP_SELECT, { 3, 2, 1 }, 0 },
+ { "mix", "iib", "", OP_SELECT, { 3, 2, 1 }, 0 },
+ { "step", "ff", "GLSL.std.450", GLSL450_F_STEP, { 1, 2 }, 0 },
+ { "smoothstep", "fff", "GLSL.std.450", GLSL450_F_SMOOTH_STEP, { 1, 2, 3 }, 0 },
+ { "isnan", "f", "", OP_IS_NAN, { 1 }, 0 },
+ { "isinf", "f", "", OP_IS_INF, { 1 }, 0 },
+ { "fma", "fff", "GLSL.std.450", GLSL450_F_FMA, { 1, 2, 3 }, 0 },
+ { "length", "f", "GLSL.std.450", GLSL450_LENGTH, { 1 }, 0 },
+ { "distance", "ff", "GLSL.std.450", GLSL450_DISTANCE, { 1, 2 }, 0 },
+ { "dot", "ff", "", OP_DOT, { 1, 2 }, 0 },
+ { "cross", "ff", "GLSL.std.450", GLSL450_CROSS, { 1, 2 }, 0 },
+ { "normalize", "f", "GLSL.std.450", GLSL450_NORMALIZE, { 1 }, 0 },
+ { "faceforward", "fff", "GLSL.std.450", GLSL450_FACE_FORWARD, { 1, 2, 3 }, 0 },
+ { "reflect", "ff", "GLSL.std.450", GLSL450_REFLECT, { 1, 2 }, 0 },
+ { "refract", "fff", "GLSL.std.450", GLSL450_REFRACT, { 1, 2, 3 }, 0 },
+ { "matrixCompMult", "ff", "", 0, { 0 }, &SpirVGenerator::visit_builtin_matrix_comp_mult },
+ { "outerProduct", "ff", "", OP_OUTER_PRODUCT, { 1, 2 }, 0 },
+ { "transpose", "f", "", OP_TRANSPOSE, { 1 }, 0 },
+ { "determinant", "f", "GLSL.std.450", GLSL450_DETERMINANT, { 1 }, 0 },
+ { "inverse", "f", "GLSL.std.450", GLSL450_MATRIX_INVERSE, { 1 }, 0 },
+ { "lessThan", "ff", "", OP_F_ORD_LESS_THAN, { 1, 2 }, 0 },
+ { "lessThan", "ii", "", OP_S_LESS_THAN, { 1, 2 }, 0 },
+ { "lessThanEqual", "ff", "", OP_F_ORD_LESS_THAN_EQUAL, { 1, 2 }, 0 },
+ { "lessThanEqual", "ii", "", OP_S_LESS_THAN_EQUAL, { 1, 2 }, 0 },
+ { "greaterThan", "ff", "", OP_F_ORD_GREATER_THAN, { 1, 2 }, 0 },
+ { "greaterThan", "ii", "", OP_S_GREATER_THAN, { 1, 2 }, 0 },
+ { "greaterThanEqual", "ff", "", OP_F_ORD_GREATER_THAN_EQUAL, { 1, 2 }, 0 },
+ { "greaterThanEqual", "ii", "", OP_S_GREATER_THAN_EQUAL, { 1, 2 }, 0 },
+ { "equal", "ff", "", OP_F_ORD_EQUAL, { 1, 2 }, 0 },
+ { "equal", "ii", "", OP_I_EQUAL, { 1, 2 }, 0 },
+ { "notEqual", "ff", "", OP_F_ORD_NOT_EQUAL, { 1, 2 }, 0 },
+ { "notEqual", "ii", "", OP_I_NOT_EQUAL, { 1, 2 }, 0 },
+ { "any", "b", "", OP_ANY, { 1 }, 0 },
+ { "all", "b", "", OP_ALL, { 1 }, 0 },
+ { "not", "b", "", OP_LOGICAL_NOT, { 1 }, 0 },
+ { "bitfieldExtract", "iii", "", OP_BIT_FIELD_S_EXTRACT, { 1, 2, 3 }, 0 },
+ { "bitfieldInsert", "iiii", "", OP_BIT_FIELD_INSERT, { 1, 2, 3, 4 }, 0 },
+ { "bitfieldReverse", "i", "", OP_BIT_REVERSE, { 1 }, 0 },
+ { "bitCount", "i", "", OP_BIT_COUNT, { 1 }, 0 },
+ { "findLSB", "i", "GLSL.std.450", GLSL450_FIND_I_LSB, { 1 }, 0 },
+ { "findMSB", "i", "GLSL.std.450", GLSL450_FIND_S_MSB, { 1 }, 0 },
+ { "textureSize", "", "", OP_IMAGE_QUERY_SIZE_LOD, { 1, 2 }, 0 },
+ { "texture", "", "", 0, { }, &SpirVGenerator::visit_builtin_texture },
+ { "textureLod", "", "", 0, { }, &SpirVGenerator::visit_builtin_texture },
+ { "texelFetch", "", "", 0, { }, &SpirVGenerator::visit_builtin_texel_fetch },
+ { "EmitVertex", "", "", OP_EMIT_VERTEX, { }, 0 },
+ { "EndPrimitive", "", "", OP_END_PRIMITIVE, { }, 0 },
+ { "dFdx", "f", "", OP_DP_DX, { 1 }, 0 },
+ { "dFdy", "f", "", OP_DP_DY, { 1 }, 0 },
+ { "dFdxFine", "f", "", OP_DP_DX_FINE, { 1 }, 0 },
+ { "dFdyFine", "f", "", OP_DP_DY_FINE, { 1 }, 0 },
+ { "dFdxCoarse", "f", "", OP_DP_DX_COARSE, { 1 }, 0 },
+ { "dFdyCoarse", "f", "", OP_DP_DY_COARSE, { 1 }, 0 },
+ { "fwidth", "f", "", OP_FWIDTH, { 1 }, 0 },
+ { "fwidthFine", "f", "", OP_FWIDTH_FINE, { 1 }, 0 },
+ { "fwidthCoarse", "f", "", OP_FWIDTH_COARSE, { 1 }, 0 },
+ { "interpolateAtCentroid", "", "", 0, { }, &SpirVGenerator::visit_builtin_interpolate },
+ { "interpolateAtSample", "", "", 0, { }, &SpirVGenerator::visit_builtin_interpolate },
+ { "interpolateAtOffset", "", "", 0, { }, &SpirVGenerator::visit_builtin_interpolate },
+ { "", "", "", 0, { }, 0 }
+};
+
+SpirVGenerator::SpirVGenerator():
+ stage(0),
+ current_function(0),
+ writer(content),
+ next_id(1),
+ r_expression_result_id(0),
+ constant_expression(false),
+ spec_constant(false),
+ reachable(false),
+ composite_access(false),
+ r_composite_base_id(0),
+ r_composite_base(0),
+ assignment_source_id(0),
+ loop_merge_block_id(0),
+ loop_continue_target_id(0)
+{ }
+
+void SpirVGenerator::apply(Module &module)
+{
+ use_capability(CAP_SHADER);
+
+ for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
+ {
+ stage = &*i;
+ interface_layouts.clear();
+ i->content.visit(*this);
+ }
+
+ writer.finalize(next_id);
+}
+
+SpirVGenerator::StorageClass SpirVGenerator::get_interface_storage(const string &iface, bool block)
+{
+ if(iface=="in")
+ return STORAGE_INPUT;
+ else if(iface=="out")
+ return STORAGE_OUTPUT;
+ else if(iface=="uniform")
+ return (block ? STORAGE_UNIFORM : STORAGE_UNIFORM_CONSTANT);
+ else if(iface.empty())
+ return STORAGE_PRIVATE;
+ else
+ throw invalid_argument("SpirVGenerator::get_interface_storage");
+}
+
+SpirVGenerator::BuiltinSemantic SpirVGenerator::get_builtin_semantic(const string &name)
+{
+ if(name=="gl_Position")
+ return BUILTIN_POSITION;
+ else if(name=="gl_PointSize")
+ return BUILTIN_POINT_SIZE;
+ else if(name=="gl_ClipDistance")
+ return BUILTIN_CLIP_DISTANCE;
+ else if(name=="gl_VertexID")
+ return BUILTIN_VERTEX_ID;
+ else if(name=="gl_InstanceID")
+ return BUILTIN_INSTANCE_ID;
+ else if(name=="gl_PrimitiveID" || name=="gl_PrimitiveIDIn")
+ return BUILTIN_PRIMITIVE_ID;
+ else if(name=="gl_InvocationID")
+ return BUILTIN_INVOCATION_ID;
+ else if(name=="gl_Layer")
+ return BUILTIN_LAYER;
+ else if(name=="gl_FragCoord")
+ return BUILTIN_FRAG_COORD;
+ else if(name=="gl_PointCoord")
+ return BUILTIN_POINT_COORD;
+ else if(name=="gl_FrontFacing")
+ return BUILTIN_FRONT_FACING;
+ else if(name=="gl_SampleId")
+ return BUILTIN_SAMPLE_ID;
+ else if(name=="gl_SamplePosition")
+ return BUILTIN_SAMPLE_POSITION;
+ else if(name=="gl_FragDepth")
+ return BUILTIN_FRAG_DEPTH;
+ else
+ throw invalid_argument("SpirVGenerator::get_builtin_semantic");
+}
+
+void SpirVGenerator::use_capability(Capability cap)
+{
+ if(used_capabilities.count(cap))
+ return;
+
+ used_capabilities.insert(cap);
+ writer.write_op(content.capabilities, OP_CAPABILITY, cap);
+}
+
+SpirVGenerator::Id SpirVGenerator::import_extension(const string &name)
+{
+ Id &ext_id = imported_extension_ids[name];
+ if(!ext_id)
+ {
+ ext_id = next_id++;
+ writer.begin_op(content.extensions, OP_EXT_INST_IMPORT);
+ writer.write(ext_id);
+ writer.write_string(name);
+ writer.end_op(OP_EXT_INST_IMPORT);
+ }
+ return ext_id;
+}
+
+SpirVGenerator::Id SpirVGenerator::get_id(Node &node) const
+{
+ return get_item(declared_ids, &node).id;
+}
+
+SpirVGenerator::Id SpirVGenerator::allocate_id(Node &node, Id type_id)
+{
+ Id id = next_id++;
+ insert_unique(declared_ids, &node, Declaration(id, type_id));
+ return id;
+}
+
+SpirVGenerator::Id SpirVGenerator::write_constant(Id type_id, Word value, bool spec)
+{
+ Id const_id = next_id++;
+ if(is_scalar_type(type_id, BasicTypeDeclaration::BOOL))
+ {
+ Opcode opcode = (value ? (spec ? OP_SPEC_CONSTANT_TRUE : OP_CONSTANT_TRUE) :
+ (spec ? OP_SPEC_CONSTANT_FALSE : OP_CONSTANT_FALSE));
+ writer.write_op(content.globals, opcode, type_id, const_id);
+ }
+ else
+ {
+ Opcode opcode = (spec ? OP_SPEC_CONSTANT : OP_CONSTANT);
+ writer.write_op(content.globals, opcode, type_id, const_id, value);
+ }
+ return const_id;
+}
+
+SpirVGenerator::ConstantKey SpirVGenerator::get_constant_key(Id type_id, const Variant &value)
+{
+ if(value.check_type<bool>())
+ return ConstantKey(type_id, value.value<bool>());
+ else if(value.check_type<int>())
+ return ConstantKey(type_id, value.value<int>());
+ else if(value.check_type<float>())
+ return ConstantKey(type_id, value.value<float>());
+ else
+ throw invalid_argument("SpirVGenerator::get_constant_key");
+}
+
+SpirVGenerator::Id SpirVGenerator::get_constant_id(Id type_id, const Variant &value)
+{
+ ConstantKey key = get_constant_key(type_id, value);
+ Id &const_id = constant_ids[key];
+ if(!const_id)
+ const_id = write_constant(type_id, key.int_value, false);
+ return const_id;
+}
+
+SpirVGenerator::Id SpirVGenerator::get_vector_constant_id(Id type_id, unsigned size, Id scalar_id)
+{
+ Id &const_id = constant_ids[get_constant_key(type_id, static_cast<int>(scalar_id))];
+ if(!const_id)
+ {
+ const_id = next_id++;
+ writer.begin_op(content.globals, OP_CONSTANT_COMPOSITE, 3+size);
+ writer.write(type_id);
+ writer.write(const_id);
+ for(unsigned i=0; i<size; ++i)
+ writer.write(scalar_id);
+ writer.end_op(OP_CONSTANT_COMPOSITE);
+ }
+ return const_id;
+}
+
+SpirVGenerator::Id SpirVGenerator::get_standard_type_id(BasicTypeDeclaration::Kind kind, unsigned size)
+{
+ Id base_id = (size>1 ? get_standard_type_id(kind, 1) : 0);
+ Id &type_id = standard_type_ids[TypeKey(base_id, (size>1 ? size : static_cast<unsigned>(kind)))];
+ if(!type_id)
+ {
+ type_id = next_id++;
+ if(size>1)
+ writer.write_op(content.globals, OP_TYPE_VECTOR, type_id, base_id, size);
+ else if(kind==BasicTypeDeclaration::VOID)
+ writer.write_op(content.globals, OP_TYPE_VOID, type_id);
+ else if(kind==BasicTypeDeclaration::BOOL)
+ writer.write_op(content.globals, OP_TYPE_BOOL, type_id);
+ else if(kind==BasicTypeDeclaration::INT)
+ writer.write_op(content.globals, OP_TYPE_INT, type_id, 32, 1);
+ else if(kind==BasicTypeDeclaration::FLOAT)
+ writer.write_op(content.globals, OP_TYPE_FLOAT, type_id, 32);
+ else
+ throw invalid_argument("SpirVGenerator::get_standard_type_id");
+ }
+ return type_id;
+}
+
+bool SpirVGenerator::is_scalar_type(Id type_id, BasicTypeDeclaration::Kind kind) const
+{
+ map<TypeKey, Id>::const_iterator i = standard_type_ids.find(TypeKey(0, kind));
+ return (i!=standard_type_ids.end() && i->second==type_id);
+}
+
+SpirVGenerator::Id SpirVGenerator::get_array_type_id(TypeDeclaration &base_type, Id size_id)
+{
+ Id base_type_id = get_id(base_type);
+ Id &array_type_id = array_type_ids[TypeKey(base_type_id, size_id)];
+ if(!array_type_id)
+ {
+ array_type_id = next_id++;
+ if(size_id)
+ writer.write_op(content.globals, OP_TYPE_ARRAY, array_type_id, base_type_id, size_id);
+ else
+ writer.write_op(content.globals, OP_TYPE_RUNTIME_ARRAY, array_type_id, base_type_id);
+
+ unsigned stride = MemoryRequirementsCalculator().apply(base_type).stride;
+ writer.write_op_decorate(array_type_id, DECO_ARRAY_STRIDE, stride);
+ }
+
+ return array_type_id;
+}
+
+SpirVGenerator::Id SpirVGenerator::get_pointer_type_id(Id type_id, StorageClass storage)
+{
+ Id &ptr_type_id = pointer_type_ids[TypeKey(type_id, storage)];
+ if(!ptr_type_id)
+ {
+ ptr_type_id = next_id++;
+ writer.write_op(content.globals, OP_TYPE_POINTER, ptr_type_id, storage, type_id);
+ }
+ return ptr_type_id;
+}
+
+SpirVGenerator::Id SpirVGenerator::get_variable_type_id(const VariableDeclaration &var)
+{
+ if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(var.type_declaration))
+ if(basic->kind==BasicTypeDeclaration::ARRAY)
+ {
+ Id size_id = 0;
+ if(var.array_size)
+ {
+ SetFlag set_const(constant_expression);
+ r_expression_result_id = 0;
+ var.array_size->visit(*this);
+ size_id = r_expression_result_id;
+ }
+ else
+ size_id = get_constant_id(get_standard_type_id(BasicTypeDeclaration::INT, 1), 1);
+ return get_array_type_id(*basic->base_type, size_id);
+ }
+
+ return get_id(*var.type_declaration);
+}
+
+SpirVGenerator::Id SpirVGenerator::get_load_id(VariableDeclaration &var)
+{
+ Id &load_result_id = variable_load_ids[&var];
+ if(!load_result_id)
+ {
+ load_result_id = next_id++;
+ writer.write_op(content.function_body, OP_LOAD, get_variable_type_id(var), load_result_id, get_id(var));
+ }
+ return load_result_id;
+}
+
+void SpirVGenerator::prune_loads(Id min_id)
+{
+ for(map<const VariableDeclaration *, Id>::iterator i=variable_load_ids.begin(); i!=variable_load_ids.end(); )
+ {
+ if(i->second>=min_id)
+ variable_load_ids.erase(i++);
+ else
+ ++i;
+ }
+}
+
+SpirVGenerator::Id SpirVGenerator::begin_expression(Opcode opcode, Id type_id, unsigned n_args)
+{
+ bool has_result = (opcode==OP_FUNCTION_CALL || !is_scalar_type(type_id, BasicTypeDeclaration::VOID));
+ if(!constant_expression)
+ {
+ if(!current_function)
+ throw internal_error("non-constant expression outside a function");
+
+ writer.begin_op(content.function_body, opcode, (n_args ? 1+has_result*2+n_args : 0));
+ }
+ else if(opcode==OP_COMPOSITE_CONSTRUCT)
+ writer.begin_op(content.function_body, OP_SPEC_CONSTANT_COMPOSITE, (n_args ? 1+has_result*2+n_args : 0));
+ else
+ writer.begin_op(content.function_body, OP_SPEC_CONSTANT_OP, (n_args ? 2+has_result*2+n_args : 0));
+
+ Id result_id = next_id++;
+ if(has_result)
+ {
+ writer.write(type_id);
+ writer.write(result_id);
+ }
+ if(constant_expression && opcode!=OP_COMPOSITE_CONSTRUCT)
+ writer.write(opcode);
+
+ return result_id;
+}
+
+void SpirVGenerator::end_expression(Opcode opcode)
+{
+ if(constant_expression)
+ opcode = (opcode==OP_COMPOSITE_CONSTRUCT ? OP_SPEC_CONSTANT_COMPOSITE : OP_SPEC_CONSTANT_OP);
+ writer.end_op(opcode);
+}
+
+SpirVGenerator::Id SpirVGenerator::write_expression(Opcode opcode, Id type_id, Id arg_id)
+{
+ Id result_id = begin_expression(opcode, type_id, 1);
+ writer.write(arg_id);
+ end_expression(opcode);
+ return result_id;
+}
+
+SpirVGenerator::Id SpirVGenerator::write_expression(Opcode opcode, Id type_id, Id left_id, Id right_id)
+{
+ Id result_id = begin_expression(opcode, type_id, 2);
+ writer.write(left_id);
+ writer.write(right_id);
+ end_expression(opcode);
+ return result_id;
+}
+
+void SpirVGenerator::write_deconstruct(Id elem_type_id, Id composite_id, Id *elem_ids, unsigned n_elems)
+{
+ for(unsigned i=0; i<n_elems; ++i)
+ {
+ elem_ids[i] = begin_expression(OP_COMPOSITE_EXTRACT, elem_type_id, 2);
+ writer.write(composite_id);
+ writer.write(i);
+ end_expression(OP_COMPOSITE_EXTRACT);
+ }
+}
+
+SpirVGenerator::Id SpirVGenerator::write_construct(Id type_id, const Id *elem_ids, unsigned n_elems)
+{
+ Id result_id = begin_expression(OP_COMPOSITE_CONSTRUCT, type_id, n_elems);
+ for(unsigned i=0; i<n_elems; ++i)
+ writer.write(elem_ids[i]);
+ end_expression(OP_COMPOSITE_CONSTRUCT);
+
+ return result_id;
+}
+
+BasicTypeDeclaration &SpirVGenerator::get_element_type(BasicTypeDeclaration &basic)
+{
+ if(basic.kind==BasicTypeDeclaration::BOOL || basic.kind==BasicTypeDeclaration::INT || basic.kind==BasicTypeDeclaration::FLOAT)
+ return basic;
+ else if((basic.kind==BasicTypeDeclaration::VECTOR || basic.kind==BasicTypeDeclaration::MATRIX) && basic.base_type)
+ return get_element_type(dynamic_cast<BasicTypeDeclaration &>(*basic.base_type));
+ else
+ throw invalid_argument("SpirVGenerator::get_element_type");
+}
+
+void SpirVGenerator::visit(Block &block)
+{
+ for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+ (*i)->visit(*this);
+}
+
+void SpirVGenerator::visit(Literal &literal)
+{
+ Id type_id = get_id(*literal.type);
+ if(spec_constant)
+ r_expression_result_id = write_constant(type_id, get_constant_key(type_id, literal.value).int_value, true);
+ else
+ r_expression_result_id = get_constant_id(type_id, literal.value);
+}
+
+void SpirVGenerator::visit(VariableReference &var)
+{
+ if(constant_expression || var.declaration->constant)
+ {
+ if(!var.declaration->constant)
+ throw internal_error("reference to non-constant variable in constant context");
+
+ r_expression_result_id = get_id(*var.declaration);
+ return;
+ }
+ else if(!current_function)
+ throw internal_error("non-constant context outside a function");
+
+ if(composite_access)
+ {
+ r_composite_base = var.declaration;
+ r_expression_result_id = 0;
+ }
+ else if(assignment_source_id)
+ {
+ writer.write_op(content.function_body, OP_STORE, get_id(*var.declaration), assignment_source_id);
+ variable_load_ids[var.declaration] = assignment_source_id;
+ r_expression_result_id = assignment_source_id;
+ }
+ else
+ r_expression_result_id = get_load_id(*var.declaration);
+}
+
+void SpirVGenerator::visit(InterfaceBlockReference &iface)
+{
+ if(!composite_access || !current_function)
+ throw internal_error("invalid interface block reference");
+
+ r_composite_base = iface.declaration;
+ r_expression_result_id = 0;
+}
+
+void SpirVGenerator::generate_composite_access(TypeDeclaration &result_type)
+{
+ Opcode opcode;
+ Id result_type_id = get_id(result_type);
+ Id access_type_id = result_type_id;
+ if(r_composite_base)
+ {
+ if(constant_expression)
+ throw internal_error("composite access through pointer in constant context");
+
+ Id int32_type_id = get_standard_type_id(BasicTypeDeclaration::INT, 1);
+ for(vector<unsigned>::iterator i=r_composite_chain.begin(); i!=r_composite_chain.end(); ++i)
+ *i = (*i<0x400000 ? get_constant_id(int32_type_id, static_cast<int>(*i)) : *i&0x3FFFFF);
+
+ /* Find the storage class of the base and obtain appropriate pointer type
+ for the result. */
+ const Declaration &base_decl = get_item(declared_ids, r_composite_base);
+ map<TypeKey, Id>::const_iterator i = pointer_type_ids.begin();
+ for(; (i!=pointer_type_ids.end() && i->second!=base_decl.type_id); ++i) ;
+ if(i==pointer_type_ids.end())
+ throw internal_error("could not find storage class");
+ access_type_id = get_pointer_type_id(result_type_id, static_cast<StorageClass>(i->first.detail));
+
+ opcode = OP_ACCESS_CHAIN;
+ }
+ else if(assignment_source_id)
+ throw internal_error("assignment to temporary composite");
+ else
+ {
+ for(vector<unsigned>::iterator i=r_composite_chain.begin(); i!=r_composite_chain.end(); ++i)
+ for(map<ConstantKey, Id>::iterator j=constant_ids.begin(); (*i>=0x400000 && j!=constant_ids.end()); ++j)
+ if(j->second==(*i&0x3FFFFF))
+ *i = j->first.int_value;
+
+ opcode = OP_COMPOSITE_EXTRACT;
+ }
+
+ Id access_id = begin_expression(opcode, access_type_id, 1+r_composite_chain.size());
+ writer.write(r_composite_base_id);
+ for(vector<unsigned>::const_iterator i=r_composite_chain.begin(); i!=r_composite_chain.end(); ++i)
+ writer.write(*i);
+ end_expression(opcode);
+
+ if(r_composite_base)
+ {
+ if(assignment_source_id)
+ {
+ writer.write_op(content.function_body, OP_STORE, access_id, assignment_source_id);
+ r_expression_result_id = assignment_source_id;
+ }
+ else
+ r_expression_result_id = write_expression(OP_LOAD, result_type_id, access_id);
+ }
+ else
+ r_expression_result_id = access_id;
+}
+
+void SpirVGenerator::visit_composite(Expression &base_expr, unsigned index, TypeDeclaration &type)
+{
+ if(!composite_access)
+ {
+ r_composite_base = 0;
+ r_composite_base_id = 0;
+ r_composite_chain.clear();
+ }
+
+ {
+ SetFlag set_composite(composite_access);
+ base_expr.visit(*this);
+ }
+
+ if(!r_composite_base_id)
+ r_composite_base_id = (r_composite_base ? get_id(*r_composite_base) : r_expression_result_id);
+
+ r_composite_chain.push_back(index);
+ if(!composite_access)
+ generate_composite_access(type);
+ else
+ r_expression_result_id = 0;
+}
+
+void SpirVGenerator::visit_isolated(Expression &expr)
+{
+ SetForScope<Id> clear_assign(assignment_source_id, 0);
+ SetFlag clear_composite(composite_access, false);
+ SetForScope<Node *> clear_base(r_composite_base, 0);
+ SetForScope<Id> clear_base_id(r_composite_base_id, 0);
+ vector<unsigned> saved_chain;
+ swap(saved_chain, r_composite_chain);
+ expr.visit(*this);
+ swap(saved_chain, r_composite_chain);
+}
+
+void SpirVGenerator::visit(MemberAccess &memacc)
+{
+ visit_composite(*memacc.left, memacc.index, *memacc.type);
+}
+
+void SpirVGenerator::visit(Swizzle &swizzle)
+{
+ if(swizzle.count==1)
+ visit_composite(*swizzle.left, swizzle.components[0], *swizzle.type);
+ else if(assignment_source_id)
+ {
+ const BasicTypeDeclaration &basic = dynamic_cast<const BasicTypeDeclaration &>(*swizzle.left->type);
+
+ unsigned mask = 0;
+ for(unsigned i=0; i<swizzle.count; ++i)
+ mask |= 1<<swizzle.components[i];
+
+ visit_isolated(*swizzle.left);
+
+ Id combined_id = begin_expression(OP_VECTOR_SHUFFLE, get_id(*swizzle.left->type), 2+basic.size);
+ writer.write(r_expression_result_id);
+ writer.write(assignment_source_id);
+ for(unsigned i=0; i<basic.size; ++i)
+ writer.write(i+((mask>>i)&1)*basic.size);
+ end_expression(OP_VECTOR_SHUFFLE);
+
+ SetForScope<Id> set_assign(assignment_source_id, combined_id);
+ swizzle.left->visit(*this);
+
+ r_expression_result_id = combined_id;
+ }
+ else
+ {
+ swizzle.left->visit(*this);
+ Id left_id = r_expression_result_id;
+
+ r_expression_result_id = begin_expression(OP_VECTOR_SHUFFLE, get_id(*swizzle.type), 2+swizzle.count);
+ writer.write(left_id);
+ writer.write(left_id);
+ for(unsigned i=0; i<swizzle.count; ++i)
+ writer.write(swizzle.components[i]);
+ end_expression(OP_VECTOR_SHUFFLE);
+ }
+}
+
+void SpirVGenerator::visit(UnaryExpression &unary)
+{
+ unary.expression->visit(*this);
+
+ char oper = unary.oper->token[0];
+ char oper2 = unary.oper->token[1];
+ if(oper=='+' && !oper2)
+ return;
+
+ BasicTypeDeclaration &basic = dynamic_cast<BasicTypeDeclaration &>(*unary.expression->type);
+ BasicTypeDeclaration &elem = get_element_type(basic);
+
+ if(constant_expression && elem.kind!=BasicTypeDeclaration::BOOL && elem.kind!=BasicTypeDeclaration::INT)
+ /* SPIR-V allows constant operations on floating-point values only for
+ OpenGL kernels. */
+ throw internal_error("invalid operands for constant unary expression");
+
+ Id result_type_id = get_id(*unary.type);
+ Opcode opcode = OP_NOP;
+
+ if(oper=='!')
+ opcode = OP_LOGICAL_NOT;
+ else if(oper=='~')
+ opcode = OP_NOT;
+ else if(oper=='-' && !oper2)
+ {
+ opcode = (elem.kind==BasicTypeDeclaration::INT ? OP_S_NEGATE : OP_F_NEGATE);
+
+ if(basic.kind==BasicTypeDeclaration::MATRIX)
+ {
+ Id column_type_id = get_id(*basic.base_type);
+ unsigned n_columns = basic.size&0xFFFF;
+ Id column_ids[4];
+ write_deconstruct(column_type_id, r_expression_result_id, column_ids, n_columns);
+ for(unsigned i=0; i<n_columns; ++i)
+ column_ids[i] = write_expression(opcode, column_type_id, column_ids[i]);
+ r_expression_result_id = write_construct(result_type_id, column_ids, n_columns);
+ return;
+ }
+ }
+ else if((oper=='+' || oper=='-') && oper2==oper)
+ {
+ if(constant_expression)
+ throw internal_error("increment or decrement in constant expression");
+
+ Id one_id = 0;
+ if(elem.kind==BasicTypeDeclaration::INT)
+ {
+ opcode = (oper=='+' ? OP_I_ADD : OP_I_SUB);
+ one_id = get_constant_id(get_id(elem), 1);
+ }
+ else if(elem.kind==BasicTypeDeclaration::FLOAT)
+ {
+ opcode = (oper=='+' ? OP_F_ADD : OP_F_SUB);
+ one_id = get_constant_id(get_id(elem), 1.0f);
+ }
+ else
+ throw internal_error("invalid increment/decrement");
+
+ if(basic.kind==BasicTypeDeclaration::VECTOR)
+ one_id = get_vector_constant_id(result_type_id, basic.size, one_id);
+
+ Id post_value_id = write_expression(opcode, result_type_id, r_expression_result_id, one_id);
+
+ SetForScope<Id> set_assign(assignment_source_id, post_value_id);
+ unary.expression->visit(*this);
+
+ r_expression_result_id = (unary.oper->type==Operator::POSTFIX ? r_expression_result_id : post_value_id);
+ return;
+ }
+
+ if(opcode==OP_NOP)
+ throw internal_error("unknown unary operator");
+
+ r_expression_result_id = write_expression(opcode, result_type_id, r_expression_result_id);
+}
+
+void SpirVGenerator::visit(BinaryExpression &binary)
+{
+ char oper = binary.oper->token[0];
+ if(oper=='[')
+ {
+ visit_isolated(*binary.right);
+ return visit_composite(*binary.left, 0x400000|r_expression_result_id, *binary.type);
+ }
+
+ if(assignment_source_id)
+ throw internal_error("invalid binary expression in assignment target");
+
+ BasicTypeDeclaration &basic_left = dynamic_cast<BasicTypeDeclaration &>(*binary.left->type);
+ BasicTypeDeclaration &basic_right = dynamic_cast<BasicTypeDeclaration &>(*binary.right->type);
+ // Expression resolver ensures that element types are the same
+ BasicTypeDeclaration &elem = get_element_type(basic_left);
+
+ if(constant_expression && elem.kind!=BasicTypeDeclaration::BOOL && elem.kind!=BasicTypeDeclaration::INT)
+ /* SPIR-V allows constant operations on floating-point values only for
+ OpenGL kernels. */
+ throw internal_error("invalid operands for constant binary expression");
+
+ binary.left->visit(*this);
+ Id left_id = r_expression_result_id;
+ binary.right->visit(*this);
+ Id right_id = r_expression_result_id;
+
+ Id result_type_id = get_id(*binary.type);
+ Opcode opcode = OP_NOP;
+ bool swap_operands = false;
+
+ char oper2 = binary.oper->token[1];
+ if((oper=='<' || oper=='>') && oper2!=oper)
+ {
+ if(basic_left.kind==BasicTypeDeclaration::INT)
+ opcode = (oper=='<' ? (oper2=='=' ? OP_S_LESS_THAN_EQUAL : OP_S_LESS_THAN) :
+ (oper2=='=' ? OP_S_GREATER_THAN_EQUAL : OP_S_GREATER_THAN));
+ else if(basic_left.kind==BasicTypeDeclaration::FLOAT)
+ opcode = (oper=='<' ? (oper2=='=' ? OP_F_ORD_LESS_THAN_EQUAL : OP_F_ORD_LESS_THAN) :
+ (oper2=='=' ? OP_F_ORD_GREATER_THAN_EQUAL : OP_F_ORD_GREATER_THAN));
+ }
+ else if((oper=='=' || oper=='!') && oper2=='=')
+ {
+ if(elem.kind==BasicTypeDeclaration::BOOL)
+ opcode = (oper=='=' ? OP_LOGICAL_EQUAL : OP_LOGICAL_NOT_EQUAL);
+ else if(elem.kind==BasicTypeDeclaration::INT)
+ opcode = (oper=='=' ? OP_I_EQUAL : OP_I_NOT_EQUAL);
+ else if(elem.kind==BasicTypeDeclaration::FLOAT)
+ opcode = (oper=='=' ? OP_F_ORD_EQUAL : OP_F_ORD_NOT_EQUAL);
+
+ if(opcode!=OP_NOP && basic_left.base_type)
+ {
+ /* The SPIR-V equality operations produce component-wise results, but
+ GLSL operators return a single boolean. Use the any/all operations to
+ combine the results. */
+ Opcode combine_op = (oper=='!' ? OP_ANY : OP_ALL);
+ unsigned n_elems = basic_left.size&0xFFFF;
+ Id bool_vec_type_id = get_standard_type_id(BasicTypeDeclaration::BOOL, n_elems);
+
+ Id compare_id = 0;
+ if(basic_left.kind==BasicTypeDeclaration::VECTOR)
+ compare_id = write_expression(opcode, bool_vec_type_id, left_id, right_id);
+ else if(basic_left.kind==BasicTypeDeclaration::MATRIX)
+ {
+ Id column_type_id = get_id(*basic_left.base_type);
+ Id column_ids[8];
+ write_deconstruct(column_type_id, left_id, column_ids, n_elems);
+ write_deconstruct(column_type_id, right_id, column_ids+4, n_elems);
+
+ Id column_bvec_type_id = get_standard_type_id(BasicTypeDeclaration::BOOL, basic_left.size>>16);
+ for(unsigned i=0; i<n_elems; ++i)
+ {
+ compare_id = write_expression(opcode, column_bvec_type_id, column_ids[i], column_ids[4+i]);
+ column_ids[i] = write_expression(combine_op, result_type_id, compare_id);;
+ }
+
+ compare_id = write_construct(bool_vec_type_id, column_ids, n_elems);
+ }
+
+ if(compare_id)
+ r_expression_result_id = write_expression(combine_op, result_type_id, compare_id);
+ return;
+ }
+ }
+ else if(oper2=='&' && elem.kind==BasicTypeDeclaration::BOOL)
+ opcode = OP_LOGICAL_AND;
+ else if(oper2=='|' && elem.kind==BasicTypeDeclaration::BOOL)
+ opcode = OP_LOGICAL_OR;
+ else if(oper2=='^' && elem.kind==BasicTypeDeclaration::BOOL)
+ opcode = OP_LOGICAL_NOT_EQUAL;
+ else if(oper=='&' && elem.kind==BasicTypeDeclaration::INT)
+ opcode = OP_BITWISE_AND;
+ else if(oper=='|' && elem.kind==BasicTypeDeclaration::INT)
+ opcode = OP_BITWISE_OR;
+ else if(oper=='^' && elem.kind==BasicTypeDeclaration::INT)
+ opcode = OP_BITWISE_XOR;
+ else if(oper=='<' && oper2==oper && elem.kind==BasicTypeDeclaration::INT)
+ opcode = OP_SHIFT_LEFT_LOGICAL;
+ else if(oper=='>' && oper2==oper && elem.kind==BasicTypeDeclaration::INT)
+ opcode = OP_SHIFT_RIGHT_ARITHMETIC;
+ else if(oper=='%' && elem.kind==BasicTypeDeclaration::INT)
+ opcode = OP_S_MOD;
+ else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')
+ {
+ Opcode elem_op = OP_NOP;
+ if(elem.kind==BasicTypeDeclaration::INT)
+ elem_op = (oper=='+' ? OP_I_ADD : oper=='-' ? OP_I_SUB : oper=='*' ? OP_I_MUL : OP_S_DIV);
+ else if(elem.kind==BasicTypeDeclaration::FLOAT)
+ elem_op = (oper=='+' ? OP_F_ADD : oper=='-' ? OP_F_SUB : oper=='*' ? OP_F_MUL : OP_F_DIV);
+
+ if(oper=='*' && (basic_left.base_type || basic_right.base_type) && elem.kind==BasicTypeDeclaration::FLOAT)
+ {
+ /* Multiplication between floating-point vectors and matrices has
+ dedicated operations. */
+ if(basic_left.kind==BasicTypeDeclaration::MATRIX && basic_right.kind==BasicTypeDeclaration::MATRIX)
+ opcode = OP_MATRIX_TIMES_MATRIX;
+ else if(basic_left.kind==BasicTypeDeclaration::MATRIX || basic_right.kind==BasicTypeDeclaration::MATRIX)
+ {
+ if(basic_left.kind==BasicTypeDeclaration::VECTOR)
+ opcode = OP_VECTOR_TIMES_MATRIX;
+ else if(basic_right.kind==BasicTypeDeclaration::VECTOR)
+ opcode = OP_MATRIX_TIMES_VECTOR;
+ else
+ {
+ opcode = OP_MATRIX_TIMES_SCALAR;
+ swap_operands = (basic_right.kind==BasicTypeDeclaration::MATRIX);
+ }
+ }
+ else if(basic_left.kind==BasicTypeDeclaration::VECTOR && basic_right.kind==BasicTypeDeclaration::VECTOR)
+ opcode = elem_op;
+ else
+ {
+ opcode = OP_VECTOR_TIMES_SCALAR;
+ swap_operands = (basic_right.kind==BasicTypeDeclaration::VECTOR);
+ }
+ }
+ else if((basic_left.base_type!=0)!=(basic_right.base_type!=0))
+ {
+ /* One operand is scalar and the other is a vector or a matrix.
+ Expand the scalar to a vector of appropriate size. */
+ Id &scalar_id = (basic_left.base_type ? right_id : left_id);
+ BasicTypeDeclaration *vector_type = (basic_left.base_type ? &basic_left : &basic_right);
+ if(vector_type->kind==BasicTypeDeclaration::MATRIX)
+ vector_type = dynamic_cast<BasicTypeDeclaration *>(vector_type->base_type);
+ Id vector_type_id = get_id(*vector_type);
+
+ Id expanded_id = begin_expression(OP_COMPOSITE_CONSTRUCT, vector_type_id, vector_type->size);
+ for(unsigned i=0; i<vector_type->size; ++i)
+ writer.write(scalar_id);
+ end_expression(OP_COMPOSITE_CONSTRUCT);
+
+ scalar_id = expanded_id;
+
+ if(basic_left.kind==BasicTypeDeclaration::MATRIX || basic_right.kind==BasicTypeDeclaration::MATRIX)
+ {
+ // Apply matrix operation column-wise.
+ Id matrix_id = (basic_left.base_type ? left_id : right_id);
+
+ Id column_ids[4];
+ unsigned n_columns = (basic_left.base_type ? basic_left.size : basic_right.size)&0xFFFF;
+ write_deconstruct(vector_type_id, matrix_id, column_ids, n_columns);
+
+ for(unsigned i=0; i<n_columns; ++i)
+ column_ids[i] = write_expression(elem_op, vector_type_id, column_ids[i], expanded_id);
+
+ r_expression_result_id = write_construct(result_type_id, column_ids, n_columns);
+ return;
+ }
+ else
+ opcode = elem_op;
+ }
+ else if(basic_left.kind==BasicTypeDeclaration::MATRIX && basic_right.kind==BasicTypeDeclaration::MATRIX)
+ {
+ if(oper=='*')
+ throw internal_error("non-float matrix multiplication");
+
+ /* Other operations involving matrices need to be performed
+ column-wise. */
+ Id column_type_id = get_id(*basic_left.base_type);
+ Id column_ids[8];
+
+ unsigned n_columns = basic_left.size&0xFFFF;
+ write_deconstruct(column_type_id, left_id, column_ids, n_columns);
+ write_deconstruct(column_type_id, right_id, column_ids+4, n_columns);
+
+ for(unsigned i=0; i<n_columns; ++i)
+ column_ids[i] = write_expression(elem_op, column_type_id, column_ids[i], column_ids[4+i]);
+
+ r_expression_result_id = write_construct(result_type_id, column_ids, n_columns);
+ return;
+ }
+ else if(basic_left.kind==basic_right.kind)
+ // Both operands are either scalars or vectors.
+ opcode = elem_op;
+ }
+
+ if(opcode==OP_NOP)
+ throw internal_error("unknown binary operator");
+
+ if(swap_operands)
+ swap(left_id, right_id);
+
+ r_expression_result_id = write_expression(opcode, result_type_id, left_id, right_id);
+}
+
+void SpirVGenerator::visit(Assignment &assign)
+{
+ if(assign.oper->token[0]!='=')
+ visit(static_cast<BinaryExpression &>(assign));
+ else
+ assign.right->visit(*this);
+
+ SetForScope<Id> set_assign(assignment_source_id, r_expression_result_id);
+ assign.left->visit(*this);
+}
+
+void SpirVGenerator::visit(TernaryExpression &ternary)
+{
+ if(constant_expression)
+ {
+ ternary.condition->visit(*this);
+ Id condition_id = r_expression_result_id;
+ ternary.true_expr->visit(*this);
+ Id true_result_id = r_expression_result_id;
+ ternary.false_expr->visit(*this);
+ Id false_result_id = r_expression_result_id;
+
+ r_expression_result_id = begin_expression(OP_SELECT, get_id(*ternary.type), 3);
+ writer.write(condition_id);
+ writer.write(true_result_id);
+ writer.write(false_result_id);
+ end_expression(OP_SELECT);
+
+ return;
+ }
+
+ ternary.condition->visit(*this);
+ Id condition_id = r_expression_result_id;
+
+ Id true_label_id = next_id++;
+ Id false_label_id = next_id++;
+ Id merge_block_id = next_id++;
+ writer.write_op(content.function_body, OP_SELECTION_MERGE, merge_block_id, 0); // Selection control (none)
+ writer.write_op(content.function_body, OP_BRANCH_CONDITIONAL, condition_id, true_label_id, false_label_id);
+
+ writer.write_op_label(true_label_id);
+ ternary.true_expr->visit(*this);
+ Id true_result_id = r_expression_result_id;
+ writer.write_op(content.function_body, OP_BRANCH, merge_block_id);
+
+ writer.write_op_label(false_label_id);
+ ternary.false_expr->visit(*this);
+ Id false_result_id = r_expression_result_id;
+
+ writer.write_op_label(merge_block_id);
+ r_expression_result_id = begin_expression(OP_PHI, get_id(*ternary.type), 4);
+ writer.write(true_result_id);
+ writer.write(true_label_id);
+ writer.write(false_result_id);
+ writer.write(false_label_id);
+ end_expression(OP_PHI);
+}
+
+void SpirVGenerator::visit(FunctionCall &call)
+{
+ if(constant_expression)
+ throw internal_error("function call in constant expression");
+ else if(assignment_source_id)
+ throw internal_error("assignment to function call");
+ else if(composite_access)
+ return visit_isolated(call);
+ else if(call.constructor && call.arguments.size()==1 && call.arguments[0]->type==call.type)
+ return call.arguments[0]->visit(*this);
+
+ vector<Id> argument_ids;
+ argument_ids.reserve(call.arguments.size());
+ for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
+ {
+ (*i)->visit(*this);
+ argument_ids.push_back(r_expression_result_id);
+ }
+
+ Id result_type_id = get_id(*call.type);
+
+ if(call.constructor)
+ visit_constructor(call, argument_ids);
+ else if(call.declaration->source==BUILTIN_SOURCE)
+ {
+ string arg_types;
+ for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
+ if(BasicTypeDeclaration *basic_arg = dynamic_cast<BasicTypeDeclaration *>((*i)->type))
+ {
+ BasicTypeDeclaration &elem_arg = get_element_type(*basic_arg);
+ switch(elem_arg.kind)
+ {
+ case BasicTypeDeclaration::BOOL: arg_types += 'b'; break;
+ case BasicTypeDeclaration::INT: arg_types += 'i'; break;
+ case BasicTypeDeclaration::FLOAT: arg_types += 'f'; break;
+ default: arg_types += '?';
+ }
+ }
+
+ const BuiltinFunctionInfo *builtin_info;
+ for(builtin_info=builtin_functions; builtin_info->function[0]; ++builtin_info)
+ if(builtin_info->function==call.name && (!builtin_info->arg_types[0] || builtin_info->arg_types==arg_types))
+ break;
+
+ if(builtin_info->opcode)
+ {
+ Opcode opcode;
+ if(builtin_info->extension[0])
+ {
+ opcode = OP_EXT_INST;
+ Id ext_id = import_extension(builtin_info->extension);
+
+ r_expression_result_id = begin_expression(opcode, result_type_id);
+ writer.write(ext_id);
+ writer.write(builtin_info->opcode);
+ }
+ else
+ {
+ opcode = static_cast<Opcode>(builtin_info->opcode);
+ r_expression_result_id = begin_expression(opcode, result_type_id);
+ }
+
+ for(unsigned i=0; i<call.arguments.size(); ++i)
+ {
+ if(!builtin_info->arg_order[i] || builtin_info->arg_order[i]>argument_ids.size())
+ throw internal_error("invalid builtin function info");
+ writer.write(argument_ids[builtin_info->arg_order[i]-1]);
+ }
+
+ end_expression(opcode);
+ }
+ else if(builtin_info->handler)
+ (this->*(builtin_info->handler))(call, argument_ids);
+ else
+ throw internal_error("unknown builtin function "+call.name);
+ }
+ else
+ {
+ r_expression_result_id = begin_expression(OP_FUNCTION_CALL, result_type_id, 1+call.arguments.size());
+ writer.write(get_id(*call.declaration->definition));
+ for(vector<Id>::const_iterator i=argument_ids.begin(); i!=argument_ids.end(); ++i)
+ writer.write(*i);
+ end_expression(OP_FUNCTION_CALL);
+
+ // Any global variables the called function uses might have changed value
+ set<Node *> dependencies = DependencyCollector().apply(*call.declaration->definition);
+ for(set<Node *>::const_iterator i=dependencies.begin(); i!=dependencies.end(); ++i)
+ if(const VariableDeclaration *var = dynamic_cast<const VariableDeclaration *>(*i))
+ variable_load_ids.erase(var);
+ }
+}
+
+void SpirVGenerator::visit_constructor(FunctionCall &call, const vector<Id> &argument_ids)
+{
+ Id result_type_id = get_id(*call.type);
+
+ BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(call.type);
+ if(!basic)
+ {
+ if(dynamic_cast<const StructDeclaration *>(call.type))
+ r_expression_result_id = write_construct(result_type_id, &argument_ids[0], argument_ids.size());
+ else
+ throw internal_error("unconstructable type "+call.name);
+ return;
+ }
+
+ BasicTypeDeclaration &elem = get_element_type(*basic);
+ BasicTypeDeclaration &basic_arg0 = dynamic_cast<BasicTypeDeclaration &>(*call.arguments[0]->type);
+ BasicTypeDeclaration &elem_arg0 = get_element_type(basic_arg0);
+
+ if(basic->kind==BasicTypeDeclaration::MATRIX)
+ {
+ Id col_type_id = get_id(*basic->base_type);
+ unsigned n_columns = basic->size&0xFFFF;
+ unsigned n_rows = basic->size>>16;
+
+ Id column_ids[4];
+ if(call.arguments.size()==1)
+ {
+ // Construct diagonal matrix from a single scalar.
+ Id zero_id = get_constant_id(get_id(elem), 0.0f);
+ for(unsigned i=0; i<n_columns; ++i)
+ {
+ column_ids[i] = begin_expression(OP_COMPOSITE_CONSTRUCT, col_type_id, n_rows);;
+ for(unsigned j=0; j<n_rows; ++j)
+ writer.write(j==i ? argument_ids[0] : zero_id);
+ end_expression(OP_COMPOSITE_CONSTRUCT);
+ }
+ }
+ else
+ // Construct a matrix from column vectors
+ copy(argument_ids.begin(), argument_ids.begin()+n_columns, column_ids);
+
+ r_expression_result_id = write_construct(result_type_id, column_ids, n_columns);
+ }
+ else if(basic->kind==BasicTypeDeclaration::VECTOR && (call.arguments.size()>1 || basic_arg0.kind!=BasicTypeDeclaration::VECTOR))
+ {
+ /* There's either a single scalar argument or multiple arguments
+ which make up the vector's components. */
+ if(call.arguments.size()==1)
+ {
+ r_expression_result_id = begin_expression(OP_COMPOSITE_CONSTRUCT, result_type_id);
+ for(unsigned i=0; i<basic->size; ++i)
+ writer.write(argument_ids[0]);
+ end_expression(OP_COMPOSITE_CONSTRUCT);
+ }
+ else
+ r_expression_result_id = write_construct(result_type_id, &argument_ids[0], argument_ids.size());
+ }
+ else if(elem.kind==BasicTypeDeclaration::BOOL)
+ {
+ // Conversion to boolean is implemented as comparing against zero.
+ Id number_type_id = get_id(elem_arg0);
+ Id zero_id = (elem_arg0.kind==BasicTypeDeclaration::FLOAT ?
+ get_constant_id(number_type_id, 0.0f) : get_constant_id(number_type_id, 0));
+ if(basic_arg0.kind==BasicTypeDeclaration::VECTOR)
+ zero_id = get_vector_constant_id(get_id(basic_arg0), basic_arg0.size, zero_id);
+
+ Opcode opcode = (elem_arg0.kind==BasicTypeDeclaration::FLOAT ? OP_F_ORD_NOT_EQUAL : OP_I_NOT_EQUAL);
+ r_expression_result_id = write_expression(opcode, result_type_id, argument_ids[0], zero_id);
+ }
+ else if(elem_arg0.kind==BasicTypeDeclaration::BOOL)
+ {
+ /* Conversion from boolean is implemented as selecting from zero
+ or one. */
+ Id number_type_id = get_id(elem);
+ Id zero_id = (elem.kind==BasicTypeDeclaration::FLOAT ?
+ get_constant_id(number_type_id, 0.0f) : get_constant_id(number_type_id, 0));
+ Id one_id = (elem.kind==BasicTypeDeclaration::FLOAT ?
+ get_constant_id(number_type_id, 1.0f) : get_constant_id(number_type_id, 1));
+ if(basic->kind==BasicTypeDeclaration::VECTOR)
+ {
+ zero_id = get_vector_constant_id(get_id(*basic), basic->size, zero_id);
+ one_id = get_vector_constant_id(get_id(*basic), basic->size, one_id);
+ }
+
+ r_expression_result_id = begin_expression(OP_SELECT, result_type_id, 3);
+ writer.write(argument_ids[0]);
+ writer.write(zero_id);
+ writer.write(one_id);
+ end_expression(OP_SELECT);
+ }
+ else
+ {
+ // Scalar or vector conversion between types of equal size.
+ Opcode opcode;
+ if(elem.kind==BasicTypeDeclaration::INT && elem_arg0.kind==BasicTypeDeclaration::FLOAT)
+ opcode = OP_CONVERT_F_TO_S;
+ else if(elem.kind==BasicTypeDeclaration::FLOAT && elem_arg0.kind==BasicTypeDeclaration::INT)
+ opcode = OP_CONVERT_S_TO_F;
+ else
+ throw internal_error("invalid conversion");
+
+ r_expression_result_id = write_expression(opcode, result_type_id, argument_ids[0]);
+ }
+}
+
+void SpirVGenerator::visit_builtin_matrix_comp_mult(FunctionCall &call, const vector<Id> &argument_ids)
+{
+ if(argument_ids.size()!=2)
+ throw internal_error("invalid matrixCompMult call");
+
+ const BasicTypeDeclaration &basic_arg0 = dynamic_cast<const BasicTypeDeclaration &>(*call.arguments[0]->type);
+ Id column_type_id = get_id(*basic_arg0.base_type);
+ Id column_ids[8];
+
+ unsigned n_columns = basic_arg0.size&0xFFFF;
+ write_deconstruct(column_type_id, argument_ids[0], column_ids, n_columns);
+ write_deconstruct(column_type_id, argument_ids[1], column_ids+4, n_columns);
+
+ for(unsigned i=0; i<n_columns; ++i)
+ column_ids[i] = write_expression(OP_F_MUL, column_type_id, column_ids[i], column_ids[4+i]);
+
+ r_expression_result_id = write_construct(get_id(*call.type), column_ids, n_columns);
+}
+
+void SpirVGenerator::visit_builtin_texture(FunctionCall &call, const vector<Id> &argument_ids)
+{
+ if(argument_ids.size()<2)
+ throw internal_error("invalid texture sampling call");
+
+ bool explicit_lod = (stage->type!=Stage::FRAGMENT || call.name=="textureLod");
+ Id lod_id = (!explicit_lod ? 0 : call.name=="textureLod" ? argument_ids.back() :
+ get_constant_id(get_standard_type_id(BasicTypeDeclaration::FLOAT, 1), 0.0f));
+
+ const ImageTypeDeclaration &image = dynamic_cast<const ImageTypeDeclaration &>(*call.arguments[0]->type);
+
+ Opcode opcode;
+ Id result_type_id = get_id(*call.type);
+ Id dref_id = 0;
+ if(image.shadow)
+ {
+ if(argument_ids.size()==2)
+ {
+ const BasicTypeDeclaration &basic_arg1 = dynamic_cast<const BasicTypeDeclaration &>(*call.arguments[1]->type);
+ dref_id = begin_expression(OP_COMPOSITE_EXTRACT, get_id(*basic_arg1.base_type), 2);
+ writer.write(argument_ids.back());
+ writer.write(basic_arg1.size-1);
+ end_expression(OP_COMPOSITE_EXTRACT);
+ }
+ else
+ dref_id = argument_ids[2];
+
+ opcode = (explicit_lod ? OP_IMAGE_SAMPLE_DREF_EXPLICIT_LOD : OP_IMAGE_SAMPLE_DREF_IMPLICIT_LOD);
+ r_expression_result_id = begin_expression(opcode, result_type_id, 3+explicit_lod*2);
+ }
+ else
+ {
+ opcode = (explicit_lod ? OP_IMAGE_SAMPLE_EXPLICIT_LOD : OP_IMAGE_SAMPLE_IMPLICIT_LOD);
+ r_expression_result_id = begin_expression(opcode, result_type_id, 2+explicit_lod*2);
+ }
+
+ for(unsigned i=0; i<2; ++i)
+ writer.write(argument_ids[i]);
+ if(dref_id)
+ writer.write(dref_id);
+ if(explicit_lod)
+ {
+ writer.write(2); // Lod
+ writer.write(lod_id);
+ }
+
+ end_expression(opcode);
+}
+
+void SpirVGenerator::visit_builtin_texel_fetch(FunctionCall &call, const vector<Id> &argument_ids)
+{
+ if(argument_ids.size()!=3)
+ throw internal_error("invalid texelFetch call");
+
+ r_expression_result_id = begin_expression(OP_IMAGE_FETCH, get_id(*call.type), 4);
+ for(unsigned i=0; i<2; ++i)
+ writer.write(argument_ids[i]);
+ writer.write(2); // Lod
+ writer.write(argument_ids.back());
+ end_expression(OP_IMAGE_FETCH);
+}
+
+void SpirVGenerator::visit_builtin_interpolate(FunctionCall &call, const vector<Id> &argument_ids)
+{
+ if(argument_ids.size()<1)
+ throw internal_error("invalid interpolate call");
+ const VariableReference *var = dynamic_cast<const VariableReference *>(call.arguments[0].get());
+ if(!var || !var->declaration || var->declaration->interface!="in")
+ throw internal_error("invalid interpolate call");
+
+ SpirVGlslStd450Opcode opcode;
+ if(call.name=="interpolateAtCentroid")
+ opcode = GLSL450_INTERPOLATE_AT_CENTROID;
+ else if(call.name=="interpolateAtSample")
+ opcode = GLSL450_INTERPOLATE_AT_SAMPLE;
+ else if(call.name=="interpolateAtOffset")
+ opcode = GLSL450_INTERPOLATE_AT_OFFSET;
+ else
+ throw internal_error("invalid interpolate call");
+
+ use_capability(CAP_INTERPOLATION_FUNCTION);
+
+ Id ext_id = import_extension("GLSL.std.450");
+ r_expression_result_id = begin_expression(OP_EXT_INST, get_id(*call.type));
+ writer.write(ext_id);
+ writer.write(opcode);
+ writer.write(get_id(*var->declaration));
+ for(vector<Id>::const_iterator i=argument_ids.begin(); ++i!=argument_ids.end(); )
+ writer.write(*i);
+ end_expression(OP_EXT_INST);
+}
+
+void SpirVGenerator::visit(ExpressionStatement &expr)
+{
+ expr.expression->visit(*this);
+}
+
+void SpirVGenerator::visit(InterfaceLayout &layout)
+{
+ interface_layouts.push_back(&layout);
+}
+
+bool SpirVGenerator::check_duplicate_type(TypeDeclaration &type)
+{
+ for(map<Node *, Declaration>::const_iterator i=declared_ids.begin(); i!=declared_ids.end(); ++i)
+ if(TypeDeclaration *type2 = dynamic_cast<TypeDeclaration *>(i->first))
+ if(TypeComparer().apply(type, *type2))
+ {
+ insert_unique(declared_ids, &type, i->second);
+ return true;
+ }
+
+ return false;
+}
+
+bool SpirVGenerator::check_standard_type(BasicTypeDeclaration &basic)
+{
+ const BasicTypeDeclaration *elem = (basic.kind==BasicTypeDeclaration::VECTOR ?
+ dynamic_cast<const BasicTypeDeclaration *>(basic.base_type) : &basic);
+ if(!elem || elem->base_type)
+ return false;
+ if((elem->kind==BasicTypeDeclaration::INT || elem->kind==BasicTypeDeclaration::FLOAT) && elem->size!=32)
+ return false;
+
+ Id standard_id = get_standard_type_id(elem->kind, (basic.kind==BasicTypeDeclaration::VECTOR ? basic.size : 1));
+ insert_unique(declared_ids, &basic, Declaration(standard_id, 0));
+ writer.write_op_name(standard_id, basic.name);
+
+ return true;
+}
+
+void SpirVGenerator::visit(BasicTypeDeclaration &basic)
+{
+ if(check_standard_type(basic))
+ return;
+ if(check_duplicate_type(basic))
+ return;
+ // Alias types shouldn't exist at this point and arrays are handled elsewhere
+ if(basic.kind==BasicTypeDeclaration::ALIAS || basic.kind==BasicTypeDeclaration::ARRAY)
+ return;
+
+ Id type_id = allocate_id(basic, 0);
+ writer.write_op_name(type_id, basic.name);
+
+ switch(basic.kind)
+ {
+ case BasicTypeDeclaration::INT:
+ writer.write_op(content.globals, OP_TYPE_INT, type_id, basic.size, 1);
+ break;
+ case BasicTypeDeclaration::FLOAT:
+ writer.write_op(content.globals, OP_TYPE_FLOAT, type_id, basic.size);
+ break;
+ case BasicTypeDeclaration::VECTOR:
+ writer.write_op(content.globals, OP_TYPE_VECTOR, type_id, get_id(*basic.base_type), basic.size);
+ break;
+ case BasicTypeDeclaration::MATRIX:
+ writer.write_op(content.globals, OP_TYPE_MATRIX, type_id, get_id(*basic.base_type), basic.size&0xFFFF);
+ break;
+ default:
+ throw internal_error("unknown basic type");
+ }
+}
+
+void SpirVGenerator::visit(ImageTypeDeclaration &image)
+{
+ if(check_duplicate_type(image))
+ return;
+
+ Id type_id = allocate_id(image, 0);
+
+ Id image_id = (image.sampled ? next_id++ : type_id);
+ writer.begin_op(content.globals, OP_TYPE_IMAGE, 9);
+ writer.write(image_id);
+ writer.write(get_id(*image.base_type));
+ writer.write(image.dimensions-1);
+ writer.write(image.shadow);
+ writer.write(image.array);
+ writer.write(false); // Multisample
+ writer.write(image.sampled ? 1 : 2);
+ writer.write(0); // Format (unknown)
+ writer.end_op(OP_TYPE_IMAGE);
+
+ if(image.sampled)
+ {
+ writer.write_op_name(type_id, image.name);
+ writer.write_op(content.globals, OP_TYPE_SAMPLED_IMAGE, type_id, image_id);
+ }
+
+ if(image.dimensions==ImageTypeDeclaration::ONE)
+ use_capability(image.sampled ? CAP_SAMPLED_1D : CAP_IMAGE_1D);
+ else if(image.dimensions==ImageTypeDeclaration::CUBE && image.array)
+ use_capability(image.sampled ? CAP_SAMPLED_CUBE_ARRAY : CAP_IMAGE_CUBE_ARRAY);
+}
+
+void SpirVGenerator::visit(StructDeclaration &strct)
+{
+ if(check_duplicate_type(strct))
+ return;
+
+ Id type_id = allocate_id(strct, 0);
+ writer.write_op_name(type_id, strct.name);
+
+ if(strct.interface_block)
+ writer.write_op_decorate(type_id, DECO_BLOCK);
+
+ bool builtin = (strct.interface_block && !strct.interface_block->block_name.compare(0, 3, "gl_"));
+ vector<Id> member_type_ids;
+ member_type_ids.reserve(strct.members.body.size());
+ for(NodeList<Statement>::const_iterator i=strct.members.body.begin(); i!=strct.members.body.end(); ++i)
+ {
+ const VariableDeclaration *var = dynamic_cast<const VariableDeclaration *>(i->get());
+ if(!var)
+ continue;
+
+ unsigned index = member_type_ids.size();
+ member_type_ids.push_back(get_variable_type_id(*var));
+
+ writer.write_op_member_name(type_id, index, var->name);
+
+ if(builtin)
+ {
+ BuiltinSemantic semantic = get_builtin_semantic(var->name);
+ writer.write_op_member_decorate(type_id, index, DECO_BUILTIN, semantic);
+ }
+ else
+ {
+ if(var->layout)
+ {
+ const vector<Layout::Qualifier> &qualifiers = var->layout->qualifiers;
+ for(vector<Layout::Qualifier>::const_iterator j=qualifiers.begin(); j!=qualifiers.end(); ++j)
+ {
+ if(j->name=="offset")
+ writer.write_op_member_decorate(type_id, index, DECO_OFFSET, j->value);
+ else if(j->name=="column_major")
+ writer.write_op_member_decorate(type_id, index, DECO_COL_MAJOR);
+ else if(j->name=="row_major")
+ writer.write_op_member_decorate(type_id, index, DECO_ROW_MAJOR);
+ }
+ }
+
+ const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(var->type_declaration);
+ while(basic && basic->kind==BasicTypeDeclaration::ARRAY)
+ basic = dynamic_cast<const BasicTypeDeclaration *>(basic->base_type);
+ if(basic && basic->kind==BasicTypeDeclaration::MATRIX)
+ {
+ unsigned stride = MemoryRequirementsCalculator().apply(*basic->base_type).stride;
+ writer.write_op_member_decorate(type_id, index, DECO_MATRIX_STRIDE, stride);
+ }
+ }
+ }
+
+ writer.begin_op(content.globals, OP_TYPE_STRUCT);
+ writer.write(type_id);
+ for(vector<Id>::const_iterator i=member_type_ids.begin(); i!=member_type_ids.end(); ++i)
+ writer.write(*i);
+ writer.end_op(OP_TYPE_STRUCT);
+}
+
+void SpirVGenerator::visit(VariableDeclaration &var)
+{
+ const vector<Layout::Qualifier> *layout_ql = (var.layout ? &var.layout->qualifiers : 0);
+
+ int spec_id = -1;
+ if(layout_ql)
+ {
+ for(vector<Layout::Qualifier>::const_iterator i=layout_ql->begin(); (spec_id<0 && i!=layout_ql->end()); ++i)
+ if(i->name=="constant_id")
+ spec_id = i->value;
+ }
+
+ Id type_id = get_variable_type_id(var);
+ Id var_id;
+
+ if(var.constant)
+ {
+ if(!var.init_expression)
+ throw internal_error("const variable without initializer");
+
+ SetFlag set_const(constant_expression);
+ SetFlag set_spec(spec_constant, spec_id>=0);
+ r_expression_result_id = 0;
+ var.init_expression->visit(*this);
+ var_id = r_expression_result_id;
+ insert_unique(declared_ids, &var, Declaration(var_id, type_id));
+ writer.write_op_decorate(var_id, DECO_SPEC_ID, spec_id);
+
+ /* It's unclear what should be done if a specialization constant is
+ initialized with anything other than a literal. GLSL doesn't seem to
+ prohibit that but SPIR-V says OpSpecConstantOp can't be updated via
+ specialization. */
+ }
+ else
+ {
+ StorageClass storage = (current_function ? STORAGE_FUNCTION : get_interface_storage(var.interface, false));
+ Id ptr_type_id = get_pointer_type_id(type_id, storage);
+ if(var.interface=="uniform")
+ {
+ Id &uni_id = declared_uniform_ids["v"+var.name];
+ if(uni_id)
+ {
+ insert_unique(declared_ids, &var, Declaration(uni_id, ptr_type_id));
+ return;
+ }
+
+ uni_id = var_id = allocate_id(var, ptr_type_id);
+ }
+ else
+ var_id = allocate_id(var, (var.constant ? type_id : ptr_type_id));
+
+ Id init_id = 0;
+ if(var.init_expression)
+ {
+ SetFlag set_const(constant_expression, !current_function);
+ r_expression_result_id = 0;
+ var.init_expression->visit(*this);
+ init_id = r_expression_result_id;
+ }
+
+ vector<Word> &target = (current_function ? content.locals : content.globals);
+ writer.begin_op(target, OP_VARIABLE, 4+(init_id && !current_function));
+ writer.write(ptr_type_id);
+ writer.write(var_id);
+ writer.write(storage);
+ if(init_id && !current_function)
+ writer.write(init_id);
+ writer.end_op(OP_VARIABLE);
+
+ if(layout_ql)
+ {
+ for(vector<Layout::Qualifier>::const_iterator i=layout_ql->begin(); i!=layout_ql->end(); ++i)
+ {
+ if(i->name=="location")
+ writer.write_op_decorate(var_id, DECO_LOCATION, i->value);
+ else if(i->name=="set")
+ writer.write_op_decorate(var_id, DECO_DESCRIPTOR_SET, i->value);
+ else if(i->name=="binding")
+ writer.write_op_decorate(var_id, DECO_BINDING, i->value);
+ }
+ }
+
+ if(init_id && current_function)
+ writer.write_op(content.function_body, OP_STORE, var_id, init_id);
+ }
+
+ writer.write_op_name(var_id, var.name);
+}
+
+void SpirVGenerator::visit(InterfaceBlock &iface)
+{
+ StorageClass storage = get_interface_storage(iface.interface, true);
+ Id type_id;
+ if(iface.array)
+ type_id = get_array_type_id(*iface.struct_declaration, 0);
+ else
+ type_id = get_id(*iface.struct_declaration);
+ Id ptr_type_id = get_pointer_type_id(type_id, storage);
+
+ Id block_id;
+ if(iface.interface=="uniform")
+ {
+ Id &uni_id = declared_uniform_ids["b"+iface.block_name];
+ if(uni_id)
+ {
+ insert_unique(declared_ids, &iface, Declaration(uni_id, ptr_type_id));
+ return;
+ }
+
+ uni_id = block_id = allocate_id(iface, ptr_type_id);
+ }
+ else
+ block_id = allocate_id(iface, ptr_type_id);
+ writer.write_op_name(block_id, iface.instance_name);
+
+ writer.write_op(content.globals, OP_VARIABLE, ptr_type_id, block_id, storage);
+
+ if(iface.layout)
+ {
+ const vector<Layout::Qualifier> &qualifiers = iface.layout->qualifiers;
+ for(vector<Layout::Qualifier>::const_iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
+ if(i->name=="binding")
+ writer.write_op_decorate(block_id, DECO_BINDING, i->value);
+ }
+}
+
+void SpirVGenerator::visit_entry_point(FunctionDeclaration &func, Id func_id)
+{
+ writer.begin_op(content.entry_points, OP_ENTRY_POINT);
+ switch(stage->type)
+ {
+ case Stage::VERTEX: writer.write(0); break;
+ case Stage::GEOMETRY: writer.write(3); break;
+ case Stage::FRAGMENT: writer.write(4); break;
+ default: throw internal_error("unknown stage");
+ }
+ writer.write(func_id);
+ writer.write_string(func.name);
+
+ set<Node *> dependencies = DependencyCollector().apply(func);
+ for(set<Node *>::const_iterator i=dependencies.begin(); i!=dependencies.end(); ++i)
+ {
+ if(const VariableDeclaration *var = dynamic_cast<const VariableDeclaration *>(*i))
+ {
+ if(!var->interface.empty())
+ writer.write(get_id(**i));
+ }
+ else if(dynamic_cast<InterfaceBlock *>(*i))
+ writer.write(get_id(**i));
+ }
+
+ writer.end_op(OP_ENTRY_POINT);
+
+ if(stage->type==Stage::FRAGMENT)
+ writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_ORIGIN_LOWER_LEFT);
+ else if(stage->type==Stage::GEOMETRY)
+ use_capability(CAP_GEOMETRY);
+
+ for(vector<const InterfaceLayout *>::const_iterator i=interface_layouts.begin(); i!=interface_layouts.end(); ++i)
+ {
+ const vector<Layout::Qualifier> &qualifiers = (*i)->layout.qualifiers;
+ for(vector<Layout::Qualifier>::const_iterator j=qualifiers.begin(); j!=qualifiers.end(); ++j)
+ {
+ if(j->name=="point")
+ writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id,
+ ((*i)->interface=="in" ? EXEC_INPUT_POINTS : EXEC_OUTPUT_POINTS));
+ else if(j->name=="lines")
+ writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_INPUT_LINES);
+ else if(j->name=="lines_adjacency")
+ writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_INPUT_LINES_ADJACENCY);
+ else if(j->name=="triangles")
+ writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_TRIANGLES);
+ else if(j->name=="triangles_adjacency")
+ writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_INPUT_TRIANGLES_ADJACENCY);
+ else if(j->name=="line_strip")
+ writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_OUTPUT_LINE_STRIP);
+ else if(j->name=="triangle_strip")
+ writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_OUTPUT_TRIANGLE_STRIP);
+ else if(j->name=="max_vertices")
+ writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_OUTPUT_VERTICES, j->value);
+ }
+ }
+}
+
+void SpirVGenerator::visit(FunctionDeclaration &func)
+{
+ if(func.source==BUILTIN_SOURCE || func.definition!=&func)
+ return;
+
+ Id return_type_id = get_id(*func.return_type_declaration);
+ vector<unsigned> param_type_ids;
+ param_type_ids.reserve(func.parameters.size());
+ for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
+ param_type_ids.push_back(get_variable_type_id(**i));
+
+ string sig_with_return = func.return_type+func.signature;
+ Id &type_id = function_type_ids[sig_with_return];
+ if(!type_id)
+ {
+ type_id = next_id++;
+ writer.begin_op(content.globals, OP_TYPE_FUNCTION);
+ writer.write(type_id);
+ writer.write(return_type_id);
+ for(vector<unsigned>::const_iterator i=param_type_ids.begin(); i!=param_type_ids.end(); ++i)
+ writer.write(*i);
+ writer.end_op(OP_TYPE_FUNCTION);
+
+ writer.write_op_name(type_id, sig_with_return);
+ }
+
+ Id func_id = allocate_id(func, type_id);
+ writer.write_op_name(func_id, func.name+func.signature);
+
+ if(func.name=="main")
+ visit_entry_point(func, func_id);
+
+ writer.begin_op(content.functions, OP_FUNCTION, 5);
+ writer.write(return_type_id);
+ writer.write(func_id);
+ writer.write(0); // Function control flags (none)
+ writer.write(type_id);
+ writer.end_op(OP_FUNCTION);
+
+ for(unsigned i=0; i<func.parameters.size(); ++i)
+ {
+ Id param_id = allocate_id(*func.parameters[i], param_type_ids[i]);
+ writer.write_op(content.functions, OP_FUNCTION_PARAMETER, param_type_ids[i], param_id);
+ // TODO This is probably incorrect if the parameter is assigned to.
+ variable_load_ids[func.parameters[i].get()] = param_id;
+ }
+
+ writer.begin_function_body(next_id++);
+ SetForScope<FunctionDeclaration *> set_func(current_function, &func);
+ func.body.visit(*this);
+
+ if(writer.has_current_block())
+ {
+ if(!reachable)
+ writer.write_op(content.function_body, OP_UNREACHABLE);
+ else
+ {
+ const BasicTypeDeclaration *basic_return = dynamic_cast<const BasicTypeDeclaration *>(func.return_type_declaration);
+ if(basic_return && basic_return->kind==BasicTypeDeclaration::VOID)
+ writer.write_op(content.function_body, OP_RETURN);
+ else
+ throw internal_error("missing return in non-void function");
+ }
+ }
+ writer.end_function_body();
+ variable_load_ids.clear();
+}
+
+void SpirVGenerator::visit(Conditional &cond)
+{
+ cond.condition->visit(*this);
+
+ Id true_label_id = next_id++;
+ Id merge_block_id = next_id++;
+ Id false_label_id = (cond.else_body.body.empty() ? merge_block_id : next_id++);
+ writer.write_op(content.function_body, OP_SELECTION_MERGE, merge_block_id, 0); // Selection control (none)
+ writer.write_op(content.function_body, OP_BRANCH_CONDITIONAL, r_expression_result_id, true_label_id, false_label_id);
+
+ writer.write_op_label(true_label_id);
+ cond.body.visit(*this);
+ if(writer.has_current_block())
+ writer.write_op(content.function_body, OP_BRANCH, merge_block_id);
+
+ bool reachable_if_true = reachable;
+
+ reachable = true;
+ if(!cond.else_body.body.empty())
+ {
+ writer.write_op_label(false_label_id);
+ cond.else_body.visit(*this);
+ reachable |= reachable_if_true;
+ }
+
+ writer.write_op_label(merge_block_id);
+ prune_loads(true_label_id);
+}
+
+void SpirVGenerator::visit(Iteration &iter)
+{
+ if(iter.init_statement)
+ iter.init_statement->visit(*this);
+
+ Id header_id = next_id++;
+ Id continue_id = next_id++;
+ Id merge_block_id = next_id++;
+
+ SetForScope<Id> set_merge(loop_merge_block_id, merge_block_id);
+ SetForScope<Id> set_continue(loop_continue_target_id, continue_id);
+
+ writer.write_op_label(header_id);
+ writer.write_op(content.function_body, OP_LOOP_MERGE, merge_block_id, continue_id, 0); // Loop control (none)
+
+ Id body_id = next_id++;
+ if(iter.condition)
+ {
+ writer.write_op_label(next_id++);
+ iter.condition->visit(*this);
+ writer.write_op(content.function_body, OP_BRANCH_CONDITIONAL, r_expression_result_id, body_id, merge_block_id);
+ }
+
+ writer.write_op_label(body_id);
+ iter.body.visit(*this);
+
+ writer.write_op_label(continue_id);
+ if(iter.loop_expression)
+ iter.loop_expression->visit(*this);
+ writer.write_op(content.function_body, OP_BRANCH, header_id);
+
+ writer.write_op_label(merge_block_id);
+ prune_loads(header_id);
+ reachable = true;
+}
+
+void SpirVGenerator::visit(Return &ret)
+{
+ if(ret.expression)
+ {
+ ret.expression->visit(*this);
+ writer.write_op(content.function_body, OP_RETURN_VALUE, r_expression_result_id);
+ }
+ else
+ writer.write_op(content.function_body, OP_RETURN);
+ reachable = false;
+}
+
+void SpirVGenerator::visit(Jump &jump)
+{
+ if(jump.keyword=="discard")
+ writer.write_op(content.function_body, OP_KILL);
+ else if(jump.keyword=="break")
+ writer.write_op(content.function_body, OP_BRANCH, loop_merge_block_id);
+ else if(jump.keyword=="continue")
+ writer.write_op(content.function_body, OP_BRANCH, loop_continue_target_id);
+ else
+ throw internal_error("unknown jump");
+ reachable = false;
+}
+
+
+bool SpirVGenerator::TypeKey::operator<(const TypeKey &other) const
+{
+ if(type_id!=other.type_id)
+ return type_id<other.type_id;
+ return detail<other.detail;
+}
+
+
+bool SpirVGenerator::ConstantKey::operator<(const ConstantKey &other) const
+{
+ if(type_id!=other.type_id)
+ return type_id<other.type_id;
+ return int_value<other.int_value;
+}
+
+} // namespace SL
+} // namespace GL
+} // namespace Msp