]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programcompiler.cpp
Merge variable declaration layouts
[libs/gl.git] / source / programcompiler.cpp
index 88ec6e59d88054480457be91cd0da9e14f201f02..9a3986a12b21e094824175bf0dc7b052dd2af776 100644 (file)
@@ -1,4 +1,8 @@
+#include <msp/core/algorithm.h>
 #include <msp/core/raii.h>
+#include <msp/gl/extensions/arb_explicit_attrib_location.h>
+#include <msp/gl/extensions/arb_gpu_shader5.h>
+#include <msp/gl/extensions/arb_uniform_buffer_object.h>
 #include <msp/gl/extensions/ext_gpu_shader4.h>
 #include <msp/strings/format.h>
 #include <msp/strings/utils.h>
@@ -8,6 +12,8 @@
 #include "resources.h"
 #include "shader.h"
 
+#undef interface
+
 using namespace std;
 
 namespace {
@@ -319,6 +325,11 @@ void ProgramCompiler::Formatter::apply(ProgramSyntax::Stage &s)
                formatted += '\n';
        }
 
+       for(vector<const Extension *>::const_iterator i=s.required_extensions.begin(); i!=s.required_extensions.end(); ++i)
+               formatted += format("#extension %s: require\n", (*i)->get_name());
+       if(!s.required_extensions.empty())
+               formatted += '\n';
+
        Visitor::apply(s);
 }
 
@@ -543,24 +554,40 @@ void ProgramCompiler::Formatter::visit(Conditional &cond)
 
 void ProgramCompiler::Formatter::visit(Iteration &iter)
 {
-       formatted += "for(";
-       if(iter.init_statement)
-               iter.init_statement->visit(*this);
-       else
-               formatted += ';';
-       if(iter.condition)
+       if(!iter.init_statement && iter.condition && !iter.loop_expression)
        {
-               formatted += ' ';
+               formatted += "while(";
                iter.condition->visit(*this);
+               formatted += ')';
        }
-       formatted += ';';
-       if(iter.loop_expression)
+       else
        {
-               formatted += ' ';
-               iter.loop_expression->visit(*this);
+               formatted += "for(";
+               if(iter.init_statement)
+                       iter.init_statement->visit(*this);
+               else
+                       formatted += ';';
+               if(iter.condition)
+               {
+                       formatted += ' ';
+                       iter.condition->visit(*this);
+               }
+               formatted += ';';
+               if(iter.loop_expression)
+               {
+                       formatted += ' ';
+                       iter.loop_expression->visit(*this);
+               }
+               formatted += ')';
+       }
+
+       if(iter.body.body.empty())
+               formatted += " { }";
+       else
+       {
+               formatted += '\n';
+               iter.body.visit(*this);
        }
-       formatted += ')';
-       iter.body.visit(*this);
 }
 
 void ProgramCompiler::Formatter::visit(Return &ret)
@@ -616,6 +643,27 @@ void ProgramCompiler::DeclarationCombiner::visit(VariableDeclaration &var)
                ptr->type = var.type;
                if(var.init_expression)
                        ptr->init_expression = var.init_expression;
+               if(var.layout)
+               {
+                       if(ptr->layout)
+                       {
+                               for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
+                               {
+                                       bool found = false;
+                                       for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
+                                               if(j->identifier==i->identifier)
+                                               {
+                                                       j->value = i->value;
+                                                       found = true;
+                                               }
+
+                                       if(!found)
+                                               ptr->layout->qualifiers.push_back(*i);
+                               }
+                       }
+                       else
+                               ptr->layout = var.layout;
+               }
                remove_node = true;
        }
        else
@@ -1207,6 +1255,13 @@ void ProgramCompiler::FunctionInliner::visit(FunctionCall &call)
                inline_result = 0;
 }
 
