From ed1c163c7bb16f25b4ff16e8340434f4b1759335 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 3 Aug 2024 16:29:17 -0700 Subject: [PATCH] renderer/opengl: support window-padding-color=extend --- src/renderer/OpenGL.zig | 40 +++++++++++++++++++++++++++++++- src/renderer/shaders/cell.v.glsl | 17 ++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/renderer/OpenGL.zig b/src/renderer/OpenGL.zig index f1d1c3e53..e37dff83c 100644 --- a/src/renderer/OpenGL.zig +++ b/src/renderer/OpenGL.zig @@ -138,10 +138,11 @@ const SetScreenSize = struct { return error.OpenGLUninitialized; // Apply our padding + const grid_size = r.gridSize(self.size); const padding = if (r.padding.balance) renderer.Padding.balanced( self.size, - r.gridSize(self.size), + grid_size, .{ .width = r.grid_metrics.cell_width, .height = r.grid_metrics.cell_height, @@ -151,6 +152,18 @@ const SetScreenSize = struct { r.padding.explicit; const padded_size = self.size.subPadding(padding); + // Blank space around the grid. + const blank: renderer.Padding = switch (r.config.padding_color) { + // We can use zero padding because the backgroudn color is our + // clear color. + .background => .{}, + + .extend => self.size.blankPadding(padding, grid_size, .{ + .width = r.grid_metrics.cell_width, + .height = r.grid_metrics.cell_height, + }).add(padding), + }; + log.debug("GL api: screen size padded={} screen={} grid={} cell={} padding={}", .{ padded_size, self.size, @@ -189,6 +202,29 @@ const SetScreenSize = struct { ); } + // Setup our grid padding + { + const program = gl_state.cell_program; + const bind = try program.program.use(); + defer bind.unbind(); + try program.program.setUniform( + "grid_padding", + @Vector(4, f32){ + @floatFromInt(blank.top), + @floatFromInt(blank.right), + @floatFromInt(blank.bottom), + @floatFromInt(blank.left), + }, + ); + try program.program.setUniform( + "grid_size", + @Vector(2, f32){ + @floatFromInt(grid_size.columns), + @floatFromInt(grid_size.rows), + }, + ); + } + // Update our custom shader resolution if (gl_state.custom) |*custom_state| { try custom_state.setScreenSize(self.size); @@ -252,6 +288,7 @@ pub const DerivedConfig = struct { invert_selection_fg_bg: bool, bold_is_bright: bool, min_contrast: f32, + padding_color: configpkg.WindowPaddingColor, custom_shaders: std.ArrayListUnmanaged([:0]const u8), links: link.Set, @@ -308,6 +345,7 @@ pub const DerivedConfig = struct { .invert_selection_fg_bg = config.@"selection-invert-fg-bg", .bold_is_bright = config.@"bold-is-bright", .min_contrast = @floatCast(config.@"minimum-contrast"), + .padding_color = config.@"window-padding-color", .selection_background = if (config.@"selection-background") |bg| bg.toTerminalRGB() diff --git a/src/renderer/shaders/cell.v.glsl b/src/renderer/shaders/cell.v.glsl index 16e61773c..9b2b90026 100644 --- a/src/renderer/shaders/cell.v.glsl +++ b/src/renderer/shaders/cell.v.glsl @@ -55,6 +55,8 @@ flat out uint mode; uniform sampler2D text; uniform sampler2D text_color; uniform vec2 cell_size; +uniform vec2 grid_size; +uniform vec4 grid_padding; uniform mat4 projection; uniform float min_contrast; @@ -167,6 +169,21 @@ void main() { switch (mode) { case MODE_BG: + // If we're at the edge of the grid, we add our padding to the background + // to extend it. Note: grid_padding is top/right/bottom/left. + if (grid_coord.y == 0) { + cell_pos.y -= grid_padding.r; + cell_size_scaled.y += grid_padding.r; + } else if (grid_coord.y == grid_size.y - 1) { + cell_size_scaled.y += grid_padding.b; + } + if (grid_coord.x == 0) { + cell_pos.x -= grid_padding.a; + cell_size_scaled.x += grid_padding.a; + } else if (grid_coord.x == grid_size.x - 1) { + cell_size_scaled.x += grid_padding.g; + } + // Calculate the final position of our cell in world space. // We have to add our cell size since our vertices are offset // one cell up and to the left. (Do the math to verify yourself)