I’m building a video game engine using C++ as the coding language and Vulkan for…
- 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. I am using the ASSIMP library to load 3d models and animations.
I am in the process of converting the codebase from using vulkan.h to vulkan.hpp, as well as utilising RAII. Here is the header and some source code from the Renderer class:
Renderer.h:
#pragma once
#include <vulkan/vulkan.hpp> #include “Window.h” #include #include #include #include #include #include “Pipeline.h” #include “Material.h” #include “Mesh.h”
struct QueueFamilyIndices { std::optional<uint32_t> graphicsFamily; std::optional<uint32_t> presentFamily;
bool IsComplete() { return graphicsFamily.has_value() && presentFamily.has_value(); }};
struct SwapChainSupportDetails { vk::SurfaceCapabilitiesKHR capabilities; std::vectorvk::SurfaceFormatKHR formats; std::vectorvk::PresentModeKHR presentModes; };
struct MVP { glm::mat4 model; glm::mat4 view; glm::mat4 projection; };
class Renderer { public: Renderer(); ~Renderer();
void Initialize(GLFWwindow* window); void Shutdown(); void BeginFrame(); void EndFrame(); vk::DescriptorSetLayout CreateDescriptorSetLayout(); vk::DescriptorPool CreateDescriptorPool(uint32_t maxSets); vk::Device* GetDevice(); vk::PhysicalDevice* GetPhysicalDevice(); vk::CommandPool* GetCommandPool(); vk::Queue* GetGraphicsQueue(); vk::CommandBuffer* GetCurrentCommandBuffer(); std::shared_ptr<Pipeline> GetPipeline(); void CreateGraphicsPipeline(Mesh* mesh, Material* material); std::pair<vk::Buffer, vk::DeviceMemory> RequestMvpBuffer();private:
bool isShutDown = false; static const uint32_t kMvpBufferCount = 3; std::vector<vk::Buffer> mvpBuffers; std::vector<vk::DeviceMemory> mvpBufferMemory; uint32_t currentMvpBufferIndex = 0; bool shutdownInProgress; uint32_t currentCmdBufferIndex = 0; std::vector<size_t> currentFramePerImage; std::vector<vk::Image> swapChainImages; std::vector<vk::ImageView> swapChainImageViews; vk::Extent2D swapChainExtent; vk::RenderPass renderPass; uint32_t imageIndex; std::shared_ptr<Pipeline> pipeline; vk::Format swapChainImageFormat; std::vector<vk::CommandBuffer> 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(); void Present(); GLFWwindow* window; vk::Instance instance; vk::PhysicalDevice physicalDevice; vk::Device device; vk::SurfaceKHR surface; vk::SwapchainKHR swapchain; vk::CommandPool commandPool; vk::CommandBuffer currentCommandBuffer; std::vector<vk::Framebuffer> framebuffers; // Additional Vulkan objects needed for rendering… const uint32_t kMaxFramesInFlight = 2; std::vector<vk::Semaphore> imageAvailableSemaphores; std::vector<vk::Semaphore> renderFinishedSemaphores; std::vector<vk::Fence> inFlightFences; size_t currentFrame; vk::Queue graphicsQueue; vk::Queue presentQueue; void CreateSyncObjects(); void CleanupSyncObjects(); SwapChainSupportDetails querySwapChainSupport(vk::PhysicalDevice device, vk::SurfaceKHR surface); vk::SurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<vk::SurfaceFormatKHR>& availableFormats); vk::PresentModeKHR chooseSwapPresentMode(const std::vector<vk::PresentModeKHR>& availablePresentModes); vk::Extent2D chooseSwapExtent(const vk::SurfaceCapabilitiesKHR& capabilities, GLFWwindow* window); std::vector<const char*> deviceExtensions = { VK_KHR_SWAPCHAIN_EXTENSION_NAME }; std::vector<const char*> CheckPhysicalDeviceExtensionSupport(vk::PhysicalDevice physicalDevice); QueueFamilyIndices GetQueueFamilyIndices(vk::PhysicalDevice physicalDevice);};
Renderer.cpp:
void Renderer::CreateInstance() { // Set up the application info vk::ApplicationInfo appInfo{}; 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 vk::InstanceCreateInfo createInfo{}; 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); std::vector<const char*> validationLayers;#ifdef NDEBUG const bool enableValidationLayers = false; #else const bool enableValidationLayers = true; validationLayers.push_back("VK_LAYER_KHRONOS_validation"); #endif
if (enableValidationLayers) { // Check if validation layers are supported uint32_t layerCount; vk::enumerateInstanceLayerProperties(&layerCount, nullptr); std::vector<vk::LayerProperties> availableLayers(layerCount); vk::enumerateInstanceLayerProperties(&layerCount, availableLayers.data()); for (const char* layerName : validationLayers) { bool layerFound = false; for (const auto& layerProperties : availableLayers) { if (strcmp(layerName, layerProperties.layerName) == 0) { layerFound = true; break; } } if (!layerFound) { throw std::runtime_error("Validation layer requested, but it’s not available."); } } // Enable the validation layers createInfo.enabledLayerCount(static_cast<uint32_t>(validationLayers.size())); createInfo.ppEnabledLayerNames(validationLayers.data()); } else { createInfo.enabledLayerCount(0); } // Create the Vulkan instance instance = vk::createInstance(createInfo);}
I am getting errors in the Renderer::CreateInstance method for both appInfo and createInfo. It says "expression preceding parantheses of apparent call must have (pointer-to-) function type". Do you know what the issue is and how to fix it?
- Assistant
The issue here is related to the use of parentheses to set the fields of a struct. This is not the correct syntax for C++.
To set the fields of a struct, you need to use the assignment operator
=. Additionally, because you're using the vulkan.hpp header, which provides a more C++-style API, I would recommend using the designated member initializer list to construct these structs instead.Here's the updated code:
void Renderer::CreateInstance() { // Set up the application info vk::ApplicationInfo appInfo( "Game Engine", // pApplicationName VK_MAKE_VERSION(1, 0, 0), // applicationVersion "Game Engine", // pEngineName VK_MAKE_VERSION(1, 0, 0), // engineVersion VK_API_VERSION_1_2 // apiVersion ); // Set up the required extensions uint32_t glfwExtensionCount = 0; const char** glfwExtensions; glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount); // Set up the instance create info vk::InstanceCreateInfo createInfo( {}, // flags &appInfo, // pApplicationInfo 0, // enabledLayerCount nullptr, // ppEnabledLayerNames glfwExtensionCount, // enabledExtensionCount glfwExtensions // ppEnabledExtensionNames ); // ... rest of the code }Now the structs
appInfoandcreateInfoare being initialized using their constructors with the designated member initializer list, and the issue should be resolved. - User
Can you provide the code to the entire method, including the changes you have made?
- Assistant
Sure, here's the updated
Renderer::CreateInstancemethod:void Renderer::CreateInstance() { // Set up the application info vk::ApplicationInfo appInfo( "Game Engine", // pApplicationName VK_MAKE_VERSION(1, 0, 0), // applicationVersion "Game Engine", // pEngineName VK_MAKE_VERSION(1, 0, 0), // engineVersion VK_API_VERSION_1_2 // apiVersion ); // Set up the required extensions uint32_t glfwExtensionCount = 0; const char** glfwExtensions; glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount); // Set up the instance create info vk::InstanceCreateInfo createInfo( {}, // flags &appInfo, // pApplicationInfo 0, // enabledLayerCount nullptr, // ppEnabledLayerNames glfwExtensionCount, // enabledExtensionCount glfwExtensions // ppEnabledExtensionNames ); std::vector<const char*> validationLayers; #ifdef NDEBUG const bool enableValidationLayers = false; #else const bool enableValidationLayers = true; validationLayers.push_back("VK_LAYER_KHRONOS_validation"); #endif if (enableValidationLayers) { // Check if validation layers are supported uint32_t layerCount; vk::enumerateInstanceLayerProperties(&layerCount, nullptr); std::vector<vk::LayerProperties> availableLayers(layerCount); vk::enumerateInstanceLayerProperties(&layerCount, availableLayers.data()); for (const char* layerName : validationLayers) { bool layerFound = false; for (const auto& layerProperties : availableLayers) { if (strcmp(layerName, layerProperties.layerName) == 0) { layerFound = true; break; } } if (!layerFound) { throw std::runtime_error("Validation layer requested, but it’s not available."); } } // Enable the validation layers createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size()); createInfo.ppEnabledLayerNames = validationLayers.data(); } else { createInfo.enabledLayerCount = 0; } // Create the Vulkan instance instance = vk::createInstance(createInfo); }So, the main changes are related to the initialization of the
vk::ApplicationInfoandvk::InstanceCreateInfostructs using the designated member initializer list. The rest of the code remains the same.