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