From 251d42ab2eb60001127dae6cf368d460fc317384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 5 Oct 2026 11:55:32 -0600 Subject: [PATCH] SoftGPU: Leave textures with an invalid level address to the generic samplers The x86 sampler JIT returned zero for a whole sample when a texture level pointer was null, and for a mipmapped texture it checked the next level too, even when it isn't blended in. Tactics Ogre draws its font from a mipmapped texture whose next level has an invalid address, at level 0, so all its text was missing on x86-64. The generic samplers read a null level's texels as zero only where they use that level, and still apply the texture function. Now samplers for such textures aren't compiled, and the JIT's null checks are gone. Test: gpu/textures/mipinvalid. Co-Authored-By: Claude Opus 5.5 (1M context) --- GPU/Software/Sampler.cpp | 12 ++++-- GPU/Software/SamplerX86.cpp | 74 --------------------------------- pspautotests | 2 +- test.py | 1 + unittest/TestSoftwareGPUJit.cpp | 2 + 5 files changed, 13 insertions(+), 78 deletions(-) diff --git a/GPU/Software/Sampler.cpp b/GPU/Software/Sampler.cpp index 6c85b88ea5..89ad98c2a8 100644 --- a/GPU/Software/Sampler.cpp +++ b/GPU/Software/Sampler.cpp @@ -163,6 +163,12 @@ void SamplerJitCache::Flush() { compileQueue_.clear(); } +// A texture level whose address isn't valid has no pointer, and the generic samplers read its texels as zero +// (then apply the texture function). The JIT leaves those to them. +static bool CanJit(const SamplerID &id) { + return g_Config.bSoftwareRenderingJit && !id.hasInvalidPtr; +} + NearestFunc SamplerJitCache::GetByID(const SamplerID &id, size_t key, BinManager *binner) { std::unique_lock guard(jitCacheLock); @@ -201,7 +207,7 @@ NearestFunc SamplerJitCache::GetByID(const SamplerID &id, size_t key, BinManager } NearestFunc SamplerJitCache::GetNearest(const SamplerID &id, BinManager *binner) { - if (!g_Config.bSoftwareRenderingJit) + if (!CanJit(id)) return nullptr; const size_t key = std::hash()(id); @@ -214,7 +220,7 @@ NearestFunc SamplerJitCache::GetNearest(const SamplerID &id, BinManager *binner) } LinearFunc SamplerJitCache::GetLinear(const SamplerID &id, BinManager *binner) { - if (!g_Config.bSoftwareRenderingJit) + if (!CanJit(id)) return nullptr; const size_t key = std::hash()(id); @@ -227,7 +233,7 @@ LinearFunc SamplerJitCache::GetLinear(const SamplerID &id, BinManager *binner) { } FetchFunc SamplerJitCache::GetFetch(const SamplerID &id, BinManager *binner) { - if (!g_Config.bSoftwareRenderingJit) + if (!CanJit(id)) return nullptr; const size_t key = std::hash()(id); diff --git a/GPU/Software/SamplerX86.cpp b/GPU/Software/SamplerX86.cpp index 9e652da7a3..6916ffbf16 100644 --- a/GPU/Software/SamplerX86.cpp +++ b/GPU/Software/SamplerX86.cpp @@ -60,21 +60,6 @@ FetchFunc SamplerJitCache::CompileFetch(const SamplerID &id) { stackLevelOffset_ = -1; #endif - // Early exit on !srcPtr. - FixupBranch zeroSrc; - if (id.hasInvalidPtr) { - X64Reg srcReg = regCache_.Find(RegCache::GEN_ARG_TEXPTR); - CMP(PTRBITS, R(srcReg), Imm8(0)); - regCache_.Unlock(srcReg, RegCache::GEN_ARG_TEXPTR); - - FixupBranch nonZeroSrc = J_CC(CC_NZ); - X64Reg vecResultReg = regCache_.Find(RegCache::VEC_RESULT); - PXOR(vecResultReg, R(vecResultReg)); - regCache_.Unlock(vecResultReg, RegCache::VEC_RESULT); - zeroSrc = J(true); - SetJumpTarget(nonZeroSrc); - } - // This reads the pixel data into resultReg from the args. if (!Jit_ReadTextureFormat(id)) { regCache_.Reset(false); @@ -108,9 +93,6 @@ FetchFunc SamplerJitCache::CompileFetch(const SamplerID &id) { regCache_.Unlock(vecResultReg, RegCache::VEC_RESULT); Describe("Init"); - if (id.hasInvalidPtr) { - SetJumpTarget(zeroSrc); - } RET(); @@ -214,30 +196,6 @@ NearestFunc SamplerJitCache::CompileNearest(const SamplerID &id) { regCache_.Unlock(resultReg, RegCache::GEN_RESULT); regCache_.ForceRetain(RegCache::GEN_RESULT); - // Early exit on !srcPtr (either one.) - FixupBranch zeroSrc; - if (id.hasInvalidPtr) { - Describe("NullCheck"); - X64Reg srcReg = regCache_.Find(RegCache::GEN_ARG_TEXPTR_PTR); - - if (id.hasAnyMips) { - X64Reg tempReg = regCache_.Alloc(RegCache::GEN_TEMP0); - MOV(64, R(tempReg), MDisp(srcReg, 0)); - AND(64, R(tempReg), MDisp(srcReg, 8)); - - CMP(PTRBITS, R(tempReg), Imm8(0)); - regCache_.Release(tempReg, RegCache::GEN_TEMP0); - } else { - CMP(PTRBITS, MatR(srcReg), Imm8(0)); - } - FixupBranch nonZeroSrc = J_CC(CC_NZ); - PXOR(XMM0, R(XMM0)); - zeroSrc = J(true); - SetJumpTarget(nonZeroSrc); - - regCache_.Unlock(srcReg, RegCache::GEN_ARG_TEXPTR_PTR); - } - auto loadPtrs = [&](bool level1) { X64Reg bufwReg = regCache_.Alloc(RegCache::GEN_ARG_BUFW); X64Reg bufwPtrReg = regCache_.Find(RegCache::GEN_ARG_BUFW_PTR); @@ -419,10 +377,6 @@ NearestFunc SamplerJitCache::CompileNearest(const SamplerID &id) { return nullptr; } - if (id.hasInvalidPtr) { - SetJumpTarget(zeroSrc); - } - POP(R12); POP(R13); POP(R14); @@ -618,30 +572,6 @@ LinearFunc SamplerJitCache::CompileLinear(const SamplerID &id) { // Our first goal is to convert S/T and X/Y into U/V and frac_u/frac_v. success = success && Jit_GetTexelCoordsQuad(id); - // Early exit on !srcPtr (either one.) - FixupBranch zeroSrc; - if (id.hasInvalidPtr) { - Describe("NullCheck"); - X64Reg srcReg = regCache_.Find(RegCache::GEN_ARG_TEXPTR_PTR); - - if (id.hasAnyMips) { - X64Reg tempReg = regCache_.Alloc(RegCache::GEN_TEMP0); - MOV(64, R(tempReg), MDisp(srcReg, 0)); - AND(64, R(tempReg), MDisp(srcReg, 8)); - - CMP(PTRBITS, R(tempReg), Imm8(0)); - regCache_.Release(tempReg, RegCache::GEN_TEMP0); - } else { - CMP(PTRBITS, MatR(srcReg), Imm8(0)); - } - FixupBranch nonZeroSrc = J_CC(CC_NZ); - PXOR(XMM0, R(XMM0)); - zeroSrc = J(true); - SetJumpTarget(nonZeroSrc); - - regCache_.Unlock(srcReg, RegCache::GEN_ARG_TEXPTR_PTR); - } - auto prepareDataOffsets = [&](RegCache::Purpose uPurpose, RegCache::Purpose vPurpose, bool level1) { X64Reg uReg = regCache_.Find(uPurpose); X64Reg vReg = regCache_.Find(vPurpose); @@ -882,10 +812,6 @@ LinearFunc SamplerJitCache::CompileLinear(const SamplerID &id) { return nullptr; } - if (id.hasInvalidPtr) { - SetJumpTarget(zeroSrc); - } - const u8 *start = WriteFinalizedEpilog(); regCache_.Reset(true); return (LinearFunc)start; diff --git a/pspautotests b/pspautotests index c56dcf1c38..587d52f2f8 160000 --- a/pspautotests +++ b/pspautotests @@ -1 +1 @@ -Subproject commit c56dcf1c3841e1118de7b3558b1aaa7cc5155626 +Subproject commit 587d52f2f8afc2d66a427723a3f36c189f6e14bc diff --git a/test.py b/test.py index 059bedef31..6dfff703a6 100755 --- a/test.py +++ b/test.py @@ -289,6 +289,7 @@ tests_good = [ "gpu/texmtx/prims", "gpu/texmtx/source", "gpu/texmtx/uvs", + "gpu/textures/mipinvalid", "gpu/textures/mipmap", "gpu/textures/rotate", "gpu/textures/size", diff --git a/unittest/TestSoftwareGPUJit.cpp b/unittest/TestSoftwareGPUJit.cpp index ab2f5ff73c..79be7fdff1 100644 --- a/unittest/TestSoftwareGPUJit.cpp +++ b/unittest/TestSoftwareGPUJit.cpp @@ -65,6 +65,8 @@ static bool TestSamplerJit() { SamplerID id; memset(&id, 0, sizeof(id)); id.fullKey = rng.R32(); + // Textures with an invalid level address are left to the generic samplers. + id.hasInvalidPtr = false; id.cached.clut = clut; for (int i = 0; i < 8; ++i) {