Skip to content

Commit

Permalink
Separate out the checks for save vs replace textures more, fixing som…
Browse files Browse the repository at this point in the history
…e issues
  • Loading branch information
hrydgard committed Jan 23, 2024
1 parent 77d52b8 commit 768520d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
6 changes: 3 additions & 3 deletions GPU/Common/TextureCacheCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1553,7 +1553,7 @@ ReplacedTexture *TextureCacheCommon::FindReplacement(TexCacheEntry *entry, int *
// Short circuit the non-enabled case.
// Otherwise, due to bReplaceTexturesAllowLate, we'll still spawn tasks looking for replacements
// that then won't be used.
if (!replacer_.Enabled()) {
if (!replacer_.ReplaceEnabled()) {
return nullptr;
}

Expand All @@ -1562,7 +1562,7 @@ ReplacedTexture *TextureCacheCommon::FindReplacement(TexCacheEntry *entry, int *
}

double replaceStart = time_now_d();
u64 cachekey = replacer_.Enabled() ? entry->CacheKey() : 0;
u64 cachekey = entry->CacheKey();
ReplacedTexture *replaced = replacer_.FindReplacement(cachekey, entry->fullhash, *w, *h);
replacementTimeThisFrame_ += time_now_d() - replaceStart;
if (!replaced) {
Expand Down Expand Up @@ -2915,7 +2915,7 @@ bool TextureCacheCommon::PrepareBuildTexture(BuildTexturePlan &plan, TexCacheEnt
// But, we still need to create the texture at a larger size.
plan.replaced->GetSize(0, &plan.createW, &plan.createH);
} else {
if (replacer_.Enabled() && !plan.doReplace && plan.depth == 1 && canReplace) {
if (replacer_.SaveEnabled() && !plan.doReplace && plan.depth == 1 && canReplace) {
ReplacedTextureDecodeInfo replacedInfo;
// TODO: Do we handle the race where a replacement becomes valid AFTER this but before we save?
replacedInfo.cachekey = entry->CacheKey();
Expand Down
40 changes: 20 additions & 20 deletions GPU/Common/TextureReplacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,37 +78,38 @@ TextureReplacer::~TextureReplacer() {
void TextureReplacer::NotifyConfigChanged() {
gameID_ = g_paramSFO.GetDiscID();

bool wasEnabled = enabled_;
enabled_ = g_Config.bReplaceTextures || g_Config.bSaveNewTextures;
if (enabled_) {
bool wasReplaceEnabled = replaceEnabled_;
replaceEnabled_ = g_Config.bReplaceTextures;
saveEnabled_ = g_Config.bSaveNewTextures;
if (replaceEnabled_ || saveEnabled_) {
basePath_ = GetSysDirectory(DIRECTORY_TEXTURES) / gameID_;

replaceEnabled_ = replaceEnabled_ && File::IsDirectory(basePath_);
newTextureDir_ = basePath_ / NEW_TEXTURE_DIR;

// If we're saving, auto-create the directory.
if (g_Config.bSaveNewTextures && !File::Exists(newTextureDir_)) {
if (saveEnabled_ && !File::Exists(newTextureDir_)) {
INFO_LOG(G3D, "Creating new texture directory: '%s'", newTextureDir_.ToVisualString().c_str());
File::CreateFullPath(newTextureDir_);
// We no longer create a nomedia file here, since we put one
// in the TEXTURES root.
}
}

enabled_ = File::IsDirectory(basePath_);
if (saveEnabled_) {
// Somewhat crude message, re-using translation strings.
auto d = GetI18NCategory(I18NCat::DEVELOPER);
auto di = GetI18NCategory(I18NCat::DIALOG);
g_OSD.Show(OSDType::MESSAGE_INFO, std::string(d->T("Save new textures")) + ": " + di->T("Enabled"), 2.0f);
}

if (g_Config.bSaveNewTextures) {
// Somewhat crude message, re-using translation strings.
auto d = GetI18NCategory(I18NCat::DEVELOPER);
auto di = GetI18NCategory(I18NCat::DIALOG);
g_OSD.Show(OSDType::MESSAGE_INFO, std::string(d->T("Save new textures")) + ": " + di->T("Enabled"), 2.0f);
}
} else if (wasEnabled) {
if (!replaceEnabled_ && wasReplaceEnabled) {
delete vfs_;
vfs_ = nullptr;
Decimate(ReplacerDecimateMode::ALL);
}

if (enabled_) {
enabled_ = LoadIni();
if (replaceEnabled_) {
replaceEnabled_ = LoadIni();
}
}

Expand Down Expand Up @@ -315,7 +316,7 @@ bool TextureReplacer::LoadIniValues(IniFile &ini, VFSBackend *dir, bool isOverri
if (ini.HasSection("hashes")) {
auto hashes = ini.GetOrCreateSection("hashes")->ToMap();
// Format: hashname = filename.png
bool checkFilenames = g_Config.bSaveNewTextures && !g_Config.bIgnoreTextureFilenames && !vfsIsZip_;
bool checkFilenames = saveEnabled_ && !g_Config.bIgnoreTextureFilenames && !vfsIsZip_;

for (const auto &item : hashes) {
ReplacementCacheKey key(0, 0);
Expand Down Expand Up @@ -479,7 +480,7 @@ void TextureReplacer::ParseReduceHashRange(const std::string& key, const std::st
}

u32 TextureReplacer::ComputeHash(u32 addr, int bufw, int w, int h, bool swizzled, GETextureFormat fmt, u16 maxSeenV) {
_dbg_assert_msg_(enabled_, "Replacement not enabled");
_dbg_assert_msg_(replaceEnabled_ || saveEnabled_, "Replacement not enabled");

// TODO: Take swizzled into account, like in QuickTexHash().
// Note: Currently, only the MLB games are known to need this.
Expand Down Expand Up @@ -723,8 +724,7 @@ class SaveTextureTask : public Task {
};

bool TextureReplacer::WillSave(const ReplacedTextureDecodeInfo &replacedInfo) {
_assert_msg_(enabled_, "Replacement not enabled");
if (!g_Config.bSaveNewTextures)
if (!saveEnabled_)
return false;
// Don't save the PPGe texture.
if (replacedInfo.addr > 0x05000000 && replacedInfo.addr < PSP_GetKernelMemoryEnd())
Expand All @@ -736,7 +736,7 @@ bool TextureReplacer::WillSave(const ReplacedTextureDecodeInfo &replacedInfo) {
}

void TextureReplacer::NotifyTextureDecoded(ReplacedTexture *texture, const ReplacedTextureDecodeInfo &replacedInfo, const void *data, int pitch, int level, int origW, int origH, int scaledW, int scaledH) {
_assert_msg_(enabled_, "Replacement not enabled");
_assert_msg_(saveEnabled_, "Texture saving not enabled");
_assert_(pitch >= 0);

if (!WillSave(replacedInfo)) {
Expand Down
8 changes: 6 additions & 2 deletions GPU/Common/TextureReplacer.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ class TextureReplacer {

void NotifyConfigChanged();

bool Enabled() const { return enabled_; }
bool Enabled() const { return replaceEnabled_ || saveEnabled_; } // used to check hashing method etc.
bool ReplaceEnabled() const { return replaceEnabled_; }
bool SaveEnabled() const { return saveEnabled_; }

bool AllowVideo() const { return allowVideo_; }

u32 ComputeHash(u32 addr, int bufw, int w, int h, bool swizzled, GETextureFormat fmt, u16 maxSeenV);
Expand Down Expand Up @@ -140,7 +143,8 @@ class TextureReplacer {
void ScanForHashNamedFiles(VFSBackend *dir, std::map<ReplacementCacheKey, std::map<int, std::string>> &filenameMap);
void ComputeAliasMap(const std::map<ReplacementCacheKey, std::map<int, std::string>> &filenameMap);

bool enabled_ = false;
bool replaceEnabled_ = false;
bool saveEnabled_ = false;
bool allowVideo_ = false;
bool ignoreAddress_ = false;
bool reduceHash_ = false;
Expand Down

0 comments on commit 768520d

Please sign in to comment.