1 #include <msp/core/hash.h>
2 #include <msp/core/raii.h>
11 ConstantSpecializer::ConstantSpecializer():
15 void ConstantSpecializer::apply(Stage &stage, const map<string, int> *v)
18 stage.content.visit(*this);
21 void ConstantSpecializer::visit(VariableDeclaration &var)
23 bool specializable = false;
26 vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
27 for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
28 if(i->name=="constant_id")
34 i->value = hash32(var.name)&0x7FFFFFFF;
38 if(qualifiers.empty())
42 if(specializable && values)
44 map<string, int>::const_iterator i = values->find(var.name);
47 RefPtr<Literal> literal = new Literal;
50 literal->token = (i->second ? "true" : "false");
51 literal->value = static_cast<bool>(i->second);
53 else if(var.type=="int")
55 literal->token = lexical_cast<string>(i->second);
56 literal->value = i->second;
58 var.init_expression = literal;
64 InterfaceGenerator::InterfaceGenerator():
66 function_scope(false),
71 string InterfaceGenerator::get_out_prefix(Stage::Type type)
73 if(type==Stage::VERTEX)
75 else if(type==Stage::GEOMETRY)
81 void InterfaceGenerator::apply(Stage &s)
84 iface_target_block = &stage->content;
86 in_prefix = get_out_prefix(stage->previous->type);
87 out_prefix = get_out_prefix(stage->type);
88 s.content.visit(*this);
89 NodeRemover().apply(s, nodes_to_remove);
92 void InterfaceGenerator::visit(Block &block)
94 SetForScope<Block *> set_block(current_block, &block);
95 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
97 assignment_insert_point = i;
98 if(&block==&stage->content)
99 iface_insert_point = i;
105 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
107 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
108 return prefix+name.substr(offset);
111 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
113 if(stage->content.variables.count(name))
116 if(stage->type==Stage::GEOMETRY && !copy_block && var.interface=="out" && var.array)
119 VariableDeclaration* iface_var = new VariableDeclaration;
120 iface_var->sampling = var.sampling;
121 iface_var->interface = iface;
122 iface_var->type = var.type;
123 iface_var->name = name;
124 /* Geometry shader inputs are always arrays. But if we're bringing in an
125 entire block, the array is on the block and not individual variables. */
126 if(stage->type==Stage::GEOMETRY && !copy_block)
127 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
129 iface_var->array = var.array;
131 iface_var->array_size = var.array_size;
134 iface_var->layout = var.layout;
135 iface_var->linked_declaration = &var;
136 var.linked_declaration = iface_var;
139 iface_target_block->body.insert(iface_insert_point, iface_var);
140 iface_target_block->variables.insert(make_pair(name, iface_var));
141 if(iface_target_block==&stage->content && iface=="in")
142 declared_inputs.push_back(iface_var);
147 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
149 if(stage->interface_blocks.count("in"+out_block.block_name))
152 InterfaceBlock *in_block = new InterfaceBlock;
153 in_block->interface = "in";
154 in_block->block_name = out_block.block_name;
155 in_block->members = new Block;
156 in_block->instance_name = out_block.instance_name;
157 if(stage->type==Stage::GEOMETRY)
158 in_block->array = true;
160 in_block->array = out_block.array;
161 in_block->linked_block = &out_block;
162 out_block.linked_block = in_block;
165 SetFlag set_copy(copy_block, true);
166 SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
167 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
168 if(out_block.struct_declaration)
169 out_block.struct_declaration->members.visit(*this);
170 else if(out_block.members)
171 out_block.members->visit(*this);
174 iface_target_block->body.insert(iface_insert_point, in_block);
175 stage->interface_blocks.insert(make_pair("in"+in_block->block_name, in_block));
176 if(!in_block->instance_name.empty())
177 stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block));
179 SetFlag set_scope(function_scope, false);
180 SetForScope<Block *> set_block(current_block, &stage->content);
181 in_block->visit(*this);
186 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
188 Assignment *assign = new Assignment;
189 VariableReference *ref = new VariableReference;
192 assign->oper = &Operator::get_operator("=", Operator::BINARY);
193 assign->right = right;
195 ExpressionStatement *stmt = new ExpressionStatement;
196 stmt->expression = assign;
197 current_block->body.insert(assignment_insert_point, stmt);
203 void InterfaceGenerator::visit(VariableReference &var)
205 if(var.declaration || !stage->previous)
207 /* Don't pull a variable from previous stage if we just generated an output
208 interface in this stage */
209 if(stage->content.variables.count(var.name))
212 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
213 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
214 if(i==prev_vars.end() || i->second->interface!="out")
215 i = prev_vars.find(in_prefix+var.name);
216 if(i!=prev_vars.end() && i->second->interface=="out")
218 if(stage->type==Stage::GEOMETRY && i->second->array)
219 stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
220 format("Can't access '%s' through automatic interface because it's an array", var.name)));
223 generate_interface(*i->second, "in", i->second->name);
224 var.name = i->second->name;
229 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
230 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find("_"+var.name);
231 if(j!=prev_blocks.end() && j->second->interface=="out")
233 generate_interface(*j->second);
234 /* Let VariableResolver convert the variable reference into an interface
239 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
240 if(j->second->instance_name.empty() && j->second->struct_declaration)
242 const map<string, VariableDeclaration *> &iface_vars = j->second->struct_declaration->members.variables;
243 i = iface_vars.find(var.name);
244 if(i!=iface_vars.end())
246 generate_interface(*j->second);
252 void InterfaceGenerator::visit(VariableDeclaration &var)
255 generate_interface(var, "in", var.name);
256 else if(var.interface=="out")
258 /* For output variables in function scope, generate a global interface
259 and replace the local declaration with an assignment. */
260 VariableDeclaration *out_var = 0;
261 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
263 out_var->source = var.source;
264 out_var->line = var.line;
265 nodes_to_remove.insert(&var);
266 if(var.init_expression)
268 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
269 stmt.source = var.source;
270 stmt.line = var.line;
275 else if(var.interface=="in" && current_block==&stage->content)
277 if(var.name.compare(0, 3, "gl_"))
278 declared_inputs.push_back(&var);
280 /* Try to link input variables in global scope with output variables from
282 if(!var.linked_declaration && stage->previous)
284 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
285 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
286 if(i!=prev_vars.end() && i->second->interface=="out")
288 var.linked_declaration = i->second;
289 i->second->linked_declaration = &var;
294 TraversingVisitor::visit(var);
297 void InterfaceGenerator::visit(InterfaceBlock &iface)
299 if(iface.interface=="in")
301 /* Try to link input blocks with output blocks sharing the same block
302 name from previous stage. */
303 if(!iface.linked_block && stage->previous)
305 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
306 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find("out"+iface.block_name);
307 if(i!=prev_blocks.end())
309 iface.linked_block = i->second;
310 i->second->linked_block = &iface;
315 TraversingVisitor::visit(iface);
318 void InterfaceGenerator::visit(FunctionDeclaration &func)
320 SetFlag set_scope(function_scope, true);
321 // Skip parameters because they're not useful here
322 func.body.visit(*this);
325 void InterfaceGenerator::visit(Passthrough &pass)
327 // Pass through all input variables declared so far.
328 vector<VariableDeclaration *> pass_vars = declared_inputs;
332 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
333 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
335 if(i->second->interface!="out")
338 /* Pass through output variables from the previous stage, but only
339 those which are not already linked to an input here. */
340 if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
341 pass_vars.push_back(i->second);
345 if(stage->type==Stage::GEOMETRY)
347 /* Special case for geometry shader: copy gl_Position from input to
349 InterfaceBlockReference *ref = new InterfaceBlockReference;
352 BinaryExpression *subscript = new BinaryExpression;
353 subscript->left = ref;
354 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
355 subscript->right = pass.subscript;
357 MemberAccess *memacc = new MemberAccess;
358 memacc->left = subscript;
359 memacc->member = "gl_Position";
361 insert_assignment("gl_Position", memacc);
364 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
366 string out_name = change_prefix((*i)->name, out_prefix);
367 generate_interface(**i, "out", out_name);
369 VariableReference *ref = new VariableReference;
370 ref->name = (*i)->name;
373 BinaryExpression *subscript = new BinaryExpression;
374 subscript->left = ref;
375 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
376 subscript->right = pass.subscript;
377 insert_assignment(out_name, subscript);
380 insert_assignment(out_name, ref);
383 nodes_to_remove.insert(&pass);