+void Program::collect_uniforms()
+{
+ const SpirVModule &mod = static_cast<const SpirVModule &>(*module);
+
+ // Prepare the default block
+ uniform_blocks.push_back(UniformBlockInfo());
+ vector<vector<string> > block_uniform_names(1);
+
+ const vector<SpirVModule::Variable> &variables = mod.get_variables();
+ for(vector<SpirVModule::Variable>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
+ {
+ if(i->storage==SpirVModule::UNIFORM && i->struct_type)
+ {
+ uniform_blocks.push_back(UniformBlockInfo());
+ UniformBlockInfo &info = uniform_blocks.back();
+ info.name = i->struct_type->name;
+ info.bind_point = i->binding;
+ info.data_size = i->struct_type->size;
+
+ string prefix;
+ if(!i->name.empty())
+ prefix = i->struct_type->name+".";
+ block_uniform_names.push_back(vector<string>());
+ collect_block_uniforms(*i->struct_type, prefix, 0, block_uniform_names.back());
+ }
+ else if(i->storage==SpirVModule::UNIFORM_CONSTANT && i->location>=0)
+ {
+ block_uniform_names[0].push_back(i->name);
+ uniforms.push_back(UniformInfo());
+ UniformInfo &info = uniforms.back();
+ info.name = i->name;
+ info.location = i->location;
+ info.array_size = i->array_size;
+ info.type = i->type;
+ }
+ }
+
+ sort(uniforms, &uniform_name_compare);
+
+ for(unsigned i=0; i<uniform_blocks.size(); ++i)
+ {
+ UniformBlockInfo &block = uniform_blocks[i];
+ const vector<string> &names = block_uniform_names[i];
+ for(vector<string>::const_iterator j=names.begin(); j!=names.end(); ++j)
+ {
+ // The element is already known to be present
+ UniformInfo &uni = *lower_bound(uniforms, *j, &name_search<UniformInfo>);
+ block.uniforms.push_back(&uni);
+ uni.block = █
+ }
+ block.layout_hash = compute_layout_hash(block.uniforms);
+ }
+
+ update_layout_hash();
+}
+
+void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset, vector<string> &uniform_names)
+{
+ for(vector<SpirVModule::StructMember>::const_iterator i=strct.members.begin(); i!=strct.members.end(); ++i)
+ {
+ unsigned offset = base_offset+i->offset;
+ if(i->struct_type)
+ {
+ if(i->array_size)
+ {
+ for(unsigned j=0; j<i->array_size; ++j, offset+=i->array_stride)
+ collect_block_uniforms(*i->struct_type, format("%s%s[%d].", prefix, i->name, j), offset, uniform_names);
+ }
+ else
+ collect_block_uniforms(*i->struct_type, prefix+i->name+".", offset, uniform_names);
+ }
+ else
+ {
+ string name = prefix+i->name;
+ uniform_names.push_back(name);
+ uniforms.push_back(UniformInfo());
+ UniformInfo &info = uniforms.back();
+ info.name = name;
+ info.offset = offset;
+ info.array_size = i->array_size;
+ info.array_stride = i->array_stride;
+ info.matrix_stride = i->matrix_stride;
+ info.type = i->type;
+ }
+ }
+}
+
+void Program::collect_attributes()
+{
+ const SpirVModule &mod = static_cast<const SpirVModule &>(*module);
+
+ const vector<SpirVModule::EntryPoint> &entry_points = mod.get_entry_points();
+ for(vector<SpirVModule::EntryPoint>::const_iterator i=entry_points.begin(); i!=entry_points.end(); ++i)
+ if(i->stage==SpirVModule::VERTEX && i->name=="main")
+ {
+ for(vector<const SpirVModule::Variable *>::const_iterator j=i->globals.begin(); j!=i->globals.end(); ++j)
+ if((*j)->storage==SpirVModule::INPUT)
+ {
+ attributes.push_back(AttributeInfo());
+ AttributeInfo &info = attributes.back();
+ info.name = (*j)->name;
+ info.location = (*j)->location;
+ info.array_size = (*j)->array_size;
+ info.type = (*j)->type;
+ }
+ }
+}
+
+void Program::update_layout_hash()
+{
+ string layout_descriptor;
+ for(vector<UniformBlockInfo>::const_iterator i=uniform_blocks.begin(); i!=uniform_blocks.end(); ++i)
+ layout_descriptor += format("%d:%x\n", i->bind_point, i->layout_hash);
+ uniform_layout_hash = hash32(layout_descriptor);
+}
+