From 16183ea20e854d160628cccf26f57b3b5d5be53c Mon Sep 17 00:00:00 2001 From: Lee Salzman Date: Sun, 13 Sep 2020 08:40:22 +0000 Subject: [PATCH] Bug 1664459 - simplify WebRender radial gradient shader. r=mattwoodrow Differential Revision: https://phabricator.services.mozilla.com/D89915 --- .../webrender/res/brush_radial_gradient.glsl | 50 ++++--------------- layout/reftests/border-image/reftest.list | 2 +- 2 files changed, 12 insertions(+), 40 deletions(-) diff --git a/gfx/wr/webrender/res/brush_radial_gradient.glsl b/gfx/wr/webrender/res/brush_radial_gradient.glsl index b178d606e1f8..af0abcd6b352 100644 --- a/gfx/wr/webrender/res/brush_radial_gradient.glsl +++ b/gfx/wr/webrender/res/brush_radial_gradient.glsl @@ -14,7 +14,7 @@ #define V_CENTER flat_varying_vec4_0.xy #define V_START_RADIUS flat_varying_vec4_0.z -#define V_END_RADIUS flat_varying_vec4_0.w +#define V_RADIUS_SCALE flat_varying_vec4_0.w #define V_REPEATED_SIZE flat_varying_vec4_1.xy #define V_GRADIENT_REPEAT flat_varying_vec4_1.z @@ -69,7 +69,13 @@ void radial_gradient_brush_vs( V_CENTER = gradient.center_start_end_radius.xy; V_START_RADIUS = gradient.center_start_end_radius.z; - V_END_RADIUS = gradient.center_start_end_radius.w; + if (gradient.center_start_end_radius.z != gradient.center_start_end_radius.w) { + // Store 1/rd where rd = end_radius - start_start + V_RADIUS_SCALE = 1.0 / (gradient.center_start_end_radius.w - gradient.center_start_end_radius.z); + } else { + // If rd = 0, we can't get its reciprocal. Instead, just use a zero scale. + V_RADIUS_SCALE = 0.0; + } // Transform all coordinates by the y scale so the // fragment shader can work with circles @@ -114,43 +120,9 @@ Fragment radial_gradient_brush_fs() { vec2 pos = mod(V_POS, V_REPEATED_SIZE); #endif + // Solve for t in length(pd) = V_START_RADIUS + t * rd vec2 pd = pos - V_CENTER; - float rd = V_END_RADIUS - V_START_RADIUS; - - // Solve for t in length(t - pd) = V_START_RADIUS + t * rd - // using a quadratic equation in form of At^2 - 2Bt + C = 0 - float A = -(rd * rd); - float B = V_START_RADIUS * rd; - float C = dot(pd, pd) - V_START_RADIUS * V_START_RADIUS; - - float offset; - if (A == 0.0) { - // Since A is 0, just solve for -2Bt + C = 0 - if (B == 0.0) { - discard; - } - float t = 0.5 * C / B; - if (V_START_RADIUS + rd * t >= 0.0) { - offset = t; - } else { - discard; - } - } else { - float discr = B * B - A * C; - if (discr < 0.0) { - discard; - } - discr = sqrt(discr); - float t0 = (B + discr) / A; - float t1 = (B - discr) / A; - if (V_START_RADIUS + rd * t0 >= 0.0) { - offset = t0; - } else if (V_START_RADIUS + rd * t1 >= 0.0) { - offset = t1; - } else { - discard; - } - } + float offset = (length(pd) - V_START_RADIUS) * V_RADIUS_SCALE; vec4 color = sample_gradient(V_GRADIENT_ADDRESS, offset, @@ -168,7 +140,7 @@ Fragment radial_gradient_brush_fs() { #undef V_GRADIENT_ADDRESS #undef V_CENTER #undef V_START_RADIUS -#undef V_END_RADIUS +#undef V_RADIUS_SCALE #undef V_REPEATED_SIZE #undef V_GRADIENT_REPEAT #undef V_POS diff --git a/layout/reftests/border-image/reftest.list b/layout/reftests/border-image/reftest.list index ee8bb8b6ccd7..dc1d83353571 100644 --- a/layout/reftests/border-image/reftest.list +++ b/layout/reftests/border-image/reftest.list @@ -57,7 +57,7 @@ fuzzy(0-1,0-2000) fuzzy-if(webrender&&!geckoview,1-2,3249-9500) == border-image- fuzzy(0-1,0-8533) fuzzy-if(webrender&&!geckoview,1-3,3188-9500) == border-image-linear-gradient-repeat-repeat-3.html border-image-linear-gradient-repeat-repeat-3-ref.html fuzzy(0-3,0-107563) fuzzy-if(webrender&&!geckoview,1-3,43500-107563) == border-image-linear-gradient-repeat-round-3.html border-image-linear-gradient-repeat-round-3-ref.html -fuzzy-if(webrender,0-1,0-2096) == border-image-radial-gradient.html border-image-radial-gradient-ref.html +fuzzy-if(webrender,0-2,0-2096) == border-image-radial-gradient.html border-image-radial-gradient-ref.html fuzzy(0-1,0-42) fuzzy-if(skiaContent,0-2,0-20) fuzzy-if(webrender,0-1,0-37818) == border-image-radial-gradient-slice-1.html border-image-radial-gradient-slice-1-ref.html fuzzy(0-1,0-46) fuzzy-if(OSX,0-2,0-4472) fuzzy-if(webrender,0-1,0-26363) == border-image-radial-gradient-slice-2.html border-image-radial-gradient-slice-2-ref.html fuzzy(0-1,0-85970) fuzzy-if(webrender,0-1,0-90873) == border-image-radial-gradient-slice-fill-1.html border-image-radial-gradient-slice-fill-1-ref.html