From: Mikko Rasa Date: Sun, 25 Apr 2021 11:13:16 +0000 (+0300) Subject: Refactor program stage management X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=7f0e08f04536bf42b8f64e7dff5cc3e18b916c7b;p=libs%2Fgl.git Refactor program stage management There can't bee a huge number of stages in a program so a fixed-size array takes less space than a vector. It also allows accessing ids of specific stages if needed. --- diff --git a/source/core/program.cpp b/source/core/program.cpp index 038c0313..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,25 +208,24 @@ 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); if(!spec_values.empty() && !transient) transient = new TransientData; @@ -234,8 +247,9 @@ void Program::add_spirv_stages(const SpirVModule &mod, const map &s } 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 @@ -245,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); } @@ -285,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(); diff --git a/source/core/program.h b/source/core/program.h index 201d9270..090d8ed5 100644 --- a/source/core/program.h +++ b/source/core/program.h @@ -101,6 +101,14 @@ public: }; private: + enum Stage + { + VERTEX, + GEOMETRY, + FRAGMENT, + MAX_STAGES + }; + struct TransientData { std::map textures; @@ -109,7 +117,7 @@ private: }; unsigned id; - std::vector stage_ids; + unsigned stage_ids[MAX_STAGES]; const Module *module; TransientData *transient; bool linked; @@ -138,7 +146,8 @@ public: void add_stages(const Module &, const std::map & = std::map()); private: - unsigned add_stage(GLenum); + bool has_stages() const; + unsigned add_stage(Stage); void add_glsl_stages(const GlslModule &, const std::map &); void compile_glsl_stage(unsigned); void add_spirv_stages(const SpirVModule &, const std::map &);