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 InterfaceGenerator::InterfaceGenerator():
49 function_scope(false),
54 string InterfaceGenerator::get_out_prefix(Stage::Type type)
56 if(type==Stage::VERTEX)
58 else if(type==Stage::GEOMETRY)
64 void InterfaceGenerator::apply(Stage &s)
67 iface_target_block = &stage->content;
69 in_prefix = get_out_prefix(stage->previous->type);
70 out_prefix = get_out_prefix(stage->type);
71 s.content.visit(*this);
72 NodeRemover().apply(s, nodes_to_remove);
75 void InterfaceGenerator::visit(Block &block)
77 SetForScope<Block *> set_block(current_block, &block);
78 for(auto i=block.body.begin(); i!=block.body.end(); ++i)
80 assignment_insert_point = i;
81 if(&block==&stage->content)
82 iface_insert_point = i;
88 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
90 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
91 return prefix+name.substr(offset);
94 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
96 if(stage->content.variables.count(name))
99 if(stage->type==Stage::GEOMETRY && !copy_block && var.interface=="out" && var.array)
102 VariableDeclaration* iface_var = new VariableDeclaration;
103 iface_var->sampling = var.sampling;
104 iface_var->interface = iface;
105 iface_var->type = var.type;
106 iface_var->name = name;
107 /* Geometry shader inputs are always arrays. But if we're bringing in an
108 entire block, the array is on the block and not individual variables. */
109 if(stage->type==Stage::GEOMETRY && !copy_block)
110 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
112 iface_var->array = var.array;
114 iface_var->array_size = var.array_size;
117 iface_var->layout = var.layout;
118 iface_var->linked_declaration = &var;
119 var.linked_declaration = iface_var;
122 iface_target_block->body.insert(iface_insert_point, iface_var);
123 iface_target_block->variables.insert(make_pair(name, iface_var));
124 if(iface_target_block==&stage->content && iface=="in")
125 declared_inputs.push_back(iface_var);
130 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
132 if(stage->interface_blocks.count("in "+out_block.block_name))
135 InterfaceBlock *in_block = new InterfaceBlock;
136 in_block->interface = "in";
137 in_block->block_name = out_block.block_name;
138 in_block->members = new Block;
139 in_block->instance_name = out_block.instance_name;
140 if(stage->type==Stage::GEOMETRY)
141 in_block->array = true;
143 in_block->array = out_block.array;
144 in_block->linked_block = &out_block;
145 out_block.linked_block = in_block;
148 SetFlag set_copy(copy_block, true);
149 SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
150 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
151 if(out_block.struct_declaration)
152 out_block.struct_declaration->members.visit(*this);
153 else if(out_block.members)
154 out_block.members->visit(*this);
157 iface_target_block->body.insert(iface_insert_point, in_block);
158 stage->interface_blocks.insert(make_pair("in "+in_block->block_name, in_block));
159 if(!in_block->instance_name.empty())
160 stage->interface_blocks.insert(make_pair(in_block->instance_name, in_block));
162 SetFlag set_scope(function_scope, false);
163 SetForScope<Block *> set_block(current_block, &stage->content);
164 in_block->visit(*this);
169 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
171 Assignment *assign = new Assignment;
172 VariableReference *ref = new VariableReference;
175 assign->oper = &Operator::get_operator("=", Operator::BINARY);
176 assign->right = right;
178 ExpressionStatement *stmt = new ExpressionStatement;
179 stmt->expression = assign;
180 current_block->body.insert(assignment_insert_point, stmt);
186 void InterfaceGenerator::visit(VariableReference &var)
188 if(var.declaration || !stage->previous)
190 /* Don't pull a variable from previous stage if we just generated an output
191 interface in this stage */
192 if(stage->content.variables.count(var.name))
195 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
196 auto i = prev_vars.find(var.name);
197 if(i==prev_vars.end() || i->second->interface!="out")
198 i = prev_vars.find(in_prefix+var.name);
199 if(i!=prev_vars.end() && i->second->interface=="out")
201 if(stage->type==Stage::GEOMETRY && i->second->array)
202 stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
203 format("Can't access '%s' through automatic interface because it's an array", var.name)));
206 generate_interface(*i->second, "in", i->second->name);
207 var.name = i->second->name;
212 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
213 auto j = prev_blocks.find(var.name);
214 if(j!=prev_blocks.end() && j->second->interface=="out")
216 generate_interface(*j->second);
217 /* Let VariableResolver convert the variable reference into an interface
222 for(const auto &kvp: prev_blocks)
223 if(kvp.second->instance_name.empty() && kvp.second->struct_declaration)
225 const map<string, VariableDeclaration *> &iface_vars = kvp.second->struct_declaration->members.variables;
226 i = iface_vars.find(var.name);
227 if(i!=iface_vars.end())
229 generate_interface(*kvp.second);
235 void InterfaceGenerator::visit(VariableDeclaration &var)
238 generate_interface(var, "in", var.name);
239 else if(var.interface=="out")
241 /* For output variables in function scope, generate a global interface
242 and replace the local declaration with an assignment. */
243 VariableDeclaration *out_var = 0;
244 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
246 out_var->source = var.source;
247 out_var->line = var.line;
248 nodes_to_remove.insert(&var);
249 if(var.init_expression)
251 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
252 stmt.source = var.source;
253 stmt.line = var.line;
258 else if(var.interface=="in" && current_block==&stage->content)
260 if(var.name.compare(0, 3, "gl_"))
261 declared_inputs.push_back(&var);
263 /* Try to link input variables in global scope with output variables from
265 if(!var.linked_declaration && stage->previous)
267 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
268 auto i = prev_vars.find(var.name);
269 if(i!=prev_vars.end() && i->second->interface=="out")
271 var.linked_declaration = i->second;
272 i->second->linked_declaration = &var;
277 TraversingVisitor::visit(var);
280 void InterfaceGenerator::visit(InterfaceBlock &iface)
282 if(iface.interface=="in")
284 /* Try to link input blocks with output blocks sharing the same block
285 name from previous stage. */
286 if(!iface.linked_block && stage->previous)
288 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
289 auto i = prev_blocks.find("out "+iface.block_name);
290 if(i!=prev_blocks.end())
292 iface.linked_block = i->second;
293 i->second->linked_block = &iface;
298 TraversingVisitor::visit(iface);
301 void InterfaceGenerator::visit(FunctionDeclaration &func)
303 SetFlag set_scope(function_scope, true);
304 // Skip parameters because they're not useful here
305 func.body.visit(*this);
308 void InterfaceGenerator::visit(Passthrough &pass)
310 // Pass through all input variables declared so far.
311 vector<VariableDeclaration *> pass_vars = declared_inputs;
315 for(const auto &kvp: stage->previous->content.variables)
317 if(kvp.second->interface!="out")
320 /* Pass through output variables from the previous stage, but only
321 those which are not already linked to an input here. */
322 if(!kvp.second->linked_declaration && generate_interface(*kvp.second, "in", kvp.second->name))
323 pass_vars.push_back(kvp.second);
327 if(stage->type==Stage::GEOMETRY)
329 /* Special case for geometry shader: copy gl_Position from input to
331 InterfaceBlockReference *ref = new InterfaceBlockReference;
334 BinaryExpression *subscript = new BinaryExpression;
335 subscript->left = ref;
336 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
337 subscript->right = pass.subscript;
339 MemberAccess *memacc = new MemberAccess;
340 memacc->left = subscript;
341 memacc->member = "gl_Position";
343 insert_assignment("gl_Position", memacc);
346 for(VariableDeclaration *v: pass_vars)
348 string out_name = change_prefix(v->name, out_prefix);
349 generate_interface(*v, "out", out_name);
351 VariableReference *ref = new VariableReference;
355 BinaryExpression *subscript = new BinaryExpression;
356 subscript->left = ref;
357 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
358 subscript->right = pass.subscript;
359 insert_assignment(out_name, subscript);
362 insert_assignment(out_name, ref);
365 nodes_to_remove.insert(&pass);