1 #include <msp/core/algorithm.h>
2 #include <msp/core/hash.h>
3 #include <msp/core/raii.h>
12 void ConstantIdAssigner::apply(Module &module, const Features &features)
14 for(Stage &s: module.stages)
15 s.content.visit(*this);
17 for(VariableDeclaration *v: auto_constants)
20 auto j = existing_constants.find(v->name);
21 if(j!=existing_constants.end())
25 id = hash<32>(v->name)%features.constant_id_range;
26 while(used_ids.count(id))
27 id = (id+1)%features.constant_id_range;
30 auto i = find_member(v->layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
31 if(i!=v->layout->qualifiers.end())
35 existing_constants[v->name] = id;
39 void ConstantIdAssigner::visit(VariableDeclaration &var)
43 auto i = find_member(var.layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
44 if(i!=var.layout->qualifiers.end() && i->has_value)
47 auto_constants.push_back(&var);
50 existing_constants[var.name] = i->value;
51 used_ids.insert(i->value);
58 string InterfaceGenerator::get_out_prefix(Stage::Type type)
60 if(type==Stage::VERTEX)
62 else if(type==Stage::GEOMETRY)
68 void InterfaceGenerator::apply(Stage &s)
71 iface_target_block = &stage->content;
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);
79 void InterfaceGenerator::visit(Block &block)
81 SetForScope<Block *> set_block(current_block, &block);
82 for(auto i=block.body.begin(); i!=block.body.end(); ++i)
84 assignment_insert_point = i;
85 if(&block==&stage->content)
86 iface_insert_point = i;
92 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
94 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
95 return prefix+name.substr(offset);
98 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
100 if(stage->content.variables.count(name))
103 if(stage->type==Stage::GEOMETRY && var.interface=="out" && var.array)
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.
112 if(stage->type==Stage::GEOMETRY)
113 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
115 iface_var->array = var.array;
117 iface_var->array_size = var.array_size;
120 iface_var->layout = var.layout;
121 iface_var->linked_declaration = &var;
122 var.linked_declaration = iface_var;
125 if(var.block_declaration)
127 StructDeclaration *iface_type = var.block_declaration->clone();
128 iface_type->name = format("_%s_%s", iface, var.block_declaration->block_name);
129 iface_target_block->body.insert(iface_insert_point, iface_type);
131 iface_var->type = iface_type->name;
133 iface_var->name = format("%s %s", iface, var.block_declaration->block_name);
135 stage->interface_blocks.insert(make_pair("in "+var.block_declaration->block_name, iface_var));
137 stage->interface_blocks.insert(make_pair(name, iface_var));
140 iface_target_block->body.insert(iface_insert_point, iface_var);
141 iface_target_block->variables.insert(make_pair(name, iface_var));
142 if(iface_target_block==&stage->content && iface=="in")
143 declared_inputs.push_back(iface_var);
148 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
150 Assignment *assign = new Assignment;
152 string::size_type dot = left.find('.');
153 VariableReference *ref = new VariableReference;
154 ref->name = left.substr(0, dot);
157 while(dot!=string::npos)
159 string::size_type start = dot+1;
160 dot = left.find('.', start);
162 MemberAccess *memacc = new MemberAccess;
163 memacc->left = assign->left;
164 memacc->member = left.substr(start, dot-start);
165 assign->left = memacc;
168 assign->oper = &Operator::get_operator("=", Operator::BINARY);
169 assign->right = right;
171 ExpressionStatement *stmt = new ExpressionStatement;
172 stmt->expression = assign;
173 current_block->body.insert(assignment_insert_point, stmt);
179 void InterfaceGenerator::visit(VariableReference &var)
181 if(var.declaration || !stage->previous)
183 /* Don't pull a variable from previous stage if we just generated an output
184 interface in this stage */
185 if(stage->content.variables.count(var.name))
188 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
189 auto i = prev_vars.find(var.name);
190 if(i==prev_vars.end() || i->second->interface!="out")
191 i = prev_vars.find(in_prefix+var.name);
192 if(i!=prev_vars.end() && i->second->interface=="out")
194 if(stage->type==Stage::GEOMETRY && i->second->array)
195 stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
196 format("Can't access '%s' through automatic interface because it's an array", var.name)));
199 generate_interface(*i->second, "in", i->second->name);
200 var.name = i->second->name;
205 for(const auto &kvp: stage->previous->interface_blocks)
206 if(kvp.second->name.find(' ')!=string::npos)
208 const map<string, VariableDeclaration *> &iface_vars = kvp.second->block_declaration->members.variables;
209 i = iface_vars.find(var.name);
210 if(i!=iface_vars.end())
212 generate_interface(*kvp.second, "in", string());
218 void InterfaceGenerator::visit(VariableDeclaration &var)
220 if(var.interface=="out")
222 /* For output variables in function scope, generate a global interface
223 and replace the local declaration with an assignment. */
224 VariableDeclaration *out_var = 0;
225 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
227 out_var->source = var.source;
228 out_var->line = var.line;
229 nodes_to_remove.insert(&var);
230 if(var.init_expression)
232 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
233 stmt.source = var.source;
234 stmt.line = var.line;
239 else if(var.interface=="in" && current_block==&stage->content)
241 if(var.name.compare(0, 3, "gl_"))
242 declared_inputs.push_back(&var);
244 /* Try to link input variables in global scope with output variables from
246 if(!var.linked_declaration && stage->previous)
248 const map<string, VariableDeclaration *> *prev_vars;
250 // Blocks are linked by their block name, not instance name
251 if(var.block_declaration)
253 prev_vars = &stage->previous->interface_blocks;
254 name = "out "+var.block_declaration->block_name;
258 prev_vars = &stage->previous->content.variables;
262 auto i = prev_vars->find(name);
263 if(i!=prev_vars->end() && i->second->interface=="out")
265 var.linked_declaration = i->second;
266 i->second->linked_declaration = &var;
271 TraversingVisitor::visit(var);
274 void InterfaceGenerator::visit(FunctionDeclaration &func)
276 SetFlag set_scope(function_scope, true);
277 // Skip parameters because they're not useful here
278 func.body.visit(*this);
281 void InterfaceGenerator::visit(Passthrough &pass)
283 // Pass through all input variables declared so far.
284 vector<VariableDeclaration *> pass_vars = declared_inputs;
288 for(const auto &kvp: stage->previous->content.variables)
290 if(kvp.second->interface!="out")
293 /* Pass through output variables from the previous stage, but only
294 those which are not already linked to an input here. */
295 if(!kvp.second->linked_declaration && generate_interface(*kvp.second, "in", kvp.second->name))
296 pass_vars.push_back(kvp.second);
300 if(stage->type==Stage::GEOMETRY)
302 /* Special case for geometry shader: copy gl_Position from input to
304 VariableReference *ref = new VariableReference;
307 BinaryExpression *subscript = new BinaryExpression;
308 subscript->left = ref;
309 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
310 subscript->right = pass.subscript;
312 MemberAccess *memacc = new MemberAccess;
313 memacc->left = subscript;
314 memacc->member = "gl_Position";
316 insert_assignment("out gl_PerVertex.gl_Position", memacc);
319 for(VariableDeclaration *v: pass_vars)
321 string out_name = change_prefix(v->name, out_prefix);
322 generate_interface(*v, "out", out_name);
324 VariableReference *ref = new VariableReference;
328 BinaryExpression *subscript = new BinaryExpression;
329 subscript->left = ref;
330 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
331 subscript->right = pass.subscript;
332 insert_assignment(out_name, subscript);
335 insert_assignment(out_name, ref);
338 nodes_to_remove.insert(&pass);
342 void ArraySizer::apply(Stage &stage)
344 stage.content.visit(*this);
345 for(const auto &kvp: max_indices)
346 if(kvp.first->array && !kvp.first->array_size)
349 if(stage.type==Stage::GEOMETRY && kvp.first->interface=="in")
351 else if(kvp.second>=0)
353 else if(!kvp.first->name.compare(0, 3, "gl_"))
358 Literal *literal_size = new Literal;
359 literal_size->token = lexical_cast<string>(size);
360 literal_size->value = size;
361 kvp.first->array_size = literal_size;
366 void ArraySizer::visit(VariableReference &var)
368 r_declaration = var.declaration;
371 void ArraySizer::visit(MemberAccess &memacc)
374 TraversingVisitor::visit(memacc);
375 VariableDeclaration *member_declaration = 0;
377 if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(r_declaration->type_declaration))
379 auto i = strct->members.variables.find(memacc.member);
380 if(i!=strct->members.variables.end())
381 member_declaration = i->second;
383 r_declaration = member_declaration;
386 void ArraySizer::visit(Swizzle &swizzle)
388 TraversingVisitor::visit(swizzle);
392 void ArraySizer::visit(UnaryExpression &unary)
394 TraversingVisitor::visit(unary);
398 void ArraySizer::visit(BinaryExpression &binary)
400 if(binary.oper->token[0]=='[')
401 if(const Literal *literal_index = dynamic_cast<const Literal *>(binary.right.get()))
402 if(literal_index->value.check_type<int>())
405 binary.left->visit(*this);
408 max_indices[r_declaration] = literal_index->value.value<int>();
413 TraversingVisitor::visit(binary);
416 void ArraySizer::visit(TernaryExpression &ternary)
418 TraversingVisitor::visit(ternary);
422 void ArraySizer::visit(FunctionCall &call)
424 TraversingVisitor::visit(call);
428 void ArraySizer::visit(InterfaceLayout &layout)
430 if(layout.interface=="in")
432 for(const Layout::Qualifier &q: layout.layout.qualifiers)
436 else if(q.name=="lines")
438 else if(q.name=="triangles")
440 else if(q.name=="lines_adjacency")
442 else if(q.name=="triangles_adjacency")
448 void ArraySizer::visit(VariableDeclaration &var)
450 if(var.array && !var.array_size)
451 max_indices[&var] = 0;