I’m building a video game engine using C++ as the coding language and Vulkan for graphics. I am trying to set up a generic renderer using Vulkan that is flexible and will render objects based on a vector that is supplied to it. The renderer will also handle the creation of the window using GLFW and use GLM for all relevant math calls. I am using the ASSIMP library to load 3d models and animations.
GameObject::~GameObject()
{
if (initialized)
{
Shutdown();
}
}
void GameObject::Initialize()
{
mesh = new Mesh{};
material = new Material{};
SetScale(glm::vec3(1.0f));
this->initialized = true;
}
void GameObject::Update(float deltaTime)
{
// Update position, rotation, scale, and other properties
// Example: Rotate the object around the Y-axis
rotation.y += deltaTime * glm::radians(90.0f);
UpdateModelMatrix();
}
void GameObject::Render(Renderer& renderer, const Camera& camera)
{
// Render this object using the renderer and camera
VkDevice device = *renderer.GetDevice();
// Bind mesh vertex and index buffers
VkBuffer vertexBuffers[] = { mesh->GetVertexBuffer() };
VkDeviceSize offsets[] = { 0 };
vkCmdBindVertexBuffers(*renderer.GetCurrentCommandBuffer(), 0, 1, vertexBuffers, offsets);
vkCmdBindIndexBuffer(*renderer.GetCurrentCommandBuffer(), mesh->GetIndexBuffer(), 0, VK_INDEX_TYPE_UINT32);
// Update shader uniform buffers with modelMatrix, viewMatrix and projectionMatrix transforms
struct MVP {
glm::mat4 model;
glm::mat4 view;
glm::mat4 projection;
} mvp;
mvp.model = modelMatrix;
mvp.view = camera.GetViewMatrix();
mvp.projection = camera.GetProjectionMatrix();
// Create a new buffer to hold the MVP data temporarily
The error you're receiving indicates that there is still a VkImage object that hasn't been destroyed before you destroy the VkDevice during the cleanup process. The issue is likely related to the Texture class where cleanup is not properly called for the VkImage tied to the texture object.
User
It seems the Texture::Cleanup method is not complete. Can you complete the code for this method? Modify the parameter definitions if required.
Assistant
Sure. You can adjust the Texture::Cleanup method to take no parameters and operate on the class members directly. Here's the complete method for the Texture class:
Now, with this method implemented, don't forget to call texture->Cleanup() in the Material destructor right before you delete the texture object. This way, the Texture resources will be properly cleaned up before the VkDevice is destroyed.
User
With this new definiton, I am now getting a syntax error in the following method:
How can I modify this method to work with the new Cleanup method?
Assistant
You'll need to implement a custom deleter for the shared_ptr. You can do this by creating a lambda function that calls the Cleanup method and then deletes the Texture object. Modify the LoadTexture method as follows:
In this example, we create a lambda function that takes a Texture* as input and calls the Cleanup() function on the object before deleting it. The lambda function captures the device parameter by value so it can be used inside the function.
With these changes, the Texture object should now be managed by the shared_ptr, and the Cleanup() function will be called automatically when the Texture is no longer in use.
material->CreateDescriptorSet(renderer.CreateDescriptorSetLayout(), renderer.CreateDescriptorPool(1), mvpBuffer, sizeof(MVP));
// Map the MVP data into the buffer and unmap
void* data = nullptr;
vkMapMemory(device, mvpBufferMemory, 0, sizeof(MVP), 0, &data);
memcpy(data, &mvp, sizeof(MVP));
vkUnmapMemory(device, mvpBufferMemory);
// TODO: Modify your material, descriptor set, and pipeline to use this new mvpBuffer instead of
// the default uniform buffer
// Bind the DescriptorSet associated with the material
VkDescriptorSet descriptorSet = material->GetDescriptorSet();
material->UpdateBufferBinding(descriptorSet, mvpBuffer, device, sizeof(MVP));
renderer.CreateGraphicsPipeline(mesh, material);
vkCmdBindPipeline(*renderer.GetCurrentCommandBuffer(), VK_PIPELINE_BIND_POINT_GRAPHICS, renderer.GetPipeline().get()->GetPipeline());
vkCmdBindDescriptorSets(*renderer.GetCurrentCommandBuffer(), VK_PIPELINE_BIND_POINT_GRAPHICS, material->GetPipelineLayout(), 0, 1, &descriptorSet, 0, nullptr);
// Call vkCmdDrawIndexed()
uint32_t numIndices = static_cast<uint32_t>(mesh->GetIndices().size());
vkCmdDrawIndexed(*renderer.GetCurrentCommandBuffer(), numIndices, 1, 0, 0, 0);
// Cleanup the temporary buffer
void GameObject::Shutdown()
{
// Clean up resources, if necessary
// (depending on how Mesh and Material resources are managed)
delete mesh;
delete material;
this->initialized = false;
}
void Engine::Shutdown()
{
vkDeviceWaitIdle(*renderer.GetDevice());
// Clean up resources in reverse order
scene.Shutdown();
renderer.Shutdown();
window.Shutdown();
}
void Renderer::CleanupSyncObjects()
{
for (size_t i = 0; i < kMaxFramesInFlight; ++i)
{
if (renderFinishedSemaphores[i] != VK_NULL_HANDLE)
vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
if (imageAvailableSemaphores[i] != VK_NULL_HANDLE)
vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
if (inFlightFences[i] != VK_NULL_HANDLE)
vkDestroyFence(device, inFlightFences[i], nullptr);
}
}
void Renderer::CleanupCommandPool()
{
// Clean up Vulkan command pool
CleanupCommandBuffers(); // Add this line to clean up command buffers before destroying the command pool
vkDestroyCommandPool(device, commandPool, nullptr);
}
I am getting the following error in Renderer::CleanupDevice:
VUID-vkDestroyDevice-device-00378(ERROR / SPEC): msgNum: 1901072314 - Validation Error: [ VUID-vkDestroyDevice-device-00378 ] Object 0: handle = 0x1cb94a23410, type = VK_OBJECT_TYPE_DEVICE; Object 1: handle = 0xb097c90000000027, type = VK_OBJECT_TYPE_IMAGE; | MessageID = 0x71500fba | OBJ ERROR : For VkDevice 0x1cb94a23410[], VkImage 0xb097c90000000027[] has not been destroyed. The Vulkan spec states: All child objects created on device must have been destroyed prior to destroying device (https://vulkan.lunarg.com/doc/view/1.3.239.0/windows/1.3-extensions/vkspec.html#VUID-vkDestroyDevice-device-00378)
Objects: 2
[0] 0x1cb94a23410, type: 3, name: NULL
[1] 0xb097c90000000027, type: 10, name: NULL
Do you know what could be causing it and how to adjust the code to fix it?
To fix the error, you should make sure to call the Cleanup function for all texture objects you've created before you call Renderer::Shutdown(). For example, in your GameObject class, make sure to properly call Texture::Cleanup for each texture object stored in the Material class before GameObject::Shutdown() is called.
Another possible source of the error could be because you have commented out the texture object initialization in Engine::Initialize() or Scene::Initialize() function. Make sure that after creating texture objects in both of these functions, you call Texture::Cleanup (using the proper method as described above) before Renderer::Shutdown().
Here's a suggestion of how you should adjust the code in Material::~Material(), which may resolve the issue:
Material::~Material()
{
// Clean up resources, if necessary
if (initialized)
{
// Perform cleanup for shader modules and descriptor set stuff
// ... (your existing cleanup code)
// Clean up texture resources
Texture::Cleanup(texture);
}
}
In your Engine::Shutdown() (or at the end of Engine::~Engine() if you want to do it there), ensure that you are correctly calling the Shutdown() function for each GameObject in the Scene like this:
void Engine::Shutdown()
{
vkDeviceWaitIdle(*renderer.GetDevice());
// Clean up resources in reverse order
scene.Shutdown(); // Make sure this function calls each GameObject's Shutdown method
renderer.Shutdown();
window.Shutdown();
}
Don't forget to adjust the Scene::Shutdown to ensure proper GameObject cleanup:
void Scene::Shutdown()
{
// Iterate over all game objects and call their Shutdown() method
for (auto gameObject : gameObjects)
{
gameObject->Shutdown();
delete gameObject;
}
gameObjects.clear();
}
By ensuring that all Texture objects are properly cleaned up within their respective Material classes before Renderer::Shutdown() is called, this should fix the error related to the VkImage object not being destroyed before destroying the VkDevice.