diff options
author | Jeff Smith <whydoubt@gmail.com> | 2018-01-26 06:25:22 -0600 |
---|---|---|
committer | Adam Jackson <ajax@redhat.com> | 2018-02-12 14:52:29 -0500 |
commit | cb908a7840487e4b81aa16c5b3a4b609ff1153fc (patch) | |
tree | 25ed4e1d645345136be4249ecc46e55064d3a6bb /glamor/glamor_gradient.c | |
parent | b41bb8147e706cb8e615842e08a548b7e8afd279 (diff) |
glamor: tidy up some gradient color formulas
- Avoid some needlessly complex equations for calculating the color at a
point in a gradient field.
- Avoid calculating certain values multiple times.
- Use similar variable names across the two versions of the get_color
function where practical.
Signed-off-by: Jeff Smith <whydoubt@gmail.com>
Reviewed-by: Adam Jackson <ajax@redhat.com>
(cherry picked from commit ff410c56bfd6dcdf5d252ef0ba3f4c6fde91774b)
Diffstat (limited to 'glamor/glamor_gradient.c')
-rw-r--r-- | glamor/glamor_gradient.c | 66 |
1 files changed, 30 insertions, 36 deletions
diff --git a/glamor/glamor_gradient.c b/glamor/glamor_gradient.c index 5deaf5ff4..f0b7d129c 100644 --- a/glamor/glamor_gradient.c +++ b/glamor/glamor_gradient.c @@ -54,8 +54,9 @@ _glamor_create_getcolor_fs_source(ScreenPtr screen, int stops_count, "vec4 get_color(float stop_len)\n"\ "{\n"\ " int i = 0;\n"\ - " float new_alpha; \n"\ + " vec4 stop_color_before;\n"\ " vec4 gradient_color;\n"\ + " float stop_delta;\n"\ " float percentage; \n"\ " \n"\ " if(stop_len < stops[0])\n"\ @@ -67,19 +68,19 @@ _glamor_create_getcolor_fs_source(ScreenPtr screen, int stops_count, " if(i == n_stop)\n"\ " return vec4(0.0, 0.0, 0.0, 0.0); \n"\ " \n"\ - " if(stops[i] - stops[i-1] > 2.0)\n"\ + " stop_color_before = stop_colors[i-1];\n"\ + " stop_delta = stops[i] - stops[i-1];\n"\ + " if(stop_delta > 2.0)\n"\ " percentage = 0.0;\n" /*For comply with pixman, walker->stepper overflow.*/\ - " else if(stops[i] - stops[i-1] < 0.000001)\n"\ + " else if(stop_delta < 0.000001)\n"\ " percentage = 0.0;\n"\ " else \n"\ - " percentage = (stop_len - stops[i-1])/(stops[i] - stops[i-1]);\n"\ - " new_alpha = percentage * stop_colors[i].a + \n"\ - " (1.0-percentage) * stop_colors[i-1].a; \n"\ - " gradient_color = vec4((percentage * stop_colors[i].rgb \n"\ - " + (1.0-percentage) * stop_colors[i-1].rgb)*new_alpha, \n"\ - " new_alpha);\n"\ + " percentage = (stop_len - stops[i-1])/stop_delta;\n"\ " \n"\ - " return gradient_color;\n"\ + " gradient_color = stop_color_before;\n"\ + " if(percentage != 0.0)\n"\ + " gradient_color += (stop_colors[i] - gradient_color)*percentage;\n"\ + " return vec4(gradient_color.rgb * gradient_color.a, gradient_color.a);\n"\ "}\n" /* Because the array access for shader is very slow, the performance is very low @@ -106,73 +107,66 @@ _glamor_create_getcolor_fs_source(ScreenPtr screen, int stops_count, "\n" "vec4 get_color(float stop_len)\n" "{\n" - " float stop_after;\n" - " float stop_before;\n" " vec4 stop_color_before;\n" " vec4 stop_color_after;\n" - " float new_alpha; \n" " vec4 gradient_color;\n" + " float stop_before;\n" + " float stop_delta;\n" " float percentage; \n" " \n" " if((stop_len < stop0) && (n_stop >= 1)) {\n" " stop_color_before = vec4(0.0, 0.0, 0.0, 0.0);\n" - " stop_color_after = vec4(0.0, 0.0, 0.0, 0.0);\n" - " stop_after = 0.0;\n" - " stop_before = 0.0;\n" + " stop_delta = 0.0;\n" " } else if((stop_len < stop1) && (n_stop >= 2)) {\n" " stop_color_before = stop_color0;\n" " stop_color_after = stop_color1;\n" - " stop_after = stop1;\n" " stop_before = stop0;\n" + " stop_delta = stop1 - stop0;\n" " } else if((stop_len < stop2) && (n_stop >= 3)) {\n" " stop_color_before = stop_color1;\n" " stop_color_after = stop_color2;\n" - " stop_after = stop2;\n" " stop_before = stop1;\n" + " stop_delta = stop2 - stop1;\n" " } else if((stop_len < stop3) && (n_stop >= 4)){\n" " stop_color_before = stop_color2;\n" " stop_color_after = stop_color3;\n" - " stop_after = stop3;\n" " stop_before = stop2;\n" + " stop_delta = stop3 - stop2;\n" " } else if((stop_len < stop4) && (n_stop >= 5)){\n" " stop_color_before = stop_color3;\n" " stop_color_after = stop_color4;\n" - " stop_after = stop4;\n" " stop_before = stop3;\n" + " stop_delta = stop4 - stop3;\n" " } else if((stop_len < stop5) && (n_stop >= 6)){\n" " stop_color_before = stop_color4;\n" " stop_color_after = stop_color5;\n" - " stop_after = stop5;\n" " stop_before = stop4;\n" + " stop_delta = stop5 - stop4;\n" " } else if((stop_len < stop6) && (n_stop >= 7)){\n" " stop_color_before = stop_color5;\n" " stop_color_after = stop_color6;\n" - " stop_after = stop6;\n" " stop_before = stop5;\n" + " stop_delta = stop6 - stop5;\n" " } else if((stop_len < stop7) && (n_stop >= 8)){\n" " stop_color_before = stop_color6;\n" " stop_color_after = stop_color7;\n" - " stop_after = stop7;\n" " stop_before = stop6;\n" + " stop_delta = stop7 - stop6;\n" " } else {\n" " stop_color_before = vec4(0.0, 0.0, 0.0, 0.0);\n" - " stop_color_after = vec4(0.0, 0.0, 0.0, 0.0);\n" - " stop_after = 0.0;\n" - " stop_before = 0.0;\n" + " stop_delta = 0.0;\n" " }\n" - " if(stop_after - stop_before > 2.0)\n" + " if(stop_delta > 2.0)\n" " percentage = 0.0;\n" //For comply with pixman, walker->stepper overflow. - " else if(stop_after - stop_before < 0.000001)\n" + " else if(stop_delta < 0.000001)\n" " percentage = 0.0;\n" - " else \n" - " percentage = (stop_len - stop_before)/(stop_after - stop_before);\n" - " new_alpha = percentage * stop_color_after.a + \n" - " (1.0-percentage) * stop_color_before.a; \n" - " gradient_color = vec4((percentage * stop_color_after.rgb \n" - " + (1.0-percentage) * stop_color_before.rgb)*new_alpha, \n" - " new_alpha);\n" + " else\n" + " percentage = (stop_len - stop_before)/stop_delta;\n" " \n" - " return gradient_color;\n" + " gradient_color = stop_color_before;\n" + " if(percentage != 0.0)\n" + " gradient_color += (stop_color_after - gradient_color)*percentage;\n" + " return vec4(gradient_color.rgb * gradient_color.a, gradient_color.a);\n" "}\n"; if (use_array) { |