+void ProgramCompiler::FunctionInliner::visit(VariableDeclaration &var)
+{
+       if(var.init_expression)
+               visit_and_inline(var.init_expression);
+       inline_result = 0;
+}
+
 void ProgramCompiler::FunctionInliner::visit(Return &ret)
 {
        TraversingVisitor::visit(ret);
@@ -1390,6 +1445,7 @@ ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator():
        aggregate(0),
        assignment(0),
        assignment_target(false),
+       assign_to_subscript(false),
        global_scope(true)
 { }
 
@@ -1435,6 +1491,8 @@ void ProgramCompiler::UnusedVariableLocator::visit(BinaryExpression &binary)
 {
        if(binary.oper=="[")
        {
+               if(assignment_target)
+                       assign_to_subscript = true;
                binary.left->visit(*this);
                SetForScope<bool> set(assignment_target, false);
                binary.right->visit(*this);
@@ -1446,6 +1504,7 @@ void ProgramCompiler::UnusedVariableLocator::visit(BinaryExpression &binary)
 void ProgramCompiler::UnusedVariableLocator::visit(Assignment &assign)
 {
        {
+               assign_to_subscript = false;
                SetForScope<bool> set(assignment_target, !assign.self_referencing);
                assign.left->visit(*this);
        }
@@ -1453,10 +1512,10 @@ void ProgramCompiler::UnusedVariableLocator::visit(Assignment &assign)
        assignment = &assign;
 }
 
-void ProgramCompiler::UnusedVariableLocator::record_assignment(VariableDeclaration &var, Node &node, bool self_ref)
+void ProgramCompiler::UnusedVariableLocator::record_assignment(VariableDeclaration &var, Node &node, bool chained)
 {
        VariableInfo &var_info = variables.back()[&var];
-       if(!self_ref)
+       if(!chained)
                clear_assignments(var_info, true);
        var_info.assignments.push_back(&node);
        var_info.conditionally_assigned = false;
@@ -1477,7 +1536,7 @@ void ProgramCompiler::UnusedVariableLocator::visit(ExpressionStatement &expr)
        assignment = 0;
        TraversingVisitor::visit(expr);
        if(assignment && assignment->target_declaration)
-               record_assignment(*assignment->target_declaration, expr, assignment->self_referencing);
+               record_assignment(*assignment->target_declaration, expr, (assignment->self_referencing || assign_to_subscript));
 }
 
 void ProgramCompiler::UnusedVariableLocator::visit(StructDeclaration &strct)
@@ -1658,6 +1717,13 @@ void ProgramCompiler::NodeRemover::visit(VariableDeclaration &var)
                var.init_expression = 0;
 }
 
+void ProgramCompiler::NodeRemover::visit(Iteration &iter)
+{
+       if(to_remove.count(iter.init_statement.get()))
+               iter.init_statement = 0;
+       TraversingVisitor::visit(iter);
+}
+
 
 void ProgramCompiler::PrecisionRemover::visit(Precision &)
 {
@@ -1740,6 +1806,18 @@ bool ProgramCompiler::LegacyConverter::check_version(const Version &feature_vers
        return true;
 }
 
+bool ProgramCompiler::LegacyConverter::check_extension(const Extension &extension) const
+{
+       if(!extension)
+               return false;
+
+       vector<const Extension *>::iterator i = find(stage->required_extensions, &extension);
+       if(i==stage->required_extensions.end())
+               stage->required_extensions.push_back(&extension);
+
+       return true;
+}
+
 bool ProgramCompiler::LegacyConverter::supports_unified_interface_syntax() const
 {
        if(target_api==OPENGL_ES2)
@@ -1808,24 +1886,30 @@ bool ProgramCompiler::LegacyConverter::supports_interface_layouts() const
 {
        if(target_api==OPENGL_ES2)
                return check_version(Version(3, 0));
+       else if(check_version(Version(3, 30)))
+               return true;
        else
-               return check_version(Version(3, 30));
+               return check_extension(ARB_explicit_attrib_location);
 }
 
 bool ProgramCompiler::LegacyConverter::supports_centroid_sampling() const
 {
        if(target_api==OPENGL_ES2)
                return check_version(Version(3, 0));
+       else if(check_version(Version(1, 20)))
+               return true;
        else
-               return check_version(Version(1, 20));
+               return check_extension(EXT_gpu_shader4);
 }
 
 bool ProgramCompiler::LegacyConverter::supports_sample_sampling() const
 {
        if(target_api==OPENGL_ES2)
                return check_version(Version(3, 20));
+       else if(check_version(Version(4, 0)))
+               return true;
        else
-               return check_version(Version(4, 0));
+               return check_extension(ARB_gpu_shader5);
 }
 
 void ProgramCompiler::LegacyConverter::visit(VariableDeclaration &var)
@@ -1887,8 +1971,12 @@ bool ProgramCompiler::LegacyConverter::supports_interface_blocks(const string &i
                else
                        return check_version(Version(3, 20));
        }
+       else if(check_version(Version(1, 50)))
+               return true;
+       else if(iface=="uniform")
+               return check_extension(ARB_uniform_buffer_object);
        else
-               return check_version(Version(1, 50));
+               return false;
 }
 
 void ProgramCompiler::LegacyConverter::visit(InterfaceBlock &iface)