From 3ecb00f717e51039d28fabc71d3570847bb7f26c Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 16 Feb 2019 23:30:14 +0900 Subject: [PATCH] graphicsdriver: Optimize shaders Simplified the case when a color matrix is not used. --- internal/graphicsdriver/metal/driver.go | 13 ++++++++----- internal/graphicsdriver/opengl/shader.go | 10 ++++++---- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/internal/graphicsdriver/metal/driver.go b/internal/graphicsdriver/metal/driver.go index 283857cbc..e111e2a66 100644 --- a/internal/graphicsdriver/metal/driver.go +++ b/internal/graphicsdriver/metal/driver.go @@ -121,7 +121,6 @@ template struct GetColorFromTexel { inline float4 Do(VertexOut v, texture2d texture, constant float2& source_size, float scale) { constexpr sampler texture_sampler(filter::nearest); - const float2 texel_size = 1 / source_size; float2 p = AdjustTexelByAddress
(v.tex, v.tex_region); if (p.x < v.tex_region[0] || @@ -203,13 +202,17 @@ float4 FragmentShaderImpl( constant float4& color_matrix_translation, constant float& scale) { float4 c = GetColorFromTexel().Do(v, texture, source_size, scale); - c.rgb /= c.a + (1.0 - sign(c.a)); if (useColorM) { + c.rgb /= c.a + (1.0 - sign(c.a)); c = (color_matrix_body * c) + color_matrix_translation; + c *= v.color; + c = clamp(c, 0.0, 1.0); + c.rgb *= c.a; + } else { + float4 s = v.color; + c *= float4(s.r, s.g, s.b, 1.0) * s.a; + c = clamp(c, 0.0, c.a); } - c *= v.color; - c = clamp(c, 0.0, 1.0); - c.rgb *= c.a; return c; } diff --git a/internal/graphicsdriver/opengl/shader.go b/internal/graphicsdriver/opengl/shader.go index 133e8a659..8941ad33f 100644 --- a/internal/graphicsdriver/opengl/shader.go +++ b/internal/graphicsdriver/opengl/shader.go @@ -256,19 +256,21 @@ void main(void) { color = mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y); #endif +#if defined(USE_COLOR_MATRIX) // Un-premultiply alpha. // When the alpha is 0, 1.0 - sign(alpha) is 1.0, which means division does nothing. color.rgb /= color.a + (1.0 - sign(color.a)); - -#if defined(USE_COLOR_MATRIX) // Apply the color matrix or scale. color = (color_matrix_body * color) + color_matrix_translation; -#endif - color *= varying_color_scale; color = clamp(color, 0.0, 1.0); // Premultiply alpha color.rgb *= color.a; +#else + vec4 s = varying_color_scale; + color *= vec4(s.r, s.g, s.b, 1.0) * s.a; + color = clamp(color, 0.0, color.a); +#endif gl_FragColor = color; }