]> git.tdb.fi Git - libs/gl.git/blob - source/core/program.cpp
Use a streaming buffer for ProgramData only when needed
[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         TransientData transient;
37         switch(mod.get_format())
38         {
39         case Module::GLSL:
40                 add_glsl_stages(static_cast<const GlslModule &>(mod), spec_values, transient);
41                 break;
42         case Module::SPIR_V:
43                 if(!spec_values.empty())
44                 {
45                         specialized_spirv = static_cast<const SpirVModule &>(mod).specialize(spec_values);
46                         final_module = specialized_spirv;
47                 }
48                 add_spirv_stages(*static_cast<const SpirVModule *>(final_module), spec_values);
49                 break;
50         default:
51                 throw invalid_argument("Program::add_stages");
52         }
53
54         finalize(*final_module, transient);
55
56         if(final_module->get_format()==Module::SPIR_V)
57         {
58                 const SpirVModule &spirv_mod = *static_cast<const SpirVModule *>(final_module);
59                 collect_uniforms(spirv_mod);
60                 collect_attributes(spirv_mod);
61                 collect_builtins(spirv_mod);
62         }
63
64         finalize_uniforms();
65
66         reflect_data.update_used_bindings();
67
68         for(const ReflectData::UniformInfo &u: reflect_data.uniforms)
69                 require_type(u.type);
70         for(const ReflectData::AttributeInfo &a: reflect_data.attributes)
71                 require_type(a.type);
72 }
73
74 void Program::collect_uniforms(const SpirVModule &mod)
75 {
76         // Prepare the default block
77         reflect_data.uniform_blocks.emplace_back();
78         vector<vector<string> > block_uniform_names(1);
79
80         unsigned n_descriptor_sets = 0;
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                         if(v.storage==SpirVModule::PUSH_CONSTANT)
89                                 info.bind_point = ReflectData::PUSH_CONSTANT;
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                                 n_descriptor_sets = max(n_descriptor_sets, v.descriptor_set+1);
97                         }
98                         info.data_size = v.struct_type->size;
99
100                         string prefix;
101                         if(!v.name.empty())
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());
105                 }
106                 else if(v.storage==SpirVModule::UNIFORM_CONSTANT && (v.location>=0 || v.binding>=0))
107                 {
108                         block_uniform_names[0].push_back(v.name);
109                         reflect_data.uniforms.emplace_back();
110                         ReflectData::UniformInfo &info = reflect_data.uniforms.back();
111                         info.name = v.name;
112                         info.tag = v.name;
113                         info.location = v.location;
114                         if(v.binding>=0)
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);
118                         info.type = v.type;
119                 }
120         }
121
122         sort_member(reflect_data.uniforms, &ReflectData::UniformInfo::tag);
123
124         if(block_uniform_names.front().empty())
125         {
126                 reflect_data.uniform_blocks.erase(reflect_data.uniform_blocks.begin());
127                 block_uniform_names.erase(block_uniform_names.begin());
128         }
129
130         for(unsigned i=0; i<reflect_data.uniform_blocks.size(); ++i)
131         {
132                 ReflectData::UniformBlockInfo &block = reflect_data.uniform_blocks[i];
133                 for(const string &n: block_uniform_names[i])
134                 {
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);
138                         uni.block = &block;
139                 }
140                 block.sort_uniforms();
141                 block.update_layout_hash();
142         }
143
144         reflect_data.n_descriptor_sets = n_descriptor_sets;
145         reflect_data.update_layout_hash();
146 }
147
148 void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset, vector<string> &uniform_names)
149 {
150         for(const SpirVModule::StructMember &m: strct.members)
151         {
152                 unsigned offset = base_offset+m.offset;
153                 if(m.struct_type)
154                 {
155                         if(m.array_size)
156                         {
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);
159                         }
160                         else
161                                 collect_block_uniforms(*m.struct_type, prefix+m.name+".", offset, uniform_names);
162                 }
163                 else
164                 {
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();
169                         info.name = name;
170                         info.tag = name;
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;
175                         info.type = m.type;
176                 }
177         }
178 }
179
180 void Program::collect_attributes(const SpirVModule &mod)
181 {
182         for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
183                 if(e.stage==SpirVModule::VERTEX && e.name=="main")
184                 {
185                         for(const SpirVModule::Variable *v: e.globals)
186                                 if(v->storage==SpirVModule::INPUT)
187                                 {
188                                         reflect_data.attributes.emplace_back();
189                                         ReflectData::AttributeInfo &info = reflect_data.attributes.back();
190                                         info.name = v->name;
191                                         info.location = v->location;
192                                         info.array_size = v->array_size;
193                                         info.type = v->type;
194                                 }
195                 }
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