From 18005ffa35de4be29b0b99bdbd237cea82e56acf Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Sun, 4 Feb 2024 22:50:07 -0600 Subject: [PATCH] Fix shader time uniforms (#1462) * fix shader time uniforms * renderer/metal: one typo --------- Co-authored-by: Mitchell Hashimoto --- src/renderer/Metal.zig | 14 +++++++++++--- src/renderer/opengl/custom.zig | 11 +++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/renderer/Metal.zig b/src/renderer/Metal.zig index 2f938dad8..0866e9a50 100644 --- a/src/renderer/Metal.zig +++ b/src/renderer/Metal.zig @@ -137,6 +137,11 @@ pub const CustomShaderState = struct { screen_texture: objc.Object, // MTLTexture sampler: mtl_sampler.Sampler, uniforms: mtl_shaders.PostUniforms, + /// The first time a frame was drawn. This is used to update the time + /// uniform. + first_frame_time: std.time.Instant, + /// The last time a frame was drawn. This is used to update the time + /// uniform. last_frame_time: std.time.Instant, pub fn deinit(self: *CustomShaderState) void { @@ -412,6 +417,7 @@ pub fn init(alloc: Allocator, options: renderer.Options) !Metal { .sample_rate = 1, }, + .first_frame_time = try std.time.Instant.now(), .last_frame_time = try std.time.Instant.now(), }; }; @@ -757,10 +763,12 @@ pub fn drawFrame(self: *Metal, surface: *apprt.Surface) !void { // If we have custom shaders, update the animation time. if (self.custom_shader_state) |*state| { - const now = std.time.Instant.now() catch state.last_frame_time; - const since_ns: f32 = @floatFromInt(now.since(state.last_frame_time)); + const now = std.time.Instant.now() catch state.first_frame_time; + const since_ns: f32 = @floatFromInt(now.since(state.first_frame_time)); + const delta_ns: f32 = @floatFromInt(now.since(state.last_frame_time)); state.uniforms.time = since_ns / std.time.ns_per_s; - state.uniforms.time_delta = since_ns / std.time.ns_per_s; + state.uniforms.time_delta = delta_ns / std.time.ns_per_s; + state.last_frame_time = now; } // @autoreleasepool {} diff --git a/src/renderer/opengl/custom.zig b/src/renderer/opengl/custom.zig index c14ba3c5c..c988b62d0 100644 --- a/src/renderer/opengl/custom.zig +++ b/src/renderer/opengl/custom.zig @@ -45,10 +45,14 @@ pub const State = struct { /// The set of programs for the custom shaders. programs: []const Program, - /// The last time the frame was drawn. This is used to update + /// The first time a frame was drawn. This is used to update /// the time uniform. first_frame_time: std.time.Instant, + /// The last time a frame was drawn. This is used to update + /// the time uniform. + last_frame_time: std.time.Instant, + pub fn init( alloc: Allocator, srcs: []const [:0]const u8, @@ -136,6 +140,7 @@ pub const State = struct { .ebo = ebo, .fb_texture = fb_tex, .first_frame_time = try std.time.Instant.now(), + .last_frame_time = try std.time.Instant.now(), }; } @@ -180,8 +185,10 @@ pub const State = struct { // Update our frame time const now = std.time.Instant.now() catch self.first_frame_time; const since_ns: f32 = @floatFromInt(now.since(self.first_frame_time)); + const delta_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; + self.uniforms.time_delta = delta_ns / std.time.ns_per_s; + self.last_frame_time = now; // Sync our uniform changes try self.syncUniforms();