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.
#include <vulkan/vulkan.h>
#include "Texture.h"
#include "Shader.h"
#include
#include // Don’t forget to include
#include
// Add this struct outside the Material class, possibly at the top of Material.cpp
struct ShaderDeleter {
void operator()(Shader* shaderPtr) {
if (shaderPtr != nullptr) {
Shader::Cleanup(shaderPtr);
}
}
};
if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS) {
throw std::runtime_error("Failed to create pipeline layout!");
}
}
void Material::Cleanup()
{
if (vertexShader) {
Shader::Cleanup(vertexShader.get());
}
if (fragmentShader) {
Shader::Cleanup(fragmentShader.get());
}
if (texture) {
texture->Cleanup();
texture.reset();
}
if (pipelineLayout != VK_NULL_HANDLE) {
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
pipelineLayout = VK_NULL_HANDLE;
}
CleanupDescriptorSetLayout();
// Be sure to destroy the descriptor set if necessary
// Note: If the descriptor pool is being destroyed, you don’t need to free individual descriptor sets
void Engine::Shutdown()
{
vkDeviceWaitIdle(*renderer.GetDevice());
// Clean up resources in reverse order
scene.Shutdown();
renderer.Shutdown();
window.Shutdown();
}
void Scene::Update(float deltaTime)
{
// Update game objects and camera
for (GameObject* gameObject : gameObjects)
{
gameObject->Update(deltaTime);
temp = temp + 0.01;
}
}
void Scene::Render(Renderer& renderer)
{
// Render game objects
for (GameObject* gameObject : gameObjects)
{
gameObject->Render(renderer, camera);
}
//camera.SetRotation(glm::vec3(0, 0, temp));
// Submit rendering-related commands to Vulkan queues
}
void Scene::Shutdown()
{
// Clean up game objects
for (GameObject* gameObject : gameObjects)
{
gameObject->Shutdown(); // Make sure to call Shutdown() on the game objects
delete gameObject;
gameObject = nullptr;
}
gameObjects.clear();
camera.Shutdown();
}
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
void GameObject::Shutdown()
{
// Clean up resources, if necessary
// (depending on how Mesh and Material resources are managed)
if (material) {
material->Cleanup();
delete material;
material = nullptr;
}
for (uint32_t i = 0; i < kMvpBufferCount; ++i)
{
BufferUtils::CreateBuffer(device, *GetPhysicalDevice(),
sizeof(MVP), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
mvpBuffers[i], mvpBufferMemory[i]);
}
CreateSwapchain();
CreateRenderPass();
CreateCommandPool();
CreateFramebuffers();
CreateSyncObjects();
}
void Renderer::Shutdown()
{
if (isShutDown) {
return;
}
if (shutdownInProgress) {
return;
}
shutdownInProgress = true;
if (device != VK_NULL_HANDLE) {
vkDeviceWaitIdle(device);
}
CleanupFramebuffers();
CleanupRenderPass();
CleanupSyncObjects();
CleanupCommandPool();
CleanupImageViews();
CleanupSwapchain();
// Destroy the MVP buffers
for (uint32_t i = 0; i < kMvpBufferCount; ++i)
{
vkDestroyBuffer(device, mvpBuffers[i], nullptr);
vkFreeMemory(device, mvpBufferMemory[i], nullptr);
}
if (device != VK_NULL_HANDLE) {
CleanupDevice();
}
DestroySurface();
CleanupInstance();
shutdownInProgress = false;
isShutDown = true;
}
void Renderer::CleanupDevice()
{
// Reset the pipeline first to ensure it’s destroyed before the device
pipeline.reset();
// Destroy the logical device
vkDestroyDevice(device, nullptr);
}
I am getting the current error while rendering:
VUID-vkUpdateDescriptorSets-None-03047(ERROR / SPEC): msgNum: 903342744 - Validation Error: [ VUID-vkUpdateDescriptorSets-None-03047 ] Object 0: handle = 0x980b0000000002e, type = VK_OBJECT_TYPE_DESCRIPTOR_SET; | MessageID = 0x35d7ea98 | vkUpdateDescriptorSets() pDescriptorWrites[0] failed write update validation for VkDescriptorSet 0x980b0000000002e[] with error: Cannot call vkUpdateDescriptorSets() to perform write update on VkDescriptorSet 0x980b0000000002e[] allocated with VkDescriptorSetLayout 0x95a125000000001a[] that is in use by a command buffer. The Vulkan spec states: Descriptor bindings updated by this command which were created without the VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT or VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT bits set must not be used by any command that was recorded to a command buffer which is in the pending state (https://vulkan.lunarg.com/doc/view/1.3.239.0/windows/1.3-extensions/vkspec.html#VUID-vkUpdateDescriptorSets-None-03047)
Objects: 1
[0] 0x980b0000000002e, type: 23, name: NULL
What is causing this issue and how can I fix it? I believe it may be trying to update descriptorsets while they are still in use.
The error indicates that you are trying to update a descriptor set that is currently in use by a command buffer which is in the pending state. To fix this issue, you can wait until the command buffer has finished executing or you can use more descriptor sets and update the next available one, rather than updating the currently bound one.
One possible approach is to allocate multiple descriptor sets for each object in the scene and only update the descriptor set that is not in use by pending command buffers. You can use a simple descriptor set pool or manager that allocates a descriptor set when needed and manages a set of free or available descriptor sets.
Here is a basic example of how you can implement this:
Create a DescriptorSetPool class that handles the allocation and management of descriptor sets:
class DescriptorSetPool
{
public:
void Initialize(VkDescriptorPool pool, VkDescriptorSetLayout layout);
VkDescriptorSet Allocate();
void Free(VkDescriptorSet set);
// Add functions to deallocate resources if necessary
private:
std::vector<VkDescriptorSet> freeDescriptorSets;
VkDescriptorPool descriptorPool;
VkDescriptorSetLayout descriptorSetLayout;
};
Implement the allocation and freeing of descriptor sets in DescriptorSetPool:
After the command buffer is finished executing (e.g., in Renderer::Present, after vkQueuePresentKHR), free the descriptor sets that were used in the previous frame:
for (VkDescriptorSet set : usedDescriptorSets) {
descriptorSetPool.Free(set);
}
usedDescriptorSets.clear();
// NOTE: Make sure you have a std::vector<VkDescriptorSet> usedDescriptorSets; in your Renderer class
In the GameObject::Render function, add the used descriptor set to the usedDescriptorSets vector:
usedDescriptorSets.push_back(descriptorSet);
With this approach, you should have a flexible descriptor set pool that helps avoid updating descriptor sets which are in use by pending command buffers.
Keep in mind that this is a simple example that can be further optimized to suit your specific use case, you can implement smarter allocation management or reuse descriptor sets whenever possible.