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 Program::Program(Program &&other):
16 ProgramBackend(move(other)),
17 reflect_data(move(other.reflect_data)),
18 specialized_spirv(other.specialized_spirv)
20 other.specialized_spirv = 0;
25 delete specialized_spirv;
28 void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
31 throw invalid_operation("Program::add_stages");
33 reflect_data = ReflectData();
34 const Module *final_module = &mod;
36 switch(mod.get_format())
39 add_glsl_stages(static_cast<const GlslModule &>(mod), spec_values);
42 if(static_cast<const SpirVModule &>(mod).is_specializable())
44 specialized_spirv = static_cast<const SpirVModule &>(mod).specialize(spec_values);
45 final_module = specialized_spirv;
47 add_spirv_stages(*static_cast<const SpirVModule *>(final_module), spec_values);
50 throw invalid_argument("Program::add_stages");
53 if(final_module->get_format()==Module::SPIR_V)
55 const SpirVModule &spirv_mod = *static_cast<const SpirVModule *>(final_module);
56 collect_uniforms(spirv_mod);
57 collect_attributes(spirv_mod);
58 collect_builtins(spirv_mod);
60 for(const SpirVModule::EntryPoint &e: spirv_mod.get_entry_points())
61 if(e.stage==SpirVModule::COMPUTE)
62 reflect_data.compute_wg_size = e.compute_local_size;
67 reflect_data.update_used_bindings();
69 for(const ReflectData::UniformInfo &u: reflect_data.uniforms)
71 for(const ReflectData::AttributeInfo &a: reflect_data.attributes)
75 void Program::collect_uniforms(const SpirVModule &mod)
77 // Prepare the default block
78 reflect_data.uniform_blocks.emplace_back();
79 vector<vector<string> > block_uniform_names(1);
81 for(const SpirVModule::Variable &v: mod.get_variables())
83 if((v.storage==SpirVModule::UNIFORM || v.storage==SpirVModule::PUSH_CONSTANT) && v.struct_type)
85 reflect_data.uniform_blocks.emplace_back();
86 ReflectData::UniformBlockInfo &info = reflect_data.uniform_blocks.back();
87 info.name = v.struct_type->name;
88 info.data_size = v.struct_type->size;
89 if(v.storage==SpirVModule::PUSH_CONSTANT)
91 info.bind_point = ReflectData::PUSH_CONSTANT;
92 reflect_data.push_constants_size = info.data_size;
97 info.bind_point = v.binding | (v.descriptor_set<<20);
99 info.bind_point = ReflectData::DEFAULT_BLOCK;
100 reflect_data.n_descriptor_sets = max(reflect_data.n_descriptor_sets, v.descriptor_set+1);
105 prefix = v.struct_type->name+".";
106 block_uniform_names.emplace_back();
107 collect_block_uniforms(*v.struct_type, prefix, 0, block_uniform_names.back());
109 else if(v.storage==SpirVModule::UNIFORM_CONSTANT && (v.location>=0 || v.binding>=0))
111 block_uniform_names[0].push_back(v.name);
112 reflect_data.uniforms.emplace_back();
113 ReflectData::UniformInfo &info = reflect_data.uniforms.back();
116 info.location = v.location;
118 info.binding = v.binding | (v.descriptor_set<<20);
119 reflect_data.n_descriptor_sets = max(reflect_data.n_descriptor_sets, v.descriptor_set+1);
120 info.array_size = max(v.array_size, 1U);
125 sort_member(reflect_data.uniforms, &ReflectData::UniformInfo::tag);
127 if(block_uniform_names.front().empty())
129 reflect_data.uniform_blocks.erase(reflect_data.uniform_blocks.begin());
130 block_uniform_names.erase(block_uniform_names.begin());
133 for(unsigned i=0; i<reflect_data.uniform_blocks.size(); ++i)
135 ReflectData::UniformBlockInfo &block = reflect_data.uniform_blocks[i];
136 for(const string &n: block_uniform_names[i])
138 // The element is already known to be present
139 ReflectData::UniformInfo &uni = *lower_bound_member(reflect_data.uniforms, Tag(n), &ReflectData::UniformInfo::tag);
140 block.uniforms.push_back(&uni);
143 block.sort_uniforms();
144 block.update_layout_hash();
147 reflect_data.update_layout_hash();
150 void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset, vector<string> &uniform_names)
152 for(const SpirVModule::StructMember &m: strct.members)
154 unsigned offset = base_offset+m.offset;
159 for(unsigned j=0; j<m.array_size; ++j, offset+=m.array_stride)
160 collect_block_uniforms(*m.struct_type, format("%s%s[%d].", prefix, m.name, j), offset, uniform_names);
163 collect_block_uniforms(*m.struct_type, prefix+m.name+".", offset, uniform_names);
167 string name = prefix+m.name;
168 uniform_names.push_back(name);
169 reflect_data.uniforms.emplace_back();
170 ReflectData::UniformInfo &info = reflect_data.uniforms.back();
173 info.offset = offset;
174 info.array_size = max(m.array_size, 1U);
175 info.array_stride = m.array_stride;
176 info.matrix_stride = m.matrix_stride;
182 void Program::collect_attributes(const SpirVModule &mod)
184 for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
185 if(e.stage==SpirVModule::VERTEX && e.name=="main")
187 for(const SpirVModule::Variable *v: e.globals)
188 if(v->storage==SpirVModule::INPUT)
190 reflect_data.attributes.emplace_back();
191 ReflectData::AttributeInfo &info = reflect_data.attributes.back();
193 info.location = v->location;
194 info.array_size = v->array_size;
199 sort_member(reflect_data.attributes, &ReflectData::AttributeInfo::name);
202 void Program::collect_builtins(const SpirVModule &mod)
204 for(const SpirVModule::Variable &v: mod.get_variables())
205 if(v.storage==SpirVModule::OUTPUT && v.struct_type)
206 collect_builtins(*v.struct_type);
209 void Program::collect_builtins(const SpirVModule::Structure &strct)
211 for(const SpirVModule::StructMember &m: strct.members)
212 if(m.builtin==SpirVModule::CLIP_DISTANCE)
213 reflect_data.n_clip_distances = m.array_size;
216 const ReflectData::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
218 auto i = find_member(reflect_data.uniform_blocks, name, &ReflectData::UniformBlockInfo::name);
219 if(i==reflect_data.uniform_blocks.end())
220 throw key_error(name);
224 const ReflectData::UniformInfo &Program::get_uniform_info(const string &name) const
226 auto i = lower_bound_member(reflect_data.uniforms, Tag(name), &ReflectData::UniformInfo::tag);
227 if(i==reflect_data.uniforms.end() || i->name!=name)
228 throw key_error(name);
232 const ReflectData::UniformInfo &Program::get_uniform_info(Tag tag) const
234 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
235 if(i==reflect_data.uniforms.end() || i->tag!=tag)
236 throw key_error(tag);
240 int Program::get_uniform_location(const string &name) const
242 if(name[name.size()-1]==']')
243 throw invalid_argument("Program::get_uniform_location");
245 auto i = lower_bound_member(reflect_data.uniforms, Tag(name), &ReflectData::UniformInfo::tag);
246 return i!=reflect_data.uniforms.end() && i->name==name && i->block->bind_point<0 ? i->location : -1;
249 int Program::get_uniform_location(Tag tag) const
251 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
252 return i!=reflect_data.uniforms.end() && i->tag==tag && i->block->bind_point<0 ? i->location : -1;
255 int Program::get_uniform_binding(Tag tag) const
257 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
258 return i!=reflect_data.uniforms.end() && i->tag==tag ? i->binding : -1;
261 bool Program::uses_binding(int binding) const
263 auto i = lower_bound(reflect_data.used_bindings, binding);
264 return i!=reflect_data.used_bindings.end() && *i==binding;
267 const ReflectData::AttributeInfo &Program::get_attribute_info(const string &name) const
269 auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);
270 if(i==reflect_data.attributes.end() || i->name!=name)
271 throw key_error(name);
275 int Program::get_attribute_location(const string &name) const
277 if(name[name.size()-1]==']')
278 throw invalid_argument("Program::get_attribute_location");
280 auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);
281 return i!=reflect_data.attributes.end() && i->name==name ? i->location : -1;
285 Program::Loader::Loader(Program &p, Collection &c):
286 DataFile::CollectionObjectLoader<Program>(p, &c)
288 add("module", &Loader::module);
291 void Program::Loader::module(const string &n)
293 map<string, int> spec_values;
294 SpecializationLoader ldr(spec_values);
296 obj.add_stages(get_collection().get<Module>(n), spec_values);
300 DataFile::Loader::ActionMap Program::SpecializationLoader::shared_actions;
302 Program::SpecializationLoader::SpecializationLoader(map<string, int> &sv):
305 set_actions(shared_actions);
308 void Program::SpecializationLoader::init_actions()
310 add("specialize", &SpecializationLoader::specialize_bool);
311 add("specialize", &SpecializationLoader::specialize_int);
314 void Program::SpecializationLoader::specialize_bool(const string &name, bool value)
316 spec_values[name] = value;
319 void Program::SpecializationLoader::specialize_int(const string &name, int value)
321 spec_values[name] = value;