Skip to content

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

C++

GS: small reads of the frame between TEXFLUSHes share one batch

Samir Nuria6acd8d0f6c3
diff --git a/runtime/src/gs/gpu/gpu.cpp b/runtime/src/gs/gpu/gpu.cpp
index eacf2a2..279ae49 100644
--- a/runtime/src/gs/gpu/gpu.cpp
+++ b/runtime/src/gs/gpu/gpu.cpp
@@ -110,7 +110,7 @@ struct Arena {
std::uint32_t *data() { return words.get(); }
std::uint32_t &operator[](std::size_t i) { return words[i]; }
} g_arena;
-std::uint32_t g_clut_slot = 0, g_clut_loads = 0;
+std::uint32_t g_clut_slot = 0, g_clut_loads = 0, g_texflushes = 0;
struct Image {
bool active = false;
@@ -136,6 +136,7 @@ struct Batch {
int minx = 1 << 30, miny = 1 << 30, maxx = -1, maxy = -1;
std::uint32_t fbp = 0, fbw = 0, fpsm = 0, zbp = 0, zpsm = 0;
bool zte = false, zwrite = false;
+ std::uint32_t texflushes = 0;
int rx0 = 0, ry0 = 0, rx1 = -2, ry1 = -2;
Range fb_range, z_range;
} g_batch;
@@ -945,7 +946,7 @@ std::uint32_t make_setup(const State &st, std::uint64_t attr, std::uint32_t *w)
return flags;
}
-bool texel_range(const std::uint32_t *setup, const Vertex *v, int count, std::uint32_t prim, bool fst, Range &out) {
+bool texel_rect(const std::uint32_t *setup, const Vertex *v, int count, std::uint32_t prim, bool fst, int rect[4]) {
const int tw = 1 << (setup[15] & 0xFFu), th = 1 << (setup[15] >> 8);
float u0 = 1e30f, v0 = 1e30f, u1 = -1e30f, v1 = -1e30f;
for (int i = 0; i < count; ++i) {
@@ -990,10 +991,29 @@ bool texel_range(const std::uint32_t *setup, const Vertex *v, int count, std::ui
};
int ua, ub, va, vb;
if (!axis((setup[7] >> 24) & 3u, u0, u1, tw, setup[18], ua, ub) || !axis((setup[7] >> 26) & 3u, v0, v1, th, setup[19], va, vb)) return false;
- out = region(setup[12], setup[13], setup[14], std::max(ua, 0), std::max(va, 0), std::max(ub, 0), std::max(vb, 0));
+ rect[0] = std::max(ua, 0);
+ rect[1] = std::max(va, 0);
+ rect[2] = std::max(ub, 0);
+ rect[3] = std::max(vb, 0);
return true;
}
+bool shares_frame(const std::uint32_t *setup, const Batch &b) {
+ const auto wide = [](std::uint32_t psm) { return psm == PSMCT32 || psm == PSMCT24 || psm == PSMT8H || psm == PSMT4HL || psm == PSMT4HH; };
+ return setup[12] == b.fbp && setup[13] == b.fbw && (setup[14] == b.fpsm || (wide(setup[14]) && wide(b.fpsm)));
+}
+
+bool drawn_under(const Batch &b, const int rect[4]) {
+ const auto hits = [&](int x0, int y0, int x1, int y1) { return rect[0] <= x1 && x0 <= rect[2] && rect[1] <= y1 && y0 <= rect[3]; };
+ const std::size_t prims = b.boxes.size() / 2;
+ if (prims > 64) return hits(b.minx, b.miny, b.maxx, b.maxy);
+ for (std::size_t i = 0; i < prims; ++i) {
+ const std::uint32_t lo = b.boxes[i * 2], hi = b.boxes[i * 2 + 1];
+ if (hits(static_cast<int>(lo & 0xFFFFu), static_cast<int>(lo >> 16), static_cast<int>(hi & 0xFFFFu), static_cast<int>(hi >> 16))) return true;
+ }
+ return false;
+}
+
void attrs(std::uint32_t *rec, int n, const Vertex &v, bool fst, const Vertex *color) {
const Vertex &c = color ? *color : v;
rec[12 + n] = c.r | (static_cast<std::uint32_t>(c.g) << 8) | (static_cast<std::uint32_t>(c.b) << 16) | (static_cast<std::uint32_t>(c.a) << 24);
@@ -1278,11 +1298,22 @@ void draw(const State &st, const Vertex *v, int count) {
if (!b.prims.empty()) {
batch_ranges();
const auto written = [&](const Range &t) { return t.overlaps(b.fb_range.lo, b.fb_range.hi) || (b.zwrite && t.overlaps(b.z_range.lo, b.z_range.hi)); };
- Range used;
- if (written(tex) && (!texel_range(setup, v, count, prim, fst, used) || written(used))) emit_all();
+ int rect[4];
+ if (written(tex)) {
+ if (!texel_rect(setup, v, count, prim, fst, rect)) {
+ emit_all();
+ } else {
+ const Range used = region(setup[12], setup[13], setup[14], rect[0], rect[1], rect[2], rect[3]);
+ const bool small = rect[2] - rect[0] < 64 && rect[3] - rect[1] < 64;
+ const bool cached = b.texflushes == g_texflushes && small && shares_frame(setup, b);
+ const bool frame = !cached && (shares_frame(setup, b) ? drawn_under(b, rect) : used.overlaps(b.fb_range.lo, b.fb_range.hi));
+ if (frame || (b.zwrite && used.overlaps(b.z_range.lo, b.z_range.hi))) emit_all();
+ }
+ }
}
}
if (b.prims.empty()) {
+ b.texflushes = g_texflushes;
b.key = key;
b.fbp = setup[1];
b.fbw = setup[2];
@@ -1391,6 +1422,8 @@ void load_clut(std::uint64_t tex0, std::uint64_t texclut) {
++g_clut_loads;
}
+void texflush() { ++g_texflushes; }
+
void flush() {
if (g_mode == Mode::off) return;
flush_all();
diff --git a/runtime/src/gs/gpu/gpu.hpp b/runtime/src/gs/gpu/gpu.hpp
index d29ebd4..2f09e51 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 texflush();
void wait_idle();
void download(std::uint8_t *memory, std::uint32_t offset = 0, std::uint32_t bytes = kMemorySize);
bool download_async(std::uint8_t *memory, std::uint32_t offset, std::uint32_t bytes, std::uint32_t serial);
diff --git a/runtime/src/gs/gs.cpp b/runtime/src/gs/gs.cpp
index 7dc4bce..2991477 100644
--- a/runtime/src/gs/gs.cpp
+++ b/runtime/src/gs/gs.cpp
@@ -571,7 +571,11 @@ void write(std::uint32_t reg, std::uint64_t value) {
case 0x36: case 0x37: s.ctx[reg - 0x36].miptbp2 = value; break;
case 0x3B: s.texa = value; break;
case 0x3D: s.fogcol = value; break;
- case 0x3F: break;
+ case 0x3F:
+#ifdef PS2_GPU
+ if (gpu_on()) gpu::texflush();
+#endif
+ break;
case 0x40: case 0x41: s.ctx[reg - 0x40].scissor = value; break;
case 0x42: case 0x43: s.ctx[reg - 0x42].alpha = value; break;
case 0x44: s.dimx = value; break;