#include "ppsspp_config.h"
#include <cstring>
#include <memory>
#include <png.h>
#include "ext/basis_universal/basisu_transcoder.h"
#include "ext/xxhash.h"
#include "Common/Data/Format/IniFile.h"
#include "Common/Data/Text/I18n.h"
#include "Common/Data/Text/Parsers.h"
#include "Common/File/VFS/DirectoryReader.h"
#include "Common/File/VFS/ZipFileReader.h"
#include "Common/File/FileUtil.h"
#include "Common/File/VFS/VFS.h"
#include "Common/StringUtils.h"
#include "Common/System/OSD.h"
#include "Common/Thread/ThreadManager.h"
#include "Common/TimeUtil.h"
#include "Core/Config.h"
#include "Core/System.h"
#include "Core/ELF/ParamSFO.h"
#include "GPU/Common/TextureReplacer.h"
#include "GPU/Common/TextureDecoder.h"
static const std::string INI_FILENAME = "textures.ini";
static const std::string ZIP_FILENAME = "textures.zip";
static const std::string NEW_TEXTURE_DIR = "new/";
static const int VERSION = 1;
static const double MAX_CACHE_SIZE = 4.0;
static bool basisu_initialized = false;
TextureReplacer::TextureReplacer(Draw::DrawContext *draw) {
if (!basisu_initialized) {
basist::basisu_transcoder_init();
basisu_initialized = true;
}
if (draw->GetDataFormatSupport(Draw::DataFormat::BC3_UNORM_BLOCK)) formatSupport_.bc123 = true;
if (draw->GetDataFormatSupport(Draw::DataFormat::ASTC_4x4_UNORM_BLOCK)) formatSupport_.astc = true;
if (draw->GetDataFormatSupport(Draw::DataFormat::BC7_UNORM_BLOCK)) formatSupport_.bc7 = true;
if (draw->GetDataFormatSupport(Draw::DataFormat::ETC2_R8G8B8_UNORM_BLOCK)) formatSupport_.etc2 = true;
}
TextureReplacer::~TextureReplacer() {
for (auto iter : levelCache_) {
delete iter.second;
}
delete vfs_;
}
void TextureReplacer::NotifyConfigChanged() {
gameID_ = g_paramSFO.GetDiscID();
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 (saveEnabled_ && !File::Exists(newTextureDir_)) {
INFO_LOG(Log::TexReplacement, "Creating new texture directory: '%s'", newTextureDir_.ToVisualString().c_str());
File::CreateFullPath(newTextureDir_);
}
}
if (!replaceEnabled_ && wasReplaceEnabled) {
delete vfs_;
vfs_ = nullptr;
Decimate(ReplacerDecimateMode::ALL);
} else if (!wasReplaceEnabled && replaceEnabled_) {
std::string error;
replaceEnabled_ = LoadIni(&error);
if (!error.empty() && !replaceEnabled_) {
ERROR_LOG(Log::G3D, "ERROR: %s", error.c_str());
g_OSD.Show(OSDType::MESSAGE_ERROR, error, 5.0f);
}
} else if (saveEnabled_) {
std::string error;
bool result = LoadIni(&error, false);
if (!result) {
} else {
INFO_LOG(Log::G3D, "Loaded INI file for saving.");
}
}
}
bool TextureReplacer::LoadIni(std::string *error, bool notify) {
hash_ = ReplacedTextureHash::QUICK;
aliases_.clear();
hashranges_.clear();
filtering_.clear();
reducehashranges_.clear();
allowVideo_ = false;
ignoreAddress_ = false;
reduceHash_ = false;
reduceHashGlobalValue = 0.5;
ignoreMipmap_ = false;
delete vfs_;
vfs_ = nullptr;
Path zipPath = basePath_ / ZIP_FILENAME;
VFSBackend *dir = ZipFileReader::Create(zipPath, "", false);
if (!dir) {
INFO_LOG(Log::TexReplacement, "%s wasn't a zip file - opening the directory %s instead.", zipPath.c_str(), basePath_.c_str());
vfsIsZip_ = false;
dir = new DirectoryReader(basePath_);
} else {
if (!replaceEnabled_ && saveEnabled_) {
WARN_LOG(Log::TexReplacement, "Found zip file even though only saving is enabled! This is weird.");
}
vfsIsZip_ = true;
}
IniFile ini;
bool iniLoaded = ini.LoadFromVFS(*dir, INI_FILENAME);
if (iniLoaded) {
if (!LoadIniValues(ini, dir, false, error)) {
delete dir;
return false;
}
std::string overrideFilename;
if (ini.GetOrCreateSection("games")->Get(gameID_.c_str(), &overrideFilename, "")) {
if (overrideFilename == "true") {
} else if (!overrideFilename.empty() && overrideFilename != INI_FILENAME) {
IniFile overrideIni;
iniLoaded = overrideIni.LoadFromVFS(*dir, overrideFilename);
if (!iniLoaded) {
*error = "Loading override ini failed: '" + overrideFilename + "'";
ERROR_LOG(Log::TexReplacement, "Failed to load extra texture ini: '%s'", overrideFilename.c_str());
delete dir;
return false;
}
INFO_LOG(Log::TexReplacement, "Loading extra texture ini: %s", overrideFilename.c_str());
if (!LoadIniValues(overrideIni, nullptr, true, error)) {
*error = "Override: " + *error;
delete dir;
return false;
}
}
}
} else {
if (vfsIsZip_) {
ERROR_LOG(Log::TexReplacement, "Texture pack lacking ini file: %s", basePath_.c_str());
*error = "Zip files without ini files will not load";
delete dir;
return false;
} else {
if (replaceEnabled_) {
WARN_LOG(Log::TexReplacement, "Texture pack lacking ini file: %s Proceeding with only hash-named textures in the root.", basePath_.c_str());
}
std::map<ReplacementCacheKey, std::map<int, std::string>> filenameMap;
ScanForHashNamedFiles(dir, filenameMap);
if (filenameMap.empty()) {
WARN_LOG(Log::TexReplacement, "No replacement textures found.");
return false;
}
ComputeAliasMap(filenameMap);
}
}
auto gr = GetI18NCategory(I18NCat::GRAPHICS);
if (replaceEnabled_ && notify) {
g_OSD.Show(OSDType::MESSAGE_SUCCESS, gr->T("Texture replacement pack activated"), 3.0f);
}
vfs_ = dir;
for (auto &repl : levelCache_) {
repl.second->vfs_ = vfs_;
}
if (replaceEnabled_) {
if (vfsIsZip_) {
INFO_LOG(Log::TexReplacement, "Texture pack activated from '%s'", (basePath_ / ZIP_FILENAME).c_str());
} else {
INFO_LOG(Log::TexReplacement, "Texture pack activated from '%s'", basePath_.c_str());
}
}
return true;
}
void TextureReplacer::ScanForHashNamedFiles(VFSBackend *dir, std::map<ReplacementCacheKey, std::map<int, std::string>> &filenameMap) {
std::vector<File::FileInfo> filesInRoot;
dir->GetFileListing("", &filesInRoot, nullptr);
for (auto file : filesInRoot) {
if (file.isDirectory)
continue;
if (file.name.empty() || file.name[0] == '.')
continue;
Path path(file.name);
std::string ext = path.GetFileExtension();
std::string hash = file.name.substr(0, file.name.size() - ext.size());
if (!((hash.size() >= 26 && hash.size() <= 27 && hash[24] == '_') || hash.size() == 24)) {
continue;
}
if (equalsNoCase(ext, ".ktx2") || equalsNoCase(ext, ".png") || equalsNoCase(ext, ".dds") || equalsNoCase(ext, ".zim")) {
ReplacementCacheKey key(0, 0);
int level = 0;
if (sscanf(hash.c_str(), "%16llx%8x_%d", &key.cachekey, &key.hash, &level) >= 1) {
filenameMap[key][level] = file.name;
}
}
}
}
void TextureReplacer::ComputeAliasMap(const std::map<ReplacementCacheKey, std::map<int, std::string>> &filenameMap) {
for (auto &pair : filenameMap) {
std::string alias;
int mipIndex = 0;
for (auto &level : pair.second) {
if (level.first == mipIndex) {
alias += level.second + "|";
mipIndex++;
} else {
WARN_LOG(Log::TexReplacement, "Non-sequential mip index %d, breaking. filenames=%s", level.first, level.second.c_str());
break;
}
}
if (alias == "|") {
alias.clear();
}
for (auto &c : alias) {
if (c == '\\') {
c = '/';
}
}
aliases_[pair.first] = alias;
}
}
bool TextureReplacer::LoadIniValues(IniFile &ini, VFSBackend *dir, bool isOverride, std::string *error) {
INFO_LOG(Log::G3D, "Loading ini values...");
auto options = ini.GetOrCreateSection("options");
std::string hash;
if (!options->Get("hash", &hash, "")) {
*error = "textures.ini: Hash type not specified";
return false;
}
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 = "textures.ini: Unsupported hash type: " + hash;
return false;
}
options->Get("video", &allowVideo_, allowVideo_);
options->Get("ignoreAddress", &ignoreAddress_, ignoreAddress_);
options->Get("reduceHash", &reduceHash_, reduceHash_);
options->Get("ignoreMipmap", &ignoreMipmap_, ignoreMipmap_);
options->Get("skipLastDXT1Blocks128x64", &skipLastDXT1Blocks128x64_, skipLastDXT1Blocks128x64_);
if (reduceHash_ && hash_ == ReplacedTextureHash::QUICK) {
reduceHash_ = false;
ERROR_LOG(Log::TexReplacement, "Texture Replacement: reduceHash option requires safer hash, use xxh32 or xxh64 instead.");
}
if (ignoreAddress_ && hash_ == ReplacedTextureHash::QUICK) {
ignoreAddress_ = false;
ERROR_LOG(Log::TexReplacement, "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(Log::TexReplacement, "Unsupported texture replacement version %d, trying anyway", version);
}
int badFileNameCount = 0;
std::map<ReplacementCacheKey, std::map<int, std::string>> filenameMap;
if (dir) {
ScanForHashNamedFiles(dir, filenameMap);
}
std::string badFilenames;
if (ini.HasSection("hashes")) {
const Section *hashesSection = ini.GetOrCreateSection("hashes");
bool checkFilenames = saveEnabled_ && !g_Config.bIgnoreTextureFilenames && !vfsIsZip_;
for (const auto &line : hashesSection->Lines()) {
if (line.Key().empty())
continue;
ReplacementCacheKey key(0, 0);
int level = 0;
char k[128];
truncate_cpy(k, line.Key());
std::string_view v = line.Value();
if (sscanf(k, "%16llx%8x_%d", &key.cachekey, &key.hash, &level) >= 1) {
filenameMap[key][level] = v;
if (checkFilenames) {
#if PPSSPP_PLATFORM(WINDOWS)
bool bad = v.find_first_of("\\ABCDEFGHIJKLMNOPQRSTUVWXYZ:<>|?*") != std::string::npos;
#else
bool bad = v.find_first_of("\\:<>|?*") != std::string::npos;
#endif
if (bad) {
badFileNameCount++;
if (badFileNameCount == 10) {
badFilenames.append("...");
} else if (badFileNameCount < 10) {
badFilenames.append(v);
badFilenames.push_back('\n');
}
}
}
} else {
ERROR_LOG(Log::TexReplacement, "Unsupported syntax under [hashes], ignoring: %s = ", k);
}
}
}
ComputeAliasMap(filenameMap);
if (badFileNameCount > 0) {
auto err = GetI18NCategory(I18NCat::ERRORS);
g_OSD.Show(OSDType::MESSAGE_WARNING, err->T("textures.ini filenames may not be cross - platform(banned characters)"), badFilenames, 6.0f);
WARN_LOG(Log::TexReplacement, "Potentially bad filenames: %s", badFilenames.c_str());
}
if (ini.HasSection("hashranges")) {
auto hashranges = ini.GetOrCreateSection("hashranges")->ToMap();
for (const auto &[k, v] : hashranges) {
ParseHashRange(k, v);
}
}
if (ini.HasSection("filtering")) {
auto filters = ini.GetOrCreateSection("filtering")->ToMap();
for (const auto &[k, v] : filters) {
ParseFiltering(k, v);
}
}
if (ini.HasSection("reducehashranges")) {
auto reducehashranges = ini.GetOrCreateSection("reducehashranges")->ToMap();
for (const auto &[k, v] : reducehashranges) {
ParseReduceHashRange(k, v);
}
}
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(Log::TexReplacement, "Ignoring invalid hashrange %s = %s, expecting addr,w,h = w,h", key.c_str(), value.c_str());
return;
}
if (!startsWith(keyParts[0], "0x") && !startsWith(keyParts[0], "0X")) {
keyParts[0] = "0x" + keyParts[0];
}
u32 addr;
u32 fromW;
u32 fromH;
if (!TryParse(keyParts[0], &addr) || !TryParse(keyParts[1], &fromW) || !TryParse(keyParts[2], &fromH)) {
ERROR_LOG(Log::TexReplacement, "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(Log::TexReplacement, "Ignoring invalid hashrange %s = %s, value format is 512,512", key.c_str(), value.c_str());
return;
}
if (toW > fromW || toH > fromH) {
ERROR_LOG(Log::TexReplacement, "Ignoring invalid hashrange %s = %s, range bigger than source", key.c_str(), value.c_str());
return;
}
const u64 rangeKey = ((u64)addr << 32) | ((u64)fromW << 16) | fromH;
hashranges_[rangeKey] = WidthHeightPair(toW, toH);
}
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(Log::TexReplacement, "Unsupported syntax under [filtering]: %s", value.c_str());
}
} else {
ERROR_LOG(Log::TexReplacement, "Unsupported syntax under [filtering]: %s", key.c_str());
}
}
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(Log::TexReplacement, "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(Log::TexReplacement, "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(Log::TexReplacement, "Ignoring invalid reducehashrange %s = %s, value format is 0.5", key.c_str(), value.c_str());
return;
}
if (rhashvalue == 0) {
ERROR_LOG(Log::TexReplacement, "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;
}
u32 TextureReplacer::ComputeHash(u32 addr, int bufw, int w, int h, bool swizzled, GETextureFormat fmt, u16 maxSeenV) {
_dbg_assert_msg_(replaceEnabled_ || saveEnabled_, "Replacement not enabled");
if (!LookupHashRange(addr, w, h, &w, &h)) {
if (h == 512 && maxSeenV < 512 && maxSeenV != 0) {
h = (int)maxSeenV;
}
}
const u8 *checkp = Memory::GetPointerUnchecked(addr);
float reduceHashSize = 1.0f;
if (reduceHash_) {
reduceHashSize = LookupReduceHashRange(w, h);
}
if (bufw <= w) {
const u32 totalPixels = bufw * h + (w - bufw);
u32 sizeInRAM = (textureBitsPerPixel[fmt] * totalPixels) / 8 * reduceHashSize;
if (Memory::MaxSizeAtAddress(addr) < sizeInRAM) {
ERROR_LOG(Log::G3D, "Can't hash a %d bytes textures at %08x - end point is outside memory", sizeInRAM, addr);
return 0;
}
if (skipLastDXT1Blocks128x64_ && fmt == GE_TFMT_DXT1 && w == 128 && h == 64) {
sizeInRAM -= 8 * skipLastDXT1Blocks128x64_;
}
switch (hash_) {
case ReplacedTextureHash::QUICK:
return StableQuickTexHash(checkp, sizeInRAM);
case ReplacedTextureHash::XXH32:
return XXH32(checkp, sizeInRAM, 0xBACD7814);
case ReplacedTextureHash::XXH64:
return XXH64(checkp, sizeInRAM, 0xBACD7814);
default:
return 0;
}
} else {
const u32 bytesPerLine = (textureBitsPerPixel[fmt] * w) / 8 * reduceHashSize;
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;
case ReplacedTextureHash::XXH32:
for (int y = 0; y < h; ++y) {
u32 rowHash = XXH32(checkp, bytesPerLine, 0xBACD7814);
result = (result * 11) ^ rowHash;
checkp += stride;
}
break;
case ReplacedTextureHash::XXH64:
for (int y = 0; y < h; ++y) {
u32 rowHash = XXH64(checkp, bytesPerLine, 0xBACD7814);
result = (result * 11) ^ rowHash;
checkp += stride;
}
break;
default:
break;
}
return result;
}
}
ReplacedTexture *TextureReplacer::FindReplacement(u64 cachekey, u32 hash, int w, int h) {
if (!Enabled() || !g_Config.bReplaceTextures) {
return nullptr;
}
ReplacementCacheKey replacementKey(cachekey, hash);
auto it = cache_.find(replacementKey);
if (it != cache_.end()) {
return it->second.texture;
}
ReplacementDesc desc;
desc.newW = w;
desc.newH = h;
desc.w = w;
desc.h = h;
desc.cachekey = cachekey;
desc.hash = hash;
LookupHashRange(cachekey >> 32, w, h, &desc.newW, &desc.newH);
if (ignoreAddress_) {
cachekey = cachekey & 0xFFFFFFFFULL;
}
bool foundAlias = false;
bool ignored = false;
std::string hashfiles = LookupHashFile(cachekey, hash, &foundAlias, &ignored);
if (ignored) {
ReplacedTextureRef ref{};
cache_.emplace(std::make_pair(replacementKey, ref));
return nullptr;
}
desc.forceFiltering = (TextureFiltering)0;
FindFiltering(cachekey, hash, &desc.forceFiltering);
if (!foundAlias) {
desc.filenames.resize(MAX_REPLACEMENT_MIP_LEVELS);
for (int level = 0; level < desc.filenames.size(); level++) {
desc.filenames[level] = TextureReplacer::HashName(cachekey, hash, level) + ".png";
}
desc.logId = desc.filenames[0];
desc.hashfiles = desc.filenames[0];
hashfiles.clear();
hashfiles.reserve(desc.filenames[0].size() * (desc.filenames.size() + 1));
for (int level = 0; level < desc.filenames.size(); level++) {
hashfiles += desc.filenames[level];
hashfiles.push_back('|');
}
} else {
desc.logId = hashfiles;
SplitString(hashfiles, '|', desc.filenames);
desc.hashfiles = hashfiles;
}
_dbg_assert_(!hashfiles.empty());
auto iter = levelCache_.find(hashfiles);
if (iter != levelCache_.end()) {
ReplacedTextureRef ref;
ref.hashfiles = hashfiles;
ref.texture = iter->second;
cache_.emplace(std::make_pair(replacementKey, ref));
return iter->second;
}
desc.basePath = basePath_;
desc.formatSupport = formatSupport_;
ReplacedTexture *texture = new ReplacedTexture(vfs_, desc);
ReplacedTextureRef ref;
ref.hashfiles = hashfiles;
ref.texture = texture;
cache_.emplace(std::make_pair(replacementKey, ref));
levelCache_.emplace(std::make_pair(hashfiles, texture));
return texture;
}
static bool WriteTextureToPNG(png_imagep image, const Path &filename, int convert_to_8bit, const void *buffer, png_int_32 row_stride, const void *colormap) {
FILE *fp = File::OpenCFile(filename, "wb");
if (!fp) {
ERROR_LOG(Log::TexReplacement, "Save texture: Unable to open texture file '%s' for writing.", filename.c_str());
return false;
}
if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer, row_stride, colormap)) {
fclose(fp);
return true;
} else {
ERROR_LOG(Log::TexReplacement, "Texture PNG encode failed.");
fclose(fp);
remove(filename.c_str());
return false;
}
}
class SaveTextureTask : public Task {
public:
u8 *rgbaData = nullptr;
int w = 0;
int h = 0;
Path filename;
Path saveFilename;
u32 replacedInfoHash = 0;
SaveTextureTask(u8 *_rgbaData) : rgbaData(_rgbaData) {}
~SaveTextureTask() {
free(rgbaData);
}
TaskType Type() const override { return TaskType::IO_BLOCKING; }
TaskPriority Priority() const override {
return TaskPriority::LOW;
}
void Run() override {
if (File::Exists(saveFilename)) {
return;
}
if (File::Exists(filename)) {
return;
}
Path saveDirectory = saveFilename.NavigateUp();
if (!File::Exists(saveDirectory)) {
File::CreateFullPath(saveDirectory);
}
saveFilename = saveFilename.WithReplacedExtension(".png");
png_image png{};
png.version = PNG_IMAGE_VERSION;
png.format = PNG_FORMAT_RGBA;
png.width = w;
png.height = h;
bool success = WriteTextureToPNG(&png, saveFilename, 0, rgbaData, w * 4, nullptr);
png_image_free(&png);
if (png.warning_or_error >= 2) {
ERROR_LOG(Log::TexReplacement, "Saving texture to PNG produced errors.");
} else if (success) {
NOTICE_LOG(Log::TexReplacement, "Saving texture for replacement: %08x / %dx%d in '%s'", replacedInfoHash, w, h, saveFilename.ToVisualString().c_str());
} else {
ERROR_LOG(Log::TexReplacement, "Failed to write '%s'", saveFilename.c_str());
}
}
};
bool TextureReplacer::WillSave(const ReplacedTextureDecodeInfo &replacedInfo) const {
if (!saveEnabled_)
return false;
if (replacedInfo.addr > 0x05000000 && replacedInfo.addr < PSP_GetKernelMemoryEnd())
return false;
if (replacedInfo.isVideo && !allowVideo_)
return false;
return true;
}
void TextureReplacer::NotifyTextureDecoded(ReplacedTexture *texture, const ReplacedTextureDecodeInfo &replacedInfo, const void *data, int srcPitch, int level, int origW, int origH, int scaledW, int scaledH) {
_assert_msg_(saveEnabled_, "Texture saving not enabled");
_assert_(srcPitch >= 0);
_assert_(data);
_assert_(level >= 0);
if (!WillSave(replacedInfo)) {
return;
}
if (ignoreMipmap_ && level > 0) {
return;
}
u64 cachekey = replacedInfo.cachekey;
if (ignoreAddress_) {
cachekey = cachekey & 0xFFFFFFFFULL;
}
bool foundAlias = false;
bool ignored = false;
std::string replacedLevelNames = LookupHashFile(cachekey, replacedInfo.hash, &foundAlias, &ignored);
if (ignored) {
return;
}
std::string hashfile;
if (!replacedLevelNames.empty()) {
std::vector<std::string> names;
SplitString(replacedLevelNames, '|', names);
hashfile = names[std::min(level, (int)(names.size() - 1))];
} else {
hashfile = HashName(cachekey, replacedInfo.hash, level) + ".png";
}
ReplacementCacheKey replacementKey(cachekey, replacedInfo.hash);
auto it = savedCache_.find(replacementKey);
if (it != savedCache_.end()) {
return;
}
double now = time_now_d();
int w = scaledW;
int h = scaledH;
if (w == 0 || h == 0) {
return;
}
int lookupW;
int lookupH;
if (LookupHashRange(replacedInfo.addr, origW, origH, &lookupW, &lookupH)) {
w = lookupW * (scaledW / origW);
h = lookupH * (scaledH / origH);
}
size_t saveBufSize = w * h * 4;
u8 *saveBuf = (u8 *)malloc(saveBufSize);
if (!saveBuf) {
ERROR_LOG(Log::TexReplacement, "Failed to allocated %d bytes of memory for saving a texture", (int)saveBufSize);
return;
}
for (int y = 0; y < h; y++) {
memcpy(saveBuf + y * w * 4, (const u8 *)data + y * srcPitch, w * 4);
}
SaveTextureTask *task = new SaveTextureTask(std::move(saveBuf));
task->filename = basePath_ / hashfile;
task->saveFilename = newTextureDir_ / hashfile;
task->w = w;
task->h = h;
task->replacedInfoHash = replacedInfo.hash;
g_threadManager.EnqueueTask(task);
SavedTextureCacheData &saveData = savedCache_[replacementKey];
saveData.levelW[level] = w;
saveData.levelH[level] = h;
saveData.levelSaved[level] = true;
saveData.lastTimeSaved = now;
}
void TextureReplacer::Decimate(ReplacerDecimateMode mode) {
double age = 1800.0;
if (mode == ReplacerDecimateMode::FORCE_PRESSURE) {
age = 90.0;
} else if (mode == ReplacerDecimateMode::ALL) {
age = 0.0;
} else if (lastTextureCacheSizeGB_ > 1.0) {
double pressure = std::min(MAX_CACHE_SIZE, lastTextureCacheSizeGB_) / MAX_CACHE_SIZE;
age = 90.0 + (1.0 - pressure) * 1710.0;
}
const double threshold = time_now_d() - age;
size_t totalSize = 0;
for (auto &item : levelCache_) {
if (item.second->lock_.try_lock()) {
item.second->PurgeIfNotUsedSinceTime(threshold);
totalSize += item.second->GetTotalDataSize();
item.second->lock_.unlock();
}
}
double totalSizeGB = totalSize / (1024.0 * 1024.0 * 1024.0);
if (totalSizeGB >= 1.0) {
WARN_LOG(Log::TexReplacement, "Decimated replacements older than %fs, currently using %f GB of RAM", age, totalSizeGB);
}
lastTextureCacheSizeGB_ = totalSizeGB;
}
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;
key.cachekey = cachekey & 0xFFFFFFFFULL;
key.hash = 0;
alias = map.find(key);
if (alias != map.end())
return alias;
if (!ignoreAddress) {
key.cachekey = cachekey;
key.hash = 0;
alias = map.find(key);
if (alias != map.end())
return alias;
}
key.cachekey = cachekey & 0xFFFFFFFFULL;
key.hash = hash;
alias = map.find(key);
if (alias != map.end())
return alias;
if (!ignoreAddress) {
key.cachekey = cachekey & ~0xFFFFFFFFULL;
key.hash = hash;
alias = map.find(key);
if (alias != map.end())
return alias;
}
key.cachekey = 0;
key.hash = hash;
return map.find(key);
}
bool TextureReplacer::FindFiltering(u64 cachekey, u32 hash, TextureFiltering *forceFiltering) {
if (!Enabled() || !g_Config.bReplaceTextures) {
return false;
}
ReplacementCacheKey replacementKey(cachekey, hash);
auto filter = LookupWildcard(filtering_, replacementKey, cachekey, hash, ignoreAddress_);
if (filter == filtering_.end()) {
replacementKey.cachekey = 0;
replacementKey.hash = 0;
filter = filtering_.find(replacementKey);
}
if (filter != filtering_.end()) {
*forceFiltering = filter->second;
return true;
}
return false;
}
std::string TextureReplacer::LookupHashFile(u64 cachekey, u32 hash, bool *foundAlias, bool *ignored) {
ReplacementCacheKey key(cachekey, hash);
auto alias = LookupWildcard(aliases_, key, cachekey, hash, ignoreAddress_);
if (alias != aliases_.end()) {
*foundAlias = true;
*ignored = alias->second.empty();
return alias->second;
}
*foundAlias = false;
*ignored = false;
return "";
}
std::string TextureReplacer::HashName(u64 cachekey, u32 hash, int level) {
char hashname[16 + 8 + 1 + 11 + 1] = {};
if (level > 0) {
snprintf(hashname, sizeof(hashname), "%016llx%08x_%d", cachekey, hash, level);
} else {
snprintf(hashname, sizeof(hashname), "%016llx%08x", cachekey, hash);
}
return hashname;
}
bool TextureReplacer::LookupHashRange(u32 addr, int w, int h, int *newW, int *newH) {
const u64 rangeKey = ((u64)addr << 32) | ((u64)w << 16) | h;
auto range = hashranges_.find(rangeKey);
if (range != hashranges_.end()) {
const WidthHeightPair &wh = range->second;
*newW = wh.first;
*newH = wh.second;
return true;
} else {
*newW = w;
*newH = h;
return false;
}
}
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;
}
}
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);
}
bool TextureReplacer::GenerateIni(const std::string &gameID, Path &generatedFilename) {
if (gameID.empty())
return false;
Path texturesDirectory = GetSysDirectory(DIRECTORY_TEXTURES) / gameID;
if (!File::Exists(texturesDirectory)) {
File::CreateFullPath(texturesDirectory);
}
generatedFilename = texturesDirectory / INI_FILENAME;
if (File::Exists(generatedFilename))
return true;
FILE *f = File::OpenCFile(generatedFilename, "wb");
if (f) {
fwrite("\xEF\xBB\xBF", 1, 3, f);
fprintf(f, R"(# This describes your textures and set up options for texture replacement.
# Documentation about the options and syntax is available here:
# https://www.ppsspp.org/docs/reference/texture-replacement
[options]
version = 1
hash = quick # options available: "quick", "xxh32" - more accurate, but slower, "xxh64" - more accurate and quite fast, but slower than xxh32 on 32 bit cpu's
ignoreMipmap = true # Usually, can just generate them with basisu, no need to dump.
reduceHash = false # Unsafe and can cause glitches in some cases, but allows to skip garbage data in some textures reducing endless duplicates as a side effect speeds up hashing as well, requires stronger hash like xxh32 or xxh64
ignoreAddress = false # Reduces duplicates at the cost of making hash less reliable, requires stronger hash like xxh32 or xxh64. Basically automatically sets the address to 0 in the dumped filenames.
[games]
# Used to make it easier to install, and override settings for other regions.
# Files still have to be copied to each TEXTURES folder.
%s = %s
[hashes]
# Use / for folders not \\, avoid special characters, and stick to lowercase.
# See wiki for more info.
[hashranges]
# This is useful for images that very clearly have smaller dimensions, like 480x272 image. They'll need to be redumped, since the hash will change. See the documentation.
# Example: 08b31020,512,512 = 480,272
# Example: 0x08b31020,512,512 = 480,272
[filtering]
# You can enforce specific filtering modes with this. Available modes are linear, nearest, auto. See the docs.
# Example: 08d3961000000909ba70b2af = nearest
[reducehashranges]
# Lets you set texture sizes where the hash range is reduced by a factor. See the docs.
# Example:
512,512=0.5
)", gameID.c_str(), INI_FILENAME.c_str());
fclose(f);
}
return File::Exists(generatedFilename);
}