diff --git a/components/jk/CMakeLists.txt b/components/jk/CMakeLists.txt index a2f2417..a1bead0 100644 --- a/components/jk/CMakeLists.txt +++ b/components/jk/CMakeLists.txt @@ -86,6 +86,16 @@ 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). + # 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} + # 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 @@ -127,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 6ab867a..22b0467 100644 --- a/components/jk/Kconfig +++ b/components/jk/Kconfig @@ -50,4 +50,58 @@ 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). + endmenu + + menu "Material (texture) cache" + depends on JK_ENGINE + comment "No fixed size: it grows into free memory and evicts on failure" + + config JK_MATERIAL_PURGE_FREE_KB + int "Evict unused materials below this much free memory (KB)" + range 0 16384 + default 2560 + help + 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_MATERIAL_PURGE_AGE_FRAMES + int "Evict materials unused for this many frames" + range 1 100000 + default 900 + help + 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 fd47e34..6a5d1d9 160000 --- a/components/jk/OpenJKDF2 +++ b/components/jk/OpenJKDF2 @@ -1 +1 @@ -Subproject commit fd47e3425aa22f5c2b983ac19e6d8894090aff51 +Subproject commit 6a5d1d968552c8a0409026480df601ae13b3fc90 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..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); } @@ -955,6 +960,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 +1149,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