From: Mikko Rasa Date: Mon, 15 Mar 2021 09:05:26 +0000 (+0200) Subject: Eliminate constant ternary expressions X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=c4ae3a80623be4b0f17de4a9cc25f0bedf2dbef5;p=libs%2Fgl.git Eliminate constant ternary expressions --- diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 9cfad86a..7473fe2c 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -792,6 +792,24 @@ void ConstantConditionEliminator::visit(Block &block) } } +void ConstantConditionEliminator::visit(RefPtr &expr) +{ + r_ternary_result = 0; + expr->visit(*this); + if(r_ternary_result) + expr = r_ternary_result; + r_ternary_result = 0; +} + +void ConstantConditionEliminator::visit(TernaryExpression &ternary) +{ + ConstantStatus result = check_constant_condition(*ternary.condition); + if(result!=NOT_CONSTANT) + r_ternary_result = (result==CONSTANT_TRUE ? ternary.true_expr : ternary.false_expr); + else + r_ternary_result = 0; +} + void ConstantConditionEliminator::visit(Conditional &cond) { ConstantStatus result = check_constant_condition(*cond.condition); diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index 1262823f..16038806 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -191,6 +191,7 @@ private: NodeList::iterator insert_point; std::set nodes_to_remove; + RefPtr r_ternary_result; public: void apply(Stage &); @@ -199,6 +200,8 @@ private: ConstantStatus check_constant_condition(const Expression &); virtual void visit(Block &); + virtual void visit(RefPtr &); + virtual void visit(TernaryExpression &); virtual void visit(Conditional &); virtual void visit(Iteration &); }; diff --git a/tests/glsl/constant_ternary_removal.glsl b/tests/glsl/constant_ternary_removal.glsl new file mode 100644 index 00000000..02707103 --- /dev/null +++ b/tests/glsl/constant_ternary_removal.glsl @@ -0,0 +1,33 @@ +const bool use_color = false; + +#pragma MSP stage(vertex) +layout(location=0) in vec4 position; +layout(location=1) in vec4 color; +void main() +{ + gl_Position = position; + passthrough; +} + +#pragma MSP stage(fragment) +layout(location=0) out vec4 frag_color; +void main() +{ + frag_color = (use_color ? color : vec4(1.0)); +} + +/* Expected output: vertex +layout(location=0) in vec4 position; +void main() +{ + gl_Position = position; +} +*/ + +/* Expected output: fragment +layout(location=0) out vec4 frag_color; +void main() +{ + frag_color = vec4(1.0); +} +*/