]> git.tdb.fi Git - libs/gl.git/blobdiff - source/program.cpp
Get all blocks for the program before applying them
[libs/gl.git] / source / program.cpp
index 8892bcf1ed7861b632c7c0bc5089ed53e61b4756..d962751994d38fe1e7fd93550bb51a545eaec9f6 100644 (file)
@@ -254,6 +254,8 @@ void Program::link()
                        info.block = 0;
                        info.name = name;
                        info.size = size;
+                       info.array_stride = 0;
+                       info.matrix_stride = 0;
                        info.type = type;
                        uniforms_by_index[i] = &info;
                }
@@ -295,9 +297,12 @@ void Program::link()
                        for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
                                if(uniforms_by_index[*j]->size>1)
                                        indices2.push_back(*j);
-                       glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
-                       for(unsigned j=0; j<indices2.size(); ++j)
-                               uniforms_by_index[indices[j]]->array_stride = values[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[indices[j]]->array_stride = values[j];
+                       }
 
                        indices2.clear();
                        for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
@@ -308,10 +313,14 @@ void Program::link()
                                        t==GL_FLOAT_MAT3x4 || t==GL_FLOAT_MAT4x2 || t==GL_FLOAT_MAT4x3)
                                        indices2.push_back(*j);
                        }
-                       glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
-                       for(unsigned j=0; j<indices2.size(); ++j)
-                               uniforms_by_index[indices[j]]->matrix_stride = values[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[indices[j]]->matrix_stride = values[j];
+                       }
 
+                       sort(info.uniforms.begin(), info.uniforms.end(), uniform_location_compare);
                        info.layout_hash = compute_layout_hash(info.uniforms);
                        info.bind_point = info.layout_hash%BufferRange::get_n_uniform_buffer_bindings();
                        glUniformBlockBinding(id, i, info.bind_point);
@@ -337,6 +346,11 @@ unsigned Program::compute_layout_hash(const vector<const UniformInfo *> &uniform
        return hash32(layout_descriptor);
 }
 
+bool Program::uniform_location_compare(const UniformInfo *uni1, const UniformInfo *uni2)
+{
+       return uni1->location<uni2->location;
+}
+
 string Program::get_info_log() const
 {
        GLsizei len = 0;
@@ -373,14 +387,14 @@ int Program::get_uniform_location(const string &n) const
                                add an offset. */
                                unsigned offset = lexical_cast<unsigned>(n.substr(open_bracket+1, n.size()-2-open_bracket));
                                i = uniforms.find(n.substr(0, open_bracket)+"[0]");
-                               if(i!=uniforms.end() && offset<i->second.size)
+                               if(i!=uniforms.end() && !i->second.block && offset<i->second.size)
                                        return i->second.location+offset;
                        }
                }
                return -1;
        }
 
-       return i->second.location;
+       return i->second.block ? -1 : i->second.location;
 }
 
 void Program::bind() const