Split the interface queries from Program::link into separate functions
authorMikko Rasa <tdb@tdb.fi>
Thu, 20 Jun 2019 08:38:11 +0000 (11:38 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 21 Jun 2019 11:53:12 +0000 (14:53 +0300)
source/program.cpp
source/program.h

index 270dbdc3e5675163017349d0cef901fcffaccdf8..c3625f4159589916f0e83e7c9f88135266e91b49 100644 (file)
@@ -142,6 +142,12 @@ void Program::link()
                IO::print("Program link info log:\n%s", info_log);
 #endif
 
+       query_uniforms();
+       query_attributes();
+}
+
+void Program::query_uniforms()
+{
        unsigned count = get_program_i(id, GL_ACTIVE_UNIFORMS);
        vector<UniformInfo *> uniforms_by_index(count);
        for(unsigned i=0; i<count; ++i)
@@ -171,90 +177,8 @@ void Program::link()
                        legacy_vars = true;
        }
 
-       count = get_program_i(id, GL_ACTIVE_ATTRIBUTES);
-       for(unsigned i=0; i<count; ++i)
-       {
-               char name[128];
-               int len = 0;
-               int size;
-               GLenum type;
-               glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
-               if(len && !strncmp(name, "gl_", 3))
-                       legacy_vars = true;
-       }
-
        if(ARB_uniform_buffer_object)
-       {
-               uniform_blocks.clear();
-
-               std::set<unsigned> used_bind_points;
-               count = get_program_i(id, GL_ACTIVE_UNIFORM_BLOCKS);
-               for(unsigned i=0; i<count; ++i)
-               {
-                       char name[128];
-                       int len;
-                       glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
-                       UniformBlockInfo &info = uniform_blocks[name];
-                       info.name = name;
-
-                       int value;
-                       glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
-                       info.data_size = value;
-
-                       glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value);
-                       vector<int> indices(value);
-                       glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
-                       for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
-                       {
-                               if(!uniforms_by_index[*j])
-                                       throw logic_error("Program::link");
-                               info.uniforms.push_back(uniforms_by_index[*j]);
-                               uniforms_by_index[*j]->block = &info;
-                       }
-
-                       vector<unsigned> indices2(indices.begin(), indices.end());
-                       vector<int> values(indices.size());
-                       glGetActiveUniformsiv(id, indices.size(), &indices2[0], GL_UNIFORM_OFFSET, &values[0]);
-                       for(unsigned j=0; j<indices.size(); ++j)
-                               uniforms_by_index[indices[j]]->location = values[j];
-
-                       indices2.clear();
-                       for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
-                               if(uniforms_by_index[*j]->size>1)
-                                       indices2.push_back(*j);
-                       if(!indices2.empty())
-                       {
-                               glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
-                               for(unsigned j=0; j<indices2.size(); ++j)
-                                       uniforms_by_index[indices2[j]]->array_stride = values[j];
-                       }
-
-                       indices2.clear();
-                       for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
-                       {
-                               GLenum t = uniforms_by_index[*j]->type;
-                               if(t==GL_FLOAT_MAT4 || t==GL_FLOAT_MAT3 || t==GL_FLOAT_MAT2 ||
-                                       t==GL_FLOAT_MAT2x3 || t==GL_FLOAT_MAT2x4 || t==GL_FLOAT_MAT3x2 ||
-                                       t==GL_FLOAT_MAT3x4 || t==GL_FLOAT_MAT4x2 || t==GL_FLOAT_MAT4x3)
-                                       indices2.push_back(*j);
-                       }
-                       if(!indices2.empty())
-                       {
-                               glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
-                               for(unsigned j=0; j<indices2.size(); ++j)
-                                       uniforms_by_index[indices2[j]]->matrix_stride = values[j];
-                       }
-
-                       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);
-               }
-       }
+               query_uniform_blocks(uniforms_by_index);
 
        UniformBlockInfo &default_block = uniform_blocks[string()];
        default_block.data_size = 0;
@@ -276,6 +200,94 @@ void Program::link()
        uniform_layout_hash = hash32(layout_descriptor);
 }
 
+void Program::query_uniform_blocks(const vector<UniformInfo *> &uniforms_by_index)
+{
+       uniform_blocks.clear();
+
+       std::set<unsigned> used_bind_points;
+       unsigned count = get_program_i(id, GL_ACTIVE_UNIFORM_BLOCKS);
+       for(unsigned i=0; i<count; ++i)
+       {
+               char name[128];
+               int len;
+               glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
+               UniformBlockInfo &info = uniform_blocks[name];
+               info.name = name;
+
+               int value;
+               glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
+               info.data_size = value;
+
+               glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value);
+               vector<int> indices(value);
+               glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
+               for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
+               {
+                       if(!uniforms_by_index[*j])
+                               throw logic_error("Program::link");
+                       info.uniforms.push_back(uniforms_by_index[*j]);
+                       uniforms_by_index[*j]->block = &info;
+               }
+
+               vector<unsigned> indices2(indices.begin(), indices.end());
+               vector<int> values(indices.size());
+               glGetActiveUniformsiv(id, indices.size(), &indices2[0], GL_UNIFORM_OFFSET, &values[0]);
+               for(unsigned j=0; j<indices.size(); ++j)
+                       uniforms_by_index[indices[j]]->location = values[j];
+
+               indices2.clear();
+               for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
+                       if(uniforms_by_index[*j]->size>1)
+                               indices2.push_back(*j);
+               if(!indices2.empty())
+               {
+                       glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
+                       for(unsigned j=0; j<indices2.size(); ++j)
+                               uniforms_by_index[indices2[j]]->array_stride = values[j];
+               }
+
+               indices2.clear();
+               for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
+               {
+                       GLenum t = uniforms_by_index[*j]->type;
+                       if(t==GL_FLOAT_MAT4 || t==GL_FLOAT_MAT3 || t==GL_FLOAT_MAT2 ||
+                               t==GL_FLOAT_MAT2x3 || t==GL_FLOAT_MAT2x4 || t==GL_FLOAT_MAT3x2 ||
+                               t==GL_FLOAT_MAT3x4 || t==GL_FLOAT_MAT4x2 || t==GL_FLOAT_MAT4x3)
+                               indices2.push_back(*j);
+               }
+               if(!indices2.empty())
+               {
+                       glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
+                       for(unsigned j=0; j<indices2.size(); ++j)
+                               uniforms_by_index[indices2[j]]->matrix_stride = values[j];
+               }
+
+               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);
+       }
+}
+
+void Program::query_attributes()
+{
+       unsigned count = get_program_i(id, GL_ACTIVE_ATTRIBUTES);
+       for(unsigned i=0; i<count; ++i)
+       {
+               char name[128];
+               int len = 0;
+               int size;
+               GLenum type;
+               glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
+               if(len && !strncmp(name, "gl_", 3))
+                       legacy_vars = true;
+       }
+}
+
 Program::LayoutHash Program::compute_layout_hash(const vector<const UniformInfo *> &uniforms)
 {
        string layout_descriptor;
index cbb52cd4536e93bd0b2cd65c0f9868cfe946b1f5..826a49637bf9535caf0e30af2c3cdb7ca5ed222b 100644 (file)
@@ -102,6 +102,9 @@ public:
 
        void link();
 private:
+       void query_uniforms();
+       void query_uniform_blocks(const std::vector<UniformInfo *> &);
+       void query_attributes();
        static LayoutHash compute_layout_hash(const std::vector<const UniformInfo *> &);
        static bool uniform_location_compare(const UniformInfo *, const UniformInfo *);
 public: