diff --git a/src/renderer/OpenGL.zig b/src/renderer/OpenGL.zig index 8306843b5..f5609fd13 100644 --- a/src/renderer/OpenGL.zig +++ b/src/renderer/OpenGL.zig @@ -107,8 +107,11 @@ draw_background: terminal.color.RGB, const SetScreenSize = struct { size: renderer.ScreenSize, - fn apply(self: SetScreenSize, r: *const OpenGL) !void { - const gl_state = r.gl_state orelse return error.OpenGLUninitialized; + fn apply(self: SetScreenSize, r: *OpenGL) !void { + const gl_state: *GLState = if (r.gl_state) |*v| + v + else + return error.OpenGLUninitialized; // Apply our padding const padding = if (r.padding.balance) @@ -146,6 +149,11 @@ const SetScreenSize = struct { -1 * @as(f32, @floatFromInt(padding.top)), ), ); + + // Update our custom shader resolution + if (gl_state.custom) |*custom_state| { + try custom_state.setScreenSize(self.size); + } } }; diff --git a/src/renderer/opengl/custom.zig b/src/renderer/opengl/custom.zig index b316c3635..96169be30 100644 --- a/src/renderer/opengl/custom.zig +++ b/src/renderer/opengl/custom.zig @@ -1,6 +1,7 @@ const std = @import("std"); const Allocator = std.mem.Allocator; const gl = @import("opengl"); +const ScreenSize = @import("../size.zig").ScreenSize; /// The "INDEX" is the index into the global GL state and the /// "BINDING" is the binding location in the shader. @@ -93,6 +94,16 @@ pub const State = struct { self.vao.destroy(); } + pub fn setScreenSize(self: *State, size: ScreenSize) !void { + // Update our uniforms + self.uniforms.resolution = .{ + @floatFromInt(size.width), + @floatFromInt(size.height), + 1, + }; + try self.syncUniforms(); + } + /// Call this prior to drawing a frame to update the time /// and synchronize the uniforms. This synchronizes uniforms /// so you should make changes to uniforms prior to calling @@ -105,11 +116,18 @@ pub const State = struct { self.uniforms.time_delta = since_ns / std.time.ns_per_s; // Sync our uniform changes + try self.syncUniforms(); + } + + fn syncUniforms(self: *State) !void { var ubobind = try self.ubo.bind(.uniform); defer ubobind.unbind(); try ubobind.setData(self.uniforms, .static_draw); } + /// Call this to bind all the necessary OpenGL resources for + /// all custom shaders. Each individual shader needs to be bound + /// one at a time too. pub fn bind(self: *const State) !Binding { // Move our uniform buffer into proper global index. Note that // in theory we can do this globally once and never worry about