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-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-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"
|
2020-10-04 18:48:47 +00:00
|
|
|
#include "Common/File/FileUtil.h"
|
2020-09-29 10:19:22 +00:00
|
|
|
#include "Common/StringUtils.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"
|
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";
|
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.
|
2016-04-30 23:21:16 +00:00
|
|
|
|
2018-03-26 10:33:36 +00:00
|
|
|
TextureReplacer::TextureReplacer() {
|
2016-05-01 02:35:35 +00:00
|
|
|
none_.alphaStatus_ = ReplacedTextureAlpha::UNKNOWN;
|
2016-04-30 20:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureReplacer::~TextureReplacer() {
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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-05 23:31:38 +00:00
|
|
|
basePath_ = Path(GetSysDirectory(DIRECTORY_TEXTURES)) / gameID_;
|
2016-04-30 21:05:03 +00:00
|
|
|
|
|
|
|
// If we're saving, auto-create the directory.
|
2021-05-05 23:31:38 +00:00
|
|
|
if (g_Config.bSaveNewTextures && !File::Exists((basePath_ / NEW_TEXTURE_DIR).ToString())) {
|
|
|
|
File::CreateFullPath(basePath_ / NEW_TEXTURE_DIR);
|
|
|
|
File::CreateEmptyFile(basePath_ / NEW_TEXTURE_DIR / ".nomedia");
|
2016-04-30 21:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enabled_ = File::Exists(basePath_) && File::IsDirectory(basePath_);
|
|
|
|
}
|
|
|
|
|
2016-04-30 23:21:16 +00:00
|
|
|
if (enabled_) {
|
|
|
|
enabled_ = LoadIni();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextureReplacer::LoadIni() {
|
|
|
|
// TODO: Use crc32c?
|
|
|
|
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
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
if (File::Exists(basePath_ / INI_FILENAME)) {
|
2016-04-30 23:21:16 +00:00
|
|
|
IniFile ini;
|
2021-05-05 23:31:38 +00:00
|
|
|
ini.LoadFromVFS((basePath_ / INI_FILENAME).ToString());
|
2016-04-30 23:21:16 +00:00
|
|
|
|
2019-07-14 23:01:37 +00:00
|
|
|
if (!LoadIniValues(ini)) {
|
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) {
|
|
|
|
INFO_LOG(G3D, "Loading extra texture ini: %s", overrideFilename.c_str());
|
|
|
|
IniFile overrideIni;
|
2021-05-05 23:31:38 +00:00
|
|
|
overrideIni.LoadFromVFS((basePath_ / overrideFilename).ToString());
|
2016-05-01 15:58:14 +00:00
|
|
|
|
2019-07-14 23:01:37 +00:00
|
|
|
if (!LoadIniValues(overrideIni, true)) {
|
|
|
|
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
|
|
|
|
2019-07-14 23:01:37 +00:00
|
|
|
// The ini doesn't have to exist for it to be valid.
|
|
|
|
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();
|
|
|
|
// Format: w,h = reducehashvalues
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 00:30:32 +00:00
|
|
|
const u8 *checkp = Memory::GetPointer(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
|
|
|
}
|
|
|
|
|
2016-05-01 02:35:35 +00:00
|
|
|
ReplacedTexture &TextureReplacer::FindReplacement(u64 cachekey, u32 hash, int w, int h) {
|
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()) {
|
|
|
|
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];
|
|
|
|
result.alphaStatus_ = ReplacedTextureAlpha::UNKNOWN;
|
|
|
|
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);
|
2021-05-05 23:31:38 +00:00
|
|
|
const Path filename = basePath_ / hashfile;
|
2016-05-01 01:39:30 +00:00
|
|
|
if (hashfile.empty() || !File::Exists(filename)) {
|
|
|
|
// Out of valid mip levels. Bail out.
|
|
|
|
break;
|
|
|
|
}
|
2016-04-30 22:41:12 +00:00
|
|
|
|
2018-06-06 04:53:32 +00:00
|
|
|
bool good = false;
|
2016-05-01 03:21:08 +00:00
|
|
|
ReplacedTextureLevel level;
|
2016-05-01 01:39:30 +00:00
|
|
|
level.fmt = ReplacedTextureFormat::F_8888;
|
2021-05-05 23:31:38 +00:00
|
|
|
level.file = filename.ToString();
|
2016-05-01 01:39:30 +00:00
|
|
|
|
|
|
|
png_image png = {};
|
|
|
|
png.version = PNG_IMAGE_VERSION;
|
|
|
|
FILE *fp = File::OpenCFile(filename, "rb");
|
|
|
|
if (png_image_begin_read_from_stdio(&png, fp)) {
|
|
|
|
// We pad files that have been hashrange'd so they are the same texture size.
|
|
|
|
level.w = (png.width * w) / newW;
|
|
|
|
level.h = (png.height * h) / newH;
|
2018-06-06 04:53:32 +00:00
|
|
|
good = true;
|
2016-05-01 01:39:30 +00:00
|
|
|
} else {
|
2016-05-01 06:58:58 +00:00
|
|
|
ERROR_LOG(G3D, "Could not load texture replacement info: %s - %s", filename.c_str(), png.message);
|
2016-04-30 22:41:12 +00:00
|
|
|
}
|
2016-05-01 01:39:30 +00:00
|
|
|
fclose(fp);
|
2016-05-01 06:58:58 +00:00
|
|
|
|
2016-05-01 01:39:30 +00:00
|
|
|
png_image_free(&png);
|
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
|
|
|
|
|
|
|
result->alphaStatus_ = ReplacedTextureAlpha::UNKNOWN;
|
2016-04-30 20:44:31 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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");
|
2016-04-30 21:05:03 +00:00
|
|
|
if (!g_Config.bSaveNewTextures) {
|
|
|
|
// Ignore.
|
|
|
|
return;
|
|
|
|
}
|
2016-12-16 20:53:55 +00:00
|
|
|
if (replacedInfo.addr > 0x05000000 && replacedInfo.addr < PSP_GetKernelMemoryEnd()) {
|
2016-05-01 01:39:40 +00:00
|
|
|
// Don't save the PPGe texture.
|
|
|
|
return;
|
|
|
|
}
|
2016-05-01 15:58:14 +00:00
|
|
|
if (replacedInfo.isVideo && !allowVideo_) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-05 19:40:40 +00:00
|
|
|
u64 cachekey = replacedInfo.cachekey;
|
|
|
|
if (ignoreAddress_) {
|
|
|
|
cachekey = cachekey & 0xFFFFFFFFULL;
|
|
|
|
}
|
2020-04-13 14:29:07 +00:00
|
|
|
if (ignoreMipmap_ && level > 0) {
|
2020-04-13 01:32:03 +00:00
|
|
|
return;
|
|
|
|
}
|
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;
|
|
|
|
const Path saveFilename = basePath_ / NEW_TEXTURE_DIR / 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.
|
|
|
|
if (hashfile.empty() || File::Exists(filename)) {
|
|
|
|
// 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);
|
|
|
|
if (it != savedCache_.end() && File::Exists(saveFilename)) {
|
|
|
|
// We've already saved this texture. Let's only save if it's bigger (e.g. scaled now.)
|
|
|
|
if (it->second.w >= w && it->second.h >= h) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-31 05:49:41 +00:00
|
|
|
#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.
|
2021-05-05 23:31:38 +00:00
|
|
|
const Path saveDirectory = basePath_ / NEW_TEXTURE_DIR / hashfile.substr(0, slash);
|
2016-05-31 05:49:41 +00:00
|
|
|
if (!File::Exists(saveDirectory)) {
|
|
|
|
File::CreateFullPath(saveDirectory);
|
2021-05-05 23:31:38 +00:00
|
|
|
File::CreateEmptyFile(saveDirectory / ".nomedia");
|
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
|
|
|
|
2016-05-01 15:54:43 +00:00
|
|
|
if (replacedInfo.fmt != ReplacedTextureFormat::F_8888) {
|
2016-04-30 22:03:39 +00:00
|
|
|
saveBuf.resize((pitch * h) / sizeof(u16));
|
2016-05-01 15:54:43 +00:00
|
|
|
switch (replacedInfo.fmt) {
|
2016-04-30 22:03:39 +00:00
|
|
|
case ReplacedTextureFormat::F_5650:
|
2020-05-14 01:17:58 +00:00
|
|
|
ConvertRGB565ToRGBA8888(saveBuf.data(), (const u16 *)data, (pitch * h) / sizeof(u16));
|
2016-04-30 22:03:39 +00:00
|
|
|
break;
|
|
|
|
case ReplacedTextureFormat::F_5551:
|
|
|
|
ConvertRGBA5551ToRGBA8888(saveBuf.data(), (const u16 *)data, (pitch * h) / sizeof(u16));
|
|
|
|
break;
|
|
|
|
case ReplacedTextureFormat::F_4444:
|
|
|
|
ConvertRGBA4444ToRGBA8888(saveBuf.data(), (const u16 *)data, (pitch * h) / sizeof(u16));
|
|
|
|
break;
|
2016-05-01 02:00:20 +00:00
|
|
|
case ReplacedTextureFormat::F_0565_ABGR:
|
2020-05-14 01:17:58 +00:00
|
|
|
ConvertBGR565ToRGBA8888(saveBuf.data(), (const u16 *)data, (pitch * h) / sizeof(u16));
|
2016-05-01 02:00:20 +00:00
|
|
|
break;
|
|
|
|
case ReplacedTextureFormat::F_1555_ABGR:
|
|
|
|
ConvertABGR1555ToRGBA8888(saveBuf.data(), (const u16 *)data, (pitch * h) / sizeof(u16));
|
|
|
|
break;
|
|
|
|
case ReplacedTextureFormat::F_4444_ABGR:
|
|
|
|
ConvertABGR4444ToRGBA8888(saveBuf.data(), (const u16 *)data, (pitch * h) / sizeof(u16));
|
|
|
|
break;
|
2016-04-30 22:03:39 +00:00
|
|
|
case ReplacedTextureFormat::F_8888_BGRA:
|
|
|
|
ConvertBGRA8888ToRGBA8888(saveBuf.data(), (const u32 *)data, (pitch * h) / sizeof(u32));
|
|
|
|
break;
|
2016-05-31 18:53:17 +00:00
|
|
|
case ReplacedTextureFormat::F_8888:
|
|
|
|
// Impossible. Just so we can get warnings on other missed formats.
|
|
|
|
break;
|
2016-04-30 22:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data = saveBuf.data();
|
2016-05-01 15:54:43 +00:00
|
|
|
if (replacedInfo.fmt != ReplacedTextureFormat::F_8888_BGRA) {
|
2016-05-01 02:00:20 +00:00
|
|
|
// We doubled our pitch.
|
|
|
|
pitch *= 2;
|
|
|
|
}
|
2016-04-30 22:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
png_image png;
|
|
|
|
memset(&png, 0, sizeof(png));
|
|
|
|
png.version = PNG_IMAGE_VERSION;
|
|
|
|
png.format = PNG_FORMAT_RGBA;
|
|
|
|
png.width = w;
|
|
|
|
png.height = h;
|
2016-05-01 03:21:08 +00:00
|
|
|
bool success = WriteTextureToPNG(&png, saveFilename, 0, data, pitch, nullptr);
|
2016-04-30 22:03:39 +00:00
|
|
|
png_image_free(&png);
|
|
|
|
|
|
|
|
if (png.warning_or_error >= 2) {
|
|
|
|
ERROR_LOG(COMMON, "Saving screenshot to PNG produced errors.");
|
|
|
|
} else if (success) {
|
2016-05-01 15:54:43 +00:00
|
|
|
NOTICE_LOG(G3D, "Saving texture for replacement: %08x / %dx%d", replacedInfo.hash, w, h);
|
2016-04-30 22:03:39 +00:00
|
|
|
}
|
2016-05-01 03:21:08 +00:00
|
|
|
|
|
|
|
// Remember that we've saved this for next time.
|
|
|
|
ReplacedTextureLevel saved;
|
|
|
|
saved.fmt = ReplacedTextureFormat::F_8888;
|
2021-05-05 23:31:38 +00:00
|
|
|
saved.file = filename.ToString();
|
2016-05-01 03:21:08 +00:00
|
|
|
saved.w = w;
|
|
|
|
saved.h = h;
|
|
|
|
savedCache_[replacementKey] = saved;
|
2016-04-30 20:44:31 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-30 20:44:31 +00:00
|
|
|
void ReplacedTexture::Load(int level, void *out, int rowPitch) {
|
2020-07-19 15:47:02 +00:00
|
|
|
_assert_msg_((size_t)level < levels_.size(), "Invalid miplevel");
|
|
|
|
_assert_msg_(out != nullptr && rowPitch > 0, "Invalid out/pitch");
|
2016-04-30 21:05:03 +00:00
|
|
|
|
2016-05-01 03:21:08 +00:00
|
|
|
const ReplacedTextureLevel &info = levels_[level];
|
2016-05-01 01:39:30 +00:00
|
|
|
|
|
|
|
png_image png = {};
|
|
|
|
png.version = PNG_IMAGE_VERSION;
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
FILE *fp = File::OpenCFile(Path(info.file), "rb");
|
2016-05-01 01:39:30 +00:00
|
|
|
if (!png_image_begin_read_from_stdio(&png, fp)) {
|
2016-05-01 06:58:58 +00:00
|
|
|
ERROR_LOG(G3D, "Could not load texture replacement info: %s - %s", info.file.c_str(), png.message);
|
2016-05-01 01:39:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-01 07:20:58 +00:00
|
|
|
|
|
|
|
bool checkedAlpha = false;
|
|
|
|
if ((png.format & PNG_FORMAT_FLAG_ALPHA) == 0) {
|
|
|
|
// Well, we know for sure it doesn't have alpha.
|
2016-09-11 18:03:41 +00:00
|
|
|
if (level == 0) {
|
|
|
|
alphaStatus_ = ReplacedTextureAlpha::FULL;
|
|
|
|
}
|
2016-05-01 07:20:58 +00:00
|
|
|
checkedAlpha = true;
|
|
|
|
}
|
|
|
|
png.format = PNG_FORMAT_RGBA;
|
|
|
|
|
2016-05-01 01:39:30 +00:00
|
|
|
if (!png_image_finish_read(&png, nullptr, out, rowPitch, nullptr)) {
|
2016-05-01 06:58:58 +00:00
|
|
|
ERROR_LOG(G3D, "Could not load texture replacement: %s - %s", info.file.c_str(), png.message);
|
2016-05-01 01:39:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-11 18:03:41 +00:00
|
|
|
if (!checkedAlpha) {
|
2016-05-01 06:58:58 +00:00
|
|
|
// This will only check the hashed bits.
|
|
|
|
CheckAlphaResult res = CheckAlphaRGBA8888Basic((u32 *)out, rowPitch / sizeof(u32), png.width, png.height);
|
2016-09-11 18:03:41 +00:00
|
|
|
if (res == CHECKALPHA_ANY || level == 0) {
|
|
|
|
alphaStatus_ = ReplacedTextureAlpha(res);
|
|
|
|
}
|
2016-05-01 06:58:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 01:39:30 +00:00
|
|
|
fclose(fp);
|
|
|
|
png_image_free(&png);
|
2016-04-30 20:44:31 +00:00
|
|
|
}
|
2018-10-01 00:00:05 +00:00
|
|
|
|
2021-05-05 23:31:38 +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-05 23:31:38 +00:00
|
|
|
Path texturesDirectory = Path(GetSysDirectory(DIRECTORY_TEXTURES)) / gameID;
|
2018-10-01 00:00:05 +00:00
|
|
|
if (!File::Exists(texturesDirectory)) {
|
|
|
|
File::CreateFullPath(texturesDirectory);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (generatedFilename)
|
2021-05-05 23:31:38 +00:00
|
|
|
*generatedFilename = texturesDirectory / INI_FILENAME;
|
|
|
|
if (File::Exists(*generatedFilename))
|
2018-10-01 00:00:05 +00:00
|
|
|
return true;
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
FILE *f = File::OpenCFile(texturesDirectory / INI_FILENAME, "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-05 23:31:38 +00:00
|
|
|
return File::Exists(texturesDirectory / INI_FILENAME);
|
2018-10-01 00:00:05 +00:00
|
|
|
}
|