diff --git a/GPU/Software/Rasterizer.cpp b/GPU/Software/Rasterizer.cpp index d75f7f9476..e6d4a5a84f 100644 --- a/GPU/Software/Rasterizer.cpp +++ b/GPU/Software/Rasterizer.cpp @@ -303,18 +303,18 @@ static inline bool IsRightSideOrFlatBottomLine(const Vec2& vertex, const Ve } } -static inline Vec4IntResult SOFTRAST_CALL ApplyTexturing(float s, float t, int x, int y, Vec4IntArg prim_color, int texlevel, int frac_texlevel, bool bilinear, const RasterizerState &state) { +static inline Vec4IntResult SOFTRAST_CALL ApplyTexturing(float s, float t, Vec4IntArg prim_color, int texlevel, int frac_texlevel, bool bilinear, const RasterizerState &state) { const u8 **tptr0 = const_cast(&state.texptr[texlevel]); const uint16_t *bufw0 = &state.texbufw[texlevel]; if (!bilinear) { - return state.nearest(s, t, x, y, prim_color, tptr0, bufw0, texlevel, frac_texlevel, state.samplerID); + return state.nearest(s, t, prim_color, tptr0, bufw0, texlevel, frac_texlevel, state.samplerID); } - return state.linear(s, t, x, y, prim_color, tptr0, bufw0, texlevel, frac_texlevel, state.samplerID); + return state.linear(s, t, prim_color, tptr0, bufw0, texlevel, frac_texlevel, state.samplerID); } -static inline Vec4IntResult SOFTRAST_CALL ApplyTexturingSingle(float s, float t, int x, int y, Vec4IntArg prim_color, int texlevel, int frac_texlevel, bool bilinear, const RasterizerState &state) { - return ApplyTexturing(s, t, ((x & 15) + 1) / 2, ((y & 15) + 1) / 2, prim_color, texlevel, frac_texlevel, bilinear, state); +static inline Vec4IntResult SOFTRAST_CALL ApplyTexturingSingle(float s, float t, Vec4IntArg prim_color, int texlevel, int frac_texlevel, bool bilinear, const RasterizerState &state) { + return ApplyTexturing(s, t, prim_color, texlevel, frac_texlevel, bilinear, state); } // Produces a signed 1.27.4 value. @@ -332,7 +332,7 @@ static int TexLog2(float delta) { return useful - 127 * 16; } -static inline void CalculateSamplingParams(const float ds, const float dt, const RasterizerState &state, int &level, int &levelFrac, bool &filt) { +static inline void CalculateSamplingParams(const float ds, const float dt, float w, const RasterizerState &state, int &level, int &levelFrac, bool &filt) { const int width = 1 << state.samplerID.width0Shift; const int height = 1 << state.samplerID.height0Shift; @@ -340,11 +340,11 @@ static inline void CalculateSamplingParams(const float ds, const float dt, const int detail; switch (state.TexLevelMode()) { case GE_TEXLEVEL_MODE_AUTO: - detail = TexLog2(std::max(ds * width, dt * height)); + detail = TexLog2(std::max(std::abs(ds * width), std::abs(dt * height))); break; case GE_TEXLEVEL_MODE_SLOPE: // This is always offset by an extra texlevel. - detail = 1 * 16 + TexLog2(state.textureLodSlope); + detail = TexLog2(2.0f * w * state.textureLodSlope); break; case GE_TEXLEVEL_MODE_CONST: default: @@ -377,19 +377,19 @@ static inline void CalculateSamplingParams(const float ds, const float dt, const filt = state.magFilt; } -static inline void ApplyTexturing(const RasterizerState &state, Vec4 *prim_color, const Vec4 &mask, const Vec4 &s, const Vec4 &t, int x, int y) { +static inline void ApplyTexturing(const RasterizerState &state, Vec4 *prim_color, const Vec4 &mask, const Vec4 &s, const Vec4 &t, float w) { float ds = s[1] - s[0]; float dt = t[2] - t[0]; int level; int levelFrac; bool bilinear; - CalculateSamplingParams(ds, dt, state, level, levelFrac, bilinear); + CalculateSamplingParams(ds, dt, w, state, level, levelFrac, bilinear); PROFILE_THIS_SCOPE("sampler"); for (int i = 0; i < 4; ++i) { if (mask[i] >= 0) - prim_color[i] = ApplyTexturing(s[i], t[i], ((x & 15) + 1) / 2, ((y & 15) + 1) / 2, ToVec4IntArg(prim_color[i]), level, levelFrac, bilinear, state); + prim_color[i] = ApplyTexturing(s[i], t[i], ToVec4IntArg(prim_color[i]), level, levelFrac, bilinear, state); } } @@ -717,7 +717,13 @@ void DrawTriangleSlice( GetTextureCoordinates(v0, v1, v2, w0, w1, w2, wsum_recip, s, t); } - ApplyTexturing(state, prim_color, mask, s, t, curX, curY); + if (state.TexLevelMode() == GE_TEXLEVEL_MODE_SLOPE) { + // Not sure what's right, but we need one value for the slope. + float clipw = (v0.clipw * w0.x + v1.clipw * w1.x + v2.clipw * w2.x) * wsum_recip.x; + ApplyTexturing(state, prim_color, mask, s, t, clipw); + } else { + ApplyTexturing(state, prim_color, mask, s, t, 0.0f); + } } if (!clearMode) { @@ -923,7 +929,7 @@ void DrawRectangle(const VertexData &v0, const VertexData &v1, const BinCoords & s = Vec4::AssignToAll(st.s()) + sto4; t = Vec4::AssignToAll(st.t()) + tto4; - ApplyTexturing(state, prim_color, mask, s, t, curX, curY); + ApplyTexturing(state, prim_color, mask, s, t, v1.clipw); } if (!state.pixelID.clearMode) { @@ -1014,9 +1020,9 @@ void DrawPoint(const VertexData &v0, const BinCoords &range, const RasterizerSta int texLevel; int texLevelFrac; bool bilinear; - CalculateSamplingParams(0.0f, 0.0f, state, texLevel, texLevelFrac, bilinear); + CalculateSamplingParams(0.0f, 0.0f, v0.clipw, state, texLevel, texLevelFrac, bilinear); PROFILE_THIS_SCOPE("sampler"); - prim_color = ApplyTexturingSingle(s, t, pos.x, pos.y, ToVec4IntArg(prim_color), texLevel, texLevelFrac, bilinear, state); + prim_color = ApplyTexturingSingle(s, t, ToVec4IntArg(prim_color), texLevel, texLevelFrac, bilinear, state); } if (!pixelID.clearMode) @@ -1342,11 +1348,12 @@ void DrawLine(const VertexData &v0, const VertexData &v1, const BinCoords &range // If inc is 0, force the delta to zero. float ds = xinc == 0.0 ? 0.0f : (s1 - s) * (float)SCREEN_SCALE_FACTOR * (1.0f / xinc); float dt = yinc == 0.0 ? 0.0f : (t1 - t) * (float)SCREEN_SCALE_FACTOR * (1.0f / yinc); + float w = (v0.clipw * (float)(steps - i) + v1.clipw * (float)i) / steps1; int texLevel; int texLevelFrac; bool texBilinear; - CalculateSamplingParams(ds, dt, state, texLevel, texLevelFrac, texBilinear); + CalculateSamplingParams(ds, dt, w, state, texLevel, texLevelFrac, texBilinear); if (state.antialiasLines) { // TODO: This is a naive and wrong implementation. @@ -1358,7 +1365,7 @@ void DrawLine(const VertexData &v0, const VertexData &v1, const BinCoords &range } PROFILE_THIS_SCOPE("sampler"); - prim_color = ApplyTexturingSingle(s, t, x, y, ToVec4IntArg(prim_color), texLevel, texLevelFrac, texBilinear, state); + prim_color = ApplyTexturingSingle(s, t, ToVec4IntArg(prim_color), texLevel, texLevelFrac, texBilinear, state); } if (!pixelID.clearMode) diff --git a/GPU/Software/RasterizerRectangle.cpp b/GPU/Software/RasterizerRectangle.cpp index 8d39a2426a..445c36bc32 100644 --- a/GPU/Software/RasterizerRectangle.cpp +++ b/GPU/Software/RasterizerRectangle.cpp @@ -245,9 +245,6 @@ void DrawSprite(const VertexData &v0, const VertexData &v1, const BinCoords &ran } } } else { - int xoff = ((v0.screenpos.x & 15) + 1) / 2; - int yoff = ((v0.screenpos.y & 15) + 1) / 2; - float dsf = ds * (1.0f / (float)(1 << state.samplerID.width0Shift)); float dtf = dt * (1.0f / (float)(1 << state.samplerID.height0Shift)); float sf_start = s_start * (1.0f / (float)(1 << state.samplerID.width0Shift)); @@ -261,7 +258,7 @@ void DrawSprite(const VertexData &v0, const VertexData &v1, const BinCoords &ran // Not really that fast but faster than triangle. for (int x = pos0.x; x < pos1.x; x++) { if (CheckDepthTestPassed(pixelID.DepthTestFunc(), x, y, pixelID.cached.depthbufStride, z)) { - Vec4 prim_color = state.nearest(s, t, xoff, yoff, ToVec4IntArg(c0), &texptr, &texbufw, 0, 0, state.samplerID); + Vec4 prim_color = state.nearest(s, t, ToVec4IntArg(c0), &texptr, &texbufw, 0, 0, state.samplerID); state.drawPixel(x, y, z, fog, ToVec4IntArg(prim_color), pixelID); } @@ -274,7 +271,7 @@ void DrawSprite(const VertexData &v0, const VertexData &v1, const BinCoords &ran float s = sf_start; // Not really that fast but faster than triangle. for (int x = pos0.x; x < pos1.x; x++) { - Vec4 prim_color = state.nearest(s, t, xoff, yoff, ToVec4IntArg(c0), &texptr, &texbufw, 0, 0, state.samplerID); + Vec4 prim_color = state.nearest(s, t, ToVec4IntArg(c0), &texptr, &texbufw, 0, 0, state.samplerID); state.drawPixel(x, y, z, fog, ToVec4IntArg(prim_color), pixelID); s += dsf; } diff --git a/GPU/Software/Sampler.cpp b/GPU/Software/Sampler.cpp index fa3b1a730f..28d83034de 100644 --- a/GPU/Software/Sampler.cpp +++ b/GPU/Software/Sampler.cpp @@ -38,8 +38,8 @@ using namespace Rasterizer; namespace Sampler { -static Vec4IntResult SOFTRAST_CALL SampleNearest(float s, float t, int x, int y, Vec4IntArg prim_color, const u8 *const *tptr, const uint16_t *bufw, int level, int levelFrac, const SamplerID &samplerID); -static Vec4IntResult SOFTRAST_CALL SampleLinear(float s, float t, int x, int y, Vec4IntArg prim_color, const u8 *const *tptr, const uint16_t *bufw, int level, int levelFrac, const SamplerID &samplerID); +static Vec4IntResult SOFTRAST_CALL SampleNearest(float s, float t, Vec4IntArg prim_color, const u8 *const *tptr, const uint16_t *bufw, int level, int levelFrac, const SamplerID &samplerID); +static Vec4IntResult SOFTRAST_CALL SampleLinear(float s, float t, Vec4IntArg prim_color, const u8 *const *tptr, const uint16_t *bufw, int level, int levelFrac, const SamplerID &samplerID); static Vec4IntResult SOFTRAST_CALL SampleFetch(int u, int v, const u8 *tptr, int bufw, int level, const SamplerID &samplerID); std::mutex jitCacheLock; @@ -416,7 +416,7 @@ static inline void ApplyTexelClamp(int out_u[N], int out_v[N], const int u[N], c } } -static inline void GetTexelCoordinates(int level, float s, float t, int &out_u, int &out_v, int x, int y, const SamplerID &samplerID) { +static inline void GetTexelCoordinates(int level, float s, float t, int &out_u, int &out_v, const SamplerID &samplerID) { int width = samplerID.cached.sizes[level].w; int height = samplerID.cached.sizes[level].h; @@ -535,15 +535,15 @@ Vec4IntResult SOFTRAST_CALL GetTextureFunctionOutput(Vec4IntArg prim_color_in, V return ToVec4IntResult(Vec4(out_rgb, out_a)); } -static Vec4IntResult SOFTRAST_CALL SampleNearest(float s, float t, int x, int y, Vec4IntArg prim_color, const u8 *const *tptr, const uint16_t *bufw, int level, int levelFrac, const SamplerID &samplerID) { +static Vec4IntResult SOFTRAST_CALL SampleNearest(float s, float t, Vec4IntArg prim_color, const u8 *const *tptr, const uint16_t *bufw, int level, int levelFrac, const SamplerID &samplerID) { int u, v; // Nearest filtering only. Round texcoords. - GetTexelCoordinates(level, s, t, u, v, x, y, samplerID); + GetTexelCoordinates(level, s, t, u, v, samplerID); Vec4 c0 = Vec4::FromRGBA(SampleNearest<1>(&u, &v, tptr[0], bufw[0], level, samplerID).v[0]); if (levelFrac) { - GetTexelCoordinates(level + 1, s, t, u, v, x, y, samplerID); + GetTexelCoordinates(level + 1, s, t, u, v, samplerID); Vec4 c1 = Vec4::FromRGBA(SampleNearest<1>(&u, &v, tptr[1], bufw[1], level + 1, samplerID).v[0]); c0 = (c1 * levelFrac + c0 * (16 - levelFrac)) / 16; @@ -609,7 +609,7 @@ static inline Vec4IntResult SOFTRAST_CALL ApplyTexelClampQuadT(bool clamp, int v #endif } -static inline Vec4IntResult SOFTRAST_CALL GetTexelCoordinatesQuadS(int level, float in_s, int &frac_u, int x, const SamplerID &samplerID) { +static inline Vec4IntResult SOFTRAST_CALL GetTexelCoordinatesQuadS(int level, float in_s, int &frac_u, const SamplerID &samplerID) { int width = samplerID.cached.sizes[level].w; int base_u = (int)(in_s * width * 256) - 128; @@ -620,7 +620,7 @@ static inline Vec4IntResult SOFTRAST_CALL GetTexelCoordinatesQuadS(int level, fl return ApplyTexelClampQuadS(samplerID.clampS, base_u, width); } -static inline Vec4IntResult SOFTRAST_CALL GetTexelCoordinatesQuadT(int level, float in_t, int &frac_v, int y, const SamplerID &samplerID) { +static inline Vec4IntResult SOFTRAST_CALL GetTexelCoordinatesQuadT(int level, float in_t, int &frac_v, const SamplerID &samplerID) { int height = samplerID.cached.sizes[level].h; int base_v = (int)(in_t * height * 256) - 128; @@ -631,10 +631,10 @@ static inline Vec4IntResult SOFTRAST_CALL GetTexelCoordinatesQuadT(int level, fl return ApplyTexelClampQuadT(samplerID.clampT, base_v, height); } -static Vec4IntResult SOFTRAST_CALL SampleLinearLevel(float s, float t, int x, int y, const u8 *const *tptr, const uint16_t *bufw, int texlevel, const SamplerID &samplerID) { +static Vec4IntResult SOFTRAST_CALL SampleLinearLevel(float s, float t, const u8 *const *tptr, const uint16_t *bufw, int texlevel, const SamplerID &samplerID) { int frac_u, frac_v; - const Vec4 u = GetTexelCoordinatesQuadS(texlevel, s, frac_u, x, samplerID); - const Vec4 v = GetTexelCoordinatesQuadT(texlevel, t, frac_v, y, samplerID); + const Vec4 u = GetTexelCoordinatesQuadS(texlevel, s, frac_u, samplerID); + const Vec4 v = GetTexelCoordinatesQuadT(texlevel, t, frac_v, samplerID); Nearest4 c = SampleNearest<4>(u.AsArray(), v.AsArray(), tptr[0], bufw[0], texlevel, samplerID); Vec4 texcolor_tl = Vec4::FromRGBA(c.v[0]); @@ -646,10 +646,10 @@ static Vec4IntResult SOFTRAST_CALL SampleLinearLevel(float s, float t, int x, in return ToVec4IntResult((top * (0x10 - frac_v) + bot * frac_v) / (16 * 16)); } -static Vec4IntResult SOFTRAST_CALL SampleLinear(float s, float t, int x, int y, Vec4IntArg prim_color, const u8 *const *tptr, const uint16_t *bufw, int texlevel, int levelFrac, const SamplerID &samplerID) { - Vec4 c0 = SampleLinearLevel(s, t, x, y, tptr, bufw, texlevel, samplerID); +static Vec4IntResult SOFTRAST_CALL SampleLinear(float s, float t, Vec4IntArg prim_color, const u8 *const *tptr, const uint16_t *bufw, int texlevel, int levelFrac, const SamplerID &samplerID) { + Vec4 c0 = SampleLinearLevel(s, t, tptr, bufw, texlevel, samplerID); if (levelFrac) { - const Vec4 c1 = SampleLinearLevel(s, t, x, y, tptr + 1, bufw + 1, texlevel + 1, samplerID); + const Vec4 c1 = SampleLinearLevel(s, t, tptr + 1, bufw + 1, texlevel + 1, samplerID); c0 = (c1 * levelFrac + c0 * (16 - levelFrac)) / 16; } return GetTextureFunctionOutput(prim_color, ToVec4IntArg(c0), samplerID); diff --git a/GPU/Software/Sampler.h b/GPU/Software/Sampler.h index 9af945a9f2..292a93527f 100644 --- a/GPU/Software/Sampler.h +++ b/GPU/Software/Sampler.h @@ -36,10 +36,10 @@ namespace Sampler { typedef Rasterizer::Vec4IntResult(SOFTRAST_CALL *FetchFunc)(int u, int v, const u8 *tptr, int bufw, int level, const SamplerID &samplerID); FetchFunc GetFetchFunc(SamplerID id); -typedef Rasterizer::Vec4IntResult (SOFTRAST_CALL *NearestFunc)(float s, float t, int x, int y, Rasterizer::Vec4IntArg prim_color, const u8 *const *tptr, const uint16_t *bufw, int level, int levelFrac, const SamplerID &samplerID); +typedef Rasterizer::Vec4IntResult (SOFTRAST_CALL *NearestFunc)(float s, float t, Rasterizer::Vec4IntArg prim_color, const u8 *const *tptr, const uint16_t *bufw, int level, int levelFrac, const SamplerID &samplerID); NearestFunc GetNearestFunc(SamplerID id); -typedef Rasterizer::Vec4IntResult (SOFTRAST_CALL *LinearFunc)(float s, float t, int x, int y, Rasterizer::Vec4IntArg prim_color, const u8 *const *tptr, const uint16_t *bufw, int level, int levelFrac, const SamplerID &samplerID); +typedef Rasterizer::Vec4IntResult (SOFTRAST_CALL *LinearFunc)(float s, float t, Rasterizer::Vec4IntArg prim_color, const u8 *const *tptr, const uint16_t *bufw, int level, int levelFrac, const SamplerID &samplerID); LinearFunc GetLinearFunc(SamplerID id); void Init(); diff --git a/GPU/Software/SamplerX86.cpp b/GPU/Software/SamplerX86.cpp index 150856f6f5..7b05debdb5 100644 --- a/GPU/Software/SamplerX86.cpp +++ b/GPU/Software/SamplerX86.cpp @@ -133,8 +133,6 @@ NearestFunc SamplerJitCache::CompileNearest(const SamplerID &id) { regCache_.SetupABI({ RegCache::VEC_ARG_S, RegCache::VEC_ARG_T, - RegCache::GEN_ARG_X, - RegCache::GEN_ARG_Y, RegCache::VEC_ARG_COLOR, RegCache::GEN_ARG_TEXPTR_PTR, RegCache::GEN_ARG_BUFW_PTR, @@ -144,16 +142,16 @@ NearestFunc SamplerJitCache::CompileNearest(const SamplerID &id) { }); #if PPSSPP_PLATFORM(WINDOWS) - // RET + shadow space + 8 byte space for color arg (the Win32 ABI is kinda ugly.) - stackArgPos_ = 8 + 32 + 8; + // RET + shadow space. + stackArgPos_ = 8 + 32; - // Positions: stackArgPos_+0=src, stackArgPos_+8=bufw, stackArgPos_+16=level, stackArgPos_+24=levelFrac - stackIDOffset_ = 32; - stackLevelOffset_ = 16; + // Positions: stackArgPos_+0=bufwptr, stackArgPos_+8=level, stackArgPos_+16=levelFrac + stackIDOffset_ = 24; + stackLevelOffset_ = 8; #else stackArgPos_ = 0; - // This is the only arg that went to the stack, it's after the RET. - stackIDOffset_ = 8; + // No args on the stack. + stackIDOffset_ = -1; stackLevelOffset_ = -1; #endif @@ -182,23 +180,20 @@ NearestFunc SamplerJitCache::CompileNearest(const SamplerID &id) { if (!id.hasAnyMips && regCache_.Has(RegCache::GEN_ARG_LEVELFRAC)) regCache_.ForceRelease(RegCache::GEN_ARG_LEVELFRAC); - if (regCache_.Has(RegCache::GEN_ARG_BUFW_PTR)) { - // On Linux, RCX is currently bufwptr, but we'll need it for other things. + if (regCache_.Has(RegCache::GEN_ARG_LEVELFRAC)) { + // On Linux, RCX is currently levelFrac, but we'll need it for other things. if (!cpu_info.bBMI2) { - X64Reg bufwReg = regCache_.Find(RegCache::GEN_ARG_BUFW_PTR); - MOV(64, R(R15), R(bufwReg)); - regCache_.Unlock(bufwReg, RegCache::GEN_ARG_BUFW_PTR); - regCache_.ForceRelease(RegCache::GEN_ARG_BUFW_PTR); - regCache_.ChangeReg(R15, RegCache::GEN_ARG_BUFW_PTR); - regCache_.ForceRetain(RegCache::GEN_ARG_BUFW_PTR); + X64Reg levelFracReg = regCache_.Find(RegCache::GEN_ARG_LEVELFRAC); + MOV(64, R(R15), R(levelFracReg)); + regCache_.Unlock(levelFracReg, RegCache::GEN_ARG_LEVELFRAC); + regCache_.ForceRelease(RegCache::GEN_ARG_LEVELFRAC); + regCache_.ChangeReg(R15, RegCache::GEN_ARG_LEVELFRAC); + regCache_.ForceRetain(RegCache::GEN_ARG_LEVELFRAC); } - } else { - // Let's load bufwptr/texptrptr into regs. Match Linux just for consistency - RDX is free. + } else if (!regCache_.Has(RegCache::GEN_ARG_BUFW_PTR)) { + // Let's load bufwptr into regs. RDX is free. MOV(64, R(RDX), MDisp(RSP, stackArgPos_ + 0)); - MOV(64, R(R15), MDisp(RSP, stackArgPos_ + 8)); - regCache_.ChangeReg(RDX, RegCache::GEN_ARG_TEXPTR_PTR); - regCache_.ChangeReg(R15, RegCache::GEN_ARG_BUFW_PTR); - regCache_.ForceRetain(RegCache::GEN_ARG_TEXPTR_PTR); + regCache_.ChangeReg(RDX, RegCache::GEN_ARG_BUFW_PTR); regCache_.ForceRetain(RegCache::GEN_ARG_BUFW_PTR); } // Okay, now lock RCX as a shifting reg. @@ -284,7 +279,7 @@ NearestFunc SamplerJitCache::CompileNearest(const SamplerID &id) { CMP(8, R(levelFracReg), Imm8(0)); regCache_.Unlock(levelFracReg, RegCache::GEN_ARG_LEVELFRAC); } else { - CMP(8, MDisp(RSP, stackArgPos_ + 24), Imm8(0)); + CMP(8, MDisp(RSP, stackArgPos_ + 16), Imm8(0)); } FixupBranch skip = J_CC(CC_Z, true); @@ -295,7 +290,7 @@ NearestFunc SamplerJitCache::CompileNearest(const SamplerID &id) { regCache_.Unlock(levelReg, RegCache::GEN_ARG_LEVEL); } else { // It's fine to just modify this in place. - ADD(32, MDisp(RSP, stackArgPos_ + 16), Imm8(1)); + ADD(32, MDisp(RSP, stackArgPos_ + stackLevelOffset_), Imm8(1)); } // This is inside the conditional, but it's okay because we throw it away after. @@ -357,7 +352,7 @@ NearestFunc SamplerJitCache::CompileNearest(const SamplerID &id) { Describe("BlendMips"); if (!regCache_.Has(RegCache::GEN_ARG_LEVELFRAC)) { X64Reg levelFracReg = regCache_.Alloc(RegCache::GEN_ARG_LEVELFRAC); - MOVZX(32, 8, levelFracReg, MDisp(RSP, stackArgPos_ + 24)); + MOVZX(32, 8, levelFracReg, MDisp(RSP, stackArgPos_ + 16)); regCache_.Unlock(levelFracReg, RegCache::GEN_ARG_LEVELFRAC); regCache_.ForceRetain(RegCache::GEN_ARG_LEVELFRAC); } @@ -413,6 +408,8 @@ NearestFunc SamplerJitCache::CompileNearest(const SamplerID &id) { } regCache_.ForceRelease(RegCache::VEC_RESULT); + if (regCache_.Has(RegCache::GEN_ARG_ID)) + regCache_.ForceRelease(RegCache::GEN_ARG_ID); if (!success) { regCache_.Reset(false); @@ -525,8 +522,6 @@ LinearFunc SamplerJitCache::CompileLinear(const SamplerID &id) { regCache_.SetupABI({ RegCache::VEC_ARG_S, RegCache::VEC_ARG_T, - RegCache::GEN_ARG_X, - RegCache::GEN_ARG_Y, RegCache::VEC_ARG_COLOR, RegCache::GEN_ARG_TEXPTR_PTR, RegCache::GEN_ARG_BUFW_PTR, @@ -536,22 +531,21 @@ LinearFunc SamplerJitCache::CompileLinear(const SamplerID &id) { }); #if PPSSPP_PLATFORM(WINDOWS) - // RET + shadow space + 8 byte space for color arg (the Win32 ABI is kinda ugly.) - stackArgPos_ = 8 + 32 + 8; + // RET + shadow space. + stackArgPos_ = 8 + 32; // Free up some more vector regs on Windows too, where we're a bit tight. stackArgPos_ += WriteProlog(0, { XMM6, XMM7, XMM8, XMM9, XMM10, XMM11, XMM12 }, { R15, R14, R13, R12 }); - // Positions: stackArgPos_+0=src, stackArgPos_+8=bufw, stackArgPos_+16=level, stackArgPos_+24=levelFrac - stackIDOffset_ = 32; - stackLevelOffset_ = 16; + // Positions: stackArgPos_+0=bufwptr, stackArgPos_+8=level, stackArgPos_+16=levelFrac + stackIDOffset_ = 24; + stackLevelOffset_ = 8; - // If needed, we could store UV1 data in shadow space, but we no longer due. + // If needed, we could store UV1 data in shadow space, but we no longer do. stackUV1Offset_ = -8; #else stackArgPos_ = 0; stackArgPos_ += WriteProlog(0, {}, { R15, R14, R13, R12 }); - // Just after the RET. - stackIDOffset_ = 8; + stackIDOffset_ = -1; stackLevelOffset_ = -1; // Use the red zone. @@ -591,8 +585,7 @@ LinearFunc SamplerJitCache::CompileLinear(const SamplerID &id) { } // We also want to save src and bufw for later. Might be in a reg already. - if (regCache_.Has(RegCache::GEN_ARG_TEXPTR_PTR)) { - _assert_(regCache_.Has(RegCache::GEN_ARG_BUFW_PTR)); + if (regCache_.Has(RegCache::GEN_ARG_TEXPTR_PTR) && regCache_.Has(RegCache::GEN_ARG_BUFW_PTR)) { X64Reg srcReg = regCache_.Find(RegCache::GEN_ARG_TEXPTR_PTR); X64Reg bufwReg = regCache_.Find(RegCache::GEN_ARG_BUFW_PTR); MOV(64, R(R14), R(srcReg)); @@ -601,6 +594,12 @@ LinearFunc SamplerJitCache::CompileLinear(const SamplerID &id) { regCache_.Unlock(bufwReg, RegCache::GEN_ARG_BUFW_PTR); regCache_.ForceRelease(RegCache::GEN_ARG_TEXPTR_PTR); regCache_.ForceRelease(RegCache::GEN_ARG_BUFW_PTR); + } else if (regCache_.Has(RegCache::GEN_ARG_TEXPTR_PTR)) { + X64Reg srcReg = regCache_.Find(RegCache::GEN_ARG_TEXPTR_PTR); + MOV(64, R(R14), R(srcReg)); + regCache_.Unlock(srcReg, RegCache::GEN_ARG_TEXPTR_PTR); + regCache_.ForceRelease(RegCache::GEN_ARG_TEXPTR_PTR); + MOV(64, R(R15), MDisp(RSP, stackArgPos_ + 0)); } else { MOV(64, R(R14), MDisp(RSP, stackArgPos_ + 0)); MOV(64, R(R15), MDisp(RSP, stackArgPos_ + 8)); @@ -608,9 +607,11 @@ LinearFunc SamplerJitCache::CompileLinear(const SamplerID &id) { // Okay, and now remember we moved to R14/R15. regCache_.ChangeReg(R14, RegCache::GEN_ARG_TEXPTR_PTR); - regCache_.ChangeReg(R15, RegCache::GEN_ARG_BUFW_PTR); regCache_.ForceRetain(RegCache::GEN_ARG_TEXPTR_PTR); - regCache_.ForceRetain(RegCache::GEN_ARG_BUFW_PTR); + if (!regCache_.Has(RegCache::GEN_ARG_BUFW_PTR)) { + regCache_.ChangeReg(R15, RegCache::GEN_ARG_BUFW_PTR); + regCache_.ForceRetain(RegCache::GEN_ARG_BUFW_PTR); + } bool success = true; @@ -758,7 +759,7 @@ LinearFunc SamplerJitCache::CompileLinear(const SamplerID &id) { CMP(8, R(levelFracReg), Imm8(0)); regCache_.Unlock(levelFracReg, RegCache::GEN_ARG_LEVELFRAC); } else { - CMP(8, MDisp(RSP, stackArgPos_ + 24), Imm8(0)); + CMP(8, MDisp(RSP, stackArgPos_ + 16), Imm8(0)); } FixupBranch skip = J_CC(CC_Z, true); @@ -769,7 +770,7 @@ LinearFunc SamplerJitCache::CompileLinear(const SamplerID &id) { regCache_.Unlock(levelReg, RegCache::GEN_ARG_LEVEL); } else { // It's fine to just modify this in place. - ADD(32, MDisp(RSP, stackArgPos_ + 16), Imm8(1)); + ADD(32, MDisp(RSP, stackArgPos_ + stackLevelOffset_), Imm8(1)); } if (nearest != nullptr) { @@ -808,7 +809,7 @@ LinearFunc SamplerJitCache::CompileLinear(const SamplerID &id) { Describe("BlendMips"); if (!regCache_.Has(RegCache::GEN_ARG_LEVELFRAC)) { X64Reg levelFracReg = regCache_.Alloc(RegCache::GEN_ARG_LEVELFRAC); - MOVZX(32, 8, levelFracReg, MDisp(RSP, stackArgPos_ + 24)); + MOVZX(32, 8, levelFracReg, MDisp(RSP, stackArgPos_ + 16)); regCache_.Unlock(levelFracReg, RegCache::GEN_ARG_LEVELFRAC); regCache_.ForceRetain(RegCache::GEN_ARG_LEVELFRAC); } @@ -870,6 +871,8 @@ LinearFunc SamplerJitCache::CompileLinear(const SamplerID &id) { } regCache_.ForceRelease(RegCache::VEC_RESULT); + if (regCache_.Has(RegCache::GEN_ARG_ID)) + regCache_.ForceRelease(RegCache::GEN_ARG_ID); if (!success) { regCache_.Reset(false); @@ -1223,9 +1226,9 @@ bool SamplerJitCache::Jit_ReadClutQuad(const SamplerID &id, bool level1) { } else { #if PPSSPP_PLATFORM(WINDOWS) if (cpu_info.bAVX2) { - VPBROADCASTD(128, vecLevelReg, MDisp(RSP, stackArgPos_ + 16)); + VPBROADCASTD(128, vecLevelReg, MDisp(RSP, stackArgPos_ + stackLevelOffset_)); } else { - MOVD_xmm(vecLevelReg, MDisp(RSP, stackArgPos_ + 16)); + MOVD_xmm(vecLevelReg, MDisp(RSP, stackArgPos_ + stackLevelOffset_)); PSHUFD(vecLevelReg, R(vecLevelReg), _MM_SHUFFLE(0, 0, 0, 0)); } #else @@ -2583,11 +2586,6 @@ bool SamplerJitCache::Jit_GetTexDataSwizzled(const SamplerID &id, int bitsPerTex bool SamplerJitCache::Jit_GetTexelCoords(const SamplerID &id) { Describe("Texel"); - // First, adjust X and Y... - // TODO: Shouldn't do this in the sampler, need to get s/t right. - regCache_.ForceRelease(RegCache::GEN_ARG_X); - regCache_.ForceRelease(RegCache::GEN_ARG_Y); - X64Reg uReg = regCache_.Alloc(RegCache::GEN_ARG_U); X64Reg vReg = regCache_.Alloc(RegCache::GEN_ARG_V); X64Reg sReg = regCache_.Find(RegCache::VEC_ARG_S); @@ -2602,7 +2600,7 @@ bool SamplerJitCache::Jit_GetTexelCoords(const SamplerID &id) { levelReg = regCache_.Find(RegCache::GEN_ARG_LEVEL); } else { levelReg = regCache_.Alloc(RegCache::GEN_ARG_LEVEL); - MOV(32, R(levelReg), MDisp(RSP, stackArgPos_ + 16)); + MOV(32, R(levelReg), MDisp(RSP, stackArgPos_ + stackLevelOffset_)); } // We'll multiply these at the same time, so it's nice to put together. @@ -2774,7 +2772,7 @@ bool SamplerJitCache::Jit_GetTexelCoordsQuad(const SamplerID &id) { } else { releaseLevelReg = true; levelReg = regCache_.Alloc(RegCache::GEN_ARG_LEVEL); - MOV(32, R(levelReg), MDisp(RSP, stackArgPos_ + 16)); + MOV(32, R(levelReg), MDisp(RSP, stackArgPos_ + stackLevelOffset_)); } // This will load the current and next level's sizes, 16x4. @@ -2826,8 +2824,6 @@ bool SamplerJitCache::Jit_GetTexelCoordsQuad(const SamplerID &id) { PSLLD(tempXYReg, 7); PADDD(sReg, R(tempXYReg)); regCache_.Release(tempXYReg, RegCache::VEC_TEMP0); - regCache_.ForceRelease(RegCache::GEN_ARG_X); - regCache_.ForceRelease(RegCache::GEN_ARG_Y); // We do want the fraction, though, so extract that to an XMM for later. X64Reg allFracReg = INVALID_REG; diff --git a/unittest/TestSoftwareGPUJit.cpp b/unittest/TestSoftwareGPUJit.cpp index 54b8ceda52..17d0415264 100644 --- a/unittest/TestSoftwareGPUJit.cpp +++ b/unittest/TestSoftwareGPUJit.cpp @@ -89,8 +89,8 @@ static bool TestSamplerJit() { // Try running each to make sure they don't trivially crash. const auto primArg = Rasterizer::ToVec4IntArg(Math3D::Vec4(127, 127, 127, 127)); - linearFunc(0.0f, 0.0f, 0, 0, primArg, tptr, bufw, 1, 7, id); - nearestFunc(0.0f, 0.0f, 0, 0, primArg, tptr, bufw, 1, 7, id); + linearFunc(0.0f, 0.0f, primArg, tptr, bufw, 1, 7, id); + nearestFunc(0.0f, 0.0f, primArg, tptr, bufw, 1, 7, id); fetchFunc(0, 0, tptr[0], bufw[0], 1, id); }