Skip to content

PS2 games in the browser: static recompilation to WebAssembly with a native runtime

C++

Web: present a frame only after the previous one reached the screen

Samir Nurid87a21cbe39d
diff --git a/.gitignore b/.gitignore
index 13a4aa6..4b45721 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,7 +7,6 @@
*.rar
/generated/
-
/tools/
/build/
diff --git a/host/web.cpp b/host/web.cpp
index c2539ee..36dc962 100644
--- a/host/web.cpp
+++ b/host/web.cpp
@@ -80,7 +80,24 @@ EM_JS(double, ps2web_url_read, (const char *url, std::uint8_t *out, double size)
}
});
EM_JS(char *, ps2web_disc_url, (), { return stringToNewUTF8(new URL(Module.discUrl, location.href).href); });
-EM_JS(void, ps2web_frame_done, (), { if (Module.onFrame) Module.onFrame(); });
+EM_JS(void, ps2web_frame_done, (), {
+ if (!Module.shownChannel) {
+ Module.shownChannel = new MessageChannel();
+ Module.shownWaiters = [];
+ Module.shownChannel.port1.onmessage = () => {
+ Module.shownPending = false;
+ const waiters = Module.shownWaiters;
+ Module.shownWaiters = [];
+ for (const resolve of waiters) resolve();
+ };
+ }
+ Module.shownPending = true;
+ requestAnimationFrame(() => Module.shownChannel.port2.postMessage(0));
+ if (Module.onFrame) Module.onFrame();
+});
+EM_ASYNC_JS(void, ps2web_wait_shown, (), {
+ if (Module.shownPending) await new Promise((resolve) => Module.shownWaiters.push(resolve));
+});
EM_JS(void, ps2web_blit, (const std::uint32_t *rgba, int width, int height), {
const canvas = Module.canvas;
if (!Module.blitContext) Module.blitContext = canvas.getContext('2d');
@@ -376,10 +393,28 @@ int main(int argc, char **argv) {
ps2web_blit(rgba, static_cast<int>(width), static_cast<int>(height));
};
const std::function<void(const std::uint32_t *, std::uint32_t, std::uint32_t)> none;
+ const std::function<void()> wait_shown = [] {
+ static int since_skip = 0;
+ static double window_start = 0.0, window_gpu = 0.0, gpu_load = 1.0;
+ const double start = emscripten_get_now();
+ if (start - window_start > 500.0) {
+ const double gpu = ps2::gpu_busy_ms();
+ gpu_load = window_start > 0.0 ? (gpu - window_gpu) / (start - window_start) : 1.0;
+ window_start = start;
+ window_gpu = gpu;
+ }
+ if (++since_skip > 8 && gpu_load < 0.8 && g_audio.active.load() && audio_queued_ms() < 40.0) {
+ since_skip = 0;
+ return;
+ }
+ ps2web_wait_shown();
+ g_render_idle.store(g_render_idle.load() + emscripten_get_now() - start);
+ };
for (;;) {
service_disc();
- if (ps2::render_pending(surface, copy ? blit : none)) {
- ps2web_frame_done();
+ const ps2::Rendered rendered = ps2::render_pending(surface, copy ? blit : none, wait_shown, 6.0);
+ if (rendered == ps2::Rendered::presented) ps2web_frame_done();
+ if (rendered != ps2::Rendered::idle) {
ps2::browser_yield();
} else {
const double start = emscripten_get_now();
diff --git a/runtime/include/ps2/system.hpp b/runtime/include/ps2/system.hpp
index 89b253d..91caade 100644
--- a/runtime/include/ps2/system.hpp
+++ b/runtime/include/ps2/system.hpp
@@ -99,7 +99,9 @@ void run_vu_thread();
double vu_thread_idle_ms();
std::uint64_t vu1_interpreted_calls();
void post_present();
-bool render_pending(bool surface, const std::function<void(const std::uint32_t *rgba, std::uint32_t width, std::uint32_t height)> &copy);
+enum class Rendered { idle, presented, busy };
+Rendered render_pending(bool surface, const std::function<void(const std::uint32_t *rgba, std::uint32_t width, std::uint32_t height)> &copy,
+ const std::function<void()> &before_present, double budget_ms);
void wait_render_work(int timeout_ms);
void wake_render_thread();
bool copy_frame(std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32_t &height);
diff --git a/runtime/src/gs/gs.cpp b/runtime/src/gs/gs.cpp
index 3a6212e..d83f884 100644
--- a/runtime/src/gs/gs.cpp
+++ b/runtime/src/gs/gs.cpp
@@ -12,6 +12,7 @@
#include <algorithm>
#include <atomic>
+#include <chrono>
#include <stdexcept>
#include <string>
#include <functional>
@@ -850,11 +851,13 @@ void post_present() {
for (std::uint32_t done = g_presented.load(); g_posted_presents - done > 2; done = g_presented.load()) queue::wait(g_presented, done);
}
-bool render_pending(const std::function<void(const DisplayRegs &)> &on_present) {
+Rendered render_pending(const std::function<void(const DisplayRegs &)> &on_present, double budget_ms) {
queue::role() = queue::Role::Render;
queue::Header h;
const std::uint8_t *payload = nullptr;
- while (queue::render().peek(h, payload)) {
+ const auto start = std::chrono::steady_clock::now();
+ for (std::uint32_t n = 1; queue::render().peek(h, payload); ++n) {
+ if (!(n & 7u) && std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - start).count() > budget_ms) return Rendered::busy;
bool presented = false;
switch (h.type) {
case queue::kGif: gif::render(static_cast<int>(h.arg0), payload, h.arg1); break;
@@ -888,10 +891,10 @@ bool render_pending(const std::function<void(const DisplayRegs &)> &on_present)
if (presented) {
g_presented.fetch_add(1);
queue::notify(g_presented);
- return true;
+ return Rendered::presented;
}
}
- return false;
+ return Rendered::idle;
}
std::uint64_t primitives_drawn() { return g_primitives; }
diff --git a/runtime/src/gs/gs.hpp b/runtime/src/gs/gs.hpp
index c7a457c..c6a0a73 100644
--- a/runtime/src/gs/gs.hpp
+++ b/runtime/src/gs/gs.hpp
@@ -1,6 +1,7 @@
#pragma once
#include "ps2/cpu.hpp"
+#include "ps2/system.hpp"
#include <cstdint>
#include <functional>
@@ -83,7 +84,7 @@ bool present(const DisplayRegs &regs);
void start_thread();
void flush_and_wait();
void post_present();
-bool render_pending(const std::function<void(const DisplayRegs &)> &on_present);
+Rendered render_pending(const std::function<void(const DisplayRegs &)> &on_present, double budget_ms);
double gpu_busy_ms();
std::uint64_t primitives_drawn();
void stats();
diff --git a/runtime/src/system.cpp b/runtime/src/system.cpp
index 86fdf0f..e6e6a52 100644
--- a/runtime/src/system.cpp
+++ b/runtime/src/system.cpp
@@ -454,16 +454,18 @@ void run_vu_thread() {
}
void post_present() { gs::post_present(); }
-bool render_pending(bool surface, const std::function<void(const std::uint32_t *, std::uint32_t, std::uint32_t)> &copy) {
+Rendered render_pending(bool surface, const std::function<void(const std::uint32_t *, std::uint32_t, std::uint32_t)> &copy,
+ const std::function<void()> &before_present, double budget_ms) {
static std::vector<std::uint32_t> frame;
return gs::render_pending([&](const gs::DisplayRegs &regs) {
+ if (before_present) before_present();
if (surface) {
gs::present(regs);
return;
}
std::uint32_t width = 0, height = 0;
if (copy && gs::copy_frame(regs, frame, width, height)) copy(frame.data(), width, height);
- });
+ }, budget_ms);
}
void wait_render_work(int timeout_ms) { gs::queue::render().wait_for_work(timeout_ms); }
diff --git a/web/shell.html b/web/shell.html
index 6c9f8e5..cc7320b 100644
--- a/web/shell.html
+++ b/web/shell.html
@@ -504,6 +504,13 @@ async function chooseGpu() {
}
let frames = 0, last = performance.now(), lastGpu = 0, lastEmuIdle = 0, lastRenderIdle = 0, lastVuIdle = 0, lastInterpreted = 0;
+let shown = 0, fresh = false;
+const countShown = () => {
+ if (fresh) ++shown;
+ fresh = false;
+ requestAnimationFrame(countShown);
+};
+requestAnimationFrame(countShown);
var Module = {
canvas,
arguments: ['/game/core.elf'].concat(params.has('arg') ? ['--arg', params.get('arg')] : [], params.getAll('press').flatMap((p) => p.split(',').flatMap((x) => ['--press', x]))),
@@ -559,13 +566,14 @@ var Module = {
onFrame: () => {
pollInput();
++frames;
+ fresh = true;
const now = performance.now();
if (now - last >= 1000) {
const gpu = Module._ps2web_gpu_ms();
const memory = (Module.HEAPU8.length / 1048576).toFixed(0);
const emuIdle = Module._ps2web_idle_ms(0), renderIdle = Module._ps2web_idle_ms(1), vuIdle = Module._ps2web_idle_ms(2), span = now - last;
const interpreted = Module._ps2web_vu_interpreted();
- statsEl.textContent = 'frame ' + Module._ps2web_frames() + ' ' + (frames * 1000 / span).toFixed(1) + ' fps GPU ' + ((gpu - lastGpu) / span * 100).toFixed(0) +
+ statsEl.textContent = 'frame ' + Module._ps2web_frames() + ' ' + (frames * 1000 / span).toFixed(1) + ' fps (' + (shown * 1000 / span).toFixed(0) + ' on screen) GPU ' + ((gpu - lastGpu) / span * 100).toFixed(0) +
'% idle emu ' + ((emuIdle - lastEmuIdle) / span * 100).toFixed(0) + '% vu ' + ((vuIdle - lastVuIdle) / span * 100).toFixed(0) +
'% gs ' + ((renderIdle - lastRenderIdle) / span * 100).toFixed(0) +
'% heap ' + memory + ' MB audio ' + Module.audioQueuedMs().toFixed(0) + ' ms' +
@@ -576,6 +584,7 @@ var Module = {
lastVuIdle = vuIdle;
lastGpu = gpu;
frames = 0;
+ shown = 0;
last = now;
}
},