From 83b7b33cfd565f50a344ecf4d34a45dc11288a1f Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 17 Oct 2021 12:36:20 -0700 Subject: [PATCH] Replacement: Centralize lookup logic. And make sure we don't change our minds about using a replacement during a draw. --- Core/TextureReplacer.cpp | 2 +- Core/TextureReplacer.h | 3 +++ GPU/Common/TextureCacheCommon.cpp | 20 ++++++++++++++++++++ GPU/Common/TextureCacheCommon.h | 1 + GPU/D3D11/TextureCacheD3D11.cpp | 22 ++++++---------------- GPU/Directx9/TextureCacheDX9.cpp | 22 ++++++---------------- GPU/GLES/TextureCacheGLES.cpp | 22 ++++++---------------- GPU/Vulkan/TextureCacheVulkan.cpp | 26 ++++++++------------------ 8 files changed, 51 insertions(+), 67 deletions(-) diff --git a/Core/TextureReplacer.cpp b/Core/TextureReplacer.cpp index 0aff1c7793..56de4ee66c 100644 --- a/Core/TextureReplacer.cpp +++ b/Core/TextureReplacer.cpp @@ -747,7 +747,7 @@ bool ReplacedTexture::IsReady(double budget) { lastUsed_ = time_now_d(); if (levelData_.size() == levels_.size()) - return true; + return Valid(); if (budget <= 0.0) return false; diff --git a/Core/TextureReplacer.h b/Core/TextureReplacer.h index 46ee4c58be..a931b9e645 100644 --- a/Core/TextureReplacer.h +++ b/Core/TextureReplacer.h @@ -195,6 +195,9 @@ public: ReplacedTexture &FindReplacement(u64 cachekey, u32 hash, int w, int h); bool FindFiltering(u64 cachekey, u32 hash, TextureFiltering *forceFiltering); + ReplacedTexture &None() { + return none_; + } void NotifyTextureDecoded(const ReplacedTextureDecodeInfo &replacedInfo, const void *data, int pitch, int level, int w, int h); diff --git a/GPU/Common/TextureCacheCommon.cpp b/GPU/Common/TextureCacheCommon.cpp index 04c6bfae2c..0a931c32b1 100644 --- a/GPU/Common/TextureCacheCommon.cpp +++ b/GPU/Common/TextureCacheCommon.cpp @@ -22,6 +22,7 @@ #include "Common/Profiler/Profiler.h" #include "Common/MemoryUtil.h" #include "Common/StringUtils.h" +#include "Common/TimeUtil.h" #include "Core/Config.h" #include "Core/Debugger/MemBlockInfo.h" #include "Core/Reporting.h" @@ -1268,6 +1269,25 @@ u32 TextureCacheCommon::EstimateTexMemoryUsage(const TexCacheEntry *entry) { return pixelSize << (dimW + dimH); } +ReplacedTexture &TextureCacheCommon::FindReplacement(TexCacheEntry *entry, int &w, int &h) { + double replaceStart = time_now_d(); + u64 cachekey = replacer_.Enabled() ? entry->CacheKey() : 0; + ReplacedTexture &replaced = replacer_.FindReplacement(cachekey, entry->fullhash, w, h); + if (replaced.IsReady(replacementFrameBudget_ - replacementTimeThisFrame_)) { + if (replaced.GetSize(0, w, h)) { + replacementTimeThisFrame_ += time_now_d() - replaceStart; + + // Consider it already "scaled" and remove any delayed replace flag. + entry->status |= TexCacheEntry::STATUS_IS_SCALED; + entry->status &= ~TexCacheEntry::STATUS_TO_REPLACE; + return replaced; + } + } else if (replaced.Valid()) { + entry->status |= TexCacheEntry::STATUS_TO_REPLACE; + } + return replacer_.None(); +} + static void ReverseColors(void *dstBuf, const void *srcBuf, GETextureFormat fmt, int numPixels, bool useBGRA) { switch (fmt) { case GE_TFMT_4444: diff --git a/GPU/Common/TextureCacheCommon.h b/GPU/Common/TextureCacheCommon.h index ceb2aa96ff..32ab227b06 100644 --- a/GPU/Common/TextureCacheCommon.h +++ b/GPU/Common/TextureCacheCommon.h @@ -278,6 +278,7 @@ protected: void DecodeTextureLevel(u8 *out, int outPitch, GETextureFormat format, GEPaletteFormat clutformat, uint32_t texaddr, int level, int bufw, bool reverseColors, bool useBGRA, bool expandTo32Bit); void UnswizzleFromMem(u32 *dest, u32 destPitch, const u8 *texptr, u32 bufw, u32 height, u32 bytesPerPixel); void ReadIndexedTex(u8 *out, int outPitch, int level, const u8 *texptr, int bytesPerIndex, int bufw, bool expandTo32Bit); + ReplacedTexture &FindReplacement(TexCacheEntry *entry, int &w, int &h); template inline const T *GetCurrentClut() { diff --git a/GPU/D3D11/TextureCacheD3D11.cpp b/GPU/D3D11/TextureCacheD3D11.cpp index 1a8b34a54c..199f408e4a 100644 --- a/GPU/D3D11/TextureCacheD3D11.cpp +++ b/GPU/D3D11/TextureCacheD3D11.cpp @@ -485,24 +485,14 @@ void TextureCacheD3D11::BuildTexture(TexCacheEntry *const entry) { scaleFactor = scaleFactor > 4 ? 4 : (scaleFactor > 2 ? 2 : 1); } - u64 cachekey = replacer_.Enabled() ? entry->CacheKey() : 0; int w = gstate.getTextureWidth(0); int h = gstate.getTextureHeight(0); - double replaceStart = time_now_d(); - ReplacedTexture &replaced = replacer_.FindReplacement(cachekey, entry->fullhash, w, h); - if (replaced.IsReady(replacementFrameBudget_ - replacementTimeThisFrame_)) { - if (replaced.GetSize(0, w, h)) { - replacementTimeThisFrame_ += time_now_d() - replaceStart; - - // We're replacing, so we won't scale. - scaleFactor = 1; - entry->status |= TexCacheEntry::STATUS_IS_SCALED; - entry->status &= ~TexCacheEntry::STATUS_TO_REPLACE; - maxLevel = replaced.MaxLevel(); - badMipSizes = false; - } - } else if (replaced.MaxLevel() >= 0) { - entry->status |= TexCacheEntry::STATUS_TO_REPLACE; + ReplacedTexture &replaced = FindReplacement(entry, w, h); + if (replaced.Valid()) { + // We're replacing, so we won't scale. + scaleFactor = 1; + maxLevel = replaced.MaxLevel(); + badMipSizes = false; } // Don't scale the PPGe texture. diff --git a/GPU/Directx9/TextureCacheDX9.cpp b/GPU/Directx9/TextureCacheDX9.cpp index 20f5c696cc..26f15e6998 100644 --- a/GPU/Directx9/TextureCacheDX9.cpp +++ b/GPU/Directx9/TextureCacheDX9.cpp @@ -440,24 +440,14 @@ void TextureCacheDX9::BuildTexture(TexCacheEntry *const entry) { scaleFactor = scaleFactor > 4 ? 4 : (scaleFactor > 2 ? 2 : 1); } - u64 cachekey = replacer_.Enabled() ? entry->CacheKey() : 0; int w = gstate.getTextureWidth(0); int h = gstate.getTextureHeight(0); - double replaceStart = time_now_d(); - ReplacedTexture &replaced = replacer_.FindReplacement(cachekey, entry->fullhash, w, h); - if (replaced.IsReady(replacementFrameBudget_ - replacementTimeThisFrame_)) { - if (replaced.GetSize(0, w, h)) { - replacementTimeThisFrame_ += time_now_d() - replaceStart; - - // We're replacing, so we won't scale. - scaleFactor = 1; - entry->status |= TexCacheEntry::STATUS_IS_SCALED; - entry->status &= ~TexCacheEntry::STATUS_TO_REPLACE; - maxLevel = replaced.MaxLevel(); - badMipSizes = false; - } - } else if (replaced.MaxLevel() >= 0) { - entry->status |= TexCacheEntry::STATUS_TO_REPLACE; + ReplacedTexture &replaced = FindReplacement(entry, w, h); + if (replaced.Valid()) { + // We're replacing, so we won't scale. + scaleFactor = 1; + maxLevel = replaced.MaxLevel(); + badMipSizes = false; } // Don't scale the PPGe texture. diff --git a/GPU/GLES/TextureCacheGLES.cpp b/GPU/GLES/TextureCacheGLES.cpp index 9a972792c4..b95f6881fa 100644 --- a/GPU/GLES/TextureCacheGLES.cpp +++ b/GPU/GLES/TextureCacheGLES.cpp @@ -504,24 +504,14 @@ void TextureCacheGLES::BuildTexture(TexCacheEntry *const entry) { scaleFactor = scaleFactor > 4 ? 4 : (scaleFactor > 2 ? 2 : 1); } - u64 cachekey = replacer_.Enabled() ? entry->CacheKey() : 0; int w = gstate.getTextureWidth(0); int h = gstate.getTextureHeight(0); - double replaceStart = time_now_d(); - ReplacedTexture &replaced = replacer_.FindReplacement(cachekey, entry->fullhash, w, h); - if (replaced.IsReady(replacementFrameBudget_ - replacementTimeThisFrame_)) { - if (replaced.GetSize(0, w, h)) { - replacementTimeThisFrame_ += time_now_d() - replaceStart; - - // We're replacing, so we won't scale. - scaleFactor = 1; - entry->status |= TexCacheEntry::STATUS_IS_SCALED; - entry->status &= ~TexCacheEntry::STATUS_TO_REPLACE; - maxLevel = replaced.MaxLevel(); - badMipSizes = false; - } - } else if (replaced.MaxLevel() >= 0) { - entry->status |= TexCacheEntry::STATUS_TO_REPLACE; + ReplacedTexture &replaced = FindReplacement(entry, w, h); + if (replaced.Valid()) { + // We're replacing, so we won't scale. + scaleFactor = 1; + maxLevel = replaced.MaxLevel(); + badMipSizes = false; } // Don't scale the PPGe texture. diff --git a/GPU/Vulkan/TextureCacheVulkan.cpp b/GPU/Vulkan/TextureCacheVulkan.cpp index 7df7877109..47b2fe8924 100644 --- a/GPU/Vulkan/TextureCacheVulkan.cpp +++ b/GPU/Vulkan/TextureCacheVulkan.cpp @@ -775,24 +775,14 @@ void TextureCacheVulkan::BuildTexture(TexCacheEntry *const entry) { scaleFactor = scaleFactor > 4 ? 4 : (scaleFactor > 2 ? 2 : 1); } - u64 cachekey = replacer_.Enabled() ? entry->CacheKey() : 0; int w = gstate.getTextureWidth(0); int h = gstate.getTextureHeight(0); - double replaceStart = time_now_d(); - ReplacedTexture &replaced = replacer_.FindReplacement(cachekey, entry->fullhash, w, h); - if (replaced.IsReady(replacementFrameBudget_ - replacementTimeThisFrame_)) { - if (replaced.GetSize(0, w, h)) { - replacementTimeThisFrame_ += time_now_d() - replaceStart; - - // We're replacing, so we won't scale. - scaleFactor = 1; - entry->status |= TexCacheEntry::STATUS_IS_SCALED; - entry->status &= ~TexCacheEntry::STATUS_TO_REPLACE; - maxLevel = replaced.MaxLevel(); - badMipSizes = false; - } - } else if (replaced.MaxLevel() >= 0) { - entry->status |= TexCacheEntry::STATUS_TO_REPLACE; + ReplacedTexture &replaced = FindReplacement(entry, w, h); + if (replaced.Valid()) { + // We're replacing, so we won't scale. + scaleFactor = 1; + maxLevel = replaced.MaxLevel(); + badMipSizes = false; } bool hardwareScaling = g_Config.bTexHardwareScaling && (uploadCS_ != VK_NULL_HANDLE || copyCS_ != VK_NULL_HANDLE); @@ -909,7 +899,7 @@ void TextureCacheVulkan::BuildTexture(TexCacheEntry *const entry) { ReplacedTextureDecodeInfo replacedInfo; if (replacer_.Enabled() && !replaced.Valid()) { - replacedInfo.cachekey = cachekey; + replacedInfo.cachekey = entry->CacheKey(); replacedInfo.hash = entry->fullhash; replacedInfo.addr = entry->addr; replacedInfo.isVideo = IsVideo(entry->addr); @@ -944,7 +934,7 @@ void TextureCacheVulkan::BuildTexture(TexCacheEntry *const entry) { if (replaced.Valid()) { // Directly load the replaced image. data = drawEngine_->GetPushBufferForTextureData()->PushAligned(size, &bufferOffset, &texBuf, pushAlignment); - replaceStart = time_now_d(); + double replaceStart = time_now_d(); replaced.Load(i, data, stride); // if it fails, it'll just be garbage data... OK for now. replacementTimeThisFrame_ += time_now_d() - replaceStart; entry->vkTex->UploadMip(cmdInit, i, mipWidth, mipHeight, texBuf, bufferOffset, stride / bpp);