]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/resolve.cpp
Use extended alignment in SPIR-V struct layout when necessary
[libs/gl.git] / source / glsl / resolve.cpp
index f72dedfd2aa006fe71409fad62c559908be118bd..2f3192d77fa423a2f8fa3fb6587d283eec9da5a0 100644 (file)
@@ -28,7 +28,9 @@ bool TypeResolver::apply(Stage &s)
 
 TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type)
 {
-       auto i = array_types.find(&type);
+       bool extended_alignment = iface_block;
+       auto key = make_pair(&type, extended_alignment);
+       auto i = array_types.find(key);
        if(i!=array_types.end())
                return i->second;
 
@@ -36,10 +38,11 @@ TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type)
        array->source = INTERNAL_SOURCE;
        array->name = type.name+"[]";
        array->kind = BasicTypeDeclaration::ARRAY;
+       array->extended_alignment = extended_alignment;
        array->base = type.name;
        array->base_type = &type;
        stage->content.body.insert(type_insert_point, array);
-       array_types[&type] = array;
+       array_types[key] = array;
        return array;
 }
 
@@ -88,7 +91,7 @@ void TypeResolver::visit(BasicTypeDeclaration &type)
        if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type)
                alias_map[&type] = type.base_type;
        else if(type.kind==BasicTypeDeclaration::ARRAY && type.base_type)
-               array_types[type.base_type] = &type;
+               array_types[make_pair(type.base_type, type.extended_alignment)] = &type;
 
        stage->types.insert(make_pair(type.name, &type));
 }
@@ -102,14 +105,20 @@ void TypeResolver::visit(ImageTypeDeclaration &type)
 void TypeResolver::visit(StructDeclaration &strct)
 {
        stage->types.insert(make_pair(strct.name, &strct));
+       SetForScope<InterfaceBlock *> set_iface(iface_block, strct.interface_block);
        TraversingVisitor::visit(strct);
 }
 
 void TypeResolver::visit(VariableDeclaration &var)
 {
        resolve_type(var.type_declaration, var.type, var.array);
-       if(iface_block && var.interface==iface_block->interface)
-               var.interface.clear();
+       if(iface_block)
+       {
+               if(var.interface==iface_block->interface)
+                       var.interface.clear();
+               if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(var.type_declaration))
+                       strct->extended_alignment = true;
+       }
 }
 
 void TypeResolver::visit(InterfaceBlock &iface)
@@ -123,6 +132,7 @@ void TypeResolver::visit(InterfaceBlock &iface)
                strct->source = INTERNAL_SOURCE;
                strct->name = format("_%s_%s", iface.interface, iface.block_name);
                strct->members.body.splice(strct->members.body.begin(), iface.members->body);
+               strct->extended_alignment = true;
                stage->content.body.insert(type_insert_point, strct);
                stage->types.insert(make_pair(strct->name, strct));
 
@@ -390,6 +400,21 @@ void VariableResolver::merge_layouts(Layout &to_layout, const Layout &from_layou
        }
 }
 
