Skip to content

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

C++

Native window presents through Metal on macOS and reads its memory use there

Samir Nurie6114582d29a
diff --git a/host/window.cpp b/host/window.cpp
index 8289130..44d3252 100644
--- a/host/window.cpp
+++ b/host/window.cpp
@@ -6,6 +6,10 @@
#include <SDL_syswm.h>
#include <sys/resource.h>
#include <unistd.h>
+#ifdef __APPLE__
+#include <SDL_metal.h>
+#include <mach/mach.h>
+#endif
#include <algorithm>
#include <atomic>
@@ -83,6 +87,18 @@ bool attach_gpu() {
SDL_GetWindowSizeInPixels(g_window, &w, &h);
target.width = static_cast<std::uint32_t>(w);
target.height = static_cast<std::uint32_t>(h);
+#ifdef __APPLE__
+ if (info.subsystem == SDL_SYSWM_COCOA) {
+ SDL_MetalView view = SDL_Metal_CreateView(g_window);
+ if (!view) return false;
+ target.kind = ps2::WindowTarget::Metal;
+ target.surface = SDL_Metal_GetLayer(view);
+ SDL_Metal_GetDrawableSize(g_window, &w, &h);
+ target.width = static_cast<std::uint32_t>(w);
+ target.height = static_cast<std::uint32_t>(h);
+ return target.surface && ps2::attach_window(target);
+ }
+#endif
switch (info.subsystem) {
#if defined(SDL_VIDEO_DRIVER_X11)
case SDL_SYSWM_X11:
@@ -198,7 +214,11 @@ void poll() {
}
if (e.type == SDL_WINDOWEVENT && e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED && g_gpu_present) {
int w = 0, h = 0;
+#ifdef __APPLE__
+ SDL_Metal_GetDrawableSize(g_window, &w, &h);
+#else
SDL_GetWindowSizeInPixels(g_window, &w, &h);
+#endif
ps2::resize_window(static_cast<std::uint32_t>(w), static_cast<std::uint32_t>(h));
}
if (e.type == SDL_CONTROLLERDEVICEADDED) open_pad(e.cdevice.which);
@@ -262,6 +282,12 @@ double process_cpu_seconds() {
}
double resident_mb() {
+#ifdef __APPLE__
+ mach_task_basic_info_data_t task{};
+ mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
+ if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, reinterpret_cast<task_info_t>(&task), &count) != KERN_SUCCESS) return 0.0;
+ return static_cast<double>(task.resident_size) / (1024.0 * 1024.0);
+#endif
long pages = 0, resident = 0;
if (std::FILE *f = std::fopen("/proc/self/statm", "r")) {
if (std::fscanf(f, "%ld %ld", &pages, &resident) != 2) resident = 0;
@@ -313,7 +339,12 @@ bool open_window(bool fullscreen) {
std::fprintf(stderr, "SDL: %s\n", SDL_GetError());
return false;
}
- const Uint32 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | (fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0u);
+#ifdef __APPLE__
+ const Uint32 metal = std::getenv("PS2_SDL_RENDERER") ? 0u : SDL_WINDOW_METAL;
+#else
+ const Uint32 metal = 0u;
+#endif
+ const Uint32 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | metal | (fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0u);
g_window = SDL_CreateWindow("ps2-web-recomp", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, ps2::g_options.widescreen ? 1280 : 960, 720, flags);
if (!g_window) {
std::fprintf(stderr, "SDL: %s\n", SDL_GetError());
diff --git a/runtime/include/ps2/system.hpp b/runtime/include/ps2/system.hpp
index d794aee..ccda2d0 100644
--- a/runtime/include/ps2/system.hpp
+++ b/runtime/include/ps2/system.hpp
@@ -101,7 +101,7 @@ std::uint64_t frame_count();
bool display_frame(std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32_t &height);
struct WindowTarget {
- enum Kind { X11, Wayland, Canvas } kind = X11;
+ enum Kind { X11, Wayland, Canvas, Metal } kind = X11;
void *display = nullptr;
std::uint64_t window = 0;
void *surface = nullptr;
diff --git a/runtime/src/gs/gpu/gpu.cpp b/runtime/src/gs/gpu/gpu.cpp
index 2540c65..5a13c3e 100644
--- a/runtime/src/gs/gpu/gpu.cpp
+++ b/runtime/src/gs/gpu/gpu.cpp
@@ -437,12 +437,6 @@ bool read_rect(const std::uint32_t *rec, const std::uint32_t *setup, int &u0, in
return true;
}
-Range read_range(const std::uint32_t *rec, const std::uint32_t *setup, const Range &whole) {
- int u0, v0, u1, v1;
- if (!read_rect(rec, setup, u0, v0, u1, v1)) return whole;
- return region(setup[12], setup[13], setup[14], u0, v0, u1, v1);
-}
-
Range pages(std::uint32_t bp, std::uint32_t bw, std::uint32_t psm, int x0, int y0, int x1, int y1) {
std::uint32_t pw, ph;
page_size(psm, pw, ph);
@@ -1714,7 +1708,11 @@ bool attach_surface(const SurfaceTarget &target) {
#else
WGPUSurfaceSourceXlibWindow xlib = WGPU_SURFACE_SOURCE_XLIB_WINDOW_INIT;
WGPUSurfaceSourceWaylandSurface wayland = WGPU_SURFACE_SOURCE_WAYLAND_SURFACE_INIT;
- if (target.kind == SurfaceTarget::X11) {
+ WGPUSurfaceSourceMetalLayer metal = WGPU_SURFACE_SOURCE_METAL_LAYER_INIT;
+ if (target.kind == SurfaceTarget::Metal) {
+ metal.layer = target.surface;
+ sd.nextInChain = &metal.chain;
+ } else if (target.kind == SurfaceTarget::X11) {
xlib.display = target.display;
xlib.window = target.window;
sd.nextInChain = &xlib.chain;
diff --git a/runtime/src/gs/gpu/gpu.hpp b/runtime/src/gs/gpu/gpu.hpp
index 2f09e51..734e4e4 100644
--- a/runtime/src/gs/gpu/gpu.hpp
+++ b/runtime/src/gs/gpu/gpu.hpp
@@ -15,7 +15,7 @@ struct DisplaySource {
};
struct SurfaceTarget {
- enum Kind { X11, Wayland, Canvas } kind;
+ enum Kind { X11, Wayland, Canvas, Metal } kind;
void *display;
std::uint64_t window;
void *surface;