renderer/opengl: increment time uniform

This commit is contained in:
Mitchell Hashimoto
2023-11-17 16:53:26 -08:00
parent e0afa442c4
commit 2559d6b367
2 changed files with 19 additions and 9 deletions

View File

@ -1405,13 +1405,15 @@ pub fn drawFrame(self: *OpenGL, surface: *apprt.Surface) !void {
// If we're in single-threaded more we grab a lock since we use shared data.
if (single_threaded_draw) self.draw_mutex.lock();
defer if (single_threaded_draw) self.draw_mutex.unlock();
const gl_state = self.gl_state orelse return;
const gl_state: *GLState = if (self.gl_state) |*v| v else return;
// Draw our terminal cells
try self.drawCellProgram(&gl_state);
try self.drawCellProgram(gl_state);
// Draw our custom shaders
try self.drawCustomPrograms(&gl_state);
if (gl_state.custom) |*custom_state| {
try self.drawCustomPrograms(custom_state);
}
// Swap our window buffers
switch (apprt.runtime) {
@ -1423,20 +1425,17 @@ pub fn drawFrame(self: *OpenGL, surface: *apprt.Surface) !void {
fn drawCustomPrograms(
self: *OpenGL,
gl_state: *const GLState,
custom_state: *custom.State,
) !void {
_ = self;
// If we have no custom shaders then we do nothing.
const custom_state = gl_state.custom orelse return;
// Bind our state that is global to all custom shaders
const custom_bind = try custom_state.bind();
defer custom_bind.unbind();
// Sync the uniform data.
// TODO: only do this when the data has changed
try custom_state.syncUniforms();
try custom_state.newFrame();
// Go through each custom shader and draw it.
for (custom_state.programs) |program| {

View File

@ -93,7 +93,18 @@ pub const State = struct {
self.vao.destroy();
}
pub fn syncUniforms(self: *const State) !void {
/// Call this prior to drawing a frame to update the time
/// and synchronize the uniforms. This synchronizes uniforms
/// so you should make changes to uniforms prior to calling
/// 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));
self.uniforms.time = since_ns / std.time.ns_per_s;
self.uniforms.time_delta = since_ns / std.time.ns_per_s;
// Sync our uniform changes
var ubobind = try self.ubo.bind(.uniform);
defer ubobind.unbind();
try ubobind.setData(self.uniforms, .static_draw);