renderer/opengl: some comments

This commit is contained in:
Mitchell Hashimoto
2023-11-17 20:59:20 -08:00
parent a64d12d3cb
commit 2db36646ac
3 changed files with 28 additions and 40 deletions

View File

@ -1438,6 +1438,7 @@ pub fn drawFrame(self: *OpenGL, surface: *apprt.Surface) !void {
} }
} }
/// Draw the custom shaders.
fn drawCustomPrograms( fn drawCustomPrograms(
self: *OpenGL, self: *OpenGL,
custom_state: *custom.State, custom_state: *custom.State,
@ -1456,13 +1457,7 @@ fn drawCustomPrograms(
// Bind our cell program state, buffers // Bind our cell program state, buffers
const bind = try program.bind(); const bind = try program.bind();
defer bind.unbind(); defer bind.unbind();
try bind.draw();
try gl.drawElementsInstanced(
gl.c.GL_TRIANGLES,
6,
gl.c.GL_UNSIGNED_BYTE,
1,
);
} }
} }

View File

@ -10,6 +10,7 @@ const log = std.log.scoped(.opengl_custom);
const UNIFORM_INDEX: gl.c.GLuint = 0; const UNIFORM_INDEX: gl.c.GLuint = 0;
const UNIFORM_BINDING: gl.c.GLuint = 0; const UNIFORM_BINDING: gl.c.GLuint = 0;
/// Global uniforms for custom shaders.
pub const Uniforms = extern struct { pub const Uniforms = extern struct {
resolution: [3]f32 align(16) = .{ 0, 0, 0 }, resolution: [3]f32 align(16) = .{ 0, 0, 0 },
time: f32 align(4) = 1, time: f32 align(4) = 1,
@ -23,7 +24,13 @@ pub const Uniforms = extern struct {
sample_rate: f32 align(4) = 1, 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 { pub const State = struct {
/// The uniform data /// The uniform data
uniforms: Uniforms, uniforms: Uniforms,
@ -40,12 +47,14 @@ pub const State = struct {
/// The last time the frame was drawn. This is used to update /// The last time the frame was drawn. This is used to update
/// the time uniform. /// the time uniform.
last_frame_time: std.time.Instant, first_frame_time: std.time.Instant,
pub fn init( pub fn init(
alloc: Allocator, alloc: Allocator,
srcs: []const [:0]const u8, srcs: []const [:0]const u8,
) !State { ) !State {
if (srcs.len == 0) return error.OneCustomShaderRequired;
// Create our programs // Create our programs
var programs = std.ArrayList(Program).init(alloc); var programs = std.ArrayList(Program).init(alloc);
defer programs.deinit(); defer programs.deinit();
@ -126,7 +135,7 @@ pub const State = struct {
.vao = vao, .vao = vao,
.ebo = ebo, .ebo = ebo,
.fb_texture = fb_tex, .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. /// this.
pub fn newFrame(self: *State) !void { pub fn newFrame(self: *State) !void {
// Update our frame time // Update our frame time
const now = std.time.Instant.now() catch self.last_frame_time; const now = std.time.Instant.now() catch self.first_frame_time;
const since_ns: f32 = @floatFromInt(now.since(self.last_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 = since_ns / std.time.ns_per_s;
self.uniforms.time_delta = 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(); 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 { pub fn bind(self: *const Program) !Binding {
const program = try self.program.use(); const program = try self.program.use();
errdefer program.unbind(); errdefer program.unbind();
@ -264,5 +275,15 @@ pub const Program = struct {
pub fn unbind(self: Binding) void { pub fn unbind(self: Binding) void {
self.program.unbind(); self.program.unbind();
} }
pub fn draw(self: Binding) !void {
_ = self;
try gl.drawElementsInstanced(
gl.c.GL_TRIANGLES,
6,
gl.c.GL_UNSIGNED_BYTE,
1,
);
}
}; };
}; };

View File

@ -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];
}