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 unsigned max_id = features.max_constant_id;
17 for(vector<VariableDeclaration *>::iterator i=auto_constants.begin(); i!=auto_constants.end(); ++i)
19 unsigned id = hash32((*i)->name)%(max_id+1);
20 while(used_ids.count(id))
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")
35 void ConstantIdAssigner::visit(VariableDeclaration &var)
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)
44 auto_constants.push_back(&var);
46 used_ids.insert(i->value);
53 InterfaceGenerator::InterfaceGenerator():
55 function_scope(false),
60 string InterfaceGenerator::get_out_prefix(Stage::Type type)
62 if(type==Stage::VERTEX)
64 else if(type==Stage::GEOMETRY)
70 void InterfaceGenerator::apply(Stage &s)
73 iface_target_block = &stage->content;
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);
81 void InterfaceGenerator::visit(Block &block)
83 SetForScope<Block *> set_block(current_block, &block);
84 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
86 assignment_insert_point = i;
87 if(&block==&stage->content)
88 iface_insert_point = i;
94 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
96 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
97 return prefix+name.substr(offset);
100 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
102 if(stage->content.variables.count(name))
105 if(stage->type==Stage::GEOMETRY && !copy_block && var.interface=="out" && var.array)
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");
118 iface_var->array = var.array;
120 iface_var->array_size = var.array_size;
123 iface_var->layout = var.layout;
124 iface_var->linked_declaration = &var;
125 var.linked_declaration = iface_var;
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);
136 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
138 if(stage->interface_blocks.count("in"+out_block.block_name))
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;
149 in_block->array = out_block.array;
150 in_block->linked_block = &out_block;
151 out_block.linked_block = in_block;
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);
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));
168 SetFlag set_scope(function_scope, false);
169 SetForScope<Block *> set_block(current_block, &stage->content);
170 in_block->visit(*this);
175 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
177 Assignment *assign = new Assignment;
178 VariableReference *ref = new VariableReference;
181 assign->oper = &Operator::get_operator("=", Operator::BINARY);
182 assign->right = right;
184 ExpressionStatement *stmt = new ExpressionStatement;
185 stmt->expression = assign;
186 current_block->body.insert(assignment_insert_point, stmt);
192 void InterfaceGenerator::visit(VariableReference &var)
194 if(var.declaration || !stage->previous)
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))
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")
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)));
212 generate_interface(*i->second, "in", i->second->name);
213 var.name = i->second->name;
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")
222 generate_interface(*j->second);
223 /* Let VariableResolver convert the variable reference into an interface
228 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
229 if(j->second->instance_name.empty() && j->second->struct_declaration)
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())
235 generate_interface(*j->second);
241 void InterfaceGenerator::visit(VariableDeclaration &var)
244 generate_interface(var, "in", var.name);
245 else if(var.interface=="out")
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)))
252 out_var->source = var.source;
253 out_var->line = var.line;
254 nodes_to_remove.insert(&var);
255 if(var.init_expression)
257 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
258 stmt.source = var.source;
259 stmt.line = var.line;
264 else if(var.interface=="in" && current_block==&stage->content)
266 if(var.name.compare(0, 3, "gl_"))
267 declared_inputs.push_back(&var);
269 /* Try to link input variables in global scope with output variables from
271 if(!var.linked_declaration && stage->previous)
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")
277 var.linked_declaration = i->second;
278 i->second->linked_declaration = &var;
283 TraversingVisitor::visit(var);
286 void InterfaceGenerator::visit(InterfaceBlock &iface)
288 if(iface.interface=="in")
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)
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())
298 iface.linked_block = i->second;
299 i->second->linked_block = &iface;
304 TraversingVisitor::visit(iface);
307 void InterfaceGenerator::visit(FunctionDeclaration &func)
309 SetFlag set_scope(function_scope, true);
310 // Skip parameters because they're not useful here
311 func.body.visit(*this);
314 void InterfaceGenerator::visit(Passthrough &pass)
316 // Pass through all input variables declared so far.
317 vector<VariableDeclaration *> pass_vars = declared_inputs;
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)
324 if(i->second->interface!="out")
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);
334 if(stage->type==Stage::GEOMETRY)
336 /* Special case for geometry shader: copy gl_Position from input to
338 InterfaceBlockReference *ref = new InterfaceBlockReference;
341 BinaryExpression *subscript = new BinaryExpression;
342 subscript->left = ref;
343 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
344 subscript->right = pass.subscript;
346 MemberAccess *memacc = new MemberAccess;
347 memacc->left = subscript;
348 memacc->member = "gl_Position";
350 insert_assignment("gl_Position", memacc);
353 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
355 string out_name = change_prefix((*i)->name, out_prefix);
356 generate_interface(**i, "out", out_name);
358 VariableReference *ref = new VariableReference;
359 ref->name = (*i)->name;
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);
369 insert_assignment(out_name, ref);
372 nodes_to_remove.insert(&pass);