PS2 games in the browser: static recompilation to WebAssembly with a native runtime
C++
GPU: decide the fill rule per triangle on the CPU
Samir Nuri78d79d4c7582
diff --git a/runtime/src/gs/gpu/gpu.cpp b/runtime/src/gs/gpu/gpu.cppindex 506d4aa..5013156 100644--- a/runtime/src/gs/gpu/gpu.cpp+++ b/runtime/src/gs/gpu/gpu.cpp@@ -1024,6 +1024,12 @@ void draw(const State &st, const Vertex *v, int count) {const float xy[6] = {x0, y0, x1, y1, x2, y2};for (int i = 0; i < 6; ++i) rec[4 + i] = f2u(xy[i]);rec[10] = f2u(1.0f / area);+ const float sg = area > 0.0f ? 1.0f : -1.0f;+ const auto inclusive = [&](float ax, float ay, float bx, float by) {+ const float gx = (ay - by) * sg, gy = (bx - ax) * sg;+ return gx > 0.0f || (gx == 0.0f && gy > 0.0f);+ };+ rec[27] = (inclusive(x1, y1, x2, y2) ? 1u : 0u) | (inclusive(x2, y2, x0, y0) ? 2u : 0u) | (inclusive(x0, y0, x1, y1) ? 4u : 0u);for (int i = 0; i < 3; ++i) attrs(rec, i, v[i], fst, iip ? nullptr : &v[2]);rec[24] = v[0].z;rec[25] = f2u(static_cast<float>(static_cast<std::int64_t>(v[1].z) - static_cast<std::int64_t>(v[0].z)));diff --git a/runtime/src/gs/gpu/gs.wgsl b/runtime/src/gs/gpu/gs.wgslindex 8c16a9c..9077a36 100644--- a/runtime/src/gs/gpu/gs.wgsl+++ b/runtime/src/gs/gpu/gs.wgsl@@ -938,8 +938,8 @@ fn z_at(p: u32, d: f32) -> u32 {return z0 - u32(min(-step, f32(z0)));}-fn edge_in(e: f32, gx: f32, gy: f32) -> bool {- return e > 0.0 || (e == 0.0 && (gx > 0.0 || (gx == 0.0 && gy > 0.0)));+fn edge_in(w: f32, inclusive: u32) -> bool {+ return w > 0.0 || (w == 0.0 && inclusive != 0u);}fn shade_at(p: u32, s: Setup, x: i32, y: i32, k: u32, px: f32, py: f32) {@@ -954,16 +954,13 @@ fn shade_at(p: u32, s: Setup, x: i32, y: i32, k: u32, px: f32, py: f32) {let x2 = fat(p + 8u);let y2 = fat(p + 9u);let inv = fat(p + 10u);- let e0 = (x1 - px) * (y2 - py) - (x2 - px) * (y1 - py);- let e1 = (x2 - px) * (y0 - py) - (x0 - px) * (y2 - py);- let e2 = (x0 - px) * (y1 - py) - (x1 - px) * (y0 - py);- let sg = sign(inv);- if (!edge_in(e0 * sg, (y1 - y2) * sg, (x2 - x1) * sg) || !edge_in(e1 * sg, (y2 - y0) * sg, (x0 - x2) * sg) ||- !edge_in(e2 * sg, (y0 - y1) * sg, (x1 - x0) * sg)) {+ let w0 = ((x1 - px) * (y2 - py) - (x2 - px) * (y1 - py)) * inv;+ let w1 = ((x2 - px) * (y0 - py) - (x0 - px) * (y2 - py)) * inv;+ let c2 = ((x0 - px) * (y1 - py) - (x1 - px) * (y0 - py)) * inv;+ let inclusive = data[p + 27u];+ if (!edge_in(w0, inclusive & 1u) || !edge_in(w1, inclusive & 2u) || !edge_in(c2, inclusive & 4u)) {return;}- let w0 = e0 * inv;- let w1 = e1 * inv;let w2 = 1.0 - w0 - w1;let c = vertex_attr(p, 0u) * w0 + vertex_attr(p, 1u) * w1 + vertex_attr(p, 2u) * w2;let t = vertex_tex(p, 0u) * w0 + vertex_tex(p, 1u) * w1 + vertex_tex(p, 2u) * w2;