From 524fac1c32c9c66a94232ded7a0b7e8d11f52ef7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 30 Sep 2026 20:19:10 -0600 Subject: [PATCH] SoftGPU: Clip at the near plane like the GE Measured with gpu/probe exp43-47, bit exact: - New vertices are interpolated from the inside vertex, t = d_in / (d_in - d_out) with d = z + w through the GE adder, using the GE's reciprocal; out - in is truncated to float24 before the multiply by t, and t and each coordinate use float24 products and adds. - With one vertex outside, the quad is split from the vertex before it in the GE's order: (p, a, b) and (p, b, n). The GE takes odd strip triangles in reverse and fan triangles in order, so SendTriangle now says when the vertices it gets are reversed from that. Co-Authored-By: Claude Opus 5.5 (1M context) --- GPU/Software/Clipper.cpp | 157 ++++++++++++--------------------- GPU/Software/Clipper.h | 2 +- GPU/Software/TransformUnit.cpp | 39 ++++++-- GPU/Software/TransformUnit.h | 6 +- 4 files changed, 92 insertions(+), 112 deletions(-) diff --git a/GPU/Software/Clipper.cpp b/GPU/Software/Clipper.cpp index bc84342bb1..74a075078f 100644 --- a/GPU/Software/Clipper.cpp +++ b/GPU/Software/Clipper.cpp @@ -28,7 +28,6 @@ namespace Clipper { enum { - SKIP_FLAG = -1, CLIP_NEG_Z_BIT = 0x20, }; @@ -65,51 +64,6 @@ inline void clip_interpolate(ClipVertexData &dest, float t, const ClipVertexData dest.v.screenpos.x = 0x7FFFFFFF; } -#define CLIP_POLY( PLANE_BIT, A, B, C, D ) \ -{ \ - if (mask & PLANE_BIT) { \ - int idxPrev = inlist[0]; \ - float dpPrev = clip_dotprod(*Vertices[idxPrev], A, B, C, D );\ - int outcount = 0; \ - \ - inlist[n] = inlist[0]; \ - for (int j = 1; j <= n; j++) { \ - int idx = inlist[j]; \ - float dp = clip_dotprod(*Vertices[idx], A, B, C, D ); \ - if (dpPrev >= 0) { \ - outlist[outcount++] = idxPrev; \ - } \ - \ - /* Skipping w sign mismatches avoids inversions, but is incorrect. See #16131. */ \ - /* For now, it's better to avoid inversions as they usually are undesired. */ \ - if (different_signs(dp, dpPrev)) { \ - auto &vert = Vertices[numVertices++]; \ - if (dp < 0) { \ - float t = dp / (dp - dpPrev); \ - clip_interpolate(*vert, t, *Vertices[idx], *Vertices[idxPrev]); \ - } else { \ - float t = dpPrev / (dpPrev - dp); \ - clip_interpolate(*vert, t, *Vertices[idxPrev], *Vertices[idx]); \ - } \ - outlist[outcount++] = numVertices - 1; \ - } \ - \ - idxPrev = idx; \ - dpPrev = dp; \ - } \ - \ - if (outcount < 3) \ - continue; \ - \ - { \ - int *tmp = inlist; \ - inlist = outlist; \ - outlist = tmp; \ - n = outcount; \ - } \ - } \ -} - #define CLIP_LINE(PLANE_BIT, A, B, C, D) \ { \ if (mask & PLANE_BIT) { \ @@ -322,7 +276,7 @@ void ProcessLine(const ClipVertexData &v0, const ClipVertexData &v1, BinManager binner.AddLine(data[0].v, data[1].v); } -void ProcessTriangle(const ClipVertexData &v0, const ClipVertexData &v1, const ClipVertexData &v2, const ClipVertexData &provoking, BinManager &binner) { +void ProcessTriangle(const ClipVertexData &v0, const ClipVertexData &v1, const ClipVertexData &v2, const ClipVertexData &provoking, BinManager &binner, bool reversed) { int mask = 0; if (!binner.State().throughMode) { const bool depthClip = gstate.isDepthClipEnabled(); @@ -369,65 +323,66 @@ void ProcessTriangle(const ClipVertexData &v0, const ClipVertexData &v1, const C return; } - enum { NUM_CLIPPED_VERTICES = 3, NUM_INDICES = NUM_CLIPPED_VERTICES + 3 }; - - ClipVertexData* Vertices[NUM_INDICES]; - ClipVertexData ClippedVertices[NUM_INDICES]; - for (int i = 0; i < NUM_INDICES; ++i) - Vertices[i] = &ClippedVertices[i]; - - // TODO: Change logic when it's a backface (why? In what way?) - ClippedVertices[0] = v0; - ClippedVertices[1] = v1; - ClippedVertices[2] = v2; - - int indices[NUM_INDICES] = { 0, 1, 2, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG }; - int numIndices = 3; - - for (int i = 0; i < 3; i += 3) { - int vlist[2][2*6+1]; - int *inlist = vlist[0], *outlist = vlist[1]; - int n = 3; - int numVertices = 3; - - inlist[0] = 0; - inlist[1] = 1; - inlist[2] = 2; - - // mark this triangle as unused in case it should be completely clipped - indices[0] = SKIP_FLAG; - indices[1] = SKIP_FLAG; - indices[2] = SKIP_FLAG; - - // The PSP only clips on negative Z (importantly, regardless of viewport.) - CLIP_POLY(CLIP_NEG_Z_BIT, 0, 0, 1, 1); - - // transform the poly in inlist into triangles - indices[0] = inlist[0]; - indices[1] = inlist[1]; - indices[2] = inlist[2]; - for (int j = 3; j < n; ++j) { - indices[numIndices++] = inlist[0]; - indices[numIndices++] = inlist[j - 1]; - indices[numIndices++] = inlist[j]; - } + // Clip at the near plane like the GE (gpu/probe exp43, exp44): new vertices are interpolated from + // the inside vertex with the GE's math, and with one vertex outside (o), the quad is split from the + // vertex before it (p) in the submitted order: (p, a, b) and (p, b, n), a and b being the new + // vertices on p-o and o-n. reversed means we got the vertices in the opposite order. + const ClipVertexData *src[3] = { &v0, &v1, &v2 }; + bool outside[3]; + int numOutside = 0; + for (int i = 0; i < 3; ++i) { + outside[i] = src[i]->clippos.z < -src[i]->clippos.w; + numOutside += outside[i] ? 1 : 0; } - for (int i = 0; i + 3 <= numIndices; i += 3) { - if (indices[i] != SKIP_FLAG) { - ClipVertexData &subv0 = *Vertices[indices[i + 0]]; - ClipVertexData &subv1 = *Vertices[indices[i + 1]]; - ClipVertexData &subv2 = *Vertices[indices[i + 2]]; + auto nearPoint = [](ClipVertexData &dest, const ClipVertexData &in, const ClipVertexData &out) { + const float t = TransformUnit::NearPlaneT(in.clippos, out.clippos); + dest.Lerp(t, in, out); + dest.clippos = TransformUnit::NearPlanePoint(in.clippos, out.clippos, t); + bool outsideRange = false; + dest.v.screenpos = TransformUnit::ClipToScreen(dest.clippos, &outsideRange); + dest.v.clipw = dest.clippos.w; + if (outsideRange) + dest.v.screenpos.x = 0x7FFFFFFF; + }; - if (subv0.OutsideRange() || subv1.OutsideRange() || subv2.OutsideRange()) - continue; + ClipVertexData made[2]; + const ClipVertexData *tris[2][3]; + int numTris = 0; + if (numOutside == 1) { + const int o = outside[0] ? 0 : (outside[1] ? 1 : 2); + const int p = reversed ? (o + 1) % 3 : (o + 2) % 3; + const int n = reversed ? (o + 2) % 3 : (o + 1) % 3; + nearPoint(made[0], *src[p], *src[o]); + nearPoint(made[1], *src[n], *src[o]); + // Keep the winding we were given. + const int first = reversed ? 2 : 1, second = reversed ? 1 : 2; + tris[0][0] = src[p]; tris[0][first] = &made[0]; tris[0][second] = &made[1]; + tris[1][0] = src[p]; tris[1][first] = &made[1]; tris[1][second] = src[n]; + numTris = 2; + } else if (numOutside == 2) { + const int i = !outside[0] ? 0 : (!outside[1] ? 1 : 2); + const int n = (i + 1) % 3, p = (i + 2) % 3; + nearPoint(made[0], *src[i], *src[n]); + nearPoint(made[1], *src[i], *src[p]); + tris[0][0] = src[i]; tris[0][1] = &made[0]; tris[0][2] = &made[1]; + numTris = 1; + } - if (gstate.getShadeMode() == GE_SHADE_FLAT) { - // So that the order of clipping doesn't matter... - subv2.v.color0 = provoking.v.color0; - subv2.v.color1 = provoking.v.color1; - } + for (int t = 0; t < numTris; ++t) { + const ClipVertexData &subv0 = *tris[t][0]; + const ClipVertexData &subv1 = *tris[t][1]; + const ClipVertexData &subv2 = *tris[t][2]; + if (subv0.OutsideRange() || subv1.OutsideRange() || subv2.OutsideRange()) + continue; + if (gstate.getShadeMode() == GE_SHADE_FLAT) { + // So that the order of clipping doesn't matter... + VertexData corrected2 = subv2.v; + corrected2.color0 = provoking.v.color0; + corrected2.color1 = provoking.v.color1; + binner.AddTriangle(subv0.v, subv1.v, corrected2); + } else { binner.AddTriangle(subv0.v, subv1.v, subv2.v); } } diff --git a/GPU/Software/Clipper.h b/GPU/Software/Clipper.h index 588fd186c9..ab6496dd9b 100644 --- a/GPU/Software/Clipper.h +++ b/GPU/Software/Clipper.h @@ -28,7 +28,7 @@ namespace Clipper { void ProcessPoint(const ClipVertexData &v0, BinManager &binner); void ProcessLine(const ClipVertexData &v0, const ClipVertexData &v1, BinManager &binner); -void ProcessTriangle(const ClipVertexData &v0, const ClipVertexData &v1, const ClipVertexData &v2, const ClipVertexData &provoking, BinManager &binner); +void ProcessTriangle(const ClipVertexData &v0, const ClipVertexData &v1, const ClipVertexData &v2, const ClipVertexData &provoking, BinManager &binner, bool reversed = false); void ProcessRect(const ClipVertexData &v0, const ClipVertexData &v1, BinManager &binner); } // namespace diff --git a/GPU/Software/TransformUnit.cpp b/GPU/Software/TransformUnit.cpp index 2ac3ac9189..6c03315168 100644 --- a/GPU/Software/TransformUnit.cpp +++ b/GPU/Software/TransformUnit.cpp @@ -367,6 +367,24 @@ ScreenCoords TransformUnit::ClipToScreen(const ClipCoords &coords, bool *outside return ClipToScreenInternal(coords, outsideRangeFlag); } +// Near plane clipping (gpu/probe exp43-46): from the inside vertex, t = d_in / (d_in - d_out) with +// d = z + w, using the GE's reciprocal, and each coordinate is in + t * (out - in), all in float24 math. +float TransformUnit::NearPlaneT(const ClipCoords &in, const ClipCoords &out) { + const float dIn = GEAdd(in.z, in.w); + const float dOut = GEAdd(out.z, out.w); + const float den = TruncateToFloat24(GEAdd(dIn, -dOut)); + return ProductToFloat24((double)TruncateToFloat24(dIn) * GERecip(den)); +} + +ClipCoords TransformUnit::NearPlanePoint(const ClipCoords &in, const ClipCoords &out, float t) { + ClipCoords result; + for (int c = 0; c < 4; ++c) { + const float delta = TruncateToFloat24(GEAdd(out[c], -in[c])); + result[c] = TruncateToFloat24(GEAdd(ProductToFloat24((double)t * delta), in[c])); + } + return result; +} + ScreenCoords TransformUnit::DrawingToScreen(const DrawingCoords &coords, u16 z) { ScreenCoords ret; ret.x = (u32)coords.x * SCREEN_SCALE_FACTOR; @@ -918,7 +936,8 @@ void TransformUnit::SubmitPrimitive(const void* vertices, const void* indices, G int wind = (data_index_ - 1) % 2; CullType altCullType = cullType == CullType::OFF ? cullType : CullType((int)cullType ^ wind); - SendTriangle(altCullType, &data_[0], provoking_index); + // Odd triangles reach us in the opposite order from the GE's (gpu/probe exp45). + SendTriangle(altCullType, &data_[0], provoking_index, wind != 0); } // If this is from immediate-mode drawing, we always had one new vert (already in data_.) @@ -926,7 +945,8 @@ void TransformUnit::SubmitPrimitive(const void* vertices, const void* indices, G int provoking_index = (data_index_ - 1) % 3; int wind = (data_index_ - 1) % 2; CullType altCullType = cullType == CullType::OFF ? cullType : CullType((int)cullType ^ wind); - SendTriangle(altCullType, &data_[0], provoking_index); + // Odd triangles reach us in the opposite order from the GE's (gpu/probe exp45). + SendTriangle(altCullType, &data_[0], provoking_index, wind != 0); } break; } @@ -970,7 +990,8 @@ void TransformUnit::SubmitPrimitive(const void* vertices, const void* indices, G int wind = (data_index_ - 1) % 2; CullType altCullType = cullType == CullType::OFF ? cullType : CullType((int)cullType ^ wind); - SendTriangle(altCullType, &data_[0], provoking_index); + // Odd triangles reach us in the opposite order from the GE's (gpu/probe exp45). + SendTriangle(altCullType, &data_[0], provoking_index, wind != 0); } // If this is from immediate-mode drawing, we always had one new vert (already in data_.) @@ -978,7 +999,7 @@ void TransformUnit::SubmitPrimitive(const void* vertices, const void* indices, G int wind = (data_index_ - 1) % 2; int provoking_index = 2 - wind; CullType altCullType = cullType == CullType::OFF ? cullType : CullType((int)cullType ^ wind); - SendTriangle(altCullType, &data_[0], provoking_index); + SendTriangle(altCullType, &data_[0], provoking_index, wind != 0); } break; } @@ -1031,14 +1052,14 @@ void TransformUnit::SubmitImmVertex(const ClipVertexData &vert, SoftwareDrawEngi isImmDraw_ = false; } -void TransformUnit::SendTriangle(CullType cullType, const ClipVertexData *verts, int provoking) { +void TransformUnit::SendTriangle(CullType cullType, const ClipVertexData *verts, int provoking, bool orderReversed) { if (cullType == CullType::OFF) { - Clipper::ProcessTriangle(verts[0], verts[1], verts[2], verts[provoking], *binner_); - Clipper::ProcessTriangle(verts[2], verts[1], verts[0], verts[provoking], *binner_); + Clipper::ProcessTriangle(verts[0], verts[1], verts[2], verts[provoking], *binner_, orderReversed); + Clipper::ProcessTriangle(verts[2], verts[1], verts[0], verts[provoking], *binner_, !orderReversed); } else if (cullType == CullType::CW) { - Clipper::ProcessTriangle(verts[2], verts[1], verts[0], verts[provoking], *binner_); + Clipper::ProcessTriangle(verts[2], verts[1], verts[0], verts[provoking], *binner_, !orderReversed); } else { - Clipper::ProcessTriangle(verts[0], verts[1], verts[2], verts[provoking], *binner_); + Clipper::ProcessTriangle(verts[0], verts[1], verts[2], verts[provoking], *binner_, orderReversed); } } diff --git a/GPU/Software/TransformUnit.h b/GPU/Software/TransformUnit.h index d0e2a5bb78..322a9a0f09 100644 --- a/GPU/Software/TransformUnit.h +++ b/GPU/Software/TransformUnit.h @@ -119,6 +119,9 @@ public: static WorldCoords ModelToWorldNormal(const ModelCoords& coords); static WorldCoords ModelToWorld(const ModelCoords& coords); static ScreenCoords ClipToScreen(const ClipCoords &coords, bool *outsideRangeFlag); + // Where an edge from an inside vertex crosses the near plane, as the GE computes it. + static float NearPlaneT(const ClipCoords &in, const ClipCoords &out); + static ClipCoords NearPlanePoint(const ClipCoords &in, const ClipCoords &out, float t); static inline DrawingCoords ScreenToDrawing(int x, int y) { DrawingCoords ret; // When offset > coord, this is negative and force-scissors. @@ -145,7 +148,8 @@ public: private: ClipVertexData ReadVertex(const VertexReader &vreader, const TransformState &state); - void SendTriangle(CullType cullType, const ClipVertexData *verts, int provoking = 2); + // orderReversed: verts are in the opposite order of how the GE takes the triangle (matters for clipping). + void SendTriangle(CullType cullType, const ClipVertexData *verts, int provoking = 2, bool orderReversed = false); u8 *decoded_ = nullptr; BinManager *binner_ = nullptr;