-void Program::query_uniforms()
-{
- unsigned count = 0;
- glGetProgramiv(id, GL_ACTIVE_UNIFORMS, reinterpret_cast<int *>(&count));
- reflect_data.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;
-
- reflect_data.uniforms.push_back(ReflectData::UniformInfo());
- ReflectData::UniformInfo &info = reflect_data.uniforms.back();
- info.name = name;
- info.tag = name;
- info.array_size = size;
- info.type = from_gl_type(type);
- uniform_names[i] = name;
- }
- }
-
- sort_member(reflect_data.uniforms, &ReflectData::UniformInfo::tag);
-
- if(ARB_uniform_buffer_object)
- {
- vector<ReflectData::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(reflect_data.uniforms, Tag(uniform_names[i]), &ReflectData::UniformInfo::tag);
- query_uniform_blocks(uniforms_by_index);
- }
-
- reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
- ReflectData::UniformBlockInfo &default_block = reflect_data.uniform_blocks.back();
-
- for(ReflectData::UniformInfo &u: reflect_data.uniforms)
- if(!u.block)
- {
- u.location = glGetUniformLocation(id, u.name.c_str());
- u.block = &default_block;
- u.array_stride = get_type_size(u.type);
- if(is_matrix(u.type))
- u.matrix_stride = get_type_size(get_matrix_column_type(u.type));
- default_block.uniforms.push_back(&u);
-
- if(u.location>=0)
- {
- UniformCall::FuncPtr func = 0;
- if(u.type==FLOAT)
- func = &uniform_wrapper<float, glUniform1fv>;
- else if(u.type==FLOAT_VEC2)
- func = &uniform_wrapper<float, glUniform2fv>;
- else if(u.type==FLOAT_VEC3)
- func = &uniform_wrapper<float, glUniform3fv>;
- else if(u.type==FLOAT_VEC4)
- func = &uniform_wrapper<float, glUniform4fv>;
- else if(u.type==INT)
- func = &uniform_wrapper<int, glUniform1iv>;
- else if(u.type==INT_VEC2)
- func = &uniform_wrapper<int, glUniform2iv>;
- else if(u.type==INT_VEC3)
- func = &uniform_wrapper<int, glUniform3iv>;
- else if(u.type==INT_VEC4)
- func = &uniform_wrapper<int, glUniform4iv>;
- else if(u.type==FLOAT_MAT2)
- func = &uniform_matrix_wrapper<float, glUniformMatrix2fv>;
- else if(u.type==FLOAT_MAT3)
- func = &uniform_matrix_wrapper<float, glUniformMatrix3fv>;
- else if(u.type==FLOAT_MAT4)
- func = &uniform_matrix_wrapper<float, glUniformMatrix4fv>;
- else if(u.type==FLOAT_MAT2x3)
- func = &uniform_matrix_wrapper<float, glUniformMatrix2x3fv>;
- else if(u.type==FLOAT_MAT3x2)
- func = &uniform_matrix_wrapper<float, glUniformMatrix3x2fv>;
- else if(u.type==FLOAT_MAT2x4)
- func = &uniform_matrix_wrapper<float, glUniformMatrix2x4fv>;
- else if(u.type==FLOAT_MAT4x2)
- func = &uniform_matrix_wrapper<float, glUniformMatrix4x2fv>;
- else if(u.type==FLOAT_MAT3x4)
- func = &uniform_matrix_wrapper<float, glUniformMatrix3x4fv>;
- else if(u.type==FLOAT_MAT4x3)
- func = &uniform_matrix_wrapper<float, glUniformMatrix4x3fv>;
- else if(is_image(u.type))
- glGetUniformiv(id, u.location, &u.binding);
-
- if(func)
- uniform_calls.push_back(UniformCall(u.location, u.array_size, func));
- }
- }
-
- default_block.sort_uniforms();
- if(!default_block.uniforms.empty())
- {
- const ReflectData::UniformInfo &uni = *default_block.uniforms.back();
- default_block.data_size = uni.location*16+uni.array_size*get_type_size(uni.type);
- }
- default_block.update_layout_hash();
- reflect_data.update_layout_hash();
-}
-
-void Program::query_uniform_blocks(const vector<ReflectData::UniformInfo *> &uniforms_by_index)
-{
- unsigned count = 0;
- glGetProgramiv(id, GL_ACTIVE_UNIFORM_BLOCKS, reinterpret_cast<int *>(&count));
- // Reserve an extra index for the default block
- reflect_data.uniform_blocks.reserve(count+1);
- for(unsigned i=0; i<count; ++i)
- {
- char name[128];
- int len;
- glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
- reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
- ReflectData::UniformBlockInfo &info = reflect_data.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];
- }
-
- info.sort_uniforms();
- info.update_layout_hash();
- }
-}
-
-void Program::query_attributes()
-{
- unsigned count = 0;
- glGetProgramiv(id, GL_ACTIVE_ATTRIBUTES, reinterpret_cast<int *>(&count));
- reflect_data.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;
-
- reflect_data.attributes.push_back(ReflectData::AttributeInfo());
- ReflectData::AttributeInfo &info = reflect_data.attributes.back();
- info.name = name;
- info.location = glGetAttribLocation(id, name);
- info.array_size = size;
- info.type = from_gl_type(type);
- }
- }
-}
-
-void Program::collect_uniforms(const SpirVModule &mod, const map<unsigned, int> &spec_values)