]> git.tdb.fi Git - libs/gl.git/blob - source/core/program.cpp
Use emplace_back when a new object is being constructed
[libs/gl.git] / source / core / program.cpp
1 #include <msp/core/algorithm.h>
2 #include "error.h"
3 #include "program.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 Program::Program(const Module &mod, const map<string, int> &spec_values)
11 {
12         add_stages(mod, spec_values);
13 }
14
15 void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
16 {
17         if(has_stages())
18                 throw invalid_operation("Program::add_stages");
19
20         reflect_data = ReflectData();
21
22         TransientData transient;
23         switch(mod.get_format())
24         {
25         case Module::GLSL:
26                 add_glsl_stages(static_cast<const GlslModule &>(mod), spec_values, transient);
27                 break;
28         case Module::SPIR_V:
29                 add_spirv_stages(static_cast<const SpirVModule &>(mod), spec_values);
30                 break;
31         default:
32                 throw invalid_argument("Program::add_stages");
33         }
34
35         finalize(mod, transient);
36
37         if(mod.get_format()==Module::SPIR_V)
38         {
39                 const SpirVModule &spirv_mod = static_cast<const SpirVModule &>(mod);
40                 vector<uint8_t> used_variables = collect_used_variables(spirv_mod, spec_values);
41                 collect_uniforms(spirv_mod, used_variables);
42                 collect_attributes(spirv_mod, used_variables);
43                 collect_builtins(spirv_mod);
44         }
45
46         finalize_uniforms();
47
48         for(const ReflectData::UniformInfo &u: reflect_data.uniforms)
49                 require_type(u.type);
50         for(const ReflectData::AttributeInfo &a: reflect_data.attributes)
51                 require_type(a.type);
52 }
53
54 vector<uint8_t> Program::collect_used_variables(const SpirVModule &mod, const map<string, int> &spec_values)
55 {
56         std::map<unsigned, int> spec_values_by_id;
57         for(const SpirVModule::Constant &c: mod.get_spec_constants())
58         {
59                 auto i = spec_values.find(c.name);
60                 if(i!=spec_values.end())
61                         spec_values_by_id[c.constant_id] = i->second;
62         }
63
64         const vector<SpirVModule::InstructionBlock> &blocks = mod.get_blocks();
65         vector<uint8_t> visited(blocks.size(), 4);
66         for(unsigned i=0; i<blocks.size(); ++i)
67         {
68                 const SpirVModule::InstructionBlock &b = blocks[i];
69
70                 bool cond = true;
71                 if(b.condition)
72                 {
73                         cond = b.condition->i_value;
74                         auto j = spec_values_by_id.find(b.condition->constant_id);
75                         if(j!=spec_values_by_id.end())
76                                 cond = j->second;
77                         if(b.negate_condition)
78                                 cond = !cond;
79                 }
80
81                 visited[i] |= cond*2;
82                 for(const SpirVModule::InstructionBlock *s: b.successors)
83                         visited[s-blocks.data()] &= 3;
84         }
85
86         for(unsigned i=0; i<blocks.size(); ++i)
87                 if(visited[i]&4)
88                         collect_visited_blocks(blocks, i, visited);
89
90         const vector<SpirVModule::Variable> &variables = mod.get_variables();
91         vector<uint8_t> used(variables.size());
92         for(unsigned i=0; i<blocks.size(); ++i)
93                 if(visited[i]&1)
94                 {
95                         for(const SpirVModule::Variable *v: blocks[i].accessed_variables)
96                                 used[v-variables.data()] = 1;
97                 }
98
99         return used;
100 }
101
102 void Program::collect_visited_blocks(const vector<SpirVModule::InstructionBlock> &blocks, unsigned i, vector<uint8_t> &visited)
103 {
104         visited[i] |= 1;
105         for(const SpirVModule::InstructionBlock *s: blocks[i].successors)
106         {
107                 unsigned j = s-blocks.data();
108                 if((visited[j]&3)==2)
109                         collect_visited_blocks(blocks, j, visited);
110         }
111 }
112
113 void Program::collect_uniforms(const SpirVModule &mod, const vector<uint8_t> &used_variables)
114 {
115         // Prepare the default block
116         reflect_data.uniform_blocks.emplace_back();
117         vector<vector<string> > block_uniform_names(1);
118
119         const vector<SpirVModule::Variable> &variables = mod.get_variables();
120         unsigned n_descriptor_sets = 0;
121         for(unsigned i=0; i<variables.size(); ++i)
122         {
123                 if(!used_variables[i])
124                         continue;
125
126                 const SpirVModule::Variable &v = variables[i];
127                 if((v.storage==SpirVModule::UNIFORM || v.storage==SpirVModule::PUSH_CONSTANT) && v.struct_type)
128                 {
129                         reflect_data.uniform_blocks.emplace_back();
130                         ReflectData::UniformBlockInfo &info = reflect_data.uniform_blocks.back();
131                         info.name = v.struct_type->name;
132                         if(v.storage==SpirVModule::PUSH_CONSTANT)
133                                 info.bind_point = ReflectData::PUSH_CONSTANT;
134                         else
135                         {
136                                 if(v.binding>=0)
137                                         info.bind_point = v.binding | (v.descriptor_set<<20);
138                                 else
139                                         info.bind_point = ReflectData::DEFAULT_BLOCK;
140                                 n_descriptor_sets = max(n_descriptor_sets, v.descriptor_set+1);
141                         }
142                         info.data_size = v.struct_type->size;
143
144                         string prefix;
145                         if(!v.name.empty())
146                                 prefix = v.struct_type->name+".";
147                         block_uniform_names.emplace_back();
148                         collect_block_uniforms(*v.struct_type, prefix, 0, block_uniform_names.back());
149                 }
150                 else if(v.storage==SpirVModule::UNIFORM_CONSTANT && (v.location>=0 || v.binding>=0))
151                 {
152                         block_uniform_names[0].push_back(v.name);
153                         reflect_data.uniforms.emplace_back();
154                         ReflectData::UniformInfo &info = reflect_data.uniforms.back();
155                         info.name = v.name;
156                         info.tag = v.name;
157                         info.location = v.location;
158                         if(v.binding>=0)
159                                 info.binding = v.binding | (v.descriptor_set<<20);
160                         n_descriptor_sets = max(n_descriptor_sets, v.descriptor_set+1);
161                         info.array_size = max(v.array_size, 1U);
162                         info.type = v.type;
163                 }
164         }
165
166         sort_member(reflect_data.uniforms, &ReflectData::UniformInfo::tag);
167
168         if(block_uniform_names.front().empty())
169         {
170                 reflect_data.uniform_blocks.erase(reflect_data.uniform_blocks.begin());
171                 block_uniform_names.erase(block_uniform_names.begin());
172         }
173
174         for(unsigned i=0; i<reflect_data.uniform_blocks.size(); ++i)
175         {
176                 ReflectData::UniformBlockInfo &block = reflect_data.uniform_blocks[i];
177                 for(const string &n: block_uniform_names[i])
178                 {
179                         // The element is already known to be present
180                         ReflectData::UniformInfo &uni = *lower_bound_member(reflect_data.uniforms, Tag(n), &ReflectData::UniformInfo::tag);
181                         block.uniforms.push_back(&uni);
182                         uni.block = &block;
183                 }
184                 block.sort_uniforms();
185                 block.update_layout_hash();
186         }
187
188         reflect_data.n_descriptor_sets = n_descriptor_sets;
189         reflect_data.update_layout_hash();
190 }
191
192 void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset, vector<string> &uniform_names)
193 {
194         for(const SpirVModule::StructMember &m: strct.members)
195         {
196                 unsigned offset = base_offset+m.offset;
197                 if(m.struct_type)
198                 {
199                         if(m.array_size)
200                         {
201                                 for(unsigned j=0; j<m.array_size; ++j, offset+=m.array_stride)
202                                         collect_block_uniforms(*m.struct_type, format("%s%s[%d].", prefix, m.name, j), offset, uniform_names);
203                         }
204                         else
205                                 collect_block_uniforms(*m.struct_type, prefix+m.name+".", offset, uniform_names);
206                 }
207                 else
208                 {
209                         string name = prefix+m.name;
210                         uniform_names.push_back(name);
211                         reflect_data.uniforms.emplace_back();
212                         ReflectData::UniformInfo &info = reflect_data.uniforms.back();
213                         info.name = name;
214                         info.tag = name;
215                         info.offset = offset;
216                         info.array_size = max(m.array_size, 1U);
217                         info.array_stride = m.array_stride;
218                         info.matrix_stride = m.matrix_stride;
219                         info.type = m.type;
220                 }
221         }
222 }
223
224 void Program::collect_attributes(const SpirVModule &mod, const vector<uint8_t> &used_variables)
225 {
226         const vector<SpirVModule::Variable> &variables = mod.get_variables();
227         for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
228                 if(e.stage==SpirVModule::VERTEX && e.name=="main")
229                 {
230                         for(const SpirVModule::Variable *v: e.globals)
231                                 if(v->storage==SpirVModule::INPUT && used_variables[v-variables.data()])
232                                 {
233                                         reflect_data.attributes.emplace_back();
234                                         ReflectData::AttributeInfo &info = reflect_data.attributes.back();
235                                         info.name = v->name;
236                                         info.location = v->location;
237                                         info.array_size = v->array_size;
238                                         info.type = v->type;
239                                 }
240                 }
241 }
242
243 void Program::collect_builtins(const SpirVModule &mod)
244 {
245         for(const SpirVModule::Variable &v: mod.get_variables())
246                 if(v.storage==SpirVModule::OUTPUT && v.struct_type)
247                         collect_builtins(*v.struct_type);
248 }
249
250 void Program::collect_builtins(const SpirVModule::Structure &strct)
251 {
252         for(const SpirVModule::StructMember &m: strct.members)
253                 if(m.builtin==SpirVModule::CLIP_DISTANCE)
254                         reflect_data.n_clip_distances = m.array_size;
255 }
256
257 const ReflectData::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
258 {
259         auto i = find_member(reflect_data.uniform_blocks, name, &ReflectData::UniformBlockInfo::name);
260         if(i==reflect_data.uniform_blocks.end())
261                 throw key_error(name);
262         return *i;
263 }
264
265 const ReflectData::UniformInfo &Program::get_uniform_info(const string &name) const
266 {
267         auto i = lower_bound_member(reflect_data.uniforms, Tag(name), &ReflectData::UniformInfo::tag);
268         if(i==reflect_data.uniforms.end() || i->name!=name)
269                 throw key_error(name);
270         return *i;
271 }
272
273 const ReflectData::UniformInfo &Program::get_uniform_info(Tag tag) const
274 {
275         auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
276         if(i==reflect_data.uniforms.end() || i->tag!=tag)
277                 throw key_error(tag);
278         return *i;
279 }
280
281 int Program::get_uniform_location(const string &name) const
282 {
283         if(name[name.size()-1]==']')
284                 throw invalid_argument("Program::get_uniform_location");
285
286         auto i = lower_bound_member(reflect_data.uniforms, Tag(name), &ReflectData::UniformInfo::tag);
287         return i!=reflect_data.uniforms.end() && i->name==name && i->block->bind_point<0 ? i->location : -1;
288 }
289
290 int Program::get_uniform_location(Tag tag) const
291 {
292         auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
293         return i!=reflect_data.uniforms.end() && i->tag==tag && i->block->bind_point<0 ? i->location : -1;
294 }
295
296 int Program::get_uniform_binding(Tag tag) const
297 {
298         auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
299         return i!=reflect_data.uniforms.end() && i->tag==tag ? i->binding : -1;
300 }
301
302 const ReflectData::AttributeInfo &Program::get_attribute_info(const string &name) const
303 {
304         auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);
305         if(i==reflect_data.attributes.end() || i->name!=name)
306                 throw key_error(name);
307         return *i;
308 }
309
310 int Program::get_attribute_location(const string &name) const
311 {
312         if(name[name.size()-1]==']')
313                 throw invalid_argument("Program::get_attribute_location");
314
315         auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);
316         return i!=reflect_data.attributes.end() && i->name==name ? i->location : -1;
317 }
318
319
320 Program::Loader::Loader(Program &p, Collection &c):
321         DataFile::CollectionObjectLoader<Program>(p, &c)
322 {
323         add("module", &Loader::module);
324 }
325
326 void Program::Loader::module(const string &n)
327 {
328         map<string, int> spec_values;
329         SpecializationLoader ldr(spec_values);
330         load_sub_with(ldr);
331         obj.add_stages(get_collection().get<Module>(n), spec_values);
332 }
333
334
335 DataFile::Loader::ActionMap Program::SpecializationLoader::shared_actions;
336
337 Program::SpecializationLoader::SpecializationLoader(map<string, int> &sv):
338         spec_values(sv)
339 {
340         set_actions(shared_actions);
341 }
342
343 void Program::SpecializationLoader::init_actions()
344 {
345         add("specialize", &SpecializationLoader::specialize_bool);
346         add("specialize", &SpecializationLoader::specialize_int);
347 }
348
349 void Program::SpecializationLoader::specialize_bool(const string &name, bool value)
350 {
351         spec_values[name] = value;
352 }
353
354 void Program::SpecializationLoader::specialize_int(const string &name, int value)
355 {
356         spec_values[name] = value;
357 }
358
359 } // namespace GL
360 } // namespace Msp