Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

I’m building a video game engine using C++ as the coding language and Vulkan for…

  1. User

    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.

    As a start, this engine will render a basic spinning box with uniform color. However, it will then expand to cover the full functionality of the game engine.

    I am having some issues with the Renderer class. Here is the code for both the header and the source file:

    Renderer.h:

    #pragma once

    #include <vulkan/vulkan.h> #include "Window.h" #include #include #include #include

    struct QueueFamilyIndices { std::optional<uint32_t> graphicsFamily; std::optional<uint32_t> presentFamily;

    bool IsComplete()
    {
    	return graphicsFamily.has_value() && presentFamily.has_value();
    }
    

    };

    struct SwapChainSupportDetails { VkSurfaceCapabilitiesKHR capabilities; std::vector formats; std::vector presentModes; };

    class Renderer { public: Renderer(); ~Renderer();

    void Initialize(GLFWwindow* window);
    void Shutdown();
    
    void BeginFrame();
    void EndFrame();
    

    private:

    std::vector<VkImage> swapChainImages;
    std::vector<VkImageView> swapChainImageViews;
    VkExtent2D swapChainExtent;
    VkRenderPass renderPass;
    
    VkFormat swapChainImageFormat;
    
    std::vector<VkCommandBuffer> commandBuffers;
    
    void CreateImageViews();
    void CleanupImageViews();
    void CreateRenderPass();
    void CleanupRenderPass();
    
    void CreateSurface();
    void DestroySurface();
    
    void CreateInstance();
    void CleanupInstance();
    void ChoosePhysicalDevice();
    void CreateDevice();
    void CleanupDevice();
    void CreateSwapchain();
    void CleanupSwapchain();
    void CreateCommandPool();
    void CleanupCommandPool();
    void CreateFramebuffers();
    void CleanupFramebuffers();
    
    void CreateCommandBuffers();
    void CleanupCommandBuffers();
    
    GLFWwindow* window;
    VkInstance instance;
    VkPhysicalDevice physicalDevice;
    VkDevice device;
    
    VkSurfaceKHR surface;
    VkSwapchainKHR swapchain;
    VkCommandPool commandPool;
    VkCommandBuffer currentCommandBuffer;
    
    std::vector<VkFramebuffer> framebuffers;
    
    // Additional Vulkan objects needed for rendering…
    
    const uint32_t kMaxFramesInFlight = 2;
    std::vector<VkSemaphore> imageAvailableSemaphores;
    std::vector<VkSemaphore> renderFinishedSemaphores;
    std::vector<VkFence> inFlightFences;
    size_t currentFrame;
    
    VkQueue graphicsQueue;
    VkQueue presentQueue;
    
    void CreateSyncObjects();
    void CleanupSyncObjects();
    
    SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device, VkSurfaceKHR surface);
    VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats);
    VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes);
    VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, GLFWwindow* window);
    
    std::vector<const char*> deviceExtensions = {
    	VK_KHR_SWAPCHAIN_EXTENSION_NAME
    };
    
    std::vector<const char*> CheckPhysicalDeviceExtensionSupport(VkPhysicalDevice physicalDevice);
    QueueFamilyIndices GetQueueFamilyIndices(VkPhysicalDevice physicalDevice);
    

    };

    Renderer.cpp:

    #include "Renderer.h"

    Renderer::Renderer() { }

    Renderer::~Renderer() { Shutdown(); }

    void Renderer::Initialize(GLFWwindow* window) { this->window = window; CreateInstance(); CreateSurface(); ChoosePhysicalDevice(); CreateDevice(); CreateSwapchain(); CreateRenderPass(); CreateCommandPool(); CreateFramebuffers(); CreateSyncObjects(); }

    void Renderer::Shutdown() { CleanupFramebuffers(); CleanupCommandPool(); CleanupRenderPass(); CleanupSwapchain(); DestroySurface(); CleanupDevice(); CleanupInstance(); CleanupSyncObjects(); }

    void Renderer::BeginFrame() { // Acquire an image from the swapchain, then begin recording commands for the current frame. vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX); vkResetFences(device, 1, &inFlightFences[currentFrame]);

    uint32_t imageIndex;
    vkAcquireNextImageKHR(device, swapchain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
    
    VkCommandBufferBeginInfo beginInfo{};
    beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
    currentCommandBuffer = commandBuffers[currentFrame];
    vkBeginCommandBuffer(currentCommandBuffer, &beginInfo);
    

    }

    void Renderer::EndFrame() { // Acquire the next available swapchain image uint32_t imageIndex; VkResult acquireResult = vkAcquireNextImageKHR(device, swapchain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);

    if (acquireResult == VK_ERROR_OUT_OF_DATE_KHR) {
    	// Handle swapchain recreation if needed, e.g. due to resizing the window or other swapchain properties changes
    	return;
    }
    else if (acquireResult != VK_SUCCESS && acquireResult != VK_SUBOPTIMAL_KHR) {
    	throw std::runtime_error("Failed to acquire next swapchain image.");
    }
    
    // Wait for any previous work on this swapchain image to complete
    vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
    
    // Finish recording commands, then present the rendered frame back to the swapchain.
    VkSubmitInfo submitInfo{};
    submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
    
    VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
    submitInfo.waitSemaphoreCount = 1;
    submitInfo.pWaitSemaphores = &imageAvailableSemaphores[currentFrame];
    submitInfo.pWaitDstStageMask = waitStages;
    
    submitInfo.commandBufferCount = 1;
    submitInfo.pCommandBuffers = &currentCommandBuffer;
    
    submitInfo.signalSemaphoreCount = 1;
    submitInfo.pSignalSemaphores = &renderFinishedSemaphores[currentFrame];
    
    vkEndCommandBuffer(currentCommandBuffer);
    vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]);
    
    VkPresentInfoKHR presentInfo{};
    presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
    presentInfo.waitSemaphoreCount = 1;
    presentInfo.pWaitSemaphores = &renderFinishedSemaphores[currentFrame];
    
    VkSwapchainKHR swapChains[] = { swapchain };
    presentInfo.swapchainCount = 1;
    presentInfo.pSwapchains = swapChains;
    presentInfo.pImageIndices = &imageIndex;
    
    VkResult queuePresentResult = vkQueuePresentKHR(presentQueue, &presentInfo);
    
    if (queuePresentResult == VK_ERROR_OUT_OF_DATE_KHR || queuePresentResult == VK_SUBOPTIMAL_KHR) {
    	// Handle swapchain recreation if needed, e.g. due to resizing the window or other swapchain properties changes
    }
    else if (queuePresentResult != VK_SUCCESS) {
    	throw std::runtime_error("Failed to present the swapchain image.");
    }
    
    currentFrame = (currentFrame + 1) % kMaxFramesInFlight;
    

    }

    void Renderer::CreateSurface() { if (glfwCreateWindowSurface(instance, window, nullptr, &surface) != VK_SUCCESS) { throw std::runtime_error("Failed to create a window surface."); } }

    void Renderer::DestroySurface() { vkDestroySurfaceKHR(instance, surface, nullptr); }

    void Renderer::CreateInstance() { // Set up the application info VkApplicationInfo appInfo{}; appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; appInfo.pApplicationName = "Game Engine"; appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0); appInfo.pEngineName = "Game Engine"; appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0); appInfo.apiVersion = VK_API_VERSION_1_2;

    // Set up the instance create info
    VkInstanceCreateInfo createInfo{};
    createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    createInfo.pApplicationInfo = &appInfo;
    
    // Set up the required extensions
    uint32_t glfwExtensionCount = 0;
    const char** glfwExtensions;
    glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
    
    createInfo.enabledExtensionCount = glfwExtensionCount;
    createInfo.ppEnabledExtensionNames = glfwExtensions;
    createInfo.enabledLayerCount = 0;
    
    // Create the Vulkan instance
    if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS)
    {
    	throw std::runtime_error("Failed to create the Vulkan instance.");
    }
    

    }

    void Renderer::CleanupInstance() { // Destroy the Vulkan instance vkDestroyInstance(instance, nullptr); }

    void Renderer::ChoosePhysicalDevice() { // Enumerate the available physical devices and choose one that supports required features uint32_t deviceCount = 0; vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);

    if (deviceCount == 0)
    {
    	throw std::runtime_error("Failed to find a GPU with Vulkan support.");
    }
    
    std::vector<VkPhysicalDevice> allDevices(deviceCount);
    vkEnumeratePhysicalDevices(instance, &deviceCount, allDevices.data());
    
    for (const auto& testDevice : allDevices)
    {
    	if (glfwGetPhysicalDevicePresentationSupport(instance, testDevice, 0) &&
    		CheckPhysicalDeviceExtensionSupport(testDevice).empty() &&
    		GetQueueFamilyIndices(testDevice).IsComplete())
    	{
    		physicalDevice = testDevice;
    		break;
    	}
    }
    
    if (physicalDevice == VK_NULL_HANDLE)
    {
    	throw std::runtime_error("Failed to find a suitable GPU.");
    }
    

    }

    void Renderer::CreateDevice() { // Get the GPU’s queue family indices const QueueFamilyIndices indices = GetQueueFamilyIndices(physicalDevice);

    // Set up the device queue create info
    std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;
    std::set<uint32_t> uniqueQueueFamilyIndices = { indices.graphicsFamily.value(),indices.presentFamily.value() };
    
    float queuePriority = 1.0f;
    for (uint32_t queueFamilyIndex : uniqueQueueFamilyIndices)
    {
    	VkDeviceQueueCreateInfo queueCreateInfo{};
    	queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
    	queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
    	queueCreateInfo.queueCount = 1;
    	queueCreateInfo.pQueuePriorities = &queuePriority;
    
    	queueCreateInfos.push_back(queueCreateInfo);
    }
    
    // Set up the physical device features
    VkPhysicalDeviceFeatures deviceFeatures{};
    
    // Set up the device create info
    VkDeviceCreateInfo createInfo{};
    createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
    createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
    createInfo.pQueueCreateInfos = queueCreateInfos.data();
    createInfo.pEnabledFeatures = &deviceFeatures;
    createInfo.enabledExtensionCount = static_cast<uint32_t>(deviceExtensions.size());
    createInfo.ppEnabledExtensionNames = deviceExtensions.data();
    
    // Create the logical device
    if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS)
    {
    	throw std::runtime_error("Failed to create a logical device.");
    }
    
    // Retrieve the graphics queue and the present queue
    vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue);
    vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
    

    }

    void Renderer::CleanupDevice() { // Destroy the logical device vkDestroyDevice(device, nullptr); }

    void Renderer::CreateSwapchain() { // Get swapchain support details SwapChainSupportDetails swapChainSupport = querySwapChainSupport(physicalDevice,surface);

    VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.formats);
    swapChainImageFormat = surfaceFormat.format; // Initialize the swapChainImageFormat
    VkPresentModeKHR presentMode = chooseSwapPresentMode(swapChainSupport.presentModes);
    VkExtent2D extent = chooseSwapExtent(swapChainSupport.capabilities,window);
    
    uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
    if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount)
    {
    	imageCount = swapChainSupport.capabilities.maxImageCount;
    }
    
    // Create the swapchain
    // …
    VkSwapchainCreateInfoKHR createInfo{};
    createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
    createInfo.surface = surface;
    
    createInfo.minImageCount = imageCount;
    createInfo.imageFormat = surfaceFormat.format;
    createInfo.imageColorSpace = surfaceFormat.colorSpace;
    createInfo.imageExtent = extent;
    createInfo.imageArrayLayers = 1;
    createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
    
    QueueFamilyIndices indices = GetQueueFamilyIndices(physicalDevice);
    uint32_t queueFamilyIndices[] = { indices.graphicsFamily.value(), indices.presentFamily.value() };
    
    if (indices.graphicsFamily != indices.presentFamily) {
    	createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
    	createInfo.queueFamilyIndexCount = 2;
    	createInfo.pQueueFamilyIndices = queueFamilyIndices;
    }
    else {
    	createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
    }
    
    createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
    createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
    createInfo.presentMode = presentMode;
    createInfo.clipped = VK_TRUE;
    
    if (vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapchain) != VK_SUCCESS) {
    	throw std::runtime_error("failed to create swap chain!");
    }
    
    // Retrieve swapchain images (color buffers)
    // …
    
    // Retrieve swapchain images
    vkGetSwapchainImagesKHR(device, swapchain, &imageCount, nullptr);
    swapChainImages.resize(imageCount);
    vkGetSwapchainImagesKHR(device, swapchain, &imageCount, swapChainImages.data());
    
    // Create image views for swapchain images
    CreateImageViews();
    

    }

    void Renderer::CleanupSwapchain() { // Clean up Vulkan swapchain }

    void Renderer::CreateImageViews() { swapChainImageViews.resize(swapChainImages.size());

    for (size_t i = 0; i < swapChainImages.size(); ++i)
    {
    	VkImageViewCreateInfo createInfo{};
    	createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
    	createInfo.image = swapChainImages[i];
    	createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
    	createInfo.format = swapChainImageFormat;
    	createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
    	createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
    	createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
    	createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
    	createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
    	createInfo.subresourceRange.baseMipLevel = 0;
    	createInfo.subresourceRange.levelCount = 1;
    	createInfo.subresourceRange.baseArrayLayer = 0;
    	createInfo.subresourceRange.layerCount = 1;
    	createInfo.flags = 0;
    
    	if (vkCreateImageView(device, &createInfo, nullptr, &swapChainImageViews[i]) != VK_SUCCESS)
    	{
    		throw std::runtime_error("Failed to create an image view.");
    	}
    }
    

    }

    void Renderer::CleanupImageViews() { for (auto imageView : swapChainImageViews) { vkDestroyImageView(device, imageView, nullptr); } swapChainImageViews.clear(); }

    void Renderer::CreateRenderPass() { VkAttachmentDescription colorAttachment{}; colorAttachment.format = swapChainImageFormat; colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT; colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;

    VkAttachmentReference colorAttachmentRef{};
    colorAttachmentRef.attachment = 0;
    colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
    
    VkSubpassDescription subpass{};
    subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
    subpass.colorAttachmentCount = 1;
    subpass.pColorAttachments = &colorAttachmentRef;
    
    VkRenderPassCreateInfo renderPassInfo{};
    renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
    renderPassInfo.attachmentCount = 1;
    renderPassInfo.pAttachments = &colorAttachment;
    renderPassInfo.subpassCount = 1;
    renderPassInfo.pSubpasses = &subpass;
    
    VkSubpassDependency dependency{};
    dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
    dependency.dstSubpass = 0;
    dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
    dependency.srcAccessMask = 0;
    dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
    dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
    
    renderPassInfo.dependencyCount = 1;
    renderPassInfo.pDependencies = &dependency;
    
    if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS)
    {
    	throw std::runtime_error("Failed to create render pass.");
    }
    

    }

    void Renderer::CleanupRenderPass() { vkDestroyRenderPass(device, renderPass, nullptr); }

    void Renderer::CreateCommandPool() { // Find a queue family index that supports graphics operations QueueFamilyIndices queueFamilyIndices = GetQueueFamilyIndices(physicalDevice);

    // Create a command pool for the queue family
    VkCommandPoolCreateInfo poolInfo{};
    poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
    poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily.value();
    poolInfo.flags = 0;
    
    if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS)
    {
    	throw std::runtime_error("Failed to create command pool.");
    }
    
    CreateCommandBuffers(); // Create command buffers after creating the command pool
    

    }

    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); }

    void Renderer::CreateCommandBuffers() { commandBuffers.resize(kMaxFramesInFlight);

    VkCommandBufferAllocateInfo allocInfo{};
    allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
    allocInfo.commandPool = commandPool;
    allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
    allocInfo.commandBufferCount = static_cast<uint32_t>(commandBuffers.size());
    
    if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS)
    {
    	throw std::runtime_error("Failed to allocate command buffers.");
    }
    
    // Set the initial value of the currentCommandBuffer
    currentCommandBuffer = commandBuffers[currentFrame];
    

    }

    void Renderer::CleanupCommandBuffers() { vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data()); }

    void Renderer::CreateFramebuffers() { // Create Vulkan framebuffers for swapchain images framebuffers.resize(swapChainImageViews.size());

    for (size_t i = 0; i < swapChainImageViews.size(); ++i)
    {
    	VkImageView attachments[] = { swapChainImageViews[i] };
    
    	VkFramebufferCreateInfo framebufferInfo{};
    	framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
    	framebufferInfo.renderPass = renderPass;
    	framebufferInfo.attachmentCount = 1;
    	framebufferInfo.pAttachments = attachments;
    	framebufferInfo.width = swapChainExtent.width;
    	framebufferInfo.height = swapChainExtent.height;
    	framebufferInfo.layers = 1;
    
    	if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, &framebuffers[i]) != VK_SUCCESS)
    	{
    		throw std::runtime_error("Failed to create framebuffer.");
    	}
    }
    

    }

    void Renderer::CleanupFramebuffers() { // Clean up Vulkan framebuffers }

    void Renderer::CreateSyncObjects() { imageAvailableSemaphores.resize(kMaxFramesInFlight); renderFinishedSemaphores.resize(kMaxFramesInFlight); inFlightFences.resize(kMaxFramesInFlight);

    VkSemaphoreCreateInfo semaphoreInfo{};
    semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
    
    VkFenceCreateInfo fenceInfo{};
    fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
    fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
    
    for (size_t i = 0; i < kMaxFramesInFlight; ++i)
    {
    	if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]) != VK_SUCCESS ||
    		vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS ||
    		vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS)
    	{
    		throw std::runtime_error("Failed to create synchronization objects for a frame.");
    	}
    }
    

    }

    void Renderer::CleanupSyncObjects() { for (size_t i = 0; i < kMaxFramesInFlight; ++i) { vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr); vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr); vkDestroyFence(device, inFlightFences[i], nullptr); } }

    SwapChainSupportDetails Renderer::querySwapChainSupport(VkPhysicalDevice device, VkSurfaceKHR surface) { SwapChainSupportDetails details;

    // Query the capabilities
    vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities);
    
    // Query the supported formats
    uint32_t formatCount;
    vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, nullptr);
    
    if (formatCount != 0)
    {
    	details.formats.resize(formatCount);
    	vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats.data());
    }
    
    // Query the supported present modes
    uint32_t presentModeCount;
    vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, nullptr);
    
    if (presentModeCount != 0)
    {
    	details.presentModes.resize(presentModeCount);
    	vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.presentModes.data());
    }
    
    return details;
    

    }

    VkSurfaceFormatKHR Renderer::chooseSwapSurfaceFormat(const std::vector& availableFormats) { for (const auto& availableFormat : availableFormats) { if (availableFormat.format == VK_FORMAT_B8G8R8A8_SRGB && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { return availableFormat; } } return availableFormats[0]; }

    VkPresentModeKHR Renderer::chooseSwapPresentMode(const std::vector& availablePresentModes) { for (const auto& availablePresentMode : availablePresentModes) { if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) { return availablePresentMode; } } return VK_PRESENT_MODE_FIFO_KHR; }

    VkExtent2D Renderer::chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, GLFWwindow* window) { if (capabilities.currentExtent.width != UINT32_MAX) { return capabilities.currentExtent; } else { int width, height; glfwGetFramebufferSize(window, &width, &height);

    	VkExtent2D actualExtent = { static_cast<uint32_t>(width), static_cast<uint32_t>(height) };
    
    	actualExtent.width = std::max(capabilities.minImageExtent.width, std::min(capabilities.maxImageExtent.width, actualExtent.width));
    	actualExtent.height = std::max(capabilities.minImageExtent.height, std::min(capabilities.maxImageExtent.height, actualExtent.height));
    
    	return actualExtent;
    }
    

    }

    std::vector<const char*> Renderer::CheckPhysicalDeviceExtensionSupport(VkPhysicalDevice physicalDevice) { uint32_t extensionCount; vkEnumerateDeviceExtensionProperties(physicalDevice, nullptr, &extensionCount, nullptr);

    std::vector<VkExtensionProperties> availableExtensions(extensionCount);
    vkEnumerateDeviceExtensionProperties(physicalDevice, nullptr, &extensionCount, availableExtensions.data());
    
    std::set<std::string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
    for (const auto& extension : availableExtensions)
    {
    	requiredExtensions.erase(extension.extensionName);
    }
    
    std::vector<const char*> remainingExtensions;
    for (const auto& extension : requiredExtensions)
    {
    	remainingExtensions.push_back(extension.c_str());
    }
    return remainingExtensions;
    

    }

    QueueFamilyIndices Renderer::GetQueueFamilyIndices(VkPhysicalDevice physicalDevice) { QueueFamilyIndices indices;

    uint32_t queueFamilyCount = 0;
    vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, nullptr);
    
    std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
    vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, queueFamilies.data());
    
    int i = 0;
    for (const auto& queueFamily : queueFamilies)
    {
    	if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT)
    	{
    		indices.graphicsFamily = i;
    	}
    
    	VkBool32 presentSupport = false;
    	vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, i, surface, &presentSupport);
    	if (presentSupport)
    	{
    		indices.presentFamily = i;
    	}
    
    	if (indices.IsComplete())
    	{
    		break;
    	}
    
    	i++;
    }
    
    return indices;
    

    }

    The code seems to get stuck in the EndFrame method in the Renderer class at this line:

    vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);

    Do you know what the issue could be and how to fix it?

  2. Assistant

    The issue might be caused by not properly resetting and submitting the command buffer on every frame. I noticed you're recording the command buffer in BeginFrame() and submitting it in EndFrame(). However, the actual drawing commands are missing in between, so the command buffer might not actually have any work sent to the GPU.

    Now, let's insert a simple drawing pass between BeginFrame() and EndFrame(). Since you already set up a spinning cube scenario, add the drawing pass that records and executes drawing commands for your spinning box before the synchronization objects in EndFrame():

    Renderer.h:

    void DrawFrame();
    

    Renderer.cpp:

    void Renderer::DrawFrame()
    {
        // Here, insert the drawing commands for your spinning box.
        // We'll assume you have a pipeline and buffer objects and render pass configured
        // Make sure to use the currentCommandBuffer for recording commands
    }
    
    void Renderer::EndFrame()
    {
        DrawFrame(); // Call the drawing method here
    
        // The rest of your EndFrame() code
    }
    

    This should ensure there's work being submitted to the GPU for every frame, potentially fixing the synchronization issue. If this doesn't resolve the problem, it's possible there are other issues in your setup that may require a deeper investigation.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.