From 9c8c53bffb9d5a8f4cc712fcea083fbf79f833c4 Mon Sep 17 00:00:00 2001 From: Julia <58243358+juliapaci@users.noreply.github.com> Date: Fri, 24 Jan 2025 07:57:14 +1100 Subject: [PATCH] use main buffer and copy data to fbo texture (opengl) (#5294) NEEDS REVIEW continuation of #5037 resolves #4729 renders all shaders to the default buffer and then copies it to the designated custom shader texture. this is a draft pr because: - it introduces a new shader "pipeline" which doesnt fit in with how the system was designed to work (which is only rendering to the fbo) - im not sure if this is the best way to achieve shaders being able to sample their output while also drawing to the screen. the cusom fbo (previous implementation) was useful in that it modularized the custom shader stage in rendering --------- Co-authored-by: Mitchell Hashimoto --- pkg/opengl/Texture.zig | 22 ++++++++++++++++++++++ src/renderer/OpenGL.zig | 18 ++---------------- src/renderer/opengl/custom.zig | 15 +++++++++++++++ 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/pkg/opengl/Texture.zig b/pkg/opengl/Texture.zig index 4cd1cf9f9..a9fa5d4fe 100644 --- a/pkg/opengl/Texture.zig +++ b/pkg/opengl/Texture.zig @@ -162,4 +162,26 @@ pub const Binding = struct { data, ); } + + pub fn copySubImage2D( + b: Binding, + level: c.GLint, + xoffset: c.GLint, + yoffset: c.GLint, + x: c.GLint, + y: c.GLint, + width: c.GLsizei, + height: c.GLsizei, + ) !void { + glad.context.CopyTexSubImage2D.?( + @intFromEnum(b.target), + level, + xoffset, + yoffset, + x, + y, + width, + height + ); + } }; diff --git a/src/renderer/OpenGL.zig b/src/renderer/OpenGL.zig index 80fc3cab9..3e674c715 100644 --- a/src/renderer/OpenGL.zig +++ b/src/renderer/OpenGL.zig @@ -2363,26 +2363,12 @@ fn drawCustomPrograms(self: *OpenGL, custom_state: *custom.State) !void { // Setup the new frame try custom_state.newFrame(); - // To allow programs to retrieve each other via a texture - // then we must render the next shaders to the screen fbo. - // However, the first shader must be run while the default fbo - // is attached - { - const bind = try custom_state.programs[0].bind(); - defer bind.unbind(); - try bind.draw(); - if (custom_state.programs.len == 1) return; - } - - const fbobind = try custom_state.fbo.bind(.framebuffer); - defer fbobind.unbind(); - // Go through each custom shader and draw it. - for (custom_state.programs[1..]) |program| { - // Bind our cell program state, buffers + for (custom_state.programs) |program| { const bind = try program.bind(); defer bind.unbind(); try bind.draw(); + try custom_state.copyFramebuffer(); } } diff --git a/src/renderer/opengl/custom.zig b/src/renderer/opengl/custom.zig index 3806921df..859277ce5 100644 --- a/src/renderer/opengl/custom.zig +++ b/src/renderer/opengl/custom.zig @@ -230,6 +230,21 @@ pub const State = struct { }; } + /// Copy the fbo's attached texture to the backbuffer. + pub fn copyFramebuffer(self: *State) !void { + const texbind = try self.fb_texture.bind(.@"2D"); + errdefer texbind.unbind(); + try texbind.copySubImage2D( + 0, + 0, + 0, + 0, + 0, + @intFromFloat(self.uniforms.resolution[0]), + @intFromFloat(self.uniforms.resolution[1]), + ); + } + pub const Binding = struct { vao: gl.VertexArray.Binding, ebo: gl.Buffer.Binding,