diff --git a/GPU/Software/Sampler.cpp b/GPU/Software/Sampler.cpp index 216c7219a4..3df075998b 100644 --- a/GPU/Software/Sampler.cpp +++ b/GPU/Software/Sampler.cpp @@ -962,23 +962,16 @@ static inline Vec4S32 ColumnOffsets4(Vec4S32 u) { return swizzled ? b.Shr<4>().Shl<7>() + (b & Vec4S32::Splat(15)) : b; } -// SampleLinearLevelT for four pixels at the same level: the coordinates and offsets in vector lanes, the -// texel reads one by one. +// The four texels of SampleLinearLevelT for four pixels at the same level (texels[pixel][tl, tr, bl, br]), +// the coordinates and offsets in vector lanes, the texel reads one by one. template -static inline void SampleLinearLevel4T(Vec4F32 s, Vec4F32 t, const u8 *tptr, int bufw, int level, const SamplerID &samplerID, uint64_t out[4]) { - if (!tptr) { - out[0] = out[1] = out[2] = out[3] = 0; - return; - } - Vec4S32 u0, u1, v0, v1, fracU, fracV; +static inline void ReadTexels4T(Vec4F32 s, Vec4F32 t, const u8 *tptr, int bufw, int level, const SamplerID &samplerID, uint32_t texels[4][4], Vec4S32 &fracU, Vec4S32 &fracV) { + Vec4S32 u0, u1, v0, v1; TexelPairs4(s, samplerID.cached.sizes[level].w, samplerID.clampS, u0, u1, fracU); TexelPairs4(t, samplerID.cached.sizes[level].h, samplerID.clampT, v0, v1, fracV); - alignas(16) int us0[4], us1[4], fu[4], fv[4]; + alignas(16) int us0[4], us1[4]; u0.Store(us0); u1.Store(us1); - fracU.Store(fu); - fracV.Store(fv); - uint32_t texels[4][4]; if constexpr (fmt == GE_TFMT_DXT1 || fmt == GE_TFMT_DXT3 || fmt == GE_TFMT_DXT5) { alignas(16) int vs0[4], vs1[4]; v0.Store(vs0); @@ -1008,14 +1001,49 @@ static inline void SampleLinearLevel4T(Vec4F32 s, Vec4F32 t, const u8 *tptr, int texels[i][3] = ReadTexelT(tptr + (uint32_t)br[i], us1[i], clutOffset, samplerID); } } +} + +// LerpSpread of one channel in vector lanes. +static inline Vec4S32 LerpLanes(Vec4S32 a, Vec4S32 b, Vec4S32 f) { + return (a * (Vec4S32::Splat(16) - f) + b * f).Shr<4>(); +} + +// SampleLinearLevelT for four pixels at the same level, a channel per vector (as TextureFunction4 takes them). +template +static inline void SampleLinearLevel4LanesT(Vec4F32 s, Vec4F32 t, const u8 *tptr, int bufw, int level, const SamplerID &samplerID, Vec4S32 out[4]) { + if (!tptr) { + out[0] = out[1] = out[2] = out[3] = Vec4S32::Zero(); + return; + } + alignas(16) uint32_t texels[4][4]; + Vec4S32 fracU, fracV; + ReadTexels4T(s, t, tptr, bufw, level, samplerID, texels, fracU, fracV); + // Texels by corner, each vector a corner's texel for the four pixels. + alignas(16) int corners[4][4]; for (int i = 0; i < 4; ++i) { + for (int k = 0; k < 4; ++k) + corners[k][i] = (int)texels[i][k]; + } + const Vec4S32 tl = Vec4S32::LoadAligned(corners[0]), tr = Vec4S32::LoadAligned(corners[1]); + const Vec4S32 bl = Vec4S32::LoadAligned(corners[2]), br = Vec4S32::LoadAligned(corners[3]); + const Vec4S32 byteMask = Vec4S32::Splat(0xFF); + auto channel = [&](Vec4S32 v, int c) { + switch (c) { + case 0: return v & byteMask; + case 1: return v.Shr<8>() & byteMask; + case 2: return v.Shr<16>() & byteMask; + default: return v.Shr<24>() & byteMask; + } + }; + for (int c = 0; c < 4; ++c) { // Like the GE: horizontal lerps truncated to 8 bits, then the vertical one (gpu/probe exp52). - const uint64_t top = LerpSpread(SpreadRGBA(texels[i][0]), SpreadRGBA(texels[i][1]), fu[i]); - const uint64_t bot = LerpSpread(SpreadRGBA(texels[i][2]), SpreadRGBA(texels[i][3]), fu[i]); - out[i] = LerpSpread(top, bot, fv[i]); + const Vec4S32 top = LerpLanes(channel(tl, c), channel(tr, c), fracU); + const Vec4S32 bot = LerpLanes(channel(bl, c), channel(br, c), fracU); + out[c] = LerpLanes(top, bot, fracV); } } + // GetTextureFunctionOutput for four pixels, one per lane: prim and tex are R, G, B and A (all 0 to 255). static inline void TextureFunction4(const Vec4S32 prim[4], const Vec4S32 tex[4], const SamplerID &samplerID, Vec4S32 out[4]) { const bool rgba = samplerID.useTextureAlpha; @@ -1087,24 +1115,18 @@ static void SOFTRAST_CALL SampleLinearQuadT(const float *s, const float *t, cons if (sameLevel) { const int l = level[first]; const Vec4F32 sv = Vec4F32::Load(s), tv = Vec4F32::Load(t); - SampleLinearLevel4T(sv, tv, texptr[l], texbufw[l], l, samplerID, c); + Vec4S32 prim[4], tex[4], out[4]; + SampleLinearLevel4LanesT(sv, tv, texptr[l], texbufw[l], l, samplerID, tex); if (levelFrac[first]) { - uint64_t c1[4]; - SampleLinearLevel4T(sv, tv, texptr[l + 1], texbufw[l + 1], l + 1, samplerID, c1); - for (int i = 0; i < 4; ++i) - c[i] = LerpSpread(c[i], c1[i], levelFrac[first]); + Vec4S32 tex1[4]; + SampleLinearLevel4LanesT(sv, tv, texptr[l + 1], texbufw[l + 1], l + 1, samplerID, tex1); + const Vec4S32 f = Vec4S32::Splat(levelFrac[first]); + for (int ch = 0; ch < 4; ++ch) + tex[ch] = LerpLanes(tex[ch], tex1[ch], f); } // The texture function, in lanes too. - alignas(16) int texLanes[4][4]; - for (int i = 0; i < 4; ++i) { - for (int ch = 0; ch < 4; ++ch) - texLanes[ch][i] = (int)((c[i] >> (16 * ch)) & 0xFFFF); - } - Vec4S32 prim[4], tex[4], out[4]; - for (int ch = 0; ch < 4; ++ch) { + for (int ch = 0; ch < 4; ++ch) prim[ch] = Vec4S32::Load(colors + ch * colorStride); - tex[ch] = Vec4S32::Load(texLanes[ch]); - } TextureFunction4(prim, tex, samplerID, out); alignas(16) static const int laneBits[4] = { 1, 2, 4, 8 }; const Vec4S32 keep = (Vec4S32::Splat(active) & Vec4S32::LoadAligned(laneBits)).CompareEq(Vec4S32::Zero());