]> git.tdb.fi Git - libs/gl.git/commitdiff
Automatically determine array sizes in GLSL if possible
authorMikko Rasa <tdb@tdb.fi>
Sun, 28 Nov 2021 23:01:01 +0000 (01:01 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 28 Nov 2021 23:02:41 +0000 (01:02 +0200)
Consequently the SPIR-V backend no longer needs to deal with missing
array sizes.

source/glsl/compiler.cpp
source/glsl/generate.cpp
source/glsl/generate.h
source/glsl/spirv.cpp
source/glsl/validate.cpp

index 16ac375ca57f05660aa83ae89d9e75b4aff2a941..f1ab6d088e4e1bdedd5d4188685ef2f6a55f02e0 100644 (file)
@@ -299,6 +299,9 @@ void Compiler::generate(Stage &stage)
        variables through interfaces. */
        InterfaceGenerator().apply(stage);
        resolve(stage, RESOLVE_BLOCKS|RESOLVE_TYPES|RESOLVE_VARIABLES);
+
+       ArraySizer().apply(stage);
+       resolve(stage, RESOLVE_EXPRESSIONS);
 }
 
 template<typename T>
index 0a9e8e85a81380977cdf6cbc27f767d61fcf4ebc..fe2d56b8a4737bba58f6086a070ecc3da281b691 100644 (file)
@@ -338,6 +338,119 @@ void InterfaceGenerator::visit(Passthrough &pass)
        nodes_to_remove.insert(&pass);
 }
 
+
+void ArraySizer::apply(Stage &stage)
+{
+       stage.content.visit(*this);
+       for(const auto &kvp: max_indices)
+               if(kvp.first->array && !kvp.first->array_size)
+               {
+                       int size = 0;
+                       if(stage.type==Stage::GEOMETRY && kvp.first->interface=="in")
+                               size = input_size;
+                       else if(kvp.second>=0)
+                               size = kvp.second+1;
+                       else if(!kvp.first->name.compare(0, 3, "gl_"))
+                               size = 1;
+
+                       if(size>0)
+                       {
+                               Literal *literal_size = new Literal;
+                               literal_size->token = lexical_cast<string>(size);
+                               literal_size->value = size;
+                               kvp.first->array_size = literal_size;
+                       }
+               }
+}
+
+void ArraySizer::visit(VariableReference &var)
+{
+       r_declaration = var.declaration;
+}
+
+void ArraySizer::visit(MemberAccess &memacc)
+{
+       r_declaration = 0;
+       TraversingVisitor::visit(memacc);
+       VariableDeclaration *member_declaration = 0;
+       if(r_declaration)
+               if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(r_declaration->type_declaration))
+               {
+                       auto i = strct->members.variables.find(memacc.member);
+                       if(i!=strct->members.variables.end())
+                               member_declaration = i->second;
+               }
+       r_declaration = member_declaration;
+}
+
+void ArraySizer::visit(Swizzle &swizzle)
+{
+       TraversingVisitor::visit(swizzle);
+       r_declaration = 0;
+}
+
+void ArraySizer::visit(UnaryExpression &unary)
+{
+       TraversingVisitor::visit(unary);
+       r_declaration = 0;
+}
+
+void ArraySizer::visit(BinaryExpression &binary)
+{
+       if(binary.oper->token[0]=='[')
+               if(const Literal *literal_index = dynamic_cast<const Literal *>(binary.right.get()))
+                       if(literal_index->value.check_type<int>())
+                       {
+                               r_declaration = 0;
+                               binary.left->visit(*this);
+                               if(r_declaration)
+                               {
+                                       max_indices[r_declaration] = literal_index->value.value<int>();
+                                       return;
+                               }
+                       }
+
+       TraversingVisitor::visit(binary);
+}
+
+void ArraySizer::visit(TernaryExpression &ternary)
+{
+       TraversingVisitor::visit(ternary);
+       r_declaration = 0;
+}
+
+void ArraySizer::visit(FunctionCall &call)
+{
+       TraversingVisitor::visit(call);
+       r_declaration = 0;
+}
+
+void ArraySizer::visit(InterfaceLayout &layout)
+{
+       if(layout.interface=="in")
+       {
+               for(const Layout::Qualifier &q: layout.layout.qualifiers)
+               {
+                       if(q.name=="points")
+                               input_size = 1;
+                       else if(q.name=="lines")
+                               input_size = 2;
+                       else if(q.name=="triangles")
+                               input_size = 3;
+                       else if(q.name=="lines_adjacency")
+                               input_size = 4;
+                       else if(q.name=="triangles_adjacency")
+                               input_size = 6;
+               }
+       }
+}
+
+void ArraySizer::visit(VariableDeclaration &var)
+{
+       if(var.array && !var.array_size)
+               max_indices[&var] = 0;
+}
+
 } // namespace SL
 } // namespace GL
 } // namespace Msp
index 73b933bfb63b0f04155c80ab2bb7e484f2529f03..a4ab75719e29b2416ad36e3be6918ace0beeddd1 100644 (file)
@@ -63,6 +63,32 @@ private:
        virtual void visit(Passthrough &);
 };
 
+/**
+Assigns sizes to arrays which don't have a size.  Geometry shader inputs are
+sized by topology.  Other arrays are sized by their use with literal indices.
+*/
+class ArraySizer: private TraversingVisitor
+{
+private:
+       std::map<VariableDeclaration *, int> max_indices;
+       unsigned input_size = 0;
+       VariableDeclaration *r_declaration;
+
+public:
+       void apply(Stage &);
+
+private:
+       virtual void visit(VariableReference &);
+       virtual void visit(MemberAccess &);
+       virtual void visit(Swizzle &);
+       virtual void visit(UnaryExpression&);
+       virtual void visit(BinaryExpression &);
+       virtual void visit(TernaryExpression &);
+       virtual void visit(FunctionCall &);
+       virtual void visit(InterfaceLayout &);
+       virtual void visit(VariableDeclaration &);
+};
+
 } // namespace SL
 } // namespace GL
 } // namespace Msp
index 4828a00c452ad1e35e54156f97dfaf2ef4a4ffd8..1f7d6a1b722c98289446f95a40aacd0c8101a09b 100644 (file)
@@ -375,17 +375,13 @@ SpirVGenerator::Id SpirVGenerator::get_variable_type_id(const VariableDeclaratio
        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, basic->extended_alignment);
+                       if(!var.array_size)
+                               throw logic_error("array without size");
+
+                       SetFlag set_const(constant_expression);
+                       r_expression_result_id = 0;
+                       var.array_size->visit(*this);
+                       return get_array_type_id(*basic->base_type, r_expression_result_id, basic->extended_alignment);
                }
 
        return get_id(*var.type_declaration);
index f43217ce6314c24a00d1e5885c6f405e129fba38..883b950ff07f2e46d9aa0037ddfce2fa2719c425 100644 (file)
@@ -293,6 +293,9 @@ void DeclarationValidator::visit(VariableDeclaration &var)
                        error(var, "Type 'bool' not allowed on interface variable");
        }
 
+       if(var.array && !var.array_size)
+               error(var, "Array must have a size");
+
        if(var.init_expression)
        {
                if(scope==GLOBAL && !var.constant)