1 #include <msp/core/hash.h>
2 #include <msp/core/raii.h>
11 void ConstantIdAssigner::apply(Module &module, const Features &features)
13 for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
14 i->content.visit(*this);
16 for(vector<VariableDeclaration *>::iterator i=auto_constants.begin(); i!=auto_constants.end(); ++i)
18 unsigned id = hash32((*i)->name)%features.constant_id_range;
19 while(used_ids.count(id))
20 id = (id+1)%features.constant_id_range;
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")
34 void ConstantIdAssigner::visit(VariableDeclaration &var)
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)
43 auto_constants.push_back(&var);
45 used_ids.insert(i->value);
52 InterfaceGenerator::InterfaceGenerator():
54 function_scope(false),
59 string InterfaceGenerator::get_out_prefix(Stage::Type type)
61 if(type==Stage::VERTEX)
63 else if(type==Stage::GEOMETRY)
69 void InterfaceGenerator::apply(Stage &s)
72 iface_target_block = &stage->content;
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);
80 void InterfaceGenerator::visit(Block &block)
82 SetForScope<Block *> set_block(current_block, &block);
83 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
85 assignment_insert_point = i;
86 if(&block==&stage->content)
87 iface_insert_point = i;
93 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
95 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
96 return prefix+name.substr(offset);
99 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
101 if(stage->content.variables.count(name))
104 if(stage->type==Stage::GEOMETRY && !copy_block && var.interface=="out" && var.array)
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");
117 iface_var->array = var.array;
119 iface_var->array_size = var.array_size;
122 iface_var->layout = var.layout;
123 iface_var->linked_declaration = &var;
124 var.linked_declaration = iface_var;
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);
135 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
137 if(stage->interface_blocks.count("in "+out_block.block_name))
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;
148 in_block->array = out_block.array;
149 in_block->linked_block = &out_block;
150 out_block.linked_block = in_block;
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);
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));
167 SetFlag set_scope(function_scope, false);
168 SetForScope<Block *> set_block(current_block, &stage->content);
169 in_block->visit(*this);
174 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
176 Assignment *assign = new Assignment;
177 VariableReference *ref = new VariableReference;
180 assign->oper = &Operator::get_operator("=", Operator::BINARY);
181 assign->right = right;
183 ExpressionStatement *stmt = new ExpressionStatement;
184 stmt->expression = assign;
185 current_block->body.insert(assignment_insert_point, stmt);
191 void InterfaceGenerator::visit(VariableReference &var)
193 if(var.declaration || !stage->previous)
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))
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")
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)));
211 generate_interface(*i->second, "in", i->second->name);
212 var.name = i->second->name;
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")
221 generate_interface(*j->second);
222 /* Let VariableResolver convert the variable reference into an interface
227 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
228 if(j->second->instance_name.empty() && j->second->struct_declaration)
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())
234 generate_interface(*j->second);
240 void InterfaceGenerator::visit(VariableDeclaration &var)
243 generate_interface(var, "in", var.name);
244 else if(var.interface=="out")
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)))
251 out_var->source = var.source;
252 out_var->line = var.line;
253 nodes_to_remove.insert(&var);
254 if(var.init_expression)
256 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
257 stmt.source = var.source;
258 stmt.line = var.line;
263 else if(var.interface=="in" && current_block==&stage->content)
265 if(var.name.compare(0, 3, "gl_"))
266 declared_inputs.push_back(&var);
268 /* Try to link input variables in global scope with output variables from
270 if(!var.linked_declaration && stage->previous)
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")
276 var.linked_declaration = i->second;
277 i->second->linked_declaration = &var;
282 TraversingVisitor::visit(var);
285 void InterfaceGenerator::visit(InterfaceBlock &iface)
287 if(iface.interface=="in")
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)
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())
297 iface.linked_block = i->second;
298 i->second->linked_block = &iface;
303 TraversingVisitor::visit(iface);
306 void InterfaceGenerator::visit(FunctionDeclaration &func)
308 SetFlag set_scope(function_scope, true);
309 // Skip parameters because they're not useful here
310 func.body.visit(*this);
313 void InterfaceGenerator::visit(Passthrough &pass)
315 // Pass through all input variables declared so far.
316 vector<VariableDeclaration *> pass_vars = declared_inputs;
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)
323 if(i->second->interface!="out")
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);
333 if(stage->type==Stage::GEOMETRY)
335 /* Special case for geometry shader: copy gl_Position from input to
337 InterfaceBlockReference *ref = new InterfaceBlockReference;
340 BinaryExpression *subscript = new BinaryExpression;
341 subscript->left = ref;
342 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
343 subscript->right = pass.subscript;
345 MemberAccess *memacc = new MemberAccess;
346 memacc->left = subscript;
347 memacc->member = "gl_Position";
349 insert_assignment("gl_Position", memacc);
352 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
354 string out_name = change_prefix((*i)->name, out_prefix);
355 generate_interface(**i, "out", out_name);
357 VariableReference *ref = new VariableReference;
358 ref->name = (*i)->name;
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);
368 insert_assignment(out_name, ref);
371 nodes_to_remove.insert(&pass);