diff --git a/GPU/Common/TextureCacheCommon.cpp b/GPU/Common/TextureCacheCommon.cpp index bcaec6d426..68977e39e3 100644 --- a/GPU/Common/TextureCacheCommon.cpp +++ b/GPU/Common/TextureCacheCommon.cpp @@ -39,7 +39,10 @@ TextureCacheCommon::TextureCacheCommon(Draw::DrawContext *draw) : draw_(draw), + clearCacheNextFrame_(false), + lowMemoryMode_(false), cacheSizeEstimate_(0), + secondCacheSizeEstimate_(0), nextTexture_(nullptr), clutLastFormat_(0xFFFFFFFF), clutTotalBytes_(0), @@ -782,3 +785,67 @@ bool TextureCacheCommon::ReadIndexedTex(u8 *out, int outPitch, int level, const return true; } + + +bool TextureCacheCommon::CheckFullHash(TexCacheEntry *const entry, bool &doDelete) { + bool hashFail = false; + int w = gstate.getTextureWidth(0); + int h = gstate.getTextureHeight(0); + u32 fullhash = QuickTexHash(replacer, entry->addr, entry->bufw, w, h, GETextureFormat(entry->format), entry); + if (fullhash != entry->fullhash) { + hashFail = true; + } else { + if (g_Config.bTextureBackoffCache) { + if (entry->GetHashStatus() != TexCacheEntry::STATUS_HASHING && entry->numFrames > TexCacheEntry::FRAMES_REGAIN_TRUST) { + // Reset to STATUS_HASHING. + entry->SetHashStatus(TexCacheEntry::STATUS_HASHING); + entry->status &= ~TexCacheEntry::STATUS_CHANGE_FREQUENT; + } + } else if (entry->numFrames > TEXCACHE_FRAME_CHANGE_FREQUENT_REGAIN_TRUST) { + entry->status &= ~TexCacheEntry::STATUS_CHANGE_FREQUENT; + } + } + + if (hashFail) { + entry->status |= TexCacheEntry::STATUS_UNRELIABLE; + if (entry->numFrames < TEXCACHE_FRAME_CHANGE_FREQUENT) { + if (entry->status & TexCacheEntry::STATUS_FREE_CHANGE) { + entry->status &= ~TexCacheEntry::STATUS_FREE_CHANGE; + } else { + entry->status |= TexCacheEntry::STATUS_CHANGE_FREQUENT; + } + } + entry->numFrames = 0; + + // Don't give up just yet. Let's try the secondary cache if it's been invalidated before. + // If it's failed a bunch of times, then the second cache is just wasting time and VRAM. + if (g_Config.bTextureSecondaryCache) { + if (entry->numInvalidated > 2 && entry->numInvalidated < 128 && !lowMemoryMode_) { + u64 secondKey = fullhash | (u64)entry->cluthash << 32; + TexCache::iterator secondIter = secondCache.find(secondKey); + if (secondIter != secondCache.end()) { + TexCacheEntry *secondEntry = &secondIter->second; + if (secondEntry->Matches(entry->dim, entry->format, entry->maxLevel)) { + // Reset the numInvalidated value lower, we got a match. + if (entry->numInvalidated > 8) { + --entry->numInvalidated; + } + nextTexture_ = secondEntry; + return true; + } + } else { + secondKey = entry->fullhash | ((u64)entry->cluthash << 32); + secondCacheSizeEstimate_ += EstimateTexMemoryUsage(entry); + secondCache[secondKey] = *entry; + doDelete = false; + } + } + } + + // We know it failed, so update the full hash right away. + entry->fullhash = fullhash; + return false; + } + + return true; +} diff --git a/GPU/Common/TextureCacheCommon.h b/GPU/Common/TextureCacheCommon.h index 71eb7bfa8c..69cbf24847 100644 --- a/GPU/Common/TextureCacheCommon.h +++ b/GPU/Common/TextureCacheCommon.h @@ -24,6 +24,7 @@ #include "Common/MemoryUtil.h" #include "Core/TextureReplacer.h" #include "GPU/Common/GPUDebugInterface.h" +#include "GPU/Common/TextureDecoder.h" enum TextureFiltering { TEX_FILTER_AUTO = 1, @@ -38,6 +39,11 @@ enum FramebufferNotification { NOTIFY_FB_DESTROYED, }; +// Changes more frequent than this will be considered "frequent" and prevent texture scaling. +#define TEXCACHE_FRAME_CHANGE_FREQUENT 6 +// Note: only used when hash backoff is disabled. +#define TEXCACHE_FRAME_CHANGE_FREQUENT_REGAIN_TRUST 33 + struct VirtualFramebuffer; class CachedTextureVulkan; @@ -150,6 +156,8 @@ public: }; protected: + bool CheckFullHash(TexCacheEntry *const entry, bool &doDelete); + // Can't be unordered_map, we use lower_bound ... although for some reason that compiles on MSVC. typedef std::map TexCache; @@ -181,12 +189,37 @@ protected: void DecimateVideos(); + inline u32 QuickTexHash(TextureReplacer &replacer, u32 addr, int bufw, int w, int h, GETextureFormat format, TexCacheEntry *entry) { + if (replacer.Enabled()) { + return replacer.ComputeHash(addr, bufw, w, h, format, entry->maxSeenV); + } + + if (h == 512 && entry->maxSeenV < 512 && entry->maxSeenV != 0) { + h = (int)entry->maxSeenV; + } + + const u32 sizeInRAM = (textureBitsPerPixel[format] * bufw * h) / 8; + const u32 *checkp = (const u32 *)Memory::GetPointer(addr); + + if (Memory::IsValidAddress(addr + sizeInRAM)) { + return DoQuickTexHash(checkp, sizeInRAM); + } else { + return 0; + } + } + Draw::DrawContext *draw_; TextureReplacer replacer; + bool clearCacheNextFrame_; + bool lowMemoryMode_; + TexCache cache; u32 cacheSizeEstimate_; + TexCache secondCache; + u32 secondCacheSizeEstimate_; + std::vector fbCache_; std::map fbTexInfo_; diff --git a/GPU/Directx9/TextureCacheDX9.cpp b/GPU/Directx9/TextureCacheDX9.cpp index 622e14d6ef..8e2bcca642 100644 --- a/GPU/Directx9/TextureCacheDX9.cpp +++ b/GPU/Directx9/TextureCacheDX9.cpp @@ -70,9 +70,6 @@ static const D3DVERTEXELEMENT9 g_FramebufferVertexElements[] = { TextureCacheDX9::TextureCacheDX9(Draw::DrawContext *draw) : TextureCacheCommon(draw), - secondCacheSizeEstimate_(0), - clearCacheNextFrame_(false), - lowMemoryMode_(false), texelsScaledThisFrame_(0) { timesInvalidatedAllThisFrame_ = 0; lastBoundTexture = INVALID_TEX; diff --git a/GPU/Directx9/TextureCacheDX9.h b/GPU/Directx9/TextureCacheDX9.h index 1767fe4610..88336c263f 100644 --- a/GPU/Directx9/TextureCacheDX9.h +++ b/GPU/Directx9/TextureCacheDX9.h @@ -102,11 +102,6 @@ private: } } - TexCache secondCache; - u32 secondCacheSizeEstimate_; - - bool clearCacheNextFrame_; - bool lowMemoryMode_; TextureScalerDX9 scaler; u32 clutHash_; diff --git a/GPU/GLES/TextureCacheGLES.cpp b/GPU/GLES/TextureCacheGLES.cpp index efa615dc20..bf75be7787 100644 --- a/GPU/GLES/TextureCacheGLES.cpp +++ b/GPU/GLES/TextureCacheGLES.cpp @@ -72,9 +72,6 @@ TextureCacheGLES::TextureCacheGLES(Draw::DrawContext *draw) : TextureCacheCommon(draw), - secondCacheSizeEstimate_(0), - clearCacheNextFrame_(false), - lowMemoryMode_(false), texelsScaledThisFrame_(0) { timesInvalidatedAllThisFrame_ = 0; lastBoundTexture = INVALID_TEX; @@ -506,25 +503,6 @@ static inline u32 MiniHash(const u32 *ptr) { return ptr[0]; } -static inline u32 QuickTexHash(TextureReplacer &replacer, u32 addr, int bufw, int w, int h, GETextureFormat format, TextureCacheGLES::TexCacheEntry *entry) { - if (replacer.Enabled()) { - return replacer.ComputeHash(addr, bufw, w, h, format, entry->maxSeenV); - } - - if (h == 512 && entry->maxSeenV < 512 && entry->maxSeenV != 0) { - h = (int)entry->maxSeenV; - } - - const u32 sizeInRAM = (textureBitsPerPixel[format] * bufw * h) / 8; - const u32 *checkp = (const u32 *) Memory::GetPointer(addr); - - if (Memory::IsValidAddress(addr + sizeInRAM)) { - return DoQuickTexHash(checkp, sizeInRAM); - } else { - return 0; - } -} - void TextureCacheGLES::UpdateCurrentClut(GEPaletteFormat clutFormat, u32 clutBase, bool clutIndexIsSimple) { const u32 clutBaseBytes = clutFormat == GE_CMODE_32BIT_ABGR8888 ? (clutBase * sizeof(u32)) : (clutBase * sizeof(u16)); // Technically, these extra bytes weren't loaded, but hopefully it was loaded earlier. @@ -1164,69 +1142,6 @@ void TextureCacheGLES::SetTexture(bool force) { nextNeedsRebuild_= true; } -bool TextureCacheGLES::CheckFullHash(TexCacheEntry *const entry, bool &doDelete) { - bool hashFail = false; - int w = gstate.getTextureWidth(0); - int h = gstate.getTextureHeight(0); - u32 fullhash = QuickTexHash(replacer, entry->addr, entry->bufw, w, h, GETextureFormat(entry->format), entry); - if (fullhash != entry->fullhash) { - hashFail = true; - } else { - if (g_Config.bTextureBackoffCache) { - if (entry->GetHashStatus() != TexCacheEntry::STATUS_HASHING && entry->numFrames > TexCacheEntry::FRAMES_REGAIN_TRUST) { - // Reset to STATUS_HASHING. - entry->SetHashStatus(TexCacheEntry::STATUS_HASHING); - entry->status &= ~TexCacheEntry::STATUS_CHANGE_FREQUENT; - } - } else if (entry->numFrames > TEXCACHE_FRAME_CHANGE_FREQUENT_REGAIN_TRUST) { - entry->status &= ~TexCacheEntry::STATUS_CHANGE_FREQUENT; - } - } - - if (hashFail) { - entry->status |= TexCacheEntry::STATUS_UNRELIABLE; - if (entry->numFrames < TEXCACHE_FRAME_CHANGE_FREQUENT) { - if (entry->status & TexCacheEntry::STATUS_FREE_CHANGE) { - entry->status &= ~TexCacheEntry::STATUS_FREE_CHANGE; - } else { - entry->status |= TexCacheEntry::STATUS_CHANGE_FREQUENT; - } - } - entry->numFrames = 0; - - // Don't give up just yet. Let's try the secondary cache if it's been invalidated before. - // If it's failed a bunch of times, then the second cache is just wasting time and VRAM. - if (g_Config.bTextureSecondaryCache) { - if (entry->numInvalidated > 2 && entry->numInvalidated < 128 && !lowMemoryMode_) { - u64 secondKey = fullhash | (u64)entry->cluthash << 32; - TexCache::iterator secondIter = secondCache.find(secondKey); - if (secondIter != secondCache.end()) { - TexCacheEntry *secondEntry = &secondIter->second; - if (secondEntry->Matches(entry->dim, entry->format, entry->maxLevel)) { - // Reset the numInvalidated value lower, we got a match. - if (entry->numInvalidated > 8) { - --entry->numInvalidated; - } - nextTexture_ = secondEntry; - return true; - } - } else { - secondKey = entry->fullhash | ((u64)entry->cluthash << 32); - secondCacheSizeEstimate_ += EstimateTexMemoryUsage(entry); - secondCache[secondKey] = *entry; - doDelete = false; - } - } - } - - // We know it failed, so update the full hash right away. - entry->fullhash = fullhash; - return false; - } - - return true; -} - bool TextureCacheGLES::HandleTextureChange(TexCacheEntry *const entry, const char *reason, bool initialMatch, bool doDelete) { bool replaceImages = false; diff --git a/GPU/GLES/TextureCacheGLES.h b/GPU/GLES/TextureCacheGLES.h index c031c65ff5..21aac4fed5 100644 --- a/GPU/GLES/TextureCacheGLES.h +++ b/GPU/GLES/TextureCacheGLES.h @@ -103,16 +103,10 @@ private: void SetTextureFramebuffer(TexCacheEntry *entry, VirtualFramebuffer *framebuffer); void ApplyTextureFramebuffer(TexCacheEntry *entry, VirtualFramebuffer *framebuffer); - bool CheckFullHash(TexCacheEntry *const entry, bool &doDelete); bool HandleTextureChange(TexCacheEntry *const entry, const char *reason, bool initialMatch, bool doDelete); void BuildTexture(TexCacheEntry *const entry, bool replaceImages); std::vector nameCache_; - TexCache secondCache; - u32 secondCacheSizeEstimate_; - - bool clearCacheNextFrame_; - bool lowMemoryMode_; TextureScalerGLES scaler; diff --git a/GPU/Vulkan/TextureCacheVulkan.cpp b/GPU/Vulkan/TextureCacheVulkan.cpp index 966435d9b7..9527d15a32 100644 --- a/GPU/Vulkan/TextureCacheVulkan.cpp +++ b/GPU/Vulkan/TextureCacheVulkan.cpp @@ -57,11 +57,6 @@ // Try to be prime to other decimation intervals. #define TEXCACHE_DECIMATION_INTERVAL 13 -// Changes more frequent than this will be considered "frequent" and prevent texture scaling. -#define TEXCACHE_FRAME_CHANGE_FREQUENT 6 -// Note: only used when hash backoff is disabled. -#define TEXCACHE_FRAME_CHANGE_FREQUENT_REGAIN_TRUST 33 - #define TEXCACHE_NAME_CACHE_SIZE 16 #define TEXCACHE_MAX_TEXELS_SCALED (256*256) // Per frame @@ -145,9 +140,6 @@ TextureCacheVulkan::TextureCacheVulkan(Draw::DrawContext *draw, VulkanContext *v : TextureCacheCommon(draw), vulkan_(vulkan), samplerCache_(vulkan), - secondCacheSizeEstimate_(0), - clearCacheNextFrame_(false), - lowMemoryMode_(false), texelsScaledThisFrame_(0) { timesInvalidatedAllThisFrame_ = 0; lastBoundTexture = nullptr; diff --git a/GPU/Vulkan/TextureCacheVulkan.h b/GPU/Vulkan/TextureCacheVulkan.h index 69857cac4f..02c56b2517 100644 --- a/GPU/Vulkan/TextureCacheVulkan.h +++ b/GPU/Vulkan/TextureCacheVulkan.h @@ -150,12 +150,6 @@ private: VulkanContext *vulkan_; VulkanDeviceAllocator *allocator_; - TexCache secondCache; - u32 secondCacheSizeEstimate_; - - bool clearCacheNextFrame_; - bool lowMemoryMode_; - SamplerCache samplerCache_; TextureScalerVulkan scaler;