From 5f5b0da3231732f5985ebfa226609ea0049d7aa1 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Sat, 21 Nov 2026 19:34:59 -0700 Subject: [PATCH] Handhelds: the later levels fit (only used textures, level freed before the next cutscene, smaller renderer and task state) On the ESP32-S3-BOX, JABSHIP (mission 10) could not be loaded at all (out of memory loading KELL.WAX), and finishing NARSHADA crashed in the next mission's cutscene. With TFE_ESPBOX: - Only the textures the sectors and walls use are loaded, once they are read (level_loadUsedTextures()): a level's list can name many it never draws (JABSHIP lists 359 and draws 51: 1.1MB). The other slots stay null like ; INF only copies textures from one sector or wall to another, and old save states (which list every texture) still load. - At mission end the level is freed before the next mode starts, not after: the next mission's cutscene (jabba1: ~1MB) no longer has to fit next to the level that just ended (NARSHADA: 1.7MB). - adjoinEdgeList has MAX_ADJOIN_SEG entries: wall_addAdjoinSegment() keeps the index below that for the whole frame (-253KB of renderer state). - Task stacks are 2KB: they hold the local contexts of up to 16 nested task functions, none over 64 bytes (-96KB per chunk of 16 tasks; ctxAllocate() still asserts that they fit). - Loading a save state frees the loaders' read buffers too, as a level load does (espbox_free_loader_scratch()). On the box, with the code overlay (PSRAM free): - Talay 1275 KB -> 1778 KB, render time unchanged (13.0 ms) - NARSHADA 1080 KB -> 1431 KB; finishing it with LASKIP plays jabba1 and loads JABSHIP (1329 KB free), where it crashed before - JABSHIP directly (warp from Talay, or from the agent menu): crashed -> 1762 KB free, 51 of 359 textures loaded - ARC (mission 14) 937 KB free; a save made in ARC loads back in ARC (795 KB free) and from the title (925 KB) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY --- tfe/TFE_DarkForces/darkForcesMain.cpp | 23 ++++ tfe/TFE_Jedi/Level/level.cpp | 122 ++++++++++++++---- .../RClassic_Fixed/rclassicFixedSharedState.h | 7 + tfe/TFE_Jedi/Task/task.cpp | 7 +- 4 files changed, 131 insertions(+), 28 deletions(-) diff --git a/tfe/TFE_DarkForces/darkForcesMain.cpp b/tfe/TFE_DarkForces/darkForcesMain.cpp index d95fc9f..d2e9dc7 100644 --- a/tfe/TFE_DarkForces/darkForcesMain.cpp +++ b/tfe/TFE_DarkForces/darkForcesMain.cpp @@ -61,6 +61,10 @@ using namespace TFE_Memory; using namespace TFE_Input; +#ifdef TFE_ESPBOX +void espbox_free_loader_scratch(); // TFE_Jedi/Level/level.cpp +#endif + namespace TFE_DarkForces { ///////////////////////////////////////////// @@ -666,6 +670,19 @@ namespace TFE_DarkForces handleLevelComplete(); } +#ifdef TFE_ESPBOX + // The level is freed before what comes next starts (here, as in the + // original order below, nothing uses it any more): the next mission's + // cutscene (up to ~1MB) does not fit next to a big level on the + // handhelds (finishing NARSHADA ran out of memory in 'jabba1'). + region_clear(s_levelRegion); + bitmap_clearLevelData(); + bitmap_setAllocator(s_gameRegion); + level_freeAllAssets(); + TFE_A11Y::clearCaptions(); + + startNextMode(); +#else startNextMode(); region_clear(s_levelRegion); @@ -673,6 +690,7 @@ namespace TFE_DarkForces bitmap_setAllocator(s_gameRegion); level_freeAllAssets(); TFE_A11Y::clearCaptions(); +#endif } } break; } @@ -1371,6 +1389,11 @@ namespace TFE_DarkForces if (!writeState) { agent_restartEndLevelTask(); +#ifdef TFE_ESPBOX + // As after a level load: the loaders' read buffers keep the size of the largest + // file they read (up to ~0.7MB) and are not needed while playing. + espbox_free_loader_scratch(); +#endif } time_pause(JFALSE); diff --git a/tfe/TFE_Jedi/Level/level.cpp b/tfe/TFE_Jedi/Level/level.cpp index 1942e1d..4a50c97 100644 --- a/tfe/TFE_Jedi/Level/level.cpp +++ b/tfe/TFE_Jedi/Level/level.cpp @@ -1,5 +1,7 @@ #include #include +#include +#include #include "level.h" #include "levelData.h" @@ -41,6 +43,15 @@ void espbox_free_sprite_scratch(); void espbox_free_level_scratch(); void espbox_free_inf_scratch(); void espbox_free_model_scratch(); +// All of the loaders' scratch read buffers (after a level or a save state is loaded). +void espbox_free_loader_scratch() +{ + espbox_free_rtexture_scratch(); + espbox_free_sprite_scratch(); + espbox_free_level_scratch(); + espbox_free_inf_scratch(); + espbox_free_model_scratch(); +} #endif namespace TFE_Jedi @@ -85,11 +96,7 @@ namespace TFE_Jedi inf_load(levelName); #ifdef TFE_ESPBOX // The loaders' scratch read buffers (~0.8MB) are not needed while playing. - espbox_free_rtexture_scratch(); - espbox_free_sprite_scratch(); - espbox_free_level_scratch(); - espbox_free_inf_scratch(); - espbox_free_model_scratch(); + espbox_free_loader_scratch(); #endif level_loadGoals(levelName); @@ -113,6 +120,71 @@ namespace TFE_Jedi s_palModified = JTRUE; } + // Loads the level texture of slot `index` (and the base copy kept for serialization). + static bool level_loadTexture(s32 index, const char* textureName) + { + TextureData* tex = bitmap_load(textureName, 1); + if (!tex) + { + TFE_System::logWrite(LOG_WARNING, "level_loadGeometry", "Could not open '%s', using 'default.bm' instead.", textureName); + tex = bitmap_load("default.bm", 1); + if (!tex) + { + TFE_System::logWrite(LOG_ERROR, "level_loadGeometry", "'default.bm' is not a valid BM file!"); + assert(0); + return false; + } + } + TextureData** texture = &s_levelState.textures[index]; + *texture = tex; + // This version never gets modified, so serialization is simpler. + s_levelState.textures[s_levelState.textureCount + index] = tex; + + // Setup an animated texture. + if (tex->uvWidth == BM_ANIMATED_TEXTURE) + { + bitmap_setupAnimatedTexture(texture, index); + } + return true; + } + +#ifdef TFE_ESPBOX + // Loads the textures (named in the level's list) that a sector or a wall uses. + static bool level_loadUsedTextures(const std::vector& names) + { + const s32 count = s_levelState.textureCount; + std::vector used(count, 0); + auto mark = [&](TextureData** slot) + { + const ptrdiff_t index = slot ? slot - s_levelState.textures : -1; + if (index >= 0 && index < count) { used[index] = 1; } + }; + for (s32 s = 0; s < s_levelState.sectorCount; s++) + { + const RSector* sector = &s_levelState.sectors[s]; + mark(sector->floorTex); + mark(sector->ceilTex); + for (s32 w = 0; w < sector->wallCount; w++) + { + const RWall* wall = §or->walls[w]; + mark(wall->midTex); + mark(wall->topTex); + mark(wall->botTex); + mark(wall->signTex); + } + } + s32 loaded = 0; + for (s32 i = 0; i < count; i++) + { + if (!used[i] || names[i].empty()) { continue; } + if (!level_loadTexture(i, names[i].c_str())) { return false; } + loaded++; + } + TFE_System::logWrite(LOG_MSG, "level_loadGeometry", "Loaded the %d of %d listed textures the level uses.", loaded, count); + return true; + } +#endif + JBool level_loadGeometry(const char* levelName) { s_levelState.secretCount = 0; @@ -217,6 +289,13 @@ namespace TFE_Jedi memset(s_levelState.textures, 0, 2 * s_levelState.textureCount * sizeof(TextureData**)); // Load Textures. + // TFE_ESPBOX: only the textures the sectors and walls use are loaded, after they are read + // (level_loadUsedTextures() below): a level's list can name many it never draws (JABSHIP + // lists 359 and draws 51: 1.1MB of unused bitmaps). The others' slots stay null, like + // ; INF only copies textures from one sector or wall to another. +#ifdef TFE_ESPBOX + std::vector textureNames(s_levelState.textureCount); +#endif TextureData** texture = s_levelState.textures; TextureData** texBase = s_levelState.textures + s_levelState.textureCount; for (s32 i = 0; i < s_levelState.textureCount; i++, texture++, texBase++) @@ -226,7 +305,11 @@ namespace TFE_Jedi if (sscanf(line, " TEXTURE: %s ", textureName) != 1) { TFE_System::logWrite(LOG_ERROR, "level_loadGeometry", "Cannot read texture name."); +#ifdef TFE_ESPBOX + textureNames[i] = "default.bm"; +#else *texture = bitmap_load("default.bm", 1); +#endif } else if (strcasecmp(textureName, "") == 0) { @@ -234,27 +317,11 @@ namespace TFE_Jedi } else { - TextureData* tex = bitmap_load(textureName, 1); - if (!tex) - { - TFE_System::logWrite(LOG_WARNING, "level_loadGeometry", "Could not open '%s', using 'default.bm' instead.", textureName); - tex = bitmap_load("default.bm", 1); - if (!tex) - { - TFE_System::logWrite(LOG_ERROR, "level_loadGeometry", "'default.bm' is not a valid BM file!"); - assert(0); - return false; - } - } - *texture = tex; - // This version never gets modified, so serialization is simpler. - *texBase = tex; - - // Setup an animated texture. - if (tex->uvWidth == BM_ANIMATED_TEXTURE) - { - bitmap_setupAnimatedTexture(texture, i); - } +#ifdef TFE_ESPBOX + textureNames[i] = textureName; +#else + if (!level_loadTexture(i, textureName)) { return false; } +#endif } } @@ -571,6 +638,9 @@ namespace TFE_Jedi s_levelState.controlSector->id = s_levelState.sectorCount; s_levelState.controlSector->index = s_levelState.controlSector->id; +#ifdef TFE_ESPBOX + if (!level_loadUsedTextures(textureNames)) { return false; } +#endif return true; } diff --git a/tfe/TFE_Jedi/Renderer/RClassic_Fixed/rclassicFixedSharedState.h b/tfe/TFE_Jedi/Renderer/RClassic_Fixed/rclassicFixedSharedState.h index a1be430..9bd66b5 100644 --- a/tfe/TFE_Jedi/Renderer/RClassic_Fixed/rclassicFixedSharedState.h +++ b/tfe/TFE_Jedi/Renderer/RClassic_Fixed/rclassicFixedSharedState.h @@ -63,7 +63,14 @@ namespace TFE_Jedi EdgePairFixed* flatEdge; EdgePairFixed flatEdgeList[MAX_SEG]; EdgePairFixed* adjoinEdge; +#ifdef TFE_ESPBOX + // Indexed by s_adjoinSegCount, which wall_addAdjoinSegment() keeps below MAX_ADJOIN_SEG + // for the whole frame (each sector's edges start at the count, which never goes down + // within a frame): MAX_ADJOIN_SEG entries are all it uses (-253KB). + EdgePairFixed adjoinEdgeList[MAX_ADJOIN_SEG]; +#else EdgePairFixed adjoinEdgeList[MAX_ADJOIN_SEG * MAX_ADJOIN_DEPTH]; +#endif RWallSegmentFixed wallSegListDst[MAX_SEG]; RWallSegmentFixed wallSegListSrc[MAX_SEG]; diff --git a/tfe/TFE_Jedi/Task/task.cpp b/tfe/TFE_Jedi/Task/task.cpp index 56e5ba6..edbd08d 100644 --- a/tfe/TFE_Jedi/Task/task.cpp +++ b/tfe/TFE_Jedi/Task/task.cpp @@ -73,8 +73,11 @@ namespace TFE_Jedi TASK_PREALLOCATED_CHUNKS = 1, #ifdef TFE_ESPBOX - TASK_STACK_SIZE = 8 * 1024, // 8KB of (local context) stack memory per task. - TASK_STACK_CHUNK_SIZE = 16, // 128KB of memory per chunk of 16 tasks with stack memory. + // The stack holds a task's local contexts, one per recursion level (at most + // TASK_MAX_LEVELS): none has an array, the largest is 64 bytes, so 1KB at most. + // (ctxAllocate() asserts that it fits.) + TASK_STACK_SIZE = 2 * 1024, // 2KB of (local context) stack memory per task. + TASK_STACK_CHUNK_SIZE = 16, // 32KB of memory per chunk of 16 tasks with stack memory. #else TASK_STACK_SIZE = 32 * 1024, // 32KB of stack memory. TASK_STACK_CHUNK_SIZE = 64, // 2MB of memory for 64 tasks with stack memory.