void ConstantConditionEliminator::visit(Conditional &cond)
{
- ExpressionEvaluator eval;
- cond.condition->visit(eval);
- if(eval.is_result_valid())
- {
- Block &block = (eval.get_result() ? cond.body : cond.else_body);
- current_block->body.splice(insert_point, block.body);
- nodes_to_remove.insert(&cond);
- return;
- }
+ if(Literal *literal = dynamic_cast<Literal *>(cond.condition.get()))
+ if(literal->value.check_type<bool>())
+ {
+ Block &block = (literal->value.value<bool>() ? cond.body : cond.else_body);
+ current_block->body.splice(insert_point, block.body);
+ nodes_to_remove.insert(&cond);
+ return;
+ }
TraversingVisitor::visit(cond);
}
--- /dev/null
+layout(constant_id=0) const bool use_texture = false;
+layout(constant_id=1) const bool use_vertex_color = false;
+uniform sampler2D tex;
+uniform mat4 mvp;
+
+#pragma MSP stage(vertex)
+layout(location=0) in vec4 position;
+layout(location=1) in vec4 color;
+layout(location=2) in vec2 texcoord;
+void main()
+{
+ passthrough;
+ gl_Position = mvp*position;
+}
+
+#pragma MSP stage(fragment)
+layout(location=0) out vec4 frag_color;
+void main()
+{
+ frag_color = vec4(1.0);
+ if(use_texture)
+ frag_color *= texture(tex, texcoord);
+ if(use_vertex_color)
+ frag_color *= color;
+}
+
+// Compile mode: module
+
+/* Expected output: vertex
+uniform mat4 mvp;
+layout(location=0) in vec4 position;
+layout(location=1) in vec4 color;
+layout(location=2) in vec2 texcoord;
+out vec4 _vs_out_color;
+out vec2 _vs_out_texcoord;
+void main()
+{
+ _vs_out_color = color;
+ _vs_out_texcoord = texcoord;
+ gl_Position = mvp*position;
+}
+*/
+
+/* Expected output: fragment
+layout(constant_id=0) const bool use_texture = false;
+layout(constant_id=1) const bool use_vertex_color = false;
+uniform sampler2D tex;
+layout(location=0) out vec4 frag_color;
+in vec2 _vs_out_texcoord;
+in vec4 _vs_out_color;
+void main()
+{
+ frag_color = vec4(1.0);
+ if(use_texture)
+ frag_color *= texture(tex, _vs_out_texcoord);
+ if(use_vertex_color)
+ frag_color *= _vs_out_color;
+}
+*/
--- /dev/null
+layout(constant_id=0) const bool use_texture = false;
+layout(constant_id=1) const bool use_vertex_color = false;
+uniform sampler2D tex;
+uniform mat4 mvp;
+
+#pragma MSP stage(vertex)
+layout(location=0) in vec4 position;
+layout(location=1) in vec4 color;
+layout(location=2) in vec2 texcoord;
+void main()
+{
+ passthrough;
+ gl_Position = mvp*position;
+}
+
+#pragma MSP stage(fragment)
+layout(location=0) out vec4 frag_color;
+void main()
+{
+ frag_color = vec4(1.0);
+ if(use_texture)
+ frag_color *= texture(tex, texcoord);
+ if(use_vertex_color)
+ frag_color *= color;
+}
+
+// Specialize: use_texture true
+
+/* Expected output: vertex
+uniform mat4 mvp;
+layout(location=0) in vec4 position;
+layout(location=2) in vec2 texcoord;
+out vec2 _vs_out_texcoord;
+void main()
+{
+ _vs_out_texcoord = texcoord;
+ gl_Position = mvp*position;
+}
+*/
+
+/* Expected output: fragment
+uniform sampler2D tex;
+layout(location=0) out vec4 frag_color;
+in vec2 _vs_out_texcoord;
+void main()
+{
+ frag_color = vec4(1.0);
+ frag_color *= texture(tex, _vs_out_texcoord);
+}
+*/