Skip to content

Commit 4c116f2

Browse files
FF7: Enable cached animated textures
Load them once per cycle and re-use them in the same scene, until the engine decides to unload the entire texture and all its animated states. Additionally bump vcpkg ffmpeg port version.
1 parent 738cf8d commit 4c116f2

File tree

6 files changed

+48
-12
lines changed

6 files changed

+48
-12
lines changed

src/common.cpp

+21-6
Original file line numberDiff line numberDiff line change
@@ -1047,10 +1047,22 @@ void common_unload_texture(struct texture_set *texture_set)
10471047
if(!VREF(texture_set, ogl.gl_set)) return;
10481048

10491049
for (uint32_t idx = 0; idx < VREF(texture_set, ogl.gl_set->textures); idx++)
1050+
{
1051+
if (VREF(texture_set, ogl.gl_set->is_animated))
1052+
{
1053+
// Destroy animated textures
1054+
for(std::map<uint64_t,uint32_t>::iterator it = VREF(texture_set, ogl.gl_set->animated_textures).begin(); it != VREF(texture_set, ogl.gl_set->animated_textures).end(); ++it) {
1055+
newRenderer.deleteTexture(it->second);
1056+
}
1057+
VREF(texture_set, ogl.gl_set->animated_textures).clear();
1058+
}
1059+
1060+
// Destroy original static texture
10501061
newRenderer.deleteTexture(VREF(texture_set, texturehandle[idx]));
1062+
}
10511063

10521064
external_free(VREF(texture_set, texturehandle));
1053-
external_free(VREF(texture_set, ogl.gl_set));
1065+
delete VREF(texture_set, ogl.gl_set);
10541066

