gl renderer: Consolidate color pre-multiplication

Add a common function that tells us what it does and replace all the
manual stuff with it.

Fixes #3170
This commit is contained in:
Timm Bäder 2020-09-18 15:26:58 +02:00
parent b8e4240751
commit a770ab34c4
7 changed files with 19 additions and 34 deletions

View File

@ -10,9 +10,7 @@ _OUT_ _ROUNDED_RECT_UNIFORM_ transformed_inside_outline;
void main() { void main() {
gl_Position = u_projection * u_modelview * vec4(aPosition, 0.0, 1.0); gl_Position = u_projection * u_modelview * vec4(aPosition, 0.0, 1.0);
final_color = u_color; final_color = premultiply(u_color) * u_alpha;
final_color.rgb *= final_color.a; // pre-multiply
final_color *= u_alpha;
RoundedRect outside = create_rect(u_outline_rect); RoundedRect outside = create_rect(u_outline_rect);
RoundedRect inside = rounded_rect_shrink (outside, u_widths); RoundedRect inside = rounded_rect_shrink (outside, u_widths);

View File

@ -6,10 +6,7 @@ _OUT_ vec4 final_color;
void main() { void main() {
gl_Position = u_projection * u_modelview * vec4(aPosition, 0.0, 1.0); gl_Position = u_projection * u_modelview * vec4(aPosition, 0.0, 1.0);
final_color = u_color; final_color = premultiply(u_color) * u_alpha;
// Pre-multiply alpha
final_color.rgb *= final_color.a;
final_color *= u_alpha;
} }
// FRAGMENT_SHADER: // FRAGMENT_SHADER:

View File

@ -8,10 +8,7 @@ void main() {
vUv = vec2(aUv.x, aUv.y); vUv = vec2(aUv.x, aUv.y);
final_color = u_color; final_color = premultiply(u_color) * u_alpha;
// pre-multiply
final_color.rgb *= final_color.a;
final_color *= u_alpha;
} }
// FRAGMENT_SHADER: // FRAGMENT_SHADER:

View File

@ -25,10 +25,10 @@ void main() {
for (int i = 0; i < u_num_color_stops; i ++) { for (int i = 0; i < u_num_color_stops; i ++) {
color_offsets[i] = u_color_stops[(i * 5) + 0]; color_offsets[i] = u_color_stops[(i * 5) + 0];
color_stops[i].r = u_color_stops[(i * 5) + 1]; color_stops[i] = premultiply(vec4(u_color_stops[(i * 5) + 1],
color_stops[i].g = u_color_stops[(i * 5) + 2]; u_color_stops[(i * 5) + 2],
color_stops[i].b = u_color_stops[(i * 5) + 3]; u_color_stops[(i * 5) + 3],
color_stops[i].a = u_color_stops[(i * 5) + 4]; u_color_stops[(i * 5) + 4]));
} }
} }
@ -66,8 +66,5 @@ void main() {
} }
} }
/* Pre-multiply */
color.rgb *= color.a;
setOutputColor(color * u_alpha); setOutputColor(color * u_alpha);
} }

View File

@ -10,10 +10,7 @@ void main() {
vUv = vec2(aUv.x, aUv.y); vUv = vec2(aUv.x, aUv.y);
final_color = u_color; final_color = premultiply(u_color) * u_alpha;
// pre-multiply
final_color.rgb *= final_color.a;
final_color *= u_alpha;
RoundedRect outline = create_rect(u_outline_rect); RoundedRect outline = create_rect(u_outline_rect);
rounded_rect_transform(outline, u_modelview); rounded_rect_transform(outline, u_modelview);

View File

@ -35,3 +35,7 @@ create_rect(vec4[3] data)
return RoundedRect(bounds, corner_points1, corner_points2); return RoundedRect(bounds, corner_points1, corner_points2);
} }
vec4 premultiply(vec4 c) {
return vec4(c.rgb * c.a, c.a);
}

View File

@ -21,10 +21,10 @@ void main() {
for (int i = 0; i < u_num_color_stops; i ++) { for (int i = 0; i < u_num_color_stops; i ++) {
color_offsets[i] = u_color_stops[(i * 5) + 0]; color_offsets[i] = u_color_stops[(i * 5) + 0];
color_stops[i].r = u_color_stops[(i * 5) + 1]; color_stops[i] = premultiply(vec4(u_color_stops[(i * 5) + 1],
color_stops[i].g = u_color_stops[(i * 5) + 2]; u_color_stops[(i * 5) + 2],
color_stops[i].b = u_color_stops[(i * 5) + 3]; u_color_stops[(i * 5) + 3],
color_stops[i].a = u_color_stops[(i * 5) + 4]; u_color_stops[(i * 5) + 4]));
} }
} }
@ -50,23 +50,18 @@ float abs_offset(float offset) {
return start + ((end - start) * offset); return start + ((end - start) * offset);
} }
vec4 premultiply(vec4 c) {
vec4 k = vec4(c.rgb * c.a, c.a);
return k;
}
void main() { void main() {
vec2 pixel = get_frag_coord(); vec2 pixel = get_frag_coord();
vec2 rel = (center - pixel) / (u_radius); vec2 rel = (center - pixel) / (u_radius);
float d = sqrt(dot(rel, rel)); float d = sqrt(dot(rel, rel));
if (d < abs_offset (color_offsets[0])) { if (d < abs_offset (color_offsets[0])) {
setOutputColor(premultiply(color_stops[0]) * u_alpha); setOutputColor(color_stops[0] * u_alpha);
return; return;
} }
if (d > end) { if (d > end) {
setOutputColor(premultiply(color_stops[u_num_color_stops - 1]) * u_alpha); setOutputColor(color_stops[u_num_color_stops - 1] * u_alpha);
return; return;
} }
@ -85,5 +80,5 @@ void main() {
} }
} }
setOutputColor(premultiply(color) * u_alpha); setOutputColor(color * u_alpha);
} }