X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fprogram.cpp;h=d6bd5ee88d889c4efc02a0b9ee86a8051b8ec9a2;hb=7f0e08f04536bf42b8f64e7dff5cc3e18b916c7b;hp=39184bbd3b9b18304cc969ffd955cf8e3ed6f4eb;hpb=c5366b68add58fbc4889186125f15205e093e310;p=libs%2Fgl.git diff --git a/source/core/program.cpp b/source/core/program.cpp index 39184bbd..d6bd5ee8 100644 --- a/source/core/program.cpp +++ b/source/core/program.cpp @@ -70,6 +70,7 @@ void Program::init() static Require _req(ARB_shader_objects); id = glCreateProgram(); + fill(stage_ids, stage_ids+MAX_STAGES, 0); module = 0; transient = 0; linked = false; @@ -77,14 +78,15 @@ void Program::init() Program::~Program() { - for(vector::iterator i=stage_ids.begin(); i!=stage_ids.end(); ++i) - glDeleteShader(*i); + for(unsigned i=0; i &spec_values) { - if(!stage_ids.empty()) + if(has_stages()) throw invalid_operation("Program::add_stages"); switch(mod.get_format()) @@ -95,18 +97,30 @@ void Program::add_stages(const Module &mod, const map &spec_values) } } -unsigned Program::add_stage(GLenum type) +bool Program::has_stages() const { + for(unsigned i=0; i &spe unsigned stage_id = 0; switch(*i) { - case SL::Stage::VERTEX: stage_id = add_stage(GL_VERTEX_SHADER); break; - case SL::Stage::GEOMETRY: stage_id = add_stage(GL_GEOMETRY_SHADER); break; - case SL::Stage::FRAGMENT: stage_id = add_stage(GL_FRAGMENT_SHADER); break; + case SL::Stage::VERTEX: stage_id = add_stage(VERTEX); break; + case SL::Stage::GEOMETRY: stage_id = add_stage(GEOMETRY); break; + case SL::Stage::FRAGMENT: stage_id = add_stage(FRAGMENT); break; default: throw invalid_operation("Program::add_glsl_stages"); } @@ -194,44 +208,48 @@ void Program::add_spirv_stages(const SpirVModule &mod, const map &s module = &mod; const vector &entry_points = mod.get_entry_points(); - std::set stages; + unsigned n_stages = 0; + unsigned used_stage_ids[MAX_STAGES]; for(vector::const_iterator i=entry_points.begin(); i!=entry_points.end(); ++i) { - if(stages.count(i->stage)) - throw invalid_argument("Program::add_spirv_stages"); - + unsigned stage_id = 0; switch(i->stage) { - case SpirVModule::VERTEX: add_stage(GL_VERTEX_SHADER); break; - case SpirVModule::GEOMETRY: add_stage(GL_GEOMETRY_SHADER); break; - case SpirVModule::FRAGMENT: add_stage(GL_FRAGMENT_SHADER); break; + case SpirVModule::VERTEX: stage_id = add_stage(VERTEX); break; + case SpirVModule::GEOMETRY: stage_id = add_stage(GEOMETRY); break; + case SpirVModule::FRAGMENT: stage_id = add_stage(FRAGMENT); break; default: throw invalid_operation("Program::add_spirv_stages"); } - stages.insert(i->stage); + used_stage_ids[n_stages++] = stage_id; } const vector &code = mod.get_code(); - glShaderBinary(stage_ids.size(), &stage_ids[0], GL_SHADER_BINARY_FORMAT_SPIR_V, &code[0], code.size()*4); + glShaderBinary(n_stages, used_stage_ids, GL_SHADER_BINARY_FORMAT_SPIR_V, &code[0], code.size()*4); - const vector &spec_consts = mod.get_spec_constants(); + if(!spec_values.empty() && !transient) + transient = new TransientData; + + const vector &spec_consts = mod.get_spec_constants(); vector spec_id_array; vector spec_value_array; spec_id_array.reserve(spec_consts.size()); spec_value_array.reserve(spec_consts.size()); - for(vector::const_iterator i=spec_consts.begin(); i!=spec_consts.end(); ++i) + for(vector::const_iterator i=spec_consts.begin(); i!=spec_consts.end(); ++i) { map::const_iterator j = spec_values.find(i->name); if(j!=spec_values.end()) { spec_id_array.push_back(i->constant_id); spec_value_array.push_back(j->second); + transient->spec_values[i->constant_id] = j->second; } } vector::const_iterator j=entry_points.begin(); - for(vector::const_iterator i=stage_ids.begin(); i!=stage_ids.end(); ++i, ++j) - glSpecializeShader(*i, j->name.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]); + for(unsigned i=0; iname.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]); } #pragma GCC diagnostic push @@ -241,7 +259,17 @@ void Program::attach_shader(Shader &shader) unsigned shader_id = shader.steal_id(); if(!shader_id) throw invalid_argument("Program::attach_shader"); - stage_ids.push_back(shader_id); + + int type; + glGetShaderiv(shader_id, GL_SHADER_TYPE, &type); + switch(type) + { + case GL_VERTEX_SHADER: stage_ids[VERTEX] = shader_id; break; + case GL_GEOMETRY_SHADER: stage_ids[GEOMETRY] = shader_id; break; + case GL_FRAGMENT_SHADER: stage_ids[FRAGMENT] = shader_id; break; + } + + glAttachShader(id, shader_id); compile_glsl_stage(shader_id); } @@ -281,7 +309,7 @@ void Program::bind_fragment_data(unsigned index, const string &name) void Program::link() { - if(stage_ids.empty()) + if(!has_stages()) throw invalid_operation("Program::link"); uniforms.clear(); @@ -333,9 +361,6 @@ void Program::link() glUniform1i(location, i->second); } } - - delete transient; - transient = 0; } } else if(module->get_format()==Module::SPIR_V) @@ -344,6 +369,9 @@ void Program::link() collect_attributes(); } + delete transient; + transient = 0; + for(vector::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i) require_type(i->type); for(vector::const_iterator i=attributes.begin(); i!=attributes.end(); ++i) @@ -570,9 +598,21 @@ void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const unsigned offset = base_offset+i->offset; if(i->struct_type) { - if(i->array_size) + unsigned array_size = i->array_size; + if(i->array_size_spec) + { + array_size = i->array_size_spec->i_value; + if(transient) + { + map::const_iterator j = transient->spec_values.find(i->array_size_spec->constant_id); + if(j!=transient->spec_values.end()) + array_size = j->second; + } + } + + if(array_size) { - for(unsigned j=0; jarray_size; ++j, offset+=i->array_stride) + for(unsigned j=0; jarray_stride) collect_block_uniforms(*i->struct_type, format("%s%s[%d].", prefix, i->name, j), offset, uniform_names); } else