+void VariableResolver::redeclare_builtin(VariableDeclaration &existing, VariableDeclaration &var)
+{
+       if(var.layout)
+       {
+               if(existing.layout)
+                       merge_layouts(*existing.layout, *var.layout);
+               else
+                       existing.layout = var.layout;
+       }
+       if(var.array_size)
+               existing.array_size = var.array_size;
+
+       redeclared_builtins.push_back(&existing);
+}
+
 void VariableResolver::visit(VariableDeclaration &var)
 {
        TraversingVisitor::visit(var);
@@ -422,17 +447,8 @@ void VariableResolver::visit(VariableDeclaration &var)
        {
                if(existing->source==BUILTIN_SOURCE)
                {
-                       if(var.layout)
-                       {
-                               if(existing->layout)
-                                       merge_layouts(*existing->layout, *var.layout);
-                               else
-                                       existing->layout = var.layout;
-                       }
-                       if(var.array_size)
-                               existing->array_size = var.array_size;
+                       redeclare_builtin(*existing, var);
 
-                       redeclared_builtins.push_back(existing);
                        if(block)
                        {
                                redeclared_builtins.push_back(block);
@@ -454,11 +470,41 @@ void VariableResolver::visit(VariableDeclaration &var)
 
 void VariableResolver::visit(InterfaceBlock &iface)
 {
-       /* Block names can be reused in different interfaces.  Prefix the name with
-       the first character of the interface to avoid conflicts. */
-       stage->interface_blocks.insert(make_pair(format("%s %s", iface.interface, iface.block_name), &iface));
-       if(!iface.instance_name.empty())
-               stage->interface_blocks.insert(make_pair(iface.instance_name, &iface));
+       string key = format("%s %s", iface.interface, iface.block_name);
+       auto i = stage->interface_blocks.find(key);
+       if(i!=stage->interface_blocks.end())
+       {
+               if(i->second->source==BUILTIN_SOURCE && iface.struct_declaration && i->second->struct_declaration)
+               {
+                       const map<string, VariableDeclaration *> &vars = iface.struct_declaration->members.variables;
+                       const map<string, VariableDeclaration *> &existing_vars = i->second->struct_declaration->members.variables;
+
+                       bool found_all = true;
+                       for(const auto &kvp: vars)
+                       {
+                               auto j = existing_vars.find(kvp.first);
+                               if(j!=existing_vars.end() && j->second->type==kvp.second->type && j->second->array==kvp.second->array)
+                                       redeclare_builtin(*j->second, *kvp.second);
+                               else
+                                       found_all = false;
+                       }
+
+                       if(found_all)
+                       {
+                               redeclared_builtins.push_back(i->second);
+                               nodes_to_remove.insert(&iface);
+                               nodes_to_remove.insert(iface.struct_declaration);
+                       }
+               }
+       }
+       else
+       {
+               /* Block names can be reused in different interfaces.  Prepend the interface
+               to the name to avoid conflicts. */
+               stage->interface_blocks.insert(make_pair(key, &iface));
+               if(!iface.instance_name.empty())
+                       stage->interface_blocks.insert(make_pair(iface.instance_name, &iface));
+       }
 
        TraversingVisitor::visit(iface);
 }
@@ -936,6 +982,11 @@ void ExpressionResolver::visit_constructor(FunctionCall &call)
                {
                        unsigned column_count = basic->size&0xFFFF;
                        unsigned row_count = basic->size>>16;
+
+                       vector<RefPtr<Expression> > columns;
+                       columns.reserve(column_count);
+                       bool changed_columns = false;
+
                        if(call.arguments.size()==1)
                        {
                                /* A matrix can be constructed from a single element or another
@@ -958,8 +1009,6 @@ void ExpressionResolver::visit_constructor(FunctionCall &call)
                                        current_block->body.insert(insert_point, temporary);
 
                                        // Create expressions to build each column.
-                                       vector<RefPtr<Expression> > columns;
-                                       columns.reserve(column_count);
                                        for(unsigned j=0; j<column_count; ++j)
                                        {
                                                RefPtr<VariableReference> ref = new VariableReference;
@@ -980,13 +1029,7 @@ void ExpressionResolver::visit_constructor(FunctionCall &call)
                                                        truncate_vector(columns.back(), row_count);
                                        }
 
-                                       call.arguments.resize(column_count);
-                                       copy(columns.begin(), columns.end(), call.arguments.begin());
-
-                                       /* Let VariableResolver process the new nodes and finish
-                                       resolving the constructor on the next pass. */
-                                       r_any_resolved = true;
-                                       return;
+                                       changed_columns = true;
                                }
                                else
                                        return;
@@ -995,9 +1038,6 @@ void ExpressionResolver::visit_constructor(FunctionCall &call)
                        {
                                /* Construct a matrix from individual components in column-major
                                order.  Arguments must align at column boundaries. */
-                               vector<RefPtr<Expression> > columns;
-                               columns.reserve(column_count);
-
                                vector<RefPtr<Expression> > column_args;
                                column_args.reserve(row_count);
                                unsigned column_component_count = 0;
@@ -1031,11 +1071,24 @@ void ExpressionResolver::visit_constructor(FunctionCall &call)
                                                else if(column_component_count>row_count)
                                                        // Argument alignment mismatch.
                                                        return;
+
+                                               changed_columns = true;
                                        }
                                }
                        }
                        else
                                return;
+
+                       if(changed_columns)
+                       {
+                               call.arguments.resize(column_count);
+                               copy(columns.begin(), columns.end(), call.arguments.begin());
+
+                               /* Let VariableResolver process the new nodes and finish
+                               resolving the constructor on the next pass. */
+                               r_any_resolved = true;
+                               return;
+                       }
                }
                else
                        return;
@@ -1105,6 +1158,28 @@ void ExpressionResolver::visit(VariableDeclaration &var)
                convert_to(var.init_expression, *var_basic);
 }
 
+void ExpressionResolver::visit(FunctionDeclaration &func)
+{
+       SetForScope<const FunctionDeclaration *> set_func(current_function, &func);
+       TraversingVisitor::visit(func);
+}
+
+void ExpressionResolver::visit(Return &ret)
+{
+       TraversingVisitor::visit(ret);
+       if(!current_function || !ret.expression)
+               return;
+
+       BasicTypeDeclaration *ret_basic = dynamic_cast<BasicTypeDeclaration *>(current_function->return_type_declaration);
+       BasicTypeDeclaration *expr_basic = dynamic_cast<BasicTypeDeclaration *>(ret.expression->type);
+       if(!ret_basic || !expr_basic)
+               return;
+
+       Compatibility compat = get_compatibility(*ret_basic, *expr_basic);
+       if(compat==RIGHT_CONVERTIBLE)
+               convert_to(ret.expression, *ret_basic);
+}
+
 
 bool FunctionResolver::apply(Stage &s)
 {