Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Common/TextureReplacer.cpp
5654 views
1
// Copyright (c) 2016- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include "ppsspp_config.h"
19
20
#include <cstring>
21
#include <memory>
22
#include <png.h>
23
24
#include "ext/basis_universal/basisu_transcoder.h"
25
#include "ext/xxhash.h"
26
27
#include "Common/Data/Format/IniFile.h"
28
#include "Common/Data/Format/PNGLoad.h"
29
#include "Common/Data/Text/I18n.h"
30
#include "Common/Data/Text/Parsers.h"
31
#include "Common/File/VFS/DirectoryReader.h"
32
#include "Common/File/VFS/ZipFileReader.h"
33
#include "Common/File/FileUtil.h"
34
#include "Common/File/VFS/VFS.h"
35
#include "Common/StringUtils.h"
36
#include "Common/System/OSD.h"
37
#include "Common/Thread/ThreadManager.h"
38
#include "Common/TimeUtil.h"
39
#include "Core/Config.h"
40
#include "Core/System.h"
41
#include "Core/ELF/ParamSFO.h"
42
#include "GPU/Common/TextureReplacer.h"
43
#include "GPU/Common/TextureDecoder.h"
44
45
static const std::string INI_FILENAME = "textures.ini";
46
static const std::string ZIP_FILENAME = "textures.zip";
47
static const std::string NEW_TEXTURE_DIR = "new/";
48
static const int VERSION = 1;
49
static const double MAX_CACHE_SIZE = 4.0;
50
static bool basisu_initialized = false;
51
52
TextureReplacer::TextureReplacer(Draw::DrawContext *draw) {
53
if (!basisu_initialized) {
54
basist::basisu_transcoder_init();
55
basisu_initialized = true;
56
}
57
// We don't want to keep the draw object around, so extract the info we need.
58
if (draw->GetDataFormatSupport(Draw::DataFormat::BC3_UNORM_BLOCK)) formatSupport_.bc123 = true;
59
if (draw->GetDataFormatSupport(Draw::DataFormat::ASTC_4x4_UNORM_BLOCK)) formatSupport_.astc = true;
60
if (draw->GetDataFormatSupport(Draw::DataFormat::BC7_UNORM_BLOCK)) formatSupport_.bc7 = true;
61
if (draw->GetDataFormatSupport(Draw::DataFormat::ETC2_R8G8B8_UNORM_BLOCK)) formatSupport_.etc2 = true;
62
}
63
64
TextureReplacer::~TextureReplacer() {
65
for (auto iter : levelCache_) {
66
delete iter.second;
67
}
68
delete vfs_;
69
}
70
71
void TextureReplacer::NotifyConfigChanged() {
72
gameID_ = g_paramSFO.GetDiscID();
73
74
bool wasReplaceEnabled = replaceEnabled_;
75
replaceEnabled_ = g_Config.bReplaceTextures;
76
saveEnabled_ = g_Config.bSaveNewTextures;
77
if (replaceEnabled_ || saveEnabled_) {
78
basePath_ = GetSysDirectory(DIRECTORY_TEXTURES) / gameID_;
79
replaceEnabled_ = replaceEnabled_ && File::IsDirectory(basePath_);
80
newTextureDir_ = basePath_ / NEW_TEXTURE_DIR;
81
82
// If we're saving, auto-create the directory.
83
if (saveEnabled_ && !File::Exists(newTextureDir_)) {
84
INFO_LOG(Log::TexReplacement, "Creating new texture directory: '%s'", newTextureDir_.ToVisualString().c_str());
85
File::CreateFullPath(newTextureDir_);
86
// We no longer create a nomedia file here, since we put one
87
// in the TEXTURES root.
88
}
89
}
90
91
if (!replaceEnabled_ && wasReplaceEnabled) {
92
delete vfs_;
93
vfs_ = nullptr;
94
Decimate(ReplacerDecimateMode::ALL);
95
} else if (!wasReplaceEnabled && replaceEnabled_) {
96
std::string error;
97
replaceEnabled_ = LoadIni(&error);
98
if (!error.empty() && !replaceEnabled_) {
99
ERROR_LOG(Log::G3D, "ERROR: %s", error.c_str());
100
g_OSD.Show(OSDType::MESSAGE_ERROR, error, 5.0f);
101
}
102
} else if (saveEnabled_) {
103
// Even if just saving is enabled, it makes sense to reload the ini to get the correct
104
// settings for saving. See issue #19086. This can be expensive though.
105
std::string error;
106
bool result = LoadIni(&error, false);
107
if (!result) {
108
// Ignore errors here, just log if we successfully loaded an ini.
109
} else {
110
INFO_LOG(Log::G3D, "Loaded INI file for saving.");
111
}
112
}
113
}
114
115
bool TextureReplacer::LoadIni(std::string *error, bool notify) {
116
hash_ = ReplacedTextureHash::QUICK;
117
aliases_.clear();
118
hashranges_.clear();
119
filtering_.clear();
120
reducehashranges_.clear();
121
122
allowVideo_ = false;
123
ignoreAddress_ = false;
124
reduceHash_ = false;
125
reduceHashGlobalValue = 0.5;
126
// Prevents dumping the mipmaps.
127
ignoreMipmap_ = false;
128
129
delete vfs_;
130
vfs_ = nullptr;
131
132
Path zipPath = basePath_ / ZIP_FILENAME;
133
134
// First, check for textures.zip, which is used to reduce IO.
135
VFSBackend *dir = ZipFileReader::Create(zipPath, "", false);
136
if (!dir) {
137
INFO_LOG(Log::TexReplacement, "%s wasn't a zip file - opening the directory %s instead.", zipPath.c_str(), basePath_.c_str());
138
vfsIsZip_ = false;
139
dir = new DirectoryReader(basePath_);
140
} else {
141
if (!replaceEnabled_ && saveEnabled_) {
142
WARN_LOG(Log::TexReplacement, "Found zip file even though only saving is enabled! This is weird.");
143
}
144
vfsIsZip_ = true;
145
}
146
147
IniFile ini;
148
bool iniLoaded = ini.LoadFromVFS(*dir, INI_FILENAME);
149
150
if (iniLoaded) {
151
if (!LoadIniValues(ini, dir, false, error)) {
152
delete dir;
153
return false;
154
}
155
156
// Allow overriding settings per game id.
157
std::string overrideFilename;
158
if (ini.GetOrCreateSection("games")->Get(gameID_.c_str(), &overrideFilename)) {
159
if (overrideFilename == "true") {
160
// Ignore it
161
} else if (!overrideFilename.empty() && overrideFilename != INI_FILENAME) {
162
IniFile overrideIni;
163
iniLoaded = overrideIni.LoadFromVFS(*dir, overrideFilename);
164
if (!iniLoaded) {
165
*error = "Loading override ini failed: '" + overrideFilename + "'";
166
ERROR_LOG(Log::TexReplacement, "Failed to load extra texture ini: '%s'", overrideFilename.c_str());
167
// Since this error is most likely to occure for texture pack creators, let's just bail here
168
// so that the creator is more likely to look in the logs for what happened.
169
delete dir;
170
return false;
171
}
172
173
INFO_LOG(Log::TexReplacement, "Loading extra texture ini: %s", overrideFilename.c_str());
174
if (!LoadIniValues(overrideIni, nullptr, true, error)) {
175
*error = "Override: " + *error;
176
delete dir;
177
return false;
178
}
179
}
180
}
181
} else {
182
if (vfsIsZip_) {
183
// We don't accept zip files without inis.
184
ERROR_LOG(Log::TexReplacement, "Texture pack lacking ini file: %s", basePath_.c_str());
185
*error = "Zip files without ini files will not load";
186
delete dir;
187
return false;
188
} else {
189
if (replaceEnabled_) {
190
WARN_LOG(Log::TexReplacement, "Texture pack lacking ini file: %s Proceeding with only hash-named textures in the root.", basePath_.c_str());
191
}
192
// Do what we can do anyway: Scan for textures and build the map.
193
std::map<ReplacementCacheKey, std::map<int, std::string>> filenameMap;
194
ScanForHashNamedFiles(dir, filenameMap);
195
196
if (filenameMap.empty()) {
197
WARN_LOG(Log::TexReplacement, "No replacement textures found.");
198
return false;
199
}
200
201
ComputeAliasMap(filenameMap);
202
}
203
}
204
205
auto gr = GetI18NCategory(I18NCat::GRAPHICS);
206
if (replaceEnabled_ && notify) {
207
g_OSD.Show(OSDType::MESSAGE_SUCCESS, gr->T("Texture replacement pack activated"), 3.0f);
208
}
209
210
vfs_ = dir;
211
212
// If we have stuff loaded from before, need to update the vfs pointers to avoid
213
// crash on exit. The actual problem is that we tend to call LoadIni a little too much...
214
for (auto &repl : levelCache_) {
215
repl.second->vfs_ = vfs_;
216
}
217
218
if (replaceEnabled_) {
219
if (vfsIsZip_) {
220
INFO_LOG(Log::TexReplacement, "Texture pack activated from '%s'", (basePath_ / ZIP_FILENAME).c_str());
221
} else {
222
INFO_LOG(Log::TexReplacement, "Texture pack activated from '%s'", basePath_.c_str());
223
}
224
}
225
226
// The ini doesn't have to exist for the texture directory or zip to be valid.
227
return true;
228
}
229
230
void TextureReplacer::ScanForHashNamedFiles(VFSBackend *dir, std::map<ReplacementCacheKey, std::map<int, std::string>> &filenameMap) {
231
// Scan the root of the texture folder/zip and preinitialize the hash map.
232
// TODO: Could put VFSFileReference into the map...
233
std::vector<File::FileInfo> filesInRoot;
234
dir->GetFileListing("", &filesInRoot, nullptr);
235
for (auto file : filesInRoot) {
236
if (file.isDirectory)
237
continue;
238
if (file.name.empty() || file.name[0] == '.')
239
continue;
240
Path path(file.name);
241
std::string ext = path.GetFileExtension();
242
243
std::string hash = file.name.substr(0, file.name.size() - ext.size());
244
if (!((hash.size() >= 26 && hash.size() <= 27 && hash[24] == '_') || hash.size() == 24)) {
245
continue;
246
}
247
// OK, it's hash-like enough to try to parse it into the map.
248
if (equalsNoCase(ext, ".ktx2") || equalsNoCase(ext, ".png") || equalsNoCase(ext, ".dds") || equalsNoCase(ext, ".zim")) {
249
ReplacementCacheKey key(0, 0);
250
int level = 0; // sscanf might fail to pluck the level, but that's ok, we default to 0. sscanf doesn't write to non-matched outputs.
251
if (sscanf(hash.c_str(), "%16llx%8x_%d", &key.cachekey, &key.hash, &level) >= 1) {
252
// INFO_LOG(Log::TexReplacement, "hash-like file in root, adding: %s", file.name.c_str());
253
filenameMap[key][level] = file.name;
254
}
255
}
256
}
257
}
258
259
void TextureReplacer::ComputeAliasMap(const std::map<ReplacementCacheKey, std::map<int, std::string>> &filenameMap) {
260
for (auto &pair : filenameMap) {
261
std::string alias;
262
int mipIndex = 0;
263
for (auto &level : pair.second) {
264
if (level.first == mipIndex) {
265
alias += level.second + "|";
266
mipIndex++;
267
} else {
268
WARN_LOG(Log::TexReplacement, "Non-sequential mip index %d, breaking. filenames=%s", level.first, level.second.c_str());
269
break;
270
}
271
}
272
if (alias == "|") {
273
alias.clear(); // marker for no replacement
274
}
275
// Replace any '\' with '/', to be safe and consistent. Since these are from the ini file, we do this on all platforms.
276
for (auto &c : alias) {
277
if (c == '\\') {
278
c = '/';
279
}
280
}
281
aliases_[pair.first] = alias;
282
}
283
}
284
285
bool TextureReplacer::LoadIniValues(IniFile &ini, VFSBackend *dir, bool isOverride, std::string *error) {
286
INFO_LOG(Log::G3D, "Loading ini values...");
287
288
auto options = ini.GetOrCreateSection("options");
289
std::string hash;
290
if (!options->Get("hash", &hash)) {
291
*error = "textures.ini: Hash type not specified";
292
return false;
293
}
294
if (strcasecmp(hash.c_str(), "quick") == 0) {
295
hash_ = ReplacedTextureHash::QUICK;
296
} else if (strcasecmp(hash.c_str(), "xxh32") == 0) {
297
hash_ = ReplacedTextureHash::XXH32;
298
} else if (strcasecmp(hash.c_str(), "xxh64") == 0) {
299
hash_ = ReplacedTextureHash::XXH64;
300
} else if (!isOverride || !hash.empty()) {
301
*error = "textures.ini: Unsupported hash type: " + hash;
302
return false;
303
}
304
305
options->Get("video", &allowVideo_);
306
options->Get("ignoreAddress", &ignoreAddress_);
307
// Multiplies sizeInRAM/bytesPerLine in XXHASH by 0.5.
308
options->Get("reduceHash", &reduceHash_);
309
options->Get("ignoreMipmap", &ignoreMipmap_);
310
options->Get("skipLastDXT1Blocks128x64", &skipLastDXT1Blocks128x64_);
311
if (reduceHash_ && hash_ == ReplacedTextureHash::QUICK) {
312
reduceHash_ = false;
313
ERROR_LOG(Log::TexReplacement, "Texture Replacement: reduceHash option requires safer hash, use xxh32 or xxh64 instead.");
314
}
315
316
if (ignoreAddress_ && hash_ == ReplacedTextureHash::QUICK) {
317
ignoreAddress_ = false;
318
ERROR_LOG(Log::TexReplacement, "Texture Replacement: ignoreAddress option requires safer hash, use xxh32 or xxh64 instead.");
319
}
320
321
int version = 0;
322
if (options->Get("version", &version) && version > VERSION) {
323
ERROR_LOG(Log::TexReplacement, "Unsupported texture replacement version %d, trying anyway", version);
324
}
325
326
int badFileNameCount = 0;
327
328
std::map<ReplacementCacheKey, std::map<int, std::string>> filenameMap;
329
330
if (dir) {
331
ScanForHashNamedFiles(dir, filenameMap);
332
}
333
334
std::string badFilenames;
335
336
if (ini.HasSection("hashes")) {
337
const Section *hashesSection = ini.GetOrCreateSection("hashes");
338
// Format: hashname = filename.png
339
bool checkFilenames = saveEnabled_ && !g_Config.bIgnoreTextureFilenames && !vfsIsZip_;
340
341
for (const auto &line : hashesSection->Lines()) {
342
if (line.Key().empty())
343
continue;
344
ReplacementCacheKey key(0, 0);
345
// sscanf might fail to pluck the level if omitted from the line, but that's ok, we default level to 0.
346
// sscanf doesn't write to non-matched outputs.
347
int level = 0;
348
char k[128];
349
truncate_cpy(k, line.Key());
350
std::string_view v = line.Value();
351
if (sscanf(k, "%16llx%8x_%d", &key.cachekey, &key.hash, &level) >= 1) {
352
// We allow empty filenames, to mark textures that we don't want to keep saving.
353
filenameMap[key][level] = v;
354
if (checkFilenames) {
355
// TODO: We should check for the union of these on all platforms, really.
356
#if PPSSPP_PLATFORM(WINDOWS)
357
bool bad = v.find_first_of("\\ABCDEFGHIJKLMNOPQRSTUVWXYZ:<>|?*") != std::string::npos;
358
// Uppercase probably means the filenames don't match.
359
// Avoiding an actual check of the filenames to avoid performance impact.
360
#else
361
bool bad = v.find_first_of("\\:<>|?*") != std::string::npos;
362
#endif
363
if (bad) {
364
badFileNameCount++;
365
if (badFileNameCount == 10) {
366
badFilenames.append("...");
367
} else if (badFileNameCount < 10) {
368
badFilenames.append(v);
369
badFilenames.push_back('\n');
370
}
371
}
372
}
373
} else {
374
ERROR_LOG(Log::TexReplacement, "Unsupported syntax under [hashes], ignoring: %s = ", k);
375
}
376
}
377
}
378
379
// Now, translate the filenameMap to the final aliasMap.
380
ComputeAliasMap(filenameMap);
381
382
if (badFileNameCount > 0) {
383
auto err = GetI18NCategory(I18NCat::ERRORS);
384
g_OSD.Show(OSDType::MESSAGE_WARNING, err->T("textures.ini filenames may not be cross - platform(banned characters)"), badFilenames, 6.0f);
385
WARN_LOG(Log::TexReplacement, "Potentially bad filenames: %s", badFilenames.c_str());
386
}
387
388
if (ini.HasSection("hashranges")) {
389
auto hashranges = ini.GetOrCreateSection("hashranges")->ToMap();
390
// Format: addr,w,h = newW,newH
391
for (const auto &[k, v] : hashranges) {
392
ParseHashRange(k, v);
393
}
394
}
395
396
if (ini.HasSection("filtering")) {
397
auto filters = ini.GetOrCreateSection("filtering")->ToMap();
398
// Format: hashname = nearest or linear
399
for (const auto &[k, v] : filters) {
400
ParseFiltering(k, v);
401
}
402
}
403
404
if (ini.HasSection("reducehashranges")) {
405
auto reducehashranges = ini.GetOrCreateSection("reducehashranges")->ToMap();
406
// Format: w,h = reducehashvalues
407
for (const auto &[k, v] : reducehashranges) {
408
ParseReduceHashRange(k, v);
409
}
410
}
411
412
return true;
413
}
414
415
void TextureReplacer::ParseHashRange(const std::string &key, const std::string &value) {
416
std::vector<std::string> keyParts;
417
SplitString(key, ',', keyParts);
418
std::vector<std::string> valueParts;
419
SplitString(value, ',', valueParts);
420
421
if (keyParts.size() != 3 || valueParts.size() != 2) {
422
ERROR_LOG(Log::TexReplacement, "Ignoring invalid hashrange %s = %s, expecting addr,w,h = w,h", key.c_str(), value.c_str());
423
return;
424
}
425
426
// Allow addr not starting with 0x, for consistency. TryParse requires 0x to parse as hex.
427
if (!startsWith(keyParts[0], "0x") && !startsWith(keyParts[0], "0X")) {
428
keyParts[0] = "0x" + keyParts[0];
429
}
430
431
u32 addr;
432
u32 fromW;
433
u32 fromH;
434
if (!TryParse(keyParts[0], &addr) || !TryParse(keyParts[1], &fromW) || !TryParse(keyParts[2], &fromH)) {
435
ERROR_LOG(Log::TexReplacement, "Ignoring invalid hashrange %s = %s, key format is 0x12345678,512,512", key.c_str(), value.c_str());
436
return;
437
}
438
439
u32 toW;
440
u32 toH;
441
if (!TryParse(valueParts[0], &toW) || !TryParse(valueParts[1], &toH)) {
442
ERROR_LOG(Log::TexReplacement, "Ignoring invalid hashrange %s = %s, value format is 512,512", key.c_str(), value.c_str());
443
return;
444
}
445
446
if (toW > fromW || toH > fromH) {
447
ERROR_LOG(Log::TexReplacement, "Ignoring invalid hashrange %s = %s, range bigger than source", key.c_str(), value.c_str());
448
return;
449
}
450
451
const u64 rangeKey = ((u64)addr << 32) | ((u64)fromW << 16) | fromH;
452
hashranges_[rangeKey] = WidthHeightPair(toW, toH);
453
}
454
455
void TextureReplacer::ParseFiltering(const std::string &key, const std::string &value) {
456
ReplacementCacheKey itemKey(0, 0);
457
if (sscanf(key.c_str(), "%16llx%8x", &itemKey.cachekey, &itemKey.hash) >= 1) {
458
if (!strcasecmp(value.c_str(), "nearest")) {
459
filtering_[itemKey] = TEX_FILTER_FORCE_NEAREST;
460
} else if (!strcasecmp(value.c_str(), "linear")) {
461
filtering_[itemKey] = TEX_FILTER_FORCE_LINEAR;
462
} else if (!strcasecmp(value.c_str(), "auto")) {
463
filtering_[itemKey] = TEX_FILTER_AUTO;
464
} else {
465
ERROR_LOG(Log::TexReplacement, "Unsupported syntax under [filtering]: %s", value.c_str());
466
}
467
} else {
468
ERROR_LOG(Log::TexReplacement, "Unsupported syntax under [filtering]: %s", key.c_str());
469
}
470
}
471
472
void TextureReplacer::ParseReduceHashRange(const std::string& key, const std::string& value) {
473
std::vector<std::string> keyParts;
474
SplitString(key, ',', keyParts);
475
std::vector<std::string> valueParts;
476
SplitString(value, ',', valueParts);
477
478
if (keyParts.size() != 2 || valueParts.size() != 1) {
479
ERROR_LOG(Log::TexReplacement, "Ignoring invalid reducehashrange %s = %s, expecting w,h = reducehashvalue", key.c_str(), value.c_str());
480
return;
481
}
482
483
u32 forW;
484
u32 forH;
485
if (!TryParse(keyParts[0], &forW) || !TryParse(keyParts[1], &forH)) {
486
ERROR_LOG(Log::TexReplacement, "Ignoring invalid reducehashrange %s = %s, key format is 512,512", key.c_str(), value.c_str());
487
return;
488
}
489
490
float rhashvalue;
491
if (!TryParse(valueParts[0], &rhashvalue)) {
492
ERROR_LOG(Log::TexReplacement, "Ignoring invalid reducehashrange %s = %s, value format is 0.5", key.c_str(), value.c_str());
493
return;
494
}
495
496
if (rhashvalue == 0) {
497
ERROR_LOG(Log::TexReplacement, "Ignoring invalid hashrange %s = %s, reducehashvalue can't be 0", key.c_str(), value.c_str());
498
return;
499
}
500
501
const u64 reducerangeKey = ((u64)forW << 16) | forH;
502
reducehashranges_[reducerangeKey] = rhashvalue;
503
}
504
505
u32 TextureReplacer::ComputeHash(u32 addr, int bufw, int w, int h, bool swizzled, GETextureFormat fmt, u16 maxSeenV) {
506
_dbg_assert_msg_(replaceEnabled_ || saveEnabled_, "Replacement not enabled");
507
508
// TODO: Take swizzled into account, like in QuickTexHash().
509
// Note: Currently, only the MLB games are known to need this.
510
511
if (!LookupHashRange(addr, w, h, &w, &h)) {
512
// There wasn't any hash range, let's fall back to maxSeenV logic.
513
if (h == 512 && maxSeenV < 512 && maxSeenV != 0) {
514
h = (int)maxSeenV;
515
}
516
}
517
518
const u8 *checkp = Memory::GetPointerUnchecked(addr);
519
520
float reduceHashSize = 1.0f;
521
if (reduceHash_) {
522
reduceHashSize = LookupReduceHashRange(w, h);
523
// default to reduceHashGlobalValue which default is 0.5
524
}
525
526
if (bufw <= w) {
527
// We can assume the data is contiguous. These are the total used pixels.
528
const u32 totalPixels = bufw * h + (w - bufw);
529
u32 sizeInRAM = (textureBitsPerPixel[fmt] * totalPixels) / 8 * reduceHashSize;
530
531
// Sanity check: Ignore textures that are at the end of RAM.
532
if (Memory::MaxSizeAtAddress(addr) < sizeInRAM) {
533
ERROR_LOG(Log::G3D, "Can't hash a %d bytes textures at %08x - end point is outside memory", sizeInRAM, addr);
534
return 0;
535
}
536
537
// Hack for Yu Gi Oh texture hashing problem. See issue #19714
538
if (skipLastDXT1Blocks128x64_ && fmt == GE_TFMT_DXT1 && w == 128 && h == 64) {
539
// Skip the last few blocks as specified.
540
sizeInRAM -= 8 * skipLastDXT1Blocks128x64_;
541
}
542
543
switch (hash_) {
544
case ReplacedTextureHash::QUICK:
545
return StableQuickTexHash(checkp, sizeInRAM);
546
case ReplacedTextureHash::XXH32:
547
return XXH32(checkp, sizeInRAM, 0xBACD7814);
548
case ReplacedTextureHash::XXH64:
549
return XXH64(checkp, sizeInRAM, 0xBACD7814);
550
default:
551
return 0;
552
}
553
} else {
554
// We have gaps. Let's hash each row and sum.
555
const u32 bytesPerLine = (textureBitsPerPixel[fmt] * w) / 8 * reduceHashSize;
556
const u32 stride = (textureBitsPerPixel[fmt] * bufw) / 8;
557
558
u32 result = 0;
559
switch (hash_) {
560
case ReplacedTextureHash::QUICK:
561
for (int y = 0; y < h; ++y) {
562
u32 rowHash = StableQuickTexHash(checkp, bytesPerLine);
563
result = (result * 11) ^ rowHash;
564
checkp += stride;
565
}
566
break;
567
568
case ReplacedTextureHash::XXH32:
569
for (int y = 0; y < h; ++y) {
570
u32 rowHash = XXH32(checkp, bytesPerLine, 0xBACD7814);
571
result = (result * 11) ^ rowHash;
572
checkp += stride;
573
}
574
break;
575
576
case ReplacedTextureHash::XXH64:
577
for (int y = 0; y < h; ++y) {
578
u32 rowHash = XXH64(checkp, bytesPerLine, 0xBACD7814);
579
result = (result * 11) ^ rowHash;
580
checkp += stride;
581
}
582
break;
583
584
default:
585
break;
586
}
587
588
return result;
589
}
590
}
591
592
ReplacedTexture *TextureReplacer::FindReplacement(u64 cachekey, u32 hash, int w, int h) {
593
// Only actually replace if we're replacing. We might just be saving.
594
if (!Enabled() || !g_Config.bReplaceTextures) {
595
return nullptr;
596
}
597
598
ReplacementCacheKey replacementKey(cachekey, hash);
599
auto it = cache_.find(replacementKey);
600
if (it != cache_.end()) {
601
return it->second.texture;
602
}
603
604
ReplacementDesc desc;
605
desc.newW = w;
606
desc.newH = h;
607
desc.w = w;
608
desc.h = h;
609
desc.cachekey = cachekey;
610
desc.hash = hash;
611
LookupHashRange(cachekey >> 32, w, h, &desc.newW, &desc.newH);
612
613
if (ignoreAddress_) {
614
cachekey = cachekey & 0xFFFFFFFFULL;
615
}
616
617
bool foundAlias = false;
618
bool ignored = false;
619
std::string hashfiles = LookupHashFile(cachekey, hash, &foundAlias, &ignored);
620
621
// Early-out for ignored textures, let's not bother even starting a thread task.
622
if (ignored) {
623
// WARN_LOG(Log::TexReplacement, "Not found/ignored: %s (%d, %d)", hashfiles.c_str(), (int)foundReplacement, (int)ignored);
624
// Insert an entry into the cache for faster lookup next time.
625
ReplacedTextureRef ref{};
626
cache_.emplace(std::make_pair(replacementKey, ref));
627
return nullptr;
628
}
629
630
desc.forceFiltering = (TextureFiltering)0; // invalid value
631
FindFiltering(cachekey, hash, &desc.forceFiltering);
632
633
if (!foundAlias) {
634
// We'll just need to generate the names for each level.
635
// By default, we look for png since that's also what's dumped.
636
// For other file formats, use the ini to create aliases.
637
desc.filenames.resize(MAX_REPLACEMENT_MIP_LEVELS);
638
for (int level = 0; level < desc.filenames.size(); level++) {
639
desc.filenames[level] = TextureReplacer::HashName(cachekey, hash, level) + ".png";
640
}
641
desc.logId = desc.filenames[0];
642
desc.hashfiles = desc.filenames[0]; // The generated filename of the top level is used as the key in the data cache.
643
hashfiles.clear();
644
hashfiles.reserve(desc.filenames[0].size() * (desc.filenames.size() + 1));
645
for (int level = 0; level < desc.filenames.size(); level++) {
646
hashfiles += desc.filenames[level];
647
hashfiles.push_back('|');
648
}
649
} else {
650
desc.logId = hashfiles;
651
SplitString(hashfiles, '|', desc.filenames);
652
desc.hashfiles = hashfiles;
653
}
654
655
_dbg_assert_(!hashfiles.empty());
656
// OK, we might already have a matching texture, we use hashfiles as a key. Look it up in the level cache.
657
auto iter = levelCache_.find(hashfiles);
658
if (iter != levelCache_.end()) {
659
// Insert an entry into the cache for faster lookup next time.
660
ReplacedTextureRef ref;
661
ref.hashfiles = hashfiles;
662
ref.texture = iter->second;
663
cache_.emplace(std::make_pair(replacementKey, ref));
664
return iter->second;
665
}
666
667
// Final path - we actually need a new replacement texture, because we haven't seen "hashfiles" before.
668
desc.basePath = basePath_;
669
desc.formatSupport = formatSupport_;
670
671
ReplacedTexture *texture = new ReplacedTexture(vfs_, desc);
672
673
ReplacedTextureRef ref;
674
ref.hashfiles = hashfiles;
675
ref.texture = texture;
676
cache_.emplace(std::make_pair(replacementKey, ref));
677
678
// Also, insert the level in the level cache so we can look up by desc_->hashfiles again.
679
levelCache_.emplace(std::make_pair(hashfiles, texture));
680
return texture;
681
}
682
683
// We save textures on threadpool tasks since it's a fire-and-forget task, and both I/O and png compression
684
// can be pretty slow.
685
class SaveTextureTask : public Task {
686
public:
687
// malloc'd
688
u8 *rgbaData = nullptr;
689
690
int w = 0;
691
int h = 0;
692
693
Path filename;
694
Path saveFilename;
695
696
u32 replacedInfoHash = 0;
697
698
SaveTextureTask(u8 *_rgbaData) : rgbaData(_rgbaData) {}
699
~SaveTextureTask() {
700
free(rgbaData);
701
}
702
703
// This must be set to I/O blocking because of Android storage (so we attach the thread to JNI), while being CPU heavy too.
704
TaskType Type() const override { return TaskType::IO_BLOCKING; }
705
706
TaskPriority Priority() const override {
707
return TaskPriority::LOW;
708
}
709
710
void Run() override {
711
// Should we skip writing if the newly saved data already exists?
712
if (File::Exists(saveFilename)) {
713
return;
714
}
715
716
// And we always skip if the replace file already exists.
717
if (File::Exists(filename)) {
718
return;
719
}
720
721
Path saveDirectory = saveFilename.NavigateUp();
722
if (!File::Exists(saveDirectory)) {
723
// Previously, we created a .nomedia file here. This is unnecessary as they have recursive behavior.
724
// When initializing (see NotifyConfigChange above) we create one in the "root" of the "new" folder.
725
File::CreateFullPath(saveDirectory);
726
}
727
728
// Now that we've passed the checks, we change the file extension of the path we're actually
729
// going to write to to .png.
730
saveFilename = saveFilename.WithReplacedExtension(".png");
731
732
bool success = pngSave(saveFilename, rgbaData, w, h, 4);
733
if (!success) {
734
ERROR_LOG(Log::TexReplacement, "Saving texture to PNG produced errors.");
735
} else {
736
NOTICE_LOG(Log::TexReplacement, "Saving texture for replacement: %08x / %dx%d in '%s'", replacedInfoHash, w, h, saveFilename.ToVisualString().c_str());
737
}
738
}
739
};
740
741
bool TextureReplacer::WillSave(const ReplacedTextureDecodeInfo &replacedInfo) const {
742
if (!saveEnabled_)
743
return false;
744
// Don't save the PPGe texture.
745
if (replacedInfo.addr > 0x05000000 && replacedInfo.addr < PSP_GetKernelMemoryEnd())
746
return false;
747
if (replacedInfo.isVideo && !allowVideo_)
748
return false;
749
750
return true;
751
}
752
753
void TextureReplacer::NotifyTextureDecoded(ReplacedTexture *texture, const ReplacedTextureDecodeInfo &replacedInfo, const void *data, int srcPitch, int level, int origW, int origH, int scaledW, int scaledH) {
754
_assert_msg_(saveEnabled_, "Texture saving not enabled");
755
_assert_(srcPitch >= 0);
756
_assert_(data);
757
_assert_(level >= 0);
758
759
if (!WillSave(replacedInfo)) {
760
// Ignore.
761
return;
762
}
763
764
if (ignoreMipmap_ && level > 0) {
765
// Not saving higher mips.
766
return;
767
}
768
769
u64 cachekey = replacedInfo.cachekey;
770
if (ignoreAddress_) {
771
cachekey = cachekey & 0xFFFFFFFFULL;
772
}
773
774
bool foundAlias = false;
775
bool ignored = false;
776
std::string replacedLevelNames = LookupHashFile(cachekey, replacedInfo.hash, &foundAlias, &ignored);
777
if (ignored) {
778
// The ini file entry was set to empty string. We can early-out.
779
return;
780
}
781
782
// Alright, get the specified filename for the level.
783
std::string hashfile;
784
if (!replacedLevelNames.empty()) {
785
// If the user has specified a name before, we get it here.
786
std::vector<std::string> names;
787
SplitString(replacedLevelNames, '|', names);
788
hashfile = names[std::min(level, (int)(names.size() - 1))];
789
} else {
790
// Generate a new PNG filename, complete with level.
791
hashfile = HashName(cachekey, replacedInfo.hash, level) + ".png";
792
}
793
794
ReplacementCacheKey replacementKey(cachekey, replacedInfo.hash);
795
auto it = savedCache_.find(replacementKey);
796
if (it != savedCache_.end()) {
797
// We've already saved this texture. Ignore it.
798
// We don't really care about changing the scale factor during runtime, only confusing.
799
return;
800
}
801
double now = time_now_d();
802
803
// Width/height of the image to save.
804
int w = scaledW;
805
int h = scaledH;
806
807
if (w == 0 || h == 0) {
808
return;
809
}
810
811
// Only save the hashed portion of the PNG.
812
int lookupW;
813
int lookupH;
814
if (LookupHashRange(replacedInfo.addr, origW, origH, &lookupW, &lookupH)) {
815
w = lookupW * (scaledW / origW);
816
h = lookupH * (scaledH / origH);
817
}
818
819
820
size_t saveBufSize = w * h * 4;
821
u8 *saveBuf = (u8 *)malloc(saveBufSize);
822
if (!saveBuf) {
823
ERROR_LOG(Log::TexReplacement, "Failed to allocated %d bytes of memory for saving a texture", (int)saveBufSize);
824
return;
825
}
826
827
// Copy data to a buffer so we can send it to the thread. Might as well compact-away the pitch
828
// while we're at it.
829
for (int y = 0; y < h; y++) {
830
memcpy(saveBuf + y * w * 4, (const u8 *)data + y * srcPitch, w * 4);
831
}
832
833
SaveTextureTask *task = new SaveTextureTask(std::move(saveBuf));
834
835
task->filename = basePath_ / hashfile;
836
task->saveFilename = newTextureDir_ / hashfile;
837
838
task->w = w;
839
task->h = h;
840
task->replacedInfoHash = replacedInfo.hash;
841
g_threadManager.EnqueueTask(task); // We don't care about waiting for the task. It'll be fine.
842
843
// Remember that we've saved this for next time.
844
// Should be OK that the actual disk write may not be finished yet.
845
SavedTextureCacheData &saveData = savedCache_[replacementKey];
846
saveData.levelW[level] = w;
847
saveData.levelH[level] = h;
848
saveData.levelSaved[level] = true;
849
saveData.lastTimeSaved = now;
850
}
851
852
void TextureReplacer::Decimate(ReplacerDecimateMode mode) {
853
// Allow replacements to be cached for a long time, although they're large.
854
double age = 1800.0;
855
if (mode == ReplacerDecimateMode::FORCE_PRESSURE) {
856
age = 90.0;
857
} else if (mode == ReplacerDecimateMode::ALL) {
858
age = 0.0;
859
} else if (lastTextureCacheSizeGB_ > 1.0) {
860
double pressure = std::min(MAX_CACHE_SIZE, lastTextureCacheSizeGB_) / MAX_CACHE_SIZE;
861
// Get more aggressive the closer we are to the max.
862
age = 90.0 + (1.0 - pressure) * 1710.0;
863
}
864
865
const double threshold = time_now_d() - age;
866
size_t totalSize = 0;
867
for (auto &item : levelCache_) {
868
// During decimation, it's fine to try-lock here to avoid blocking the main thread while
869
// the level is being loaded - in that case we don't want to decimate anyway.
870
if (item.second->lock_.try_lock()) {
871
item.second->PurgeIfNotUsedSinceTime(threshold);
872
totalSize += item.second->GetTotalDataSize(); // TODO: Make something better.
873
item.second->lock_.unlock();
874
}
875
// don't actually delete the items here, just clean out the data.
876
}
877
878
double totalSizeGB = totalSize / (1024.0 * 1024.0 * 1024.0);
879
if (totalSizeGB >= 1.0) {
880
WARN_LOG(Log::TexReplacement, "Decimated replacements older than %fs, currently using %f GB of RAM", age, totalSizeGB);
881
}
882
lastTextureCacheSizeGB_ = totalSizeGB;
883
}
884
885
template <typename Key, typename Value>
886
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) {
887
auto alias = map.find(key);
888
if (alias != map.end())
889
return alias;
890
891
// Also check for a few more aliases with zeroed portions:
892
// Only clut hash (very dangerous in theory, in practice not more than missing "just" data hash)
893
key.cachekey = cachekey & 0xFFFFFFFFULL;
894
key.hash = 0;
895
alias = map.find(key);
896
if (alias != map.end())
897
return alias;
898
899
if (!ignoreAddress) {
900
// No data hash.
901
key.cachekey = cachekey;
902
key.hash = 0;
903
alias = map.find(key);
904
if (alias != map.end())
905
return alias;
906
}
907
908
// No address.
909
key.cachekey = cachekey & 0xFFFFFFFFULL;
910
key.hash = hash;
911
alias = map.find(key);
912
if (alias != map.end())
913
return alias;
914
915
if (!ignoreAddress) {
916
// Address, but not clut hash (in case of garbage clut data.)
917
key.cachekey = cachekey & ~0xFFFFFFFFULL;
918
key.hash = hash;
919
alias = map.find(key);
920
if (alias != map.end())
921
return alias;
922
}
923
924
// Anything with this data hash (a little dangerous.)
925
key.cachekey = 0;
926
key.hash = hash;
927
return map.find(key);
928
}
929
930
bool TextureReplacer::FindFiltering(u64 cachekey, u32 hash, TextureFiltering *forceFiltering) {
931
if (!Enabled() || !g_Config.bReplaceTextures) {
932
return false;
933
}
934
935
ReplacementCacheKey replacementKey(cachekey, hash);
936
auto filter = LookupWildcard(filtering_, replacementKey, cachekey, hash, ignoreAddress_);
937
if (filter == filtering_.end()) {
938
// Allow a global wildcard.
939
replacementKey.cachekey = 0;
940
replacementKey.hash = 0;
941
filter = filtering_.find(replacementKey);
942
}
943
if (filter != filtering_.end()) {
944
*forceFiltering = filter->second;
945
return true;
946
}
947
return false;
948
}
949
950
std::string TextureReplacer::LookupHashFile(u64 cachekey, u32 hash, bool *foundAlias, bool *ignored) {
951
ReplacementCacheKey key(cachekey, hash);
952
auto alias = LookupWildcard(aliases_, key, cachekey, hash, ignoreAddress_);
953
if (alias != aliases_.end()) {
954
// Note: this will be blank if explicitly ignored.
955
*foundAlias = true;
956
*ignored = alias->second.empty();
957
return alias->second;
958
}
959
*foundAlias = false;
960
*ignored = false;
961
return "";
962
}
963
964
std::string TextureReplacer::HashName(u64 cachekey, u32 hash, int level) {
965
char hashname[16 + 8 + 1 + 11 + 1] = {};
966
if (level > 0) {
967
snprintf(hashname, sizeof(hashname), "%016llx%08x_%d", cachekey, hash, level);
968
} else {
969
snprintf(hashname, sizeof(hashname), "%016llx%08x", cachekey, hash);
970
}
971
972
return hashname;
973
}
974
975
bool TextureReplacer::LookupHashRange(u32 addr, int w, int h, int *newW, int *newH) {
976
const u64 rangeKey = ((u64)addr << 32) | ((u64)w << 16) | h;
977
auto range = hashranges_.find(rangeKey);
978
if (range != hashranges_.end()) {
979
const WidthHeightPair &wh = range->second;
980
*newW = wh.first;
981
*newH = wh.second;
982
return true;
983
} else {
984
*newW = w;
985
*newH = h;
986
return false;
987
}
988
}
989
990
float TextureReplacer::LookupReduceHashRange(int w, int h) {
991
const u64 reducerangeKey = ((u64)w << 16) | h;
992
auto range = reducehashranges_.find(reducerangeKey);
993
if (range != reducehashranges_.end()) {
994
float rhv = range->second;
995
return rhv;
996
}
997
else {
998
return reduceHashGlobalValue;
999
}
1000
}
1001
1002
bool TextureReplacer::IniExists(const std::string &gameID) {
1003
if (gameID.empty())
1004
return false;
1005
1006
Path texturesDirectory = GetSysDirectory(DIRECTORY_TEXTURES) / gameID;
1007
Path generatedFilename = texturesDirectory / INI_FILENAME;
1008
return File::Exists(generatedFilename);
1009
}
1010
1011
bool TextureReplacer::GenerateIni(const std::string &gameID, Path &generatedFilename) {
1012
if (gameID.empty())
1013
return false;
1014
1015
Path texturesDirectory = GetSysDirectory(DIRECTORY_TEXTURES) / gameID;
1016
if (!File::Exists(texturesDirectory)) {
1017
File::CreateFullPath(texturesDirectory);
1018
}
1019
1020
generatedFilename = texturesDirectory / INI_FILENAME;
1021
if (File::Exists(generatedFilename))
1022
return true;
1023
1024
FILE *f = File::OpenCFile(generatedFilename, "wb");
1025
if (f) {
1026
// Unicode byte order mark
1027
fwrite("\xEF\xBB\xBF", 1, 3, f);
1028
1029
// Let's also write some defaults.
1030
fprintf(f, R"(# This describes your textures and set up options for texture replacement.
1031
# Documentation about the options and syntax is available here:
1032
# https://www.ppsspp.org/docs/reference/texture-replacement
1033
1034
[options]
1035
version = 1
1036
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
1037
ignoreMipmap = true # Usually, can just generate them with basisu, no need to dump.
1038
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
1039
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.
1040
1041
[games]
1042
# Used to make it easier to install, and override settings for other regions.
1043
# Files still have to be copied to each TEXTURES folder.
1044
%s = %s
1045
1046
[hashes]
1047
# Use / for folders not \\, avoid special characters, and stick to lowercase.
1048
# See wiki for more info.
1049
1050
[hashranges]
1051
# 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.
1052
# Example: 08b31020,512,512 = 480,272
1053
# Example: 0x08b31020,512,512 = 480,272
1054
1055
[filtering]
1056
# You can enforce specific filtering modes with this. Available modes are linear, nearest, auto. See the docs.
1057
# Example: 08d3961000000909ba70b2af = nearest
1058
1059
[reducehashranges]
1060
# Lets you set texture sizes where the hash range is reduced by a factor. See the docs.
1061
# Example:
1062
512,512=0.5
1063
1064
)", gameID.c_str(), INI_FILENAME.c_str());
1065
fclose(f);
1066
}
1067
return File::Exists(generatedFilename);
1068
}
1069
1070