From 2db36646ac62ab269a9881e377a157922505ab77 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 17 Nov 2023 20:59:20 -0800 Subject: [PATCH] renderer/opengl: some comments --- src/renderer/OpenGL.zig | 9 ++------- src/renderer/opengl/custom.zig | 31 ++++++++++++++++++++++++++----- src/renderer/shaders/temp.f.glsl | 28 ---------------------------- 3 files changed, 28 insertions(+), 40 deletions(-) delete mode 100644 src/renderer/shaders/temp.f.glsl diff --git a/src/renderer/OpenGL.zig b/src/renderer/OpenGL.zig index 909388a78..99b89c527 100644 --- a/src/renderer/OpenGL.zig +++ b/src/renderer/OpenGL.zig @@ -1438,6 +1438,7 @@ pub fn drawFrame(self: *OpenGL, surface: *apprt.Surface) !void { } } +/// Draw the custom shaders. fn drawCustomPrograms( self: *OpenGL, custom_state: *custom.State, @@ -1456,13 +1457,7 @@ fn drawCustomPrograms( // Bind our cell program state, buffers const bind = try program.bind(); defer bind.unbind(); - - try gl.drawElementsInstanced( - gl.c.GL_TRIANGLES, - 6, - gl.c.GL_UNSIGNED_BYTE, - 1, - ); + try bind.draw(); } } diff --git a/src/renderer/opengl/custom.zig b/src/renderer/opengl/custom.zig index 6e0dd8d2a..c14ba3c5c 100644 --- a/src/renderer/opengl/custom.zig +++ b/src/renderer/opengl/custom.zig @@ -10,6 +10,7 @@ const log = std.log.scoped(.opengl_custom); const UNIFORM_INDEX: gl.c.GLuint = 0; const UNIFORM_BINDING: gl.c.GLuint = 0; +/// Global uniforms for custom shaders. pub const Uniforms = extern struct { resolution: [3]f32 align(16) = .{ 0, 0, 0 }, time: f32 align(4) = 1, @@ -23,7 +24,13 @@ pub const Uniforms = extern struct { sample_rate: f32 align(4) = 1, }; -/// The state associated with custom shaders. +/// The state associated with custom shaders. This should only be initialized +/// if there is at least one custom shader. +/// +/// To use this, the main terminal shader should render to the framebuffer +/// specified by "fbo". The resulting "fb_texture" will contain the color +/// attachment. This is then used as the iChannel0 input to the custom +/// shader. pub const State = struct { /// The uniform data uniforms: Uniforms, @@ -40,12 +47,14 @@ pub const State = struct { /// The last time the frame was drawn. This is used to update /// the time uniform. - last_frame_time: std.time.Instant, + first_frame_time: std.time.Instant, pub fn init( alloc: Allocator, srcs: []const [:0]const u8, ) !State { + if (srcs.len == 0) return error.OneCustomShaderRequired; + // Create our programs var programs = std.ArrayList(Program).init(alloc); defer programs.deinit(); @@ -126,7 +135,7 @@ pub const State = struct { .vao = vao, .ebo = ebo, .fb_texture = fb_tex, - .last_frame_time = try std.time.Instant.now(), + .first_frame_time = try std.time.Instant.now(), }; } @@ -169,8 +178,8 @@ pub const State = struct { /// this. pub fn newFrame(self: *State) !void { // Update our frame time - const now = std.time.Instant.now() catch self.last_frame_time; - const since_ns: f32 = @floatFromInt(now.since(self.last_frame_time)); + const now = std.time.Instant.now() catch self.first_frame_time; + const since_ns: f32 = @floatFromInt(now.since(self.first_frame_time)); self.uniforms.time = since_ns / std.time.ns_per_s; self.uniforms.time_delta = since_ns / std.time.ns_per_s; @@ -249,6 +258,8 @@ pub const Program = struct { self.program.destroy(); } + /// Bind the program for use. This should be called so that draw can + /// be called. pub fn bind(self: *const Program) !Binding { const program = try self.program.use(); errdefer program.unbind(); @@ -264,5 +275,15 @@ pub const Program = struct { pub fn unbind(self: Binding) void { self.program.unbind(); } + + pub fn draw(self: Binding) !void { + _ = self; + try gl.drawElementsInstanced( + gl.c.GL_TRIANGLES, + 6, + gl.c.GL_UNSIGNED_BYTE, + 1, + ); + } }; }; diff --git a/src/renderer/shaders/temp.f.glsl b/src/renderer/shaders/temp.f.glsl deleted file mode 100644 index b97b34549..000000000 --- a/src/renderer/shaders/temp.f.glsl +++ /dev/null @@ -1,28 +0,0 @@ -#version 430 core - -layout(binding = 0, std140) uniform Globals -{ - vec3 iResolution; - float iTime; - float iTimeDelta; - float iFrameRate; - int iFrame; - float iChannelTime[4]; - vec3 iChannelResolution[4]; - vec4 iMouse; - vec4 iDate; - float iSampleRate; -} _89; - -layout(binding = 1) uniform sampler2D iChannel0; - -layout(location = 0) out vec4 _fragColor; - -void main() { - // red - _fragColor = vec4(_89.iSampleRate, 0.0, 0.0, 1.0); - - // maze - //vec4 I = gl_FragCoord; - //_fragColor = vec4(3)*modf(I*.1,I)[int(length(I)*1e4)&1]; -}