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.
void Renderer::CreateSurface()
{
if (glfwCreateWindowSurface(instance, window, nullptr, &surface) != VK_SUCCESS)
{
throw std::runtime_error("Failed to create a window surface.");
}
}
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())
{
VkPhysicalDeviceFeatures supportedFeatures;
vkGetPhysicalDeviceFeatures(testDevice, &supportedFeatures);
// Check for anisotropic filtering support
if (supportedFeatures.samplerAnisotropy) {
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{};
deviceFeatures.samplerAnisotropy = VK_TRUE; // Enable anisotropic filtering
// 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()
{
// Reset the pipeline first to ensure it’s destroyed before the device
pipeline.reset();
// Destroy the logical device
vkDestroyDevice(device, nullptr);
}
Here's an updated version of CreateSurface() that uses Vulkan.hpp and RAII:
void Renderer::CreateSurface()
{
vk::Result result;
VkSurfaceKHR tempSurface;
result = static_cast<vk::Result>(glfwCreateWindowSurface(instance, window, nullptr, &tempSurface));
if (result != vk::Result::eSuccess)
{
throw std::runtime_error("Failed to create a window surface.");
}
surface = tempSurface;
}
Your DestroySurface() method doesn’t need any changes since you are already using a vk::SurfaceKHR. Also, it would make more sense to move the DestroySurface() method's contents to the destructor of the Renderer class since that's where RAII should be utilized.
Now let's update the CreateInstance() and CleanupInstance() methods to use Vulkan.hpp and RAII:
void Renderer::CreateInstance()
{
// Set up the application info
vk::ApplicationInfo appInfo{"Game Engine",
VK_MAKE_VERSION(1, 0, 0),
"Game Engine",
VK_MAKE_VERSION(1, 0, 0),
VK_API_VERSION_1_2};
// Set up the required extensions
uint32_t glfwExtensionCount = 0;
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
// Check if validation layers are supported
#ifdef NDEBUG
const bool enableValidationLayers = false;
#else
const bool enableValidationLayers = true;
std::vector<const char*> validationLayers{"VK_LAYER_KHRONOS_validation"};
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.");
}
}
#endif
// Set up the instance create info
vk::InstanceCreateInfo createInfo{{},
&appInfo,
enableValidationLayers ? static_cast<uint32_t>(validationLayers.size()) : 0,
enableValidationLayers ? validationLayers.data() : nullptr,
glfwExtensionCount,
glfwExtensions};
// Create the Vulkan instance
vk::Result result = vk::createInstance(&createInfo, nullptr, &instance);
if (result != vk::Result::eSuccess)
{
throw std::runtime_error("Failed to create the Vulkan instance.");
}
}
void Renderer::CleanupInstance()
{
instance.destroy();
}
The other methods in the provided code snippet can also be updated to use the Vulkan.hpp library in a similar way. The cleanup methods can preferably be moved to the destructor to properly utilize RAII.