]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.cpp
Replace some local pointers with references in VulkanCommands
[libs/gl.git] / source / glsl / generate.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/core/hash.h>
3 #include <msp/core/raii.h>
4 #include "generate.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10 namespace SL {
11
12 void ConstantIdAssigner::apply(Module &module, const Features &features)
13 {
14         for(Stage &s: module.stages)
15                 s.content.visit(*this);
16
17         for(VariableDeclaration *v: auto_constants)
18         {
19                 unsigned id;
20                 auto j = existing_constants.find(v->name);
21                 if(j!=existing_constants.end())
22                         id = j->second;
23                 else
24                 {
25                         id = hash<32>(v->name)%features.constant_id_range;
26                         while(used_ids.count(id))
27                                 id = (id+1)%features.constant_id_range;
28                 }
29
30                 auto i = find_member(v->layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
31                 if(i!=v->layout->qualifiers.end())
32                         i->value = id;
33
34                 used_ids.insert(id);
35                 existing_constants[v->name] = id;
36         }
37 }
38
39 void ConstantIdAssigner::visit(VariableDeclaration &var)
40 {
41         if(var.layout)
42         {
43                 auto i = find_member(var.layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
44                 if(i!=var.layout->qualifiers.end() && i->has_value)
45                 {
46                         if(i->value==-1)
47                                 auto_constants.push_back(&var);
48                         else
49                         {
50                                 existing_constants[var.name] = i->value;
51                                 used_ids.insert(i->value);
52                         }
53                 }
54         }
55 }
56
57
58 string InterfaceGenerator::get_out_prefix(Stage::Type type)
59 {
60         if(type==Stage::VERTEX)
61                 return "_vs_out_";
62         else if(type==Stage::GEOMETRY)
63                 return "_gs_out_";
64         else
65                 return string();
66 }
67
68 void InterfaceGenerator::apply(Stage &s)
69 {
70         stage = &s;
71         iface_target_block = &stage->content;
72         if(stage->previous)
73                 in_prefix = get_out_prefix(stage->previous->type);
74         out_prefix = get_out_prefix(stage->type);
75         s.content.visit(*this);
76         NodeRemover().apply(s, nodes_to_remove);
77 }
78
79 void InterfaceGenerator::visit(Block &block)
80 {
81         SetForScope<Block *> set_block(current_block, &block);
82         for(auto i=block.body.begin(); i!=block.body.end(); ++i)
83         {
84                 assignment_insert_point = i;
85                 if(&block==&stage->content)
86                         iface_insert_point = i;
87
88                 (*i)->visit(*this);
89         }
90 }
91
92 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
93 {
94         unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
95         return prefix+name.substr(offset);
96 }
97
98 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
99 {
100         if(stage->content.variables.count(name))
101                 return 0;
102
103         if(stage->type==Stage::GEOMETRY && var.interface=="out" && var.array)
104                 return 0;
105
106         VariableDeclaration* iface_var = new VariableDeclaration;
107         iface_var->sampling = var.sampling;
108         iface_var->interface = iface;
109         iface_var->type = var.type;
110         iface_var->name = name;
111         // Geometry shader inputs are always arrays.
112         if(stage->type==Stage::GEOMETRY)
113                 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
114         else
115                 iface_var->array = var.array;
116         if(iface_var->array)
117                 iface_var->array_size = var.array_size;
118         if(iface=="in")
119         {
120                 iface_var->layout = var.layout;
121                 iface_var->linked_declaration = &var;
122                 var.linked_declaration = iface_var;
123         }
124
125         if(var.block_declaration)
126         {
127                 StructDeclaration *iface_type = var.block_declaration->clone();
128                 iface_type->name = format("_%s_%s", iface, var.block_declaration->block_name);
129                 iface_target_block->body.insert(iface_insert_point, iface_type);
130
131                 iface_var->type = iface_type->name;
132                 if(name.empty())
133                         iface_var->name = format("%s %s", iface, var.block_declaration->block_name);
134
135                 stage->interface_blocks.insert(make_pair("in "+var.block_declaration->block_name, iface_var));
136                 if(!name.empty())
137                         stage->interface_blocks.insert(make_pair(name, iface_var));
138         }
139
140         iface_target_block->body.insert(iface_insert_point, iface_var);
141         iface_target_block->variables.insert(make_pair(name, iface_var));
142         if(iface_target_block==&stage->content && iface=="in")
143                 declared_inputs.push_back(iface_var);
144
145         return iface_var;
146 }
147
148 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
149 {
150         Assignment *assign = new Assignment;
151
152         string::size_type dot = left.find('.');
153         VariableReference *ref = new VariableReference;
154         ref->name = left.substr(0, dot);
155         assign->left = ref;
156
157         while(dot!=string::npos)
158         {
159                 string::size_type start = dot+1;
160                 dot = left.find('.', start);
161
162                 MemberAccess *memacc = new MemberAccess;
163                 memacc->left = assign->left;
164                 memacc->member = left.substr(start, dot-start);
165                 assign->left = memacc;
166         }
167
168         assign->oper = &Operator::get_operator("=", Operator::BINARY);
169         assign->right = right;
170
171         ExpressionStatement *stmt = new ExpressionStatement;
172         stmt->expression = assign;
173         current_block->body.insert(assignment_insert_point, stmt);
174         stmt->visit(*this);
175
176         return *stmt;
177 }
178
179 void InterfaceGenerator::visit(VariableReference &var)
180 {
181         if(var.declaration || !stage->previous)
182                 return;
183         /* Don't pull a variable from previous stage if we just generated an output
184         interface in this stage */
185         if(stage->content.variables.count(var.name))
186                 return;
187
188         const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
189         auto i = prev_vars.find(var.name);
190         if(i==prev_vars.end() || i->second->interface!="out")
191                 i = prev_vars.find(in_prefix+var.name);
192         if(i!=prev_vars.end() && i->second->interface=="out")
193         {
194                 if(stage->type==Stage::GEOMETRY && i->second->array)
195                         stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
196                                 format("Can't access '%s' through automatic interface because it's an array", var.name)));
197                 else
198                 {
199                         generate_interface(*i->second, "in", i->second->name);
200                         var.name = i->second->name;
201                 }
202                 return;
203         }
204
205         for(const auto &kvp: stage->previous->interface_blocks)
206                 if(kvp.second->name.find(' ')!=string::npos)
207                 {
208                         const map<string, VariableDeclaration *> &iface_vars = kvp.second->block_declaration->members.variables;
209                         i = iface_vars.find(var.name);
210                         if(i!=iface_vars.end())
211                         {
212                                 generate_interface(*kvp.second, "in", string());
213                                 return;
214                         }
215                 }
216 }
217
218 void InterfaceGenerator::visit(VariableDeclaration &var)
219 {
220         if(var.interface=="out")
221         {
222                 /* For output variables in function scope, generate a global interface
223                 and replace the local declaration with an assignment. */
224                 VariableDeclaration *out_var = 0;
225                 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
226                 {
227                         out_var->source = var.source;
228                         out_var->line = var.line;
229                         nodes_to_remove.insert(&var);
230                         if(var.init_expression)
231                         {
232                                 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
233                                 stmt.source = var.source;
234                                 stmt.line = var.line;
235                                 return;
236                         }
237                 }
238         }
239         else if(var.interface=="in" && current_block==&stage->content)
240         {
241                 if(var.name.compare(0, 3, "gl_"))
242                         declared_inputs.push_back(&var);
243
244                 /* Try to link input variables in global scope with output variables from
245                 previous stage. */
246                 if(!var.linked_declaration && stage->previous)
247                 {
248                         const map<string, VariableDeclaration *> *prev_vars;
249                         string name;
250                         // Blocks are linked by their block name, not instance name
251                         if(var.block_declaration)
252                         {
253                                 prev_vars = &stage->previous->interface_blocks;
254                                 name = "out "+var.block_declaration->block_name;
255                         }
256                         else
257                         {
258                                 prev_vars = &stage->previous->content.variables;
259                                 name = var.name;
260                         }
261
262                         auto i = prev_vars->find(name);
263                         if(i!=prev_vars->end() && i->second->interface=="out")
264                         {
265                                 var.linked_declaration = i->second;
266                                 i->second->linked_declaration = &var;
267                         }
268                 }
269         }
270
271         TraversingVisitor::visit(var);
272 }
273
274 void InterfaceGenerator::visit(FunctionDeclaration &func)
275 {
276         SetFlag set_scope(function_scope, true);
277         // Skip parameters because they're not useful here
278         func.body.visit(*this);
279 }
280
281 void InterfaceGenerator::visit(Passthrough &pass)
282 {
283         // Pass through all input variables declared so far.
284         vector<VariableDeclaration *> pass_vars = declared_inputs;
285
286         if(stage->previous)
287         {
288                 for(const auto &kvp: stage->previous->content.variables)
289                 {
290                         if(kvp.second->interface!="out")
291                                 continue;
292
293                         /* Pass through output variables from the previous stage, but only
294                         those which are not already linked to an input here. */
295                         if(!kvp.second->linked_declaration && generate_interface(*kvp.second, "in", kvp.second->name))
296                                 pass_vars.push_back(kvp.second);
297                 }
298         }
299
300         if(stage->type==Stage::GEOMETRY)
301         {
302                 /* Special case for geometry shader: copy gl_Position from input to
303                 output. */
304                 VariableReference *ref = new VariableReference;
305                 ref->name = "gl_in";
306
307                 BinaryExpression *subscript = new BinaryExpression;
308                 subscript->left = ref;
309                 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
310                 subscript->right = pass.subscript;
311
312                 MemberAccess *memacc = new MemberAccess;
313                 memacc->left = subscript;
314                 memacc->member = "gl_Position";
315
316                 insert_assignment("out gl_PerVertex.gl_Position", memacc);
317         }
318
319         for(VariableDeclaration *v: pass_vars)
320         {
321                 string out_name = change_prefix(v->name, out_prefix);
322                 generate_interface(*v, "out", out_name);
323
324                 VariableReference *ref = new VariableReference;
325                 ref->name = v->name;
326                 if(pass.subscript)
327                 {
328                         BinaryExpression *subscript = new BinaryExpression;
329                         subscript->left = ref;
330                         subscript->oper = &Operator::get_operator("[", Operator::BINARY);
331                         subscript->right = pass.subscript;
332                         insert_assignment(out_name, subscript);
333                 }
334                 else
335                         insert_assignment(out_name, ref);
336         }
337
338         nodes_to_remove.insert(&pass);
339 }
340
341
342 void ArraySizer::apply(Stage &stage)
343 {
344         stage.content.visit(*this);
345         for(const auto &kvp: max_indices)
346                 if(kvp.first->array && !kvp.first->array_size)
347                 {
348                         int size = 0;
349                         if(stage.type==Stage::GEOMETRY && kvp.first->interface=="in")
350                                 size = input_size;
351                         else if(kvp.second>=0)
352                                 size = kvp.second+1;
353                         else if(!kvp.first->name.compare(0, 3, "gl_"))
354                                 size = 1;
355
356                         if(size>0)
357                         {
358                                 Literal *literal_size = new Literal;
359                                 literal_size->token = lexical_cast<string>(size);
360                                 literal_size->value = size;
361                                 kvp.first->array_size = literal_size;
362                         }
363                 }
364 }
365
366 void ArraySizer::visit(VariableReference &var)
367 {
368         r_declaration = var.declaration;
369 }
370
371 void ArraySizer::visit(MemberAccess &memacc)
372 {
373         r_declaration = 0;
374         TraversingVisitor::visit(memacc);
375         VariableDeclaration *member_declaration = 0;
376         if(r_declaration)
377                 if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(r_declaration->type_declaration))
378                 {
379                         auto i = strct->members.variables.find(memacc.member);
380                         if(i!=strct->members.variables.end())
381                                 member_declaration = i->second;
382                 }
383         r_declaration = member_declaration;
384 }
385
386 void ArraySizer::visit(Swizzle &swizzle)
387 {
388         TraversingVisitor::visit(swizzle);
389         r_declaration = 0;
390 }
391
392 void ArraySizer::visit(UnaryExpression &unary)
393 {
394         TraversingVisitor::visit(unary);
395         r_declaration = 0;
396 }
397
398 void ArraySizer::visit(BinaryExpression &binary)
399 {
400         if(binary.oper->token[0]=='[')
401                 if(const Literal *literal_index = dynamic_cast<const Literal *>(binary.right.get()))
402                         if(literal_index->value.check_type<int>())
403                         {
404                                 r_declaration = 0;
405                                 binary.left->visit(*this);
406                                 if(r_declaration)
407                                 {
408                                         max_indices[r_declaration] = literal_index->value.value<int>();
409                                         return;
410                                 }
411                         }
412
413         TraversingVisitor::visit(binary);
414 }
415
416 void ArraySizer::visit(TernaryExpression &ternary)
417 {
418         TraversingVisitor::visit(ternary);
419         r_declaration = 0;
420 }
421
422 void ArraySizer::visit(FunctionCall &call)
423 {
424         TraversingVisitor::visit(call);
425         r_declaration = 0;
426 }
427
428 void ArraySizer::visit(InterfaceLayout &layout)
429 {
430         if(layout.interface=="in")
431         {
432                 for(const Layout::Qualifier &q: layout.layout.qualifiers)
433                 {
434                         if(q.name=="points")
435                                 input_size = 1;
436                         else if(q.name=="lines")
437                                 input_size = 2;
438                         else if(q.name=="triangles")
439                                 input_size = 3;
440                         else if(q.name=="lines_adjacency")
441                                 input_size = 4;
442                         else if(q.name=="triangles_adjacency")
443                                 input_size = 6;
444                 }
445         }
446 }
447
448 void ArraySizer::visit(VariableDeclaration &var)
449 {
450         if(var.array && !var.array_size)
451                 max_indices[&var] = 0;
452 }
453
454 } // namespace SL
455 } // namespace GL
456 } // namespace Msp