From 89f26eba675defb38542faacacde6b9d4a2248c5 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 29 Nov 2021 01:01:01 +0200 Subject: [PATCH] Automatically determine array sizes in GLSL if possible Consequently the SPIR-V backend no longer needs to deal with missing array sizes. --- source/glsl/compiler.cpp | 3 ++ source/glsl/generate.cpp | 113 +++++++++++++++++++++++++++++++++++++++ source/glsl/generate.h | 26 +++++++++ source/glsl/spirv.cpp | 18 +++---- source/glsl/validate.cpp | 3 ++ 5 files changed, 152 insertions(+), 11 deletions(-) diff --git a/source/glsl/compiler.cpp b/source/glsl/compiler.cpp index 16ac375c..f1ab6d08 100644 --- a/source/glsl/compiler.cpp +++ b/source/glsl/compiler.cpp @@ -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 diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 0a9e8e85..fe2d56b8 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -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(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(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(binary.right.get())) + if(literal_index->value.check_type()) + { + r_declaration = 0; + binary.left->visit(*this); + if(r_declaration) + { + max_indices[r_declaration] = literal_index->value.value(); + 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 diff --git a/source/glsl/generate.h b/source/glsl/generate.h index 73b933bf..a4ab7571 100644 --- a/source/glsl/generate.h +++ b/source/glsl/generate.h @@ -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 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 diff --git a/source/glsl/spirv.cpp b/source/glsl/spirv.cpp index 4828a00c..1f7d6a1b 100644 --- a/source/glsl/spirv.cpp +++ b/source/glsl/spirv.cpp @@ -375,17 +375,13 @@ SpirVGenerator::Id SpirVGenerator::get_variable_type_id(const VariableDeclaratio if(const BasicTypeDeclaration *basic = dynamic_cast(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); diff --git a/source/glsl/validate.cpp b/source/glsl/validate.cpp index f43217ce..883b950f 100644 --- a/source/glsl/validate.cpp +++ b/source/glsl/validate.cpp @@ -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) -- 2.45.2