-void Program::query_uniforms()
-{
- unsigned count = get_program_i(id, GL_ACTIVE_UNIFORMS);
- uniforms.reserve(count);
- vector<string> uniform_names(count);
- for(unsigned i=0; i<count; ++i)
- {
- char name[128];
- int len = 0;
- int size;
- GLenum type;
- glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
- if(len && strncmp(name, "gl_", 3))
- {
- /* Some implementations report the first element of a uniform array,
- others report just the name of the array itself. */
- if(len>3 && !strcmp(name+len-3, "[0]"))
- name[len-3] = 0;
-
- uniforms.push_back(UniformInfo());
- UniformInfo &info = uniforms.back();
- info.name = name;
- info.tag = name;
- info.array_size = size;
- info.type = from_gl_type(type);
- uniform_names[i] = name;
- }
- }
-
- sort_member(uniforms, &UniformInfo::tag);
-
- if(ARB_uniform_buffer_object)
- {
- vector<UniformInfo *> uniforms_by_index(count);
- for(unsigned i=0; i<count; ++i)
- if(!uniform_names[i].empty())
- // The element is already known to be present
- uniforms_by_index[i] = &*lower_bound_member(uniforms, Tag(uniform_names[i]), &UniformInfo::tag);
- query_uniform_blocks(uniforms_by_index);
- }
-
- uniform_blocks.push_back(UniformBlockInfo());
- UniformBlockInfo &default_block = uniform_blocks.back();
-
- for(UniformInfo &u: uniforms)
- if(!u.block)
- {
- u.location = glGetUniformLocation(id, u.name.c_str());
- u.block = &default_block;
- default_block.uniforms.push_back(&u);
-
- if(is_image(u.type) && u.location>=0)
- glGetUniformiv(id, u.location, &u.binding);
- }
-
- default_block.layout_hash = compute_layout_hash(default_block.uniforms);
-
- update_layout_hash();
-}
-
-void Program::query_uniform_blocks(const vector<UniformInfo *> &uniforms_by_index)
-{
- unsigned count = get_program_i(id, GL_ACTIVE_UNIFORM_BLOCKS);
- // Reserve an extra index for the default block
- uniform_blocks.reserve(count+1);
- for(unsigned i=0; i<count; ++i)
- {
- char name[128];
- int len;
- glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
- uniform_blocks.push_back(UniformBlockInfo());
- UniformBlockInfo &info = uniform_blocks.back();
- info.name = name;
-
- int value;
- 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<int> indices(value);
- glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
- for(int j: indices)
- {
- 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> query_indices(indices.begin(), indices.end());
- vector<int> values(indices.size());
- glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_OFFSET, &values[0]);
- for(unsigned j=0; j<indices.size(); ++j)
- uniforms_by_index[indices[j]]->offset = values[j];
-
- query_indices.clear();
- for(int j: indices)
- if(uniforms_by_index[j]->array_size>1)
- query_indices.push_back(j);
- if(!query_indices.empty())
- {
- glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
- for(unsigned j=0; j<query_indices.size(); ++j)
- uniforms_by_index[query_indices[j]]->array_stride = values[j];
- }
-
- query_indices.clear();
- for(int j: indices)
- {
- DataType t = uniforms_by_index[j]->type;
- if(is_matrix(t))
- query_indices.push_back(j);
- }
- if(!query_indices.empty())
- {
- glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
- for(unsigned j=0; j<query_indices.size(); ++j)
- uniforms_by_index[query_indices[j]]->matrix_stride = values[j];
- }
-
- sort(info.uniforms, uniform_location_compare);
- info.layout_hash = compute_layout_hash(info.uniforms);
- }
-}
-
-void Program::query_attributes()
-{
- unsigned count = get_program_i(id, GL_ACTIVE_ATTRIBUTES);
- attributes.reserve(count);
- 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))
- {
- if(len>3 && !strcmp(name+len-3, "[0]"))
- name[len-3] = 0;
-
- attributes.push_back(AttributeInfo());
- AttributeInfo &info = attributes.back();
- info.name = name;
- info.location = glGetAttribLocation(id, name);
- info.array_size = size;
- info.type = from_gl_type(type);
- }
- }
-}
-
-void Program::collect_uniforms()