]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.cpp
Set color write mask if no blending is used
[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 && !copy_block && 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.  But if we're bringing in an
112         entire block, the array is on the block and not individual variables. */
113         if(stage->type==Stage::GEOMETRY && !copy_block)
114                 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
115         else
116                 iface_var->array = var.array;
117         if(iface_var->array)
118                 iface_var->array_size = var.array_size;
119         if(iface=="in")
120         {
121                 iface_var->layout = var.layout;
122                 iface_var->linked_declaration = &var;
123                 var.linked_declaration = iface_var;
124         }
125
126         iface_target_block->body.insert(iface_insert_point, iface_var);
127         iface_target_block->variables.insert(make_pair(name, iface_var));
128         if(iface_target_block==&stage->content && iface=="in")
129                 declared_inputs.push_back(iface_var);
130
131         return iface_var;
132 }
133
134 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
135 {
136         if(stage->interface_blocks.count("in "+out_block.block_name))
137                 return 0;
138
139         InterfaceBlock *in_block = new InterfaceBlock;
140         in_block->interface = "in";
141         in_block->block_name = out_block.block_name;
142         in_block->members = new Block;
143         in_block->instance_name = out_block.instance_name;
144         if(stage->type==Stage::GEOMETRY)
145                 in_block->array = true;
146         else
147                 in_block->array = out_block.array;
148         in_block->linked_block = &out_block;
149         out_block.linked_block = in_block;
150
151         {
152                 SetFlag set_copy(copy_block, true);
153                 SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
154                 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
155                 if(out_block.struct_declaration)
156                         out_block.struct_declaration->members.visit(*this);
157                 else if(out_block.members)
158                         out_block.members->visit(*this);
159         }
160
161         iface_target_block->body.insert(iface_insert_point, in_block);
162         stage->interface_blocks.insert(make_pair("in "+in_block->block_name, in_block));
163         if(!in_block->instance_name.empty())
164                 stage->interface_blocks.insert(make_pair(in_block->instance_name, in_block));
165
166         SetFlag set_scope(function_scope, false);
167         SetForScope<Block *> set_block(current_block, &stage->content);
168         in_block->visit(*this);
169
170         return in_block;
171 }
172
173 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
174 {
175         Assignment *assign = new Assignment;
176         VariableReference *ref = new VariableReference;
177         ref->name = left;
178         assign->left = ref;
179         assign->oper = &Operator::get_operator("=", Operator::BINARY);
180         assign->right = right;
181
182         ExpressionStatement *stmt = new ExpressionStatement;
183         stmt->expression = assign;
184         current_block->body.insert(assignment_insert_point, stmt);
185         stmt->visit(*this);
186
187         return *stmt;
188 }
189
190 void InterfaceGenerator::visit(VariableReference &var)
191 {
192         if(var.declaration || !stage->previous)
193                 return;
194         /* Don't pull a variable from previous stage if we just generated an output
195         interface in this stage */
196         if(stage->content.variables.count(var.name))
197                 return;
198
199         const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
200         auto i = prev_vars.find(var.name);
201         if(i==prev_vars.end() || i->second->interface!="out")
202                 i = prev_vars.find(in_prefix+var.name);
203         if(i!=prev_vars.end() && i->second->interface=="out")
204         {
205                 if(stage->type==Stage::GEOMETRY && i->second->array)
206                         stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
207                                 format("Can't access '%s' through automatic interface because it's an array", var.name)));
208                 else
209                 {
210                         generate_interface(*i->second, "in", i->second->name);
211                         var.name = i->second->name;
212                 }
213                 return;
214         }
215
216         const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
217         auto j = prev_blocks.find(var.name);
218         if(j!=prev_blocks.end() && j->second->interface=="out")
219         {
220                 generate_interface(*j->second);
221                 /* Let VariableResolver convert the variable reference into an interface
222                 block reference. */
223                 return;
224         }
225
226         for(const auto &kvp: prev_blocks)
227                 if(kvp.second->instance_name.empty() && kvp.second->struct_declaration)
228                 {
229                         const map<string, VariableDeclaration *> &iface_vars = kvp.second->struct_declaration->members.variables;
230                         i = iface_vars.find(var.name);
231                         if(i!=iface_vars.end())
232                         {
233                                 generate_interface(*kvp.second);
234                                 return;
235                         }
236                 }
237 }
238
239 void InterfaceGenerator::visit(VariableDeclaration &var)
240 {
241         if(copy_block)
242                 generate_interface(var, "in", var.name);
243         else if(var.interface=="out")
244         {
245                 /* For output variables in function scope, generate a global interface
246                 and replace the local declaration with an assignment. */
247                 VariableDeclaration *out_var = 0;
248                 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
249                 {
250                         out_var->source = var.source;
251                         out_var->line = var.line;
252                         nodes_to_remove.insert(&var);
253                         if(var.init_expression)
254                         {
255                                 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
256                                 stmt.source = var.source;
257                                 stmt.line = var.line;
258                                 return;
259                         }
260                 }
261         }
262         else if(var.interface=="in" && current_block==&stage->content)
263         {
264                 if(var.name.compare(0, 3, "gl_"))
265                         declared_inputs.push_back(&var);
266
267                 /* Try to link input variables in global scope with output variables from
268                 previous stage. */
269                 if(!var.linked_declaration && stage->previous)
270                 {
271                         const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
272                         auto i = prev_vars.find(var.name);
273                         if(i!=prev_vars.end() && i->second->interface=="out")
274                         {
275                                 var.linked_declaration = i->second;
276                                 i->second->linked_declaration = &var;
277                         }
278                 }
279         }
280
281         TraversingVisitor::visit(var);
282 }
283
284 void InterfaceGenerator::visit(InterfaceBlock &iface)
285 {
286         if(iface.interface=="in")
287         {
288                 /* Try to link input blocks with output blocks sharing the same block
289                 name from previous stage. */
290                 if(!iface.linked_block && stage->previous)
291                 {
292                         const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
293                         auto i = prev_blocks.find("out "+iface.block_name);
294                         if(i!=prev_blocks.end())
295                         {
296                                 iface.linked_block = i->second;
297                                 i->second->linked_block = &iface;
298                         }
299                 }
300         }
301
302         TraversingVisitor::visit(iface);
303 }
304
305 void InterfaceGenerator::visit(FunctionDeclaration &func)
306 {
307         SetFlag set_scope(function_scope, true);
308         // Skip parameters because they're not useful here
309         func.body.visit(*this);
310 }
311
312 void InterfaceGenerator::visit(Passthrough &pass)
313 {
314         // Pass through all input variables declared so far.
315         vector<VariableDeclaration *> pass_vars = declared_inputs;
316
317         if(stage->previous)
318         {
319                 for(const auto &kvp: stage->previous->content.variables)
320                 {
321                         if(kvp.second->interface!="out")
322                                 continue;
323
324                         /* Pass through output variables from the previous stage, but only
325                         those which are not already linked to an input here. */
326                         if(!kvp.second->linked_declaration && generate_interface(*kvp.second, "in", kvp.second->name))
327                                 pass_vars.push_back(kvp.second);
328                 }
329         }
330
331         if(stage->type==Stage::GEOMETRY)
332         {
333                 /* Special case for geometry shader: copy gl_Position from input to
334                 output. */
335                 InterfaceBlockReference *ref = new InterfaceBlockReference;
336                 ref->name = "gl_in";
337
338                 BinaryExpression *subscript = new BinaryExpression;
339                 subscript->left = ref;
340                 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
341                 subscript->right = pass.subscript;
342
343                 MemberAccess *memacc = new MemberAccess;
344                 memacc->left = subscript;
345                 memacc->member = "gl_Position";
346
347                 insert_assignment("gl_Position", memacc);
348         }
349
350         for(VariableDeclaration *v: pass_vars)
351         {
352                 string out_name = change_prefix(v->name, out_prefix);
353                 generate_interface(*v, "out", out_name);
354
355                 VariableReference *ref = new VariableReference;
356                 ref->name = v->name;
357                 if(pass.subscript)
358                 {
359                         BinaryExpression *subscript = new BinaryExpression;
360                         subscript->left = ref;
361                         subscript->oper = &Operator::get_operator("[", Operator::BINARY);
362                         subscript->right = pass.subscript;
363                         insert_assignment(out_name, subscript);
364                 }
365                 else
366                         insert_assignment(out_name, ref);
367         }
368
369         nodes_to_remove.insert(&pass);
370 }
371
372 } // namespace SL
373 } // namespace GL
374 } // namespace Msp