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));
41 collect_builtins(static_cast<const SpirVModule &>(mod));
46 for(const ReflectData::UniformInfo &u: reflect_data.uniforms)
48 for(const ReflectData::AttributeInfo &a: reflect_data.attributes)
52 void Program::collect_uniforms(const SpirVModule &mod)
54 // Prepare the default block
55 reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
56 vector<vector<string> > block_uniform_names(1);
58 for(const SpirVModule::Variable &v: mod.get_variables())
60 if(v.storage==SpirVModule::UNIFORM && v.struct_type)
62 reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
63 ReflectData::UniformBlockInfo &info = reflect_data.uniform_blocks.back();
64 info.name = v.struct_type->name;
65 info.bind_point = v.binding;
66 info.data_size = v.struct_type->size;
70 prefix = v.struct_type->name+".";
71 block_uniform_names.push_back(vector<string>());
72 collect_block_uniforms(*v.struct_type, prefix, 0, block_uniform_names.back());
74 else if(v.storage==SpirVModule::UNIFORM_CONSTANT && v.location>=0)
76 block_uniform_names[0].push_back(v.name);
77 reflect_data.uniforms.push_back(ReflectData::UniformInfo());
78 ReflectData::UniformInfo &info = reflect_data.uniforms.back();
81 info.location = v.location;
82 info.binding = v.binding;
83 info.array_size = max(v.array_size, 1U);
88 sort_member(reflect_data.uniforms, &ReflectData::UniformInfo::tag);
90 for(unsigned i=0; i<reflect_data.uniform_blocks.size(); ++i)
92 ReflectData::UniformBlockInfo &block = reflect_data.uniform_blocks[i];
93 for(const string &n: block_uniform_names[i])
95 // The element is already known to be present
96 ReflectData::UniformInfo &uni = *lower_bound_member(reflect_data.uniforms, Tag(n), &ReflectData::UniformInfo::tag);
97 block.uniforms.push_back(&uni);
100 block.sort_uniforms();
101 block.update_layout_hash();
104 reflect_data.update_layout_hash();
107 void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset, vector<string> &uniform_names)
109 for(const SpirVModule::StructMember &m: strct.members)
111 unsigned offset = base_offset+m.offset;
116 for(unsigned j=0; j<m.array_size; ++j, offset+=m.array_stride)
117 collect_block_uniforms(*m.struct_type, format("%s%s[%d].", prefix, m.name, j), offset, uniform_names);
120 collect_block_uniforms(*m.struct_type, prefix+m.name+".", offset, uniform_names);
124 string name = prefix+m.name;
125 uniform_names.push_back(name);
126 reflect_data.uniforms.push_back(ReflectData::UniformInfo());
127 ReflectData::UniformInfo &info = reflect_data.uniforms.back();
130 info.offset = offset;
131 info.array_size = max(m.array_size, 1U);
132 info.array_stride = m.array_stride;
133 info.matrix_stride = m.matrix_stride;
139 void Program::collect_attributes(const SpirVModule &mod)
141 for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
142 if(e.stage==SpirVModule::VERTEX && e.name=="main")
144 for(const SpirVModule::Variable *v: e.globals)
145 if(v->storage==SpirVModule::INPUT)
147 reflect_data.attributes.push_back(ReflectData::AttributeInfo());
148 ReflectData::AttributeInfo &info = reflect_data.attributes.back();
150 info.location = v->location;
151 info.array_size = v->array_size;
157 void Program::collect_builtins(const SpirVModule &mod)
159 for(const SpirVModule::Variable &v: mod.get_variables())
160 if(v.storage==SpirVModule::OUTPUT && v.struct_type)
161 collect_builtins(*v.struct_type);
164 void Program::collect_builtins(const SpirVModule::Structure &strct)
166 for(const SpirVModule::StructMember &m: strct.members)
167 if(m.builtin==SpirVModule::CLIP_DISTANCE)
168 reflect_data.n_clip_distances = m.array_size;
171 const ReflectData::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
173 auto i = find_member(reflect_data.uniform_blocks, name, &ReflectData::UniformBlockInfo::name);
174 if(i==reflect_data.uniform_blocks.end())
175 throw key_error(name);
179 const ReflectData::UniformInfo &Program::get_uniform_info(const string &name) const
181 auto i = lower_bound_member(reflect_data.uniforms, Tag(name), &ReflectData::UniformInfo::tag);
182 if(i==reflect_data.uniforms.end() || i->name!=name)
183 throw key_error(name);
187 const ReflectData::UniformInfo &Program::get_uniform_info(Tag tag) const
189 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
190 if(i==reflect_data.uniforms.end() || i->tag!=tag)
191 throw key_error(tag);
195 int Program::get_uniform_location(const string &name) const
197 if(name[name.size()-1]==']')
198 throw invalid_argument("Program::get_uniform_location");
200 auto i = lower_bound_member(reflect_data.uniforms, Tag(name), &ReflectData::UniformInfo::tag);
201 return i!=reflect_data.uniforms.end() && i->name==name && i->block->bind_point<0 ? i->location : -1;
204 int Program::get_uniform_location(Tag tag) const
206 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
207 return i!=reflect_data.uniforms.end() && i->tag==tag && i->block->bind_point<0 ? i->location : -1;
210 int Program::get_uniform_binding(Tag tag) const
212 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
213 return i!=reflect_data.uniforms.end() && i->tag==tag ? i->binding : -1;
216 const ReflectData::AttributeInfo &Program::get_attribute_info(const string &name) const
218 auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);
219 if(i==reflect_data.attributes.end() || i->name!=name)
220 throw key_error(name);
224 int Program::get_attribute_location(const string &name) const
226 if(name[name.size()-1]==']')
227 throw invalid_argument("Program::get_attribute_location");
229 auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);
230 return i!=reflect_data.attributes.end() && i->name==name ? i->location : -1;
234 Program::Loader::Loader(Program &p, Collection &c):
235 DataFile::CollectionObjectLoader<Program>(p, &c)
237 add("module", &Loader::module);
240 void Program::Loader::module(const string &n)
242 map<string, int> spec_values;
243 SpecializationLoader ldr(spec_values);
245 obj.add_stages(get_collection().get<Module>(n), spec_values);
249 DataFile::Loader::ActionMap Program::SpecializationLoader::shared_actions;
251 Program::SpecializationLoader::SpecializationLoader(map<string, int> &sv):
254 set_actions(shared_actions);
257 void Program::SpecializationLoader::init_actions()
259 add("specialize", &SpecializationLoader::specialize_bool);
260 add("specialize", &SpecializationLoader::specialize_int);
263 void Program::SpecializationLoader::specialize_bool(const string &name, bool value)
265 spec_values[name] = value;
268 void Program::SpecializationLoader::specialize_int(const string &name, int value)
270 spec_values[name] = value;