From: Mikko Rasa Date: Sat, 3 Apr 2021 18:17:27 +0000 (+0300) Subject: Use uniform block and texture bindings from GLSL X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=0375be1a27f8852ed8ca422db78bf6380d2b11b1;p=libs%2Fgl.git Use uniform block and texture bindings from GLSL --- diff --git a/source/core/program.cpp b/source/core/program.cpp index 999a94ad..dd38075a 100644 --- a/source/core/program.cpp +++ b/source/core/program.cpp @@ -3,10 +3,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include @@ -69,6 +71,7 @@ void Program::init() id = glCreateProgram(); module = 0; + bindings = 0; linked = false; } @@ -156,6 +159,11 @@ void Program::add_glsl_stages(const GlslModule &mod, const map &spe compile_glsl_stage(stage_id); } + + if(!bindings) + bindings = new Bindings; + bindings->textures = compiler.get_texture_bindings(); + bindings->blocks = compiler.get_uniform_block_bindings(); } void Program::compile_glsl_stage(unsigned stage_id) @@ -301,6 +309,34 @@ void Program::link() { query_uniforms(); query_attributes(); + if(bindings) + { + for(unsigned i=0; i::const_iterator j = bindings->blocks.find(uniform_blocks[i].name); + if(j!=bindings->blocks.end()) + { + glUniformBlockBinding(id, i, j->second); + uniform_blocks[i].bind_point = j->second; + } + } + + Conditional _bind(!ARB_separate_shader_objects, this); + for(map::const_iterator i=bindings->textures.begin(); i!=bindings->textures.end(); ++i) + { + int location = get_uniform_location(i->first); + if(location>=0) + { + if(ARB_separate_shader_objects) + glProgramUniform1i(id, location, i->second); + else + glUniform1i(location, i->second); + } + } + + delete bindings; + bindings = 0; + } } else if(module->get_format()==Module::SPIR_V) { @@ -372,7 +408,6 @@ void Program::query_uniforms() void Program::query_uniform_blocks(const vector &uniforms_by_index) { - std::set used_bind_points; unsigned count = get_program_i(id, GL_ACTIVE_UNIFORM_BLOCKS); // Reserve an extra index for the default block uniform_blocks.reserve(count+1); @@ -389,6 +424,9 @@ void Program::query_uniform_blocks(const vector &uniforms_by_inde glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value); info.data_size = value; + glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_BINDING, &value); + info.bind_point = value; + glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value); vector indices(value); glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]); @@ -433,12 +471,6 @@ void Program::query_uniform_blocks(const vector &uniforms_by_inde sort(info.uniforms.begin(), info.uniforms.end(), uniform_location_compare); info.layout_hash = compute_layout_hash(info.uniforms); - unsigned n_bindings = BufferRange::get_n_uniform_buffer_bindings(); - info.bind_point = info.layout_hash%n_bindings; - while(used_bind_points.count(info.bind_point)) - info.bind_point = (info.bind_point+1)%n_bindings; - glUniformBlockBinding(id, i, info.bind_point); - used_bind_points.insert(info.bind_point); } } diff --git a/source/core/program.h b/source/core/program.h index 3af4e582..6613a134 100644 --- a/source/core/program.h +++ b/source/core/program.h @@ -98,9 +98,16 @@ public: }; private: + struct Bindings + { + std::map textures; + std::map blocks; + }; + unsigned id; std::vector stage_ids; const Module *module; + Bindings *bindings; bool linked; std::vector uniform_blocks; std::vector uniforms;