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)

This commit is contained in:
ecker 2026-08-05 20:18:21 -05:00
parent f4fdf5208f
commit d758cf6db9
14 changed files with 98 additions and 76 deletions

View File

@ -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
}
}
}

View File

@ -12,6 +12,7 @@
"./scripts/player.lua"
],
"system": {
"noclipped": true,
"hot reload": {
"enabled": true
}

View File

@ -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 },

View File

@ -6,17 +6,11 @@
#include <type_traits>
#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 {

View File

@ -101,11 +101,14 @@ void uf::stl::KeyMap<T,Key>::merge( KeyMap<T, Key>&& 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();

View File

@ -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 );
}

View File

@ -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<size_t>() ) return value.as<size_t>();
if ( value.is<uf::stl::string>() ) {
uf::stl::string str = value.as<uf::stl::string>();
if (str.empty()) return 0;
if ( !enabled ) return 0;
if ( value.is<size_t>() ) return value.as<size_t>();
if ( value.is<uf::stl::string>() ) {
uf::stl::string str = value.as<uf::stl::string>();
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

View File

@ -26,6 +26,13 @@ void ext::PlayerMovementBehavior::initialize(uf::Object& self) {
auto& metadata = this->getComponent<ext::PlayerMovementBehavior::Metadata>();
auto& metadataJson = this->getComponent<uf::Serializer>();
UF_BEHAVIOR_METADATA_BIND_SERIALIZER_HOOKS(metadata, metadataJson);
if ( metadata.noclipped ) {
auto& physicsBody = this->getComponent<pod::PhysicsBody>();
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) {

View File

@ -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);
}
}

View File

@ -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

View File

@ -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";

View File

@ -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(...)

View File

@ -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;

View File

@ -278,28 +278,31 @@ namespace {
#include <dc/pvr.h>
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) {}