1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
// Copyright 2009 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "VideoCommon/HiresTextures.h"
#include <algorithm>
#include <memory>
#include <mutex>
#include <string>
#include <string_view>
#include <thread>
#include <unordered_map>
#include <utility>
#include <vector>
#include <xxhash.h>
#include <fmt/format.h>
#include "Common/CommonPaths.h"
#include "Common/FileSearch.h"
#include "Common/FileUtil.h"
#include "Common/Flag.h"
#include "Common/IOFile.h"
#include "Common/Image.h"
#include "Common/Logging/Log.h"
#include "Common/MemoryUtil.h"
#include "Common/StringUtil.h"
#include "Common/Swap.h"
#include "Common/Thread.h"
#include "Common/Timer.h"
#include "Core/Config/GraphicsSettings.h"
#include "Core/ConfigManager.h"
#include "VideoCommon/OnScreenDisplay.h"
#include "VideoCommon/VideoConfig.h"
struct DiskTexture
{
std::string path;
bool has_arbitrary_mipmaps;
};
constexpr std::string_view s_format_prefix{"tex1_"};
static std::unordered_map<std::string, DiskTexture> s_textureMap;
static std::unordered_map<std::string, std::shared_ptr<HiresTexture>> s_textureCache;
static std::mutex s_textureCacheMutex;
static Common::Flag s_textureCacheAbortLoading;
static std::thread s_prefetcher;
void HiresTexture::Init()
{
// Note: Update is not called here so that we handle dynamic textures on startup more gracefully
}
void HiresTexture::Shutdown()
{
Clear();
}
void HiresTexture::Update()
{
if (s_prefetcher.joinable())
{
s_textureCacheAbortLoading.Set();
s_prefetcher.join();
}
if (!g_ActiveConfig.bHiresTextures)
{
Clear();
return;
}
if (!g_ActiveConfig.bCacheHiresTextures)
{
s_textureCache.clear();
}
const std::string& game_id = SConfig::GetInstance().GetGameID();
const std::set<std::string> texture_directories =
GetTextureDirectoriesWithGameId(File::GetUserPath(D_HIRESTEXTURES_IDX), game_id);
const std::vector<std::string> extensions{".png", ".dds"};
for (const auto& texture_directory : texture_directories)
{
const auto texture_paths =
Common::DoFileSearch({texture_directory}, extensions, /*recursive*/ true);
bool failed_insert = false;
for (auto& path : texture_paths)
{
std::string filename;
SplitPath(path, nullptr, &filename, nullptr);
if (filename.substr(0, s_format_prefix.length()) == s_format_prefix)
{
const size_t arb_index = filename.rfind("_arb");
const bool has_arbitrary_mipmaps = arb_index != std::string::npos;
if (has_arbitrary_mipmaps)
filename.erase(arb_index, 4);
const auto [it, inserted] =
s_textureMap.try_emplace(filename, DiskTexture{path, has_arbitrary_mipmaps});
if (!inserted)
{
failed_insert = true;
}
}
}
if (failed_insert)
{
ERROR_LOG_FMT(VIDEO, "One or more textures at path '{}' were already inserted",
texture_directory);
}
}
if (g_ActiveConfig.bCacheHiresTextures)
{
// remove cached but deleted textures
auto iter = s_textureCache.begin();
while (iter != s_textureCache.end())
{
if (s_textureMap.find(iter->first) == s_textureMap.end())
{
iter = s_textureCache.erase(iter);
}
else
{
iter++;
}
}
s_textureCacheAbortLoading.Clear();
s_prefetcher = std::thread(Prefetch);
}
}
void HiresTexture::Clear()
{
if (s_prefetcher.joinable())
{
s_textureCacheAbortLoading.Set();
s_prefetcher.join();
}
s_textureMap.clear();
s_textureCache.clear();
}
void HiresTexture::Prefetch()
{
Common::SetCurrentThreadName("Prefetcher");
size_t size_sum = 0;
const size_t sys_mem = Common::MemPhysical();
const size_t recommended_min_mem = 2 * size_t(1024 * 1024 * 1024);
// keep 2GB memory for system stability if system RAM is 4GB+ - use half of memory in other cases
const size_t max_mem =
(sys_mem / 2 < recommended_min_mem) ? (sys_mem / 2) : (sys_mem - recommended_min_mem);
const u32 start_time = Common::Timer::GetTimeMs();
for (const auto& entry : s_textureMap)
{
const std::string& base_filename = entry.first;
if (base_filename.find("_mip") == std::string::npos)
{
std::unique_lock<std::mutex> lk(s_textureCacheMutex);
auto iter = s_textureCache.find(base_filename);
if (iter == s_textureCache.end())
{
// unlock while loading a texture. This may result in a race condition where
// we'll load a texture twice, but it reduces the stuttering a lot.
lk.unlock();
std::unique_ptr<HiresTexture> texture = Load(base_filename, 0, 0);
lk.lock();
if (texture)
{
std::shared_ptr<HiresTexture> ptr(std::move(texture));
iter = s_textureCache.insert(iter, std::make_pair(base_filename, ptr));
}
}
if (iter != s_textureCache.end())
{
for (const Level& l : iter->second->m_levels)
size_sum += l.data.size();
}
}
if (s_textureCacheAbortLoading.IsSet())
{
return;
}
if (size_sum > max_mem)
{
Config::SetCurrent(Config::GFX_HIRES_TEXTURES, false);
OSD::AddMessage(
fmt::format(
"Custom Textures prefetching after {:.1f} MB aborted, not enough RAM available",
size_sum / (1024.0 * 1024.0)),
10000);
return;
}
}
const u32 stop_time = Common::Timer::GetTimeMs();
OSD::AddMessage(fmt::format("Custom Textures loaded, {:.1f} MB in {:.1f}s",
size_sum / (1024.0 * 1024.0), (stop_time - start_time) / 1000.0),
10000);
}
std::string HiresTexture::GenBaseName(TextureInfo& texture_info, bool dump)
{
if (!dump && s_textureMap.empty())
return "";
const auto texture_name_details = texture_info.CalculateTextureName();
// try to match a wildcard template
if (!dump)
{
const std::string texture_name =
fmt::format("{}_${}", texture_name_details.base_name, texture_name_details.format_name);
if (s_textureMap.find(texture_name) != s_textureMap.end())
return texture_name;
}
// else generate the complete texture
const std::string full_name = texture_name_details.GetFullName();
if (dump || s_textureMap.find(full_name) != s_textureMap.end())
return full_name;
return "";
}
u32 HiresTexture::CalculateMipCount(u32 width, u32 height)
{
u32 mip_width = width;
u32 mip_height = height;
u32 mip_count = 1;
while (mip_width > 1 || mip_height > 1)
{
mip_width = std::max(mip_width / 2, 1u);
mip_height = std::max(mip_height / 2, 1u);
mip_count++;
}
return mip_count;
}
std::shared_ptr<HiresTexture> HiresTexture::Search(TextureInfo& texture_info)
{
const std::string base_filename = GenBaseName(texture_info);
std::lock_guard<std::mutex> lk(s_textureCacheMutex);
auto iter = s_textureCache.find(base_filename);
if (iter != s_textureCache.end())
{
return iter->second;
}
std::shared_ptr<HiresTexture> ptr(
Load(base_filename, texture_info.GetRawWidth(), texture_info.GetRawHeight()));
if (ptr && g_ActiveConfig.bCacheHiresTextures)
{
s_textureCache[base_filename] = ptr;
}
return ptr;
}
std::unique_ptr<HiresTexture> HiresTexture::Load(const std::string& base_filename, u32 width,
u32 height)
{
// We need to have a level 0 custom texture to even consider loading.
auto filename_iter = s_textureMap.find(base_filename);
if (filename_iter == s_textureMap.end())
return nullptr;
// Try to load level 0 (and any mipmaps) from a DDS file.
// If this fails, it's fine, we'll just load level0 again using SOIL.
// Can't use make_unique due to private constructor.
std::unique_ptr<HiresTexture> ret = std::unique_ptr<HiresTexture>(new HiresTexture());
const DiskTexture& first_mip_file = filename_iter->second;
ret->m_has_arbitrary_mipmaps = first_mip_file.has_arbitrary_mipmaps;
LoadDDSTexture(ret.get(), first_mip_file.path);
// Load remaining mip levels, or from the start if it's not a DDS texture.
for (u32 mip_level = static_cast<u32>(ret->m_levels.size());; mip_level++)
{
std::string filename = base_filename;
if (mip_level != 0)
filename += fmt::format("_mip{}", mip_level);
filename_iter = s_textureMap.find(filename);
if (filename_iter == s_textureMap.end())
break;
// Try loading DDS textures first, that way we maintain compression of DXT formats.
// TODO: Reduce the number of open() calls here. We could use one fd.
Level level;
if (!LoadDDSTexture(level, filename_iter->second.path, mip_level))
{
File::IOFile file;
file.Open(filename_iter->second.path, "rb");
std::vector<u8> buffer(file.GetSize());
file.ReadBytes(buffer.data(), file.GetSize());
if (!LoadTexture(level, buffer))
{
ERROR_LOG_FMT(VIDEO, "Custom texture {} failed to load", filename);
break;
}
}
ret->m_levels.push_back(std::move(level));
}
// If we failed to load any mip levels, we can't use this texture at all.
if (ret->m_levels.empty())
return nullptr;
// Verify that the aspect ratio of the texture hasn't changed, as this could have side-effects.
const Level& first_mip = ret->m_levels[0];
if (first_mip.width * height != first_mip.height * width)
{
ERROR_LOG_FMT(VIDEO,
"Invalid custom texture size {}x{} for texture {}. The aspect differs "
"from the native size {}x{}.",
first_mip.width, first_mip.height, first_mip_file.path, width, height);
}
// Same deal if the custom texture isn't a multiple of the native size.
if (width != 0 && height != 0 && (first_mip.width % width || first_mip.height % height))
{
ERROR_LOG_FMT(VIDEO,
"Invalid custom texture size {}x{} for texture {}. Please use an integer "
"upscaling factor based on the native size {}x{}.",
first_mip.width, first_mip.height, first_mip_file.path, width, height);
}
// Verify that each mip level is the correct size (divide by 2 each time).
u32 current_mip_width = first_mip.width;
u32 current_mip_height = first_mip.height;
for (u32 mip_level = 1; mip_level < static_cast<u32>(ret->m_levels.size()); mip_level++)
{
if (current_mip_width != 1 || current_mip_height != 1)
{
current_mip_width = std::max(current_mip_width / 2, 1u);
current_mip_height = std::max(current_mip_height / 2, 1u);
const Level& level = ret->m_levels[mip_level];
if (current_mip_width == level.width && current_mip_height == level.height)
continue;
ERROR_LOG_FMT(
VIDEO, "Invalid custom texture size {}x{} for texture {}. Mipmap level {} must be {}x{}.",
level.width, level.height, first_mip_file.path, mip_level, current_mip_width,
current_mip_height);
}
else
{
// It is invalid to have more than a single 1x1 mipmap.
ERROR_LOG_FMT(VIDEO, "Custom texture {} has too many 1x1 mipmaps. Skipping extra levels.",
first_mip_file.path);
}
// Drop this mip level and any others after it.
while (ret->m_levels.size() > mip_level)
ret->m_levels.pop_back();
}
// All levels have to have the same format.
if (std::any_of(ret->m_levels.begin(), ret->m_levels.end(),
[&ret](const Level& l) { return l.format != ret->m_levels[0].format; }))
{
ERROR_LOG_FMT(VIDEO, "Custom texture {} has inconsistent formats across mip levels.",
first_mip_file.path);
return nullptr;
}
return ret;
}
bool HiresTexture::LoadTexture(Level& level, const std::vector<u8>& buffer)
{
if (!Common::LoadPNG(buffer, &level.data, &level.width, &level.height))
return false;
if (level.data.empty())
return false;
// Loaded PNG images are converted to RGBA.
level.format = AbstractTextureFormat::RGBA8;
level.row_length = level.width;
return true;
}
std::set<std::string> GetTextureDirectoriesWithGameId(const std::string& root_directory,
const std::string& game_id)
{
std::set<std::string> result;
const std::string texture_directory = root_directory + game_id;
if (File::Exists(texture_directory))
{
result.insert(texture_directory);
}
else
{
// If there's no directory with the region-specific ID, look for a 3-character region-free one
const std::string region_free_directory = root_directory + game_id.substr(0, 3);
if (File::Exists(region_free_directory))
{
result.insert(region_free_directory);
}
}
const auto match_gameid = [game_id](const std::string& filename) {
std::string basename;
SplitPath(filename, nullptr, &basename, nullptr);
return basename == game_id || basename == game_id.substr(0, 3);
};
// Look for any other directories that might be specific to the given gameid
const auto files = Common::DoFileSearch({root_directory}, {".txt"}, true);
for (const auto& file : files)
{
if (match_gameid(file))
{
// The following code is used to calculate the top directory
// of a found gameid.txt file
// ex: <root directory>/My folder/gameids/<gameid>.txt
// would insert "<root directory>/My folder"
const auto directory_path = file.substr(root_directory.size());
const std::size_t first_path_separator_position = directory_path.find_first_of(DIR_SEP_CHR);
result.insert(root_directory + directory_path.substr(0, first_path_separator_position));
}
}
return result;
}
HiresTexture::~HiresTexture()
{
}
AbstractTextureFormat HiresTexture::GetFormat() const
{
return m_levels.at(0).format;
}
bool HiresTexture::HasArbitraryMipmaps() const
{
return m_has_arbitrary_mipmaps;
}
|