PS2 games in the browser: static recompilation to WebAssembly with a native runtime
C++
web: copy frames to the 2D canvas without waiting for the GPU
Samir Nurie15fb1bf5b22
diff --git a/host/web.cpp b/host/web.cppindex 881e89d..5d6790b 100644--- a/host/web.cpp+++ b/host/web.cpp@@ -122,7 +122,7 @@ void on_vblank() {}if (g_window) ps2::present_frame();std::uint32_t w = 0, h = 0;- if (g_copy && ps2::display_frame(g_frame, w, h)) ps2web_blit(g_frame.data(), static_cast<int>(w), static_cast<int>(h));+ if (g_copy && ps2::copy_frame(g_frame, w, h)) ps2web_blit(g_frame.data(), static_cast<int>(w), static_cast<int>(h));if (EM_ASM_INT({ return Module.wantSnapshot ? 1 : 0; })) {std::vector<std::uint32_t> rgba;std::uint32_t w = 0, h = 0;diff --git a/runtime/include/ps2/system.hpp b/runtime/include/ps2/system.hppindex 960f285..b31d4cc 100644--- a/runtime/include/ps2/system.hpp+++ b/runtime/include/ps2/system.hpp@@ -84,6 +84,8 @@ struct WindowTarget {bool attach_window(const WindowTarget &target);void resize_window(std::uint32_t width, std::uint32_t height);bool present_frame();+// Latest frame for hosts that draw it themselves, fields blended; may lag a frame and is false while none is ready.+bool copy_frame(std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32_t &height);// Parses "frame:button[+button][:frames]" as used by --press.bool parse_press(const std::string &spec, Press &press);// GPU time spent on emulation so far, estimated from timestamp queries (0 when unsupported).diff --git a/runtime/src/gs/gpu/gpu.cpp b/runtime/src/gs/gpu/gpu.cppindex 9de7aa2..ea19707 100644--- a/runtime/src/gs/gpu/gpu.cpp+++ b/runtime/src/gs/gpu/gpu.cpp@@ -1134,6 +1134,77 @@ double busy_ms() {return static_cast<double>(g_measured_ns) / static_cast<double>(g_measured) * static_cast<double>(g_total_passes) / 1e6;}+struct Copy {+ WGPUBuffer buffer = nullptr;+ std::uint64_t size = 0, order = 0;+ std::uint32_t width = 0, height = 0;+ bool busy = false, ready = false;+};+Copy g_copies[3];+std::uint64_t g_copy_order = 0;++bool display_async(const DisplaySource &src, std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32_t &height) {+#ifdef __EMSCRIPTEN__+ wgpuInstanceProcessEvents(g_instance);+#else+ wgpuDevicePoll(g_device, false, nullptr);+#endif+ Copy *newest = nullptr;+ for (Copy &c : g_copies)+ if (c.ready && (!newest || c.order > newest->order)) newest = &c;+ if (newest) {+ const std::uint64_t bytes = static_cast<std::uint64_t>(newest->width) * newest->height * 4;+ const void *mapped = wgpuBufferGetConstMappedRange(newest->buffer, 0, bytes);+ if (mapped) {+ rgba.resize(static_cast<std::size_t>(newest->width) * newest->height);+ std::memcpy(rgba.data(), mapped, bytes);+ width = newest->width;+ height = newest->height;+ }+ for (Copy &c : g_copies)+ if (c.ready && c.order <= newest->order) {+ wgpuBufferUnmap(c.buffer);+ c.ready = c.busy = false;+ }+ if (!mapped) newest = nullptr;+ }+ Copy *slot = nullptr;+ for (Copy &c : g_copies)+ if (!c.busy) slot = &c;+ if (slot) {+ const std::uint64_t bytes = static_cast<std::uint64_t>(src.width) * src.height * 4;+ if (slot->size < bytes) {+ if (slot->buffer) wgpuBufferRelease(slot->buffer);+ slot->size = bytes;+ slot->buffer = make_buffer(bytes, WGPUBufferUsage_MapRead | WGPUBufferUsage_CopyDst);+ }+ flush_all();+ const std::uint32_t param = push_params({src.fbp, src.fbw, src.psm, src.dbx, src.dby, src.width, src.height, src.halved ? 1u : 0u});+ g_commands.push_back({kDisplay, (src.width + 7) / 8, (src.height + 7) / 8, param});+ submit();+ WGPUCommandEncoder enc = wgpuDeviceCreateCommandEncoder(g_device, nullptr);+ wgpuCommandEncoderCopyBufferToBuffer(enc, g_scratch, 0, slot->buffer, 0, bytes);+ WGPUCommandBuffer cmd = wgpuCommandEncoderFinish(enc, nullptr);+ wgpuQueueSubmit(g_queue, 1, &cmd);+ wgpuCommandBufferRelease(cmd);+ wgpuCommandEncoderRelease(enc);+ slot->busy = true;+ slot->order = ++g_copy_order;+ slot->width = src.width;+ slot->height = src.height;+ WGPUBufferMapCallbackInfo cb = WGPU_BUFFER_MAP_CALLBACK_INFO_INIT;+ cb.mode = WGPUCallbackMode_AllowProcessEvents;+ cb.callback = [](WGPUMapAsyncStatus status, WGPUStringView, void *copy, void *) {+ Copy &c = *static_cast<Copy *>(copy);+ if (status == WGPUMapAsyncStatus_Success) c.ready = true;+ else c.busy = false;+ };+ cb.userdata1 = slot;+ wgpuBufferMapAsync(slot->buffer, WGPUMapMode_Read, 0, bytes, cb);+ }+ return newest != nullptr;+}+bool display(const DisplaySource &src, std::vector<std::uint32_t> &rgba) {flush_all();const std::uint32_t param = push_params({src.fbp, src.fbw, src.psm, src.dbx, src.dby, src.width, src.height, src.halved ? 1u : 0u});diff --git a/runtime/src/gs/gpu/gpu.hpp b/runtime/src/gs/gpu/gpu.hppindex fc31458..0f1e97e 100644--- a/runtime/src/gs/gpu/gpu.hpp+++ b/runtime/src/gs/gpu/gpu.hpp@@ -33,6 +33,8 @@ void load_clut(std::uint64_t tex0, std::uint64_t texclut);void flush();void download(std::uint8_t *memory);bool display(const DisplaySource &src, std::vector<std::uint32_t> &rgba);+// Starts a readback of the frame and returns the newest finished one without waiting; false while none is ready.+bool display_async(const DisplaySource &src, std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32_t &height);bool attach_surface(const SurfaceTarget &target);void resize_surface(std::uint32_t width, std::uint32_t height);bool has_surface();diff --git a/runtime/src/gs/gs.cpp b/runtime/src/gs/gs.cppindex dcaf6db..563d7ce 100644--- a/runtime/src/gs/gs.cpp+++ b/runtime/src/gs/gs.cpp@@ -747,6 +747,38 @@ bool display(std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32return true;}+bool copy_frame(std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32_t &height) {+ static std::vector<std::uint32_t> previous;+ bool got = false;+#ifdef PS2_GPU+ if (gpu_on() && !cpu_raster()) {+ const int circuit = (g_pmode & 2u) ? 1 : (g_pmode & 1u) ? 0 : -1;+ if (circuit < 0) return false;+ const std::uint64_t fb = g_dispfb[circuit], disp = g_display[circuit];+ const std::uint32_t magh = (static_cast<std::uint32_t>(disp >> 23) & 0xFu) + 1, magv = (static_cast<std::uint32_t>(disp >> 27) & 3u) + 1;+ const std::uint32_t w = ((static_cast<std::uint32_t>(disp >> 32) & 0xFFFu) + 1) / magh, h = ((static_cast<std::uint32_t>(disp >> 44) & 0x7FFu) + 1) / magv;+ if (!w || !h || w > 2048 || h > 2048) return false;+ const gpu::DisplaySource src{(static_cast<std::uint32_t>(fb) & 0x1FFu) * 32, static_cast<std::uint32_t>(fb >> 9) & 0x3Fu, static_cast<std::uint32_t>(fb >> 15) & 0x1Fu,+ static_cast<std::uint32_t>(fb >> 32) & 0x7FFu, static_cast<std::uint32_t>(fb >> 43) & 0x7FFu, w, h, (g_smode2 & 3u) == 3u};+ got = gpu::display_async(src, rgba, width, height);+ } else+#endif+ got = display(rgba, width, height);+ if (!got) return false;+ if (previous.size() != rgba.size()) {+ previous = rgba;+ return true;+ }+ static const bool weave_off = std::getenv("PS2_NO_DEINTERLACE") != nullptr;+ const bool blend = (g_smode2 & 3u) == 3u && !weave_off;+ for (std::size_t i = 0; i < rgba.size(); ++i) {+ const std::uint32_t c = rgba[i];+ if (blend) rgba[i] = (c & previous[i]) + (((c ^ previous[i]) & 0xFEFEFEFEu) >> 1);+ previous[i] = c;+ }+ return true;+}+std::uint64_t primitives_drawn() { return g_primitives; }void dump_region(const char *spec) {diff --git a/runtime/src/gs/gs.hpp b/runtime/src/gs/gs.hppindex d072e30..6f9c17e 100644--- a/runtime/src/gs/gs.hpp+++ b/runtime/src/gs/gs.hpp@@ -56,6 +56,8 @@ std::uint32_t read_pixel(std::uint32_t psm, std::uint32_t bp, std::uint32_t bw,void write_pixel(std::uint32_t psm, std::uint32_t bp, std::uint32_t bw, std::uint32_t x, std::uint32_t y, std::uint32_t value);bool display(std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32_t &height);+// Frame for hosts that draw it themselves, with interlaced fields blended like present(); may lag a frame.+bool copy_frame(std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32_t &height);bool attach_window(int kind, void *display, std::uint64_t window, void *surface, const char *selector, std::uint32_t width, std::uint32_t height);void resize_window(std::uint32_t width, std::uint32_t height);bool present();diff --git a/runtime/src/system.cpp b/runtime/src/system.cppindex 5f6ebd3..e428a89 100644--- a/runtime/src/system.cpp+++ b/runtime/src/system.cpp@@ -281,6 +281,7 @@ bool display_frame(std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::bool attach_window(const WindowTarget &t) { return gs::attach_window(static_cast<int>(t.kind), t.display, t.window, t.surface, t.selector, t.width, t.height); }void resize_window(std::uint32_t width, std::uint32_t height) { gs::resize_window(width, height); }bool present_frame() { return gs::present(); }+bool copy_frame(std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32_t &height) { return gs::copy_frame(rgba, width, height); }bool parse_press(const std::string &spec, Press &press) {static const char *const kNames[16] = {"select", "l3", "r3", "start", "up", "right", "down", "left",