X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fglsl%2Ffinalize.cpp;h=0805a29e830eb45d626970a80faa1d10ae072c3b;hp=0adb2960b1da3784a4554d9a97b447b12163481d;hb=7b03ca83ca6c48b9f8c3f77c13760e7f87e9632d;hpb=9cfccf5c4e366e033cfd5eebf8955d2b13202b46 diff --git a/source/glsl/finalize.cpp b/source/glsl/finalize.cpp index 0adb2960..0805a29e 100644 --- a/source/glsl/finalize.cpp +++ b/source/glsl/finalize.cpp @@ -3,6 +3,7 @@ #include #include "finalize.h" #include "glsl_error.h" +#include "reflect.h" using namespace std; @@ -10,6 +11,113 @@ namespace Msp { namespace GL { namespace SL { +void LocationAllocator::apply(Module &module) +{ + for(list::iterator i=module.stages.begin(); i!=module.stages.end(); ++i) + apply(*i); + allocate_locations("uniform"); +} + +void LocationAllocator::apply(Stage &stage) +{ + swap(used_locations["in"], used_locations["out"]); + used_locations["out"].clear(); + + stage.content.visit(*this); + + allocate_locations("in"); + allocate_locations("out"); +} + +void LocationAllocator::allocate_locations(const string &iface) +{ + vector::iterator write = unplaced_variables.begin(); + unsigned next = 0; + for(vector::const_iterator i=unplaced_variables.begin(); i!=unplaced_variables.end(); ++i) + { + if((*i)->interface!=iface) + { + if(write!=i) + *write = *i; + ++write; + continue; + } + + if((*i)->interface=="uniform") + { + map::const_iterator j = uniform_locations.find((*i)->name); + if(j!=uniform_locations.end()) + { + add_location((*i)->layout, j->second); + continue; + } + } + + set &used = used_locations[(*i)->interface]; + + unsigned size = LocationCounter().apply(**i); + while(1) + { + int blocking = -1; + for(unsigned j=0; jlayout, next); + if((*i)->interface=="uniform") + uniform_locations[(*i)->name] = next; + + for(unsigned j=0; j &layout, unsigned location) +{ + if(!layout) + layout = new Layout; + + Layout::Qualifier qual; + qual.name = "location"; + qual.has_value = true; + qual.value = location; + layout->qualifiers.push_back(qual); +} + +void LocationAllocator::visit(VariableDeclaration &var) +{ + if(!var.interface.empty() && var.name.compare(0, 3, "gl_")) + { + int location = (var.layout ? get_layout_value(*var.layout, "location") : -1); + + if(location<0 && var.linked_declaration && var.linked_declaration->layout) + { + location = get_layout_value(*var.linked_declaration->layout, "location"); + if(location>=0) + add_location(var.layout, location); + } + + if(location>=0) + { + unsigned size = LocationCounter().apply(var); + for(unsigned i=0; i