X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Ffinalize.cpp;h=eee8a5198efa8b2037848bcc7a34bd8cd0d333ff;hb=c4aeeced7b397d46772577775bd3a0d6c4706cba;hp=b8c3378cc5ee6e6ff010e3f7b65bde411468b6f6;hpb=599f50ec34aba2c35dcab9d17d8b9fc6bfbd0a07;p=libs%2Fgl.git diff --git a/source/glsl/finalize.cpp b/source/glsl/finalize.cpp index b8c3378c..eee8a519 100644 --- a/source/glsl/finalize.cpp +++ b/source/glsl/finalize.cpp @@ -1,8 +1,10 @@ #include +#include #include #include #include "finalize.h" #include "glsl_error.h" +#include "reflect.h" using namespace std; @@ -10,6 +12,232 @@ namespace Msp { namespace GL { namespace SL { +StructOrganizer::StructOrganizer(): + offset(-1) +{ } + +void StructOrganizer::visit(StructDeclaration &strct) +{ + SetForScope set_offset(offset, 0); + TraversingVisitor::visit(strct); +} + +void StructOrganizer::visit(VariableDeclaration &var) +{ + if(offset>=0) + { + int *layout_offset = 0; + bool has_matrix_order = false; + if(var.layout) + { + for(Layout::Qualifier &q: var.layout->qualifiers) + { + if(q.name=="offset" && q.has_value) + { + layout_offset = &q.value; + if(q.value>=offset) + offset = q.value; + } + else if(q.name=="column_major" || q.name=="row_major") + has_matrix_order = true; + } + } + + MemoryRequirementsCalculator::Result mem_reqs = MemoryRequirementsCalculator().apply(var); + offset += mem_reqs.alignment-1; + offset -= offset%mem_reqs.alignment; + + if(layout_offset) + *layout_offset = offset; + else + { + if(!var.layout) + var.layout = new Layout; + + var.layout->qualifiers.push_back(Layout::Qualifier("offset", offset)); + } + + if(!has_matrix_order) + { + const BasicTypeDeclaration *basic = dynamic_cast(var.type_declaration); + while(basic && basic->kind==BasicTypeDeclaration::ARRAY) + basic = dynamic_cast(basic->base_type); + if(basic && basic->kind==BasicTypeDeclaration::MATRIX) + var.layout->qualifiers.push_back(Layout::Qualifier("column_major")); + } + + offset += mem_reqs.size; + } +} + + +void LocationAllocator::apply(Module &module, const Features &features) +{ + for(Stage &s: module.stages) + apply(s); + allocate_locations("uniform"); + + for(InterfaceBlock *b: unbound_blocks) + bind_uniform(b->layout, b->block_name, features.uniform_binding_range); + for(VariableDeclaration *t: unbound_textures) + bind_uniform(t->layout, t->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) +{ + auto write = unplaced_variables.begin(); + unsigned next = 0; + for(auto i=unplaced_variables.begin(); i!=unplaced_variables.end(); ++i) + { + if((*i)->interface!=iface) + { + if(write!=i) + *write = *i; + ++write; + continue; + } + + if((*i)->interface=="uniform") + { + auto j = uniforms.find((*i)->name); + if(j!=uniforms.end() && j->second.location>=0) + { + add_layout_value((*i)->layout, "location", j->second.location); + continue; + } + } + + set &used = used_locations[(*i)->interface]; + + unsigned size = LocationCounter().apply(**i); + while(1) + { + int blocking = -1; + for(unsigned j=0; jlayout, "location", next); + if((*i)->interface=="uniform") + uniforms[(*i)->name].location = next; + + for(unsigned j=0; j &layout, const string &name, unsigned range) +{ + auto i = uniforms.find(name); + if(i!=uniforms.end() && i->second.bind_point>=0) + add_layout_value(layout, "binding", i->second.bind_point); + else + { + set &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, const string &name, unsigned value) +{ + if(!layout) + layout = new Layout; + + layout->qualifiers.push_back(Layout::Qualifier(name, value)); +} + +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(type)) + type = basic->base_type; + if(dynamic_cast(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); + } +} + + PrecisionConverter::PrecisionConverter(): stage(0) { } @@ -23,7 +251,7 @@ void PrecisionConverter::apply(Stage &s) void PrecisionConverter::visit(Block &block) { - for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ++i) + for(auto i=block.body.begin(); i!=block.body.end(); ++i) { if(&block==&stage->content) insert_point = i; @@ -112,7 +340,7 @@ void LegacyConverter::unsupported(const string &reason) void LegacyConverter::visit(Block &block) { - for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ++i) + for(auto i=block.body.begin(); i!=block.body.end(); ++i) { if(&block==&stage->content) uniform_insert_point = i; @@ -228,8 +456,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 @@ -252,23 +492,69 @@ 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); +} + +bool LegacyConverter::supports_binding() const +{ + if(features.gl_api==OPENGL_ES2) + return check_version(Version(3, 10)); + else + return check_version(Version(4, 20)); +} + void LegacyConverter::visit(VariableDeclaration &var) { if(var.layout) { - for(vector::const_iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ) + for(auto i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ) { - if(i->name=="location" && !supports_interface_layouts()) + if(i->name=="location") { - if(stage->type==Stage::VERTEX && var.interface=="in") - stage->locations[var.name] = i->value; - else if(stage->type==Stage::FRAGMENT && var.interface=="out") + 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") { - if(check_extension(&Features::ext_gpu_shader4)) + 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(!supported) + { + if(external) stage->locations[var.name] = i->value; - else if(i->value!=0) - unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs"); + i = var.layout->qualifiers.erase(i); } + else + ++i; + } + else if(i->name=="binding" && !supports_binding()) + { + const TypeDeclaration *type = var.type_declaration; + while(const BasicTypeDeclaration *basic = dynamic_cast(type)) + type = basic->base_type; + if(dynamic_cast(type)) + stage->texture_bindings[var.name] = i->value; i = var.layout->qualifiers.erase(i); } @@ -320,8 +606,37 @@ 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(auto 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 if(i->name=="binding" && !supports_binding()) + { + stage->uniform_block_bindings[iface.block_name] = i->value; + 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())