From 75f105f84b44b6b5bf79178f4c80eee4afbe1a42 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 25 Dec 2021 08:48:27 -0800 Subject: [PATCH] softgpu: Make linear filtering more accurate. This matches tests for various u/v offsets and x/y subpixel offsets. Mipmaps are probably still wrong. --- GPU/Software/Rasterizer.cpp | 39 ++++++++++++---------- GPU/Software/Sampler.cpp | 6 ++-- GPU/Software/SamplerX86.cpp | 64 ++++++++++++++++++++----------------- 3 files changed, 59 insertions(+), 50 deletions(-) diff --git a/GPU/Software/Rasterizer.cpp b/GPU/Software/Rasterizer.cpp index 369b396d63..481225fc0c 100644 --- a/GPU/Software/Rasterizer.cpp +++ b/GPU/Software/Rasterizer.cpp @@ -215,22 +215,22 @@ static inline void GetTexelCoordinates(int level, float s, float t, int& out_u, ApplyTexelClamp<1>(&out_u, &out_v, &base_u, &base_v, width, height); } -static inline Vec4IntResult SOFTRAST_CALL GetTexelCoordinatesQuadS(int level, float in_s, int &frac_u) { +static inline Vec4IntResult SOFTRAST_CALL GetTexelCoordinatesQuadS(int level, float in_s, int &frac_u, int x) { int width = gstate.getTextureWidth(level); - int base_u = (int)(in_s * width * 256.0f + 0.375f) - 128; - frac_u = (int)(base_u) & 0xff; + int base_u = (int)(in_s * width * 256) + 12 - x - 128; + frac_u = (int)(base_u >> 4) & 0x0F; base_u >>= 8; // Need to generate and individually wrap/clamp the four sample coordinates. Ugh. return ApplyTexelClampQuadS(gstate.isTexCoordClampedS(), base_u, width); } -static inline Vec4IntResult SOFTRAST_CALL GetTexelCoordinatesQuadT(int level, float in_t, int &frac_v) { +static inline Vec4IntResult SOFTRAST_CALL GetTexelCoordinatesQuadT(int level, float in_t, int &frac_v, int y) { int height = gstate.getTextureHeight(level); - int base_v = (int)(in_t * height * 256.0f + 0.375f) - 128; - frac_v = (int)(base_v) & 0xff; + int base_v = (int)(in_t * height * 256) + 12 - y - 128; + frac_v = (int)(base_v >> 4) & 0x0F; base_v >>= 8; // Need to generate and individually wrap/clamp the four sample coordinates. Ugh. @@ -594,7 +594,7 @@ Vec3 AlphaBlendingResult(const PixelFuncID &pixelID, const Vec4 &sourc } template -static inline Vec4IntResult SOFTRAST_CALL ApplyTexturing(Sampler::Funcs sampler, Vec4IntArg prim_color, float s, float t, int texlevel, int frac_texlevel, bool bilinear, u8 *texptr[], int texbufw[]) { +static inline Vec4IntResult SOFTRAST_CALL ApplyTexturing(Sampler::Funcs sampler, Vec4IntArg prim_color, float s, float t, int texlevel, int frac_texlevel, bool bilinear, u8 *texptr[], int texbufw[], int x, int y) { int frac_u[2], frac_v[2]; Vec4 texcolor0; @@ -605,7 +605,7 @@ static inline Vec4IntResult SOFTRAST_CALL ApplyTexturing(Sampler::Funcs sampler, int bufw1 = texbufw[mayHaveMipLevels ? texlevel + 1 : 0]; if (!bilinear) { - int u[8] = { 0 }, v[8] = { 0 }; // 1.23.8 fixed point + int u[8] = { 0 }, v[8] = { 0 }; // 1.27.4 fixed point // Nearest filtering only. Round texcoords. GetTexelCoordinates(mayHaveMipLevels ? texlevel : 0, s, t, u[0], v[0]); @@ -618,12 +618,12 @@ static inline Vec4IntResult SOFTRAST_CALL ApplyTexturing(Sampler::Funcs sampler, texcolor1 = Vec4(sampler.nearest(u[1], v[1], tptr1, bufw1, texlevel + 1)); } } else { - Vec4IntResult u = GetTexelCoordinatesQuadS(mayHaveMipLevels ? texlevel : 0, s, frac_u[0]); - Vec4IntResult v = GetTexelCoordinatesQuadT(mayHaveMipLevels ? texlevel : 0, t, frac_v[0]); + Vec4IntResult u = GetTexelCoordinatesQuadS(mayHaveMipLevels ? texlevel : 0, s, frac_u[0], x); + Vec4IntResult v = GetTexelCoordinatesQuadT(mayHaveMipLevels ? texlevel : 0, t, frac_v[0], y); Vec4IntResult u1, v1; if (mayHaveMipLevels && frac_texlevel) { - u1 = GetTexelCoordinatesQuadS(texlevel + 1, s, frac_u[1]); - v1 = GetTexelCoordinatesQuadT(texlevel + 1, t, frac_v[1]); + u1 = GetTexelCoordinatesQuadS(texlevel + 1, s, frac_u[1], x); + v1 = GetTexelCoordinatesQuadT(texlevel + 1, t, frac_v[1], y); } texcolor0 = Vec4(sampler.linear(ToVec4IntArg(u), ToVec4IntArg(v), frac_u[0], frac_v[0], tptr0, bufw0, mayHaveMipLevels ? texlevel : 0)); @@ -638,6 +638,11 @@ static inline Vec4IntResult SOFTRAST_CALL ApplyTexturing(Sampler::Funcs sampler, return GetTextureFunctionOutput(prim_color, ToVec4IntArg(texcolor0)); } +template +static inline Vec4IntResult SOFTRAST_CALL ApplyTexturingSingle(Sampler::Funcs sampler, Vec4IntArg prim_color, float s, float t, int texlevel, int frac_texlevel, bool bilinear, u8 *texptr[], int texbufw[], int x, int y) { + return ApplyTexturing(sampler, prim_color, s, t, texlevel, frac_texlevel, bilinear, texptr, texbufw, ((x & 15) + 1) / 2, ((y & 15) + 1) / 2); +} + // Produces a signed 1.23.8 value. static int TexLog2(float delta) { union FloatBits { @@ -705,7 +710,7 @@ static inline void CalculateSamplingParams(const float ds, const float dt, const } template -static inline void ApplyTexturing(Sampler::Funcs sampler, Vec4 *prim_color, const Vec4 &s, const Vec4 &t, int maxTexLevel, u8 *texptr[], int texbufw[]) { +static inline void ApplyTexturing(Sampler::Funcs sampler, Vec4 *prim_color, const Vec4 &s, const Vec4 &t, int maxTexLevel, u8 *texptr[], int texbufw[], int x, int y) { float ds = s[1] - s[0]; float dt = t[2] - t[0]; @@ -715,7 +720,7 @@ static inline void ApplyTexturing(Sampler::Funcs sampler, Vec4 *prim_color, CalculateSamplingParams(ds, dt, maxTexLevel, level, levelFrac, bilinear); for (int i = 0; i < 4; ++i) { - prim_color[i] = ApplyTexturing(sampler, ToVec4IntArg(prim_color[i]), s[i], t[i], level, levelFrac, bilinear, texptr, texbufw); + prim_color[i] = ApplyTexturing(sampler, ToVec4IntArg(prim_color[i]), s[i], t[i], level, levelFrac, bilinear, texptr, texbufw, ((x & 15) + 1) / 2, ((y & 15) + 1) / 2); } } @@ -903,7 +908,7 @@ void DrawTriangleSlice( GetTextureCoordinates(v0, v1, v2, w0, w1, w2, wsum_recip, s, t); } - ApplyTexturing(sampler, prim_color, s, t, maxTexLevel, texptr, texbufw); + ApplyTexturing(sampler, prim_color, s, t, maxTexLevel, texptr, texbufw, curX, curY); } if (!clearMode) { @@ -1064,7 +1069,7 @@ void DrawPoint(const VertexData &v0) int texLevelFrac; bool bilinear; CalculateSamplingParams(0.0f, 0.0f, maxTexLevel, texLevel, texLevelFrac, bilinear); - prim_color = ApplyTexturing(sampler, ToVec4IntArg(prim_color), s, t, texLevel, texLevelFrac, bilinear, texptr, texbufw); + prim_color = ApplyTexturingSingle(sampler, ToVec4IntArg(prim_color), s, t, texLevel, texLevelFrac, bilinear, texptr, texbufw, pos.x, pos.y); } if (!pixelID.clearMode) @@ -1361,7 +1366,7 @@ void DrawLine(const VertexData &v0, const VertexData &v1) texBilinear = true; } - prim_color = ApplyTexturing(sampler, ToVec4IntArg(prim_color), s, t, texLevel, texLevelFrac, texBilinear, texptr, texbufw); + prim_color = ApplyTexturingSingle(sampler, ToVec4IntArg(prim_color), s, t, texLevel, texLevelFrac, texBilinear, texptr, texbufw, x, y); } if (!pixelID.clearMode) diff --git a/GPU/Software/Sampler.cpp b/GPU/Software/Sampler.cpp index 59bff23efe..f6504bd1d2 100644 --- a/GPU/Software/Sampler.cpp +++ b/GPU/Software/Sampler.cpp @@ -423,9 +423,9 @@ static Vec4IntResult SOFTRAST_CALL SampleLinear(Vec4IntArg u_in, Vec4IntArg v_in Vec4 texcolor_tr = Vec4::FromRGBA(c.v[1]); Vec4 texcolor_bl = Vec4::FromRGBA(c.v[2]); Vec4 texcolor_br = Vec4::FromRGBA(c.v[3]); - Vec4 t = texcolor_tl * (0x100 - frac_u) + texcolor_tr * frac_u; - Vec4 b = texcolor_bl * (0x100 - frac_u) + texcolor_br * frac_u; - return ToVec4IntResult((t * (0x100 - frac_v) + b * frac_v) / (256 * 256)); + Vec4 t = texcolor_tl * (0x10 - frac_u) + texcolor_tr * frac_u; + Vec4 b = texcolor_bl * (0x10 - frac_u) + texcolor_br * frac_u; + return ToVec4IntResult((t * (0x10 - frac_v) + b * frac_v) / (16 * 16)); } }; diff --git a/GPU/Software/SamplerX86.cpp b/GPU/Software/SamplerX86.cpp index 0dc2b4b842..2004ad1a06 100644 --- a/GPU/Software/SamplerX86.cpp +++ b/GPU/Software/SamplerX86.cpp @@ -163,12 +163,12 @@ LinearFunc SamplerJitCache::CompileLinear(const SamplerID &id) { regCache_.Reset(true); // Let's drop some helpful constants here. - const u8 *const100_11_4s = AlignCode16(); - Write16(0x100 << 4); Write16(0x100 << 4); Write16(0x100 << 4); Write16(0x100 << 4); - Write16(0x100 << 4); Write16(0x100 << 4); Write16(0x100 << 4); Write16(0x100 << 4); + const u8 *const10All = AlignCode16(); + Write16(0x10); Write16(0x10); Write16(0x10); Write16(0x10); + Write16(0x10); Write16(0x10); Write16(0x10); Write16(0x10); - const u8 *const100Low_11_4s = AlignCode16(); - Write16(0x100 << 4); Write16(0x100 << 4); Write16(0x100 << 4); Write16(0x100 << 4); + const u8 *const10Low = AlignCode16(); + Write16(0x10); Write16(0x10); Write16(0x10); Write16(0x10); Write16(0); Write16(0); Write16(0); Write16(0); // Now the actual linear func, which is exposed externally. @@ -258,51 +258,55 @@ LinearFunc SamplerJitCache::CompileLinear(const SamplerID &id) { doNearestCall(8); doNearestCall(12); - // fpScratchReg1 will have top RRRRRRRR LLLLLLLL, fpScratchReg2 for bottom. - // Start with XXXX XXXX RRRR LLLL, and swizzle the 8 bits to both slots in the 16. - PSHUFD(fpScratchReg1, R(XMM5), _MM_SHUFFLE(0, 0, 1, 0)); - PSHUFD(fpScratchReg2, R(XMM5), _MM_SHUFFLE(0, 0, 3, 2)); - PUNPCKLBW(fpScratchReg1, R(fpScratchReg1)); - PUNPCKLBW(fpScratchReg2, R(fpScratchReg2)); + // First put the top RRRRRRRR LLLLLLLL into fpScratchReg1, bottom into fpScratchReg2. + // Start with XXXX XXXX RRRR LLLL, and then expand 8 bits to 16 bits. + if (!cpu_info.bSSE4_1) { + PXOR(fpScratchReg3, R(fpScratchReg3)); + PSHUFD(fpScratchReg1, R(XMM5), _MM_SHUFFLE(0, 0, 1, 0)); + PSHUFD(fpScratchReg2, R(XMM5), _MM_SHUFFLE(0, 0, 3, 2)); + PUNPCKLBW(fpScratchReg1, R(fpScratchReg3)); + PUNPCKLBW(fpScratchReg2, R(fpScratchReg3)); + } else { + PSHUFD(fpScratchReg2, R(XMM5), _MM_SHUFFLE(0, 0, 3, 2)); + PMOVZXBW(fpScratchReg1, R(XMM5)); + PMOVZXBW(fpScratchReg2, R(fpScratchReg2)); + } // Grab frac_u and spread to lower (L) lanes. MOVD_xmm(fpScratchReg5, MDisp(RSP, 0)); PSHUFLW(fpScratchReg5, R(fpScratchReg5), _MM_SHUFFLE(0, 0, 0, 0)); - // Convert to s.11.4. - PSLLW(fpScratchReg5, 4); - // Now subtract 0x100 - frac_u in the L lanes only: 00000000 LLLLLLLL. - MOVDQA(fpScratchReg3, M(const100Low_11_4s)); + // Now subtract 0x10 - frac_u in the L lanes only: 00000000 LLLLLLLL. + MOVDQA(fpScratchReg3, M(const10Low)); PSUBW(fpScratchReg3, R(fpScratchReg5)); - // Now we just shift and OR in the original frac_u. + // Then we just shift and OR in the original frac_u. PSLLDQ(fpScratchReg5, 8); POR(fpScratchReg3, R(fpScratchReg5)); - // Okay, we have 8-bits repeated in the top and bottom rows for the color. - // Shift frac by 4, and multiply to get the top 16 bits - that will give us 12 bits of result. - PMULHUW(fpScratchReg1, R(fpScratchReg3)); - PMULHUW(fpScratchReg2, R(fpScratchReg3)); + // Okay, we have 8-bits in the top and bottom rows for the color. + // Multiply by frac to get 12, which we keep for the next stage. + PMULLW(fpScratchReg1, R(fpScratchReg3)); + PMULLW(fpScratchReg2, R(fpScratchReg3)); // Time for frac_v. This time, we want it in all 8 lanes. MOVD_xmm(fpScratchReg5, MDisp(RSP, 8)); PSHUFLW(fpScratchReg5, R(fpScratchReg5), _MM_SHUFFLE(0, 0, 0, 0)); PSHUFD(fpScratchReg5, R(fpScratchReg5), _MM_SHUFFLE(0, 0, 0, 0)); - // We need to shift before multiplying to get the 8 bits we want. - PSLLW(fpScratchReg5, 4); // Now, inverse fpScratchReg5 into fpScratchReg3 for the top row. - MOVDQA(fpScratchReg3, M(const100_11_4s)); + MOVDQA(fpScratchReg3, M(const10All)); PSUBW(fpScratchReg3, R(fpScratchReg5)); - // We had 12, plus 8 frac and 4 shift, that gives us 24. Take the top 8 bits. - PMULHUW(fpScratchReg2, R(fpScratchReg5)); - PMULHUW(fpScratchReg1, R(fpScratchReg3)); + // We had 12, plus 4 frac, that gives us 16. + PMULLW(fpScratchReg2, R(fpScratchReg5)); + PMULLW(fpScratchReg1, R(fpScratchReg3)); - // Finally, time to sum them all up. - PADDW(fpScratchReg2, R(fpScratchReg1)); + // Finally, time to sum them all up and divide by 256 to get back to 8 bits. + PADDUSW(fpScratchReg2, R(fpScratchReg1)); PSHUFD(XMM0, R(fpScratchReg2), _MM_SHUFFLE(3, 2, 3, 2)); - PADDW(XMM0, R(fpScratchReg2)); + PADDUSW(XMM0, R(fpScratchReg2)); + PSRLW(XMM0, 8); - // Finally, convert to 32-bit channels. + // Last of all, convert to 32-bit channels. if (cpu_info.bSSE4_1) { PMOVZXWD(XMM0, R(XMM0)); } else {