1 #include <msp/core/hash.h>
2 #include <msp/core/raii.h>
11 void ConstantIdAssigner::apply(Stage &stage)
13 stage.content.visit(*this);
16 void ConstantIdAssigner::visit(VariableDeclaration &var)
20 vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
21 for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
22 if(i->name=="constant_id" && i->has_value)
25 i->value = hash32(var.name)&0x7FFFFFFF;
32 InterfaceGenerator::InterfaceGenerator():
34 function_scope(false),
39 string InterfaceGenerator::get_out_prefix(Stage::Type type)
41 if(type==Stage::VERTEX)
43 else if(type==Stage::GEOMETRY)
49 void InterfaceGenerator::apply(Stage &s)
52 iface_target_block = &stage->content;
54 in_prefix = get_out_prefix(stage->previous->type);
55 out_prefix = get_out_prefix(stage->type);
56 s.content.visit(*this);
57 NodeRemover().apply(s, nodes_to_remove);
60 void InterfaceGenerator::visit(Block &block)
62 SetForScope<Block *> set_block(current_block, &block);
63 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
65 assignment_insert_point = i;
66 if(&block==&stage->content)
67 iface_insert_point = i;
73 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
75 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
76 return prefix+name.substr(offset);
79 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
81 if(stage->content.variables.count(name))
84 if(stage->type==Stage::GEOMETRY && !copy_block && var.interface=="out" && var.array)
87 VariableDeclaration* iface_var = new VariableDeclaration;
88 iface_var->sampling = var.sampling;
89 iface_var->interface = iface;
90 iface_var->type = var.type;
91 iface_var->name = name;
92 /* Geometry shader inputs are always arrays. But if we're bringing in an
93 entire block, the array is on the block and not individual variables. */
94 if(stage->type==Stage::GEOMETRY && !copy_block)
95 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
97 iface_var->array = var.array;
99 iface_var->array_size = var.array_size;
102 iface_var->layout = var.layout;
103 iface_var->linked_declaration = &var;
104 var.linked_declaration = iface_var;
107 iface_target_block->body.insert(iface_insert_point, iface_var);
108 iface_target_block->variables.insert(make_pair(name, iface_var));
109 if(iface_target_block==&stage->content && iface=="in")
110 declared_inputs.push_back(iface_var);
115 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
117 if(stage->interface_blocks.count("in"+out_block.block_name))
120 InterfaceBlock *in_block = new InterfaceBlock;
121 in_block->interface = "in";
122 in_block->block_name = out_block.block_name;
123 in_block->members = new Block;
124 in_block->instance_name = out_block.instance_name;
125 if(stage->type==Stage::GEOMETRY)
126 in_block->array = true;
128 in_block->array = out_block.array;
129 in_block->linked_block = &out_block;
130 out_block.linked_block = in_block;
133 SetFlag set_copy(copy_block, true);
134 SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
135 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
136 if(out_block.struct_declaration)
137 out_block.struct_declaration->members.visit(*this);
138 else if(out_block.members)
139 out_block.members->visit(*this);
142 iface_target_block->body.insert(iface_insert_point, in_block);
143 stage->interface_blocks.insert(make_pair("in"+in_block->block_name, in_block));
144 if(!in_block->instance_name.empty())
145 stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block));
147 SetFlag set_scope(function_scope, false);
148 SetForScope<Block *> set_block(current_block, &stage->content);
149 in_block->visit(*this);
154 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
156 Assignment *assign = new Assignment;
157 VariableReference *ref = new VariableReference;
160 assign->oper = &Operator::get_operator("=", Operator::BINARY);
161 assign->right = right;
163 ExpressionStatement *stmt = new ExpressionStatement;
164 stmt->expression = assign;
165 current_block->body.insert(assignment_insert_point, stmt);
171 void InterfaceGenerator::visit(VariableReference &var)
173 if(var.declaration || !stage->previous)
175 /* Don't pull a variable from previous stage if we just generated an output
176 interface in this stage */
177 if(stage->content.variables.count(var.name))
180 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
181 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
182 if(i==prev_vars.end() || i->second->interface!="out")
183 i = prev_vars.find(in_prefix+var.name);
184 if(i!=prev_vars.end() && i->second->interface=="out")
186 if(stage->type==Stage::GEOMETRY && i->second->array)
187 stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
188 format("Can't access '%s' through automatic interface because it's an array", var.name)));
191 generate_interface(*i->second, "in", i->second->name);
192 var.name = i->second->name;
197 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
198 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find("_"+var.name);
199 if(j!=prev_blocks.end() && j->second->interface=="out")
201 generate_interface(*j->second);
202 /* Let VariableResolver convert the variable reference into an interface
207 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
208 if(j->second->instance_name.empty() && j->second->struct_declaration)
210 const map<string, VariableDeclaration *> &iface_vars = j->second->struct_declaration->members.variables;
211 i = iface_vars.find(var.name);
212 if(i!=iface_vars.end())
214 generate_interface(*j->second);
220 void InterfaceGenerator::visit(VariableDeclaration &var)
223 generate_interface(var, "in", var.name);
224 else if(var.interface=="out")
226 /* For output variables in function scope, generate a global interface
227 and replace the local declaration with an assignment. */
228 VariableDeclaration *out_var = 0;
229 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
231 out_var->source = var.source;
232 out_var->line = var.line;
233 nodes_to_remove.insert(&var);
234 if(var.init_expression)
236 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
237 stmt.source = var.source;
238 stmt.line = var.line;
243 else if(var.interface=="in" && current_block==&stage->content)
245 if(var.name.compare(0, 3, "gl_"))
246 declared_inputs.push_back(&var);
248 /* Try to link input variables in global scope with output variables from
250 if(!var.linked_declaration && stage->previous)
252 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
253 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
254 if(i!=prev_vars.end() && i->second->interface=="out")
256 var.linked_declaration = i->second;
257 i->second->linked_declaration = &var;
262 TraversingVisitor::visit(var);
265 void InterfaceGenerator::visit(InterfaceBlock &iface)
267 if(iface.interface=="in")
269 /* Try to link input blocks with output blocks sharing the same block
270 name from previous stage. */
271 if(!iface.linked_block && stage->previous)
273 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
274 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find("out"+iface.block_name);
275 if(i!=prev_blocks.end())
277 iface.linked_block = i->second;
278 i->second->linked_block = &iface;
283 TraversingVisitor::visit(iface);
286 void InterfaceGenerator::visit(FunctionDeclaration &func)
288 SetFlag set_scope(function_scope, true);
289 // Skip parameters because they're not useful here
290 func.body.visit(*this);
293 void InterfaceGenerator::visit(Passthrough &pass)
295 // Pass through all input variables declared so far.
296 vector<VariableDeclaration *> pass_vars = declared_inputs;
300 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
301 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
303 if(i->second->interface!="out")
306 /* Pass through output variables from the previous stage, but only
307 those which are not already linked to an input here. */
308 if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
309 pass_vars.push_back(i->second);
313 if(stage->type==Stage::GEOMETRY)
315 /* Special case for geometry shader: copy gl_Position from input to
317 InterfaceBlockReference *ref = new InterfaceBlockReference;
320 BinaryExpression *subscript = new BinaryExpression;
321 subscript->left = ref;
322 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
323 subscript->right = pass.subscript;
325 MemberAccess *memacc = new MemberAccess;
326 memacc->left = subscript;
327 memacc->member = "gl_Position";
329 insert_assignment("gl_Position", memacc);
332 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
334 string out_name = change_prefix((*i)->name, out_prefix);
335 generate_interface(**i, "out", out_name);
337 VariableReference *ref = new VariableReference;
338 ref->name = (*i)->name;
341 BinaryExpression *subscript = new BinaryExpression;
342 subscript->left = ref;
343 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
344 subscript->right = pass.subscript;
345 insert_assignment(out_name, subscript);
348 insert_assignment(out_name, ref);
351 nodes_to_remove.insert(&pass);