graphicsdriver: Optimize shaders

Simplified the case when a color matrix is not used.
This commit is contained in:
Hajime Hoshi 2019-02-16 23:30:14 +09:00
parent 1c3113e763
commit 3ecb00f717
2 changed files with 14 additions and 9 deletions

View File

@ -121,7 +121,6 @@ template<uint8_t address>
struct GetColorFromTexel<FILTER_NEAREST, address> { struct GetColorFromTexel<FILTER_NEAREST, address> {
inline float4 Do(VertexOut v, texture2d<float> texture, constant float2& source_size, float scale) { inline float4 Do(VertexOut v, texture2d<float> texture, constant float2& source_size, float scale) {
constexpr sampler texture_sampler(filter::nearest); constexpr sampler texture_sampler(filter::nearest);
const float2 texel_size = 1 / source_size;
float2 p = AdjustTexelByAddress<address>(v.tex, v.tex_region); float2 p = AdjustTexelByAddress<address>(v.tex, v.tex_region);
if (p.x < v.tex_region[0] || if (p.x < v.tex_region[0] ||
@ -203,13 +202,17 @@ float4 FragmentShaderImpl(
constant float4& color_matrix_translation, constant float4& color_matrix_translation,
constant float& scale) { constant float& scale) {
float4 c = GetColorFromTexel<filter, address>().Do(v, texture, source_size, scale); float4 c = GetColorFromTexel<filter, address>().Do(v, texture, source_size, scale);
c.rgb /= c.a + (1.0 - sign(c.a));
if (useColorM) { if (useColorM) {
c.rgb /= c.a + (1.0 - sign(c.a));
c = (color_matrix_body * c) + color_matrix_translation; 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; return c;
} }

View File

@ -256,19 +256,21 @@ void main(void) {
color = mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y); color = mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y);
#endif #endif
#if defined(USE_COLOR_MATRIX)
// Un-premultiply alpha. // Un-premultiply alpha.
// When the alpha is 0, 1.0 - sign(alpha) is 1.0, which means division does nothing. // 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)); color.rgb /= color.a + (1.0 - sign(color.a));
#if defined(USE_COLOR_MATRIX)
// Apply the color matrix or scale. // Apply the color matrix or scale.
color = (color_matrix_body * color) + color_matrix_translation; color = (color_matrix_body * color) + color_matrix_translation;
#endif
color *= varying_color_scale; color *= varying_color_scale;
color = clamp(color, 0.0, 1.0); color = clamp(color, 0.0, 1.0);
// Premultiply alpha // Premultiply alpha
color.rgb *= color.a; 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; gl_FragColor = color;
} }