Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use main buffer and copy data to fbo texture (opengl) #5294

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pkg/opengl/Texture.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
};
20 changes: 6 additions & 14 deletions src/renderer/OpenGL.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2363,26 +2363,18 @@ 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();
// 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| {
for (custom_state.programs) |program| {
// Bind our cell program state, buffers
const bind = try program.bind();
defer bind.unbind();
try bind.draw();

// copy main and custom fbo
try custom_state.copy();
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/renderer/opengl/custom.zig
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ pub const State = struct {
};
}

/// copy the fbo's attached texture to the backbuffer
pub fn copy(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,
Expand Down