Stop sleeping, I guess
This commit is contained in:
+21
-58
@@ -948,8 +948,9 @@ pub fn main(init: std.process.Init) u8 {
|
|||||||
const monitor_update_hz: f64 = 30;
|
const monitor_update_hz: f64 = 30;
|
||||||
const game_update_hz: f64 = monitor_update_hz;
|
const game_update_hz: f64 = monitor_update_hz;
|
||||||
const frame_target_time_s: f64 = 1.0 / game_update_hz;
|
const frame_target_time_s: f64 = 1.0 / game_update_hz;
|
||||||
const frame_target_time_ms: f64 = std.time.ms_per_s * frame_target_time_s;
|
// const frame_target_time_ms: f64 = std.time.ms_per_s * frame_target_time_s;
|
||||||
const frame_target_time_ns = std.Io.Duration.fromNanoseconds(std.time.ns_per_ms * @as(u64, @intFromFloat(frame_target_time_ms)));
|
// const frame_target_time_ns = std.Io.Duration.fromNanoseconds(std.time.ns_per_ms * @as(u64, @intFromFloat(frame_target_time_ms)));
|
||||||
|
// const frame_target_time = std.Io.Duration.fromNanoseconds(@intFromFloat(std.time.ns_per_s * frame_target_time_s));
|
||||||
|
|
||||||
// Memory
|
// Memory
|
||||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||||
@@ -973,7 +974,7 @@ pub fn main(init: std.process.Init) u8 {
|
|||||||
std.debug.print("[Wayland] Added frame callback with ret {d}.\n", .{ret});
|
std.debug.print("[Wayland] Added frame callback with ret {d}.\n", .{ret});
|
||||||
}
|
}
|
||||||
|
|
||||||
var start = std.Io.Clock.Timestamp.now(io, clock);
|
var start = clock.now(io);
|
||||||
|
|
||||||
var frame_count: u32 = 0;
|
var frame_count: u32 = 0;
|
||||||
|
|
||||||
@@ -985,41 +986,7 @@ pub fn main(init: std.process.Init) u8 {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
while (wl_state.running) {
|
while (wl_state.running) {
|
||||||
// pollEvents(&wl_state, &fds);
|
pollEvents(&wl_state, &fds);
|
||||||
var count: u32 = 0;
|
|
||||||
outer: while (true) {
|
|
||||||
const cont = std.posix.poll(&fds, 0) catch |err| {
|
|
||||||
switch (err) {
|
|
||||||
error.Unexpected => {
|
|
||||||
std.debug.print("Poll error: Unexpected.\n", .{});
|
|
||||||
},
|
|
||||||
error.SystemResources => {
|
|
||||||
std.debug.print("Poll error: SystemResources: Kernel has no space for table allocations.\n", .{});
|
|
||||||
},
|
|
||||||
else => {
|
|
||||||
std.debug.print("Poll error: network stuff idk.\n", .{});
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (cont == 0) break;
|
|
||||||
|
|
||||||
for (fds) |fd| {
|
|
||||||
if (fd.revents & std.posix.POLL.IN > 0) {
|
|
||||||
const ret = c.wl_display_dispatch(wl_state.display);
|
|
||||||
count += 1;
|
|
||||||
if (ret < 0) {
|
|
||||||
std.debug.print("Dispatch error {d}.\n", .{ret});
|
|
||||||
wl_state.running = false;
|
|
||||||
break :outer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std.debug.print("[Wayland] Polled and dispatched {d} events.\n", .{count});
|
|
||||||
|
|
||||||
if (wl_state.resize) {
|
if (wl_state.resize) {
|
||||||
const new_pool = createSharedMemoryPool(wl_state) catch {
|
const new_pool = createSharedMemoryPool(wl_state) catch {
|
||||||
@@ -1077,23 +1044,21 @@ pub fn main(init: std.process.Init) u8 {
|
|||||||
const render_end_time = clock.now(io);
|
const render_end_time = clock.now(io);
|
||||||
const render_time = render_start_time.durationTo(render_end_time);
|
const render_time = render_start_time.durationTo(render_end_time);
|
||||||
|
|
||||||
std.debug.print("[Wayland] Render: {f}ms.\n", .{render_time});
|
std.debug.print("[Wayland] Render: {f}.\n", .{render_time});
|
||||||
|
|
||||||
var sleep_time = std.Io.Timestamp.now(io, clock);
|
// if (render_time.toNanoseconds() < frame_target_time_ns.toNanoseconds()) {
|
||||||
sleep_time = sleep_time.addDuration(frame_target_time_ns);
|
// const sleep_duration: std.Io.Clock.Duration = .{
|
||||||
if (render_time.toNanoseconds() < frame_target_time_ns.toNanoseconds()) {
|
// .raw = .{ .nanoseconds = frame_target_time.nanoseconds - render_time.nanoseconds },
|
||||||
sleep_time = sleep_time.subDuration(render_time);
|
// .clock = clock,
|
||||||
std.debug.print("[Wayland] Sleeping: {d}ms.\n", .{sleep_time});
|
// };
|
||||||
const with_clock = std.Io.Timestamp.withClock(sleep_time, clock);
|
// std.debug.print("[Wayland] Sleeping: {f}.\n", .{sleep_duration.raw});
|
||||||
const timeout = std.Io.Timeout{ .deadline = with_clock };
|
// sleep_duration.sleep(io) catch {
|
||||||
timeout.sleep(io) catch {
|
// std.debug.print("[Wayland] Error sleeping\n", .{});
|
||||||
std.debug.print("[Wayland] Error sleeping\n", .{});
|
// return 1;
|
||||||
return 1;
|
// };
|
||||||
};
|
// } else {
|
||||||
} else {
|
// std.debug.print("[Wayland] MISSED FRAME\n", .{});
|
||||||
sleep_time = std.Io.Timestamp.zero;
|
// }
|
||||||
std.debug.print("[Wayland] MISSED FRAME\n", .{});
|
|
||||||
}
|
|
||||||
|
|
||||||
c.wl_surface_attach(wl_state.surface, wl_state.buffer, 0, 0);
|
c.wl_surface_attach(wl_state.surface, wl_state.buffer, 0, 0);
|
||||||
c.wl_surface_damage_buffer(wl_state.surface, 0, 0, wl_state.window_width, wl_state.window_height);
|
c.wl_surface_damage_buffer(wl_state.surface, 0, 0, wl_state.window_width, wl_state.window_height);
|
||||||
@@ -1103,16 +1068,14 @@ pub fn main(init: std.process.Init) u8 {
|
|||||||
|
|
||||||
wl_state.frame_commited = false;
|
wl_state.frame_commited = false;
|
||||||
|
|
||||||
// std.posix.nanosleep(0, std.time.ns_per_ms * 32);
|
const last = clock.now(io);
|
||||||
|
|
||||||
const last = std.Io.Clock.Timestamp.now(io, clock);
|
|
||||||
const current = start.durationTo(last);
|
const current = start.durationTo(last);
|
||||||
start = last;
|
start = last;
|
||||||
// std.debug.print(
|
// std.debug.print(
|
||||||
// "[Wayland] Render: {d:2.3}ms, Sleeping: {d:2.3}ms, Frame: {d:.3}ms.\n",
|
// "[Wayland] Render: {d:2.3}ms, Sleeping: {d:2.3}ms, Frame: {d:.3}ms.\n",
|
||||||
// .{ render_time_ms, sleep_time_ms, current_ms },
|
// .{ render_time_ms, sleep_time_ms, current_ms },
|
||||||
// );
|
// );
|
||||||
std.debug.print("[Wayland] Frame done: {f}ms.\n", .{current.raw});
|
std.debug.print("[Wayland] Frame done: {f}.\n", .{current});
|
||||||
|
|
||||||
frame_count += 1;
|
frame_count += 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user