1 #include <msp/core/algorithm.h>
10 Program::Program(const Module &mod, const map<string, int> &spec_values)
12 add_stages(mod, spec_values);
15 void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
18 throw invalid_operation("Program::add_stages");
20 reflect_data = ReflectData();
22 TransientData transient;
23 switch(mod.get_format())
26 add_glsl_stages(static_cast<const GlslModule &>(mod), spec_values, transient);
29 add_spirv_stages(static_cast<const SpirVModule &>(mod), spec_values);
32 throw invalid_argument("Program::add_stages");
35 finalize(mod, transient);
37 if(mod.get_format()==Module::SPIR_V)
39 collect_uniforms(static_cast<const SpirVModule &>(mod));
40 collect_attributes(static_cast<const SpirVModule &>(mod));
45 for(const ReflectData::UniformInfo &u: reflect_data.uniforms)
47 for(const ReflectData::AttributeInfo &a: reflect_data.attributes)
51 void Program::collect_uniforms(const SpirVModule &mod)
53 // Prepare the default block
54 reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
55 vector<vector<string> > block_uniform_names(1);
57 for(const SpirVModule::Variable &v: mod.get_variables())
59 if(v.storage==SpirVModule::UNIFORM && v.struct_type)
61 reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
62 ReflectData::UniformBlockInfo &info = reflect_data.uniform_blocks.back();
63 info.name = v.struct_type->name;
64 info.bind_point = v.binding;
65 info.data_size = v.struct_type->size;
69 prefix = v.struct_type->name+".";
70 block_uniform_names.push_back(vector<string>());
71 collect_block_uniforms(*v.struct_type, prefix, 0, block_uniform_names.back());
73 else if(v.storage==SpirVModule::UNIFORM_CONSTANT && v.location>=0)
75 block_uniform_names[0].push_back(v.name);
76 reflect_data.uniforms.push_back(ReflectData::UniformInfo());
77 ReflectData::UniformInfo &info = reflect_data.uniforms.back();
80 info.location = v.location;
81 info.binding = v.binding;
82 info.array_size = max(v.array_size, 1U);
87 sort_member(reflect_data.uniforms, &ReflectData::UniformInfo::tag);
89 for(unsigned i=0; i<reflect_data.uniform_blocks.size(); ++i)
91 ReflectData::UniformBlockInfo &block = reflect_data.uniform_blocks[i];
92 for(const string &n: block_uniform_names[i])
94 // The element is already known to be present
95 ReflectData::UniformInfo &uni = *lower_bound_member(reflect_data.uniforms, Tag(n), &ReflectData::UniformInfo::tag);
96 block.uniforms.push_back(&uni);
99 block.sort_uniforms();
100 block.update_layout_hash();
103 reflect_data.update_layout_hash();
106 void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset, vector<string> &uniform_names)
108 for(const SpirVModule::StructMember &m: strct.members)
110 unsigned offset = base_offset+m.offset;
115 for(unsigned j=0; j<m.array_size; ++j, offset+=m.array_stride)
116 collect_block_uniforms(*m.struct_type, format("%s%s[%d].", prefix, m.name, j), offset, uniform_names);
119 collect_block_uniforms(*m.struct_type, prefix+m.name+".", offset, uniform_names);
123 string name = prefix+m.name;
124 uniform_names.push_back(name);
125 reflect_data.uniforms.push_back(ReflectData::UniformInfo());
126 ReflectData::UniformInfo &info = reflect_data.uniforms.back();
129 info.offset = offset;
130 info.array_size = max(m.array_size, 1U);
131 info.array_stride = m.array_stride;
132 info.matrix_stride = m.matrix_stride;
138 void Program::collect_attributes(const SpirVModule &mod)
140 for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
141 if(e.stage==SpirVModule::VERTEX && e.name=="main")
143 for(const SpirVModule::Variable *v: e.globals)
144 if(v->storage==SpirVModule::INPUT)
146 reflect_data.attributes.push_back(ReflectData::AttributeInfo());
147 ReflectData::AttributeInfo &info = reflect_data.attributes.back();
149 info.location = v->location;
150 info.array_size = v->array_size;
156 void Program::collect_builtins(const SpirVModule &mod)
158 for(const SpirVModule::Variable &v: mod.get_variables())
159 if(v.storage==SpirVModule::OUTPUT && v.struct_type)
160 collect_builtins(*v.struct_type);
163 void Program::collect_builtins(const SpirVModule::Structure &strct)
165 for(const SpirVModule::StructMember &m: strct.members)
166 if(m.builtin==SpirVModule::CLIP_DISTANCE)
167 reflect_data.n_clip_distances = m.array_size;
170 const ReflectData::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
172 auto i = find_member(reflect_data.uniform_blocks, name, &ReflectData::UniformBlockInfo::name);
173 if(i==reflect_data.uniform_blocks.end())
174 throw key_error(name);
178 const ReflectData::UniformInfo &Program::get_uniform_info(const string &name) const
180 auto i = lower_bound_member(reflect_data.uniforms, Tag(name), &ReflectData::UniformInfo::tag);
181 if(i==reflect_data.uniforms.end() || i->name!=name)
182 throw key_error(name);
186 const ReflectData::UniformInfo &Program::get_uniform_info(Tag tag) const
188 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
189 if(i==reflect_data.uniforms.end() || i->tag!=tag)
190 throw key_error(tag);
194 int Program::get_uniform_location(const string &name) const
196 if(name[name.size()-1]==']')
197 throw invalid_argument("Program::get_uniform_location");
199 auto i = lower_bound_member(reflect_data.uniforms, Tag(name), &ReflectData::UniformInfo::tag);
200 return i!=reflect_data.uniforms.end() && i->name==name && i->block->bind_point<0 ? i->location : -1;
203 int Program::get_uniform_location(Tag tag) const
205 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
206 return i!=reflect_data.uniforms.end() && i->tag==tag && i->block->bind_point<0 ? i->location : -1;
209 int Program::get_uniform_binding(Tag tag) const
211 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
212 return i!=reflect_data.uniforms.end() && i->tag==tag ? i->binding : -1;
215 const ReflectData::AttributeInfo &Program::get_attribute_info(const string &name) const
217 auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);
218 if(i==reflect_data.attributes.end() || i->name!=name)
219 throw key_error(name);
223 int Program::get_attribute_location(const string &name) const
225 if(name[name.size()-1]==']')
226 throw invalid_argument("Program::get_attribute_location");
228 auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);
229 return i!=reflect_data.attributes.end() && i->name==name ? i->location : -1;
233 Program::Loader::Loader(Program &p, Collection &c):
234 DataFile::CollectionObjectLoader<Program>(p, &c)
236 add("module", &Loader::module);
239 void Program::Loader::module(const string &n)
241 map<string, int> spec_values;
242 SpecializationLoader ldr(spec_values);
244 obj.add_stages(get_collection().get<Module>(n), spec_values);
248 DataFile::Loader::ActionMap Program::SpecializationLoader::shared_actions;
250 Program::SpecializationLoader::SpecializationLoader(map<string, int> &sv):
253 set_actions(shared_actions);
256 void Program::SpecializationLoader::init_actions()
258 add("specialize", &SpecializationLoader::specialize_bool);
259 add("specialize", &SpecializationLoader::specialize_int);
262 void Program::SpecializationLoader::specialize_bool(const string &name, bool value)
264 spec_values[name] = value;
267 void Program::SpecializationLoader::specialize_int(const string &name, int value)
269 spec_values[name] = value;