10551067
VRASS(texture_set, texturehandle, 0);
10561068
VRASS(texture_set, ogl.gl_set, 0);
@@ -1111,7 +1123,7 @@ uint32_t load_external_texture(void* image_data, uint32_t dataSize, struct textu
11111123
{
11121124
if(trace_all || trace_loaders) ffnx_trace("texture file name: %s\n", VREF(tex_header, file.pc_name));
11131125

1114-
texture = load_texture(image_data, dataSize, VREF(tex_header, file.pc_name), VREF(tex_header, palette_index), VREFP(texture_set, ogl.width), VREFP(texture_set, ogl.height), gl_set->is_animated);
1126+
texture = load_texture(image_data, dataSize, VREF(tex_header, file.pc_name), VREF(tex_header, palette_index), VREFP(texture_set, ogl.width), VREFP(texture_set, ogl.height), gl_set);
11151127

11161128
if(!_strnicmp(VREF(tex_header, file.pc_name), "world", strlen("world") - 1)) gl_set->force_filter = true;
11171129

@@ -1139,10 +1151,13 @@ uint32_t load_external_texture(void* image_data, uint32_t dataSize, struct textu
11391151

11401152
if(texture)
11411153
{
1142-
gl_replace_texture(texture_set, VREF(tex_header, palette_index), texture);
1154+
if (!gl_set->is_animated)
1155+
{
1156+
gl_replace_texture(texture_set, VREF(tex_header, palette_index), texture);
11431157

1144-
if(!VREF(texture_set, ogl.external)) stats.external_textures++;
1145-
VRASS(texture_set, ogl.external, true);
1158+
if(!VREF(texture_set, ogl.external)) stats.external_textures++;
1159+
VRASS(texture_set, ogl.external, true);
1160+
}
11461161

11471162
return true;
11481163
}
@@ -1276,7 +1291,7 @@ struct texture_set *common_load_texture(struct texture_set *_texture_set, struct
12761291
}
12771292

12781293
// allocate space for our private data
1279-
if(!VREF(texture_set, ogl.gl_set)) VRASS(texture_set, ogl.gl_set, (gl_texture_set*)external_calloc(sizeof(struct gl_texture_set), 1));
1294+
if(!VREF(texture_set, ogl.gl_set)) VRASS(texture_set, ogl.gl_set, new gl_texture_set());
12801295

12811296
// texture handle array may not have been initialized
12821297
if(!VREF(texture_set, texturehandle))

src/gl.h

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ struct gl_texture_set
8787
uint32_t force_filter;
8888
uint32_t force_zsort;
8989
uint32_t is_animated;
90+
std::map<uint64_t, uint32_t> animated_textures;
91+
uint64_t current_animated_texture;
9092
uint32_t is_aspect_ratio_changed;
9193
};
9294

src/gl/texture.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,14 @@ void gl_bind_texture_set(struct texture_set *_texture_set)
9898
{
9999
VOBJ(tex_header, tex_header, VREF(texture_set, tex_header));
100100

101-
gl_set_texture(VREF(texture_set, texturehandle[VREF(tex_header, palette_index)]));
101+
struct gl_texture_set* gl_set = VREF(texture_set, ogl.gl_set);
102+
103+
if (gl_set->is_animated && VREF(texture_set, ogl.external))
104+
{
105+
gl_set_texture(gl_set->animated_textures[gl_set->current_animated_texture]);
106+
}
107+
else
108+
gl_set_texture(VREF(texture_set, texturehandle[VREF(tex_header, palette_index)]));
102109

103110
if(VREF(tex_header, version) == FB_TEX_VERSION) current_state.fb_texture = true;
104111
else current_state.fb_texture = false;

src/saveload.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,12 @@ uint32_t load_texture_helper(char* name, uint32_t* width, uint32_t* height, bool
106106
return ret;
107107
}
108108

109-
uint32_t load_texture(void* data, uint32_t dataSize, char* name, uint32_t palette_index, uint32_t* width, uint32_t* height, bool is_animated)
109+
uint32_t load_texture(void* data, uint32_t dataSize, char* name, uint32_t palette_index, uint32_t* width, uint32_t* height, struct gl_texture_set* gl_set)
110110
{
111111
uint32_t ret = 0;
112112
char filename[sizeof(basedir) + 1024]{ 0 };
113113
uint64_t hash;
114+
bool is_animated = gl_set->is_animated;
114115

115116
struct stat dummy;
116117

@@ -120,6 +121,13 @@ uint32_t load_texture(void* data, uint32_t dataSize, char* name, uint32_t palett
120121
{
121122
if (is_animated)
122123
{
124+
if (gl_set->animated_textures.count(hash))
125+
{
126+
// We already know the texture, return its handler and move on
127+
ret = gl_set->animated_textures[hash];
128+
break;
129+
}
130+
123131
_snprintf(filename, sizeof(filename), "%s/%s/%s_%02i_%llx.%s", basedir, mod_path.c_str(), name, palette_index, hash, mod_ext[idx].c_str());
124132

125133
if (stat(filename, &dummy) != 0)
@@ -138,6 +146,8 @@ uint32_t load_texture(void* data, uint32_t dataSize, char* name, uint32_t palett
138146

139147
if (!ret && trace_all) ffnx_warning("External texture [%s] found but not loaded due to memory limitations.\n", filename);
140148

149+
if (ret && is_animated ) gl_set->animated_textures[hash] = ret;
150+
141151
break;
142152
}
143153
else if (trace_all || show_missing_textures)
@@ -151,7 +161,7 @@ uint32_t load_texture(void* data, uint32_t dataSize, char* name, uint32_t palett
151161
if(palette_index != 0)
152162
{
153163
if(trace_all || show_missing_textures) ffnx_info("No external texture found, falling back to palette 0\n", basedir, mod_path.c_str(), name, palette_index);
154-
return load_texture(data, dataSize, name, 0, width, height, false);
164+
return load_texture(data, dataSize, name, 0, width, height, gl_set);
155165
}
156166
else
157167
{
@@ -164,5 +174,7 @@ uint32_t load_texture(void* data, uint32_t dataSize, char* name, uint32_t palett
164174
if (trace_all) ffnx_trace("Created external texture: %u from %s\n", ret, filename);
165175
}
166176

177+
if (is_animated) gl_set->current_animated_texture = hash;
178+
167179
return ret;
168180
}

src/saveload.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
#pragma once
2323

2424
void save_texture(void *data, uint32_t dataSize, uint32_t width, uint32_t height, uint32_t palette_index, char *name, bool is_animated);
25-
uint32_t load_texture(void *data, uint32_t dataSize, char *name, uint32_t palette_index, uint32_t *width, uint32_t *height, bool is_animated);
25+
uint32_t load_texture(void *data, uint32_t dataSize, char *name, uint32_t palette_index, uint32_t *width, uint32_t *height, struct gl_texture_set* gl_set);

vcpkg.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ffnx",
33
"version": "1.9.0",
4-
"builtin-baseline": "b361c2eefa3966cb7cec45275aff32e90430aaa6",
4+
"builtin-baseline": "111220b3cf3311744369425d5c323a0f17941076",
55
"dependencies": [
66
"bgfx",
77
{
@@ -46,7 +46,7 @@
4646
{
4747
"name": "ffmpeg",
4848
"version": "4.4",
49-
"port-version": 6
49+
"port-version": 9
5050
},
5151
{
5252
"name": "imgui",

0 commit comments

Comments
 (0)