]> git.tdb.fi Git - libs/gl.git/blob - source/core/program.cpp
Check the flat qualifier from the correct member
[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 Program::Program(Program &&other):
16         ProgramBackend(move(other)),
17         reflect_data(move(other.reflect_data)),
18         specialized_spirv(other.specialized_spirv)
19 {
20         other.specialized_spirv = 0;
21 }
22
23 Program::~Program()
24 {
25         delete specialized_spirv;
26 }
27
28 void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
29 {
30         if(has_stages())
31                 throw invalid_operation("Program::add_stages");
32
33         reflect_data = ReflectData();
34         const Module *final_module = &mod;
35
36         switch(mod.get_format())
37         {
38         case Module::GLSL:
39                 add_glsl_stages(static_cast<const GlslModule &>(mod), spec_values);
40                 break;
41         case Module::SPIR_V:
42                 if(static_cast<const SpirVModule &>(mod).is_specializable())
43                 {
44                         specialized_spirv = static_cast<const SpirVModule &>(mod).specialize(spec_values);
45                         final_module = specialized_spirv;
46                 }
47                 add_spirv_stages(*static_cast<const SpirVModule *>(final_module), spec_values);
48                 break;
49         default:
50                 throw invalid_argument("Program::add_stages");
51         }
52
53         if(final_module->get_format()==Module::SPIR_V)
54         {
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);
59         }
60
61         finalize_uniforms();
62
63         reflect_data.update_used_bindings();
64
65         for(const ReflectData::UniformInfo &u: reflect_data.uniforms)
66                 require_type(u.type);
67         for(const ReflectData::AttributeInfo &a: reflect_data.attributes)
68                 require_type(a.type);
69 }
70
71 void Program::collect_uniforms(const SpirVModule &mod)
72 {
73         // Prepare the default block
74         reflect_data.uniform_blocks.emplace_back();
75         vector<vector<string> > block_uniform_names(1);
76
77         for(const SpirVModule::Variable &v: mod.get_variables())
78         {
79                 if((v.storage==SpirVModule::UNIFORM || v.storage==SpirVModule::PUSH_CONSTANT) && v.struct_type)
80                 {
81                         reflect_data.uniform_blocks.emplace_back();
82                         ReflectData::UniformBlockInfo &info = reflect_data.uniform_blocks.back();
83                         info.name = v.struct_type->name;
84                         info.data_size = v.struct_type->size;
85                         if(v.storage==SpirVModule::PUSH_CONSTANT)
86                         {
87                                 info.bind_point = ReflectData::PUSH_CONSTANT;
88                                 reflect_data.push_constants_size = info.data_size;
89                         }
90                         else
91                         {
92                                 if(v.binding>=0)
93                                         info.bind_point = v.binding | (v.descriptor_set<<20);
94                                 else
95                                         info.bind_point = ReflectData::DEFAULT_BLOCK;
96                                 reflect_data.n_descriptor_sets = max(reflect_data.n_descriptor_sets, v.descriptor_set+1);
97                         }
98
99                         string prefix;
100                         if(!v.name.empty())
101                                 prefix = v.struct_type->name+".";
102                         block_uniform_names.emplace_back();
103                         collect_block_uniforms(*v.struct_type, prefix, 0, block_uniform_names.back());
104                 }
105                 else if(v.storage==SpirVModule::UNIFORM_CONSTANT && (v.location>=0 || v.binding>=0))
106                 {
107                         block_uniform_names[0].push_back(v.name);
108                         reflect_data.uniforms.emplace_back();
109                         ReflectData::UniformInfo &info = reflect_data.uniforms.back();
110                         info.name = v.name;
111                         info.tag = v.name;
112                         info.location = v.location;
113                         if(v.binding>=0)
114                                 info.binding = v.binding | (v.descriptor_set<<20);
115                         reflect_data.n_descriptor_sets = max(reflect_data.n_descriptor_sets, v.descriptor_set+1);
116                         info.array_size = max(v.array_size, 1U);
117                         info.type = v.type;
118                 }
119         }
120
121         sort_member(reflect_data.uniforms, &ReflectData::UniformInfo::tag);
122
123         if(block_uniform_names.front().empty())
124         {
125                 reflect_data.uniform_blocks.erase(reflect_data.uniform_blocks.begin());
126                 block_uniform_names.erase(block_uniform_names.begin());
127         }
128
129         for(unsigned i=0; i<reflect_data.uniform_blocks.size(); ++i)
130         {
131                 ReflectData::UniformBlockInfo &block = reflect_data.uniform_blocks[i];
132                 for(const string &n: block_uniform_names[i])
133                 {
134                         // The element is already known to be present
135                         ReflectData::UniformInfo &uni = *lower_bound_member(reflect_data.uniforms, Tag(n), &ReflectData::UniformInfo::tag);
136                         block.uniforms.push_back(&uni);
137                         uni.block = &block;
138                 }
139                 block.sort_uniforms();
140                 block.update_layout_hash();
141         }
142
143         reflect_data.update_layout_hash();
144 }
145
146 void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset, vector<string> &uniform_names)
147 {
148         for(const SpirVModule::StructMember &m: strct.members)
149         {
150                 unsigned offset = base_offset+m.offset;
151                 if(m.struct_type)
152                 {
153                         if(m.array_size)
154                         {
155                                 for(unsigned j=0; j<m.array_size; ++j, offset+=m.array_stride)
156                                         collect_block_uniforms(*m.struct_type, format("%s%s[%d].", prefix, m.name, j), offset, uniform_names);
157                         }
158                         else
159                                 collect_block_uniforms(*m.struct_type, prefix+m.name+".", offset, uniform_names);
160                 }
161                 else
162                 {
163                         string name = prefix+m.name;
164                         uniform_names.push_back(name);
165                         reflect_data.uniforms.emplace_back();
166                         ReflectData::UniformInfo &info = reflect_data.uniforms.back();
167                         info.name = name;
168                         info.tag = name;
169                         info.offset = offset;
170                         info.array_size = max(m.array_size, 1U);
171                         info.array_stride = m.array_stride;
172                         info.matrix_stride = m.matrix_stride;
173                         info.type = m.type;
174                 }
175         }
176 }
177
178 void Program::collect_attributes(const SpirVModule &mod)
179 {
180         for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
181                 if(e.stage==SpirVModule::VERTEX && e.name=="main")
182                 {
183                         for(const SpirVModule::Variable *v: e.globals)
184                                 if(v->storage==SpirVModule::INPUT)
185                                 {
186                                         reflect_data.attributes.emplace_back();
187                                         ReflectData::AttributeInfo &info = reflect_data.attributes.back();
188                                         info.name = v->name;
189                                         info.location = v->location;
190                                         info.array_size = v->array_size;
191                                         info.type = v->type;
192                                 }
193                 }
194
195         sort_member(reflect_data.attributes, &ReflectData::AttributeInfo::name);
196 }
197
198 void Program::collect_builtins(const SpirVModule &mod)
199 {
200         for(const SpirVModule::Variable &v: mod.get_variables())
201                 if(v.storage==SpirVModule::OUTPUT && v.struct_type)
202                         collect_builtins(*v.struct_type);
203 }
204
205 void Program::collect_builtins(const SpirVModule::Structure &strct)
206 {
207         for(const SpirVModule::StructMember &m: strct.members)
208                 if(m.builtin==SpirVModule::CLIP_DISTANCE)
209                         reflect_data.n_clip_distances = m.array_size;
210 }
211
212 const ReflectData::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
213 {
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);
217         return *i;
218 }
219
220 const ReflectData::UniformInfo &Program::get_uniform_info(const string &name) const
221 {
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);
225         return *i;
226 }
227
228 const ReflectData::UniformInfo &Program::get_uniform_info(Tag tag) const
229 {
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);
233         return *i;
234 }
235
236 int Program::get_uniform_location(const string &name) const
237 {
238         if(name[name.size()-1]==']')
239                 throw invalid_argument("Program::get_uniform_location");
240
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;
243 }
244
245 int Program::get_uniform_location(Tag tag) const
246 {
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;
249 }
250
251 int Program::get_uniform_binding(Tag tag) const
252 {
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;
255 }
256
257 bool Program::uses_binding(int binding) const
258 {
259         auto i = lower_bound(reflect_data.used_bindings, binding);
260         return i!=reflect_data.used_bindings.end() && *i==binding;
261 }
262
263 const ReflectData::AttributeInfo &Program::get_attribute_info(const string &name) const
264 {
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);
268         return *i;
269 }
270
271 int Program::get_attribute_location(const string &name) const
272 {
273         if(name[name.size()-1]==']')
274                 throw invalid_argument("Program::get_attribute_location");
275
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;
278 }
279
280
281 Program::Loader::Loader(Program &p, Collection &c):
282         DataFile::CollectionObjectLoader<Program>(p, &c)
283 {
284         add("module", &Loader::module);
285 }
286
287 void Program::Loader::module(const string &n)
288 {
289         map<string, int> spec_values;
290         SpecializationLoader ldr(spec_values);
291         load_sub_with(ldr);
292         obj.add_stages(get_collection().get<Module>(n), spec_values);
293 }
294
295
296 DataFile::Loader::ActionMap Program::SpecializationLoader::shared_actions;
297
298 Program::SpecializationLoader::SpecializationLoader(map<string, int> &sv):
299         spec_values(sv)
300 {
301         set_actions(shared_actions);
302 }
303
304 void Program::SpecializationLoader::init_actions()
305 {
306         add("specialize", &SpecializationLoader::specialize_bool);
307         add("specialize", &SpecializationLoader::specialize_int);
308 }
309
310 void Program::SpecializationLoader::specialize_bool(const string &name, bool value)
311 {
312         spec_values[name] = value;
313 }
314
315 void Program::SpecializationLoader::specialize_int(const string &name, int value)
316 {
317         spec_values[name] = value;
318 }
319
320 } // namespace GL
321 } // namespace Msp