Fix shader time uniforms (#1462)

* fix shader time uniforms

* renderer/metal: one typo

---------

Co-authored-by: Mitchell Hashimoto <mitchell.hashimoto@gmail.com>
This commit is contained in:
Jeffrey C. Ollie
2024-02-04 22:50:07 -06:00
committed by GitHub
parent efb4eab44b
commit 18005ffa35
2 changed files with 20 additions and 5 deletions

View File

@ -137,6 +137,11 @@ pub const CustomShaderState = struct {
screen_texture: objc.Object, // MTLTexture screen_texture: objc.Object, // MTLTexture
sampler: mtl_sampler.Sampler, sampler: mtl_sampler.Sampler,
uniforms: mtl_shaders.PostUniforms, 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, last_frame_time: std.time.Instant,
pub fn deinit(self: *CustomShaderState) void { pub fn deinit(self: *CustomShaderState) void {
@ -412,6 +417,7 @@ pub fn init(alloc: Allocator, options: renderer.Options) !Metal {
.sample_rate = 1, .sample_rate = 1,
}, },
.first_frame_time = try std.time.Instant.now(),
.last_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 we have custom shaders, update the animation time.
if (self.custom_shader_state) |*state| { if (self.custom_shader_state) |*state| {
const now = std.time.Instant.now() catch state.last_frame_time; const now = std.time.Instant.now() catch state.first_frame_time;
const since_ns: f32 = @floatFromInt(now.since(state.last_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 = 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 {} // @autoreleasepool {}

View File

@ -45,10 +45,14 @@ pub const State = struct {
/// The set of programs for the custom shaders. /// The set of programs for the custom shaders.
programs: []const Program, 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. /// the time uniform.
first_frame_time: std.time.Instant, 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( pub fn init(
alloc: Allocator, alloc: Allocator,
srcs: []const [:0]const u8, srcs: []const [:0]const u8,
@ -136,6 +140,7 @@ pub const State = struct {
.ebo = ebo, .ebo = ebo,
.fb_texture = fb_tex, .fb_texture = fb_tex,
.first_frame_time = try std.time.Instant.now(), .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 // Update our frame time
const now = std.time.Instant.now() catch self.first_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 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 = 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 // Sync our uniform changes
try self.syncUniforms(); try self.syncUniforms();