]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/finalize.cpp
Allocate locations to interface variables
[libs/gl.git] / source / glsl / finalize.cpp
index 532e2ffcbb3914fd2d7e81dafab0f784e0b52daf..0805a29e830eb45d626970a80faa1d10ae072c3b 100644 (file)
@@ -3,6 +3,7 @@
 #include <msp/strings/lexicalcast.h>
 #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<Stage>::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<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, unsigned>::const_iterator j = uniform_locations.find((*i)->name);
+                       if(j!=uniform_locations.end())
+                       {
+                               add_location((*i)->layout, j->second);
+                               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_location((*i)->layout, next);
+               if((*i)->interface=="uniform")
+                       uniform_locations[(*i)->name] = next;
+
+               for(unsigned j=0; j<size; ++j)
+                       used.insert(next+j);
+               next += size;
+       }
+
+       unplaced_variables.erase(write, unplaced_variables.end());
+}
+
+void LocationAllocator::add_location(RefPtr<Layout> &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<size; ++i)
+                               used_locations[var.interface].insert(location+i);
+                       if(var.interface=="uniform")
+                               uniform_locations[var.name] = location;
+               }
+               else
+                       unplaced_variables.push_back(&var);
+       }
+}
+
+
 PrecisionConverter::PrecisionConverter():
        stage(0)
 { }
@@ -91,6 +199,7 @@ void LegacyConverter::apply(Stage &s, const Features &feat)
        if(supports_stage(s.type))
        {
                s.content.visit(*this);
+               NodeRemover().apply(s, nodes_to_remove);
 
                if(!stage->required_features.glsl_version)
                        stage->required_features.glsl_version = Version(1, (stage->required_features.gl_api==OPENGL_ES2 ? 0 : 10));
@@ -227,8 +336,20 @@ bool LegacyConverter::supports_interface_layouts() const
                return check_version(Version(3, 0));
        else if(check_version(Version(3, 30)))
                return true;
-       else
+       else if(check_version(Version(1, 30)))
                return check_extension(&Features::arb_explicit_attrib_location);
+       else
+               return false;
+}
+
+bool LegacyConverter::supports_stage_interface_layouts() const
+{
+       if(features.gl_api==OPENGL_ES2)
+               return check_version(Version(3, 10));
+       else if(check_version(Version(4, 10)))
+               return true;
+       else
+               return check_extension(&Features::arb_separate_shader_objects);
 }
 
 bool LegacyConverter::supports_centroid_sampling() const
@@ -251,31 +372,60 @@ bool LegacyConverter::supports_sample_sampling() const
                return check_extension(&Features::arb_gpu_shader5);
 }
 
+bool LegacyConverter::supports_uniform_location() const
+{
+       if(features.gl_api==OPENGL_ES2)
+               return check_version(Version(3, 10));
+       else if(check_version(Version(4, 30)))
+               return true;
+       else
+               return check_extension(&Features::arb_explicit_uniform_location);
+}
+
 void LegacyConverter::visit(VariableDeclaration &var)
 {
-       if(var.layout && !supports_interface_layouts())
+       if(var.layout)
        {
-               vector<Layout::Qualifier>::iterator i;
-               for(i=var.layout->qualifiers.begin(); (i!=var.layout->qualifiers.end() && i->name!="location"); ++i) ;
-               if(i!=var.layout->qualifiers.end())
+               for(vector<Layout::Qualifier>::const_iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); )
                {
-                       if(stage->type==Stage::VERTEX && var.interface=="in")
-                       {
-                               stage->locations[var.name] = i->value;
-                               var.layout->qualifiers.erase(i);
-                       }
-                       else if(stage->type==Stage::FRAGMENT && var.interface=="out")
+                       if(i->name=="location")
                        {
-                               if(check_extension(&Features::ext_gpu_shader4))
-                                       stage->locations[var.name] = i->value;
-                               else if(i->value!=0)
-                                       unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs");
-                               var.layout->qualifiers.erase(i);
-                       }
+                               bool supported = true;
+                               bool external = false;
+                               if(var.interface=="in")
+                               {
+                                       external = (stage->type==Stage::VERTEX);
+                                       supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
+                               }
+                               else if(var.interface=="out")
+                               {
+                                       external = (stage->type==Stage::FRAGMENT);
+                                       supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
+                                       if(external && !supported && !check_extension(&Features::ext_gpu_shader4))
+                                       {
+                                               external = false;
+                                               if(i->value!=0)
+                                                       unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs");
+                                       }
+                               }
+                               else if(var.interface=="uniform")
+                                       supported = supports_uniform_location();
 
-                       if(var.layout->qualifiers.empty())
-                               var.layout = 0;
+                               if(!supported)
+                               {
+                                       if(external)
+                                               stage->locations[var.name] = i->value;
+                                       i = var.layout->qualifiers.erase(i);
+                               }
+                               else
+                                       ++i;
+                       }
+                       else
+                               ++i;
                }
+
+               if(var.layout->qualifiers.empty())
+                       var.layout = 0;
        }
 
        if(var.sampling=="centroid")
@@ -318,8 +468,32 @@ bool LegacyConverter::supports_interface_blocks(const string &iface) const
                return false;
 }
 
+bool LegacyConverter::supports_interface_block_location() const
+{
+       if(features.gl_api==OPENGL_ES2)
+               return check_version(Version(3, 20));
+       else if(check_version(Version(4, 40)))
+               return true;
+       else
+               return check_extension(&Features::arb_enhanced_layouts);
+}
+
 void LegacyConverter::visit(InterfaceBlock &iface)
 {
+       if(iface.layout)
+       {
+               for(vector<Layout::Qualifier>::const_iterator i=iface.layout->qualifiers.begin(); i!=iface.layout->qualifiers.end(); )
+               {
+                       if(i->name=="location" && !supports_interface_block_location())
+                               i = iface.layout->qualifiers.erase(i);
+                       else
+                               ++i;
+               }
+
+               if(iface.layout->qualifiers.empty())
+                       iface.layout = 0;
+       }
+
        if(!supports_interface_blocks(iface.interface) && iface.type_declaration)
        {
                if(!iface.instance_name.empty())