]> git.tdb.fi Git - libs/gl.git/commitdiff
Make ConstantConditionEliminator less trigger-happy
authorMikko Rasa <tdb@tdb.fi>
Wed, 10 Mar 2021 10:12:26 +0000 (12:12 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 10 Mar 2021 13:33:38 +0000 (15:33 +0200)
Since I removed the variable value tracking in 1cd0ea7 it started
optimizing conditions on specialization constants, even when they
had not been specialized yet.

source/glsl/optimize.cpp
tests/glsl/keep_spec_constants_in_module.glsl [new file with mode: 0644]
tests/glsl/specialization_constants.glsl [new file with mode: 0644]

index 500c841793315aa1f1decb29b611c824291b003f..87a57eb7f82496c797c6afdcf7ddbb257049655c 100644 (file)
@@ -523,15 +523,14 @@ void ConstantConditionEliminator::visit(Block &block)
 
 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);
 }
diff --git a/tests/glsl/keep_spec_constants_in_module.glsl b/tests/glsl/keep_spec_constants_in_module.glsl
new file mode 100644 (file)
index 0000000..a7feab1
--- /dev/null
@@ -0,0 +1,59 @@
+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;
+}
+*/
diff --git a/tests/glsl/specialization_constants.glsl b/tests/glsl/specialization_constants.glsl
new file mode 100644 (file)
index 0000000..b5666f2
--- /dev/null
@@ -0,0 +1,50 @@
+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);
+}
+*/