From 09a570dff8b266e082a28b7bec20b2d50d380ca6 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Fri, 20 Nov 2026 16:58:36 -0600 Subject: [PATCH 1/2] JK: allocate the engine's largest arrays at launch, sizes in Kconfig (~1 MB less static PSRAM) Every core's .bss is summed into static PSRAM, so Jedi Knight's 1.31 MB of statics was reserved in every other game. The engine (OpenJKDF2 bump to esp32/alloc-arrays-at-start) now keeps its largest arrays as pointers: jk.cpp allocates them in one zeroed block from the engine heap right after the ROM arena is set up, before the engine task starts (a launch without the memory fails cleanly: the cart quits), and frees them in deinit() once the engine and audio tasks are gone. Each launch gets fresh zeroed buffers. - per launch (362 KB at the defaults): sithAI_actors, jkPlayer_playerInfos, jkPlayer_otherThings, jkPlayer_aMotsInfos, aEntryStack, sithCollision_aCollisions, sithSurface_aSurfaces, the port's directory cache - gone: the active-edge rasterizer pools (616 KB, unused with the z-buffered rasterizer) and sithSound's bounce buffer (16 KB, unused on ESP32) generated/globals.{c,h} declare the five moved globals as pointers under PLATFORM_SESSION_ARRAYS, as the updated cog templates generate them. Kconfig "Engine table sizes": AI actors (128), directory cache entries (512), active-edge faces / spans (1024 / 2048, not allocated with the z-buffered rasterizer), passed to the engine as compile definitions. .ext_ram.bss: 2,197,512 -> 1,179,912 bytes; JK's share 1,338,411 -> 320,851. overlay_image.py check 2c skips merged string sections (.str1.N): text holds no addresses, and with the smaller .ext_ram.bss the overlay moved to where two string words in esp_stdio's .rodata happened to fall (false positive). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY --- components/jk/CMakeLists.txt | 6 +++++ components/jk/Kconfig | 44 +++++++++++++++++++++++++++++++ components/jk/OpenJKDF2 | 2 +- components/jk/generated/globals.c | 40 ++++++++++++++++++++++++++++ components/jk/generated/globals.h | 20 ++++++++++++++ components/jk/include/jk_esp.h | 8 ++++++ components/jk/src/jk.cpp | 15 +++++++++++ tools/overlay_image.py | 2 ++ 8 files changed, 136 insertions(+), 1 deletion(-) diff --git a/components/jk/CMakeLists.txt b/components/jk/CMakeLists.txt index ef48b4d..29c99df 100644 --- a/components/jk/CMakeLists.txt +++ b/components/jk/CMakeLists.txt @@ -86,6 +86,12 @@ if(CONFIG_JK_ENGINE) OPENJKDF2_RELEASE_COMMIT_SHORT="esp32" OPENJKDF2_RELEASE_COMMIT_U=u"esp32" OPENJKDF2_RELEASE_COMMIT_SHORT_U=u"esp32" + # table sizes (Kconfig "Engine table sizes"); the engine allocates these + # per session (PLATFORM_SESSION_ARRAYS, Platform/ESP32/jk_esp_arrays.c) + SITHAI_MAX_ACTORS=${CONFIG_JK_SITHAI_MAX_ACTORS} + JK_ESP_FS_DIRCACHE=${CONFIG_JK_FS_DIR_CACHE_ENTRIES} + RDACTIVE_MAX_FACES=${CONFIG_JK_RDACTIVE_MAX_FACES} + RDACTIVE_MAX_SPANS=${CONFIG_JK_RDACTIVE_MAX_SPANS} ) # Engine-only flags (the C++ glue in src/ keeps the project defaults) set(ENGINE_COMMON_FLAGS diff --git a/components/jk/Kconfig b/components/jk/Kconfig index 6ab867a..f9e5306 100644 --- a/components/jk/Kconfig +++ b/components/jk/Kconfig @@ -50,4 +50,48 @@ menu "Jedi Knight (OpenJKDF2)" int "Audio mixer sample rate" default 22050 + menu "Engine table sizes" + depends on JK_ENGINE + comment "Allocated when Jedi Knight starts and freed when it quits" + + config JK_SITHAI_MAX_ACTORS + int "AI actors per level (SITHAI_MAX_ACTORS)" + range 32 1024 + default 128 + help + Entries in the engine's AI actor table, 672 bytes each (128 = + 84 KB). A level with more AI-driven things than this loads the + rest without AI (they stand still). The PC engine uses 256. + + config JK_FS_DIR_CACHE_ENTRIES + int "Directory cache entries (JK_ESP_FS_DIRCACHE)" + range 16 4096 + default 512 + help + The port remembers whether each directory it probed on the SD + card exists, so the engine's many file lookups do not repeat + FatFs directory scans: 129 bytes per entry (512 = 64.5 KB). + When it is full, new directories are asked of the card each + time (slower level loads, nothing else). + + config JK_RDACTIVE_MAX_FACES + int "Active-edge rasterizer faces (RDACTIVE_MAX_FACES)" + range 256 8192 + default 1024 + help + Face pool of the engine's active-edge (painter's) software + rasterizer, 512 bytes per face. This port draws the world with + the z-buffered rasterizer (RDRASTER_SW_ZBUFFER), so the pool is + not allocated at all; the size only matters for an engine built + without RDRASTER_SW_ZBUFFER. + + config JK_RDACTIVE_MAX_SPANS + int "Active-edge rasterizer spans (RDACTIVE_MAX_SPANS)" + range 256 16384 + default 2048 + help + Span pool of the same rasterizer, 52 bytes per span (not + allocated with RDRASTER_SW_ZBUFFER, see above). + endmenu + endmenu diff --git a/components/jk/OpenJKDF2 b/components/jk/OpenJKDF2 index fd47e34..36de50d 160000 --- a/components/jk/OpenJKDF2 +++ b/components/jk/OpenJKDF2 @@ -1 +1 @@ -Subproject commit fd47e3425aa22f5c2b983ac19e6d8894090aff51 +Subproject commit 36de50d93bd39e0814e6192ae74df4ea5272d796 diff --git a/components/jk/generated/globals.c b/components/jk/generated/globals.c index 94f2467..8eaec34 100644 --- a/components/jk/generated/globals.c +++ b/components/jk/generated/globals.c @@ -284,7 +284,11 @@ int sith_bOpen MAYBE_UNUSED = 0; int sithSurface_numAvail MAYBE_UNUSED = 0; int sithSurface_aAvail[256+1] MAYBE_UNUSED = {0}; int sithSurface_numSurfaces MAYBE_UNUSED = 0; +#ifdef PLATFORM_SESSION_ARRAYS // Added: allocated for each engine session +rdSurface* sithSurface_aSurfaces MAYBE_UNUSED = NULL; +#else rdSurface sithSurface_aSurfaces[256] MAYBE_UNUSED = {0}; +#endif int sithSurface_bOpened MAYBE_UNUSED = 0; uint32_t sithSurface_byte_8EE668 MAYBE_UNUSED = 0; int sithSurface_numUnsyncedSurfaces MAYBE_UNUSED = 0; @@ -669,11 +673,19 @@ flex32_t sithWeapon_8BD060 MAYBE_UNUSED = 0.0f; int sithWeapon_8BD008[6] MAYBE_UNUSED = {0}; int sithWeapon_8BD024 MAYBE_UNUSED = 0; int sithWeapon_senderIndex MAYBE_UNUSED = 0; +#ifdef PLATFORM_SESSION_ARRAYS // Added: allocated for each engine session +SithPlayer* jkPlayer_playerInfos MAYBE_UNUSED = NULL; +#else SithPlayer jkPlayer_playerInfos[JKPLAYER_NUM_INFOS] MAYBE_UNUSED = {0}; +#endif char16_t jkPlayer_playerShortName[32] MAYBE_UNUSED = {0}; int jkPlayer_numOtherThings MAYBE_UNUSED = 0; int jkPlayer_numThings MAYBE_UNUSED = 0; +#ifdef PLATFORM_SESSION_ARRAYS // Added: allocated for each engine session +jkPlayerInfo* jkPlayer_otherThings MAYBE_UNUSED = NULL; +#else jkPlayerInfo jkPlayer_otherThings[NUM_JKPLAYER_THINGS] MAYBE_UNUSED = {0}; +#endif int jkPlayer_bLoadingSomething MAYBE_UNUSED = 1; int playerThingIdx MAYBE_UNUSED = 0; uint32_t jkPlayer_maxPlayers MAYBE_UNUSED = {0}; @@ -705,7 +717,11 @@ char jkPlayer_tipMat[32] MAYBE_UNUSED = {0}; int sithCollision_aNumSearchedSectors[4] MAYBE_UNUSED = {0}; SithCollideResult sithCollision_collisionHandlers[144] MAYBE_UNUSED = {0}; sithCollisionHitHandler_t sithCollision_aThingSurfaceCollideResults[12] MAYBE_UNUSED = {0}; +#ifdef PLATFORM_SESSION_ARRAYS // Added: allocated for each engine session +sithCollisionSearchResult* sithCollision_aCollisions MAYBE_UNUSED = NULL; +#else sithCollisionSearchResult sithCollision_aCollisions[4] MAYBE_UNUSED = {0}; +#endif int sithCollision_aNumStackCollisions[4] MAYBE_UNUSED = {0}; int sithCollision_searchStackIdx MAYBE_UNUSED = -1; sithCollisionSectorEntry sithCollision_apSearchedSectors[4] MAYBE_UNUSED = {0}; @@ -799,7 +815,11 @@ uint16_t stdPalEffects_aPalette[256] MAYBE_UNUSED = {0}; char aFilenameStack[2560] MAYBE_UNUSED = {0}; char* apBufferStack[20] MAYBE_UNUSED = {0}; int linenumStack[20] MAYBE_UNUSED = {0}; +#ifdef PLATFORM_SESSION_ARRAYS // Added: allocated for each engine session +char* aEntryStack MAYBE_UNUSED = NULL; +#else char aEntryStack[0x14*(STDCONF_LINEBUFFER_LEN+4)] MAYBE_UNUSED = {0}; +#endif stdFile_t openFileStack[20] MAYBE_UNUSED = {0}; char printfBuffer[STDCONF_LINEBUFFER_LEN] MAYBE_UNUSED = {0}; int stdConffile_linenum MAYBE_UNUSED = 0; @@ -1153,7 +1173,11 @@ sith_bOpen = 0; sithSurface_numAvail = 0; _memset_inline(&sithSurface_aAvail, 0, sizeof(sithSurface_aAvail)); sithSurface_numSurfaces = 0; +#ifdef PLATFORM_SESSION_ARRAYS +if (sithSurface_aSurfaces) _memset_inline(sithSurface_aSurfaces, 0, sizeof(*sithSurface_aSurfaces) * (256)); +#else _memset_inline(&sithSurface_aSurfaces, 0, sizeof(sithSurface_aSurfaces)); +#endif sithSurface_bOpened = 0; uint32_t __sithSurface_byte_8EE668_origValue = 0; _memcpy(&sithSurface_byte_8EE668, &__sithSurface_byte_8EE668_origValue, sizeof(sithSurface_byte_8EE668)); @@ -1565,11 +1589,19 @@ sithWeapon_8BD060 = 0.0f; _memset_inline(&sithWeapon_8BD008, 0, sizeof(sithWeapon_8BD008)); sithWeapon_8BD024 = 0; sithWeapon_senderIndex = 0; +#ifdef PLATFORM_SESSION_ARRAYS +if (jkPlayer_playerInfos) _memset_inline(jkPlayer_playerInfos, 0, sizeof(*jkPlayer_playerInfos) * (JKPLAYER_NUM_INFOS)); +#else _memset_inline(&jkPlayer_playerInfos, 0, sizeof(jkPlayer_playerInfos)); +#endif _memset_inline(&jkPlayer_playerShortName, 0, sizeof(jkPlayer_playerShortName)); jkPlayer_numOtherThings = 0; jkPlayer_numThings = 0; +#ifdef PLATFORM_SESSION_ARRAYS +if (jkPlayer_otherThings) _memset_inline(jkPlayer_otherThings, 0, sizeof(*jkPlayer_otherThings) * (NUM_JKPLAYER_THINGS)); +#else _memset_inline(&jkPlayer_otherThings, 0, sizeof(jkPlayer_otherThings)); +#endif int __jkPlayer_bLoadingSomething_origValue = 1; _memcpy(&jkPlayer_bLoadingSomething, &__jkPlayer_bLoadingSomething_origValue, sizeof(jkPlayer_bLoadingSomething)); playerThingIdx = 0; @@ -1602,7 +1634,11 @@ _memset_inline(&jkPlayer_tipMat, 0, sizeof(jkPlayer_tipMat)); _memset_inline(&sithCollision_aNumSearchedSectors, 0, sizeof(sithCollision_aNumSearchedSectors)); _memset_inline(&sithCollision_collisionHandlers, 0, sizeof(sithCollision_collisionHandlers)); _memset_inline(&sithCollision_aThingSurfaceCollideResults, 0, sizeof(sithCollision_aThingSurfaceCollideResults)); +#ifdef PLATFORM_SESSION_ARRAYS +if (sithCollision_aCollisions) _memset_inline(sithCollision_aCollisions, 0, sizeof(*sithCollision_aCollisions) * (4)); +#else _memset_inline(&sithCollision_aCollisions, 0, sizeof(sithCollision_aCollisions)); +#endif _memset_inline(&sithCollision_aNumStackCollisions, 0, sizeof(sithCollision_aNumStackCollisions)); int __sithCollision_searchStackIdx_origValue = -1; _memcpy(&sithCollision_searchStackIdx, &__sithCollision_searchStackIdx_origValue, sizeof(sithCollision_searchStackIdx)); @@ -1707,7 +1743,11 @@ _memset_inline(&stdPalEffects_aPalette, 0, sizeof(stdPalEffects_aPalette)); _memset_inline(&aFilenameStack, 0, sizeof(aFilenameStack)); _memset_inline(&apBufferStack, 0, sizeof(apBufferStack)); _memset_inline(&linenumStack, 0, sizeof(linenumStack)); +#ifdef PLATFORM_SESSION_ARRAYS +if (aEntryStack) _memset_inline(aEntryStack, 0, sizeof(*aEntryStack) * (0x14*(STDCONF_LINEBUFFER_LEN+4))); +#else _memset_inline(&aEntryStack, 0, sizeof(aEntryStack)); +#endif _memset_inline(&openFileStack, 0, sizeof(openFileStack)); _memset_inline(&printfBuffer, 0, sizeof(printfBuffer)); stdConffile_linenum = 0; diff --git a/components/jk/generated/globals.h b/components/jk/generated/globals.h index a200de5..1e9fee2 100644 --- a/components/jk/generated/globals.h +++ b/components/jk/generated/globals.h @@ -1148,7 +1148,11 @@ extern int sith_bOpen MAYBE_UNUSED; extern int sithSurface_numAvail MAYBE_UNUSED; extern int sithSurface_aAvail[256+1] MAYBE_UNUSED; extern int sithSurface_numSurfaces MAYBE_UNUSED; +#ifdef PLATFORM_SESSION_ARRAYS +extern rdSurface* sithSurface_aSurfaces MAYBE_UNUSED; +#else extern rdSurface sithSurface_aSurfaces[256] MAYBE_UNUSED; +#endif extern int sithSurface_bOpened MAYBE_UNUSED; extern uint32_t sithSurface_byte_8EE668 MAYBE_UNUSED; extern int sithSurface_numUnsyncedSurfaces MAYBE_UNUSED; @@ -1531,11 +1535,19 @@ extern flex32_t sithWeapon_8BD060 MAYBE_UNUSED; extern int sithWeapon_8BD008[6] MAYBE_UNUSED; extern int sithWeapon_8BD024 MAYBE_UNUSED; extern int sithWeapon_senderIndex MAYBE_UNUSED; +#ifdef PLATFORM_SESSION_ARRAYS +extern SithPlayer* jkPlayer_playerInfos MAYBE_UNUSED; +#else extern SithPlayer jkPlayer_playerInfos[JKPLAYER_NUM_INFOS] MAYBE_UNUSED; +#endif extern char16_t jkPlayer_playerShortName[32] MAYBE_UNUSED; extern int jkPlayer_numOtherThings MAYBE_UNUSED; extern int jkPlayer_numThings MAYBE_UNUSED; +#ifdef PLATFORM_SESSION_ARRAYS +extern jkPlayerInfo* jkPlayer_otherThings MAYBE_UNUSED; +#else extern jkPlayerInfo jkPlayer_otherThings[NUM_JKPLAYER_THINGS] MAYBE_UNUSED; +#endif extern int jkPlayer_bLoadingSomething MAYBE_UNUSED; extern int playerThingIdx MAYBE_UNUSED; extern uint32_t jkPlayer_maxPlayers MAYBE_UNUSED; @@ -1567,7 +1579,11 @@ extern char jkPlayer_tipMat[32] MAYBE_UNUSED; extern int sithCollision_aNumSearchedSectors[4] MAYBE_UNUSED; extern SithCollideResult sithCollision_collisionHandlers[144] MAYBE_UNUSED; extern sithCollisionHitHandler_t sithCollision_aThingSurfaceCollideResults[12] MAYBE_UNUSED; +#ifdef PLATFORM_SESSION_ARRAYS +extern sithCollisionSearchResult* sithCollision_aCollisions MAYBE_UNUSED; +#else extern sithCollisionSearchResult sithCollision_aCollisions[4] MAYBE_UNUSED; +#endif extern int sithCollision_aNumStackCollisions[4] MAYBE_UNUSED; extern int sithCollision_searchStackIdx MAYBE_UNUSED; extern sithCollisionSectorEntry sithCollision_apSearchedSectors[4] MAYBE_UNUSED; @@ -1661,7 +1677,11 @@ extern uint16_t stdPalEffects_aPalette[256] MAYBE_UNUSED; extern char aFilenameStack[2560] MAYBE_UNUSED; extern char* apBufferStack[20] MAYBE_UNUSED; extern int linenumStack[20] MAYBE_UNUSED; +#ifdef PLATFORM_SESSION_ARRAYS +extern char* aEntryStack MAYBE_UNUSED; +#else extern char aEntryStack[0x14*(STDCONF_LINEBUFFER_LEN+4)] MAYBE_UNUSED; +#endif extern stdFile_t openFileStack[20] MAYBE_UNUSED; extern char printfBuffer[STDCONF_LINEBUFFER_LEN] MAYBE_UNUSED; extern int stdConffile_linenum MAYBE_UNUSED; diff --git a/components/jk/include/jk_esp.h b/components/jk/include/jk_esp.h index 6906668..9b778b6 100644 --- a/components/jk/include/jk_esp.h +++ b/components/jk/include/jk_esp.h @@ -99,6 +99,14 @@ void *jk_esp_malloc_site(size_t size, const void *site); void *jk_esp_realloc_site(void *ptr, size_t size, const void *site); void jk_esp_alloc_dump(int top); void jk_esp_alloc_release(void); ///< free the tracker table (engine shut down) +/// The engine's largest arrays (AI actors, player infos, ...; see +/// OpenJKDF2/src/Platform/ESP32/jk_esp_arrays.c), allocated zeroed for one +/// engine session instead of being static: alloc before the engine starts +/// (returns 0, with nothing allocated, when there is no memory), free once +/// the engine and the audio task have stopped (no-op when not allocated). +int jk_esp_arrays_alloc(void); +void jk_esp_arrays_free(void); +size_t jk_esp_arrays_bytes(void); ///< size of the allocated block (0: none) /// Log line void jk_esp_log(const char *fmt, ...); /// Set to 1 by the glue when the engine should quit at the next opportunity. diff --git a/components/jk/src/jk.cpp b/components/jk/src/jk.cpp index 274b43c..32d6409 100644 --- a/components/jk/src/jk.cpp +++ b/components/jk/src/jk.cpp @@ -955,6 +955,19 @@ bool init(const Config &config) { logger.info("init: game_dir='{}' internal {}x{}", config.game_dir, g_native_w, g_native_h); #if CONFIG_JK_ENGINE arena_create(); + // The engine's largest arrays (AI actors, player infos, the port's + // directory cache ...) are allocated for this launch instead of being static + // (they would be reserved in every other game): taken first, so they sit at + // the start of the fresh engine heap and do not cut up what the level loads + // into. Freed in deinit(), once nothing runs the engine any more. Without + // them the launch fails: run_frame() reports the engine stopped and the + // cart quits (deinit() cleans up). + if (!jk_esp_arrays_alloc()) { + logger.error("not enough memory for the engine's arrays; psram free {} (largest {}), arena free {}", + heap_caps_get_free_size(MALLOC_CAP_SPIRAM), heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM), + arena_free_size()); + return false; + } #if defined(JK_ESP_FS_DEBUG) // bring-up benchmark: SD read throughput and float parsing speed { @@ -1131,6 +1144,8 @@ void deinit() { } g_engine_task = nullptr; g_audio_task.reset(); + // the engine task and the mixer are gone: nothing uses the arrays now + jk_esp_arrays_free(); #endif Tab5Emu::get().wait_frame(); // the presentation buffers belong to this engine run (~1.8 MB). Swapped diff --git a/tools/overlay_image.py b/tools/overlay_image.py index d7139cd..ff83caa 100644 --- a/tools/overlay_image.py +++ b/tools/overlay_image.py @@ -315,6 +315,8 @@ def demangle(names_): if img and img[0] <= addr < img[0] + img[1]: addr = ext_start + addr - img[0] file, sec = attribute(addr) + if re.search(r"\.str\d*\.\d+$", sec): + continue # merged strings (.rodata.*.str1.4): text, no addresses if core_of(file) and not report: allowed.append(None) # (named only when printed: naming is slow) continue From 819f6404040be69edc66db2e8c5957337782e69c Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Fri, 20 Nov 2026 17:22:01 -0600 Subject: [PATCH 2/2] JK: material (texture) cache knobs in Kconfig, drop the no-op rasterizer sizes Review follow-ups to the per-launch engine arrays: - Kconfig: the "Active-edge rasterizer faces/spans" options are gone (and their -D lines): with RDRASTER_SW_ZBUFFER those pools are never allocated, so the options changed nothing. rdActive.h keeps its defaults. - Kconfig: new "Material (texture) cache" menu. The cache has no fixed size (it grows into free memory and evicts on allocation failure); its two real knobs are now options, passed to the engine as -D values: JK_MATERIAL_PURGE_FREE_KB (2560: evict unused materials while less than this is free; 0 turns it off) and JK_MATERIAL_PURGE_AGE_FRAMES (900). - jk.cpp (JK_ESP_DEBUG builds): the allocation dump says the session arrays block (jk_esp_arrays_bytes()) is among the live allocations until deinit() frees it, so it is not read as a leak. - CMakeLists: the static-state comment now says ~320 KB. - OpenJKDF2 6a5d1d96: the eviction knobs, and jkPlayer_Shutdown's MoTS loop frees its own polyline thing (it named jkPlayer_otherThings). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY --- components/jk/CMakeLists.txt | 15 +++++++++----- components/jk/Kconfig | 40 ++++++++++++++++++++++-------------- components/jk/OpenJKDF2 | 2 +- components/jk/src/jk.cpp | 5 +++++ 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/components/jk/CMakeLists.txt b/components/jk/CMakeLists.txt index 29c99df..4844233 100644 --- a/components/jk/CMakeLists.txt +++ b/components/jk/CMakeLists.txt @@ -87,11 +87,15 @@ if(CONFIG_JK_ENGINE) OPENJKDF2_RELEASE_COMMIT_U=u"esp32" OPENJKDF2_RELEASE_COMMIT_SHORT_U=u"esp32" # table sizes (Kconfig "Engine table sizes"); the engine allocates these - # per session (PLATFORM_SESSION_ARRAYS, Platform/ESP32/jk_esp_arrays.c) + # per session (PLATFORM_SESSION_ARRAYS, Platform/ESP32/jk_esp_arrays.c). + # The active-edge rasterizer's pools (RDACTIVE_MAX_FACES/SPANS) keep + # rdActive.h's sizes: with RDRASTER_SW_ZBUFFER they are never allocated. SITHAI_MAX_ACTORS=${CONFIG_JK_SITHAI_MAX_ACTORS} JK_ESP_FS_DIRCACHE=${CONFIG_JK_FS_DIR_CACHE_ENTRIES} - RDACTIVE_MAX_FACES=${CONFIG_JK_RDACTIVE_MAX_FACES} - RDACTIVE_MAX_SPANS=${CONFIG_JK_RDACTIVE_MAX_SPANS} + # material (texture) cache eviction (Kconfig "Material (texture) cache", + # Platform/ESP32/Window_ESP32.c) + JK_ESP_MATERIAL_PURGE_FREE_KB=${CONFIG_JK_MATERIAL_PURGE_FREE_KB} + JK_ESP_MATERIAL_PURGE_AGE=${CONFIG_JK_MATERIAL_PURGE_AGE_FRAMES} ) # Engine-only flags (the C++ glue in src/ keeps the project defaults) set(ENGINE_COMMON_FLAGS @@ -133,8 +137,9 @@ if(CONFIG_JK_ENGINE AND CONFIG_JK_ESP_DEBUG) endif() if(CONFIG_JK_ENGINE) - # The engine has ~1.4 MB of static state; move it out of internal SRAM into - # PSRAM by renaming the archive's .bss sections (see ../../tools/bss_to_psram.py). + # The engine has ~320 KB of static state (its largest arrays are allocated + # per launch, see above); move it out of internal SRAM into PSRAM by renaming + # the archive's .bss sections (see ../../tools/bss_to_psram.py). # executing from flash, the core's code is part of the PSRAM code overlay # (components/tab5-emu/overlay.ld) include(${CMAKE_CURRENT_LIST_DIR}/../../tools/overlay.cmake) diff --git a/components/jk/Kconfig b/components/jk/Kconfig index f9e5306..22b0467 100644 --- a/components/jk/Kconfig +++ b/components/jk/Kconfig @@ -73,25 +73,35 @@ menu "Jedi Knight (OpenJKDF2)" FatFs directory scans: 129 bytes per entry (512 = 64.5 KB). When it is full, new directories are asked of the card each time (slower level loads, nothing else). + endmenu + + menu "Material (texture) cache" + depends on JK_ENGINE + comment "No fixed size: it grows into free memory and evicts on failure" - config JK_RDACTIVE_MAX_FACES - int "Active-edge rasterizer faces (RDACTIVE_MAX_FACES)" - range 256 8192 - default 1024 + config JK_MATERIAL_PURGE_FREE_KB + int "Evict unused materials below this much free memory (KB)" + range 0 16384 + default 2560 help - Face pool of the engine's active-edge (painter's) software - rasterizer, 512 bytes per face. This port draws the world with - the z-buffered rasterizer (RDRASTER_SW_ZBUFFER), so the pool is - not allocated at all; the size only matters for an engine built - without RDRASTER_SW_ZBUFFER. + The engine keeps every material (texture) it has loaded until + an allocation fails, and then evicts down to textures used a + moment ago. While less than this much is free (PSRAM heap plus + the engine's arena), the port instead evicts, once per frame, + the materials unused for longer than the age below. Higher + keeps more memory free for the rest of the level at the cost of + reloading textures from the card more often; 0 turns this off + (eviction only on allocation failure). - config JK_RDACTIVE_MAX_SPANS - int "Active-edge rasterizer spans (RDACTIVE_MAX_SPANS)" - range 256 16384 - default 2048 + config JK_MATERIAL_PURGE_AGE_FRAMES + int "Evict materials unused for this many frames" + range 1 100000 + default 900 help - Span pool of the same rasterizer, 52 bytes per span (not - allocated with RDRASTER_SW_ZBUFFER, see above). + Age, in rendered frames, past which an unused material may be + evicted while memory is below the threshold above (900 frames + is a minute at 15 fps). Lower frees memory sooner but reloads + textures that come back into view more often. endmenu endmenu diff --git a/components/jk/OpenJKDF2 b/components/jk/OpenJKDF2 index 36de50d..6a5d1d9 160000 --- a/components/jk/OpenJKDF2 +++ b/components/jk/OpenJKDF2 @@ -1 +1 @@ -Subproject commit 36de50d93bd39e0814e6192ae74df4ea5272d796 +Subproject commit 6a5d1d968552c8a0409026480df601ae13b3fc90 diff --git a/components/jk/src/jk.cpp b/components/jk/src/jk.cpp index 32d6409..3e4cdef 100644 --- a/components/jk/src/jk.cpp +++ b/components/jk/src/jk.cpp @@ -529,6 +529,11 @@ void jk_esp_alloc_dump(int top) { } std::sort(sites.begin(), sites.end(), [](const Site &a, const Site &b) { return a.bytes > b.bytes; }); logger.info("alloc: {} live allocations, {} bytes, {} sites ({} untracked)", live, live_bytes, sites.size(), dropped); + // one of them is the engine's session arrays block until deinit() frees it + // (jk_esp_arrays_alloc() in init()): not a leak + if (const size_t arrays = jk_esp_arrays_bytes()) { + logger.info("alloc: (includes the session arrays, {} bytes in 1 allocation, freed at deinit)", arrays); + } for (size_t i = 0; i < sites.size() && i < static_cast(top); i++) { logger.info("alloc: site 0x{:08x} {} bytes in {} allocations", sites[i].site, sites[i].bytes, sites[i].count); }