From d758cf6db992d01deba0cbb3dd851c8297b7de20 Mon Sep 17 00:00:00 2001 From: ecker Date: Wed, 5 Aug 2026 20:18:21 -0500 Subject: [PATCH] fixed some weird memory leak with keymap import/merge, validated allocator settings, some other things (mds-mcdonalds can now theoretically load on a real dreamcast) (to-do: asynchronous mesh loading since it stutters on streaming new mesh data) --- bin/data/entities/model.json | 5 ++- bin/data/entities/player.json | 1 + bin/dreamcast/data/config.json | 18 ++++---- engine/inc/uf/utils/memory/allocator.h | 10 +---- engine/inc/uf/utils/memory/key_map.h | 5 ++- engine/src/engine/asset/asset.cpp | 4 +- engine/src/engine/ext/ext.cpp | 37 ++++++++-------- .../engine/ext/player/movement/behavior.cpp | 7 +++ engine/src/engine/graph/convert.cpp | 22 ++++++++-- engine/src/engine/graph/decode.cpp | 12 ++++-- engine/src/engine/graph/encode.cpp | 6 +-- engine/src/engine/graph/graph.cpp | 2 +- engine/src/ext/opengl/commands.cpp | 2 - engine/src/spec/window/dreamcast.cpp | 43 +++++++++---------- 14 files changed, 98 insertions(+), 76 deletions(-) diff --git a/bin/data/entities/model.json b/bin/data/entities/model.json index 8be779d4..cf98d35d 100644 --- a/bin/data/entities/model.json +++ b/bin/data/entities/model.json @@ -11,6 +11,7 @@ "metadata": { "graph": { "debug": { + // "simple": true, "no cleanup": false, "print": { "tree": false, @@ -79,8 +80,8 @@ "tag": "worldspawn", "player": "info_player_start", "enabled": "auto", - "radius": 32, - "every": 1 + "radius": 50, + "every": 4 } } } diff --git a/bin/data/entities/player.json b/bin/data/entities/player.json index b84df050..cfff2bb6 100644 --- a/bin/data/entities/player.json +++ b/bin/data/entities/player.json @@ -12,6 +12,7 @@ "./scripts/player.lua" ], "system": { + "noclipped": true, "hot reload": { "enabled": true } diff --git a/bin/dreamcast/data/config.json b/bin/dreamcast/data/config.json index 90bfec03..1fb40fc1 100644 --- a/bin/dreamcast/data/config.json +++ b/bin/dreamcast/data/config.json @@ -3,7 +3,7 @@ "scenes": { "start": "StartMenu", "lights": { "enabled": false, - "lightmaps": false, + "lightmaps": true, "max": 1 }, "textures": { @@ -26,7 +26,7 @@ "rebuild on tick begin": false }, "pipelines": { - "culling": false + "culling": true }, "experimental": { "rebuild on tick begin": false, @@ -108,15 +108,15 @@ } }, "memory pool": { - "enabled": false, // probably should be enabled - "subPools": false, - "alignment": 4, - "override": false, - "size": "1024 KiB", + "enabled": false, + "subPools": true, // allocates subpools out of the global pool + "alignment": 8, + "override": true, + "size": "8 MiB", // change later "pools": { - "entity": "128 KiB", + "entity": "256 KiB", "userdata": "128 KiB", - "component": "128 KiB" + "component": "512 KiB" } }, "render modes": { "gui": true, "deferred": true }, diff --git a/engine/inc/uf/utils/memory/allocator.h b/engine/inc/uf/utils/memory/allocator.h index e97d1527..4345cb49 100644 --- a/engine/inc/uf/utils/memory/allocator.h +++ b/engine/inc/uf/utils/memory/allocator.h @@ -6,17 +6,11 @@ #include #define UF_MEMORYPOOL_USE_ALLOCATOR 1 -#if UF_ENV_DREAMCAST - #define UF_MEMORYPOOL_OVERRIDE_DEFAULT 0 -#else - #define UF_MEMORYPOOL_OVERRIDE_DEFAULT 1 -#endif - - +#define UF_MEMORYPOOL_OVERRIDE_DEFAULT 1 #if __clang__ #define UF_MEMORYPOOL_OVERRIDE_NEW_DELETE 0 #else - #define UF_MEMORYPOOL_OVERRIDE_NEW_DELETE 0 + #define UF_MEMORYPOOL_OVERRIDE_NEW_DELETE 1 #endif namespace uf { diff --git a/engine/inc/uf/utils/memory/key_map.h b/engine/inc/uf/utils/memory/key_map.h index 8e5b7579..943f22c6 100644 --- a/engine/inc/uf/utils/memory/key_map.h +++ b/engine/inc/uf/utils/memory/key_map.h @@ -101,11 +101,14 @@ void uf::stl::KeyMap::merge( KeyMap&& other ) { this->reserve( this->keys.size() + other.keys.size() ); for ( auto& key : other.keys ) { + T&& value = std::move(other.map.at(key)); + if ( this->map.count(key) == 0 ) { this->indices[key] = this->keys.size(); this->keys.emplace_back(key); } - this->map[key] = other.map.at(key); + + this->map[key] = std::move(value); } other.clear(); diff --git a/engine/src/engine/asset/asset.cpp b/engine/src/engine/asset/asset.cpp index 3b7fbf88..7f24e617 100644 --- a/engine/src/engine/asset/asset.cpp +++ b/engine/src/engine/asset/asset.cpp @@ -373,11 +373,11 @@ uf::stl::string uf::asset::load( uf::asset::Payload& payload ) { bool stale = uf::io::exists( output ) ? uf::io::mtime( filename ) > uf::io::mtime( output ) : true; if ( extension != target && stale ) { UF_MSG_DEBUG("Converting image: {} => {}", filename, output ); - pod::Vector2ui size = { 32, 32 }; + pod::Vector2ui size = { 32, 32 }; // 64, 64 doesn't render... uf::stl::string filter = "linear"; uf::stl::string format = "auto"; - auto img = asset.scale( size, filter); + auto img = asset.scale( size, filter ); auto dtex = ext::texconv::convert( img, format ); ext::texconv::save( dtex, output ); } diff --git a/engine/src/engine/ext/ext.cpp b/engine/src/engine/ext/ext.cpp index 828a072a..b194dea9 100644 --- a/engine/src/engine/ext/ext.cpp +++ b/engine/src/engine/ext/ext.cpp @@ -464,31 +464,30 @@ void UF_API uf::initialize() { // check if we are even allowed to use memory pools bool enabled = configMemoryPoolJson["enabled"].as(true); auto deduceSize = [enabled]( const ext::json::Value& value ) -> size_t { - if ( !enabled ) return 0; - if ( value.is() ) return value.as(); - if ( value.is() ) { - uf::stl::string str = value.as(); - if (str.empty()) return 0; + if ( !enabled ) return 0; + if ( value.is() ) return value.as(); + if ( value.is() ) { + uf::stl::string str = value.as(); + if (str.empty()) return 0; - char* endPtr = nullptr; - size_t requested = std::strtoull(str.c_str(), &endPtr, 10); + char* endPtr = nullptr; + size_t requested = std::strtoull(str.c_str(), &endPtr, 10); - if (endPtr == str.c_str()) return 0; + if (endPtr == str.c_str()) return 0; - while (*endPtr == ' ') endPtr++; + while (*endPtr == ' ') endPtr++; - // Check for the multiplier - if (*endPtr != '\0') { - switch (*endPtr) { - case 'K': case 'k': return requested * 1024; - case 'M': case 'm': return requested * 1024 * 1024; - case 'G': case 'g': return requested * 1024 * 1024 * 1024; + if (*endPtr != '\0') { + switch (*endPtr) { + case 'K': case 'k': return requested * 1024; + case 'M': case 'm': return requested * 1024 * 1024; + case 'G': case 'g': return requested * 1024 * 1024 * 1024; + } } + return requested; } - return requested; - } - return 0; - }; + return 0; + }; // set memory pool alignment requirements uf::memoryPool::alignment = configMemoryPoolJson["alignment"].as( uf::memoryPool::alignment ); // set memory pool sizes diff --git a/engine/src/engine/ext/player/movement/behavior.cpp b/engine/src/engine/ext/player/movement/behavior.cpp index f333c3db..705b724f 100644 --- a/engine/src/engine/ext/player/movement/behavior.cpp +++ b/engine/src/engine/ext/player/movement/behavior.cpp @@ -26,6 +26,13 @@ void ext::PlayerMovementBehavior::initialize(uf::Object& self) { auto& metadata = this->getComponent(); auto& metadataJson = this->getComponent(); UF_BEHAVIOR_METADATA_BIND_SERIALIZER_HOOKS(metadata, metadataJson); + + if ( metadata.noclipped ) { + auto& physicsBody = this->getComponent(); + uf::physics::setGravity(physicsBody, pod::Vector3f{0,0,0}); + uf::physics::setColliderCategory(physicsBody, "NONE"); + uf::physics::setColliderMask(physicsBody, "NONE"); + } } void ext::PlayerMovementBehavior::tick(uf::Object& self) { diff --git a/engine/src/engine/graph/convert.cpp b/engine/src/engine/graph/convert.cpp index 28a5899c..70456a4a 100644 --- a/engine/src/engine/graph/convert.cpp +++ b/engine/src/engine/graph/convert.cpp @@ -372,10 +372,17 @@ void uf::graph::import( pod::Graph::Storage& target, pod::Graph::Storage& storag target.joints.merge(std::move(storage.joints)); target.entities.merge(std::move(storage.entities)); - for ( auto& v :storage.lights ) target.lights.emplace_back(std::move(v)); - for ( auto& v :storage.shadow2Ds ) target.shadow2Ds.emplace_back(std::move(v)); - for ( auto& v :storage.shadowCubes ) target.shadowCubes.emplace_back(std::move(v)); - for ( auto& v :storage.flattenedPrimitives ) target.flattenedPrimitives.emplace_back(std::move(v)); + target.lights.reserve(target.lights.size() + storage.lights.size()); + for ( auto& v : storage.lights ) target.lights.emplace_back(std::move(v)); + + target.shadow2Ds.reserve(target.shadow2Ds.size() + storage.shadow2Ds.size()); + for ( auto& v : storage.shadow2Ds ) target.shadow2Ds.emplace_back(std::move(v)); + + target.shadowCubes.reserve(target.shadowCubes.size() + storage.shadowCubes.size()); + for ( auto& v : storage.shadowCubes ) target.shadowCubes.emplace_back(std::move(v)); + + target.flattenedPrimitives.reserve(target.flattenedPrimitives.size() + storage.flattenedPrimitives.size()); + for ( auto& v : storage.flattenedPrimitives ) target.flattenedPrimitives.emplace_back(std::move(v)); } else { target.primitives.import(storage.primitives); target.instances.import(storage.instances); @@ -391,9 +398,16 @@ void uf::graph::import( pod::Graph::Storage& target, pod::Graph::Storage& storag target.joints.import(storage.joints); target.entities.import(storage.entities); + target.lights.reserve(target.lights.size() + storage.lights.size()); for ( auto& v :storage.lights ) target.lights.emplace_back(v); + + target.shadow2Ds.reserve(target.shadow2Ds.size() + storage.shadow2Ds.size()); for ( auto& v :storage.shadow2Ds ) target.shadow2Ds.emplace_back(v); + + target.shadowCubes.reserve(target.shadowCubes.size() + storage.shadowCubes.size()); for ( auto& v :storage.shadowCubes ) target.shadowCubes.emplace_back(v); + + target.flattenedPrimitives.reserve(target.flattenedPrimitives.size() + storage.flattenedPrimitives.size()); for ( auto& v :storage.flattenedPrimitives ) target.flattenedPrimitives.emplace_back(v); } } \ No newline at end of file diff --git a/engine/src/engine/graph/decode.cpp b/engine/src/engine/graph/decode.cpp index bf86faf3..ef4e2384 100644 --- a/engine/src/engine/graph/decode.cpp +++ b/engine/src/engine/graph/decode.cpp @@ -16,7 +16,7 @@ #if UF_ENV_DREAMCAST #define UF_DEBUG_TIMER_MULTITRACE_START(...) UF_TIMER_MULTITRACE_START(__VA_ARGS__) - #define UF_DEBUG_TIMER_MULTITRACE(...) UF_TIMER_MULTITRACE(__VA_ARGS__) + #define UF_DEBUG_TIMER_MULTITRACE(...) { UF_TIMER_MULTITRACE(__VA_ARGS__); DC_STATS(); } #define UF_DEBUG_TIMER_MULTITRACE_END(...) UF_TIMER_MULTITRACE_END(__VA_ARGS__) #else #define UF_DEBUG_TIMER_MULTITRACE_START(...) @@ -608,10 +608,13 @@ void uf::graph::load( pod::Graph& graph, const uf::stl::string& filename, const #endif }); + UF_DEBUG_TIMER_MULTITRACE("Executing tasks"); uf::thread::execute( tasks ); + UF_DEBUG_TIMER_MULTITRACE("Processing IO"); uf::asset::processIO(); // process images + UF_DEBUG_TIMER_MULTITRACE("Processing pending images"); for ( auto& pending : pendingImages ) { auto& image = storage.images[pending.name].data; @@ -622,6 +625,7 @@ void uf::graph::load( pod::Graph& graph, const uf::stl::string& filename, const } // process meshes that need to be minified because I can't easily tie it to the callback + UF_DEBUG_TIMER_MULTITRACE("Processing meshes for minification"); for ( auto& name : meshesToMinify ) { auto& mesh = storage.meshes[name]; mesh.prune( { "position", "uv", "st" } ); @@ -630,24 +634,26 @@ void uf::graph::load( pod::Graph& graph, const uf::stl::string& filename, const } // re-reference all transform parents + UF_DEBUG_TIMER_MULTITRACE("Re-referencing nodes"); for ( auto& node : graph.nodes ) { if ( 0 <= node.parent && node.parent < graph.nodes.size() && node.index != node.parent ) { node.transform.reference = &graph.nodes[node.parent].transform; } } - UF_DEBUG_TIMER_MULTITRACE_END("Processing graph..."); // migrate + UF_DEBUG_TIMER_MULTITRACE("Migrating storage"); if ( graph.storage && uf::graph::storageMode != pod::Graph::Storage::GRAPH ) { auto* pointer = graph.storage; graph.storage = NULL; auto& storage = *pointer; auto& target = uf::graph::getStorage( graph ); - uf::graph::import( target, storage ); + uf::graph::import( target, storage, true ); delete pointer; } + UF_DEBUG_TIMER_MULTITRACE_END("Loaded graph."); #if UF_ENV_DREAMCAST DC_STATS(); #endif diff --git a/engine/src/engine/graph/encode.cpp b/engine/src/engine/graph/encode.cpp index 1e240aeb..4d2a2ac4 100644 --- a/engine/src/engine/graph/encode.cpp +++ b/engine/src/engine/graph/encode.cpp @@ -429,10 +429,10 @@ uf::stl::string uf::graph::save( const pod::Graph& graph, const uf::stl::string& #if UF_USE_DC_TEXCONV auto img = uf::Image(image); - pod::Vector2ui size = { 32, 32 }; - uf::stl::string filter = "nearest"; + pod::Vector2ui size = { 64, 64 }; + uf::stl::string filter = "linear"; // "nearest" uf::stl::string dtexFormat = "ARGB4444"; - if ( name == "lightmap_atlas" || img.getFormat() == uf::renderer::enums::Format::R8G8B8A8_RGBE ) { + if ( name.starts_with("lightmap") || img.getFormat() == uf::renderer::enums::Format::R8G8B8A8_RGBE ) { size = { 128, 128 }; filter = "linear"; dtexFormat = "RGB565"; diff --git a/engine/src/engine/graph/graph.cpp b/engine/src/engine/graph/graph.cpp index cb10333d..71934b45 100644 --- a/engine/src/engine/graph/graph.cpp +++ b/engine/src/engine/graph/graph.cpp @@ -17,7 +17,7 @@ #if UF_ENV_DREAMCAST #define UF_DEBUG_TIMER_MULTITRACE_START(...) UF_TIMER_MULTITRACE_START(__VA_ARGS__) - #define UF_DEBUG_TIMER_MULTITRACE(...) UF_TIMER_MULTITRACE(__VA_ARGS__) + #define UF_DEBUG_TIMER_MULTITRACE(...) { UF_TIMER_MULTITRACE(__VA_ARGS__); DC_STATS(); } #define UF_DEBUG_TIMER_MULTITRACE_END(...) UF_TIMER_MULTITRACE_END(__VA_ARGS__) #else #define UF_DEBUG_TIMER_MULTITRACE_START(...) diff --git a/engine/src/ext/opengl/commands.cpp b/engine/src/ext/opengl/commands.cpp index 9bd80ce6..ca198076 100644 --- a/engine/src/ext/opengl/commands.cpp +++ b/engine/src/ext/opengl/commands.cpp @@ -384,13 +384,11 @@ void ext::opengl::CommandBuffer::submit() { InfoDraw* info = (InfoDraw*) header; ::shadowState.update( info->matrices ); - /* if ( ext::opengl::settings::pipelines::culling && info->attributes.instance.pointer && info->attributes.instance.length == sizeof(pod::Instance) ) { const pod::Instance& instance = *(pod::Instance*) info->attributes.instance.pointer; if ( !::inside( instance, ::shadowState.ndc ) ) continue; } - */ drawIndexed( *info ); } break; diff --git a/engine/src/spec/window/dreamcast.cpp b/engine/src/spec/window/dreamcast.cpp index f25dbe39..49b2a65a 100644 --- a/engine/src/spec/window/dreamcast.cpp +++ b/engine/src/spec/window/dreamcast.cpp @@ -278,28 +278,31 @@ namespace { #include uf::stl::string spec::dreamcast::malloc_stats( bool verbose ) { - std::stringstream str; - if ( verbose ) { - str << "malloc Info:\n" - "\tarena " << uf::string::si( mallinfo().arena, "B" ) << " (non-mmapped space allocated from system)\n" - "\tordblks " << uf::string::si( mallinfo().ordblks, "B" ) << " (number of free chunks)\n" - "\tsmblks " << uf::string::si( mallinfo().smblks, "B" ) << " (number of fastbin blocks)\n" - "\tusmblks " << uf::string::si( mallinfo().usmblks, "B" ) << " (maximum total allocated space)\n" - "\tfsmblks " << uf::string::si( mallinfo().fsmblks, "B" ) << " (space available in freed fastbin blocks)\n" - "\tuordblks " << uf::string::si( mallinfo().uordblks, "B" ) << " (total allocated space)\n" - "\tfordblks " << uf::string::si( mallinfo().fordblks, "B" ) << " (total free space)\n" - "\tkeepcost " << uf::string::si( mallinfo().keepcost, "B" ) << " (top-most, releasable (via malloc_trim) space)\n"; - } else { - str << "malloc Info: Free: " << uf::string::si( mallinfo().arena - mallinfo().uordblks, "B" ) << " | Used: " << uf::string::si( mallinfo().uordblks, "B" ); + return FMT_FORMAT("malloc Info:\n" + "\tarena {} (non-mmapped space allocated from system)\n" + "\tordblks {} (number of free chunks)\n" + "\tsmblks {} (number of fastbin blocks)\n" + "\tusmblks {} (maximum total allocated space)\n" + "\tfsmblks {} (space available in freed fastbin blocks)\n" + "\tuordblks {} (total allocated space)\n" + "\tfordblks {} (total free space)\n" + "\tkeepcost {} (top-most, releasable (via malloc_trim) space)\n", + uf::string::si( mallinfo().arena, "B" ), + uf::string::si( mallinfo().ordblks, "B" ), + uf::string::si( mallinfo().smblks, "B" ), + uf::string::si( mallinfo().usmblks, "B" ), + uf::string::si( mallinfo().fsmblks, "B" ), + uf::string::si( mallinfo().uordblks, "B" ), + uf::string::si( mallinfo().fordblks, "B" ), + uf::string::si( mallinfo().keepcost, "B" ) + ); } - - return str.str(); + + return FMT_FORMAT("malloc Info: Free: {} | Used: {}", uf::string::si( mallinfo().arena - mallinfo().uordblks, "B" ), uf::string::si( mallinfo().uordblks, "B" )); } uf::stl::string spec::dreamcast::pvr_malloc_stats( bool verbose ) { - std::stringstream str; - if ( verbose ) { /* "PVR malloc Info:\n" @@ -313,12 +316,8 @@ uf::stl::string spec::dreamcast::pvr_malloc_stats( bool verbose ) { "\tfordblks " << uf::string::si( pvr_int_mallinfo().fordblks, "B" ) << " (total free space)\n" "\tkeepcost " << uf::string::si( pvr_int_mallinfo().keepcost, "B" ) << " (top-most, releasable (via malloc_trim) space)"; */ - } else { - str << "PVR malloc Info: Free: " << uf::string::si( pvr_mem_available(), "B" ); } - - - return str.str(); + return FMT_FORMAT("PVR malloc Info: Free: {}", uf::string::si( pvr_mem_available(), "B" )); } spec::dreamcast::Window::Window() : m_context(NULL) {}