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 reflect_data.update_used_bindings();
68 for(const ReflectData::UniformInfo &u: reflect_data.uniforms)
70 for(const ReflectData::AttributeInfo &a: reflect_data.attributes)
74 void Program::collect_uniforms(const SpirVModule &mod)
76 // Prepare the default block
77 reflect_data.uniform_blocks.emplace_back();
78 vector<vector<string> > block_uniform_names(1);
80 unsigned n_descriptor_sets = 0;
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 if(v.storage==SpirVModule::PUSH_CONSTANT)
89 info.bind_point = ReflectData::PUSH_CONSTANT;
93 info.bind_point = v.binding | (v.descriptor_set<<20);
95 info.bind_point = ReflectData::DEFAULT_BLOCK;
96 n_descriptor_sets = max(n_descriptor_sets, v.descriptor_set+1);
98 info.data_size = v.struct_type->size;
102 prefix = v.struct_type->name+".";
103 block_uniform_names.emplace_back();
104 collect_block_uniforms(*v.struct_type, prefix, 0, block_uniform_names.back());
106 else if(v.storage==SpirVModule::UNIFORM_CONSTANT && (v.location>=0 || v.binding>=0))
108 block_uniform_names[0].push_back(v.name);
109 reflect_data.uniforms.emplace_back();
110 ReflectData::UniformInfo &info = reflect_data.uniforms.back();
113 info.location = v.location;
115 info.binding = v.binding | (v.descriptor_set<<20);
116 n_descriptor_sets = max(n_descriptor_sets, v.descriptor_set+1);
117 info.array_size = max(v.array_size, 1U);
122 sort_member(reflect_data.uniforms, &ReflectData::UniformInfo::tag);
124 if(block_uniform_names.front().empty())
126 reflect_data.uniform_blocks.erase(reflect_data.uniform_blocks.begin());
127 block_uniform_names.erase(block_uniform_names.begin());
130 for(unsigned i=0; i<reflect_data.uniform_blocks.size(); ++i)
132 ReflectData::UniformBlockInfo &block = reflect_data.uniform_blocks[i];
133 for(const string &n: block_uniform_names[i])
135 // The element is already known to be present
136 ReflectData::UniformInfo &uni = *lower_bound_member(reflect_data.uniforms, Tag(n), &ReflectData::UniformInfo::tag);
137 block.uniforms.push_back(&uni);
140 block.sort_uniforms();
141 block.update_layout_hash();
144 reflect_data.n_descriptor_sets = n_descriptor_sets;
145 reflect_data.update_layout_hash();
148 void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset, vector<string> &uniform_names)
150 for(const SpirVModule::StructMember &m: strct.members)
152 unsigned offset = base_offset+m.offset;
157 for(unsigned j=0; j<m.array_size; ++j, offset+=m.array_stride)
158 collect_block_uniforms(*m.struct_type, format("%s%s[%d].", prefix, m.name, j), offset, uniform_names);
161 collect_block_uniforms(*m.struct_type, prefix+m.name+".", offset, uniform_names);
165 string name = prefix+m.name;
166 uniform_names.push_back(name);
167 reflect_data.uniforms.emplace_back();
168 ReflectData::UniformInfo &info = reflect_data.uniforms.back();
171 info.offset = offset;
172 info.array_size = max(m.array_size, 1U);
173 info.array_stride = m.array_stride;
174 info.matrix_stride = m.matrix_stride;
180 void Program::collect_attributes(const SpirVModule &mod)
182 for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
183 if(e.stage==SpirVModule::VERTEX && e.name=="main")
185 for(const SpirVModule::Variable *v: e.globals)
186 if(v->storage==SpirVModule::INPUT)
188 reflect_data.attributes.emplace_back();
189 ReflectData::AttributeInfo &info = reflect_data.attributes.back();
191 info.location = v->location;
192 info.array_size = v->array_size;
198 void Program::collect_builtins(const SpirVModule &mod)
200 for(const SpirVModule::Variable &v: mod.get_variables())
201 if(v.storage==SpirVModule::OUTPUT && v.struct_type)
202 collect_builtins(*v.struct_type);
205 void Program::collect_builtins(const SpirVModule::Structure &strct)
207 for(const SpirVModule::StructMember &m: strct.members)
208 if(m.builtin==SpirVModule::CLIP_DISTANCE)
209 reflect_data.n_clip_distances = m.array_size;
212 const ReflectData::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
214 auto i = find_member(reflect_data.uniform_blocks, name, &ReflectData::UniformBlockInfo::name);
215 if(i==reflect_data.uniform_blocks.end())
216 throw key_error(name);
220 const ReflectData::UniformInfo &Program::get_uniform_info(const string &name) const
222 auto i = lower_bound_member(reflect_data.uniforms, Tag(name), &ReflectData::UniformInfo::tag);
223 if(i==reflect_data.uniforms.end() || i->name!=name)
224 throw key_error(name);
228 const ReflectData::UniformInfo &Program::get_uniform_info(Tag tag) const
230 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
231 if(i==reflect_data.uniforms.end() || i->tag!=tag)
232 throw key_error(tag);
236 int Program::get_uniform_location(const string &name) const
238 if(name[name.size()-1]==']')
239 throw invalid_argument("Program::get_uniform_location");
241 auto i = lower_bound_member(reflect_data.uniforms, Tag(name), &ReflectData::UniformInfo::tag);
242 return i!=reflect_data.uniforms.end() && i->name==name && i->block->bind_point<0 ? i->location : -1;
245 int Program::get_uniform_location(Tag tag) const
247 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
248 return i!=reflect_data.uniforms.end() && i->tag==tag && i->block->bind_point<0 ? i->location : -1;
251 int Program::get_uniform_binding(Tag tag) const
253 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
254 return i!=reflect_data.uniforms.end() && i->tag==tag ? i->binding : -1;
257 bool Program::uses_binding(int binding) const
259 auto i = lower_bound(reflect_data.used_bindings, binding);
260 return i!=reflect_data.used_bindings.end() && *i==binding;
263 const ReflectData::AttributeInfo &Program::get_attribute_info(const string &name) const
265 auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);
266 if(i==reflect_data.attributes.end() || i->name!=name)
267 throw key_error(name);
271 int Program::get_attribute_location(const string &name) const
273 if(name[name.size()-1]==']')
274 throw invalid_argument("Program::get_attribute_location");
276 auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);
277 return i!=reflect_data.attributes.end() && i->name==name ? i->location : -1;
281 Program::Loader::Loader(Program &p, Collection &c):
282 DataFile::CollectionObjectLoader<Program>(p, &c)
284 add("module", &Loader::module);
287 void Program::Loader::module(const string &n)
289 map<string, int> spec_values;
290 SpecializationLoader ldr(spec_values);
292 obj.add_stages(get_collection().get<Module>(n), spec_values);
296 DataFile::Loader::ActionMap Program::SpecializationLoader::shared_actions;
298 Program::SpecializationLoader::SpecializationLoader(map<string, int> &sv):
301 set_actions(shared_actions);
304 void Program::SpecializationLoader::init_actions()
306 add("specialize", &SpecializationLoader::specialize_bool);
307 add("specialize", &SpecializationLoader::specialize_int);
310 void Program::SpecializationLoader::specialize_bool(const string &name, bool value)
312 spec_values[name] = value;
315 void Program::SpecializationLoader::specialize_int(const string &name, int value)
317 spec_values[name] = value;