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