mach: introduce cross platform Timer abstraction

This Timer uses std.time.Timer as backing timer in native platforms, and
will use custom timers for special platforms (wasm, android?, ios?).

Unlike std.time.Timer, its primary API is focused on floats. Also meant
to provides some convenient functions alongside base ones.

Follows std.time.Timer API, but methods by default return f32 i.e
non-precise variant with precise variants available returning u64.
This commit is contained in:
iddev5 2022-05-17 13:20:19 +05:30 committed by Stephen Gutekanst
parent be935c64ef
commit 3bb45c75a1
9 changed files with 63 additions and 20 deletions

View file

@ -22,7 +22,7 @@ const UniformBufferObject = struct {
mat: zm.Mat,
};
var timer: std.time.Timer = undefined;
var timer: mach.Timer = undefined;
pipeline: gpu.RenderPipeline,
queue: gpu.Queue,
@ -38,7 +38,7 @@ sampler: gpu.Sampler,
bgl: gpu.BindGroupLayout,
pub fn init(app: *App, engine: *mach.Engine) !void {
timer = try std.time.Timer.start();
timer = try mach.Timer.start();
engine.core.setKeyCallback(struct {
fn callback(_: *App, eng: *mach.Engine, key: mach.Key, action: mach.Action) void {
@ -269,7 +269,7 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
};
{
const time = @intToFloat(f32, timer.read()) / @as(f32, std.time.ns_per_s);
const time = timer.read();
const model = zm.mul(zm.rotationX(time * (std.math.pi / 2.0)), zm.rotationZ(time * (std.math.pi / 2.0)));
const view = zm.lookAtRh(
zm.f32x4(0, -4, 0, 1),

View file

@ -10,7 +10,7 @@ const UniformBufferObject = struct {
mat: zm.Mat,
};
var timer: std.time.Timer = undefined;
var timer: mach.Timer = undefined;
pipeline: gpu.RenderPipeline,
queue: gpu.Queue,
@ -21,7 +21,7 @@ bind_group: gpu.BindGroup,
const App = @This();
pub fn init(app: *App, engine: *mach.Engine) !void {
timer = try std.time.Timer.start();
timer = try mach.Timer.start();
engine.core.setKeyCallback(struct {
fn callback(_: *App, eng: *mach.Engine, key: mach.Key, action: mach.Action) void {
@ -171,7 +171,7 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
);
var ubos: [16]UniformBufferObject = undefined;
const time = @intToFloat(f32, timer.read()) / @as(f32, std.time.ns_per_s);
const time = timer.read();
const step: f32 = 4.0;
var m: u8 = 0;
var x: u8 = 0;

View file

@ -12,7 +12,7 @@ const UniformBufferObject = struct {
mat: zm.Mat,
};
var timer: std.time.Timer = undefined;
var timer: mach.Timer = undefined;
pipeline: gpu.RenderPipeline,
queue: gpu.Queue,
@ -21,7 +21,7 @@ uniform_buffer: gpu.Buffer,
bind_group: gpu.BindGroup,
pub fn init(app: *App, engine: *mach.Engine) !void {
timer = try std.time.Timer.start();
timer = try mach.Timer.start();
// TODO: higher level input handlers
engine.core.setKeyCallback(struct {
@ -173,7 +173,7 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
};
{
const time = @intToFloat(f32, timer.read()) / @as(f32, std.time.ns_per_s);
const time = timer.read();
const model = zm.mul(zm.rotationX(time * (std.math.pi / 2.0)), zm.rotationZ(time * (std.math.pi / 2.0)));
const view = zm.lookAtRh(
zm.f32x4(0, 4, 2, 1),

View file

@ -11,7 +11,7 @@ const UniformBufferObject = struct {
mat: zm.Mat,
};
var timer: std.time.Timer = undefined;
var timer: mach.Timer = undefined;
pipeline: gpu.RenderPipeline,
queue: gpu.Queue,
@ -24,7 +24,7 @@ depth_size: mach.Size,
const App = @This();
pub fn init(app: *App, engine: *mach.Engine) !void {
timer = try std.time.Timer.start();
timer = try mach.Timer.start();
engine.core.setKeyCallback(struct {
fn callback(_: *App, eng: *mach.Engine, key: mach.Key, action: mach.Action) void {
@ -239,7 +239,7 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
};
{
const time = @intToFloat(f32, timer.read()) / @as(f32, std.time.ns_per_s);
const time = timer.read();
const model = zm.mul(zm.rotationX(time * (std.math.pi / 2.0)), zm.rotationZ(time * (std.math.pi / 2.0)));
const view = zm.lookAtRh(
zm.f32x4(0, 4, 2, 1),

View file

@ -10,7 +10,7 @@ const UniformBufferObject = struct {
mat: zm.Mat,
};
var timer: std.time.Timer = undefined;
var timer: mach.Timer = undefined;
pipeline: gpu.RenderPipeline,
queue: gpu.Queue,
@ -22,7 +22,7 @@ bind_group2: gpu.BindGroup,
const App = @This();
pub fn init(app: *App, engine: *mach.Engine) !void {
timer = try std.time.Timer.start();
timer = try mach.Timer.start();
engine.core.setKeyCallback(struct {
fn callback(_: *App, eng: *mach.Engine, key: mach.Key, action: mach.Action) void {
@ -189,7 +189,7 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
};
{
const time = @intToFloat(f32, timer.read()) / @as(f32, std.time.ns_per_s);
const time = timer.read();
const rotation1 = zm.mul(zm.rotationX(time * (std.math.pi / 2.0)), zm.rotationZ(time * (std.math.pi / 2.0)));
const rotation2 = zm.mul(zm.rotationZ(time * (std.math.pi / 2.0)), zm.rotationX(time * (std.math.pi / 2.0)));
const model1 = zm.mul(rotation1, zm.translation(-2, 0, 0));