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.
Can you modify the following header file to properly utilise Vulkan.hpp and RAII? We will then begin updating the class source file to conform to the new header and then expand to the broader codebase.
I am in the process of modifying the Renderer class to properly utilise Vulkan.hpp and RAII. Here are the old and new header files:
Please update the following portion of the Renderer.cpp to conform to the new header and to properly utilise both Vulkan.hpp and RAII. Notify me if there are no changes required.
for (uint32_t i = 0; i < kMvpBufferCount; ++i)
{
BufferUtils::CreateBuffer(device, *GetPhysicalDevice(),
sizeof(MVP), vk::BufferUsageFlagBits::eUniformBuffer,
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent,
mvpBuffers[i], mvpBufferMemory[i]);
}
CreateSwapchain();
CreateRenderPass();
CreateCommandPool();
CreateFramebuffers();
CreateSyncObjects();
}
void Renderer::Shutdown()
{
if (isShutDown) {
return;
}
code
if (shutdownInProgress) {
return;
}
shutdownInProgress = true;
if (device) {
device.waitIdle();
}
CleanupFramebuffers();
CleanupRenderPass();
CleanupSyncObjects();
CleanupCommandPool();
CleanupImageViews();
CleanupSwapchain();
// Destroy the MVP buffers
for (uint32_t i = 0; i < kMvpBufferCount; ++i)
{
device.destroyBuffer(mvpBuffers[i], nullptr);
device.freeMemory(mvpBufferMemory[i], nullptr);
}
if (device) {
CleanupDevice();
}
DestroySurface();
CleanupInstance();
shutdownInProgress = false;
isShutDown = true;
}
void Renderer::BeginFrame()
{
// Wait for any previous work on this swapchain image to complete
device.waitForFences(1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
device.resetFences(1, &inFlightFences[currentFrame]);
code
// Acquire an image from the swapchain, then begin recording commands for the current frame.
vk::Result acquireResult = device.acquireNextImageKHR(swapchain, UINT64_MAX, imageAvailableSemaphores[currentFrame], vk::Fence(), &imageIndex);
if (acquireResult != vk::Result::eSuccess && acquireResult != vk::Result::eSuboptimalKHR) {
throw std::runtime_error("Failed to acquire next swapchain image.");
}
vk::CommandBufferBeginInfo beginInfo{};
beginInfo.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit;
currentCmdBufferIndex = (currentCmdBufferIndex + 1) % 2;
currentCommandBuffer = commandBuffers[currentFrame * 2 + currentCmdBufferIndex];
currentCommandBuffer.begin(beginInfo);
vk::RenderPassBeginInfo renderPassInfo{};
renderPassInfo.renderPass = renderPass;
renderPassInfo.framebuffer = framebuffers[imageIndex];
renderPassInfo.renderArea.offset = { 0, 0 };
renderPassInfo.renderArea.extent = swapChainExtent;
// Set the clear color to black
vk::ClearValue clearColor = { vk::ClearColorValue(std::array<float, 4>{0.0f, 0.0f, 0.0f, 1.0f}) };
renderPassInfo.clearValueCount = 1;
renderPassInfo.pClearValues = &clearColor;
currentCommandBuffer.beginRenderPass(renderPassInfo, vk::SubpassContents::eInline);