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 TransientData transient;
37 switch(mod.get_format())
40 add_glsl_stages(static_cast<const GlslModule &>(mod), spec_values, transient);
43 if(!spec_values.empty())
45 specialized_spirv = static_cast<const SpirVModule &>(mod).specialize(spec_values);
46 final_module = specialized_spirv;
48 add_spirv_stages(*static_cast<const SpirVModule *>(final_module), spec_values);
51 throw invalid_argument("Program::add_stages");
54 finalize(*final_module, transient);
56 if(final_module->get_format()==Module::SPIR_V)
58 const SpirVModule &spirv_mod = *static_cast<const SpirVModule *>(final_module);
59 collect_uniforms(spirv_mod);
60 collect_attributes(spirv_mod);
61 collect_builtins(spirv_mod);
66 for(const ReflectData::UniformInfo &u: reflect_data.uniforms)
68 reflect_data.used_bindings.push_back(u.binding);
69 for(const ReflectData::UniformBlockInfo &b: reflect_data.uniform_blocks)
70 reflect_data.used_bindings.push_back(b.bind_point);
71 sort(reflect_data.used_bindings);
73 for(const ReflectData::UniformInfo &u: reflect_data.uniforms)
75 for(const ReflectData::AttributeInfo &a: reflect_data.attributes)
79 void Program::collect_uniforms(const SpirVModule &mod)
81 // Prepare the default block
82 reflect_data.uniform_blocks.emplace_back();
83 vector<vector<string> > block_uniform_names(1);
85 unsigned n_descriptor_sets = 0;
86 for(const SpirVModule::Variable &v: mod.get_variables())
88 if((v.storage==SpirVModule::UNIFORM || v.storage==SpirVModule::PUSH_CONSTANT) && v.struct_type)
90 reflect_data.uniform_blocks.emplace_back();
91 ReflectData::UniformBlockInfo &info = reflect_data.uniform_blocks.back();
92 info.name = v.struct_type->name;
93 if(v.storage==SpirVModule::PUSH_CONSTANT)
94 info.bind_point = ReflectData::PUSH_CONSTANT;
98 info.bind_point = v.binding | (v.descriptor_set<<20);
100 info.bind_point = ReflectData::DEFAULT_BLOCK;
101 n_descriptor_sets = max(n_descriptor_sets, v.descriptor_set+1);
103 info.data_size = v.struct_type->size;
107 prefix = v.struct_type->name+".";
108 block_uniform_names.emplace_back();
109 collect_block_uniforms(*v.struct_type, prefix, 0, block_uniform_names.back());
111 else if(v.storage==SpirVModule::UNIFORM_CONSTANT && (v.location>=0 || v.binding>=0))
113 block_uniform_names[0].push_back(v.name);
114 reflect_data.uniforms.emplace_back();
115 ReflectData::UniformInfo &info = reflect_data.uniforms.back();
118 info.location = v.location;
120 info.binding = v.binding | (v.descriptor_set<<20);
121 n_descriptor_sets = max(n_descriptor_sets, v.descriptor_set+1);
122 info.array_size = max(v.array_size, 1U);
127 sort_member(reflect_data.uniforms, &ReflectData::UniformInfo::tag);
129 if(block_uniform_names.front().empty())
131 reflect_data.uniform_blocks.erase(reflect_data.uniform_blocks.begin());
132 block_uniform_names.erase(block_uniform_names.begin());
135 for(unsigned i=0; i<reflect_data.uniform_blocks.size(); ++i)
137 ReflectData::UniformBlockInfo &block = reflect_data.uniform_blocks[i];
138 for(const string &n: block_uniform_names[i])
140 // The element is already known to be present
141 ReflectData::UniformInfo &uni = *lower_bound_member(reflect_data.uniforms, Tag(n), &ReflectData::UniformInfo::tag);
142 block.uniforms.push_back(&uni);
145 block.sort_uniforms();
146 block.update_layout_hash();
149 reflect_data.n_descriptor_sets = n_descriptor_sets;
150 reflect_data.update_layout_hash();
153 void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset, vector<string> &uniform_names)
155 for(const SpirVModule::StructMember &m: strct.members)
157 unsigned offset = base_offset+m.offset;
162 for(unsigned j=0; j<m.array_size; ++j, offset+=m.array_stride)
163 collect_block_uniforms(*m.struct_type, format("%s%s[%d].", prefix, m.name, j), offset, uniform_names);
166 collect_block_uniforms(*m.struct_type, prefix+m.name+".", offset, uniform_names);
170 string name = prefix+m.name;
171 uniform_names.push_back(name);
172 reflect_data.uniforms.emplace_back();
173 ReflectData::UniformInfo &info = reflect_data.uniforms.back();
176 info.offset = offset;
177 info.array_size = max(m.array_size, 1U);
178 info.array_stride = m.array_stride;
179 info.matrix_stride = m.matrix_stride;
185 void Program::collect_attributes(const SpirVModule &mod)
187 for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
188 if(e.stage==SpirVModule::VERTEX && e.name=="main")
190 for(const SpirVModule::Variable *v: e.globals)
191 if(v->storage==SpirVModule::INPUT)
193 reflect_data.attributes.emplace_back();
194 ReflectData::AttributeInfo &info = reflect_data.attributes.back();
196 info.location = v->location;
197 info.array_size = v->array_size;
203 void Program::collect_builtins(const SpirVModule &mod)
205 for(const SpirVModule::Variable &v: mod.get_variables())
206 if(v.storage==SpirVModule::OUTPUT && v.struct_type)
207 collect_builtins(*v.struct_type);
210 void Program::collect_builtins(const SpirVModule::Structure &strct)
212 for(const SpirVModule::StructMember &m: strct.members)
213 if(m.builtin==SpirVModule::CLIP_DISTANCE)
214 reflect_data.n_clip_distances = m.array_size;
217 const ReflectData::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
219 auto i = find_member(reflect_data.uniform_blocks, name, &ReflectData::UniformBlockInfo::name);
220 if(i==reflect_data.uniform_blocks.end())
221 throw key_error(name);
225 const ReflectData::UniformInfo &Program::get_uniform_info(const string &name) const
227 auto i = lower_bound_member(reflect_data.uniforms, Tag(name), &ReflectData::UniformInfo::tag);
228 if(i==reflect_data.uniforms.end() || i->name!=name)
229 throw key_error(name);
233 const ReflectData::UniformInfo &Program::get_uniform_info(Tag tag) const
235 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
236 if(i==reflect_data.uniforms.end() || i->tag!=tag)
237 throw key_error(tag);
241 int Program::get_uniform_location(const string &name) const
243 if(name[name.size()-1]==']')
244 throw invalid_argument("Program::get_uniform_location");
246 auto i = lower_bound_member(reflect_data.uniforms, Tag(name), &ReflectData::UniformInfo::tag);
247 return i!=reflect_data.uniforms.end() && i->name==name && i->block->bind_point<0 ? i->location : -1;
250 int Program::get_uniform_location(Tag tag) const
252 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
253 return i!=reflect_data.uniforms.end() && i->tag==tag && i->block->bind_point<0 ? i->location : -1;
256 int Program::get_uniform_binding(Tag tag) const
258 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
259 return i!=reflect_data.uniforms.end() && i->tag==tag ? i->binding : -1;
262 bool Program::uses_binding(int binding) const
264 auto i = lower_bound(reflect_data.used_bindings, binding);
265 return i!=reflect_data.used_bindings.end() && *i==binding;
268 const ReflectData::AttributeInfo &Program::get_attribute_info(const string &name) const
270 auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);
271 if(i==reflect_data.attributes.end() || i->name!=name)
272 throw key_error(name);
276 int Program::get_attribute_location(const string &name) const
278 if(name[name.size()-1]==']')
279 throw invalid_argument("Program::get_attribute_location");
281 auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);
282 return i!=reflect_data.attributes.end() && i->name==name ? i->location : -1;
286 Program::Loader::Loader(Program &p, Collection &c):
287 DataFile::CollectionObjectLoader<Program>(p, &c)
289 add("module", &Loader::module);
292 void Program::Loader::module(const string &n)
294 map<string, int> spec_values;
295 SpecializationLoader ldr(spec_values);
297 obj.add_stages(get_collection().get<Module>(n), spec_values);
301 DataFile::Loader::ActionMap Program::SpecializationLoader::shared_actions;
303 Program::SpecializationLoader::SpecializationLoader(map<string, int> &sv):
306 set_actions(shared_actions);
309 void Program::SpecializationLoader::init_actions()
311 add("specialize", &SpecializationLoader::specialize_bool);
312 add("specialize", &SpecializationLoader::specialize_int);
315 void Program::SpecializationLoader::specialize_bool(const string &name, bool value)
317 spec_values[name] = value;
320 void Program::SpecializationLoader::specialize_int(const string &name, int value)
322 spec_values[name] = value;