]> git.tdb.fi Git - libs/gl.git/commitdiff
Add a module for builtin interface variables
authorMikko Rasa <tdb@tdb.fi>
Sat, 12 Nov 2016 18:08:24 +0000 (20:08 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 12 Nov 2016 18:08:24 +0000 (20:08 +0200)
Required to make unused assignment removal work for gl_Position.

source/programcompiler.cpp
source/programcompiler.h
source/programparser.cpp
source/programsyntax.cpp
source/programsyntax.h

index 096a5d84927a062904f92e45b28421a554a0c07e..541fd8e29cd1c6ddb059e4f3cf155e5cf4d4a5c2 100644 (file)
@@ -8,6 +8,26 @@
 
 using namespace std;
 
+namespace {
+
+const char builtins_src[] =
+       "////// vertex\n"
+       "out gl_PerVertex {\n"
+       "  vec4 gl_Position;\n"
+       "  float gl_ClipDistance[];\n"
+       "};"
+       "////// geometry\n"
+       "in gl_PerVertex {\n"
+       "  vec4 gl_Position;\n"
+       "  float gl_ClipDistance[];\n"
+       "} gl_in[];\n"
+       "out gl_PerVertex {\n"
+       "  vec4 gl_Position;\n"
+       "  float gl_ClipDistance[];\n"
+       "};\n";
+
+}
+
 namespace Msp {
 namespace GL {
 
@@ -51,6 +71,35 @@ void ProgramCompiler::add_shaders(Program &program)
        program.bind_attribute(TEXCOORD4, "texcoord");
 }
 
+Module *ProgramCompiler::create_builtins_module()
+{
+       ProgramParser parser;
+       Module *module = new Module(parser.parse(builtins_src));
+       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+       {
+               VariableResolver resolver;
+               i->content.visit(resolver);
+               for(map<string, VariableDeclaration *>::iterator j=i->content.variables.begin(); j!=i->content.variables.end(); ++j)
+                       j->second->linked_declaration = j->second;
+       }
+       return module;
+}
+
+Module &ProgramCompiler::get_builtins_module()
+{
+       static RefPtr<Module> builtins_module = create_builtins_module();
+       return *builtins_module;
+}
+
+Stage *ProgramCompiler::get_builtins(StageType type)
+{
+       Module &module = get_builtins_module();
+       for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
+               if(i->type==type)
+                       return &*i;
+       return 0;
+}
+
 void ProgramCompiler::process()
 {
        for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
@@ -242,7 +291,7 @@ void ProgramCompiler::Formatter::visit(VariableDeclaration &var)
                formatted += "const ";
        if(!var.sampling.empty())
                formatted += format("%s ", var.sampling);
-       if(!var.interface.empty())
+       if(!var.interface.empty() && var.interface!=block_interface)
                formatted += format("%s ", var.interface);
        formatted += format("%s %s", var.type, var.name);
        if(var.array)
@@ -263,6 +312,7 @@ void ProgramCompiler::Formatter::visit(VariableDeclaration &var)
 
 void ProgramCompiler::Formatter::visit(InterfaceBlock &iface)
 {
+       SetForScope<string> set(block_interface, iface.interface);
        formatted += format("%s %s\n", iface.interface, iface.name);
        iface.members.visit(*this);
        formatted += ';';
@@ -333,6 +383,17 @@ ProgramCompiler::VariableResolver::VariableResolver():
        anonymous(false)
 { }
 
+void ProgramCompiler::VariableResolver::apply(Stage &s)
+{
+       SetForScope<Stage *> set(stage, &s);
+       Stage *builtins = get_builtins(stage->type);
+       if(builtins)
+               blocks.push_back(&builtins->content);
+       stage->content.visit(*this);
+       if(builtins)
+               blocks.pop_back();
+}
+
 void ProgramCompiler::VariableResolver::visit(Block &block)
 {
        blocks.push_back(&block);
@@ -407,6 +468,9 @@ void ProgramCompiler::VariableResolver::visit(VariableDeclaration &var)
                        var.type_declaration = j->second;
        }
 
+       if(!block_interface.empty() && var.interface.empty())
+               var.interface = block_interface;
+
        TraversingVisitor::visit(var);
        blocks.back()->variables[var.name] = &var;
        if(anonymous && blocks.size()>1)
@@ -416,6 +480,7 @@ void ProgramCompiler::VariableResolver::visit(VariableDeclaration &var)
 void ProgramCompiler::VariableResolver::visit(InterfaceBlock &iface)
 {
        SetFlag set(anonymous);
+       SetForScope<string> set2(block_interface, iface.interface);
        TraversingVisitor::visit(iface);
 }
 
index 829f75358cdd08695d16730184f109cc03e5c0d6..695c2e260a7c65ec4ced67b5c9b15c7c537c5d16 100644 (file)
@@ -28,6 +28,7 @@ private:
                unsigned indent;
                bool parameter_list;
                bool else_if;
+               std::string block_interface;
 
                Formatter();
 
@@ -55,9 +56,11 @@ private:
                std::vector<ProgramSyntax::Block *> blocks;
                ProgramSyntax::StructDeclaration *type;
                bool anonymous;
+               std::string block_interface;
 
                VariableResolver();
 
+               virtual void apply(ProgramSyntax::Stage &);
                virtual void visit(ProgramSyntax::Block &);
                virtual void visit(ProgramSyntax::VariableReference &);
                virtual void visit(ProgramSyntax::MemberAccess &);
@@ -137,6 +140,9 @@ public:
        void add_shaders(Program &);
 
 private:
+       static ProgramSyntax::Module *create_builtins_module();
+       static ProgramSyntax::Module &get_builtins_module();
+       static ProgramSyntax::Stage *get_builtins(ProgramSyntax::StageType);
        void process();
        void generate(ProgramSyntax::Stage &);
        bool optimize(ProgramSyntax::Stage &);
index 36cc6e081b1da26d5ef745b9bd7a66e8ff7099b5..11c9a9fe4060acd391b390cbb36749b82f998d6f 100644 (file)
@@ -650,7 +650,16 @@ InterfaceBlock *ProgramParser::parse_interface_block()
 
        iface->name = expect_identifier();
        parse_block(iface->members, true);
-       expect(";");
+       if(!check(";"))
+       {
+               iface->instance_name = expect_identifier();
+               if(check("["))
+               {
+                       iface->array = true;
+                       expect("]");
+               }
+               expect(";");
+       }
 
        return iface.release();
 }
index 4341f06b72c4542393c052d62c39acccb80fe53e..dcd19f2bb3b52b0ac75dac2f6f78c0900d3f1edf 100644 (file)
@@ -110,7 +110,8 @@ void VariableDeclaration::visit(NodeVisitor &visitor)
 }
 
 
-InterfaceBlock::InterfaceBlock()
+InterfaceBlock::InterfaceBlock():
+       array(false)
 {
        members.use_braces = true;
 }
index cbce078e2d65c1eb74bb9985fa2639c4bc525326..88f333ac1559d2e662dbaeca7903aecfbdbfa1ed 100644 (file)
@@ -204,6 +204,8 @@ struct InterfaceBlock: Node
        std::string interface;
        std::string name;
        Block members;
+       std::string instance_name;
+       bool array;
 
        InterfaceBlock();