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)
19 unsigned id = hash32(v->name)%features.constant_id_range;
20 while(used_ids.count(id))
21 id = (id+1)%features.constant_id_range;
23 auto i = find_member(v->layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
24 if(i!=v->layout->qualifiers.end())
31 void ConstantIdAssigner::visit(VariableDeclaration &var)
35 auto i = find_member(var.layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
36 if(i!=var.layout->qualifiers.end() && i->has_value)
39 auto_constants.push_back(&var);
41 used_ids.insert(i->value);
47 string InterfaceGenerator::get_out_prefix(Stage::Type type)
49 if(type==Stage::VERTEX)
51 else if(type==Stage::GEOMETRY)
57 void InterfaceGenerator::apply(Stage &s)
60 iface_target_block = &stage->content;
62 in_prefix = get_out_prefix(stage->previous->type);
63 out_prefix = get_out_prefix(stage->type);
64 s.content.visit(*this);
65 NodeRemover().apply(s, nodes_to_remove);
68 void InterfaceGenerator::visit(Block &block)
70 SetForScope<Block *> set_block(current_block, &block);
71 for(auto i=block.body.begin(); i!=block.body.end(); ++i)
73 assignment_insert_point = i;
74 if(&block==&stage->content)
75 iface_insert_point = i;
81 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
83 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
84 return prefix+name.substr(offset);
87 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
89 if(stage->content.variables.count(name))
92 if(stage->type==Stage::GEOMETRY && !copy_block && var.interface=="out" && var.array)
95 VariableDeclaration* iface_var = new VariableDeclaration;
96 iface_var->sampling = var.sampling;
97 iface_var->interface = iface;
98 iface_var->type = var.type;
99 iface_var->name = name;
100 /* Geometry shader inputs are always arrays. But if we're bringing in an
101 entire block, the array is on the block and not individual variables. */
102 if(stage->type==Stage::GEOMETRY && !copy_block)
103 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
105 iface_var->array = var.array;
107 iface_var->array_size = var.array_size;
110 iface_var->layout = var.layout;
111 iface_var->linked_declaration = &var;
112 var.linked_declaration = iface_var;
115 iface_target_block->body.insert(iface_insert_point, iface_var);
116 iface_target_block->variables.insert(make_pair(name, iface_var));
117 if(iface_target_block==&stage->content && iface=="in")
118 declared_inputs.push_back(iface_var);
123 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
125 if(stage->interface_blocks.count("in "+out_block.block_name))
128 InterfaceBlock *in_block = new InterfaceBlock;
129 in_block->interface = "in";
130 in_block->block_name = out_block.block_name;
131 in_block->members = new Block;
132 in_block->instance_name = out_block.instance_name;
133 if(stage->type==Stage::GEOMETRY)
134 in_block->array = true;
136 in_block->array = out_block.array;
137 in_block->linked_block = &out_block;
138 out_block.linked_block = in_block;
141 SetFlag set_copy(copy_block, true);
142 SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
143 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
144 if(out_block.struct_declaration)
145 out_block.struct_declaration->members.visit(*this);
146 else if(out_block.members)
147 out_block.members->visit(*this);
150 iface_target_block->body.insert(iface_insert_point, in_block);
151 stage->interface_blocks.insert(make_pair("in "+in_block->block_name, in_block));
152 if(!in_block->instance_name.empty())
153 stage->interface_blocks.insert(make_pair(in_block->instance_name, in_block));
155 SetFlag set_scope(function_scope, false);
156 SetForScope<Block *> set_block(current_block, &stage->content);
157 in_block->visit(*this);
162 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
164 Assignment *assign = new Assignment;
165 VariableReference *ref = new VariableReference;
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 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
206 auto j = prev_blocks.find(var.name);
207 if(j!=prev_blocks.end() && j->second->interface=="out")
209 generate_interface(*j->second);
210 /* Let VariableResolver convert the variable reference into an interface
215 for(const auto &kvp: prev_blocks)
216 if(kvp.second->instance_name.empty() && kvp.second->struct_declaration)
218 const map<string, VariableDeclaration *> &iface_vars = kvp.second->struct_declaration->members.variables;
219 i = iface_vars.find(var.name);
220 if(i!=iface_vars.end())
222 generate_interface(*kvp.second);
228 void InterfaceGenerator::visit(VariableDeclaration &var)
231 generate_interface(var, "in", var.name);
232 else if(var.interface=="out")
234 /* For output variables in function scope, generate a global interface
235 and replace the local declaration with an assignment. */
236 VariableDeclaration *out_var = 0;
237 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
239 out_var->source = var.source;
240 out_var->line = var.line;
241 nodes_to_remove.insert(&var);
242 if(var.init_expression)
244 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
245 stmt.source = var.source;
246 stmt.line = var.line;
251 else if(var.interface=="in" && current_block==&stage->content)
253 if(var.name.compare(0, 3, "gl_"))
254 declared_inputs.push_back(&var);
256 /* Try to link input variables in global scope with output variables from
258 if(!var.linked_declaration && stage->previous)
260 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
261 auto i = prev_vars.find(var.name);
262 if(i!=prev_vars.end() && i->second->interface=="out")
264 var.linked_declaration = i->second;
265 i->second->linked_declaration = &var;
270 TraversingVisitor::visit(var);
273 void InterfaceGenerator::visit(InterfaceBlock &iface)
275 if(iface.interface=="in")
277 /* Try to link input blocks with output blocks sharing the same block
278 name from previous stage. */
279 if(!iface.linked_block && stage->previous)
281 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
282 auto i = prev_blocks.find("out "+iface.block_name);
283 if(i!=prev_blocks.end())
285 iface.linked_block = i->second;
286 i->second->linked_block = &iface;
291 TraversingVisitor::visit(iface);
294 void InterfaceGenerator::visit(FunctionDeclaration &func)
296 SetFlag set_scope(function_scope, true);
297 // Skip parameters because they're not useful here
298 func.body.visit(*this);
301 void InterfaceGenerator::visit(Passthrough &pass)
303 // Pass through all input variables declared so far.
304 vector<VariableDeclaration *> pass_vars = declared_inputs;
308 for(const auto &kvp: stage->previous->content.variables)
310 if(kvp.second->interface!="out")
313 /* Pass through output variables from the previous stage, but only
314 those which are not already linked to an input here. */
315 if(!kvp.second->linked_declaration && generate_interface(*kvp.second, "in", kvp.second->name))
316 pass_vars.push_back(kvp.second);
320 if(stage->type==Stage::GEOMETRY)
322 /* Special case for geometry shader: copy gl_Position from input to
324 InterfaceBlockReference *ref = new InterfaceBlockReference;
327 BinaryExpression *subscript = new BinaryExpression;
328 subscript->left = ref;
329 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
330 subscript->right = pass.subscript;
332 MemberAccess *memacc = new MemberAccess;
333 memacc->left = subscript;
334 memacc->member = "gl_Position";
336 insert_assignment("gl_Position", memacc);
339 for(VariableDeclaration *v: pass_vars)
341 string out_name = change_prefix(v->name, out_prefix);
342 generate_interface(*v, "out", out_name);
344 VariableReference *ref = new VariableReference;
348 BinaryExpression *subscript = new BinaryExpression;
349 subscript->left = ref;
350 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
351 subscript->right = pass.subscript;
352 insert_assignment(out_name, subscript);
355 insert_assignment(out_name, ref);
358 nodes_to_remove.insert(&pass);