- for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- require_type(i->type);
- for(vector<AttributeInfo>::const_iterator i=attributes.begin(); i!=attributes.end(); ++i)
- require_type(i->type);
-}
-
-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(vector<UniformInfo>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- if(!i->block)
- {
- i->location = glGetUniformLocation(id, i->name.c_str());
- i->block = &default_block;
- default_block.uniforms.push_back(&*i);
-
- if(is_image(i->type) && i->location>=0)
- glGetUniformiv(id, i->location, &i->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(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> 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(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
- if(uniforms_by_index[*j]->array_size>1)
- query_indices.push_back(*j);
- if(!query_indices.empty())