PS2 games in the browser: static recompilation to WebAssembly with a native runtime
C++
GS: top-left fill rule for triangle edges
Samir Nuribc7d685264de
diff --git a/runtime/src/gs/gpu/gs.wgsl b/runtime/src/gs/gpu/gs.wgslindex 2a8f0ed..8c16a9c 100644--- a/runtime/src/gs/gpu/gs.wgsl+++ b/runtime/src/gs/gpu/gs.wgsl@@ -938,6 +938,10 @@ 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 shade_at(p: u32, s: Setup, x: i32, y: i32, k: u32, px: f32, py: f32) {let kind = data[p + 1u];var at: Attr;@@ -950,12 +954,17 @@ 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 w0 = ((x1 - px) * (y2 - py) - (x2 - px) * (y1 - py)) * inv;- let w1 = ((x2 - px) * (y0 - py) - (x0 - px) * (y2 - py)) * inv;- let w2 = 1.0 - w0 - w1;- if (w0 < 0.0 || w1 < 0.0 || w2 < 0.0) {+ 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)) {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;at = Attr(c.x, c.y, c.z, c.w, t.x, t.y, t.z, t.w, z_at(p, fat(p + 25u) * w1 + fat(p + 26u) * w2));diff --git a/runtime/src/gs/raster.cpp b/runtime/src/gs/raster.cppindex 68a4c26..6aca80a 100644--- a/runtime/src/gs/raster.cpp+++ b/runtime/src/gs/raster.cpp@@ -393,14 +393,21 @@ void triangle(const Setup &s, const Vertex *v) {const int maxy = std::min(s.scay1, static_cast<int>(std::ceil(std::max({y0, y1, y2}))) - 1);if (minx > maxx || miny > maxy) return;const float inv = 1.0f / area;+ const float sg = area > 0.0f ? 1.0f : -1.0f;+ const auto edge_in = [](float e, float gx, float gy) { return e > 0.0f || (e == 0.0f && (gx > 0.0f || (gx == 0.0f && gy > 0.0f))); };for (int y = miny; y <= maxy; ++y) {const float py = static_cast<float>(y);for (int x = minx; x <= maxx; ++x) {const float px = static_cast<float>(x);- const float w0 = ((x1 - px) * (y2 - py) - (x2 - px) * (y1 - py)) * inv;- const float w1 = ((x2 - px) * (y0 - py) - (x0 - px) * (y2 - py)) * inv;+ const float e0 = (x1 - px) * (y2 - py) - (x2 - px) * (y1 - py);+ const float e1 = (x2 - px) * (y0 - py) - (x0 - px) * (y2 - py);+ const float e2 = (x0 - px) * (y1 - py) - (x1 - px) * (y0 - py);+ 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))+ continue;+ const float w0 = e0 * inv;+ const float w1 = e1 * inv;const float w2 = 1.0f - w0 - w1;- if (w0 < 0.0f || w1 < 0.0f || w2 < 0.0f) continue;plot(s, x, y, lerp3(a0, a1, a2, w0, w1, w2));}}