From 3502db0f5f2553a875d22681f4395f6a4834c9c8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 17 Nov 2023 09:07:42 -0800 Subject: [PATCH] renderer/opengl: start custom program work --- src/renderer/OpenGL.zig | 24 ++++++++++-------- src/renderer/opengl/CellProgram.zig | 1 + src/renderer/opengl/CustomProgram.zig | 36 +++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 src/renderer/opengl/CustomProgram.zig diff --git a/src/renderer/OpenGL.zig b/src/renderer/OpenGL.zig index 3abf277e2..dd8e42dda 100644 --- a/src/renderer/OpenGL.zig +++ b/src/renderer/OpenGL.zig @@ -22,6 +22,7 @@ const math = @import("../math.zig"); const Surface = @import("../Surface.zig"); const CellProgram = @import("opengl/CellProgram.zig"); +const CustomProgram = @import("opengl/CustomProgram.zig"); const log = std.log.scoped(.grid); @@ -296,7 +297,7 @@ pub fn init(alloc: Allocator, options: renderer.Options) !OpenGL { pub fn deinit(self: *OpenGL) void { self.font_shaper.deinit(); - if (self.gl_state) |*v| v.deinit(); + if (self.gl_state) |*v| v.deinit(self.alloc); self.cells.deinit(self.alloc); self.cells_bg.deinit(self.alloc); @@ -370,7 +371,7 @@ pub fn displayUnrealized(self: *OpenGL) void { defer if (single_threaded_draw) self.draw_mutex.unlock(); if (self.gl_state) |*v| { - v.deinit(); + v.deinit(self.alloc); self.gl_state = null; } } @@ -392,7 +393,7 @@ pub fn displayRealize(self: *OpenGL) !void { errdefer gl_state.deinit(); // Unrealize if we have to - if (self.gl_state) |*v| v.deinit(); + if (self.gl_state) |*v| v.deinit(self.alloc); // Set our new state self.gl_state = gl_state; @@ -1512,6 +1513,7 @@ const GLState = struct { cell_program: CellProgram, texture: gl.Texture, texture_color: gl.Texture, + custom_programs: []const CustomProgram, pub fn init( alloc: Allocator, @@ -1532,12 +1534,11 @@ const GLState = struct { break :err &.{}; }; - if (custom_shaders.len > 0) { - const cp = try gl.Program.createVF( - @embedFile("shaders/custom.v.glsl"), - custom_shaders[0], - ); - _ = cp; + // Create our custom programs + const custom_programs = try CustomProgram.createList(alloc, custom_shaders); + errdefer { + for (custom_programs) |p| p.deinit(); + alloc.free(custom_programs); } // Blending for text. We use GL_ONE here because we should be using @@ -1595,12 +1596,15 @@ const GLState = struct { return .{ .cell_program = cell_program, + .custom_programs = custom_programs, .texture = tex, .texture_color = tex_color, }; } - pub fn deinit(self: *GLState) void { + pub fn deinit(self: *GLState, alloc: Allocator) void { + for (self.custom_programs) |p| p.deinit(); + alloc.free(self.custom_programs); self.texture.destroy(); self.texture_color.destroy(); self.cell_program.deinit(); diff --git a/src/renderer/opengl/CellProgram.zig b/src/renderer/opengl/CellProgram.zig index 8e9d81556..aa308d44a 100644 --- a/src/renderer/opengl/CellProgram.zig +++ b/src/renderer/opengl/CellProgram.zig @@ -69,6 +69,7 @@ pub fn init() !CellProgram { @embedFile("../shaders/cell.v.glsl"), @embedFile("../shaders/cell.f.glsl"), ); + errdefer program.destroy(); // Set our cell dimensions const pbind = try program.use(); diff --git a/src/renderer/opengl/CustomProgram.zig b/src/renderer/opengl/CustomProgram.zig new file mode 100644 index 000000000..97340dcc6 --- /dev/null +++ b/src/renderer/opengl/CustomProgram.zig @@ -0,0 +1,36 @@ +/// The OpenGL program for custom shaders. +const CustomProgram = @This(); + +const std = @import("std"); +const Allocator = std.mem.Allocator; +const gl = @import("opengl"); + +program: gl.Program, + +pub fn createList(alloc: Allocator, srcs: []const [:0]const u8) ![]const CustomProgram { + var programs = std.ArrayList(CustomProgram).init(alloc); + defer programs.deinit(); + errdefer for (programs.items) |program| program.deinit(); + + for (srcs) |src| { + try programs.append(try CustomProgram.init(src)); + } + + return try programs.toOwnedSlice(); +} + +pub fn init(src: [:0]const u8) !CustomProgram { + const program = try gl.Program.createVF( + @embedFile("../shaders/custom.v.glsl"), + src, + ); + errdefer program.destroy(); + + return .{ + .program = program, + }; +} + +pub fn deinit(self: CustomProgram) void { + self.program.destroy(); +}