From 35cb9d20b1b0eada67ccaf8d53761e7d9fda6bf7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 24 Feb 2023 07:57:50 -0800 Subject: [PATCH] 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/ --- src/renderer/OpenGL.zig | 7 +++++-- src/renderer/shaders/cell.f.glsl | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/renderer/OpenGL.zig b/src/renderer/OpenGL.zig index 6be6fc90c..c8cdf6a26 100644 --- a/src/renderer/OpenGL.zig +++ b/src/renderer/OpenGL.zig @@ -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 diff --git a/src/renderer/shaders/cell.f.glsl b/src/renderer/shaders/cell.f.glsl index 4a01e6958..080748216 100644 --- a/src/renderer/shaders/cell.f.glsl +++ b/src/renderer/shaders/cell.f.glsl @@ -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: