From 792d8e20d703888f2220eff1fe4bf4b062ce28a0 Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Mon, 6 Mar 2023 21:18:13 +0800 Subject: [PATCH] [Impeller] Optimize the calculation of interpolant value of linear gradient --- impeller/entity/shaders/linear_gradient_fill.frag | 8 ++++---- impeller/entity/shaders/linear_gradient_ssbo_fill.frag | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/impeller/entity/shaders/linear_gradient_fill.frag b/impeller/entity/shaders/linear_gradient_fill.frag index f8d588c289728..f8a9a98695c0e 100644 --- a/impeller/entity/shaders/linear_gradient_fill.frag +++ b/impeller/entity/shaders/linear_gradient_fill.frag @@ -22,10 +22,10 @@ in vec2 v_position; out vec4 frag_color; void main() { - float len = length(frag_info.end_point - frag_info.start_point); - float dot = dot(v_position - frag_info.start_point, - frag_info.end_point - frag_info.start_point); - float t = dot / (len * len); + vec2 start_to_end = frag_info.end_point - frag_info.start_point; + vec2 start_to_position = v_position - frag_info.start_point; + float t = + dot(start_to_position, start_to_end) / dot(start_to_end, start_to_end); frag_color = IPSampleLinearWithTileMode( texture_sampler, vec2(t, 0.5), frag_info.texture_sampler_y_coord_scale, frag_info.half_texel, frag_info.tile_mode); diff --git a/impeller/entity/shaders/linear_gradient_ssbo_fill.frag b/impeller/entity/shaders/linear_gradient_ssbo_fill.frag index a8546834e3b02..718e319d469c9 100644 --- a/impeller/entity/shaders/linear_gradient_ssbo_fill.frag +++ b/impeller/entity/shaders/linear_gradient_ssbo_fill.frag @@ -30,10 +30,10 @@ in vec2 v_position; out vec4 frag_color; void main() { - float len = length(frag_info.end_point - frag_info.start_point); - float dot = dot(v_position - frag_info.start_point, - frag_info.end_point - frag_info.start_point); - float t = dot / (len * len); + vec2 start_to_end = frag_info.end_point - frag_info.start_point; + vec2 start_to_position = v_position - frag_info.start_point; + float t = + dot(start_to_position, start_to_end) / dot(start_to_end, start_to_end); if ((t < 0.0 || t > 1.0) && frag_info.tile_mode == kTileModeDecal) { frag_color = vec4(0);