GEMath: GESpriteUPlaneExact, whether a sprite's u gradient is exact

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 <noreply@anthropic.com>
This commit is contained in:
Henrik RydgårdandClaude Opus 5.5 committed 2026-10-10 01:04:23 +02:00
1 parent 962c542f6f
commit 5232627ba2
3 files changed
+44

No files matched your search

+15
View File
@@ -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;
+6
View File
@@ -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) {
+23
View File
@@ -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;
}