1 #include <msp/core/raii.h>
11 void DeclarationCombiner::apply(Stage &stage)
13 stage.content.visit(*this);
14 NodeRemover().apply(stage, nodes_to_remove);
17 void DeclarationCombiner::visit(Block &block)
22 TraversingVisitor::visit(block);
25 void DeclarationCombiner::visit(FunctionDeclaration &func)
27 vector<FunctionDeclaration *> &decls = functions[func.name];
30 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
32 (*i)->definition = func.definition;
33 (*i)->body.body.clear();
36 decls.push_back(&func);
39 void DeclarationCombiner::visit(VariableDeclaration &var)
41 VariableDeclaration *&ptr = variables[var.name];
45 if(var.init_expression)
46 ptr->init_expression = var.init_expression;
51 for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
54 for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
57 j->has_value = i->value;
63 ptr->layout->qualifiers.push_back(*i);
67 ptr->layout = var.layout;
69 nodes_to_remove.insert(&var);
76 void BlockResolver::enter(Block &block)
78 block.parent = current_block;
81 void BlockResolver::visit(InterfaceBlock &iface)
83 iface.members.anonymous = true;
84 TraversingVisitor::visit(iface);
88 VariableResolver::VariableResolver():
91 self_referencing(false)
94 void VariableResolver::apply(Stage &stage)
96 Stage *builtin_stage = get_builtins(stage.type);
97 builtins = (builtin_stage ? &builtin_stage->content : 0);
98 stage.content.visit(*this);
101 Block *VariableResolver::next_block(Block &block)
103 return block.parent ? block.parent : &block!=builtins ? builtins : 0;
106 void VariableResolver::enter(Block &block)
108 block.variables.clear();
111 void VariableResolver::visit(VariableReference &var)
115 for(Block *block=current_block; block; block=next_block(*block))
117 map<string, VariableDeclaration *>::iterator j = block->variables.find(var.name);
118 if(j!=block->variables.end())
120 var.declaration = j->second;
121 type = j->second->type_declaration;
128 if(assignment_target)
130 record_target = false;
131 assignment_target = 0;
134 assignment_target = var.declaration;
136 else if(var.declaration && var.declaration==assignment_target)
137 self_referencing = true;
140 void VariableResolver::visit(MemberAccess &memacc)
143 TraversingVisitor::visit(memacc);
144 memacc.declaration = 0;
147 map<string, VariableDeclaration *>::iterator i = type->members.variables.find(memacc.member);
148 if(i!=type->members.variables.end())
150 memacc.declaration = i->second;
151 type = i->second->type_declaration;
158 void VariableResolver::visit(BinaryExpression &binary)
163 SetForScope<bool> set(record_target, false);
164 binary.right->visit(*this);
167 binary.left->visit(*this);
171 TraversingVisitor::visit(binary);
176 void VariableResolver::visit(Assignment &assign)
179 SetFlag set(record_target);
180 assignment_target = 0;
181 assign.left->visit(*this);
184 self_referencing = false;
185 assign.right->visit(*this);
187 assign.self_referencing = (self_referencing || assign.oper!="=");
188 assign.target_declaration = assignment_target;
191 void VariableResolver::visit(StructDeclaration &strct)
193 TraversingVisitor::visit(strct);
194 current_block->types[strct.name] = &strct;
197 void VariableResolver::visit(VariableDeclaration &var)
199 for(Block *block=current_block; block; block=next_block(*block))
201 map<string, StructDeclaration *>::iterator j = block->types.find(var.type);
202 if(j!=block->types.end())
203 var.type_declaration = j->second;
206 if(!block_interface.empty() && var.interface.empty())
207 var.interface = block_interface;
209 TraversingVisitor::visit(var);
210 current_block->variables[var.name] = &var;
211 if(current_block->anonymous && current_block->parent)
212 current_block->parent->variables[var.name] = &var;
215 void VariableResolver::visit(InterfaceBlock &iface)
217 SetForScope<string> set_iface(block_interface, iface.interface);
218 TraversingVisitor::visit(iface);
222 void FunctionResolver::visit(FunctionCall &call)
224 map<string, vector<FunctionDeclaration *> >::iterator i = functions.find(call.name);
225 if(i!=functions.end())
226 call.declaration = i->second.back();
228 TraversingVisitor::visit(call);
231 void FunctionResolver::visit(FunctionDeclaration &func)
233 vector<FunctionDeclaration *> &decls = functions[func.name];
236 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
237 (*i)->definition = func.definition;
239 decls.push_back(&func);
241 else if(!decls.empty() && decls.back()->definition)
242 func.definition = decls.back()->definition;
244 decls.push_back(&func);
246 TraversingVisitor::visit(func);
250 InterfaceGenerator::InterfaceGenerator():
254 string InterfaceGenerator::get_out_prefix(Stage::Type type)
256 if(type==Stage::VERTEX)
258 else if(type==Stage::GEOMETRY)
264 void InterfaceGenerator::apply(Stage &s)
268 in_prefix = get_out_prefix(stage->previous->type);
269 out_prefix = get_out_prefix(stage->type);
270 s.content.visit(*this);
271 NodeRemover().apply(s, nodes_to_remove);
274 void InterfaceGenerator::visit(Block &block)
276 SetForScope<Block *> set_block(current_block, &block);
277 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
279 assignment_insert_point = i;
280 if(&block==&stage->content)
281 iface_insert_point = i;
287 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
289 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
290 return prefix+name.substr(offset);
293 bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
295 const map<string, VariableDeclaration *> &stage_vars = (iface=="in" ? stage->in_variables : stage->out_variables);
296 if(stage_vars.count(name))
299 VariableDeclaration* iface_var = new VariableDeclaration;
300 iface_var->sampling = var.sampling;
301 iface_var->interface = iface;
302 iface_var->type = var.type;
303 iface_var->type_declaration = var.type_declaration;
304 iface_var->name = name;
305 if(stage->type==Stage::GEOMETRY)
306 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
308 iface_var->array = var.array;
310 iface_var->array_size = var.array_size;
312 iface_var->linked_declaration = &var;
313 stage->content.body.insert(iface_insert_point, iface_var);
315 SetForScope<Block *> set_block(current_block, &stage->content);
316 iface_var->visit(*this);
322 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
324 Assignment *assign = new Assignment;
325 VariableReference *ref = new VariableReference;
329 assign->right = right;
331 ExpressionStatement *stmt = new ExpressionStatement;
332 stmt->expression = assign;
333 current_block->body.insert(assignment_insert_point, stmt);
339 void InterfaceGenerator::visit(VariableReference &var)
341 if(var.declaration || !stage->previous)
343 /* Don't pull a variable from previous stage if we just generated an out
344 interface in this stage */
345 if(stage->out_variables.count(var.name))
348 const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
349 map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
350 if(i==prev_out.end())
351 i = prev_out.find(in_prefix+var.name);
352 if(i!=prev_out.end())
354 generate_interface(*i->second, "in", i->second->name);
355 var.name = i->second->name;
359 void InterfaceGenerator::visit(VariableDeclaration &var)
361 if(var.interface=="out")
363 if(current_block==&stage->content)
364 stage->out_variables[var.name] = &var;
365 else if(generate_interface(var, "out", change_prefix(var.name, string())))
367 nodes_to_remove.insert(&var);
368 if(var.init_expression)
370 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
371 stmt.source = var.source;
372 stmt.line = var.line;
377 else if(var.interface=="in")
379 stage->in_variables[var.name] = &var;
380 if(var.linked_declaration)
381 var.linked_declaration->linked_declaration = &var;
382 else if(stage->previous)
384 const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
385 map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
386 if(i!=prev_out.end())
388 var.linked_declaration = i->second;
389 i->second->linked_declaration = &var;
394 TraversingVisitor::visit(var);
397 void InterfaceGenerator::visit(FunctionDeclaration &func)
399 // Skip parameters because they're not useful here
400 func.body.visit(*this);
403 void InterfaceGenerator::visit(Passthrough &pass)
405 vector<VariableDeclaration *> pass_vars;
407 for(map<string, VariableDeclaration *>::const_iterator i=stage->in_variables.begin(); i!=stage->in_variables.end(); ++i)
408 pass_vars.push_back(i->second);
412 const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
413 for(map<string, VariableDeclaration *>::const_iterator i=prev_out.begin(); i!=prev_out.end(); ++i)
416 for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
417 linked = ((*j)->linked_declaration==i->second);
419 if(!linked && generate_interface(*i->second, "in", i->second->name))
420 pass_vars.push_back(i->second);
424 if(stage->type==Stage::GEOMETRY)
426 VariableReference *ref = new VariableReference;
429 BinaryExpression *subscript = new BinaryExpression;
430 subscript->left = ref;
431 subscript->oper = "[";
432 subscript->right = pass.subscript;
433 subscript->after = "]";
435 MemberAccess *memacc = new MemberAccess;
436 memacc->left = subscript;
437 memacc->member = "gl_Position";
439 insert_assignment("gl_Position", memacc);
442 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
444 string out_name = change_prefix((*i)->name, out_prefix);
445 generate_interface(**i, "out", out_name);
447 VariableReference *ref = new VariableReference;
448 ref->name = (*i)->name;
451 BinaryExpression *subscript = new BinaryExpression;
452 subscript->left = ref;
453 subscript->oper = "[";
454 subscript->right = pass.subscript;
455 subscript->after = "]";
456 insert_assignment(out_name, subscript);
459 insert_assignment(out_name, ref);
462 nodes_to_remove.insert(&pass);
466 DeclarationReorderer::DeclarationReorderer():
470 void DeclarationReorderer::visit(FunctionCall &call)
472 FunctionDeclaration *def = call.declaration;
474 def = def->definition;
475 if(def && !ordered_funcs.count(def))
476 needed_funcs.insert(def);
479 void DeclarationReorderer::visit(Block &block)
482 return TraversingVisitor::visit(block);
484 NodeList<Statement>::iterator struct_insert_point = block.body.end();
485 NodeList<Statement>::iterator variable_insert_point = block.body.end();
486 NodeList<Statement>::iterator function_insert_point = block.body.end();
487 unsigned unordered_func_count = 0;
488 bool ordered_any_funcs = false;
490 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
492 kind = NO_DECLARATION;
496 if(kind==STRUCT && struct_insert_point!=block.body.end())
498 block.body.insert(struct_insert_point, *i);
501 else if(kind>STRUCT && struct_insert_point==block.body.end())
502 struct_insert_point = i;
504 if(kind==VARIABLE && variable_insert_point!=block.body.end())
506 block.body.insert(variable_insert_point, *i);
509 else if(kind>VARIABLE && variable_insert_point==block.body.end())
510 variable_insert_point = i;
514 if(function_insert_point==block.body.end())
515 function_insert_point = i;
517 if(needed_funcs.empty())
519 ordered_funcs.insert(i->get());
520 if(i!=function_insert_point)
522 block.body.insert(function_insert_point, *i);
526 ++function_insert_point;
527 ordered_any_funcs = true;
530 ++unordered_func_count;
535 if(function_insert_point==i)
536 ++function_insert_point;
537 block.body.erase(i++);
542 if(i==block.body.end() && unordered_func_count)
544 if(!ordered_any_funcs)
545 // A subset of the remaining functions forms a recursive loop
546 /* TODO pick a function and move it up, adding any necessary
550 i = function_insert_point;
551 unordered_func_count = 0;
556 void DeclarationReorderer::visit(VariableDeclaration &var)
558 TraversingVisitor::visit(var);
562 void DeclarationReorderer::visit(FunctionDeclaration &func)
564 needed_funcs.clear();
565 func.body.visit(*this);
566 needed_funcs.erase(&func);