]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programcompiler.cpp
Use layout declarations to set attribute and fragment data locations
[libs/gl.git] / source / programcompiler.cpp
index d65869316423d64806a23bdf3e60254d0f014ce7..fd756de0e00cbbca4c5aa18587b251afc4e94ccb 100644 (file)
@@ -61,17 +61,20 @@ void ProgramCompiler::add_shaders(Program &program)
        for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
        {
                if(i->type==VERTEX)
+               {
                        program.attach_shader_owned(new VertexShader(apply<Formatter>(*i)));
+                       for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
+                               program.bind_attribute(j->second, j->first);
+               }
                else if(i->type==GEOMETRY)
                        program.attach_shader_owned(new GeometryShader(apply<Formatter>(*i)));
                else if(i->type==FRAGMENT)
+               {
                        program.attach_shader_owned(new FragmentShader(apply<Formatter>(*i)));
+                       for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
+                               program.bind_fragment_data(j->second, j->first);
+               }
        }
-
-       program.bind_attribute(VERTEX4, "vertex");
-       program.bind_attribute(NORMAL3, "normal");
-       program.bind_attribute(COLOR4_FLOAT, "color");
-       program.bind_attribute(TEXCOORD4, "texcoord");
 }
 
 Module *ProgramCompiler::create_builtins_module()
@@ -123,9 +126,8 @@ void ProgramCompiler::process()
 
 void ProgramCompiler::import(const string &name)
 {
-       if(!resources)
-               throw runtime_error("no resources");
-       RefPtr<IO::Seekable> io = resources->open_raw(name+".glsl");
+       string fn = name+".glsl";
+       RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
        if(!io)
                throw runtime_error(format("module %s not found", name));
        ProgramParser import_parser;
@@ -335,7 +337,13 @@ void ProgramCompiler::Formatter::visit(Layout &layout)
                if(!i->value.empty())
                        formatted += format("=%s", i->value);
        }
-       formatted += format(") %s;", layout.interface);
+       formatted += ')';
+}
+
+void ProgramCompiler::Formatter::visit(InterfaceLayout &layout)
+{
+       layout.layout.visit(*this);
+       formatted += format(" %s;", layout.interface);
 }
 
 void ProgramCompiler::Formatter::visit(StructDeclaration &strct)
@@ -347,6 +355,11 @@ void ProgramCompiler::Formatter::visit(StructDeclaration &strct)
 
 void ProgramCompiler::Formatter::visit(VariableDeclaration &var)
 {
+       if(var.layout)
+       {
+               var.layout->visit(*this);
+               formatted += ' ';
+       }
        if(var.constant)
                formatted += "const ";
        if(!var.sampling.empty())
@@ -479,6 +492,7 @@ void ProgramCompiler::DeclarationCombiner::visit(VariableDeclaration &var)
        VariableDeclaration *&ptr = variables[var.name];
        if(ptr)
        {
+               ptr->type = var.type;
                if(var.init_expression)
                        ptr->init_expression = var.init_expression;
                remove_node = true;
@@ -1283,6 +1297,7 @@ void ProgramCompiler::NodeRemover::visit(VariableDeclaration &var)
        {
                stage->in_variables.erase(var.name);
                stage->out_variables.erase(var.name);
+               stage->locations.erase(var.name);
                if(var.linked_declaration)
                        var.linked_declaration->linked_declaration = 0;
        }
@@ -1352,19 +1367,41 @@ void ProgramCompiler::LegacyConverter::visit(FunctionCall &call)
 
 void ProgramCompiler::LegacyConverter::visit(VariableDeclaration &var)
 {
-       if(var.interface=="in" || var.interface=="out")
-               if(!check_version(Version(1, 30)))
+       if(var.layout && !check_version(Version(3, 30)))
+       {
+               vector<Layout::Qualifier>::iterator i;
+               for(i=var.layout->qualifiers.begin(); (i!=var.layout->qualifiers.end() && i->identifier!="location"); ++i) ;
+               if(i!=var.layout->qualifiers.end())
                {
+                       unsigned location = lexical_cast<unsigned>(i->value);
                        if(stage->type==VERTEX && var.interface=="in")
-                               var.interface = "attribute";
-                       else if((stage->type==VERTEX && var.interface=="out") || (stage->type==FRAGMENT && var.interface=="in"))
-                               var.interface = "varying";
+                       {
+                               stage->locations[var.name] = location;
+                               var.layout->qualifiers.erase(i);
+                       }
                        else if(stage->type==FRAGMENT && var.interface=="out")
                        {
-                               frag_out_name = var.name;
-                               remove_node = true;
+                               stage->locations[var.name] = location;
+                               var.layout->qualifiers.erase(i);
                        }
+
+                       if(var.layout->qualifiers.empty())
+                               var.layout = 0;
                }
+       }
+
+       if((var.interface=="in" || var.interface=="out") && !check_version(Version(1, 30)))
+       {
+               if(stage->type==VERTEX && var.interface=="in")
+                       var.interface = "attribute";
+               else if((stage->type==VERTEX && var.interface=="out") || (stage->type==FRAGMENT && var.interface=="in"))
+                       var.interface = "varying";
+               else if(stage->type==FRAGMENT && var.interface=="out")
+               {
+                       frag_out_name = var.name;
+                       remove_node = true;
+               }
+       }
 
        TraversingVisitor::visit(var);
 }