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