renderer/opengl: draw custom shaders, simplified

This commit is contained in:
Mitchell Hashimoto
2023-11-17 09:58:28 -08:00
parent da600fee8f
commit 5fc91401f2
4 changed files with 54 additions and 4 deletions

View File

@ -1410,6 +1410,9 @@ pub fn drawFrame(self: *OpenGL, surface: *apprt.Surface) !void {
// Draw our terminal cells
try self.drawCellProgram(&gl_state);
// Draw our custom shaders
try self.drawCustomPrograms(&gl_state);
// Swap our window buffers
switch (apprt.runtime) {
apprt.glfw => surface.window.swapBuffers(),
@ -1418,6 +1421,20 @@ pub fn drawFrame(self: *OpenGL, surface: *apprt.Surface) !void {
}
}
fn drawCustomPrograms(
self: *OpenGL,
gl_state: *const GLState,
) !void {
_ = self;
for (gl_state.custom_programs) |program| {
// 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);
}
}
/// Runs the cell program (shaders) to draw the terminal grid.
fn drawCellProgram(
self: *OpenGL,

View File

@ -84,3 +84,32 @@ pub fn deinit(self: CustomProgram) void {
self.vao.destroy();
self.program.destroy();
}
pub fn bind(self: CustomProgram) !Binding {
const program = try self.program.use();
errdefer program.unbind();
const vao = try self.vao.bind();
errdefer vao.unbind();
const ebo = try self.ebo.bind(.element_array);
errdefer ebo.unbind();
return .{
.program = program,
.vao = vao,
.ebo = ebo,
};
}
pub const Binding = struct {
program: gl.Program.Binding,
vao: gl.VertexArray.Binding,
ebo: gl.Buffer.Binding,
pub fn unbind(self: Binding) void {
self.ebo.unbind();
self.vao.unbind();
self.program.unbind();
}
};

View File

@ -2,7 +2,7 @@
void main(){
vec2 position;
position.x = (gl_VertexID == 0 || gl_VertexID == 1) ? 1. : 0.;
position.y = (gl_VertexID == 0 || gl_VertexID == 3) ? 0. : 1.;
position.x = (gl_VertexID == 0 || gl_VertexID == 1) ? -1. : 1.;
position.y = (gl_VertexID == 0 || gl_VertexID == 3) ? 1. : -1.;
gl_Position = vec4(position.xy, 0.0f, 1.0f);
}

View File

@ -3,6 +3,10 @@
layout(location = 0) out vec4 out_FragColor;
void main() {
// read
out_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
// red
//out_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
// maze
vec4 I = gl_FragCoord;
out_FragColor = vec4(3)*modf(I*.1,I)[int(length(I)*1e4)&1];
}