Skip to content
This repository was archived by the owner on Jul 10, 2023. It is now read-only.

Commit 344d9b4

Browse files
committed
Move customtexture initialization to the mod system. Split off texture dumping since it's a rather separate feature that doesn't share any code with the loader.
1 parent ddaf757 commit 344d9b4

13 files changed

+198
-154
lines changed

CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ configure_file(${reicast_core_path}/version.h.in ${reicast_core_path}/version.h
2626

2727

2828

29-
3029
## reicast build modules #
3130
#
3231

@@ -53,9 +52,9 @@ file(GLOB imgread_SRCS ${d_core}/imgread/*.cpp ${d_core}/imgread/*.h)
5352
file(GLOB profiler_SRCS ${d_core}/profiler/*.cpp ${d_core}/profiler/*.h)
5453
file(GLOB archive_SRCS ${d_core}/archive/*.cpp ${d_core}/archive/*.h)
5554
file(GLOB glwrap_SRCS ${d_core}/glwrap/*.cpp ${d_core}/glwrap/*.h)
56-
file(GLOB scripting_SRCS ${d_core}/scripting/*.cpp ${d_core}/scripting/*.h)
57-
file(GLOB utils_SRCS ${d_core}/utils/*.cpp ${d_core}/utils/*.h)
58-
file(GLOB gui_SRCS ${d_core}/gui/*.cpp ${d_core}/gui/*.h)
55+
file(GLOB mods_SRCS ${d_core}/mods/*.cpp ${d_core}/mods/*.h)
56+
file(GLOB utils_SRCS ${d_core}/utils/*.cpp ${d_core}/utils/*.h)
57+
file(GLOB gui_SRCS ${d_core}/gui/*.cpp ${d_core}/gui/*.h)
5958
file(GLOB wgl_SRCS ${d_core}/utils/glinit/wgl/*.cpp )
6059

6160
#### option(rend)
@@ -79,7 +78,7 @@ set(core_SRCS
7978
${reios_SRCS}
8079
${imgread_SRCS}
8180
${profiler_SRCS}
82-
${scripting_SRCS}
81+
${mods_SRCS}
8382
${utils_SRCS}
8483
${gui_SRCS}
8584
${wgl_SRCS}

libswirl/hw/pvr/Renderer_if.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "deps/crypto/md5.h"
1010

11-
#include "scripting/mods.h"
11+
#include "mods/mods.h"
1212

1313
#if FEAT_HAS_NIXPROF
1414
#include "profiler/profiler.h"

libswirl/libswirl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "profiler/profiler.h"
2626
#include "input/gamepad_device.h"
2727
#include "rend/TexCache.h"
28-
#include "scripting/mods.h"
28+
#include "mods/mods.h"
2929

3030
#define fault_printf(...)
3131

libswirl/mods/CustomTexture.cpp

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
Copyright 2018 flyinghead
3+
4+
This file is part of reicast.
5+
6+
reicast is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 2 of the License, or
9+
(at your option) any later version.
10+
11+
reicast is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with reicast. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
#include "CustomTexture.h"
20+
21+
#include <algorithm>
22+
#include <sstream>
23+
#include <sys/types.h>
24+
#include <sys/stat.h>
25+
#ifdef _MSC_VER
26+
#include "dirent/dirent.h"
27+
#else
28+
#include <dirent.h>
29+
#endif
30+
31+
#include "deps/libpng/png.h"
32+
#include "reios/reios.h"
33+
34+
#include "hw/pvr/Renderer_if.h"
35+
36+
#include "mods/mods.h"
37+
38+
void CustomTexture::LoaderThread()
39+
{
40+
while (initialized)
41+
{
42+
TextureCacheData *texture;
43+
44+
do {
45+
texture = NULL;
46+
47+
work_queue_mutex.Lock();
48+
if (!work_queue.empty())
49+
{
50+
texture = work_queue.back();
51+
work_queue.pop_back();
52+
}
53+
work_queue_mutex.Unlock();
54+
55+
if (texture != NULL)
56+
{
57+
texture->ComputeHash();
58+
if (texture->custom_image_data != NULL)
59+
{
60+
delete [] texture->custom_image_data;
61+
texture->custom_image_data = NULL;
62+
}
63+
if (!texture->dirty)
64+
{
65+
int width, height;
66+
u8 *image_data = LoadCustomTexture(texture->texture_hash, width, height);
67+
if (image_data == NULL)
68+
{
69+
image_data = LoadCustomTexture(texture->old_texture_hash, width, height);
70+
}
71+
if (image_data != NULL)
72+
{
73+
texture->custom_width = width;
74+
texture->custom_height = height;
75+
texture->custom_image_data = image_data;
76+
}
77+
}
78+
texture->custom_load_in_progress--;
79+
}
80+
81+
} while (texture != NULL);
82+
83+
wakeup_thread.Wait();
84+
}
85+
}
86+
87+
bool CustomTexture::Init(std::string path)
88+
{
89+
if (!initialized)
90+
{
91+
initialized = true;
92+
custom_textures_available = false;
93+
#ifndef TARGET_NO_THREADS
94+
textures_path = path + "/textures";
95+
96+
DIR* dir = opendir(textures_path.c_str());
97+
if (dir != NULL)
98+
{
99+
printf("Found custom textures directory: %s\n", textures_path.c_str());
100+
custom_textures_available = true;
101+
closedir(dir);
102+
loader_thread.Start();
103+
}
104+
#endif
105+
}
106+
return custom_textures_available;
107+
}
108+
109+
void CustomTexture::Terminate()
110+
{
111+
if (initialized)
112+
{
113+
initialized = false;
114+
custom_textures_available = false;
115+
work_queue_mutex.Lock();
116+
work_queue.clear();
117+
work_queue_mutex.Unlock();
118+
wakeup_thread.Set();
119+
#ifndef TARGET_NO_THREADS
120+
loader_thread.WaitToEnd();
121+
#endif
122+
}
123+
}
124+
125+
u8* CustomTexture::LoadCustomTexture(u32 hash, int& width, int& height)
126+
{
127+
if (unknown_hashes.find(hash) != unknown_hashes.end())
128+
return NULL;
129+
std::stringstream path;
130+
path << textures_path << std::hex << hash << ".png";
131+
132+
u8 *image_data = loadPNGData(path.str(), width, height);
133+
if (image_data == NULL)
134+
unknown_hashes.insert(hash);
135+
136+
return image_data;
137+
}
138+
139+
void CustomTexture::LoadCustomTextureAsync(TextureCacheData *texture_data)
140+
{
141+
if (!custom_textures_available)
142+
return;
143+
144+
texture_data->custom_load_in_progress++;
145+
work_queue_mutex.Lock();
146+
work_queue.insert(work_queue.begin(), texture_data);
147+
work_queue_mutex.Unlock();
148+
wakeup_thread.Set();
149+
}
150+
151+
CustomTexture custom_texture;
152+
153+
static bool customtexture_detect(std::string path) {
154+
return custom_texture.Init(path);
155+
}
156+
157+
static void customtexture_close() {
158+
custom_texture.Terminate();
159+
}
160+
161+
static mod_handlers customtexturemod = {
162+
customtexture_detect,
163+
customtexture_close,
164+
nullptr
165+
};
166+
static bool registered = mod_handler_register(customtexturemod);

libswirl/rend/gles/CustomTexture.h renamed to libswirl/mods/CustomTexture.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
#include <string>
2424
#include <set>
25-
#include "gles.h"
25+
#include "types.h"
26+
#include "rend/gles/gles.h"
2627

2728
class CustomTexture {
2829
public:
@@ -35,13 +36,12 @@ class CustomTexture {
3536
~CustomTexture() { Terminate(); }
3637
u8* LoadCustomTexture(u32 hash, int& width, int& height);
3738
void LoadCustomTextureAsync(TextureCacheData *texture_data);
38-
void DumpTexture(u32 hash, int w, int h, GLuint textype, void *temp_tex_buffer);
3939

40-
private:
41-
bool Init();
40+
bool Init(std::string path);
4241
void Terminate();
42+
43+
private:
4344
void LoaderThread();
44-
std::string GetGameId();
4545

4646
static void *loader_thread_func(void *param) { ((CustomTexture *)param)->LoaderThread(); return NULL; }
4747

@@ -57,4 +57,6 @@ class CustomTexture {
5757
cMutex work_queue_mutex;
5858
};
5959

60+
extern CustomTexture custom_texture;
61+
6062
#endif /* CORE_REND_GLES_CUSTOMTEXTURE_H_ */
File renamed without changes.
File renamed without changes.

libswirl/scripting/lua_loader.cpp renamed to libswirl/mods/lua_loader.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,4 @@ static mod_handlers luamod = {
164164
luabindings_onstop,
165165
luabindings_onreset
166166
};
167-
#ifdef SCRIPTING
168167
static bool registered = mod_handler_register(luamod);
169-
#endif

libswirl/scripting/mods.cpp renamed to libswirl/mods/mods.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
std::string mods_path;
1010
std::vector<mod_handlers>* modhandlers;
1111

12-
static std::string _getGameId()
12+
std::string get_game_id()
1313
{
1414
std::string game_id = reios_product_number;
1515
const size_t str_end = game_id.find_last_not_of(" ");
@@ -39,7 +39,7 @@ void modpack_detect_mods(std::string path)
3939

4040
void modpack_gamestart()
4141
{
42-
std::string game_id = _getGameId();
42+
std::string game_id = get_game_id();
4343
if (game_id.length() > 0)
4444
{
4545
mods_path = get_readonly_data_path(DATA_PATH) + "mods/" + game_id + "/";

libswirl/scripting/mods.h renamed to libswirl/mods/mods.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ void modpack_close();
2424
void modpack_onframe();
2525
void modpack_onstart();
2626
void modpack_onstop();
27-
void modpack_onreset();
27+
void modpack_onreset();
28+
29+
std::string get_game_id();

libswirl/rend/gles/glestex.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
#include "hw/mem/_vmem.h"
66
#include "deps/libpng/png.h"
77
#include "deps/xxhash/xxhash.h"
8-
#include "CustomTexture.h"
8+
#include "mods/CustomTexture.h"
9+
#include "texture_dump.h"
910

1011
/*
1112
Textures
@@ -73,8 +74,6 @@ const u32 MipPoint[8] =
7374
const GLuint PAL_TYPE[4]=
7475
{GL_UNSIGNED_SHORT_5_5_5_1,GL_UNSIGNED_SHORT_5_6_5,GL_UNSIGNED_SHORT_4_4_4_4, GL_UNSIGNED_BYTE};
7576

76-
CustomTexture custom_texture;
77-
7877
static void dumpRtTexture(u32 name, u32 w, u32 h) {
7978
char sname[256];
8079
sprintf(sname, "texdump/%x-%d.png", name, FrameCount);
@@ -379,7 +378,7 @@ void TextureCacheData::Update()
379378
if (settings.rend.DumpTextures)
380379
{
381380
ComputeHash();
382-
custom_texture.DumpTexture(texture_hash, upscaled_w, upscaled_h, textype, temp_tex_buffer);
381+
gles_dump_texture(texture_hash, upscaled_w, upscaled_h, textype, temp_tex_buffer);
383382
}
384383
}
385384
else {

0 commit comments

Comments
 (0)