From 0db48c956ca4179f3bde0572b178ddf3670b76f5 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 30 Oct 2022 08:15:37 -0700 Subject: [PATCH] Replacement: Decimate aggressively with high usage. --- Core/TextureReplacer.cpp | 33 ++++++++++++++++++++++++++++----- Core/TextureReplacer.h | 4 +++- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Core/TextureReplacer.cpp b/Core/TextureReplacer.cpp index 450fb0adb2..56bc897a47 100644 --- a/Core/TextureReplacer.cpp +++ b/Core/TextureReplacer.cpp @@ -47,6 +47,7 @@ static const std::string INI_FILENAME = "textures.ini"; static const std::string NEW_TEXTURE_DIR = "new/"; static const int VERSION = 1; static const int MAX_MIP_LEVELS = 12; // 12 should be plenty, 8 is the max mip levels supported by the PSP. +static const double MAX_CACHE_SIZE = 4.0; TextureReplacer::TextureReplacer() { none_.initDone_ = true; @@ -674,15 +675,27 @@ void TextureReplacer::NotifyTextureDecoded(const ReplacedTextureDecodeInfo &repl void TextureReplacer::Decimate(ReplacerDecimateMode mode) { // Allow replacements to be cached for a long time, although they're large. double age = 1800.0; - if (mode == ReplacerDecimateMode::FORCE_PRESSURE) + if (mode == ReplacerDecimateMode::FORCE_PRESSURE) { age = 90.0; - else if (mode == ReplacerDecimateMode::ALL) + } else if (mode == ReplacerDecimateMode::ALL) { age = 0.0; + } else if (lastTextureCacheSizeGB_ > 1.0) { + double pressure = std::min(MAX_CACHE_SIZE, lastTextureCacheSizeGB_) / MAX_CACHE_SIZE; + // Get more aggressive the closer we are to the max. + age = 90.0 + (1.0 - pressure) * 1710.0; + } const double threshold = time_now_d() - age; + size_t totalSize = 0; for (auto &item : cache_) { - item.second.PurgeIfOlder(threshold); + totalSize += item.second.PurgeIfOlder(threshold); } + + double totalSizeGB = totalSize / (1024.0 * 1024.0 * 1024.0); + if (totalSizeGB >= 1.0) { + WARN_LOG(G3D, "Decimated replacements older than %fs, currently using %f GB of RAM", age, totalSizeGB); + } + lastTextureCacheSizeGB_ = totalSizeGB; } template @@ -968,11 +981,21 @@ void ReplacedTexture::PrepareData(int level) { fclose(fp); } -void ReplacedTexture::PurgeIfOlder(double t) { - if (lastUsed_ < t && (!threadWaitable_ || threadWaitable_->WaitFor(0.0))) { +size_t ReplacedTexture::PurgeIfOlder(double t) { + if (threadWaitable_ && !threadWaitable_->WaitFor(0.0)) + return 0; + + if (lastUsed_ < t) { levelData_.clear(); initDone_ = false; + return 0; } + + size_t s = 0; + for (auto &l : levelData_) { + s += l.size(); + } + return s; } ReplacedTexture::~ReplacedTexture() { diff --git a/Core/TextureReplacer.h b/Core/TextureReplacer.h index b32d31a7d3..f64c9d9b7f 100644 --- a/Core/TextureReplacer.h +++ b/Core/TextureReplacer.h @@ -166,7 +166,8 @@ struct ReplacedTexture { protected: void Prepare(); void PrepareData(int level); - void PurgeIfOlder(double t); + // Returns size of data not purged. + size_t PurgeIfOlder(double t); std::vector levels_; std::vector> levelData_; @@ -248,6 +249,7 @@ protected: bool reduceHash_ = false; float reduceHashSize = 1.0; // default value with reduceHash to false float reduceHashGlobalValue = 0.5; // Global value for textures dump pngs of all sizes, 0.5 by default but can be set in textures.ini + double lastTextureCacheSizeGB_ = 0.0; bool ignoreMipmap_ = false; std::string gameID_; Path basePath_;