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