]> git.tdb.fi Git - libs/gl.git/commitdiff
Allow redeclaring entire builtin interface blocks
authorMikko Rasa <tdb@tdb.fi>
Sun, 10 Oct 2021 15:35:43 +0000 (18:35 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 10 Oct 2021 15:36:36 +0000 (18:36 +0300)
source/glsl/resolve.cpp
source/glsl/resolve.h
tests/glsl/redeclare_builtin.glsl [new file with mode: 0644]

index f72dedfd2aa006fe71409fad62c559908be118bd..b80ae2176f5c8fae81a3c36cdaaf22a383f59256 100644 (file)
@@ -390,6 +390,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 +437,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 +460,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);
 }
index 856117272f892e9c5e60cd0a6f0ca53e34f6e567..10322d49f67e89d80f61b994177f18c1b2b0588b 100644 (file)
@@ -78,6 +78,7 @@ private:
        virtual void visit(BinaryExpression &);
        virtual void visit(Assignment &);
        void merge_layouts(Layout &, const Layout &);
+       void redeclare_builtin(VariableDeclaration &, VariableDeclaration &);
        virtual void visit(VariableDeclaration &);
        virtual void visit(InterfaceBlock &);
 };
diff --git a/tests/glsl/redeclare_builtin.glsl b/tests/glsl/redeclare_builtin.glsl
new file mode 100644 (file)
index 0000000..c0b84c4
--- /dev/null
@@ -0,0 +1,23 @@
+#pragma MSP stage(vertex)
+layout(location=0) in vec4 position;
+out float gl_ClipDistance[1];
+void main()
+{
+       gl_ClipDistance[0] = dot(position.xyz, vec3(0.0, 0.0, 1.0));
+       gl_Position = position;
+}
+
+/* Expected output: vertex
+out gl_PerVertex
+{
+       vec4 gl_Position;
+       float gl_PointSize;
+       float gl_ClipDistance[1];
+};
+layout(location=0) in vec4 position;
+void main()
+{
+       gl_ClipDistance[0] = dot(position.xyz, vec3(0.0, 0.0, 1.0));
+       gl_Position = position;
+}
+*/