Skip to content

Commit 6d1cf6f

Browse files
committed
Refractor
1 parent b030982 commit 6d1cf6f

File tree

9 files changed

+78
-91
lines changed

9 files changed

+78
-91
lines changed

core/emulator.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ void Emulator::unloadGame()
752752
settings.content.fileName.clear();
753753
settings.content.title.clear();
754754
settings.platform.system = DC_PLATFORM_DREAMCAST;
755-
custom_texture.Terminate();
755+
custom_texture.terminate();
756756
state = Init;
757757
EventManager::event(Event::Terminate);
758758
}
@@ -776,7 +776,7 @@ void Emulator::term()
776776
delete recompiler;
777777
recompiler = nullptr;
778778
}
779-
custom_texture.Terminate(); // lr: avoid deadlock on exit (win32)
779+
custom_texture.terminate(); // lr: avoid deadlock on exit (win32)
780780
reios_term();
781781
aica::term();
782782
pvr::term();
@@ -852,7 +852,7 @@ void loadGameSpecificSettings()
852852
loadSpecialSettings();
853853

854854
config::Settings::instance().setGameId(settings.content.gameId);
855-
custom_texture.Init();
855+
custom_texture.init();
856856

857857
// Reload per-game settings
858858
config::Settings::instance().load(true);
@@ -885,7 +885,7 @@ void Emulator::stepRange(u32 from, u32 to)
885885
void Emulator::loadstate(Deserializer& deser)
886886
{
887887
if (!custom_texture.preloaded())
888-
custom_texture.Terminate();
888+
custom_texture.terminate();
889889
#if FEAT_AREC == DYNAREC_JIT
890890
aica::arm::recompiler::flush();
891891
#endif
@@ -1122,7 +1122,7 @@ void Emulator::diskChange()
11221122
cheatManager.reset(settings.content.gameId);
11231123
if (cheatManager.isWidescreen())
11241124
config::ScreenStretching.override(134); // 4:3 -> 16:9
1125-
custom_texture.Terminate();
1125+
custom_texture.terminate();
11261126
EventManager::event(Event::DiskChange);
11271127
}
11281128

core/rend/CustomTexture.cpp

Lines changed: 47 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,12 @@ class CustomTextureSource : public BaseCustomTextureSource
5454
}
5555
}
5656
}
57-
bool customTexturesAvailable() override { return custom_textures_available; }
58-
bool shouldReplace() const override { return config::CustomTextures; }
59-
bool shouldPreload() const override { return config::CustomTextures && config::PreloadCustomTextures; }
60-
bool LoadMap() override;
61-
size_t GetTextureCount() const override { return texture_map.size(); }
57+
bool shouldReplace() const override { return config::CustomTextures && custom_textures_available; }
58+
bool shouldPreload() const override { return shouldReplace() && config::PreloadCustomTextures; }
59+
bool loadMap() override;
60+
size_t getTextureCount() const override { return texture_map.size(); }
6261
void PreloadTextures(TextureCallback callback, std::atomic<bool>* stop_flag) override;
63-
u8* LoadCustomTexture(u32 hash, int& width, int& height) override;
62+
u8* loadCustomTexture(u32 hash, int& width, int& height) override;
6463
bool isTextureReplaced(u32 hash) override final;
6564

6665

@@ -71,7 +70,7 @@ class CustomTextureSource : public BaseCustomTextureSource
7170
std::map<u32, std::string> texture_map;
7271
};
7372

