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.
The code now runs. However, I am now getting the following error when trying to close the application:
VUID-vkDestroyDevice-device-00378(ERROR / SPEC): msgNum: 1901072314 - Validation Error: [ VUID-vkDestroyDevice-device-00378 ] Object 0: handle = 0x234dc33cbc0, type = VK_OBJECT_TYPE_DEVICE; Object 1: handle = 0x149d740000000087, type = VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT; | MessageID = 0x71500fba | OBJ ERROR : For VkDevice 0x234dc33cbc0[], VkDescriptorSetLayout 0x149d740000000087[] 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] 0x234dc33cbc0, type: 3, name: NULL
[1] 0x149d740000000087, type: 20, name: NULL
Assistant
This error occurs because the VkDescriptorSetLayout objects are not being properly destroyed before the Vulkan device is destroyed.
In the Material class, the CleanupDescriptorSetLayout() and CleanupSamplerDescriptorSetLayout() functions are empty. You need to properly destroy the VkDescriptorSetLayout objects in these functions. Update the corresponding Cleanup functions in your Material.cpp as follows:
With these changes, the objects should be properly destroyed when the application is closed. If you still encounter issues, please ensure that you call for each material in your class before destroying the and objects in the function.
User
These functions are not empty and are populated with the same code that you suggested. Further, I have used breakpoints during debug to confirm that the Material::Shutdown function is being run for each material and the code in the Cleanup functions is running. The DescriptorSetLayout variables are reverted to null after these functions run.
Assistant
I apologize for my misunderstanding on the implementation of the CleanupDescriptorSetLayout() and CleanupSamplerDescriptorSetLayout() functions. Since these functions are implemented correctly, the issue may be related to the order of destruction in the Engine::Shutdown() function.
Ensure that the shutdown order in Engine::Shutdown() function follows this flow:
Shut down the Scene.
Wait for the device to be idle.
Shut down the Renderer.
Shut down the Window.
Here's the corrected Engine::Shutdown() function:
void Engine::Shutdown()
{
// Shut down the Scene
scene.Shutdown();
// Wait for the device to be idle before shutting down the Renderer and Window
vkDeviceWaitIdle(*renderer.GetDevice());
// Shut down the Renderer and Window
renderer.Shutdown();
window.Shutdown();
}
By ensuring that the Scene objects are destroyed and the device is idle before shutting down the Renderer, the validation error should be resolved. If the error persists, please also double-check your `Scene
};
Material.h:
#pragma once
#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();
CleanupSamplerDescriptorSetLayout();
// 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();
}
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
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;
}
I am getting an access violation error in Material::CreateDescriptorSet when trying to allocate descriptorsets. Here is the log output:
VUID-VkPipelineLayoutCreateInfo-pSetLayouts-parameter(ERROR / SPEC): msgNum: -1275504685 - Validation Error: [ VUID-VkPipelineLayoutCreateInfo-pSetLayouts-parameter ] Object 0: handle = 0x1f2f1e29a40, type = VK_OBJECT_TYPE_INSTANCE; | MessageID = 0xb3f957d3 | Invalid VkDescriptorSetLayout Object 0xb3dc6ff590. The Vulkan spec states: If setLayoutCount is not 0, pSetLayouts must be a valid pointer to an array of setLayoutCount valid or VK_NULL_HANDLE VkDescriptorSetLayout handles (https://vulkan.lunarg.com/doc/view/1.3.239.0/windows/1.3-extensions/vkspec.html#VUID-VkPipelineLayoutCreateInfo-pSetLayouts-parameter)
Objects: 1
[0] 0x1f2f1e29a40, type: 1, name: NULL
Current Frame: 0 | Cmd Buffer Index: 1 | Image Index: 0
Calling vkBeginCommandBufferà
vkBeginCommandBuffer calledà
VUID-VkDescriptorSetAllocateInfo-pSetLayouts-parameter(ERROR / SPEC): msgNum: 210066664 - Validation Error: [ VUID-VkDescriptorSetAllocateInfo-pSetLayouts-parameter ] Object 0: handle = 0x1f2f1e29a40, type = VK_OBJECT_TYPE_INSTANCE; | MessageID = 0xc855ce8 | Invalid VkDescriptorSetLayout Object 0xcccccccccccccccc. The Vulkan spec states: pSetLayouts must be a valid pointer to an array of descriptorSetCount valid VkDescriptorSetLayout handles (https://vulkan.lunarg.com/doc/view/1.3.239.0/windows/1.3-extensions/vkspec.html#VUID-VkDescriptorSetAllocateInfo-pSetLayouts-parameter)
Objects: 1
[0] 0x1f2f1e29a40, type: 1, name: NULL
What might be causing this issue and how can I fix it?
The issue might be caused due to the incorrect handling of pointers and references to VkDescriptorSetLayout objects. Specifically, you should store VkDescriptorSetLayout objects directly instead of pointers to those objects, which might get deallocated or go out of scope during runtime. I've made some changes to your code to reflect the correct handling of VkDescriptorSetLayout objects:
Change CreateDescriptorSetLayout() and CreateSamplerDescriptorSetLayout() return values from VkDescriptorSetLayout* to VkDescriptorSetLayout.
Change Material's descriptorSetLayout and samplerDescriptorSetLayout member variables from VkDescriptorSetLayout* to VkDescriptorSetLayout, and update relevant member functions accordingly.
Update the usage of CreateDescriptorSetLayout() and CreateSamplerDescriptorSetLayout() in Engine::Initialize() to directly store the returned VkDescriptorSetLayout objects.