+void LocationAllocator::apply(Module &module, const Features &features)
+{
+ for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
+ apply(*i);
+ allocate_locations("uniform");
+
+ for(vector<InterfaceBlock *>::const_iterator i=unbound_blocks.begin(); i!=unbound_blocks.end(); ++i)
+ bind_uniform((*i)->layout, (*i)->block_name, features.uniform_binding_range);
+ for(vector<VariableDeclaration *>::const_iterator i=unbound_textures.begin(); i!=unbound_textures.end(); ++i)
+ bind_uniform((*i)->layout, (*i)->name, features.texture_binding_range);
+}
+
+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<VariableDeclaration *>::iterator write = unplaced_variables.begin();
+ unsigned next = 0;
+ for(vector<VariableDeclaration *>::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<string, Uniform>::const_iterator j = uniforms.find((*i)->name);
+ if(j!=uniforms.end() && j->second.location>=0)
+ {
+ add_layout_value((*i)->layout, "location", j->second.location);
+ continue;
+ }
+ }
+
+ set<unsigned> &used = used_locations[(*i)->interface];
+
+ unsigned size = LocationCounter().apply(**i);
+ while(1)
+ {
+ int blocking = -1;
+ for(unsigned j=0; j<size; ++j)
+ if(used.count(next+j))
+ blocking = next+j;
+ if(blocking<0)
+ break;
+ next = blocking+1;
+ }
+
+ add_layout_value((*i)->layout, "location", next);
+ if((*i)->interface=="uniform")
+ uniforms[(*i)->name].location = next;
+
+ for(unsigned j=0; j<size; ++j)
+ used.insert(next+j);
+ next += size;
+ }
+
+ unplaced_variables.erase(write, unplaced_variables.end());
+}
+
+void LocationAllocator::bind_uniform(RefPtr<Layout> &layout, const string &name, unsigned range)
+{
+ map<string, Uniform>::const_iterator i = uniforms.find(name);
+ if(i!=uniforms.end() && i->second.bind_point>=0)
+ add_layout_value(layout, "binding", i->second.bind_point);
+ else
+ {
+ set<unsigned> &used = used_bindings[0];
+
+ unsigned bind_point = fold32(hash64(name))%range;
+ while(used.count(bind_point))
+ bind_point = (bind_point+1)%range;
+
+ add_layout_value(layout, "binding", bind_point);
+ uniforms[name].bind_point = bind_point;
+ used.insert(bind_point);
+ }
+}
+
+void LocationAllocator::add_layout_value(RefPtr<Layout> &layout, const string &name, unsigned value)
+{
+ if(!layout)
+ layout = new Layout;
+
+ Layout::Qualifier qual;
+ qual.name = name;
+ qual.has_value = true;
+ qual.value = value;
+ layout->qualifiers.push_back(qual);
+}
+
+void LocationAllocator::visit(VariableDeclaration &var)
+{
+ if(!var.name.compare(0, 3, "gl_"))
+ return;
+
+ if(!var.interface.empty())
+ {
+ 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_layout_value(var.layout, "location", location);
+ }
+
+ if(location>=0)
+ {
+ unsigned size = LocationCounter().apply(var);
+ for(unsigned i=0; i<size; ++i)
+ used_locations[var.interface].insert(location+i);
+ if(var.interface=="uniform")
+ uniforms[var.name].location = location;
+ }
+ else
+ unplaced_variables.push_back(&var);
+ }
+
+ if(var.interface=="uniform")
+ {
+ const TypeDeclaration *type = var.type_declaration;
+ while(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
+ type = basic->base_type;
+ if(dynamic_cast<const ImageTypeDeclaration *>(type))
+ {
+ int bind_point = (var.layout ? get_layout_value(*var.layout, "binding") : -1);
+ if(bind_point>=0)
+ {
+ used_bindings[0].insert(bind_point);
+ uniforms[var.name].bind_point = bind_point;
+ }
+ else
+ unbound_textures.push_back(&var);
+ }
+ }
+}
+
+void LocationAllocator::visit(InterfaceBlock &iface)
+{
+ if(!iface.instance_name.compare(0, 3, "gl_"))
+ return;
+
+ if(iface.interface=="uniform")
+ {
+ int bind_point = (iface.layout ? get_layout_value(*iface.layout, "binding") : -1);
+
+ if(bind_point>=0)
+ {
+ used_bindings[0].insert(bind_point);
+ uniforms[iface.block_name].bind_point = bind_point;
+ }
+ else
+ unbound_blocks.push_back(&iface);
+ }
+}
+
+