2016-04-30 20:44:31 +00:00
|
|
|
// Copyright (c) 2016- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
2021-03-03 04:42:55 +00:00
|
|
|
#include "ppsspp_config.h"
|
2016-05-01 04:10:48 +00:00
|
|
|
#include <algorithm>
|
2021-10-24 02:58:23 +00:00
|
|
|
#include <atomic>
|
2021-05-16 16:30:33 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <memory>
|
2021-03-03 04:42:55 +00:00
|
|
|
#include <png.h>
|
2020-09-29 10:19:22 +00:00
|
|
|
|
2016-04-30 21:19:23 +00:00
|
|
|
#include "ext/xxhash.h"
|
2020-10-05 22:16:23 +00:00
|
|
|
|
2021-05-01 14:15:04 +00:00
|
|
|
#include "Common/Data/Convert/ColorConv.h"
|
2020-10-03 22:25:21 +00:00
|
|
|
#include "Common/Data/Format/IniFile.h"
|
2021-05-16 16:30:33 +00:00
|
|
|
#include "Common/Data/Format/ZIMLoad.h"
|
2023-03-07 21:20:56 +00:00
|
|
|
#include "Common/Data/Format/PNGLoad.h"
|
2021-05-01 14:15:04 +00:00
|
|
|
#include "Common/Data/Text/I18n.h"
|
2020-10-01 11:05:04 +00:00
|
|
|
#include "Common/Data/Text/Parsers.h"
|
2023-03-07 19:53:50 +00:00
|
|
|
#include "Common/File/VFS/DirectoryReader.h"
|
|
|
|
#include "Common/File/VFS/ZipFileReader.h"
|
2020-10-04 18:48:47 +00:00
|
|
|
#include "Common/File/FileUtil.h"
|
2023-03-07 09:02:59 +00:00
|
|
|
#include "Common/File/VFS/VFS.h"
|
2022-12-31 14:31:45 +00:00
|
|
|
#include "Common/LogReporting.h"
|
2020-09-29 10:19:22 +00:00
|
|
|
#include "Common/StringUtils.h"
|
2020-11-27 23:12:06 +00:00
|
|
|
#include "Common/Thread/ParallelLoop.h"
|
2022-04-08 10:04:34 +00:00
|
|
|
#include "Common/Thread/Waitable.h"
|
2022-04-17 22:25:59 +00:00
|
|
|
#include "Common/Thread/ThreadManager.h"
|
2021-10-17 16:16:54 +00:00
|
|
|
#include "Common/TimeUtil.h"
|
2016-04-30 21:05:03 +00:00
|
|
|
#include "Core/Config.h"
|
2018-10-01 00:55:51 +00:00
|
|
|
#include "Core/Host.h"
|
2016-04-30 21:05:03 +00:00
|
|
|
#include "Core/System.h"
|
2016-04-30 22:07:56 +00:00
|
|
|
#include "Core/TextureReplacer.h"
|
2021-05-16 16:30:33 +00:00
|
|
|
#include "Core/ThreadPools.h"
|
2016-04-30 21:05:03 +00:00
|
|
|
#include "Core/ELF/ParamSFO.h"
|
2016-04-30 21:19:23 +00:00
|
|
|
#include "GPU/Common/TextureDecoder.h"
|
2016-04-30 20:44:31 +00:00
|
|
|
|
2016-04-30 23:21:16 +00:00
|
|
|
static const std::string INI_FILENAME = "textures.ini";
|
2022-10-30 17:34:25 +00:00
|
|
|
static const std::string ZIP_FILENAME = "textures.zip";
|
2016-05-01 03:21:08 +00:00
|
|
|
static const std::string NEW_TEXTURE_DIR = "new/";
|
2016-04-30 23:21:16 +00:00
|
|
|
static const int VERSION = 1;
|
2017-12-05 11:18:13 +00:00
|
|
|
static const int MAX_MIP_LEVELS = 12; // 12 should be plenty, 8 is the max mip levels supported by the PSP.
|
2022-10-30 15:15:37 +00:00
|
|
|
static const double MAX_CACHE_SIZE = 4.0;
|
2016-04-30 23:21:16 +00:00
|
|
|
|
2018-03-26 10:33:36 +00:00
|
|
|
TextureReplacer::TextureReplacer() {
|
2022-08-22 05:07:05 +00:00
|
|
|
none_.initDone_ = true;
|
|
|
|
none_.prepareDone_ = true;
|
2016-04-30 20:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureReplacer::~TextureReplacer() {
|
2023-03-07 19:53:50 +00:00
|
|
|
delete vfs_;
|
2016-04-30 20:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureReplacer::Init() {
|
2016-04-30 21:05:03 +00:00
|
|
|
NotifyConfigChanged();
|
2016-04-30 20:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureReplacer::NotifyConfigChanged() {
|
2017-06-03 03:54:28 +00:00
|
|
|
gameID_ = g_paramSFO.GetDiscID();
|
2016-04-30 21:05:03 +00:00
|
|
|
|
2022-07-28 03:27:56 +00:00
|
|
|
bool wasEnabled = enabled_;
|
2017-05-26 21:15:04 +00:00
|
|
|
enabled_ = g_Config.bReplaceTextures || g_Config.bSaveNewTextures;
|
2016-04-30 21:05:03 +00:00
|
|
|
if (enabled_) {
|
2021-05-11 07:50:28 +00:00
|
|
|
basePath_ = GetSysDirectory(DIRECTORY_TEXTURES) / gameID_;
|
2016-04-30 21:05:03 +00:00
|
|
|
|
2021-05-09 13:02:46 +00:00
|
|
|
Path newTextureDir = basePath_ / NEW_TEXTURE_DIR;
|
|
|
|
|
2016-04-30 21:05:03 +00:00
|
|
|
// If we're saving, auto-create the directory.
|
2021-05-09 13:02:46 +00:00
|
|
|
if (g_Config.bSaveNewTextures && !File::Exists(newTextureDir)) {
|
|
|
|
File::CreateFullPath(newTextureDir);
|
|
|
|
File::CreateEmptyFile(newTextureDir / ".nomedia");
|
2016-04-30 21:05:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 14:55:17 +00:00
|
|
|
enabled_ = File::IsDirectory(basePath_);
|
2022-07-28 03:27:56 +00:00
|
|
|
} else if (wasEnabled) {
|
2023-03-07 19:53:50 +00:00
|
|
|
delete vfs_;
|
|
|
|
vfs_ = nullptr;
|
2022-07-28 03:27:56 +00:00
|
|
|
Decimate(ReplacerDecimateMode::ALL);
|
2016-04-30 21:05:03 +00:00
|
|
|
}
|
|
|
|
|
2016-04-30 23:21:16 +00:00
|
|
|
if (enabled_) {
|
|
|
|
enabled_ = LoadIni();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextureReplacer::LoadIni() {
|
|
|
|
hash_ = ReplacedTextureHash::QUICK;
|
|
|
|
aliases_.clear();
|
|
|
|
hashranges_.clear();
|
2021-02-28 01:16:16 +00:00
|
|
|
filtering_.clear();
|
2021-04-01 09:18:06 +00:00
|
|
|
reducehashranges_.clear();
|
2016-04-30 23:21:16 +00:00
|
|
|
|
2019-07-14 23:01:37 +00:00
|
|
|
allowVideo_ = false;
|
|
|
|
ignoreAddress_ = false;
|
|
|
|
reduceHash_ = false;
|
2021-04-01 09:18:06 +00:00
|
|
|
reduceHashGlobalValue = 0.5;
|
2020-04-13 14:29:07 +00:00
|
|
|
// Prevents dumping the mipmaps.
|
|
|
|
ignoreMipmap_ = false;
|
2019-07-14 23:01:37 +00:00
|
|
|
|
2023-03-07 19:53:50 +00:00
|
|
|
delete vfs_;
|
|
|
|
vfs_ = nullptr;
|
2022-10-30 17:34:25 +00:00
|
|
|
|
|
|
|
// First, check for textures.zip, which is used to reduce IO.
|
2023-03-07 19:53:50 +00:00
|
|
|
VFSBackend *dir = ZipFileReader::Create(basePath_ / ZIP_FILENAME, "");
|
|
|
|
if (!dir) {
|
|
|
|
dir = new DirectoryReader(basePath_);
|
2022-10-30 17:34:25 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 19:53:50 +00:00
|
|
|
IniFile ini;
|
|
|
|
bool iniLoaded = ini.LoadFromVFS(g_VFS, (basePath_ / INI_FILENAME).ToString());
|
2022-10-30 17:34:25 +00:00
|
|
|
|
|
|
|
if (iniLoaded) {
|
2019-07-14 23:01:37 +00:00
|
|
|
if (!LoadIniValues(ini)) {
|
2023-03-07 19:53:50 +00:00
|
|
|
delete dir;
|
2016-04-30 23:21:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-14 23:01:37 +00:00
|
|
|
// Allow overriding settings per game id.
|
|
|
|
std::string overrideFilename;
|
|
|
|
if (ini.GetOrCreateSection("games")->Get(gameID_.c_str(), &overrideFilename, "")) {
|
|
|
|
if (!overrideFilename.empty() && overrideFilename != INI_FILENAME) {
|
|
|
|
IniFile overrideIni;
|
2023-03-07 19:53:50 +00:00
|
|
|
iniLoaded = overrideIni.LoadFromVFS(*dir, overrideFilename);
|
2022-10-30 17:34:25 +00:00
|
|
|
if (!iniLoaded) {
|
2022-10-30 14:55:17 +00:00
|
|
|
ERROR_LOG(G3D, "Failed to load extra texture ini: %s", overrideFilename.c_str());
|
2023-03-08 07:23:13 +00:00
|
|
|
// Since this error is most likely to occure for texture pack creators, let's just bail here
|
|
|
|
// so that the creator is more likely to look in the logs for what happened.
|
2023-03-07 19:53:50 +00:00
|
|
|
delete dir;
|
2022-10-30 14:55:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-01 15:58:14 +00:00
|
|
|
|
2022-10-30 14:55:17 +00:00
|
|
|
INFO_LOG(G3D, "Loading extra texture ini: %s", overrideFilename.c_str());
|
2019-07-14 23:01:37 +00:00
|
|
|
if (!LoadIniValues(overrideIni, true)) {
|
2023-03-07 19:53:50 +00:00
|
|
|
delete dir;
|
2019-07-14 23:01:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-05-06 22:12:20 +00:00
|
|
|
}
|
2019-07-14 23:01:37 +00:00
|
|
|
}
|
2016-05-01 15:58:14 +00:00
|
|
|
|
2023-03-07 19:53:50 +00:00
|
|
|
vfs_ = dir;
|
|
|
|
|
|
|
|
// The ini doesn't have to exist for the texture directory or zip to be valid.
|
2019-07-14 23:01:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextureReplacer::LoadIniValues(IniFile &ini, bool isOverride) {
|
|
|
|
auto options = ini.GetOrCreateSection("options");
|
|
|
|
std::string hash;
|
|
|
|
options->Get("hash", &hash, "");
|
|
|
|
// TODO: crc32c.
|
|
|
|
if (strcasecmp(hash.c_str(), "quick") == 0) {
|
|
|
|
hash_ = ReplacedTextureHash::QUICK;
|
|
|
|
} else if (strcasecmp(hash.c_str(), "xxh32") == 0) {
|
|
|
|
hash_ = ReplacedTextureHash::XXH32;
|
|
|
|
} else if (strcasecmp(hash.c_str(), "xxh64") == 0) {
|
|
|
|
hash_ = ReplacedTextureHash::XXH64;
|
|
|
|
} else if (!isOverride || !hash.empty()) {
|
|
|
|
ERROR_LOG(G3D, "Unsupported hash type: %s", hash.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
options->Get("video", &allowVideo_, allowVideo_);
|
|
|
|
options->Get("ignoreAddress", &ignoreAddress_, ignoreAddress_);
|
|
|
|
// Multiplies sizeInRAM/bytesPerLine in XXHASH by 0.5.
|
|
|
|
options->Get("reduceHash", &reduceHash_, reduceHash_);
|
2020-04-13 14:29:07 +00:00
|
|
|
options->Get("ignoreMipmap", &ignoreMipmap_, ignoreMipmap_);
|
2019-07-14 23:01:37 +00:00
|
|
|
if (reduceHash_ && hash_ == ReplacedTextureHash::QUICK) {
|
|
|
|
reduceHash_ = false;
|
|
|
|
ERROR_LOG(G3D, "Texture Replacement: reduceHash option requires safer hash, use xxh32 or xxh64 instead.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ignoreAddress_ && hash_ == ReplacedTextureHash::QUICK) {
|
|
|
|
ignoreAddress_ = false;
|
|
|
|
ERROR_LOG(G3D, "Texture Replacement: ignoreAddress option requires safer hash, use xxh32 or xxh64 instead.");
|
|
|
|
}
|
|
|
|
|
|
|
|
int version = 0;
|
|
|
|
if (options->Get("version", &version, 0) && version > VERSION) {
|
|
|
|
ERROR_LOG(G3D, "Unsupported texture replacement version %d, trying anyway", version);
|
|
|
|
}
|
2016-04-30 23:21:16 +00:00
|
|
|
|
2019-07-14 23:01:37 +00:00
|
|
|
bool filenameWarning = false;
|
|
|
|
if (ini.HasSection("hashes")) {
|
|
|
|
auto hashes = ini.GetOrCreateSection("hashes")->ToMap();
|
|
|
|
// Format: hashname = filename.png
|
|
|
|
bool checkFilenames = g_Config.bSaveNewTextures && !g_Config.bIgnoreTextureFilenames;
|
|
|
|
for (const auto &item : hashes) {
|
|
|
|
ReplacementAliasKey key(0, 0, 0);
|
|
|
|
if (sscanf(item.first.c_str(), "%16llx%8x_%d", &key.cachekey, &key.hash, &key.level) >= 1) {
|
|
|
|
aliases_[key] = item.second;
|
|
|
|
if (checkFilenames) {
|
2018-10-01 00:55:51 +00:00
|
|
|
#if PPSSPP_PLATFORM(WINDOWS)
|
2019-07-14 23:01:37 +00:00
|
|
|
// Uppercase probably means the filenames don't match.
|
|
|
|
// Avoiding an actual check of the filenames to avoid performance impact.
|
|
|
|
filenameWarning = filenameWarning || item.second.find_first_of("\\ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos;
|
2018-10-01 00:55:51 +00:00
|
|
|
#else
|
2019-07-14 23:01:37 +00:00
|
|
|
filenameWarning = filenameWarning || item.second.find_first_of("\\:<>|?*") != std::string::npos;
|
2018-10-01 00:55:51 +00:00
|
|
|
#endif
|
2016-05-31 05:31:02 +00:00
|
|
|
}
|
2019-07-14 23:01:37 +00:00
|
|
|
} else {
|
|
|
|
ERROR_LOG(G3D, "Unsupported syntax under [hashes]: %s", item.first.c_str());
|
2016-04-30 23:21:16 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-14 23:01:37 +00:00
|
|
|
}
|
2016-04-30 23:21:16 +00:00
|
|
|
|
2019-07-14 23:01:37 +00:00
|
|
|
if (filenameWarning) {
|
2020-01-26 18:43:18 +00:00
|
|
|
auto err = GetI18NCategory("Error");
|
2019-07-14 23:01:37 +00:00
|
|
|
host->NotifyUserMessage(err->T("textures.ini filenames may not be cross-platform"), 6.0f);
|
|
|
|
}
|
2018-10-01 00:55:51 +00:00
|
|
|
|
2019-07-14 23:01:37 +00:00
|
|
|
if (ini.HasSection("hashranges")) {
|
|
|
|
auto hashranges = ini.GetOrCreateSection("hashranges")->ToMap();
|
|
|
|
// Format: addr,w,h = newW,newH
|
|
|
|
for (const auto &item : hashranges) {
|
|
|
|
ParseHashRange(item.first, item.second);
|
2016-04-30 23:21:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:16:16 +00:00
|
|
|
if (ini.HasSection("filtering")) {
|
|
|
|
auto filters = ini.GetOrCreateSection("filtering")->ToMap();
|
|
|
|
// Format: hashname = nearest or linear
|
|
|
|
for (const auto &item : filters) {
|
|
|
|
ParseFiltering(item.first, item.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 09:18:06 +00:00
|
|
|
if (ini.HasSection("reducehashranges")) {
|
|
|
|
auto reducehashranges = ini.GetOrCreateSection("reducehashranges")->ToMap();
|
2023-03-07 09:02:59 +00:00
|
|
|
// Format: w,h = reducehashvalues
|
2021-04-01 09:18:06 +00:00
|
|
|
for (const auto& item : reducehashranges) {
|
|
|
|
ParseReduceHashRange(item.first, item.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-30 23:21:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureReplacer::ParseHashRange(const std::string &key, const std::string &value) {
|
|
|
|
std::vector<std::string> keyParts;
|
|
|
|
SplitString(key, ',', keyParts);
|
|
|
|
std::vector<std::string> valueParts;
|
|
|
|
SplitString(value, ',', valueParts);
|
|
|
|
|
|
|
|
if (keyParts.size() != 3 || valueParts.size() != 2) {
|
|
|
|
ERROR_LOG(G3D, "Ignoring invalid hashrange %s = %s, expecting addr,w,h = w,h", key.c_str(), value.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 addr;
|
|
|
|
u32 fromW;
|
|
|
|
u32 fromH;
|
|
|
|
if (!TryParse(keyParts[0], &addr) || !TryParse(keyParts[1], &fromW) || !TryParse(keyParts[2], &fromH)) {
|
|
|
|
ERROR_LOG(G3D, "Ignoring invalid hashrange %s = %s, key format is 0x12345678,512,512", key.c_str(), value.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 toW;
|
|
|
|
u32 toH;
|
|
|
|
if (!TryParse(valueParts[0], &toW) || !TryParse(valueParts[1], &toH)) {
|
|
|
|
ERROR_LOG(G3D, "Ignoring invalid hashrange %s = %s, value format is 512,512", key.c_str(), value.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-01 01:39:30 +00:00
|
|
|
if (toW > fromW || toH > fromH) {
|
|
|
|
ERROR_LOG(G3D, "Ignoring invalid hashrange %s = %s, range bigger than source", key.c_str(), value.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-25 20:34:52 +00:00
|
|
|
const u64 rangeKey = ((u64)addr << 32) | ((u64)fromW << 16) | fromH;
|
2016-04-30 23:21:16 +00:00
|
|
|
hashranges_[rangeKey] = WidthHeightPair(toW, toH);
|
2016-04-30 20:44:31 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 01:16:16 +00:00
|
|
|
void TextureReplacer::ParseFiltering(const std::string &key, const std::string &value) {
|
|
|
|
ReplacementCacheKey itemKey(0, 0);
|
|
|
|
if (sscanf(key.c_str(), "%16llx%8x", &itemKey.cachekey, &itemKey.hash) >= 1) {
|
|
|
|
if (!strcasecmp(value.c_str(), "nearest")) {
|
|
|
|
filtering_[itemKey] = TEX_FILTER_FORCE_NEAREST;
|
|
|
|
} else if (!strcasecmp(value.c_str(), "linear")) {
|
|
|
|
filtering_[itemKey] = TEX_FILTER_FORCE_LINEAR;
|
|
|
|
} else if (!strcasecmp(value.c_str(), "auto")) {
|
|
|
|
filtering_[itemKey] = TEX_FILTER_AUTO;
|
|
|
|
} else {
|
|
|
|
ERROR_LOG(G3D, "Unsupported syntax under [filtering]: %s", value.c_str());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ERROR_LOG(G3D, "Unsupported syntax under [filtering]: %s", key.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 09:18:06 +00:00
|
|
|
void TextureReplacer::ParseReduceHashRange(const std::string& key, const std::string& value) {
|
|
|
|
std::vector<std::string> keyParts;
|
|
|
|
SplitString(key, ',', keyParts);
|
|
|
|
std::vector<std::string> valueParts;
|
|
|
|
SplitString(value, ',', valueParts);
|
|
|
|
|
|
|
|
if (keyParts.size() != 2 || valueParts.size() != 1) {
|
|
|
|
ERROR_LOG(G3D, "Ignoring invalid reducehashrange %s = %s, expecting w,h = reducehashvalue", key.c_str(), value.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 forW;
|
|
|
|
u32 forH;
|
|
|
|
if (!TryParse(keyParts[0], &forW) || !TryParse(keyParts[1], &forH)) {
|
|
|
|
ERROR_LOG(G3D, "Ignoring invalid reducehashrange %s = %s, key format is 512,512", key.c_str(), value.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
float rhashvalue;
|
|
|
|
if (!TryParse(valueParts[0], &rhashvalue)) {
|
|
|
|
ERROR_LOG(G3D, "Ignoring invalid reducehashrange %s = %s, value format is 0.5", key.c_str(), value.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rhashvalue == 0) {
|
|
|
|
ERROR_LOG(G3D, "Ignoring invalid hashrange %s = %s, reducehashvalue can't be 0", key.c_str(), value.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const u64 reducerangeKey = ((u64)forW << 16) | forH;
|
|
|
|
reducehashranges_[reducerangeKey] = rhashvalue;
|
2023-03-07 09:02:59 +00:00
|
|
|
}
|
2021-04-01 09:18:06 +00:00
|
|
|
|
2016-04-30 20:44:31 +00:00
|
|
|
u32 TextureReplacer::ComputeHash(u32 addr, int bufw, int w, int h, GETextureFormat fmt, u16 maxSeenV) {
|
2020-07-19 15:47:02 +00:00
|
|
|
_dbg_assert_msg_(enabled_, "Replacement not enabled");
|
2016-04-30 21:19:23 +00:00
|
|
|
|
|
|
|
if (!LookupHashRange(addr, w, h)) {
|
|
|
|
// There wasn't any hash range, let's fall back to maxSeenV logic.
|
|
|
|
if (h == 512 && maxSeenV < 512 && maxSeenV != 0) {
|
|
|
|
h = (int)maxSeenV;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-01 20:12:04 +00:00
|
|
|
const u8 *checkp = Memory::GetPointerUnchecked(addr);
|
2021-04-01 09:18:06 +00:00
|
|
|
if (reduceHash_) {
|
|
|
|
reduceHashSize = LookupReduceHashRange(w, h);
|
|
|
|
// default to reduceHashGlobalValue which default is 0.5
|
|
|
|
}
|
2016-05-01 00:30:32 +00:00
|
|
|
if (bufw <= w) {
|
|
|
|
// We can assume the data is contiguous. These are the total used pixels.
|
|
|
|
const u32 totalPixels = bufw * h + (w - bufw);
|
2017-05-07 04:03:21 +00:00
|
|
|
const u32 sizeInRAM = (textureBitsPerPixel[fmt] * totalPixels) / 8 * reduceHashSize;
|
2016-05-01 00:30:32 +00:00
|
|
|
|
|
|
|
switch (hash_) {
|
|
|
|
case ReplacedTextureHash::QUICK:
|
|
|
|
return StableQuickTexHash(checkp, sizeInRAM);
|
2017-05-06 12:51:24 +00:00
|
|
|
case ReplacedTextureHash::XXH32:
|
2020-08-28 08:15:22 +00:00
|
|
|
return XXH32(checkp, sizeInRAM, 0xBACD7814);
|
2017-05-06 12:51:24 +00:00
|
|
|
case ReplacedTextureHash::XXH64:
|
2020-08-28 08:15:22 +00:00
|
|
|
return XXH64(checkp, sizeInRAM, 0xBACD7814);
|
2016-05-01 00:30:32 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We have gaps. Let's hash each row and sum.
|
2017-05-07 04:03:21 +00:00
|
|
|
const u32 bytesPerLine = (textureBitsPerPixel[fmt] * w) / 8 * reduceHashSize;
|
2016-05-01 00:30:32 +00:00
|
|
|
const u32 stride = (textureBitsPerPixel[fmt] * bufw) / 8;
|
|
|
|
|
|
|
|
u32 result = 0;
|
|
|
|
switch (hash_) {
|
|
|
|
case ReplacedTextureHash::QUICK:
|
|
|
|
for (int y = 0; y < h; ++y) {
|
|
|
|
u32 rowHash = StableQuickTexHash(checkp, bytesPerLine);
|
|
|
|
result = (result * 11) ^ rowHash;
|
|
|
|
checkp += stride;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-05-06 12:51:24 +00:00
|
|
|
case ReplacedTextureHash::XXH32:
|
|
|
|
for (int y = 0; y < h; ++y) {
|
2020-08-28 08:15:22 +00:00
|
|
|
u32 rowHash = XXH32(checkp, bytesPerLine, 0xBACD7814);
|
2017-05-06 12:51:24 +00:00
|
|
|
result = (result * 11) ^ rowHash;
|
|
|
|
checkp += stride;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ReplacedTextureHash::XXH64:
|
|
|
|
for (int y = 0; y < h; ++y) {
|
2020-08-28 08:15:22 +00:00
|
|
|
u32 rowHash = XXH64(checkp, bytesPerLine, 0xBACD7814);
|
2017-05-06 12:51:24 +00:00
|
|
|
result = (result * 11) ^ rowHash;
|
2016-05-01 00:30:32 +00:00
|
|
|
checkp += stride;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2016-04-30 23:21:16 +00:00
|
|
|
}
|
2016-04-30 20:44:31 +00:00
|
|
|
}
|
|
|
|
|
2022-08-22 05:07:05 +00:00
|
|
|
ReplacedTexture &TextureReplacer::FindReplacement(u64 cachekey, u32 hash, int w, int h, double budget) {
|
2016-04-30 21:05:03 +00:00
|
|
|
// Only actually replace if we're replacing. We might just be saving.
|
2016-05-01 15:04:15 +00:00
|
|
|
if (!Enabled() || !g_Config.bReplaceTextures) {
|
2016-05-01 02:35:35 +00:00
|
|
|
return none_;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReplacementCacheKey replacementKey(cachekey, hash);
|
|
|
|
auto it = cache_.find(replacementKey);
|
|
|
|
if (it != cache_.end()) {
|
2022-08-22 05:07:05 +00:00
|
|
|
if (!it->second.prepareDone_ && budget > 0.0) {
|
|
|
|
// We don't do this on a thread, but we only do it while within budget.
|
|
|
|
PopulateReplacement(&it->second, cachekey, hash, w, h);
|
|
|
|
}
|
2016-05-01 02:35:35 +00:00
|
|
|
return it->second;
|
2016-05-01 01:39:30 +00:00
|
|
|
}
|
2016-05-01 02:35:35 +00:00
|
|
|
|
|
|
|
// Okay, let's construct the result.
|
|
|
|
ReplacedTexture &result = cache_[replacementKey];
|
2023-03-07 19:53:50 +00:00
|
|
|
result.vfs_ = this->vfs_;
|
2022-08-22 05:07:05 +00:00
|
|
|
if (!g_Config.bReplaceTexturesAllowLate || budget > 0.0) {
|
|
|
|
PopulateReplacement(&result, cachekey, hash, w, h);
|
|
|
|
}
|
2016-05-01 01:39:30 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureReplacer::PopulateReplacement(ReplacedTexture *result, u64 cachekey, u32 hash, int w, int h) {
|
|
|
|
int newW = w;
|
|
|
|
int newH = h;
|
|
|
|
LookupHashRange(cachekey >> 32, newW, newH);
|
|
|
|
|
2017-05-05 19:40:40 +00:00
|
|
|
if (ignoreAddress_) {
|
|
|
|
cachekey = cachekey & 0xFFFFFFFFULL;
|
|
|
|
}
|
|
|
|
|
2016-05-01 01:39:30 +00:00
|
|
|
for (int i = 0; i < MAX_MIP_LEVELS; ++i) {
|
2021-04-03 15:56:28 +00:00
|
|
|
const std::string hashfile = LookupHashFile(cachekey, hash, i);
|
2022-10-30 14:50:57 +00:00
|
|
|
if (hashfile.empty()) {
|
2016-05-01 01:39:30 +00:00
|
|
|
// Out of valid mip levels. Bail out.
|
|
|
|
break;
|
|
|
|
}
|
2016-04-30 22:41:12 +00:00
|
|
|
|
2023-03-07 19:53:50 +00:00
|
|
|
const Path filename = basePath_ / hashfile;
|
|
|
|
|
2016-05-01 03:21:08 +00:00
|
|
|
ReplacedTextureLevel level;
|
2022-07-29 16:36:21 +00:00
|
|
|
level.fmt = Draw::DataFormat::R8G8B8A8_UNORM;
|
2021-05-11 07:50:28 +00:00
|
|
|
level.file = filename;
|
2022-10-30 17:34:25 +00:00
|
|
|
|
|
|
|
bool good;
|
|
|
|
bool logError = hashfile != HashName(cachekey, hash, i) + ".png";
|
2022-12-31 04:25:58 +00:00
|
|
|
|
2023-03-07 19:53:50 +00:00
|
|
|
VFSFileReference *fileRef = vfs_->GetFile(hashfile.c_str());
|
2023-03-07 21:02:44 +00:00
|
|
|
if (!fileRef) {
|
|
|
|
// If the file doesn't exist, let's just bail immediately here.
|
|
|
|
break;
|
|
|
|
}
|
2023-03-07 19:53:50 +00:00
|
|
|
|
|
|
|
level.fileRef = fileRef;
|
|
|
|
good = PopulateLevel(level, !logError);
|
2016-05-01 01:39:30 +00:00
|
|
|
|
2021-05-16 16:30:33 +00:00
|
|
|
// We pad files that have been hashrange'd so they are the same texture size.
|
|
|
|
level.w = (level.w * w) / newW;
|
|
|
|
level.h = (level.h * h) / newH;
|
2018-06-06 04:53:32 +00:00
|
|
|
|
|
|
|
if (good && i != 0) {
|
|
|
|
// Check that the mipmap size is correct. Can't load mips of the wrong size.
|
|
|
|
if (level.w != (result->levels_[0].w >> i) || level.h != (result->levels_[0].h >> i)) {
|
|
|
|
WARN_LOG(G3D, "Replacement mipmap invalid: size=%dx%d, expected=%dx%d (level %d, '%s')", level.w, level.h, result->levels_[0].w >> i, result->levels_[0].h >> i, i, filename.c_str());
|
|
|
|
good = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (good)
|
|
|
|
result->levels_.push_back(level);
|
|
|
|
// Otherwise, we're done loading mips (bad PNG or bad size, either way.)
|
|
|
|
else
|
|
|
|
break;
|
2016-04-30 21:05:03 +00:00
|
|
|
}
|
2016-09-11 18:03:41 +00:00
|
|
|
|
2022-10-30 15:54:52 +00:00
|
|
|
// Populate the level data pointers for each level.
|
|
|
|
result->levelData_.resize(result->levels_.size());
|
|
|
|
for (size_t i = 0; i < result->levels_.size(); ++i) {
|
|
|
|
result->levelData_[i] = &levelCache_[result->levels_[i]];
|
|
|
|
}
|
|
|
|
|
2022-08-22 05:07:05 +00:00
|
|
|
result->prepareDone_ = true;
|
2016-04-30 20:44:31 +00:00
|
|
|
}
|
|
|
|
|
2021-05-16 16:30:33 +00:00
|
|
|
enum class ReplacedImageType {
|
|
|
|
PNG,
|
|
|
|
ZIM,
|
|
|
|
INVALID,
|
|
|
|
};
|
|
|
|
|
2023-03-07 19:53:50 +00:00
|
|
|
static inline ReplacedImageType IdentifyMagic(const uint8_t magic[4]) {
|
2022-10-30 17:34:25 +00:00
|
|
|
if (strncmp((const char *)magic, "ZIMG", 4) == 0)
|
|
|
|
return ReplacedImageType::ZIM;
|
|
|
|
if (magic[0] == 0x89 && strncmp((const char *)&magic[1], "PNG", 3) == 0)
|
|
|
|
return ReplacedImageType::PNG;
|
|
|
|
return ReplacedImageType::INVALID;
|
|
|
|
}
|
|
|
|
|
2023-03-07 19:53:50 +00:00
|
|
|
static ReplacedImageType Identify(VFSBackend *vfs, VFSOpenFile *openFile, std::string *outMagic) {
|
2021-05-16 16:30:33 +00:00
|
|
|
uint8_t magic[4];
|
2023-03-07 19:53:50 +00:00
|
|
|
if (vfs->Read(openFile, magic, 4) != 4) {
|
|
|
|
*outMagic = "FAIL";
|
2021-05-16 16:30:33 +00:00
|
|
|
return ReplacedImageType::INVALID;
|
2022-07-26 21:48:39 +00:00
|
|
|
}
|
2023-03-07 19:53:50 +00:00
|
|
|
// Turn the signature into a readable string that we can display in an error message.
|
|
|
|
*outMagic = std::string((const char *)magic, 4);
|
|
|
|
for (int i = 0; i < outMagic->size(); i++) {
|
|
|
|
if ((s8)(*outMagic)[i] < 32) {
|
|
|
|
(*outMagic)[i] = '_';
|
2021-05-16 16:30:33 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-07 19:53:50 +00:00
|
|
|
vfs->Rewind(openFile);
|
|
|
|
return IdentifyMagic(magic);
|
2021-05-16 16:30:33 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 19:53:50 +00:00
|
|
|
bool TextureReplacer::PopulateLevel(ReplacedTextureLevel &level, bool ignoreError) {
|
2022-10-30 17:34:25 +00:00
|
|
|
bool good = false;
|
|
|
|
|
2023-03-07 19:53:50 +00:00
|
|
|
if (!level.fileRef) {
|
2022-10-30 17:34:25 +00:00
|
|
|
if (!ignoreError)
|
|
|
|
ERROR_LOG(G3D, "Error opening replacement texture file '%s' in textures.zip", level.file.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2023-03-07 21:02:44 +00:00
|
|
|
|
2023-03-07 19:53:50 +00:00
|
|
|
File::FileInfo info;
|
2023-03-07 21:02:44 +00:00
|
|
|
if (!vfs_->GetFileInfo(level.fileRef, &info)) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-03-07 19:53:50 +00:00
|
|
|
size_t zsize = info.size;
|
2022-10-30 17:34:25 +00:00
|
|
|
|
2023-03-07 19:53:50 +00:00
|
|
|
VFSOpenFile *file = vfs_->OpenFileForRead(level.fileRef);
|
|
|
|
if (!file) {
|
2022-10-30 17:34:25 +00:00
|
|
|
return false;
|
2023-03-07 19:53:50 +00:00
|
|
|
}
|
2022-10-30 17:34:25 +00:00
|
|
|
|
2023-03-07 19:53:50 +00:00
|
|
|
std::string magic;
|
|
|
|
auto imageType = Identify(vfs_, file, &magic);
|
2022-10-30 17:34:25 +00:00
|
|
|
|
|
|
|
if (imageType == ReplacedImageType::ZIM) {
|
|
|
|
uint32_t ignore = 0;
|
2023-03-07 19:53:50 +00:00
|
|
|
struct ZimHeader {
|
|
|
|
uint32_t magic;
|
|
|
|
uint32_t w;
|
|
|
|
uint32_t h;
|
|
|
|
uint32_t flags;
|
|
|
|
} header;
|
|
|
|
good = vfs_->Read(file, &header, sizeof(header)) == sizeof(header);
|
2023-03-08 07:23:13 +00:00
|
|
|
level.w = header.w;
|
|
|
|
level.h = header.h;
|
2023-03-07 19:53:50 +00:00
|
|
|
good = (header.flags & ZIM_FORMAT_MASK) == ZIM_RGBA8888;
|
2022-10-30 17:34:25 +00:00
|
|
|
} else if (imageType == ReplacedImageType::PNG) {
|
2023-03-07 21:20:56 +00:00
|
|
|
PNGHeaderPeek headerPeek;
|
2023-03-07 19:53:50 +00:00
|
|
|
good = vfs_->Read(file, &headerPeek, sizeof(headerPeek)) == sizeof(headerPeek);
|
2023-03-08 07:29:23 +00:00
|
|
|
if (good && headerPeek.IsValidPNGHeader()) {
|
2023-03-07 21:20:56 +00:00
|
|
|
level.w = headerPeek.Width();
|
|
|
|
level.h = headerPeek.Height();
|
2022-10-30 17:34:25 +00:00
|
|
|
good = true;
|
|
|
|
} else {
|
2023-03-07 21:20:56 +00:00
|
|
|
ERROR_LOG(G3D, "Could not get PNG dimensions: %s (zip)", level.file.ToVisualString().c_str());
|
2023-03-08 07:29:23 +00:00
|
|
|
good = false;
|
2022-10-30 17:34:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-03-07 19:53:50 +00:00
|
|
|
ERROR_LOG(G3D, "Could not load texture replacement info: %s - unsupported format %s", level.file.ToVisualString().c_str(), magic.c_str());
|
2022-10-30 17:34:25 +00:00
|
|
|
}
|
2023-03-07 19:53:50 +00:00
|
|
|
vfs_->CloseFile(file);
|
2022-10-30 17:34:25 +00:00
|
|
|
|
|
|
|
return good;
|
|
|
|
}
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
static bool WriteTextureToPNG(png_imagep image, const Path &filename, int convert_to_8bit, const void *buffer, png_int_32 row_stride, const void *colormap) {
|
2016-04-30 22:03:39 +00:00
|
|
|
FILE *fp = File::OpenCFile(filename, "wb");
|
|
|
|
if (!fp) {
|
2020-08-15 09:51:22 +00:00
|
|
|
ERROR_LOG(IO, "Unable to open texture file for writing.");
|
2016-04-30 22:03:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer, row_stride, colormap)) {
|
2020-08-15 18:02:07 +00:00
|
|
|
fclose(fp);
|
2016-04-30 22:03:39 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2017-03-06 12:10:23 +00:00
|
|
|
ERROR_LOG(SYSTEM, "Texture PNG encode failed.");
|
2016-04-30 22:03:39 +00:00
|
|
|
fclose(fp);
|
|
|
|
remove(filename.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 22:25:59 +00:00
|
|
|
class TextureSaveTask : public Task {
|
|
|
|
public:
|
|
|
|
// Could probably just use a vector.
|
|
|
|
SimpleBuf<u32> data;
|
|
|
|
|
|
|
|
int w = 0;
|
|
|
|
int h = 0;
|
|
|
|
int pitch = 0; // bytes
|
|
|
|
|
2022-04-18 03:25:41 +00:00
|
|
|
Path basePath;
|
|
|
|
std::string hashfile;
|
2022-04-17 22:25:59 +00:00
|
|
|
u32 replacedInfoHash;
|
|
|
|
|
2022-04-18 03:25:41 +00:00
|
|
|
bool skipIfExists = false;
|
|
|
|
|
2022-04-17 22:25:59 +00:00
|
|
|
TextureSaveTask(SimpleBuf<u32> _data) : data(std::move(_data)) {}
|
|
|
|
|
2023-01-15 16:15:59 +00:00
|
|
|
// This must be IO blocking because of Android storage, despite being CPU heavy.
|
|
|
|
TaskType Type() const override { return TaskType::IO_BLOCKING; }
|
2023-01-15 15:55:07 +00:00
|
|
|
|
|
|
|
TaskPriority Priority() const override {
|
|
|
|
return TaskPriority::LOW;
|
|
|
|
}
|
|
|
|
|
2022-04-17 22:25:59 +00:00
|
|
|
void Run() override {
|
2022-04-18 03:25:41 +00:00
|
|
|
const Path filename = basePath / hashfile;
|
|
|
|
const Path saveFilename = basePath / NEW_TEXTURE_DIR / hashfile;
|
|
|
|
|
|
|
|
// Should we skip writing if the newly saved data already exists?
|
|
|
|
// We do this on the thread due to slow IO.
|
|
|
|
if (skipIfExists && File::Exists(saveFilename))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// And we always skip if the replace file already exists.
|
2022-04-18 03:30:20 +00:00
|
|
|
if (File::Exists(filename))
|
2022-04-18 03:25:41 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Create subfolder as needed.
|
|
|
|
#ifdef _WIN32
|
|
|
|
size_t slash = hashfile.find_last_of("/\\");
|
|
|
|
#else
|
|
|
|
size_t slash = hashfile.find_last_of("/");
|
|
|
|
#endif
|
|
|
|
if (slash != hashfile.npos) {
|
|
|
|
// Create any directory structure as needed.
|
|
|
|
const Path saveDirectory = basePath / NEW_TEXTURE_DIR / hashfile.substr(0, slash);
|
|
|
|
if (!File::Exists(saveDirectory)) {
|
|
|
|
File::CreateFullPath(saveDirectory);
|
|
|
|
File::CreateEmptyFile(saveDirectory / ".nomedia");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-23 20:52:28 +00:00
|
|
|
png_image png{};
|
2022-04-17 22:25:59 +00:00
|
|
|
png.version = PNG_IMAGE_VERSION;
|
|
|
|
png.format = PNG_FORMAT_RGBA;
|
|
|
|
png.width = w;
|
|
|
|
png.height = h;
|
2022-04-18 03:25:41 +00:00
|
|
|
bool success = WriteTextureToPNG(&png, saveFilename, 0, data.data(), pitch, nullptr);
|
2022-04-17 22:25:59 +00:00
|
|
|
png_image_free(&png);
|
|
|
|
if (png.warning_or_error >= 2) {
|
|
|
|
ERROR_LOG(COMMON, "Saving screenshot to PNG produced errors.");
|
|
|
|
} else if (success) {
|
2022-09-01 12:31:52 +00:00
|
|
|
NOTICE_LOG(G3D, "Saving texture for replacement: %08x / %dx%d in '%s'", replacedInfoHash, w, h, saveFilename.ToVisualString().c_str());
|
2022-04-17 22:25:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-18 03:25:41 +00:00
|
|
|
bool TextureReplacer::WillSave(const ReplacedTextureDecodeInfo &replacedInfo) {
|
|
|
|
_assert_msg_(enabled_, "Replacement not enabled");
|
|
|
|
if (!g_Config.bSaveNewTextures)
|
|
|
|
return false;
|
|
|
|
// Don't save the PPGe texture.
|
|
|
|
if (replacedInfo.addr > 0x05000000 && replacedInfo.addr < PSP_GetKernelMemoryEnd())
|
|
|
|
return false;
|
|
|
|
if (replacedInfo.isVideo && !allowVideo_)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-04-17 22:25:59 +00:00
|
|
|
|
2016-05-01 15:54:43 +00:00
|
|
|
void TextureReplacer::NotifyTextureDecoded(const ReplacedTextureDecodeInfo &replacedInfo, const void *data, int pitch, int level, int w, int h) {
|
2020-07-19 15:47:02 +00:00
|
|
|
_assert_msg_(enabled_, "Replacement not enabled");
|
2022-04-18 03:25:41 +00:00
|
|
|
if (!WillSave(replacedInfo)) {
|
2016-04-30 21:05:03 +00:00
|
|
|
// Ignore.
|
|
|
|
return;
|
|
|
|
}
|
2022-04-18 03:25:41 +00:00
|
|
|
if (ignoreMipmap_ && level > 0) {
|
2016-05-01 15:58:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-04-18 03:25:41 +00:00
|
|
|
|
2017-05-05 19:40:40 +00:00
|
|
|
u64 cachekey = replacedInfo.cachekey;
|
|
|
|
if (ignoreAddress_) {
|
|
|
|
cachekey = cachekey & 0xFFFFFFFFULL;
|
|
|
|
}
|
2016-04-30 21:05:03 +00:00
|
|
|
|
2021-04-03 15:56:28 +00:00
|
|
|
std::string hashfile = LookupHashFile(cachekey, replacedInfo.hash, level);
|
2021-05-05 23:31:38 +00:00
|
|
|
const Path filename = basePath_ / hashfile;
|
2016-04-30 22:03:39 +00:00
|
|
|
|
2016-04-30 22:41:12 +00:00
|
|
|
// If it's empty, it's an ignored hash, we intentionally don't save.
|
2022-04-18 03:25:41 +00:00
|
|
|
if (hashfile.empty()) {
|
2016-04-30 22:41:12 +00:00
|
|
|
// If it exists, must've been decoded and saved as a new texture already.
|
2016-04-30 22:03:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-05 19:40:40 +00:00
|
|
|
ReplacementCacheKey replacementKey(cachekey, replacedInfo.hash);
|
2016-05-01 03:21:08 +00:00
|
|
|
auto it = savedCache_.find(replacementKey);
|
2022-04-18 03:25:41 +00:00
|
|
|
bool skipIfExists = false;
|
2022-04-18 03:30:20 +00:00
|
|
|
double now = time_now_d();
|
2022-04-18 03:25:41 +00:00
|
|
|
if (it != savedCache_.end()) {
|
2016-05-01 03:21:08 +00:00
|
|
|
// We've already saved this texture. Let's only save if it's bigger (e.g. scaled now.)
|
2022-04-18 03:30:20 +00:00
|
|
|
if (it->second.first.w >= w && it->second.first.h >= h) {
|
|
|
|
// If it's been more than 5 seconds, we'll check again. Maybe they deleted.
|
|
|
|
double age = now - it->second.second;
|
|
|
|
if (age < 5.0)
|
|
|
|
return;
|
|
|
|
|
2022-04-18 03:25:41 +00:00
|
|
|
skipIfExists = true;
|
2016-05-31 05:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-30 23:33:09 +00:00
|
|
|
// Only save the hashed portion of the PNG.
|
2016-05-01 15:54:43 +00:00
|
|
|
int lookupW = w / replacedInfo.scaleFactor;
|
|
|
|
int lookupH = h / replacedInfo.scaleFactor;
|
|
|
|
if (LookupHashRange(replacedInfo.addr, lookupW, lookupH)) {
|
|
|
|
w = lookupW * replacedInfo.scaleFactor;
|
|
|
|
h = lookupH * replacedInfo.scaleFactor;
|
2016-05-01 03:33:14 +00:00
|
|
|
}
|
2016-04-30 23:33:09 +00:00
|
|
|
|
2022-04-17 22:25:59 +00:00
|
|
|
SimpleBuf<u32> saveBuf;
|
|
|
|
|
2022-07-29 16:27:52 +00:00
|
|
|
// Copy data to a buffer so we can send it to the thread. Might as well compact-away the pitch
|
|
|
|
// while we're at it.
|
|
|
|
saveBuf.resize(w * h);
|
|
|
|
for (int y = 0; y < h; y++) {
|
|
|
|
memcpy((u8 *)saveBuf.data() + y * w * 4, (const u8 *)data + y * pitch, w * sizeof(u32));
|
2016-04-30 22:03:39 +00:00
|
|
|
}
|
2022-07-29 16:27:52 +00:00
|
|
|
pitch = w * 4;
|
2016-04-30 22:03:39 +00:00
|
|
|
|
2022-04-17 22:25:59 +00:00
|
|
|
TextureSaveTask *task = new TextureSaveTask(std::move(saveBuf));
|
|
|
|
// Should probably do a proper move constructor but this'll work.
|
|
|
|
task->w = w;
|
|
|
|
task->h = h;
|
|
|
|
task->pitch = pitch;
|
2022-04-18 03:25:41 +00:00
|
|
|
task->basePath = basePath_;
|
|
|
|
task->hashfile = hashfile;
|
2022-04-17 22:25:59 +00:00
|
|
|
task->replacedInfoHash = replacedInfo.hash;
|
2022-04-18 03:25:41 +00:00
|
|
|
task->skipIfExists = skipIfExists;
|
2022-04-17 22:25:59 +00:00
|
|
|
g_threadManager.EnqueueTask(task); // We don't care about waiting for the task. It'll be fine.
|
2016-05-01 03:21:08 +00:00
|
|
|
|
|
|
|
// Remember that we've saved this for next time.
|
2022-04-17 22:25:59 +00:00
|
|
|
// Should be OK that the actual disk write may not be finished yet.
|
2016-05-01 03:21:08 +00:00
|
|
|
ReplacedTextureLevel saved;
|
2022-07-29 16:36:21 +00:00
|
|
|
saved.fmt = Draw::DataFormat::R8G8B8A8_UNORM;
|
2021-05-11 07:50:28 +00:00
|
|
|
saved.file = filename;
|
2016-05-01 03:21:08 +00:00
|
|
|
saved.w = w;
|
|
|
|
saved.h = h;
|
2022-04-18 03:30:20 +00:00
|
|
|
savedCache_[replacementKey] = std::make_pair(saved, now);
|
2016-04-30 20:44:31 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 03:27:56 +00:00
|
|
|
void TextureReplacer::Decimate(ReplacerDecimateMode mode) {
|
2021-10-17 16:16:54 +00:00
|
|
|
// Allow replacements to be cached for a long time, although they're large.
|
2022-07-28 03:27:56 +00:00
|
|
|
double age = 1800.0;
|
2022-10-30 15:15:37 +00:00
|
|
|
if (mode == ReplacerDecimateMode::FORCE_PRESSURE) {
|
2022-07-28 03:27:56 +00:00
|
|
|
age = 90.0;
|
2022-10-30 15:15:37 +00:00
|
|
|
} else if (mode == ReplacerDecimateMode::ALL) {
|
2022-07-28 03:27:56 +00:00
|
|
|
age = 0.0;
|
2022-10-30 15:15:37 +00:00
|
|
|
} 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;
|
|
|
|
}
|
2022-07-28 03:27:56 +00:00
|
|
|
|
2021-10-17 16:16:54 +00:00
|
|
|
const double threshold = time_now_d() - age;
|
|
|
|
for (auto &item : cache_) {
|
2022-10-30 17:44:33 +00:00
|
|
|
item.second.PurgeIfOlder(threshold);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t totalSize = 0;
|
|
|
|
for (auto &item : levelCache_) {
|
|
|
|
std::lock_guard<std::mutex> guard(item.second.lock);
|
|
|
|
totalSize += item.second.data.size();
|
2022-10-30 15:15:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2021-10-17 16:16:54 +00:00
|
|
|
}
|
2022-10-30 15:15:37 +00:00
|
|
|
lastTextureCacheSizeGB_ = totalSizeGB;
|
2021-10-17 16:16:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 01:16:16 +00:00
|
|
|
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);
|
|
|
|
if (alias != map.end())
|
|
|
|
return alias;
|
|
|
|
|
|
|
|
// Also check for a few more aliases with zeroed portions:
|
|
|
|
// Only clut hash (very dangerous in theory, in practice not more than missing "just" data hash)
|
|
|
|
key.cachekey = cachekey & 0xFFFFFFFFULL;
|
|
|
|
key.hash = 0;
|
|
|
|
alias = map.find(key);
|
|
|
|
if (alias != map.end())
|
|
|
|
return alias;
|
|
|
|
|
|
|
|
if (!ignoreAddress) {
|
|
|
|
// No data hash.
|
|
|
|
key.cachekey = cachekey;
|
2016-05-31 05:31:02 +00:00
|
|
|
key.hash = 0;
|
2021-02-28 01:16:16 +00:00
|
|
|
alias = map.find(key);
|
|
|
|
if (alias != map.end())
|
|
|
|
return alias;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No address.
|
|
|
|
key.cachekey = cachekey & 0xFFFFFFFFULL;
|
|
|
|
key.hash = hash;
|
|
|
|
alias = map.find(key);
|
|
|
|
if (alias != map.end())
|
|
|
|
return alias;
|
|
|
|
|
|
|
|
if (!ignoreAddress) {
|
|
|
|
// Address, but not clut hash (in case of garbage clut data.)
|
|
|
|
key.cachekey = cachekey & ~0xFFFFFFFFULL;
|
|
|
|
key.hash = hash;
|
|
|
|
alias = map.find(key);
|
|
|
|
if (alias != map.end())
|
|
|
|
return alias;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Anything with this data hash (a little dangerous.)
|
|
|
|
key.cachekey = 0;
|
|
|
|
key.hash = hash;
|
|
|
|
return map.find(key);
|
|
|
|
}
|
2016-05-31 05:31:02 +00:00
|
|
|
|
2021-02-28 01:16:16 +00:00
|
|
|
bool TextureReplacer::FindFiltering(u64 cachekey, u32 hash, TextureFiltering *forceFiltering) {
|
|
|
|
if (!Enabled() || !g_Config.bReplaceTextures) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-31 05:31:02 +00:00
|
|
|
|
2021-02-28 01:16:16 +00:00
|
|
|
ReplacementCacheKey replacementKey(cachekey, hash);
|
|
|
|
auto filter = LookupWildcard(filtering_, replacementKey, cachekey, hash, ignoreAddress_);
|
|
|
|
if (filter == filtering_.end()) {
|
|
|
|
// Allow a global wildcard.
|
|
|
|
replacementKey.cachekey = 0;
|
|
|
|
replacementKey.hash = 0;
|
|
|
|
filter = filtering_.find(replacementKey);
|
|
|
|
}
|
|
|
|
if (filter != filtering_.end()) {
|
|
|
|
*forceFiltering = filter->second;
|
|
|
|
return true;
|
2016-04-30 23:21:16 +00:00
|
|
|
}
|
2021-02-28 01:16:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-30 23:21:16 +00:00
|
|
|
|
2021-04-03 15:56:28 +00:00
|
|
|
std::string TextureReplacer::LookupHashFile(u64 cachekey, u32 hash, int level) {
|
2021-02-28 01:16:16 +00:00
|
|
|
ReplacementAliasKey key(cachekey, hash, level);
|
|
|
|
auto alias = LookupWildcard(aliases_, key, cachekey, hash, ignoreAddress_);
|
2016-05-31 05:31:02 +00:00
|
|
|
if (alias != aliases_.end()) {
|
2016-05-01 03:55:30 +00:00
|
|
|
// Note: this will be blank if explicitly ignored.
|
2016-05-31 05:31:02 +00:00
|
|
|
return alias->second;
|
2016-05-01 03:55:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 15:56:28 +00:00
|
|
|
return HashName(cachekey, hash, level) + ".png";
|
2016-04-30 22:41:12 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 15:56:28 +00:00
|
|
|
std::string TextureReplacer::HashName(u64 cachekey, u32 hash, int level) {
|
2016-04-30 22:41:12 +00:00
|
|
|
char hashname[16 + 8 + 1 + 11 + 1] = {};
|
|
|
|
if (level > 0) {
|
2016-05-01 01:39:30 +00:00
|
|
|
snprintf(hashname, sizeof(hashname), "%016llx%08x_%d", cachekey, hash, level);
|
2016-04-30 22:41:12 +00:00
|
|
|
} else {
|
2016-05-01 01:39:30 +00:00
|
|
|
snprintf(hashname, sizeof(hashname), "%016llx%08x", cachekey, hash);
|
2016-04-30 22:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hashname;
|
|
|
|
}
|
|
|
|
|
2016-04-30 21:19:23 +00:00
|
|
|
bool TextureReplacer::LookupHashRange(u32 addr, int &w, int &h) {
|
2017-11-25 20:34:52 +00:00
|
|
|
const u64 rangeKey = ((u64)addr << 32) | ((u64)w << 16) | h;
|
2016-04-30 23:21:16 +00:00
|
|
|
auto range = hashranges_.find(rangeKey);
|
|
|
|
if (range != hashranges_.end()) {
|
|
|
|
const WidthHeightPair &wh = range->second;
|
|
|
|
w = wh.first;
|
|
|
|
h = wh.second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-30 21:19:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-01 09:18:06 +00:00
|
|
|
float TextureReplacer::LookupReduceHashRange(int& w, int& h) {
|
|
|
|
const u64 reducerangeKey = ((u64)w << 16) | h;
|
|
|
|
auto range = reducehashranges_.find(reducerangeKey);
|
|
|
|
if (range != reducehashranges_.end()) {
|
|
|
|
float rhv = range->second;
|
|
|
|
return rhv;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return reduceHashGlobalValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 20:21:23 +00:00
|
|
|
class ReplacedTextureTask : public Task {
|
|
|
|
public:
|
2023-03-07 19:53:50 +00:00
|
|
|
ReplacedTextureTask(VFSBackend *vfs, ReplacedTexture &tex, LimitedWaitable *w) : vfs_(vfs), tex_(tex), waitable_(w) {}
|
2021-10-21 20:21:23 +00:00
|
|
|
|
2021-12-06 03:22:13 +00:00
|
|
|
TaskType Type() const override {
|
|
|
|
return TaskType::IO_BLOCKING;
|
|
|
|
}
|
|
|
|
|
2023-01-15 15:55:07 +00:00
|
|
|
TaskPriority Priority() const override {
|
|
|
|
return TaskPriority::NORMAL;
|
|
|
|
}
|
|
|
|
|
2021-10-21 20:21:23 +00:00
|
|
|
void Run() override {
|
2023-03-07 19:53:50 +00:00
|
|
|
tex_.Prepare(vfs_);
|
2021-10-21 20:21:23 +00:00
|
|
|
waitable_->Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-03-08 07:23:13 +00:00
|
|
|
VFSBackend *vfs_;
|
2021-10-21 20:21:23 +00:00
|
|
|
ReplacedTexture &tex_;
|
|
|
|
LimitedWaitable *waitable_;
|
|
|
|
};
|
|
|
|
|
2021-10-17 15:54:45 +00:00
|
|
|
bool ReplacedTexture::IsReady(double budget) {
|
2023-03-07 19:53:50 +00:00
|
|
|
_assert_(vfs_ != nullptr);
|
2021-10-17 16:16:54 +00:00
|
|
|
lastUsed_ = time_now_d();
|
2022-04-09 16:41:49 +00:00
|
|
|
if (threadWaitable_ && !threadWaitable_->WaitFor(budget)) {
|
|
|
|
return false;
|
2021-10-21 20:21:23 +00:00
|
|
|
}
|
2021-10-17 16:16:54 +00:00
|
|
|
|
2021-10-21 20:21:23 +00:00
|
|
|
// Loaded already, or not yet on a thread?
|
2022-10-30 15:54:52 +00:00
|
|
|
if (initDone_ && !levelData_.empty()) {
|
|
|
|
for (auto &l : levelData_)
|
|
|
|
l->lastUsed = lastUsed_;
|
2021-10-24 02:58:23 +00:00
|
|
|
return true;
|
2022-10-30 15:54:52 +00:00
|
|
|
}
|
2021-10-24 05:42:57 +00:00
|
|
|
// Let's not even start a new texture if we're already behind.
|
|
|
|
if (budget < 0.0)
|
2021-10-17 16:16:54 +00:00
|
|
|
return false;
|
2022-08-22 05:07:05 +00:00
|
|
|
if (!prepareDone_)
|
|
|
|
return false;
|
2021-10-17 16:16:54 +00:00
|
|
|
|
2021-10-24 03:56:19 +00:00
|
|
|
if (g_Config.bReplaceTexturesAllowLate) {
|
2022-07-28 03:29:24 +00:00
|
|
|
if (threadWaitable_)
|
|
|
|
delete threadWaitable_;
|
2021-10-24 03:56:19 +00:00
|
|
|
threadWaitable_ = new LimitedWaitable();
|
2023-03-07 19:53:50 +00:00
|
|
|
g_threadManager.EnqueueTask(new ReplacedTextureTask(vfs_, *this, threadWaitable_));
|
2021-10-21 20:21:23 +00:00
|
|
|
|
2021-10-24 05:42:57 +00:00
|
|
|
if (threadWaitable_->WaitFor(budget)) {
|
2021-10-24 03:56:19 +00:00
|
|
|
// If we finished all the levels, we're done.
|
2022-08-22 05:07:05 +00:00
|
|
|
return initDone_ && !levelData_.empty();
|
2021-10-24 03:56:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-03-07 19:53:50 +00:00
|
|
|
Prepare(vfs_);
|
2022-08-22 05:07:05 +00:00
|
|
|
_assert_(initDone_);
|
2021-10-24 03:56:19 +00:00
|
|
|
return true;
|
2021-10-17 16:16:54 +00:00
|
|
|
}
|
2021-10-21 20:21:23 +00:00
|
|
|
|
|
|
|
// Still pending on thread.
|
|
|
|
return false;
|
2021-10-17 16:16:54 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 19:53:50 +00:00
|
|
|
void ReplacedTexture::Prepare(VFSBackend *vfs) {
|
2022-04-09 16:41:49 +00:00
|
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
2023-03-07 19:53:50 +00:00
|
|
|
this->vfs_ = vfs;
|
2022-08-22 05:07:05 +00:00
|
|
|
if (cancelPrepare_) {
|
|
|
|
initDone_ = true;
|
2022-04-09 16:41:49 +00:00
|
|
|
return;
|
2022-08-22 05:07:05 +00:00
|
|
|
}
|
2022-04-09 16:41:49 +00:00
|
|
|
|
2022-08-22 05:07:05 +00:00
|
|
|
for (int i = 0; i < (int)levels_.size(); ++i) {
|
2021-10-24 03:56:19 +00:00
|
|
|
if (cancelPrepare_)
|
|
|
|
break;
|
|
|
|
PrepareData(i);
|
|
|
|
}
|
2022-04-09 16:41:49 +00:00
|
|
|
|
2022-08-22 05:07:05 +00:00
|
|
|
initDone_ = true;
|
2022-04-09 16:41:49 +00:00
|
|
|
if (!cancelPrepare_ && threadWaitable_)
|
|
|
|
threadWaitable_->Notify();
|
2021-10-24 03:56:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-17 16:16:54 +00:00
|
|
|
void ReplacedTexture::PrepareData(int level) {
|
2020-07-19 15:47:02 +00:00
|
|
|
_assert_msg_((size_t)level < levels_.size(), "Invalid miplevel");
|
2022-10-30 15:54:52 +00:00
|
|
|
_assert_msg_(levelData_[level] != nullptr, "Level cache not set for miplevel");
|
2016-04-30 21:05:03 +00:00
|
|
|
|
2022-10-30 17:39:28 +00:00
|
|
|
// We must lock around access to levelData_ in case two textures try to load it at once.
|
|
|
|
std::lock_guard<std::mutex> guard(levelData_[level]->lock);
|
|
|
|
|
2023-03-08 07:23:13 +00:00
|
|
|
const ReplacedTextureLevel &info = levels_[level];
|
2022-10-30 15:54:52 +00:00
|
|
|
std::vector<uint8_t> &out = levelData_[level]->data;
|
|
|
|
|
|
|
|
// Already populated from cache.
|
|
|
|
if (!out.empty())
|
|
|
|
return;
|
2016-05-01 01:39:30 +00:00
|
|
|
|
2022-10-30 17:34:25 +00:00
|
|
|
ReplacedImageType imageType;
|
|
|
|
|
2023-03-07 19:53:50 +00:00
|
|
|
File::FileInfo fileInfo;
|
|
|
|
if (!vfs_->GetFileInfo(info.fileRef, &fileInfo)) {
|
|
|
|
ERROR_LOG(G3D, "Failed to get file info for level");
|
|
|
|
return;
|
2021-10-08 13:37:03 +00:00
|
|
|
}
|
2023-03-07 19:53:50 +00:00
|
|
|
size_t fileSize = fileInfo.size;
|
|
|
|
|
|
|
|
VFSOpenFile *openFile = vfs_->OpenFileForRead(info.fileRef);
|
|
|
|
std::string magic;
|
|
|
|
imageType = Identify(vfs_, openFile, &magic);
|
2021-10-08 13:37:03 +00:00
|
|
|
|
2022-10-30 17:34:25 +00:00
|
|
|
auto cleanup = [&] {
|
2023-03-07 19:53:50 +00:00
|
|
|
vfs_->CloseFile(openFile);
|
2022-10-30 17:34:25 +00:00
|
|
|
};
|
|
|
|
|
2021-05-16 16:30:33 +00:00
|
|
|
if (imageType == ReplacedImageType::ZIM) {
|
2023-03-07 19:53:50 +00:00
|
|
|
std::unique_ptr<uint8_t[]> zim(new uint8_t[fileSize]);
|
2021-10-08 13:37:03 +00:00
|
|
|
if (!zim) {
|
|
|
|
ERROR_LOG(G3D, "Failed to allocate memory for texture replacement");
|
2022-10-30 17:34:25 +00:00
|
|
|
cleanup();
|
2021-10-17 16:35:11 +00:00
|
|
|
return;
|
2021-10-08 13:37:03 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 19:53:50 +00:00
|
|
|
if (vfs_->Read(openFile, &zim[0], fileSize) != fileSize) {
|
|
|
|
ERROR_LOG(G3D, "Could not load texture replacement: %s - failed to read ZIM", info.file.c_str());
|
|
|
|
cleanup();
|
|
|
|
return;
|
2021-09-13 15:13:14 +00:00
|
|
|
}
|
2021-05-16 16:30:33 +00:00
|
|
|
|
|
|
|
int w, h, f;
|
|
|
|
uint8_t *image;
|
2023-03-07 19:53:50 +00:00
|
|
|
if (LoadZIMPtr(&zim[0], fileSize, &w, &h, &f, &image)) {
|
2021-10-21 20:21:23 +00:00
|
|
|
if (w > info.w || h > info.h) {
|
2021-10-17 16:35:11 +00:00
|
|
|
ERROR_LOG(G3D, "Texture replacement changed since header read: %s", info.file.c_str());
|
2022-10-30 17:34:25 +00:00
|
|
|
cleanup();
|
2021-10-17 16:35:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-21 20:21:23 +00:00
|
|
|
out.resize(info.w * info.h * 4);
|
|
|
|
if (w == info.w) {
|
2021-10-24 02:58:23 +00:00
|
|
|
memcpy(&out[0], image, info.w * 4 * info.h);
|
2021-10-21 20:21:23 +00:00
|
|
|
} else {
|
2021-10-24 02:58:23 +00:00
|
|
|
for (int y = 0; y < h; ++y) {
|
|
|
|
memcpy(&out[info.w * 4 * y], image + w * 4 * y, w * 4);
|
|
|
|
}
|
2021-10-21 20:21:23 +00:00
|
|
|
}
|
2021-05-16 16:30:33 +00:00
|
|
|
free(image);
|
2016-09-11 18:03:41 +00:00
|
|
|
}
|
2016-05-01 07:20:58 +00:00
|
|
|
|
2022-04-15 10:34:50 +00:00
|
|
|
CheckAlphaResult res = CheckAlpha32Rect((u32 *)&out[0], info.w, w, h, 0xFF000000);
|
2016-09-11 18:03:41 +00:00
|
|
|
if (res == CHECKALPHA_ANY || level == 0) {
|
|
|
|
alphaStatus_ = ReplacedTextureAlpha(res);
|
|
|
|
}
|
2021-05-16 16:30:33 +00:00
|
|
|
} else if (imageType == ReplacedImageType::PNG) {
|
|
|
|
png_image png = {};
|
|
|
|
png.version = PNG_IMAGE_VERSION;
|
|
|
|
|
2022-10-30 17:34:25 +00:00
|
|
|
std::string pngdata;
|
2023-03-07 19:53:50 +00:00
|
|
|
pngdata.resize(fileSize);
|
|
|
|
vfs_->Read(openFile, &pngdata[0], fileSize);
|
|
|
|
if (!png_image_begin_read_from_memory(&png, &pngdata[0], pngdata.size())) {
|
|
|
|
ERROR_LOG(G3D, "Could not load texture replacement info: %s - %s (zip)", info.file.c_str(), png.message);
|
|
|
|
cleanup();
|
|
|
|
return;
|
2021-10-17 16:35:11 +00:00
|
|
|
}
|
2021-10-21 20:21:23 +00:00
|
|
|
if (png.width > (uint32_t)info.w || png.height > (uint32_t)info.h) {
|
2021-10-17 16:35:11 +00:00
|
|
|
ERROR_LOG(G3D, "Texture replacement changed since header read: %s", info.file.c_str());
|
2022-10-30 17:34:25 +00:00
|
|
|
cleanup();
|
2021-10-17 16:35:11 +00:00
|
|
|
return;
|
2021-05-16 16:30:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool checkedAlpha = false;
|
|
|
|
if ((png.format & PNG_FORMAT_FLAG_ALPHA) == 0) {
|
|
|
|
// Well, we know for sure it doesn't have alpha.
|
|
|
|
if (level == 0) {
|
|
|
|
alphaStatus_ = ReplacedTextureAlpha::FULL;
|
|
|
|
}
|
|
|
|
checkedAlpha = true;
|
|
|
|
}
|
|
|
|
png.format = PNG_FORMAT_RGBA;
|
|
|
|
|
2021-10-21 20:21:23 +00:00
|
|
|
out.resize(info.w * info.h * 4);
|
|
|
|
if (!png_image_finish_read(&png, nullptr, &out[0], info.w * 4, nullptr)) {
|
2021-05-16 16:30:33 +00:00
|
|
|
ERROR_LOG(G3D, "Could not load texture replacement: %s - %s", info.file.c_str(), png.message);
|
2022-10-30 17:34:25 +00:00
|
|
|
cleanup();
|
2021-10-17 16:35:11 +00:00
|
|
|
out.resize(0);
|
|
|
|
return;
|
2021-05-16 16:30:33 +00:00
|
|
|
}
|
|
|
|
png_image_free(&png);
|
|
|
|
|
|
|
|
if (!checkedAlpha) {
|
|
|
|
// This will only check the hashed bits.
|
2022-04-15 10:34:50 +00:00
|
|
|
CheckAlphaResult res = CheckAlpha32Rect((u32 *)&out[0], info.w, png.width, png.height, 0xFF000000);
|
2021-05-16 16:30:33 +00:00
|
|
|
if (res == CHECKALPHA_ANY || level == 0) {
|
|
|
|
alphaStatus_ = ReplacedTextureAlpha(res);
|
|
|
|
}
|
|
|
|
}
|
2016-05-01 06:58:58 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 17:34:25 +00:00
|
|
|
cleanup();
|
2021-10-17 16:35:11 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 17:44:33 +00:00
|
|
|
void ReplacedTexture::PurgeIfOlder(double t) {
|
2022-10-30 15:15:37 +00:00
|
|
|
if (threadWaitable_ && !threadWaitable_->WaitFor(0.0))
|
2022-10-30 17:44:33 +00:00
|
|
|
return;
|
|
|
|
if (lastUsed_ >= t)
|
|
|
|
return;
|
2022-10-30 15:15:37 +00:00
|
|
|
|
|
|
|
for (auto &l : levelData_) {
|
2022-10-30 17:44:33 +00:00
|
|
|
if (l->lastUsed < t) {
|
|
|
|
// We have to lock since multiple textures might reference this same data.
|
|
|
|
std::lock_guard<std::mutex> guard(l->lock);
|
|
|
|
l->data.clear();
|
|
|
|
// This means we have to reload. If we never purge any, there's no need.
|
|
|
|
initDone_ = false;
|
|
|
|
}
|
2021-10-17 16:35:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 15:06:55 +00:00
|
|
|
ReplacedTexture::~ReplacedTexture() {
|
|
|
|
if (threadWaitable_) {
|
|
|
|
cancelPrepare_ = true;
|
2022-04-09 16:41:49 +00:00
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
2021-10-23 15:06:55 +00:00
|
|
|
threadWaitable_->WaitAndRelease();
|
|
|
|
threadWaitable_ = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-17 16:35:11 +00:00
|
|
|
bool ReplacedTexture::Load(int level, void *out, int rowPitch) {
|
|
|
|
_assert_msg_((size_t)level < levels_.size(), "Invalid miplevel");
|
|
|
|
_assert_msg_(out != nullptr && rowPitch > 0, "Invalid out/pitch");
|
|
|
|
|
2022-08-22 05:07:05 +00:00
|
|
|
if (!initDone_)
|
|
|
|
return false;
|
2021-10-24 02:58:23 +00:00
|
|
|
if (levelData_.empty())
|
|
|
|
return false;
|
|
|
|
|
2022-10-30 15:54:52 +00:00
|
|
|
_assert_msg_(levelData_[level] != nullptr, "Level cache not set for miplevel");
|
|
|
|
|
2022-10-30 17:39:28 +00:00
|
|
|
// We probably could avoid this lock, but better to play it safe.
|
|
|
|
std::lock_guard<std::mutex> guard(levelData_[level]->lock);
|
|
|
|
|
2021-10-17 16:35:11 +00:00
|
|
|
const ReplacedTextureLevel &info = levels_[level];
|
2022-10-30 15:54:52 +00:00
|
|
|
const std::vector<uint8_t> &data = levelData_[level]->data;
|
2021-10-17 16:35:11 +00:00
|
|
|
|
|
|
|
if (data.empty())
|
|
|
|
return false;
|
2023-03-07 19:53:50 +00:00
|
|
|
|
2022-12-31 14:31:45 +00:00
|
|
|
if (rowPitch < info.w * 4) {
|
|
|
|
ERROR_LOG_REPORT(G3D, "Replacement rowPitch=%d, but w=%d (level=%d)", rowPitch, info.w * 4, level);
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-17 16:35:11 +00:00
|
|
|
_assert_msg_(data.size() == info.w * info.h * 4, "Data has wrong size");
|
|
|
|
|
|
|
|
if (rowPitch == info.w * 4) {
|
|
|
|
ParallelMemcpy(&g_threadManager, out, &data[0], info.w * 4 * info.h);
|
|
|
|
} else {
|
|
|
|
const int MIN_LINES_PER_THREAD = 4;
|
|
|
|
ParallelRangeLoop(&g_threadManager, [&](int l, int h) {
|
|
|
|
for (int y = l; y < h; ++y) {
|
|
|
|
memcpy((uint8_t *)out + rowPitch * y, &data[0] + info.w * 4 * y, info.w * 4);
|
|
|
|
}
|
|
|
|
}, 0, info.h, MIN_LINES_PER_THREAD);
|
|
|
|
}
|
|
|
|
|
2021-10-08 13:37:03 +00:00
|
|
|
return true;
|
2016-04-30 20:44:31 +00:00
|
|
|
}
|
2018-10-01 00:00:05 +00:00
|
|
|
|
2022-07-10 20:34:44 +00:00
|
|
|
bool TextureReplacer::IniExists(const std::string &gameID) {
|
|
|
|
if (gameID.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Path texturesDirectory = GetSysDirectory(DIRECTORY_TEXTURES) / gameID;
|
|
|
|
Path generatedFilename = texturesDirectory / INI_FILENAME;
|
|
|
|
return File::Exists(generatedFilename);
|
|
|
|
}
|
|
|
|
|
2021-05-11 07:50:28 +00:00
|
|
|
bool TextureReplacer::GenerateIni(const std::string &gameID, Path &generatedFilename) {
|
2018-10-01 00:00:05 +00:00
|
|
|
if (gameID.empty())
|
|
|
|
return false;
|
|
|
|
|
2021-05-15 05:48:04 +00:00
|
|
|
Path texturesDirectory = GetSysDirectory(DIRECTORY_TEXTURES) / gameID;
|
2018-10-01 00:00:05 +00:00
|
|
|
if (!File::Exists(texturesDirectory)) {
|
|
|
|
File::CreateFullPath(texturesDirectory);
|
|
|
|
}
|
|
|
|
|
2021-05-11 07:50:28 +00:00
|
|
|
generatedFilename = texturesDirectory / INI_FILENAME;
|
|
|
|
if (File::Exists(generatedFilename))
|
2018-10-01 00:00:05 +00:00
|
|
|
return true;
|
|
|
|
|
2021-05-11 07:50:28 +00:00
|
|
|
FILE *f = File::OpenCFile(generatedFilename, "wb");
|
2018-10-01 00:00:05 +00:00
|
|
|
if (f) {
|
2021-05-09 22:48:36 +00:00
|
|
|
fwrite("\xEF\xBB\xBF", 1, 3, f);
|
2018-10-01 00:00:05 +00:00
|
|
|
|
|
|
|
// Let's also write some defaults.
|
2021-05-09 22:48:36 +00:00
|
|
|
fprintf(f, "# This file is optional and describes your textures.\n");
|
|
|
|
fprintf(f, "# Some information on syntax available here:\n");
|
|
|
|
fprintf(f, "# https://github.com/hrydgard/ppsspp/wiki/Texture-replacement-ini-syntax \n");
|
|
|
|
fprintf(f, "[options]\n");
|
|
|
|
fprintf(f, "version = 1\n");
|
|
|
|
fprintf(f, "hash = quick\n");
|
|
|
|
fprintf(f, "ignoreMipmap = false\n");
|
|
|
|
fprintf(f, "\n");
|
|
|
|
fprintf(f, "[games]\n");
|
|
|
|
fprintf(f, "# Used to make it easier to install, and override settings for other regions.\n");
|
|
|
|
fprintf(f, "# Files still have to be copied to each TEXTURES folder.");
|
|
|
|
fprintf(f, "%s = %s\n", gameID.c_str(), INI_FILENAME.c_str());
|
|
|
|
fprintf(f, "\n");
|
|
|
|
fprintf(f, "[hashes]\n");
|
|
|
|
fprintf(f, "# Use / for folders not \\, avoid special characters, and stick to lowercase.\n");
|
|
|
|
fprintf(f, "# See wiki for more info.\n");
|
|
|
|
fprintf(f, "\n");
|
|
|
|
fprintf(f, "[hashranges]\n");
|
|
|
|
fprintf(f, "\n");
|
|
|
|
fprintf(f, "[filtering]\n");
|
|
|
|
fprintf(f, "\n");
|
|
|
|
fprintf(f, "[reducehashranges]\n");
|
|
|
|
fclose(f);
|
2018-10-01 00:00:05 +00:00
|
|
|
}
|
2021-05-11 07:50:28 +00:00
|
|
|
return File::Exists(generatedFilename);
|
2018-10-01 00:00:05 +00:00
|
|
|
}
|