From 51074ca5df991144f1f1490e200bddba3886c42d Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sat, 13 Mar 2021 18:21:45 -0500 Subject: [PATCH] ngl: Small shader improvements Add a variant of gskSetOutputColor that saves a few multiplications, and use it where possible. --- gsk/ngl/resources/blend.glsl | 2 +- gsk/ngl/resources/blit.glsl | 2 +- gsk/ngl/resources/border.glsl | 2 +- gsk/ngl/resources/conic_gradient.glsl | 2 +- gsk/ngl/resources/inset_shadow.glsl | 2 +- gsk/ngl/resources/linear_gradient.glsl | 2 +- gsk/ngl/resources/outset_shadow.glsl | 4 +-- gsk/ngl/resources/preamble.fs.glsl | 28 +++++++++++++++++-- gsk/ngl/resources/radial_gradient.glsl | 2 +- gsk/ngl/resources/repeat.glsl | 2 +- .../resources/unblurred_outset_shadow.glsl | 2 +- 11 files changed, 36 insertions(+), 14 deletions(-) diff --git a/gsk/ngl/resources/blend.glsl b/gsk/ngl/resources/blend.glsl index 066c20179d..609f9f29a2 100644 --- a/gsk/ngl/resources/blend.glsl +++ b/gsk/ngl/resources/blend.glsl @@ -310,5 +310,5 @@ void main() { else discard; - gskSetOutputColor(result * u_alpha); + gskSetScaledOutputColor(result, u_alpha); } diff --git a/gsk/ngl/resources/blit.glsl b/gsk/ngl/resources/blit.glsl index c232be2bfd..ced047b9a9 100644 --- a/gsk/ngl/resources/blit.glsl +++ b/gsk/ngl/resources/blit.glsl @@ -13,5 +13,5 @@ void main() { void main() { vec4 diffuse = GskTexture(u_source, vUv); - gskSetOutputColor(diffuse * u_alpha); + gskSetScaledOutputColor(diffuse, u_alpha); } diff --git a/gsk/ngl/resources/border.glsl b/gsk/ngl/resources/border.glsl index 4f47ab87dc..b8653bad49 100644 --- a/gsk/ngl/resources/border.glsl +++ b/gsk/ngl/resources/border.glsl @@ -39,5 +39,5 @@ void main() { gsk_rounded_rect_coverage(gsk_decode_rect(transformed_inside_outline), frag), 0.0, 1.0); - gskSetOutputColor(final_color * alpha); + gskSetScaledOutputColor(final_color, alpha); } diff --git a/gsk/ngl/resources/conic_gradient.glsl b/gsk/ngl/resources/conic_gradient.glsl index 758efe1e1e..3a26cf9563 100644 --- a/gsk/ngl/resources/conic_gradient.glsl +++ b/gsk/ngl/resources/conic_gradient.glsl @@ -75,7 +75,7 @@ void main() { vec4 curr_color = gsk_premultiply(get_color(i)); vec4 next_color = gsk_premultiply(get_color(i + 1)); vec4 color = mix(curr_color, next_color, f); - gskSetOutputColor(color * u_alpha); + gskSetScaledOutputColor(color, u_alpha); return; } } diff --git a/gsk/ngl/resources/inset_shadow.glsl b/gsk/ngl/resources/inset_shadow.glsl index 8cff1edee1..f052a08580 100644 --- a/gsk/ngl/resources/inset_shadow.glsl +++ b/gsk/ngl/resources/inset_shadow.glsl @@ -40,5 +40,5 @@ void main() { gsk_rounded_rect_coverage(gsk_decode_rect(transformed_inside_outline), frag), 0.0, 1.0); - gskSetOutputColor(final_color * alpha); + gskSetScaledOutputColor(final_color, alpha); } diff --git a/gsk/ngl/resources/linear_gradient.glsl b/gsk/ngl/resources/linear_gradient.glsl index d5c1d962f4..c4dfb53722 100644 --- a/gsk/ngl/resources/linear_gradient.glsl +++ b/gsk/ngl/resources/linear_gradient.glsl @@ -97,7 +97,7 @@ void main() { vec4 curr_color = gsk_premultiply(get_color(i)); vec4 next_color = gsk_premultiply(get_color(i + 1)); vec4 color = mix(curr_color, next_color, f); - gskSetOutputColor(color * u_alpha); + gskSetScaledOutputColor(color, u_alpha); return; } } diff --git a/gsk/ngl/resources/outset_shadow.glsl b/gsk/ngl/resources/outset_shadow.glsl index 0ab943b070..44b05aa152 100644 --- a/gsk/ngl/resources/outset_shadow.glsl +++ b/gsk/ngl/resources/outset_shadow.glsl @@ -30,7 +30,5 @@ void main() { alpha *= (1.0 - clamp(gsk_rounded_rect_coverage(gsk_decode_rect(transformed_outline), frag), 0.0, 1.0)); - vec4 color = final_color * alpha; - - gskSetOutputColor(color); + gskSetScaledOutputColor(final_color, alpha); } diff --git a/gsk/ngl/resources/preamble.fs.glsl b/gsk/ngl/resources/preamble.fs.glsl index e715d2526d..c1c5a954ee 100644 --- a/gsk/ngl/resources/preamble.fs.glsl +++ b/gsk/ngl/resources/preamble.fs.glsl @@ -123,11 +123,35 @@ void gskSetOutputColor(vec4 color) { #if defined(NO_CLIP) result = color; #elif defined(RECT_CLIP) - result = color * gsk_rect_coverage(gsk_get_bounds(u_clip_rect), + float coverage = gsk_rect_coverage(gsk_get_bounds(u_clip_rect), gsk_get_frag_coord()); + result = color * coverage; #else - result = color * gsk_rounded_rect_coverage(gsk_create_rect(u_clip_rect), + float coverage = gsk_rounded_rect_coverage(gsk_create_rect(u_clip_rect), gsk_get_frag_coord()); + result = color * coverage; +#endif + +#if defined(GSK_GLES) || defined(GSK_LEGACY) + gl_FragColor = result; +#else + outputColor = result; +#endif +} + +void gskSetScaledOutputColor(vec4 color, float alpha) { + vec4 result; + +#if defined(NO_CLIP) + result = color * alpha; +#elif defined(RECT_CLIP) + float coverage = gsk_rect_coverage(gsk_get_bounds(u_clip_rect), + gsk_get_frag_coord()); + result = color * (alpha * coverage); +#else + float coverage = gsk_rounded_rect_coverage(gsk_create_rect(u_clip_rect), + gsk_get_frag_coord()); + result = color * (alpha * coverage); #endif #if defined(GSK_GLES) || defined(GSK_LEGACY) diff --git a/gsk/ngl/resources/radial_gradient.glsl b/gsk/ngl/resources/radial_gradient.glsl index 1a6774dff5..5990b39a84 100644 --- a/gsk/ngl/resources/radial_gradient.glsl +++ b/gsk/ngl/resources/radial_gradient.glsl @@ -77,7 +77,7 @@ void main() { vec4 curr_color = gsk_premultiply(get_color(i)); vec4 next_color = gsk_premultiply(get_color(i + 1)); vec4 color = mix(curr_color, next_color, f); - gskSetOutputColor(color * u_alpha); + gskSetScaledOutputColor(color, u_alpha); return; } } diff --git a/gsk/ngl/resources/repeat.glsl b/gsk/ngl/resources/repeat.glsl index 5d39603396..1b811df2da 100644 --- a/gsk/ngl/resources/repeat.glsl +++ b/gsk/ngl/resources/repeat.glsl @@ -40,5 +40,5 @@ void main() { vec4 diffuse = GskTexture(u_source, tp); - gskSetOutputColor(diffuse * u_alpha); + gskSetScaledOutputColor(diffuse, u_alpha); } diff --git a/gsk/ngl/resources/unblurred_outset_shadow.glsl b/gsk/ngl/resources/unblurred_outset_shadow.glsl index 305cc5f1e2..cd44212c93 100644 --- a/gsk/ngl/resources/unblurred_outset_shadow.glsl +++ b/gsk/ngl/resources/unblurred_outset_shadow.glsl @@ -39,6 +39,6 @@ void main() { gsk_rounded_rect_coverage(gsk_decode_rect(transformed_inside_outline), frag), 0.0, 1.0); - gskSetOutputColor(final_color * alpha); + gskSetScaledOutputColor(final_color, alpha); }