From 4737d137d0a1c7fed868c4adc7a3d7e00ba7681c Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 20 Apr 2021 18:21:35 +0300 Subject: [PATCH] Fold type conversions of constants --- source/glsl/optimize.cpp | 33 +++++++++++++++++++ source/glsl/optimize.h | 2 ++ tests/glsl/binary_operators.glsl | 2 +- tests/glsl/constructors.glsl | 4 +-- .../glsl/inexact_function_argument_type.glsl | 2 +- 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 06ea0f49..d21512f6 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -604,6 +604,17 @@ T ConstantFolder::evaluate_int_special_op(char oper, T left, T right) } } +template +void ConstantFolder::convert_to_result(const Variant &value) +{ + if(value.check_type()) + set_result(static_cast(value.value())); + else if(value.check_type()) + set_result(static_cast(value.value())); + else if(value.check_type()) + set_result(static_cast(value.value())); +} + void ConstantFolder::set_result(const Variant &value, bool literal) { r_constant_value = value; @@ -776,6 +787,28 @@ void ConstantFolder::visit(TernaryExpression &ternary) void ConstantFolder::visit(FunctionCall &call) { + if(call.constructor && call.type && call.arguments.size()==1) + { + const BasicTypeDeclaration *basic = dynamic_cast(call.type); + if(basic) + { + call.arguments[0]->visit(*this); + bool can_fold = r_constant; + r_constant = false; + if(!can_fold) + return; + + if(basic->kind==BasicTypeDeclaration::BOOL) + convert_to_result(r_constant_value); + else if(basic->kind==BasicTypeDeclaration::INT && basic->size==32) + convert_to_result(r_constant_value); + else if(basic->kind==BasicTypeDeclaration::FLOAT && basic->size==32) + convert_to_result(r_constant_value); + + return; + } + } + TraversingVisitor::visit(call); r_constant = false; } diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index ed9f9134..7d064056 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -183,6 +183,8 @@ private: static T evaluate_arithmetic(char, T, T); template static T evaluate_int_special_op(char, T, T); + template + void convert_to_result(const Variant &); void set_result(const Variant &, bool = false); virtual void visit(RefPtr &); diff --git a/tests/glsl/binary_operators.glsl b/tests/glsl/binary_operators.glsl index d8496286..edf36ebb 100644 --- a/tests/glsl/binary_operators.glsl +++ b/tests/glsl/binary_operators.glsl @@ -52,7 +52,7 @@ void main() mat2x4 m2; vec4 v1 = vec4(1.0); vec2 v3; - v3 = v1*m2+(m2*m1*float(5)*v1).xy+vec2(iv); + v3 = v1*m2+(m2*m1*5.0*v1).xy+vec2(iv); if(b) ++v3; } diff --git a/tests/glsl/constructors.glsl b/tests/glsl/constructors.glsl index ddb1dcea..9b878e49 100644 --- a/tests/glsl/constructors.glsl +++ b/tests/glsl/constructors.glsl @@ -41,7 +41,7 @@ void main() { mat3 normal_matrix = mat3(model[0].xyz, model[1].xyz, model[2].xyz); tbn_light_dir = mat3(normal_matrix*tangent, normal_matrix*binormal, normal_matrix*normal)*light_dir; - gl_Position = view_projection*model*vec4(position, float(1)); + gl_Position = view_projection*model*vec4(position, 1.0); _vs_out_texcoord = texcoord; } */ @@ -53,6 +53,6 @@ layout(location=1) in vec2 _vs_out_texcoord; layout(location=0) in vec3 tbn_light_dir; void main() { - frag_color = vec4(vec3(dot(vec3(texture(normalmap, _vs_out_texcoord).xyz)*2.0-1.0, normalize(tbn_light_dir))), float(1)); + frag_color = vec4(vec3(dot(vec3(texture(normalmap, _vs_out_texcoord).xyz)*2.0-1.0, normalize(tbn_light_dir))), 1.0); } */ diff --git a/tests/glsl/inexact_function_argument_type.glsl b/tests/glsl/inexact_function_argument_type.glsl index 2bbece37..627fa5d8 100644 --- a/tests/glsl/inexact_function_argument_type.glsl +++ b/tests/glsl/inexact_function_argument_type.glsl @@ -13,6 +13,6 @@ void main() layout(location=0) in vec4 position; void main() { - gl_Position = vec4(position.xy*float(2), 0.0, 1.0); + gl_Position = vec4(position.xy*2.0, 0.0, 1.0); } */ -- 2.43.0