From 70d551ab82edb83b785f01776a0f196392814596 Mon Sep 17 00:00:00 2001 From: MysterD Date: Tue, 27 Jun 2023 15:38:08 -0700 Subject: [PATCH] Clear texture 1 on level init (fixes corrupt level textures) --- data/dynos_mgr_lvl.cpp | 11 ++++++----- data/dynos_mgr_tex.cpp | 1 + src/game/level_update.c | 4 ++++ src/pc/gfx/gfx_pc.c | 11 +++++++++++ 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/data/dynos_mgr_lvl.cpp b/data/dynos_mgr_lvl.cpp index 00818d20..3345c6f1 100644 --- a/data/dynos_mgr_lvl.cpp +++ b/data/dynos_mgr_lvl.cpp @@ -39,11 +39,12 @@ void DynOS_Lvl_ModShutdown() { auto& _CustomLevelScripts = DynOS_Lvl_GetArray(); while (_CustomLevelScripts.Count() > 0) { - auto& pair = _CustomLevelScripts[0]; - DynOS_Tex_Invalid(pair.second); - Delete(pair.second); - free((void*)pair.first); - _CustomLevelScripts.Remove(0); + for (auto& pair : _CustomLevelScripts) { + DynOS_Tex_Invalid(pair.second); + Delete(pair.second); + free((void*)pair.first); + } + _CustomLevelScripts.Clear(); } auto& _OverrideLevelScripts = DynosOverrideLevelScripts(); diff --git a/data/dynos_mgr_tex.cpp b/data/dynos_mgr_tex.cpp index a07f790f..f7a197a6 100644 --- a/data/dynos_mgr_tex.cpp +++ b/data/dynos_mgr_tex.cpp @@ -379,6 +379,7 @@ void DynOS_Tex_Activate(DataNode* aNode, bool aCustomTexture) { void DynOS_Tex_Deactivate(DataNode* aNode) { if (!aNode) { return; } + aNode->mData->mUploaded = false; // remove from custom textures auto& _DynosCustomTexs = DynosCustomTexs(); diff --git a/src/game/level_update.c b/src/game/level_update.c index 55a5aa5e..b8a4ed74 100644 --- a/src/game/level_update.c +++ b/src/game/level_update.c @@ -1781,6 +1781,10 @@ s32 init_level(void) { } smlua_call_event_hooks(HOOK_ON_LEVEL_INIT); + // clear texture 1 on level init -- can linger and corrupt textures otherwise + extern u8 gGfxPcResetTex1; + gGfxPcResetTex1 = 1; + return 1; } diff --git a/src/pc/gfx/gfx_pc.c b/src/pc/gfx/gfx_pc.c index 8a6869ab..0bf0f711 100644 --- a/src/pc/gfx/gfx_pc.c +++ b/src/pc/gfx/gfx_pc.c @@ -63,6 +63,8 @@ #define HASHMAP_LEN (MAX_CACHED_TEXTURES * 2) #define HASH_MASK (HASHMAP_LEN - 1) +u8 gGfxPcResetTex1 = 0; + struct RGBA { uint8_t r, g, b, a; }; @@ -361,6 +363,10 @@ static struct ColorCombiner *gfx_lookup_or_create_color_combiner(struct CombineM return prev_combiner = comb; } +void gfx_texture_cache_clear(void) { + memset(&gfx_texture_cache, 0, sizeof(gfx_texture_cache)); +} + static bool gfx_texture_cache_lookup(int tile, struct TextureHashmapNode **n, const uint8_t *orig_addr, uint32_t fmt, uint32_t siz) { #ifdef EXTERNAL_DATA // hash and compare the data (i.e. the texture name) itself size_t hash = string_hash(orig_addr); @@ -1903,6 +1909,11 @@ struct GfxRenderingAPI *gfx_get_current_rendering_api(void) { } void gfx_start_frame(void) { + if (gGfxPcResetTex1 > 0) { + gGfxPcResetTex1--; + rdp.loaded_texture[1].addr = NULL; + rdp.loaded_texture[1].size_bytes = 0; + } gfx_wapi->handle_events(); gfx_wapi->get_dimensions(&gfx_current_dimensions.width, &gfx_current_dimensions.height); if (gfx_current_dimensions.height == 0) {