PS2 games in the browser: static recompilation to WebAssembly with a native runtime
C++
GS downloads: one request per DMA block, only the pages it reads
Samir Nuri17a5eee43eb0
diff --git a/runtime/src/dmac.cpp b/runtime/src/dmac.cppindex eeb3090..50c5f37 100644--- a/runtime/src/dmac.cpp+++ b/runtime/src/dmac.cpp@@ -122,7 +122,10 @@ void run_normal(int ch) {} else if ((c.chcr & 1u) || ch == 2 || ch == 0 || ch == 9) {deliver(ch, c.madr, c.qwc);} else if (ch == 1) {- for (std::uint32_t i = 0; i < c.qwc; ++i) gs::read_image(target(c.madr + i * 16), 1);+ for (std::uint32_t i = 0, n = 1; i < c.qwc; i += n, n = 1) {+ while (i + n < c.qwc && target(c.madr + (i + n) * 16) == target(c.madr + i * 16) + n * 16) ++n;+ gs::read_image(target(c.madr + i * 16), n);+ }} else {std::fprintf(stderr, "[dma] channel %d reads from a device (not emulated)\n", ch);}diff --git a/runtime/src/gs/gpu/gpu.cpp b/runtime/src/gs/gpu/gpu.cppindex 06a522b..957686e 100644--- a/runtime/src/gs/gpu/gpu.cpp+++ b/runtime/src/gs/gpu/gpu.cpp@@ -1232,16 +1232,16 @@ void wait_idle() {wait_until(done);}-void download(std::uint8_t *memory) {+void download(std::uint8_t *memory, std::uint32_t offset, std::uint32_t bytes) {flush_all();if (g_scale == 1) {submit();- read_back(g_vram, kMemorySize, memory);+ read_back(g_vram, bytes, memory + offset, offset);return;}- dispatch_linear(kResolve, kMemorySize / 4, push_params({}));+ dispatch_linear(kResolve, bytes / 4, push_params({offset / 4}));submit();- read_back(g_scratch, kMemorySize, memory);+ read_back(g_scratch, bytes, memory + offset, offset);}void read_state(std::vector<std::uint32_t> &vram, std::uint32_t &scale, std::vector<std::uint32_t> &clut) {diff --git a/runtime/src/gs/gpu/gpu.hpp b/runtime/src/gs/gpu/gpu.hppindex fb90a6d..80bd08e 100644--- a/runtime/src/gs/gpu/gpu.hpp+++ b/runtime/src/gs/gpu/gpu.hpp@@ -32,7 +32,7 @@ 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);+void download(std::uint8_t *memory, std::uint32_t offset = 0, std::uint32_t bytes = kMemorySize);void read_state(std::vector<std::uint32_t> &vram, std::uint32_t &scale, std::vector<std::uint32_t> &clut);void write_state(const std::vector<std::uint32_t> &vram, std::uint32_t scale, const std::vector<std::uint32_t> &clut);bool display(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.wgslindex 9ef4e32..a3180f2 100644--- a/runtime/src/gs/gpu/gs.wgsl+++ b/runtime/src/gs/gpu/gs.wgsl@@ -1065,7 +1065,7 @@ fn shade_at(p: u32, s: Setup, x: i32, y: i32, k: u32, px: f32, py: f32) {@compute @workgroup_size(64)fn resolve(@builtin(global_invocation_id) gid: vec3<u32>) {- let i = gid.x + gid.y * 64u * 1024u;+ let i = prm(0u) + gid.x + gid.y * 64u * 1024u;if (i >= 1048576u) {return;}diff --git a/runtime/src/gs/gs.cpp b/runtime/src/gs/gs.cppindex 5a9409b..9017530 100644--- a/runtime/src/gs/gs.cpp+++ b/runtime/src/gs/gs.cpp@@ -148,6 +148,21 @@ std::uint32_t bits_per_pixel(std::uint32_t psm) {}}+#ifdef PS2_GPU+void download_source(const State &s) {+ const std::uint32_t sbp = static_cast<std::uint32_t>(s.bitbltbuf) & 0x3FFFu, sbw = static_cast<std::uint32_t>(s.bitbltbuf >> 16) & 0x3Fu;+ const std::uint32_t spsm = static_cast<std::uint32_t>(s.bitbltbuf >> 24) & 0x3Fu;+ const std::uint32_t x = static_cast<std::uint32_t>(s.trxpos) & 0x7FFu, y = static_cast<std::uint32_t>(s.trxpos >> 16) & 0x7FFu;+ const std::uint32_t w = static_cast<std::uint32_t>(s.trxreg) & 0xFFFu, h = static_cast<std::uint32_t>(s.trxreg >> 32) & 0xFFFu;+ const bool half = bits_per_pixel(spsm) == 16;+ const std::uint32_t pw = spsm == PSMT8 || spsm == PSMT4 ? 128 : 64, ph = spsm == PSMT4 ? 128 : spsm == PSMT8 || half ? 64 : 32;+ const std::uint32_t first = page_address(sbp, sbw, x, y, pw, ph);+ const std::uint32_t end = page_address(sbp, sbw, x + w - 1, y + h - 1, pw, ph) + 8192;+ if (!w || !h || x + w > 2048 || y + h > 2048 || end > kMemorySize) gpu::download(g_memory);+ else gpu::download(g_memory, first, end - first);+}+#endif+void transfer_pixel(std::uint32_t value) {const std::uint64_t buf = g_state.bitbltbuf, pos = g_state.trxpos, reg = g_state.trxreg;const std::uint32_t dbp = static_cast<std::uint32_t>(buf >> 32) & 0x3FFFu;@@ -510,7 +525,7 @@ void write(std::uint32_t reg, std::uint64_t value) {if (gpu_on()) {if ((value & 3u) == 0) gpu::image_begin(s);else if ((value & 3u) == 2) gpu::local_copy(s);- else if ((value & 3u) == 1 && !cpu_raster()) gpu::download(g_memory);+ else if ((value & 3u) == 1 && !cpu_raster()) download_source(s);}#endifif ((value & 3u) == 0) g_transfer.active = true;