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