memory bug fixed-/vk_engine_ui.cpp, vk_loader.h

This commit is contained in:
2025-11-22 18:37:12 +09:00
parent cec2dadd94
commit b023907df8
6 changed files with 137 additions and 6 deletions

View File

@@ -164,7 +164,7 @@ VkSamplerMipmapMode extract_mipmap_mode(fastgltf::Filter filter)
std::optional<std::shared_ptr<LoadedGLTF> > loadGltf(VulkanEngine *engine, std::string_view filePath)
{
//> load_1
fmt::print("Loading GLTF: {}", filePath);
fmt::println("[GLTF] loadGltf begin: '{}'", filePath);
std::shared_ptr<LoadedGLTF> scene = std::make_shared<LoadedGLTF>();
scene->creator = engine;
@@ -849,6 +849,13 @@ std::optional<std::shared_ptr<LoadedGLTF> > loadGltf(VulkanEngine *engine, std::
}
}, img.data);
}
fmt::println("[GLTF] loadGltf done: meshes={} materials={} images={} samplers={} animations={} debugName='{}'",
file.meshes.size(),
file.materials.size(),
file.images.size(),
file.samplers.size(),
file.animations.size(),
file.debugName.empty() ? "<none>" : file.debugName);
return scene;
//< load_graph
}
@@ -1050,6 +1057,14 @@ void LoadedGLTF::updateAnimation(float dt)
void LoadedGLTF::clearAll()
{
const char *name = debugName.empty() ? "<unnamed>" : debugName.c_str();
fmt::println("[GLTF] clearAll begin for '{}' (meshes={} images={} materials={} samplers={})",
name,
meshes.size(),
images.size(),
materials.size(),
samplers.size());
VkDevice dv = creator->_deviceManager->device();
// Before destroying descriptor pools, unregister descriptor-set watches so
@@ -1097,4 +1112,11 @@ void LoadedGLTF::clearAll()
descriptorPool.destroy_pools(dv);
creator->_resourceManager->destroy_buffer(materialBuffer);
fmt::println("[GLTF] clearAll done for '{}' (meshes={}, images={}, materials={}, samplers={})",
name,
meshes.size(),
images.size(),
materials.size(),
samplers.size());
}

View File

@@ -113,7 +113,13 @@ struct LoadedGLTF : public IRenderable
void setActiveAnimation(int index, bool resetTime = true);
void setActiveAnimation(const std::string &name, bool resetTime = true);
~LoadedGLTF() { clearAll(); };
~LoadedGLTF()
{
const char *name = debugName.empty() ? "<unnamed>" : debugName.c_str();
fmt::println("[GLTF] ~LoadedGLTF destructor begin for '{}' ({})", name, static_cast<const void *>(this));
clearAll();
fmt::println("[GLTF] ~LoadedGLTF destructor end for '{}' ({})", name, static_cast<const void *>(this));
};
void clearMeshes(){ clearAll(); };

View File

@@ -17,6 +17,15 @@
#include "frame_resources.h"
#include "core/config.h"
#include <fmt/core.h>
SceneManager::~SceneManager()
{
fmt::println("[SceneManager] dtor: loadedScenes={} dynamicGLTFInstances={} pendingGLTFRelease={}",
loadedScenes.size(),
dynamicGLTFInstances.size(),
pendingGLTFRelease.size());
}
void SceneManager::init(EngineContext *context)
{
@@ -41,6 +50,13 @@ void SceneManager::update_scene()
// until the GPU has finished work that might still reference their resources.
if (_context && _context->currentFrame)
{
if (!pendingGLTFRelease.empty())
{
fmt::println("[SceneManager] update_scene: scheduling {} pending GLTF releases (hasContext={}, hasFrame={})",
pendingGLTFRelease.size(),
true,
true);
}
for (auto &sp : pendingGLTFRelease)
{
auto keepAlive = sp; // copy to keep ref count in the lambda
@@ -332,6 +348,9 @@ void SceneManager::addGLTFInstance(const std::string &name, std::shared_ptr<Load
const glm::mat4 &transform)
{
if (!scene) return;
fmt::println("[SceneManager] addGLTFInstance '{}' (scene='{}')",
name,
scene->debugName.empty() ? "<unnamed>" : scene->debugName.c_str());
dynamicGLTFInstances[name] = GLTFInstance{std::move(scene), transform};
}
@@ -346,6 +365,9 @@ bool SceneManager::removeGLTFInstance(const std::string &name)
if (_context && _context->currentFrame)
{
auto keepAlive = it->second.scene;
fmt::println("[SceneManager] removeGLTFInstance '{}' scheduling deferred destroy (scene='{}')",
name,
keepAlive && !keepAlive->debugName.empty() ? keepAlive->debugName.c_str() : "<unnamed>");
_context->currentFrame->_deletionQueue.push_function([keepAlive]() mutable { keepAlive.reset(); });
}
else
@@ -369,6 +391,9 @@ bool SceneManager::setGLTFInstanceTransform(const std::string &name, const glm::
void SceneManager::clearGLTFInstances()
{
fmt::println("[SceneManager] clearGLTFInstances: dynamicGLTFInstances={} pendingBefore={}",
dynamicGLTFInstances.size(),
pendingGLTFRelease.size());
for (auto &kv : dynamicGLTFInstances)
{
if (kv.second.scene)
@@ -377,6 +402,8 @@ void SceneManager::clearGLTFInstances()
}
}
dynamicGLTFInstances.clear();
fmt::println("[SceneManager] clearGLTFInstances: pendingAfter={}",
pendingGLTFRelease.size());
}
bool SceneManager::setSceneAnimation(const std::string &sceneName, int animationIndex, bool resetTime)

View File

@@ -55,6 +55,7 @@ struct DrawContext
class SceneManager
{
public:
~SceneManager();
void init(EngineContext *context);
void cleanup();