From 5232627ba2eda0f23fc03cbc569ecae15a4a207d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 9 Oct 2026 21:29:55 +0200 Subject: [PATCH] GEMath: GESpriteUPlaneExact, whether a sprite's u gradient is exact MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A sprite's u gradient comes from the setup reciprocal of its whole area, so it's exact only for a power of two area, whatever the height: the Δy only cancels on paper. Checked against exp87's sizes and exp256, where 64x128 CLUT8 sprites with u = 1 + 2x read the next pixel's low byte on a PSP and 64x272 ones their own high byte. Co-Authored-By: Claude Opus 5.5 --- GPU/Software/GEMath.cpp | 15 +++++++++++++++ GPU/Software/GEMath.h | 6 ++++++ unittest/TestGEMath.cpp | 23 +++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/GPU/Software/GEMath.cpp b/GPU/Software/GEMath.cpp index 8825af7a5b..d183cdbe06 100644 --- a/GPU/Software/GEMath.cpp +++ b/GPU/Software/GEMath.cpp @@ -252,6 +252,21 @@ int64_t GESetupRecip(uint64_t absDet, int *e) { return (2 * (int64_t)seg.k + seg.m * (index & 255)) >> 8; } +bool GESpriteUPlaneExact(int64_t left, int64_t top, int64_t right, int64_t bottom, double sLeft, double sRight) { + const int64_t det = (right - left) * (bottom - top); + if (det <= 0) + return false; + // The plane takes the values as integers at the larger one's exponent, 15 bits (Rasterizer's FixedPlane). + const double larger = std::max(std::fabs(sLeft), std::fabs(sRight)); + const int shift = larger == 0.0 ? 0 : 14 - std::ilogb(larger); + const int64_t n = ((int64_t)std::ldexp(sRight, shift) - (int64_t)std::ldexp(sLeft, shift)) * (bottom - top); + int e; + const int64_t q = GESetupRecip((uint64_t)det, &e); + const int64_t kx = (n * q) >> (e + 2); + // Exact when it's n / det with its 14 fraction bits per subpixel, all there. + return kx * det == n * (1LL << 14); +} + float GELightPow(float v, float e) { if (e <= 0.0f) { return 1.0f; diff --git a/GPU/Software/GEMath.h b/GPU/Software/GEMath.h index f1f91d7523..9185548bce 100644 --- a/GPU/Software/GEMath.h +++ b/GPU/Software/GEMath.h @@ -417,6 +417,12 @@ inline float GENormalize4(Vec4F32 &v) { // The reciprocal triangle setup uses for its planes: q ~ 2^(e + 16) / absDet, e = floor(log2(absDet)). int64_t GESetupRecip(uint64_t absDet, int *e); +// Whether a through-mode sprite's u plane has an exact gradient, so a pixel center whose exact u is on a texel +// edge lands on it rather than just below. The gradient comes from the setup reciprocal of the whole area, +// exact only for a power of two, whatever the height (gpu/probe exp87, exp163). x and y in subpixels, s the +// left and right u divided by the texture width. +bool GESpriteUPlaneExact(int64_t left, int64_t top, int64_t right, int64_t bottom, double sLeft, double sRight); + // The float-bits log2 the mip level selection uses, in 1/16 (a signed 1.27.4 value): the exponent and // the top 4 mantissa bits, a piecewise linear log2 floored to 1/16 (gpu/probe exp58-60). inline int GELog16(float delta) { diff --git a/unittest/TestGEMath.cpp b/unittest/TestGEMath.cpp index d50efdccfb..3059d1b164 100644 --- a/unittest/TestGEMath.cpp +++ b/unittest/TestGEMath.cpp @@ -554,6 +554,28 @@ static bool TestGELineCoverageAlpha() { return true; } +static bool TestGESpriteUPlaneExact() { + // Sprite left, top, right, bottom in pixels, u at the left and right, texture width, and whether the u plane's + // gradient is exact. The 1:1 sizes are exp87's (64x64 samples texel centers on a PSP, the others blend); + // the u = 1 + 2x ones are SOCOM's and Syphon Filter's CLUT8 reads of a 5551 framebuffer. + static const struct { int x0, y0, x1, y1, u0, u1, texW; bool exact; } cases[] = { + { 0, 0, 64, 64, 37, 101, 512, true }, + { 0, 0, 16, 272, 37, 53, 512, false }, + { 0, 0, 480, 16, 37, 517, 512, false }, + { 0, 0, 64, 272, 1, 129, 1024, false }, + { 192, 0, 240, 272, 385, 481, 1024, false }, + { 0, 0, 64, 128, 1, 129, 256, true }, + }; + for (const auto &c : cases) { + const bool exact = GESpriteUPlaneExact(c.x0 * 16, c.y0 * 16, c.x1 * 16, c.y1 * 16, (double)c.u0 / c.texW, (double)c.u1 / c.texW); + if (exact != c.exact) { + printf("GESpriteUPlaneExact(%dx%d, u %d-%d / %d) = %d, want %d\n", c.x1 - c.x0, c.y1 - c.y0, c.u0, c.u1, c.texW, (int)exact, (int)c.exact); + return false; + } + } + return true; +} + bool TestGEMath() { bool ok = true; ok = TestFloat24() && ok; @@ -572,5 +594,6 @@ bool TestGEMath() { ok = TestGELightPow() && ok; ok = TestGELightColor() && ok; ok = TestGELineCoverageAlpha() && ok; + ok = TestGESpriteUPlaneExact() && ok; return ok; }