Skip to content

Commit

Permalink
Replacement: Purge old cached decoded textures.
Browse files Browse the repository at this point in the history
Not actually decoding into the cache, just setup.
  • Loading branch information
unknownbrackets committed Oct 22, 2021
1 parent 2356280 commit 36fc2c2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
38 changes: 37 additions & 1 deletion Core/TextureReplacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "Common/File/FileUtil.h"
#include "Common/StringUtils.h"
#include "Common/Thread/ParallelLoop.h"
#include "Common/TimeUtil.h"
#include "Core/Config.h"
#include "Core/Host.h"
#include "Core/System.h"
Expand Down Expand Up @@ -621,6 +622,15 @@ void TextureReplacer::NotifyTextureDecoded(const ReplacedTextureDecodeInfo &repl
savedCache_[replacementKey] = saved;
}

void TextureReplacer::Decimate(bool forcePressure) {
// Allow replacements to be cached for a long time, although they're large.
const double age = forcePressure ? 90.0 : 1800.0;
const double threshold = time_now_d() - age;
for (auto &item : cache_) {
item.second.PurgeIfOlder(threshold);
}
}

template <typename Key, typename Value>
static typename std::unordered_map<Key, Value>::const_iterator LookupWildcard(const std::unordered_map<Key, Value> &map, Key &key, u64 cachekey, u32 hash, bool ignoreAddress) {
auto alias = map.find(key);
Expand Down Expand Up @@ -734,7 +744,33 @@ float TextureReplacer::LookupReduceHashRange(int& w, int& h) {
}

bool ReplacedTexture::IsReady(double budget) {
return true;
lastUsed_ = time_now_d();

if (levelData_.size() == levels_.size())
return true;
if (budget <= 0.0)
return false;

double deadline = lastUsed_ + budget;
for (int i = (int)levelData_.size(); i <= MaxLevel(); ++i) {
levelData_.resize(i + 1);
PrepareData(i);
if (time_now_d() >= deadline) {
break;
}
}
// If we finished all the levels, we're done.
return levelData_.size() == levels_.size();
}

void ReplacedTexture::PrepareData(int level) {
// TODO
}

void ReplacedTexture::PurgeIfOlder(double t) {
if (lastUsed_ < t) {
levelData_.clear();
}
}

bool ReplacedTexture::Load(int level, void *out, int rowPitch) {
Expand Down
7 changes: 7 additions & 0 deletions Core/TextureReplacer.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,13 @@ struct ReplacedTexture {
bool Load(int level, void *out, int rowPitch);

protected:
void PrepareData(int level);
void PurgeIfOlder(double t);

std::vector<ReplacedTextureLevel> levels_;
std::vector<std::vector<uint8_t>> levelData_;
ReplacedTextureAlpha alphaStatus_;
double lastUsed_ = 0.0;

friend TextureReplacer;
};
Expand Down Expand Up @@ -193,6 +198,8 @@ class TextureReplacer {

void NotifyTextureDecoded(const ReplacedTextureDecodeInfo &replacedInfo, const void *data, int pitch, int level, int w, int h);

void Decimate(bool forcePressure);

static bool GenerateIni(const std::string &gameID, Path &generatedFilename);

protected:
Expand Down
1 change: 1 addition & 0 deletions GPU/Common/TextureCacheCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ void TextureCacheCommon::Decimate(bool forcePressure) {
}

DecimateVideos();
replacer_.Decimate(forcePressure);
}

void TextureCacheCommon::DecimateVideos() {
Expand Down

0 comments on commit 36fc2c2

Please sign in to comment.