opengl: use premult alpha on fg color to avoid dark edges

This avoids an issue I only see in some renderers where the edges of
textures show up with a blurry border. Reading here:
https://www.realtimerendering.com/blog/gpus-prefer-premultiplication/
This commit is contained in:
Mitchell Hashimoto
2023-02-24 07:57:50 -08:00
parent 095a299a16
commit 35cb9d20b1
2 changed files with 6 additions and 3 deletions

View File

@ -369,9 +369,12 @@ pub fn windowInit(win: apprt.runtime.Window) !void {
const self: OpenGL = undefined;
try self.threadEnter(win);
// Blending for text
// Blending for text. We use GL_ONE here because we should be using
// premultiplied alpha for all our colors in our fragment shaders.
// This avoids having a blurry border where transparency is expected on
// pixels.
try gl.enable(gl.c.GL_BLEND);
try gl.blendFunc(gl.c.GL_SRC_ALPHA, gl.c.GL_ONE_MINUS_SRC_ALPHA);
try gl.blendFunc(gl.c.GL_ONE, gl.c.GL_ONE_MINUS_SRC_ALPHA);
// These are very noisy so this is commented, but easy to uncomment
// whenever we need to check the OpenGL extension list

View File

@ -42,7 +42,7 @@ void main() {
case MODE_FG:
a = texture(text, glyph_tex_coords).r;
out_FragColor = vec4(color.rgb, color.a*a);
out_FragColor = vec4(color.rgb*a, color.a*a);
break;
case MODE_FG_COLOR: