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