Skip to content

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

C++

GS: one thread per sample when upscaling, PS2_TIME frame timer

Samir Nuri944de3421655
diff --git a/runtime/src/gs/gpu/gpu.cpp b/runtime/src/gs/gpu/gpu.cpp
index 9178010..502f75b 100644
--- a/runtime/src/gs/gpu/gpu.cpp
+++ b/runtime/src/gs/gpu/gpu.cpp
@@ -34,8 +34,6 @@ constexpr std::uint32_t kClutSlots = 2048;
constexpr std::uint32_t kClutEntries = 512;
constexpr std::uint32_t kPrimWords = 40;
constexpr std::uint32_t kSetupWords = 24;
-constexpr int kTileShift = 4;
-constexpr int kTilesPerRow = 2048 >> kTileShift;
Mode g_mode = Mode::off;
std::uint64_t g_dispatches[kPipelines] = {}, g_submits = 0;
@@ -146,7 +144,8 @@ bool uploads_overlap(const PreOp &a, const PreOp &b) {
std::vector<PreOp> g_pre;
std::vector<Range> g_batch_reads;
-std::vector<std::uint32_t> g_tile_count(kTilesPerRow * kTilesPerRow), g_tile_fill(kTilesPerRow * kTilesPerRow);
+int g_tile = 16, g_tiles_per_row = 128;
+std::vector<std::uint32_t> g_tile_count(128 * 128), g_tile_fill(128 * 128);
std::vector<std::uint32_t> g_touched;
WGPUStringView view(const char *s) { return WGPUStringView{s, WGPU_STRLEN}; }
@@ -230,11 +229,11 @@ void flush_batch() {
g_touched.clear();
for (std::uint32_t i = 0; i < prims; ++i) {
const std::uint32_t lo = b.boxes[i * 2], hi = b.boxes[i * 2 + 1];
- const int tx0 = static_cast<int>(lo & 0xFFFFu) >> kTileShift, ty0 = static_cast<int>(lo >> 16) >> kTileShift;
- const int tx1 = static_cast<int>(hi & 0xFFFFu) >> kTileShift, ty1 = static_cast<int>(hi >> 16) >> kTileShift;
+ const int tx0 = static_cast<int>(lo & 0xFFFFu) / g_tile, ty0 = static_cast<int>(lo >> 16) / g_tile;
+ const int tx1 = static_cast<int>(hi & 0xFFFFu) / g_tile, ty1 = static_cast<int>(hi >> 16) / g_tile;
for (int ty = ty0; ty <= ty1; ++ty)
for (int tx = tx0; tx <= tx1; ++tx) {
- const std::uint32_t t = static_cast<std::uint32_t>(ty * kTilesPerRow + tx);
+ const std::uint32_t t = static_cast<std::uint32_t>(ty * g_tiles_per_row + tx);
if (!g_tile_count[t]++) g_touched.push_back(t);
}
}
@@ -251,7 +250,7 @@ void flush_batch() {
const std::uint32_t t = g_touched[i];
std::uint32_t *rec = g_arena.data() + tile_offset + i * 4;
rec[3] = 0;
- rec[0] = (t % kTilesPerRow) | ((t / kTilesPerRow) << 16);
+ rec[0] = (t % static_cast<std::uint32_t>(g_tiles_per_row)) | ((t / static_cast<std::uint32_t>(g_tiles_per_row)) << 16);
rec[1] = cursor;
rec[2] = g_tile_count[t];
g_tile_fill[t] = cursor;
@@ -259,10 +258,10 @@ void flush_batch() {
}
for (std::uint32_t i = 0; i < prims; ++i) {
const std::uint32_t lo = b.boxes[i * 2], hi = b.boxes[i * 2 + 1];
- const int tx0 = static_cast<int>(lo & 0xFFFFu) >> kTileShift, ty0 = static_cast<int>(lo >> 16) >> kTileShift;
- const int tx1 = static_cast<int>(hi & 0xFFFFu) >> kTileShift, ty1 = static_cast<int>(hi >> 16) >> kTileShift;
+ const int tx0 = static_cast<int>(lo & 0xFFFFu) / g_tile, ty0 = static_cast<int>(lo >> 16) / g_tile;
+ const int tx1 = static_cast<int>(hi & 0xFFFFu) / g_tile, ty1 = static_cast<int>(hi >> 16) / g_tile;
for (int ty = ty0; ty <= ty1; ++ty)
- for (int tx = tx0; tx <= tx1; ++tx) g_arena[g_tile_fill[static_cast<std::uint32_t>(ty * kTilesPerRow + tx)]++] = i;
+ for (int tx = tx0; tx <= tx1; ++tx) g_arena[g_tile_fill[static_cast<std::uint32_t>(ty * g_tiles_per_row + tx)]++] = i;
}
for (std::uint32_t t : g_touched) g_tile_count[t] = 0;
const std::uint32_t param = push_params({tile_offset, prim_offset, setup_offset, tiles});
@@ -688,6 +687,10 @@ bool init(Mode m) {
if (const char *scale = std::getenv("PS2_SCALE")) g_scale = std::clamp<std::uint32_t>(static_cast<std::uint32_t>(std::atoi(scale)), 1u, 3u);
g_copy_words = (1u << 20) * g_scale * g_scale;
+ g_tile = 16 / static_cast<int>(g_scale);
+ g_tiles_per_row = (2048 + g_tile - 1) / g_tile;
+ g_tile_count.assign(static_cast<std::size_t>(g_tiles_per_row) * g_tiles_per_row, 0);
+ g_tile_fill.assign(static_cast<std::size_t>(g_tiles_per_row) * g_tiles_per_row, 0);
g_field_words = (1u << 19) * g_scale * g_scale;
g_vram = make_buffer(static_cast<std::uint64_t>(kMemorySize) * g_scale * g_scale, WGPUBufferUsage_Storage | WGPUBufferUsage_CopySrc | WGPUBufferUsage_CopyDst);
std::vector<std::uint32_t> tables;
@@ -979,6 +982,18 @@ void flush() {
submit();
}
+void wait_idle() {
+ flush_all();
+ submit();
+ bool done = false;
+ WGPUQueueWorkDoneCallbackInfo cb = WGPU_QUEUE_WORK_DONE_CALLBACK_INFO_INIT;
+ cb.mode = WGPUCallbackMode_AllowProcessEvents;
+ cb.callback = [](WGPUQueueWorkDoneStatus, WGPUStringView, void *flag, void *) { *static_cast<bool *>(flag) = true; };
+ cb.userdata1 = &done;
+ wgpuQueueOnSubmittedWorkDone(g_queue, cb);
+ wait_until(done);
+}
+
void download(std::uint8_t *memory) {
flush_all();
if (g_scale == 1) {
diff --git a/runtime/src/gs/gpu/gpu.hpp b/runtime/src/gs/gpu/gpu.hpp
index a606cc9..e6ea437 100644
--- a/runtime/src/gs/gpu/gpu.hpp
+++ b/runtime/src/gs/gpu/gpu.hpp
@@ -31,6 +31,7 @@ void image_data(const std::uint8_t *data, std::size_t bytes);
void local_copy(const State &s);
void load_clut(std::uint64_t tex0, std::uint64_t texclut);
void flush();
+void wait_idle();
void download(std::uint8_t *memory);
bool display(const DisplaySource &src, std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32_t &height);
bool display_async(const DisplaySource &src, std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32_t &height);
diff --git a/runtime/src/gs/gpu/gs.wgsl b/runtime/src/gs/gpu/gs.wgsl
index 5280f20..efdcb0d 100644
--- a/runtime/src/gs/gpu/gs.wgsl
+++ b/runtime/src/gs/gpu/gs.wgsl
@@ -10,6 +10,8 @@ struct Params {
@group(0) @binding(5) var<storage, read_write> scratch: array<u32>;
override SCALE: u32 = 1u;
+override TILE: u32 = 16u / SCALE;
+override WG: u32 = TILE * SCALE;
const PSMCT32: u32 = 0x00u;
const PSMCT24: u32 = 0x01u;
@@ -809,14 +811,6 @@ fn z_at(p: u32, d: f32) -> u32 {
return z0 - u32(min(-step, f32(z0)));
}
-fn shade(p: u32, s: Setup, x: i32, y: i32) {
- for (var k = 0u; k < SCALE * SCALE; k = k + 1u) {
- let px = f32(x) + f32(k % SCALE) / f32(SCALE);
- let py = f32(y) + f32(k / SCALE) / f32(SCALE);
- shade_at(p, s, x, y, k, px, py);
- }
-}
-
fn shade_at(p: u32, s: Setup, x: i32, y: i32, k: u32, px: f32, py: f32) {
let kind = data[p + 1u];
var at: Attr;
@@ -920,15 +914,18 @@ fn resolve(@builtin(global_invocation_id) gid: vec3<u32>) {
scratch[i] = atomicLoad(&vram[i * SCALE * SCALE]);
}
-@compute @workgroup_size(16, 16)
+@compute @workgroup_size(WG, WG)
fn raster(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
let tile = prm(0u) + (wid.x + wid.y * 1024u) * 4u;
if (wid.x + wid.y * 1024u >= prm(3u)) {
return;
}
let origin = data[tile];
- let x = i32((origin & 0xFFFFu) * 16u + lid.x);
- let y = i32((origin >> 16u) * 16u + lid.y);
+ let x = i32((origin & 0xFFFFu) * TILE + lid.x / SCALE);
+ let y = i32((origin >> 16u) * TILE + lid.y / SCALE);
+ let k = (lid.y % SCALE) * SCALE + lid.x % SCALE;
+ let px = f32(x) + f32(lid.x % SCALE) / f32(SCALE);
+ let py = f32(y) + f32(lid.y % SCALE) / f32(SCALE);
let list = data[tile + 1u];
let count = data[tile + 2u];
let prims = prm(1u);
@@ -947,6 +944,6 @@ fn raster(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) l
s = load_setup(setups + si * SETUP_WORDS);
last_setup = si;
}
- shade(p, s, x, y);
+ shade_at(p, s, x, y, k, px, py);
}
}
diff --git a/runtime/src/gs/gs.cpp b/runtime/src/gs/gs.cpp
index 9a53c52..a65b169 100644
--- a/runtime/src/gs/gs.cpp
+++ b/runtime/src/gs/gs.cpp
@@ -825,6 +825,12 @@ bool copy_frame(const DisplayRegs &r, std::vector<std::uint32_t> &rgba, std::uin
std::atomic<std::uint32_t> g_presented{0};
std::uint32_t g_posted_presents = 0;
+void flush_and_wait() {
+#ifdef PS2_GPU
+ if (gpu_on()) gpu::wait_idle();
+#endif
+}
+
void start_thread() { queue::render().start(32u << 20); }
void post_present() {
diff --git a/runtime/src/gs/gs.hpp b/runtime/src/gs/gs.hpp
index 5678212..feba96c 100644
--- a/runtime/src/gs/gs.hpp
+++ b/runtime/src/gs/gs.hpp
@@ -75,6 +75,7 @@ void resize_window(std::uint32_t width, std::uint32_t height);
bool present();
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);
double gpu_busy_ms();
diff --git a/runtime/src/system.cpp b/runtime/src/system.cpp
index 4cdafe3..a1aaae2 100644
--- a/runtime/src/system.cpp
+++ b/runtime/src/system.cpp
@@ -103,8 +103,25 @@ void apply_presses() {
iop::sio2::set_buttons(0, mask);
}
+void time_frames() {
+ static const char *spec = std::getenv("PS2_TIME");
+ if (!spec) return;
+ static const std::uint64_t from = std::strtoull(spec, nullptr, 10);
+ static const std::uint64_t count = std::strchr(spec, ',') ? std::strtoull(std::strchr(spec, ',') + 1, nullptr, 10) : 500;
+ static std::chrono::steady_clock::time_point start;
+ if (g_frames == from) {
+ gs::flush_and_wait();
+ start = std::chrono::steady_clock::now();
+ } else if (g_frames == from + count) {
+ gs::flush_and_wait();
+ const double ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - start).count();
+ std::fprintf(stderr, "[time] frames %llu-%llu: %.2f ms per frame\n", static_cast<unsigned long long>(from), static_cast<unsigned long long>(from + count), ms / static_cast<double>(count));
+ }
+}
+
void vblank() {
++g_frames;
+ time_frames();
apply_presses();
if (g_options.on_vblank) g_options.on_vblank();
if (g_options.trace_iop_modules && g_frames % 25 == 0) std::fprintf(stderr, "[frame] %llu prims %llu\n", static_cast<unsigned long long>(g_frames), static_cast<unsigned long long>(gs::primitives_drawn()));