From 263c9e4aac6b376f87e6771a9e16fdff2893cf73 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 6 Apr 2022 08:42:39 -0700 Subject: [PATCH] opengl: unbind programs --- src/TextRenderer.zig | 6 ++++-- src/opengl/Program.zig | 9 ++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/TextRenderer.zig b/src/TextRenderer.zig index a3bf68a57..a30aebf1c 100644 --- a/src/TextRenderer.zig +++ b/src/TextRenderer.zig @@ -92,7 +92,8 @@ pub fn setScreenSize(self: *TextRenderer, w: i32, h: i32) !void { @intToFloat(f32, h), ); - try self.program.use(); + const bind = try self.program.use(); + defer bind.unbind(); try self.program.setUniform("projection", self.projection); } @@ -153,7 +154,8 @@ pub fn render( } } - try self.program.use(); + const pbind = try self.program.use(); + defer pbind.unbind(); // Bind our texture and set our data try gl.Texture.active(gl.c.GL_TEXTURE0); diff --git a/src/opengl/Program.zig b/src/opengl/Program.zig index 1ad8e0b5d..4f22f7efc 100644 --- a/src/opengl/Program.zig +++ b/src/opengl/Program.zig @@ -11,6 +11,12 @@ const errors = @import("errors.zig"); id: c.GLuint, +const Binding = struct { + pub inline fn unbind(_: Binding) void { + c.glUseProgram(0); + } +}; + pub inline fn create() !Program { const id = c.glCreateProgram(); if (id == 0) try errors.mustError(); @@ -61,8 +67,9 @@ pub inline fn link(p: Program) !void { return error.CompileFailed; } -pub inline fn use(p: Program) !void { +pub inline fn use(p: Program) !Binding { c.glUseProgram(p.id); + return Binding{}; } /// Requires the program is currently in use.