]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.cpp
Fix a name conflict in certain inlining scenarios
[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 = hash32(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         }
36 }
37
38 void ConstantIdAssigner::visit(VariableDeclaration &var)
39 {
40         if(var.layout)
41         {
42                 auto i = find_member(var.layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
43                 if(i!=var.layout->qualifiers.end() && i->has_value)
44                 {
45                         if(i->value==-1)
46                                 auto_constants.push_back(&var);
47                         else
48                         {
49                                 existing_constants[var.name] = i->value;
50                                 used_ids.insert(i->value);
51                         }
52                 }
53         }
54 }
55
56
57 string InterfaceGenerator::get_out_prefix(Stage::Type type)
58 {
59         if(type==Stage::VERTEX)
60                 return "_vs_out_";
61         else if(type==Stage::GEOMETRY)
62                 return "_gs_out_";
63         else
64                 return string();
65 }
66
67 void InterfaceGenerator::apply(Stage &s)
68 {
69         stage = &s;
70         iface_target_block = &stage->content;
71         if(stage->previous)
72                 in_prefix = get_out_prefix(stage->previous->type);
73         out_prefix = get_out_prefix(stage->type);
74         s.content.visit(*this);
75         NodeRemover().apply(s, nodes_to_remove);
76 }
77
78 void InterfaceGenerator::visit(Block &block)
79 {
80         SetForScope<Block *> set_block(current_block, &block);
81         for(auto i=block.body.begin(); i!=block.body.end(); ++i)
82         {
83                 assignment_insert_point = i;
84                 if(&block==&stage->content)
85                         iface_insert_point = i;
86
87                 (*i)->visit(*this);
88         }
89 }
90
91 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
92 {
93         unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
94         return prefix+name.substr(offset);
95 }
96
97 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
98 {
99         if(stage->content.variables.count(name))
100                 return 0;
101
102         if(stage->type==Stage::GEOMETRY && !copy_block && var.interface=="out" && var.array)
103                 return 0;
104
105         VariableDeclaration* iface_var = new VariableDeclaration;
106         iface_var->sampling = var.sampling;
107         iface_var->interface = iface;
108         iface_var->type = var.type;
109         iface_var->name = name;
110         /* Geometry shader inputs are always arrays.  But if we're bringing in an
111         entire block, the array is on the block and not individual variables. */
112         if(stage->type==Stage::GEOMETRY && !copy_block)
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         iface_target_block->body.insert(iface_insert_point, iface_var);
126         iface_target_block->variables.insert(make_pair(name, iface_var));
127         if(iface_target_block==&stage->content && iface=="in")
128                 declared_inputs.push_back(iface_var);
129
130         return iface_var;
131 }
132
133 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
134 {
135         if(stage->interface_blocks.count("in "+out_block.block_name))
136                 return 0;
137
138         InterfaceBlock *in_block = new InterfaceBlock;
139         in_block->interface = "in";
140         in_block->block_name = out_block.block_name;
141         in_block->members = new Block;
142         in_block->instance_name = out_block.instance_name;
143         if(stage->type==Stage::GEOMETRY)
144                 in_block->array = true;
145         else
146                 in_block->array = out_block.array;
147         in_block->linked_block = &out_block;
148         out_block.linked_block = in_block;
149
150         {
151                 SetFlag set_copy(copy_block, true);
152                 SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
153                 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
154                 if(out_block.struct_declaration)
155                         out_block.struct_declaration->members.visit(*this);
156                 else if(out_block.members)
157                         out_block.members->visit(*this);
158         }
159
160         iface_target_block->body.insert(iface_insert_point, in_block);
161         stage->interface_blocks.insert(make_pair("in "+in_block->block_name, in_block));
162         if(!in_block->instance_name.empty())
163                 stage->interface_blocks.insert(make_pair(in_block->instance_name, in_block));
164
165         SetFlag set_scope(function_scope, false);
166         SetForScope<Block *> set_block(current_block, &stage->content);
167         in_block->visit(*this);
168
169         return in_block;
170 }
171
172 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
173 {
174         Assignment *assign = new Assignment;
175         VariableReference *ref = new VariableReference;
176         ref->name = left;
177         assign->left = ref;
178         assign->oper = &Operator::get_operator("=", Operator::BINARY);
179         assign->right = right;
180
181         ExpressionStatement *stmt = new ExpressionStatement;
182         stmt->expression = assign;
183         current_block->body.insert(assignment_insert_point, stmt);
184         stmt->visit(*this);
185
186         return *stmt;
187 }
188
189 void InterfaceGenerator::visit(VariableReference &var)
190 {
191         if(var.declaration || !stage->previous)
192                 return;
193         /* Don't pull a variable from previous stage if we just generated an output
194         interface in this stage */
195         if(stage->content.variables.count(var.name))
196                 return;
197
198         const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
199         auto i = prev_vars.find(var.name);
200         if(i==prev_vars.end() || i->second->interface!="out")
201                 i = prev_vars.find(in_prefix+var.name);
202         if(i!=prev_vars.end() && i->second->interface=="out")
203         {
204                 if(stage->type==Stage::GEOMETRY && i->second->array)
205                         stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
206                                 format("Can't access '%s' through automatic interface because it's an array", var.name)));
207                 else
208                 {
209                         generate_interface(*i->second, "in", i->second->name);
210                         var.name = i->second->name;
211                 }
212                 return;
213         }
214
215         const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
216         auto j = prev_blocks.find(var.name);
217         if(j!=prev_blocks.end() && j->second->interface=="out")
218         {
219                 generate_interface(*j->second);
220                 /* Let VariableResolver convert the variable reference into an interface
221                 block reference. */
222                 return;
223         }
224
225         for(const auto &kvp: prev_blocks)
226                 if(kvp.second->instance_name.empty() && kvp.second->struct_declaration)
227                 {
228                         const map<string, VariableDeclaration *> &iface_vars = kvp.second->struct_declaration->members.variables;
229                         i = iface_vars.find(var.name);
230                         if(i!=iface_vars.end())
231                         {
232                                 generate_interface(*kvp.second);
233                                 return;
234                         }
235                 }
236 }
237
238 void InterfaceGenerator::visit(VariableDeclaration &var)
239 {
240         if(copy_block)
241                 generate_interface(var, "in", var.name);
242         else if(var.interface=="out")
243         {
244                 /* For output variables in function scope, generate a global interface
245                 and replace the local declaration with an assignment. */
246                 VariableDeclaration *out_var = 0;
247                 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
248                 {
249                         out_var->source = var.source;
250                         out_var->line = var.line;
251                         nodes_to_remove.insert(&var);
252                         if(var.init_expression)
253                         {
254                                 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
255                                 stmt.source = var.source;
256                                 stmt.line = var.line;
257                                 return;
258                         }
259                 }
260         }
261         else if(var.interface=="in" && current_block==&stage->content)
262         {
263                 if(var.name.compare(0, 3, "gl_"))
264                         declared_inputs.push_back(&var);
265
266                 /* Try to link input variables in global scope with output variables from
267                 previous stage. */
268                 if(!var.linked_declaration && stage->previous)
269                 {
270                         const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
271                         auto i = prev_vars.find(var.name);
272                         if(i!=prev_vars.end() && i->second->interface=="out")
273                         {
274                                 var.linked_declaration = i->second;
275                                 i->second->linked_declaration = &var;
276                         }
277                 }
278         }
279
280         TraversingVisitor::visit(var);
281 }
282
283 void InterfaceGenerator::visit(InterfaceBlock &iface)
284 {
285         if(iface.interface=="in")
286         {
287                 /* Try to link input blocks with output blocks sharing the same block
288                 name from previous stage. */
289                 if(!iface.linked_block && stage->previous)
290                 {
291                         const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
292                         auto i = prev_blocks.find("out "+iface.block_name);
293                         if(i!=prev_blocks.end())
294                         {
295                                 iface.linked_block = i->second;
296                                 i->second->linked_block = &iface;
297                         }
298                 }
299         }
300
301         TraversingVisitor::visit(iface);
302 }
303
304 void InterfaceGenerator::visit(FunctionDeclaration &func)
305 {
306         SetFlag set_scope(function_scope, true);
307         // Skip parameters because they're not useful here
308         func.body.visit(*this);
309 }
310
311 void InterfaceGenerator::visit(Passthrough &pass)
312 {
313         // Pass through all input variables declared so far.
314         vector<VariableDeclaration *> pass_vars = declared_inputs;
315
316         if(stage->previous)
317         {
318                 for(const auto &kvp: stage->previous->content.variables)
319                 {
320                         if(kvp.second->interface!="out")
321                                 continue;
322
323                         /* Pass through output variables from the previous stage, but only
324                         those which are not already linked to an input here. */
325                         if(!kvp.second->linked_declaration && generate_interface(*kvp.second, "in", kvp.second->name))
326                                 pass_vars.push_back(kvp.second);
327                 }
328         }
329
330         if(stage->type==Stage::GEOMETRY)
331         {
332                 /* Special case for geometry shader: copy gl_Position from input to
333                 output. */
334                 InterfaceBlockReference *ref = new InterfaceBlockReference;
335                 ref->name = "gl_in";
336
337                 BinaryExpression *subscript = new BinaryExpression;
338                 subscript->left = ref;
339                 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
340                 subscript->right = pass.subscript;
341
342                 MemberAccess *memacc = new MemberAccess;
343                 memacc->left = subscript;
344                 memacc->member = "gl_Position";
345
346                 insert_assignment("gl_Position", memacc);
347         }
348
349         for(VariableDeclaration *v: pass_vars)
350         {
351                 string out_name = change_prefix(v->name, out_prefix);
352                 generate_interface(*v, "out", out_name);
353
354                 VariableReference *ref = new VariableReference;
355                 ref->name = v->name;
356                 if(pass.subscript)
357                 {
358                         BinaryExpression *subscript = new BinaryExpression;
359                         subscript->left = ref;
360                         subscript->oper = &Operator::get_operator("[", Operator::BINARY);
361                         subscript->right = pass.subscript;
362                         insert_assignment(out_name, subscript);
363                 }
364                 else
365                         insert_assignment(out_name, ref);
366         }
367
368         nodes_to_remove.insert(&pass);
369 }
370
371 } // namespace SL
372 } // namespace GL
373 } // namespace Msp