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 && !copy_block && 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. But if we're bringing in an
112 entire block, the array is on the block and not individual variables. */
113 if(stage->type==Stage::GEOMETRY && !copy_block)
114 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
116 iface_var->array = var.array;
118 iface_var->array_size = var.array_size;
121 iface_var->layout = var.layout;
122 iface_var->linked_declaration = &var;
123 var.linked_declaration = iface_var;
126 iface_target_block->body.insert(iface_insert_point, iface_var);
127 iface_target_block->variables.insert(make_pair(name, iface_var));
128 if(iface_target_block==&stage->content && iface=="in")
129 declared_inputs.push_back(iface_var);
134 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
136 if(stage->interface_blocks.count("in "+out_block.block_name))
139 InterfaceBlock *in_block = new InterfaceBlock;
140 in_block->interface = "in";
141 in_block->block_name = out_block.block_name;
142 in_block->members = new Block;
143 in_block->instance_name = out_block.instance_name;
144 if(stage->type==Stage::GEOMETRY)
145 in_block->array = true;
147 in_block->array = out_block.array;
148 in_block->linked_block = &out_block;
149 out_block.linked_block = in_block;
152 SetFlag set_copy(copy_block, true);
153 SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
154 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
155 if(out_block.struct_declaration)
156 out_block.struct_declaration->members.visit(*this);
157 else if(out_block.members)
158 out_block.members->visit(*this);
161 iface_target_block->body.insert(iface_insert_point, in_block);
162 stage->interface_blocks.insert(make_pair("in "+in_block->block_name, in_block));
163 if(!in_block->instance_name.empty())
164 stage->interface_blocks.insert(make_pair(in_block->instance_name, in_block));
166 SetFlag set_scope(function_scope, false);
167 SetForScope<Block *> set_block(current_block, &stage->content);
168 in_block->visit(*this);
173 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
175 Assignment *assign = new Assignment;
176 VariableReference *ref = new VariableReference;
179 assign->oper = &Operator::get_operator("=", Operator::BINARY);
180 assign->right = right;
182 ExpressionStatement *stmt = new ExpressionStatement;
183 stmt->expression = assign;
184 current_block->body.insert(assignment_insert_point, stmt);
190 void InterfaceGenerator::visit(VariableReference &var)
192 if(var.declaration || !stage->previous)
194 /* Don't pull a variable from previous stage if we just generated an output
195 interface in this stage */
196 if(stage->content.variables.count(var.name))
199 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
200 auto i = prev_vars.find(var.name);
201 if(i==prev_vars.end() || i->second->interface!="out")
202 i = prev_vars.find(in_prefix+var.name);
203 if(i!=prev_vars.end() && i->second->interface=="out")
205 if(stage->type==Stage::GEOMETRY && i->second->array)
206 stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
207 format("Can't access '%s' through automatic interface because it's an array", var.name)));
210 generate_interface(*i->second, "in", i->second->name);
211 var.name = i->second->name;
216 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
217 auto j = prev_blocks.find(var.name);
218 if(j!=prev_blocks.end() && j->second->interface=="out")
220 generate_interface(*j->second);
221 /* Let VariableResolver convert the variable reference into an interface
226 for(const auto &kvp: prev_blocks)
227 if(kvp.second->instance_name.empty() && kvp.second->struct_declaration)
229 const map<string, VariableDeclaration *> &iface_vars = kvp.second->struct_declaration->members.variables;
230 i = iface_vars.find(var.name);
231 if(i!=iface_vars.end())
233 generate_interface(*kvp.second);
239 void InterfaceGenerator::visit(VariableDeclaration &var)
242 generate_interface(var, "in", var.name);
243 else if(var.interface=="out")
245 /* For output variables in function scope, generate a global interface
246 and replace the local declaration with an assignment. */
247 VariableDeclaration *out_var = 0;
248 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
250 out_var->source = var.source;
251 out_var->line = var.line;
252 nodes_to_remove.insert(&var);
253 if(var.init_expression)
255 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
256 stmt.source = var.source;
257 stmt.line = var.line;
262 else if(var.interface=="in" && current_block==&stage->content)
264 if(var.name.compare(0, 3, "gl_"))
265 declared_inputs.push_back(&var);
267 /* Try to link input variables in global scope with output variables from
269 if(!var.linked_declaration && stage->previous)
271 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
272 auto i = prev_vars.find(var.name);
273 if(i!=prev_vars.end() && i->second->interface=="out")
275 var.linked_declaration = i->second;
276 i->second->linked_declaration = &var;
281 TraversingVisitor::visit(var);
284 void InterfaceGenerator::visit(InterfaceBlock &iface)
286 if(iface.interface=="in")
288 /* Try to link input blocks with output blocks sharing the same block
289 name from previous stage. */
290 if(!iface.linked_block && stage->previous)
292 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
293 auto i = prev_blocks.find("out "+iface.block_name);
294 if(i!=prev_blocks.end())
296 iface.linked_block = i->second;
297 i->second->linked_block = &iface;
302 TraversingVisitor::visit(iface);
305 void InterfaceGenerator::visit(FunctionDeclaration &func)
307 SetFlag set_scope(function_scope, true);
308 // Skip parameters because they're not useful here
309 func.body.visit(*this);
312 void InterfaceGenerator::visit(Passthrough &pass)
314 // Pass through all input variables declared so far.
315 vector<VariableDeclaration *> pass_vars = declared_inputs;
319 for(const auto &kvp: stage->previous->content.variables)
321 if(kvp.second->interface!="out")
324 /* Pass through output variables from the previous stage, but only
325 those which are not already linked to an input here. */
326 if(!kvp.second->linked_declaration && generate_interface(*kvp.second, "in", kvp.second->name))
327 pass_vars.push_back(kvp.second);
331 if(stage->type==Stage::GEOMETRY)
333 /* Special case for geometry shader: copy gl_Position from input to
335 InterfaceBlockReference *ref = new InterfaceBlockReference;
338 BinaryExpression *subscript = new BinaryExpression;
339 subscript->left = ref;
340 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
341 subscript->right = pass.subscript;
343 MemberAccess *memacc = new MemberAccess;
344 memacc->left = subscript;
345 memacc->member = "gl_Position";
347 insert_assignment("gl_Position", memacc);
350 for(VariableDeclaration *v: pass_vars)
352 string out_name = change_prefix(v->name, out_prefix);
353 generate_interface(*v, "out", out_name);
355 VariableReference *ref = new VariableReference;
359 BinaryExpression *subscript = new BinaryExpression;
360 subscript->left = ref;
361 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
362 subscript->right = pass.subscript;
363 insert_assignment(out_name, subscript);
366 insert_assignment(out_name, ref);
369 nodes_to_remove.insert(&pass);