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);
63 reflect_data.update_used_bindings();
65 for(const ReflectData::UniformInfo &u: reflect_data.uniforms)
67 for(const ReflectData::AttributeInfo &a: reflect_data.attributes)
71 void Program::collect_uniforms(const SpirVModule &mod)
73 // Prepare the default block
74 reflect_data.uniform_blocks.emplace_back();
75 vector<vector<string> > block_uniform_names(1);
77 unsigned n_descriptor_sets = 0;
78 for(const SpirVModule::Variable &v: mod.get_variables())
80 if((v.storage==SpirVModule::UNIFORM || v.storage==SpirVModule::PUSH_CONSTANT) && v.struct_type)
82 reflect_data.uniform_blocks.emplace_back();
83 ReflectData::UniformBlockInfo &info = reflect_data.uniform_blocks.back();
84 info.name = v.struct_type->name;
85 info.data_size = v.struct_type->size;
86 if(v.storage==SpirVModule::PUSH_CONSTANT)
88 info.bind_point = ReflectData::PUSH_CONSTANT;
89 reflect_data.push_constants_size = info.data_size;
94 info.bind_point = v.binding | (v.descriptor_set<<20);
96 info.bind_point = ReflectData::DEFAULT_BLOCK;
97 n_descriptor_sets = max(n_descriptor_sets, v.descriptor_set+1);
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;
197 sort_member(reflect_data.attributes, &ReflectData::AttributeInfo::name);
200 void Program::collect_builtins(const SpirVModule &mod)
202 for(const SpirVModule::Variable &v: mod.get_variables())
203 if(v.storage==SpirVModule::OUTPUT && v.struct_type)
204 collect_builtins(*v.struct_type);
207 void Program::collect_builtins(const SpirVModule::Structure &strct)
209 for(const SpirVModule::StructMember &m: strct.members)
210 if(m.builtin==SpirVModule::CLIP_DISTANCE)
211 reflect_data.n_clip_distances = m.array_size;
214 const ReflectData::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
216 auto i = find_member(reflect_data.uniform_blocks, name, &ReflectData::UniformBlockInfo::name);
217 if(i==reflect_data.uniform_blocks.end())
218 throw key_error(name);
222 const ReflectData::UniformInfo &Program::get_uniform_info(const string &name) const
224 auto i = lower_bound_member(reflect_data.uniforms, Tag(name), &ReflectData::UniformInfo::tag);
225 if(i==reflect_data.uniforms.end() || i->name!=name)
226 throw key_error(name);
230 const ReflectData::UniformInfo &Program::get_uniform_info(Tag tag) const
232 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
233 if(i==reflect_data.uniforms.end() || i->tag!=tag)
234 throw key_error(tag);
238 int Program::get_uniform_location(const string &name) const
240 if(name[name.size()-1]==']')
241 throw invalid_argument("Program::get_uniform_location");
243 auto i = lower_bound_member(reflect_data.uniforms, Tag(name), &ReflectData::UniformInfo::tag);
244 return i!=reflect_data.uniforms.end() && i->name==name && i->block->bind_point<0 ? i->location : -1;
247 int Program::get_uniform_location(Tag tag) const
249 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
250 return i!=reflect_data.uniforms.end() && i->tag==tag && i->block->bind_point<0 ? i->location : -1;
253 int Program::get_uniform_binding(Tag tag) const
255 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
256 return i!=reflect_data.uniforms.end() && i->tag==tag ? i->binding : -1;
259 bool Program::uses_binding(int binding) const
261 auto i = lower_bound(reflect_data.used_bindings, binding);
262 return i!=reflect_data.used_bindings.end() && *i==binding;
265 const ReflectData::AttributeInfo &Program::get_attribute_info(const string &name) const
267 auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);
268 if(i==reflect_data.attributes.end() || i->name!=name)
269 throw key_error(name);
273 int Program::get_attribute_location(const string &name) const
275 if(name[name.size()-1]==']')
276 throw invalid_argument("Program::get_attribute_location");
278 auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);
279 return i!=reflect_data.attributes.end() && i->name==name ? i->location : -1;
283 Program::Loader::Loader(Program &p, Collection &c):
284 DataFile::CollectionObjectLoader<Program>(p, &c)
286 add("module", &Loader::module);
289 void Program::Loader::module(const string &n)
291 map<string, int> spec_values;
292 SpecializationLoader ldr(spec_values);
294 obj.add_stages(get_collection().get<Module>(n), spec_values);
298 DataFile::Loader::ActionMap Program::SpecializationLoader::shared_actions;
300 Program::SpecializationLoader::SpecializationLoader(map<string, int> &sv):
303 set_actions(shared_actions);
306 void Program::SpecializationLoader::init_actions()
308 add("specialize", &SpecializationLoader::specialize_bool);
309 add("specialize", &SpecializationLoader::specialize_int);
312 void Program::SpecializationLoader::specialize_bool(const string &name, bool value)
314 spec_values[name] = value;
317 void Program::SpecializationLoader::specialize_int(const string &name, int value)
319 spec_values[name] = value;