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