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