From: Mikko Rasa Date: Wed, 10 Mar 2021 10:12:26 +0000 (+0200) Subject: Make ConstantConditionEliminator less trigger-happy X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=05d8360ceef28e0b36e96127e04a82dd77a5c433;p=libs%2Fgl.git Make ConstantConditionEliminator less trigger-happy Since I removed the variable value tracking in 1cd0ea7 it started optimizing conditions on specialization constants, even when they had not been specialized yet. --- diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 500c8417..87a57eb7 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -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(cond.condition.get())) + if(literal->value.check_type()) + { + Block &block = (literal->value.value() ? 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 index 00000000..a7feab17 --- /dev/null +++ b/tests/glsl/keep_spec_constants_in_module.glsl @@ -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 index 00000000..b5666f2d --- /dev/null +++ b/tests/glsl/specialization_constants.glsl @@ -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); +} +*/