74-
bool CustomTextureSource::LoadMap()
73+
bool CustomTextureSource::loadMap()
7574
{
7675
texture_map.clear();
7776
hostfs::DirectoryTree tree(textures_path);
@@ -98,10 +97,10 @@ void CustomTextureSource::PreloadTextures(TextureCallback callback, std::atomic<
9897
{
9998
for (auto const& [hash, path] : texture_map)
10099
{
101-
if (stop_flag != nullptr && stop_flag->load(std::memory_order_relaxed))
100+
if (stop_flag != nullptr && *stop_flag)
102101
return;
103102
int w, h;
104-
u8* data = LoadCustomTexture(hash, w, h);
103+
u8* data = loadCustomTexture(hash, w, h);
105104
if (data != nullptr)
106105
{
107106
size_t size = (size_t)w * h * 4;
@@ -116,7 +115,7 @@ void CustomTextureSource::PreloadTextures(TextureCallback callback, std::atomic<
116115
}
117116
}
118117

119-
u8* CustomTextureSource::LoadCustomTexture(u32 hash, int& width, int& height)
118+
u8* CustomTextureSource::loadCustomTexture(u32 hash, int& width, int& height)
120119
{
121120
auto it = texture_map.find(hash);
122121
if (it == texture_map.end())
@@ -173,60 +172,60 @@ std::string CustomTexture::getGameId()
173172
return game_id;
174173
}
175174

176-
bool CustomTexture::Init()
175+
bool CustomTexture::init()
177176
{
178177
if (!initialized)
179178
{
180-
stop_preload.store(false, std::memory_order_relaxed);
179+
stop_preload = false;
181180
resetPreloadProgress();
182-
pending_preloads.store(0, std::memory_order_relaxed);
181+
pending_preloads = 0;
183182
initialized = true;
184183

185184
std::string game_id = getGameId();
186185
if (game_id.length() > 0)
187186
{
188-
AddSource(std::make_unique<CustomTextureSource>(game_id));
187+
addSource(std::make_unique<CustomTextureSource>(game_id));
189188
}
190189
}
191190

192191
return loaderThread != nullptr;
193192
}
194193

195194
bool CustomTexture::enabled() {
196-
return Init();
195+
return loaderThread != nullptr;
197196
}
198197

199198
bool CustomTexture::preloaded() {
200-
return preload_total.load(std::memory_order_relaxed) > 0;
199+
return preload_total > 0;
201200
}
202201

203202
bool CustomTexture::isPreloading() {
204-
if (pending_preloads.load(std::memory_order_relaxed) > 0)
203+
if (pending_preloads > 0)
205204
return true;
206205

207206
int texLoaded = 0;
208207
int texTotal = 0;
209208
size_t loaded_size_b = 0;
210-
GetPreloadProgress(texLoaded, texTotal, loaded_size_b);
209+
getPreloadProgress(texLoaded, texTotal, loaded_size_b);
211210

212211
return (texTotal > 0 && texLoaded < texTotal);
213212
}
214213

215-
void CustomTexture::AddSource(std::unique_ptr<BaseCustomTextureSource> source)
214+
void CustomTexture::addSource(std::unique_ptr<BaseCustomTextureSource> source)
216215
{
217216
BaseCustomTextureSource* ptr = source.get();
218217
sources.emplace_back(std::move(source));
219218

220219
if (initialized)
221220
{
222-
if (!loaderThread && ptr->customTexturesAvailable())
221+
if (!loaderThread && ptr->shouldReplace())
223222
{
224223
loaderThread = std::make_unique<WorkerThread>("CustomTexLoader");
225224
}
226225
if (loaderThread)
227226
{
228227
if (ptr->shouldPreload())
229-
pending_preloads.fetch_add(1, std::memory_order_relaxed);
228+
pending_preloads++;
230229
loaderThread->run([this, ptr]() {
231230
prepareSource(ptr);
232231
});
@@ -235,29 +234,19 @@ void CustomTexture::AddSource(std::unique_ptr<BaseCustomTextureSource> source)
235234
}
236235

237236
CustomTexture::~CustomTexture() {
238-
Terminate();
237+
terminate();
239238
}
240239

241-
void CustomTexture::Terminate()
240+
void CustomTexture::terminate()
242241
{
243-
stop_preload.store(true, std::memory_order_relaxed);
242+
stop_preload = true;
244243
if (loaderThread)
245244
loaderThread->stop();
246245
loaderThread.reset();
247246
for (auto& source : sources)
248-
source->Terminate();
247+
source->terminate();
249248
sources.clear();
250-
if (!preloaded_textures.empty())
251-
{
252-
#ifndef LIBRETRO
253-
auto textures_to_free = std::make_shared<std::map<u32, TextureData>>(std::move(preloaded_textures));
254-
std::thread([textures_to_free]() {
255-
textures_to_free->clear();
256-
}).detach();
257-
#else
258-
preloaded_textures.clear();
259-
#endif
260-
}
249+
preloaded_textures.clear();
261250
resetPreloadProgress();
262251
initialized = false;
263252
}
@@ -282,7 +271,7 @@ u8* CustomTexture::loadTexture(u32 hash, int& width, int& height)
282271
auto& source = *it;
283272
if (source->shouldReplace())
284273
{
285-
u8* data = source->LoadCustomTexture(hash, width, height);
274+
u8* data = source->loadCustomTexture(hash, width, height);
286275
if (data != nullptr)
287276
return data;
288277
}
@@ -315,9 +304,9 @@ bool CustomTexture::isTextureReplaced(BaseTextureCacheData* texture)
315304
return false;
316305
}
317306

318-
void CustomTexture::LoadCustomTextureAsync(BaseTextureCacheData *texture_data)
307+
void CustomTexture::loadCustomTextureAsync(BaseTextureCacheData *texture_data)
319308
{
320-
if (!Init())
309+
if (!init())
321310
return;
322311

323312
texture_data->custom_load_in_progress++;
@@ -326,7 +315,7 @@ void CustomTexture::LoadCustomTextureAsync(BaseTextureCacheData *texture_data)
326315
});
327316
}
328317

329-
void CustomTexture::DumpTexture(BaseTextureCacheData* texture, int w, int h, void *src_buffer)
318+
void CustomTexture::dumpTexture(BaseTextureCacheData* texture, int w, int h, void *src_buffer)
330319
{
331320
if (!config::DumpReplacedTextures.get() && isTextureReplaced(texture))
332321
return;
@@ -463,47 +452,43 @@ void CustomTexture::DumpTexture(BaseTextureCacheData* texture, int w, int h, voi
463452

464453
void CustomTexture::prepareSource(BaseCustomTextureSource* source)
465454
{
466-
struct PreloadGuard {
467-
std::atomic<int>& counter;
468-
bool shouldPreload;
469-
~PreloadGuard() { if (shouldPreload) counter.fetch_sub(1, std::memory_order_relaxed); }
470-
} guard{ pending_preloads, source->shouldPreload() };
455+
bool should_preload = source->shouldPreload();
471456

472-
if (stop_preload.load(std::memory_order_relaxed))
473-
return;
474-
475-
if (source->LoadMap())
457+
if (!stop_preload && source->loadMap())
476458
{
477-
if (guard.shouldPreload)
459+
if (should_preload)
478460
{
479-
int count = static_cast<int>(source->GetTextureCount());
461+
int count = static_cast<int>(source->getTextureCount());
480462
if (count > 0)
481463
{
482-
preload_total.fetch_add(count, std::memory_order_relaxed);
464+
preload_total += count;
483465
auto callback = [this](u32 hash, TextureData&& data) {
484466
size_t size = data.data.size();
485467
preloaded_textures[hash] = std::move(data);
486-
preload_loaded.fetch_add(1, std::memory_order_relaxed);
487-
preload_loaded_size.fetch_add(size, std::memory_order_relaxed);
468+
preload_loaded++;
469+
preload_loaded_size += size;
488470
};
489471
source->PreloadTextures(callback, &stop_preload);
490472
}
491473
}
492474
}
475+
476+
if (should_preload)
477+
pending_preloads--;
493478
}
494479

495-
void CustomTexture::GetPreloadProgress(int& completed, int& total, size_t& loaded_size) const
480+
void CustomTexture::getPreloadProgress(int& completed, int& total, size_t& loaded_size) const
496481
{
497-
total = preload_total.load(std::memory_order_relaxed);
498-
if (total == 0 && pending_preloads.load(std::memory_order_relaxed) > 0)
482+
total = preload_total;
483+
if (total == 0 && pending_preloads > 0)
499484
total = -1; // Prints Preparing... in UI
500-
completed = preload_loaded.load(std::memory_order_relaxed);
501-
loaded_size = preload_loaded_size.load(std::memory_order_relaxed);
485+
completed = preload_loaded;
486+
loaded_size = preload_loaded_size;
502487
}
503488

504489
void CustomTexture::resetPreloadProgress()
505490
{
506-
preload_total.store(0, std::memory_order_relaxed);
507-
preload_loaded.store(0, std::memory_order_relaxed);
508-
preload_loaded_size.store(0, std::memory_order_relaxed);
491+
preload_total = 0;
492+
preload_loaded = 0;
493+
preload_loaded_size = 0;
509494
}

core/rend/CustomTexture.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,12 @@ class BaseCustomTextureSource
4040
using TextureCallback = std::function<void(u32 hash, TextureData&& data)>;
4141

4242
virtual ~BaseCustomTextureSource() { }
43-
virtual bool customTexturesAvailable() { return true; }
4443
virtual bool shouldReplace() const { return false; }
4544
virtual bool shouldPreload() const { return false; }
46-
virtual bool LoadMap() = 0;
47-
virtual size_t GetTextureCount() const { return 0; }
48-
virtual void Terminate() { }
49-
virtual u8* LoadCustomTexture(u32 hash, int& width, int& height) = 0;
45+
virtual bool loadMap() = 0;
46+
virtual size_t getTextureCount() const { return 0; }
47+
virtual void terminate() { }
48+
virtual u8* loadCustomTexture(u32 hash, int& width, int& height) = 0;
5049
virtual bool isTextureReplaced(u32 hash) = 0;
5150
virtual void PreloadTextures(TextureCallback callback, std::atomic<bool>* stop_flag) { }
5251
};
@@ -55,15 +54,15 @@ class CustomTexture
5554
{
5655
public:
5756
~CustomTexture();
58-
bool Init();
57+
bool init();
5958
bool enabled();
6059
bool preloaded();
6160
bool isPreloading();
62-
void AddSource(std::unique_ptr<BaseCustomTextureSource> source);
63-
void LoadCustomTextureAsync(BaseTextureCacheData *texture_data);
64-
void DumpTexture(BaseTextureCacheData* texture, int w, int h, void *src_buffer);
65-
void Terminate();
66-
void GetPreloadProgress(int& completed, int& total, size_t& loaded_size) const;
61+
void addSource(std::unique_ptr<BaseCustomTextureSource> source);
62+
void loadCustomTextureAsync(BaseTextureCacheData *texture_data);
63+
void dumpTexture(BaseTextureCacheData* texture, int w, int h, void *src_buffer);
64+
void terminate();
65+
void getPreloadProgress(int& completed, int& total, size_t& loaded_size) const;
6766

6867
private:
6968
u8* loadTexture(u32 hash, int& width, int& height);

core/rend/TexCache.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ bool BaseTextureCacheData::Update()
516516
size = originalSize;
517517
return true;
518518
}
519-
custom_texture.LoadCustomTextureAsync(this);
519+
custom_texture.loadCustomTextureAsync(this);
520520
}
521521

522522
void *temp_tex_buffer = NULL;
@@ -676,7 +676,7 @@ bool BaseTextureCacheData::Update()
676676
if (config::DumpTextures)
677677
{
678678
ComputeHash();
679-
custom_texture.DumpTexture(this, upscaled_w, upscaled_h, temp_tex_buffer);
679+
custom_texture.dumpTexture(this, upscaled_w, upscaled_h, temp_tex_buffer);
680680
NOTICE_LOG(RENDERER, "Dumped texture %x.png. Old hash %x", texture_hash, old_texture_hash);
681681
}
682682
PrintTextureName();

core/rend/TexCache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ class BaseTextureCache
263263
void Clear()
264264
{
265265
if (!custom_texture.preloaded())
266-
custom_texture.Terminate();
266+
custom_texture.terminate();
267267
for (auto& [id, texture] : cache)
268268
texture.Delete();
269269

core/ui/gui.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,7 @@ static void gui_display_loadscreen()
13881388
int texLoaded = 0;
13891389
int texTotal = 0;
13901390
size_t loaded_size_b = 0;
1391-
custom_texture.GetPreloadProgress(texLoaded, texTotal, loaded_size_b);
1391+
custom_texture.getPreloadProgress(texLoaded, texTotal, loaded_size_b);
13921392

13931393
ImGui::Text("%s", label);
13941394
float progress = 0;

core/ui/settings_video.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,18 @@ void gui_settings_video()
213213
OptionCheckbox("Full Framebuffer Emulation", config::EmulateFramebuffer,
214214
"Fully accurate VRAM framebuffer emulation. Helps games that directly access the framebuffer for special effects. "
215215
"Very slow and incompatible with upscaling and wide screen.");
216-
OptionCheckbox("Load Custom Textures", config::CustomTextures,
217-
"Load custom/high-res textures from data/textures/<game id>");
218-
ImGui::Indent();
219216
{
220-
DisabledScope scope(!config::CustomTextures.get() || game_started);
221-
OptionCheckbox("Preload Custom Textures", config::PreloadCustomTextures,
222-
"Preload custom textures at game start. May improve performance but increase memory usage");
217+
DisabledScope scope(game_started);
218+
OptionCheckbox("Load Custom Textures", config::CustomTextures,
219+
"Load custom/high-res textures from data/textures/<game id>");
220+
ImGui::Indent();
221+
{
222+
DisabledScope scope(!config::CustomTextures.get());
223+
OptionCheckbox("Preload Custom Textures", config::PreloadCustomTextures,
224+
"Preload custom textures at game start. May improve performance but increases memory usage");
225+
}
226+
ImGui::Unindent();
223227
}
224-
ImGui::Unindent();
225228
}
226229
ImGui::Spacing();
227230
header("Aspect Ratio");

0 commit comments

Comments
 (0)