renderer/opengl: set resolution uniform on screen size change

This commit is contained in:
Mitchell Hashimoto
2023-11-17 16:58:36 -08:00
parent 2559d6b367
commit fbc13d08b0
2 changed files with 28 additions and 2 deletions

View File

@ -107,8 +107,11 @@ draw_background: terminal.color.RGB,
const SetScreenSize = struct { const SetScreenSize = struct {
size: renderer.ScreenSize, size: renderer.ScreenSize,
fn apply(self: SetScreenSize, r: *const OpenGL) !void { fn apply(self: SetScreenSize, r: *OpenGL) !void {
const gl_state = r.gl_state orelse return error.OpenGLUninitialized; const gl_state: *GLState = if (r.gl_state) |*v|
v
else
return error.OpenGLUninitialized;
// Apply our padding // Apply our padding
const padding = if (r.padding.balance) const padding = if (r.padding.balance)
@ -146,6 +149,11 @@ const SetScreenSize = struct {
-1 * @as(f32, @floatFromInt(padding.top)), -1 * @as(f32, @floatFromInt(padding.top)),
), ),
); );
// Update our custom shader resolution
if (gl_state.custom) |*custom_state| {
try custom_state.setScreenSize(self.size);
}
} }
}; };

View File

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const gl = @import("opengl"); const gl = @import("opengl");
const ScreenSize = @import("../size.zig").ScreenSize;
/// The "INDEX" is the index into the global GL state and the /// The "INDEX" is the index into the global GL state and the
/// "BINDING" is the binding location in the shader. /// "BINDING" is the binding location in the shader.
@ -93,6 +94,16 @@ pub const State = struct {
self.vao.destroy(); 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 /// Call this prior to drawing a frame to update the time
/// and synchronize the uniforms. This synchronizes uniforms /// and synchronize the uniforms. This synchronizes uniforms
/// so you should make changes to uniforms prior to calling /// 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; self.uniforms.time_delta = since_ns / std.time.ns_per_s;
// Sync our uniform changes // Sync our uniform changes
try self.syncUniforms();
}
fn syncUniforms(self: *State) !void {
var ubobind = try self.ubo.bind(.uniform); var ubobind = try self.ubo.bind(.uniform);
defer ubobind.unbind(); defer ubobind.unbind();
try ubobind.setData(self.uniforms, .static_draw); 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 { pub fn bind(self: *const State) !Binding {
// Move our uniform buffer into proper global index. Note that // Move our uniform buffer into proper global index. Note that
// in theory we can do this globally once and never worry about // in theory we can do this globally once and never worry about