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 = hash32(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())
38 void ConstantIdAssigner::visit(VariableDeclaration &var)
42 auto i = find_member(var.layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
43 if(i!=var.layout->qualifiers.end() && i->has_value)
46 auto_constants.push_back(&var);
49 existing_constants[var.name] = i->value;
50 used_ids.insert(i->value);
57 string InterfaceGenerator::get_out_prefix(Stage::Type type)
59 if(type==Stage::VERTEX)
61 else if(type==Stage::GEOMETRY)
67 void InterfaceGenerator::apply(Stage &s)
70 iface_target_block = &stage->content;
72 in_prefix = get_out_prefix(stage->previous->type);
73 out_prefix = get_out_prefix(stage->type);
74 s.content.visit(*this);
75 NodeRemover().apply(s, nodes_to_remove);
78 void InterfaceGenerator::visit(Block &block)
80 SetForScope<Block *> set_block(current_block, &block);
81 for(auto i=block.body.begin(); i!=block.body.end(); ++i)
83 assignment_insert_point = i;
84 if(&block==&stage->content)
85 iface_insert_point = i;
91 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
93 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
94 return prefix+name.substr(offset);
97 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
99 if(stage->content.variables.count(name))
102 if(stage->type==Stage::GEOMETRY && !copy_block && var.interface=="out" && var.array)
105 VariableDeclaration* iface_var = new VariableDeclaration;
106 iface_var->sampling = var.sampling;
107 iface_var->interface = iface;
108 iface_var->type = var.type;
109 iface_var->name = name;
110 /* Geometry shader inputs are always arrays. But if we're bringing in an
111 entire block, the array is on the block and not individual variables. */
112 if(stage->type==Stage::GEOMETRY && !copy_block)
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 iface_target_block->body.insert(iface_insert_point, iface_var);
126 iface_target_block->variables.insert(make_pair(name, iface_var));
127 if(iface_target_block==&stage->content && iface=="in")
128 declared_inputs.push_back(iface_var);
133 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
135 if(stage->interface_blocks.count("in "+out_block.block_name))
138 InterfaceBlock *in_block = new InterfaceBlock;
139 in_block->interface = "in";
140 in_block->block_name = out_block.block_name;
141 in_block->members = new Block;
142 in_block->instance_name = out_block.instance_name;
143 if(stage->type==Stage::GEOMETRY)
144 in_block->array = true;
146 in_block->array = out_block.array;
147 in_block->linked_block = &out_block;
148 out_block.linked_block = in_block;
151 SetFlag set_copy(copy_block, true);
152 SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
153 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
154 if(out_block.struct_declaration)
155 out_block.struct_declaration->members.visit(*this);
156 else if(out_block.members)
157 out_block.members->visit(*this);
160 iface_target_block->body.insert(iface_insert_point, in_block);
161 stage->interface_blocks.insert(make_pair("in "+in_block->block_name, in_block));
162 if(!in_block->instance_name.empty())
163 stage->interface_blocks.insert(make_pair(in_block->instance_name, in_block));
165 SetFlag set_scope(function_scope, false);
166 SetForScope<Block *> set_block(current_block, &stage->content);
167 in_block->visit(*this);
172 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
174 Assignment *assign = new Assignment;
175 VariableReference *ref = new VariableReference;
178 assign->oper = &Operator::get_operator("=", Operator::BINARY);
179 assign->right = right;
181 ExpressionStatement *stmt = new ExpressionStatement;
182 stmt->expression = assign;
183 current_block->body.insert(assignment_insert_point, stmt);
189 void InterfaceGenerator::visit(VariableReference &var)
191 if(var.declaration || !stage->previous)
193 /* Don't pull a variable from previous stage if we just generated an output
194 interface in this stage */
195 if(stage->content.variables.count(var.name))
198 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
199 auto i = prev_vars.find(var.name);
200 if(i==prev_vars.end() || i->second->interface!="out")
201 i = prev_vars.find(in_prefix+var.name);
202 if(i!=prev_vars.end() && i->second->interface=="out")
204 if(stage->type==Stage::GEOMETRY && i->second->array)
205 stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
206 format("Can't access '%s' through automatic interface because it's an array", var.name)));
209 generate_interface(*i->second, "in", i->second->name);
210 var.name = i->second->name;
215 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
216 auto j = prev_blocks.find(var.name);
217 if(j!=prev_blocks.end() && j->second->interface=="out")
219 generate_interface(*j->second);
220 /* Let VariableResolver convert the variable reference into an interface
225 for(const auto &kvp: prev_blocks)
226 if(kvp.second->instance_name.empty() && kvp.second->struct_declaration)
228 const map<string, VariableDeclaration *> &iface_vars = kvp.second->struct_declaration->members.variables;
229 i = iface_vars.find(var.name);
230 if(i!=iface_vars.end())
232 generate_interface(*kvp.second);
238 void InterfaceGenerator::visit(VariableDeclaration &var)
241 generate_interface(var, "in", var.name);
242 else if(var.interface=="out")
244 /* For output variables in function scope, generate a global interface
245 and replace the local declaration with an assignment. */
246 VariableDeclaration *out_var = 0;
247 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
249 out_var->source = var.source;
250 out_var->line = var.line;
251 nodes_to_remove.insert(&var);
252 if(var.init_expression)
254 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
255 stmt.source = var.source;
256 stmt.line = var.line;
261 else if(var.interface=="in" && current_block==&stage->content)
263 if(var.name.compare(0, 3, "gl_"))
264 declared_inputs.push_back(&var);
266 /* Try to link input variables in global scope with output variables from
268 if(!var.linked_declaration && stage->previous)
270 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
271 auto i = prev_vars.find(var.name);
272 if(i!=prev_vars.end() && i->second->interface=="out")
274 var.linked_declaration = i->second;
275 i->second->linked_declaration = &var;
280 TraversingVisitor::visit(var);
283 void InterfaceGenerator::visit(InterfaceBlock &iface)
285 if(iface.interface=="in")
287 /* Try to link input blocks with output blocks sharing the same block
288 name from previous stage. */
289 if(!iface.linked_block && stage->previous)
291 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
292 auto i = prev_blocks.find("out "+iface.block_name);
293 if(i!=prev_blocks.end())
295 iface.linked_block = i->second;
296 i->second->linked_block = &iface;
301 TraversingVisitor::visit(iface);
304 void InterfaceGenerator::visit(FunctionDeclaration &func)
306 SetFlag set_scope(function_scope, true);
307 // Skip parameters because they're not useful here
308 func.body.visit(*this);
311 void InterfaceGenerator::visit(Passthrough &pass)
313 // Pass through all input variables declared so far.
314 vector<VariableDeclaration *> pass_vars = declared_inputs;
318 for(const auto &kvp: stage->previous->content.variables)
320 if(kvp.second->interface!="out")
323 /* Pass through output variables from the previous stage, but only
324 those which are not already linked to an input here. */
325 if(!kvp.second->linked_declaration && generate_interface(*kvp.second, "in", kvp.second->name))
326 pass_vars.push_back(kvp.second);
330 if(stage->type==Stage::GEOMETRY)
332 /* Special case for geometry shader: copy gl_Position from input to
334 InterfaceBlockReference *ref = new InterfaceBlockReference;
337 BinaryExpression *subscript = new BinaryExpression;
338 subscript->left = ref;
339 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
340 subscript->right = pass.subscript;
342 MemberAccess *memacc = new MemberAccess;
343 memacc->left = subscript;
344 memacc->member = "gl_Position";
346 insert_assignment("gl_Position", memacc);
349 for(VariableDeclaration *v: pass_vars)
351 string out_name = change_prefix(v->name, out_prefix);
352 generate_interface(*v, "out", out_name);
354 VariableReference *ref = new VariableReference;
358 BinaryExpression *subscript = new BinaryExpression;
359 subscript->left = ref;
360 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
361 subscript->right = pass.subscript;
362 insert_assignment(out_name, subscript);
365 insert_assignment(out_name, ref);
368 nodes_to_remove.insert(&pass);