X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fcore%2Fmodule.cpp;h=dee124619f1c8a2ec3db7109b77d992a9109d158;hp=2474feaeb4cfffc8907caf4c1de730d40e5718cd;hb=7ef75a4c4dbfc437e466381dd67c23357e607b82;hpb=52335491d6e3b568f2b19bd0ec15c8d7b5f011da diff --git a/source/core/module.cpp b/source/core/module.cpp index 2474feae..dee12461 100644 --- a/source/core/module.cpp +++ b/source/core/module.cpp @@ -1,4 +1,6 @@ +#include #include +#include "device.h" #include "module.h" #include "resources.h" @@ -36,6 +38,7 @@ enum SpirVConstants DECO_SPEC_ID = 1, DECO_ARRAY_STRIDE = 6, DECO_MATRIX_STRIDE = 7, + DECO_BUILTIN = 11, DECO_LOCATION = 30, DECO_BINDING = 33, DECO_DESCRIPTOR_SET = 34, @@ -47,14 +50,14 @@ namespace GL { void Module::set_source(const string &src) { - SL::Compiler compiler; + SL::Compiler compiler(create_features()); compiler.set_source(src); compile(compiler); } void Module::load_source(IO::Base &io, Resources *res, const string &name) { - SL::Compiler compiler; + SL::Compiler compiler(create_features()); compiler.load_source(io, res, name); compile(compiler); } @@ -64,6 +67,20 @@ void Module::load_source(IO::Base &io, const string &name) load_source(io, 0, name); } +SL::Features Module::create_features() const +{ + const DeviceInfo &dev_info = Device::get_current().get_info(); + const SL::Features &device_features = dev_info.glsl_features; + SL::Features latest_features = SL::Features::latest(dev_info.api); + SL::Features features; + features.target_api = latest_features.target_api; + features.glsl_version = latest_features.glsl_version; + features.constant_id_range = device_features.constant_id_range; + features.uniform_binding_range = device_features.uniform_binding_range; + features.texture_binding_range = device_features.texture_binding_range; + return features; +} + void GlslModule::compile(SL::Compiler &compiler) { @@ -79,44 +96,9 @@ void GlslModule::compile(SL::Compiler &compiler) } -SpirVModule::SpirVModule(const SpirVModule &other): - code(other.code), - entry_points(other.entry_points), - structs(other.structs), - variables(other.variables) -{ - remap_pointers_from(other); -} - -SpirVModule &SpirVModule::operator=(const SpirVModule &other) -{ - code = other.code; - entry_points = other.entry_points; - structs = other.structs; - variables = other.variables; - remap_pointers_from(other); - return *this; -} - -void SpirVModule::remap_pointers_from(const SpirVModule &other) -{ - for(vector::iterator i=entry_points.begin(); i!=entry_points.end(); ++i) - for(vector::iterator j=i->globals.begin(); j!=i->globals.end(); ++j) - *j = &variables[*j-&other.variables.front()]; - - for(vector::iterator i=variables.begin(); i!=variables.end(); ++i) - if(i->struct_type) - i->struct_type = &structs[i->struct_type-&other.structs.front()]; - - for(vector::iterator i=structs.begin(); i!=structs.end(); ++i) - for(vector::iterator j=i->members.begin(); j!=i->members.end(); ++j) - if(j->struct_type) - j->struct_type = &structs[j->struct_type-&other.structs.front()]; -} - void SpirVModule::load_code(IO::Base &io) { - UInt32 buffer[1024]; + uint32_t buffer[1024]; while(1) { unsigned len = io.read(reinterpret_cast(buffer), sizeof(buffer)); @@ -144,8 +126,8 @@ void SpirVModule::reflect() if(code[0]==SPIRV_MAGIC_REVERSED) { - for(vector::iterator i=code.begin(); i!=code.end(); ++i) - *i = ((*i&0xFF)<<24) || ((*i&0xFF00)<<8) | ((*i>>8)&0xFF00) | ((*i>>24)&0xFF); + for(uint32_t &c: code) + c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF); } else if(code[0]!=SPIRV_MAGIC) throw invalid_module("SPIR-V magic number not found"); @@ -154,124 +136,83 @@ void SpirVModule::reflect() reflection.reflect_code(code); map spec_indices; - for(map::const_iterator i=reflection.constants.begin(); i!=reflection.constants.end(); ++i) - if(i->second.constant_id>=0) + for(const auto &kvp: reflection.constants) + if(kvp.second.constant_id>=0) { - spec_indices[&i->second] = spec_constants.size(); - spec_constants.push_back(i->second); + spec_indices[&kvp.second] = spec_constants.size(); + spec_constants.push_back(kvp.second); } map struct_indices; structs.reserve(reflection.structs.size()); - for(map::const_iterator i=reflection.structs.begin(); i!=reflection.structs.end(); ++i) + for(const auto &kvp: reflection.structs) { - struct_indices[&i->second] = structs.size(); - structs.push_back(i->second); + struct_indices[&kvp.second] = structs.size(); + structs.push_back(kvp.second); } - for(vector::iterator i=structs.begin(); i!=structs.end(); ++i) + for(Structure &s: structs) { - for(vector::iterator j=i->members.begin(); j!=i->members.end(); ++j) - { - if(j->struct_type) - { - map::const_iterator k = struct_indices.find(j->struct_type); - j->struct_type = (k!=struct_indices.end() ? &structs[k->second] : 0); - } - if(j->array_size_spec) + for(StructMember &m: s.members) + if(m.struct_type) { - map::const_iterator k = spec_indices.find(j->array_size_spec); - j->array_size_spec = (k!=spec_indices.end() ? &spec_constants[k->second] : 0); + auto i = struct_indices.find(m.struct_type); + m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0); } - } - const StructMember *last_member = &i->members.back(); + const StructMember *last_member = &s.members.back(); unsigned last_offset = last_member->offset; while(last_member->struct_type) { const StructMember *lm = &last_member->struct_type->members.back(); if(last_member->array_size) last_offset += last_member->array_stride*(last_member->array_size-1); - else if(last_member->array_size_spec) - last_offset += last_member->array_stride*(last_member->array_size_spec->i_value-1); last_offset += lm->offset; last_member = lm; } - i->size = last_offset+get_type_size(last_member->type); - i->size = (i->size+15)&~15; + unsigned last_size = get_type_size(last_member->type); + if(last_member->array_size) + last_size += last_member->array_stride*(last_member->array_size-1); + s.size = last_offset+last_size; + s.size = (s.size+15)&~15; } map var_indices; variables.reserve(reflection.variables.size()); - for(map::const_iterator i=reflection.variables.begin(); i!=reflection.variables.end(); ++i) + for(const auto &kvp: reflection.variables) { - int dup_index = -1; - for(vector::const_iterator j=variables.begin(); (dup_index<0 && j!=variables.end()); ++j) - if(*j==i->second) - dup_index = j-variables.begin(); - - if(dup_index>=0) - var_indices[&i->second] = dup_index; + auto i = find_if(variables, [&kvp](const Variable &v){ return v==kvp.second; }); + if(i!=variables.end()) + var_indices[&kvp.second] = i-variables.begin(); else { - var_indices[&i->second] = variables.size(); - variables.push_back(i->second); + var_indices[&kvp.second] = variables.size(); + variables.push_back(kvp.second); } } - for(vector::iterator i=variables.begin(); i!=variables.end(); ++i) - { - if(i->struct_type) + for(Variable &v: variables) + if(v.struct_type) { - map::const_iterator j = struct_indices.find(i->struct_type); - i->struct_type = (j!=struct_indices.end() ? &structs[j->second] : 0); + auto i = struct_indices.find(v.struct_type); + v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0); } - if(i->array_size_spec) - { - map::const_iterator j = spec_indices.find(i->array_size_spec); - i->array_size_spec = (j!=spec_indices.end() ? &spec_constants[j->second] : 0); - } - } entry_points.reserve(reflection.entry_points.size()); - for(map::const_iterator i=reflection.entry_points.begin(); i!=reflection.entry_points.end(); ++i) + for(const auto &kvp: reflection.entry_points) { - entry_points.push_back(i->second); + entry_points.push_back(kvp.second); EntryPoint &entry = entry_points.back(); - for(vector::iterator j=entry.globals.begin(); j!=entry.globals.end(); ++j) + for(const Variable *&v: entry.globals) { - map::const_iterator k = var_indices.find(*j); - *j = (k!=var_indices.end() ? &variables[k->second] : 0); + auto i = var_indices.find(v); + v = (i!=var_indices.end() ? &variables[i->second] : 0); } } } -SpirVModule::EntryPoint::EntryPoint(): - stage(VERTEX) -{ } - - -SpirVModule::StructMember::StructMember(): - type(VOID), - struct_type(0), - offset(0), - array_size(0), - array_size_spec(0), - array_stride(0), - matrix_stride(0) -{ } - - -SpirVModule::Variable::Variable(): - type(VOID), - struct_type(0), - location(-1), - descriptor_set(-1), - binding(-1) -{ } - bool SpirVModule::Variable::operator==(const Variable &other) const { if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM) @@ -284,17 +225,7 @@ bool SpirVModule::Variable::operator==(const Variable &other) const } -SpirVModule::TypeInfo::TypeInfo(): - type(VOID), - struct_type(0), - array_size_spec(0), - array_size(0), - array_stride(0), - storage(static_cast(-1)) -{ } - - -UInt32 SpirVModule::Reflection::get_opcode(UInt32 op) +uint32_t SpirVModule::Reflection::get_opcode(uint32_t op) { return op&0xFFFF; } @@ -326,7 +257,7 @@ string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator throw invalid_module("Unterminated SPIR-V string literal"); } -void SpirVModule::Reflection::reflect_code(const vector &code) +void SpirVModule::Reflection::reflect_code(const vector &code) { for(CodeIterator op=code.begin()+5; op!=code.end(); ) { @@ -344,7 +275,7 @@ void SpirVModule::Reflection::reflect_code(const vector &code) case OP_TYPE_INT: reflect_int_type(op); break; case OP_TYPE_FLOAT: reflect_float_type(op); break; case OP_TYPE_VECTOR: reflect_vector_type(op); break; - case OP_TYPE_MATRIX: reflect_vector_type(op); break; + case OP_TYPE_MATRIX: reflect_matrix_type(op); break; case OP_TYPE_IMAGE: reflect_image_type(op); break; case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break; case OP_TYPE_ARRAY: reflect_array_type(op); break; @@ -427,7 +358,7 @@ void SpirVModule::Reflection::reflect_vector_type(CodeIterator op) TypeInfo &type = types[*(op+1)]; DataType component = types[*(op+2)].type; unsigned count = *(op+3); - type.type = static_cast((count<<12) | (component&0xF00) | ((component&0xFF)*count)); + type.type = static_cast(((count-1)<<12) | (component&0xF00) | ((component&0xFF)*count)); } void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op) @@ -435,7 +366,7 @@ void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op) TypeInfo &type = types[*(op+1)]; DataType column = types[*(op+2)].type; unsigned count = *(op+3); - type.type = static_cast((count<<16) | (column&0xF00) | ((column&0xFF)*count)); + type.type = static_cast(((count-1)<<14) | (column&0x3F00) | ((column&0xFF)*count)); } void SpirVModule::Reflection::reflect_image_type(CodeIterator op) @@ -445,7 +376,7 @@ void SpirVModule::Reflection::reflect_image_type(CodeIterator op) unsigned dimensions = *(op+3); bool depth = *(op+4)==1; bool array = *(op+5); - type.type = static_cast((depth*0x200000) | (array*0x80000) | (dimensions+1) | sample); + type.type = static_cast((depth*0x200000) | (array*0x80000) | ((dimensions+1)<<16) | sample); } void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op) @@ -463,9 +394,7 @@ void SpirVModule::Reflection::reflect_array_type(CodeIterator op) type.struct_type = elem.struct_type; const Constant &size = constants[*(op+3)]; - if(size.constant_id>=0) - type.array_size_spec = &size; - else if(size.type==INT || size.type==UNSIGNED_INT) + if(size.type==INT || size.type==UNSIGNED_INT) type.array_size = size.i_value; } @@ -479,14 +408,13 @@ void SpirVModule::Reflection::reflect_struct_type(CodeIterator op) op += 2; strct.members.resize(op_end-op); - vector::iterator mem = strct.members.begin(); + auto mem = strct.members.begin(); for(; op!=op_end; ++op, ++mem) { TypeInfo &type = types[*op]; mem->type = type.type; mem->struct_type = type.struct_type; mem->array_size = type.array_size; - mem->array_size_spec = type.array_size_spec; mem->array_stride = type.array_stride; } } @@ -524,7 +452,6 @@ void SpirVModule::Reflection::reflect_variable(CodeIterator op) var.type = type.type; var.struct_type = type.struct_type; var.array_size = type.array_size; - var.array_size_spec = type.array_size_spec; } void SpirVModule::Reflection::reflect_decorate(CodeIterator op) @@ -541,6 +468,9 @@ void SpirVModule::Reflection::reflect_decorate(CodeIterator op) case DECO_ARRAY_STRIDE: types[id].array_stride = *op; break; + case DECO_BUILTIN: + variables[id].builtin = static_cast(*op); + break; case DECO_LOCATION: variables[id].location = *op; break; @@ -568,6 +498,9 @@ void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op) case DECO_MATRIX_STRIDE: member.matrix_stride = *op; break; + case DECO_BUILTIN: + member.builtin = static_cast(*op); + break; case DECO_OFFSET: member.offset = *op; break;