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
Based on the error message, it appears that you are attempting to free a command buffer that is invalid or has already been freed before, causing a validation error. To fix this issue, you should look at the part of your code where you create and manage command buffers and their lifetimes.
In your Renderer::Shutdown() function, you call vkDeviceWaitIdle(device); before CleanupCommandBuffers(); to ensure that all ongoing operations are finished before cleaning up resources. However, in GameObject::Render(Renderer&, const Camera&) you may need to add proper synchronization using semaphores or fences for command buffers drawing in parallel.
Additionally, make sure you are not accidentally freeing a command buffer more than once, or using a freed command buffer elsewhere in your code. You can review your code to ensure proper management of command buffer lifetimes.
Ensure that the Renderer::CleanupCommandBuffers() function is implemented correctly to clean up all resources associated with the command buffers, and make sure you do not call this function more than once or from multiple places.
User
Using breakpoints during the debug, I've noticed that it appears to be trying to run Renderer::Shutdown twice. Based on the code I've provided, can you see where this issue might be taking place?
Assistant
The problem might be coming from the fact that you have the Renderer's destructor calling the Shutdown() function:
Renderer::~Renderer()
{
Shutdown();
}
And in your Engine class, you also call Shutdown() on your Renderer object:
Engine::~Engine()
{
Shutdown();
}
It is possible that you are destructing the Engine object and then destructing the Renderer object, causing the Shutdown() to be called twice. To avoid this problem, you could follow one of these approaches:
Remove the Shutdown() call in the Renderer's destructor and make sure that the Shutdown() function is always called externally before the Renderer object is destructed. This could include some checks to see if Shutdown() method was already called inside the destructor.
User
Assistant
The error you are encountering might be due to the fact that the Cleanup() function is being called twice, once in the Pipeline destructor and again during the Renderer's shutdown process. This causes the VkCommandBuffer to be freed multiple times, which leads to the validation error. You might want to change the way you manage the lifetime and cleanup of Vulkan objects in your code.
One possible solution is to change your implementation of the Pipeline destructor and the Shutdown functions, such that:
The Pipeline's destructor does not call Cleanup(), but rather checks to see if the method has already been called when the object was going out of scope. If not, it should call .
User
I'm not sure that's the issue. The Pipeline::Cleanup method already has an "initialized" check in the method and should not try to call vkDestroyPipeline unless this is true. So if either the Renderer or Pipeline destructor try to call this method after the pipleine is destroyed, it shoud just return.
Assistant
I apologize for the confusion. Based on what you have provided, it seems that the Pipeline::Cleanup() function is indeed guarded by the initialized flag. Thus, the error you are experiencing might not be caused by this part of the code.
Now I suspect that the validation error might not actually be connected to the Pipeline class but instead in another part of the Renderer or Engine shutdown process. The validation error mentions a VkCommandBuffer, which is unrelated to the Pipeline class. Most likely, the problem is actually with the command buffer lifetime management.
You should examine locations where VkCommandBuffers are being freed or where their related command pool is being destroyed. Since it is the Renderer's Shutdown() function that causes the issue, you might want to print additional debug information during the cleanup process, especially around CleanupCommandBuffers() and CleanupCommandPool(), to better understand the resource lifecycle.
It is recommended to carefully review the shutdown sequence for Renderer, Pipeline, and other Vulkan resources to make sure that resources are released in the proper order. In addition, tracking the creation and destruction of objects like VkCommandBuffers, VkPipelines, and other Vulkan resources could be helpful in finding the source of the validation error.
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();
}
VUID-vkFreeCommandBuffers-pCommandBuffers-00048(ERROR / SPEC): msgNum: 515182009 - Validation Error: [ VUID-vkFreeCommandBuffers-pCommandBuffers-00048 ] Object 0: handle = 0x2c39c604970, type = VK_OBJECT_TYPE_COMMAND_BUFFER; | MessageID = 0x1eb50db9 | Invalid VkCommandBuffer 0x2c39c604970[]. The Vulkan spec states: pCommandBuffers must be a valid pointer to an array of commandBufferCount VkCommandBuffer handles, each element of which must either be a valid handle or NULL (https://vulkan.lunarg.com/doc/view/1.3.239.0/windows/1.3-extensions/vkspec.html#VUID-vkFreeCommandBuffers-pCommandBuffers-00048)
Objects: 1
Do you know what could be causing it and how to adjust the code to fix it?
Lastly, double-check that you are not calling vkFreeCommandBuffers() with nullptr for VkCommandBuffer handles or for the same command buffer twice. It might be useful to keep track of all allocated command buffers and their lifetimes in a separate data structure so that you can track their usage and destruction correctly.
Add a flag to the Renderer class to mark when the renderer has been shut down. This can help prevent calling Shutdown() twice during the render object's lifetime. For example:
By adding the isShutDown flag and checking it at the beginning of the Shutdown() method, you can ensure that the renderer shutdown process only runs once, even if it is called multiple times.
I have made those changes. However, the code is now stopping in Pipeline::Cleanup with the following error:
VUID-vkFreeCommandBuffers-pCommandBuffers-00048(ERROR / SPEC): msgNum: 515182009 - Validation Error: [ VUID-vkFreeCommandBuffers-pCommandBuffers-00048 ] Object 0: handle = 0x255dfa9baa0, type = VK_OBJECT_TYPE_COMMAND_BUFFER; | MessageID = 0x1eb50db9 | Invalid VkCommandBuffer 0x255dfa9baa0[]. The Vulkan spec states: pCommandBuffers must be a valid pointer to an array of commandBufferCount VkCommandBuffer handles, each element of which must either be a valid handle or NULL (https://vulkan.lunarg.com/doc/view/1.3.239.0/windows/1.3-extensions/vkspec.html#VUID-vkFreeCommandBuffers-pCommandBuffers-00048)
Objects: 1
[0] 0x255dfa9baa0, type: 6, name: NULL
To achieve (2), when you perform the Renderer's shutdown, make sure you are properly cleaning up the graphics pipeline by calling the Shutdown() or Cleanup() function of your Pipeline object. Also, make sure that the cleanup function is idempotent, meaning that it can be called more than once without any side effects:
void Renderer::Shutdown()
{
if (isShutDown)
{
return;
}
// ...
if (pipeline.IsInitialized())
{
pipeline.Cleanup(); // Or use another method for pipeline cleanup
}
// ...
isShutDown = true;
}
By following these suggestions, you should be able to fix the error you are encountering. Make sure to thoroughly review your code for managing the lifetime and cleanup of Vulkan objects to ensure that you are not accidentally freeing resources more than once or using freed resources elsewhere in your code.