From dbd28d57b4e995a707f2783815ae2f7ea59cd722 Mon Sep 17 00:00:00 2001 From: Warwick Date: Tue, 5 Dec 2023 10:26:23 +0000 Subject: [PATCH] Added a logical queue and graphics queue. --- src/yave_vulkan_instance.cpp | 56 ++++++++++++++++++++++++++++++++++-- src/yave_vulkan_instance.hpp | 7 +++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/yave_vulkan_instance.cpp b/src/yave_vulkan_instance.cpp index 3407bee..e3d0128 100644 --- a/src/yave_vulkan_instance.cpp +++ b/src/yave_vulkan_instance.cpp @@ -7,8 +7,12 @@ namespace yave { YaveVulkanInstance::YaveVulkanInstance() { createInstance(); pickPhysicalDevice(); + createLogicalDevice(); +} +YaveVulkanInstance::~YaveVulkanInstance() { + destroyLogicalDevice(); + destroyInstance(); } -YaveVulkanInstance::~YaveVulkanInstance() { destroyInstance(); } void YaveVulkanInstance::createInstance() { VkApplicationInfo appInfo{}; @@ -139,7 +143,7 @@ YaveVulkanInstance::findQueueFamilies(VkPhysicalDevice device) { indices.graphicsFamily = i; } // If we know we have support we don't need to look for anything more - if(indices.isComplete()){ + if (indices.isComplete()) { break; } i++; @@ -147,4 +151,52 @@ YaveVulkanInstance::findQueueFamilies(VkPhysicalDevice device) { return indices; } +void YaveVulkanInstance::createLogicalDevice() { + QueueFamilyIndices indices = findQueueFamilies(physicalDevice); + + // Only need a single queue as we can feed it with multiple threads and + // trigger it from the main thread to start off with. + VkDeviceQueueCreateInfo queueCreateInfo{}; + queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; + queueCreateInfo.queueFamilyIndex = indices.graphicsFamily.value(); + queueCreateInfo.queueCount = 1; + // Since we only have one queue it has all the priority. + float queuePriority = 1.0f; + queueCreateInfo.pQueuePriorities = &queuePriority; + + // TODO: set up device with interesting features as we use them + VkPhysicalDeviceFeatures deviceFeatures{}; + + // Create logical device. + VkDeviceCreateInfo createInfo{}; + createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; + createInfo.pQueueCreateInfos = &queueCreateInfo; + createInfo.queueCreateInfoCount = 1; + createInfo.pEnabledFeatures = &deviceFeatures; + + // TODO: Require specific features when we need some + // Device specific create information + createInfo.enabledExtensionCount = 0; + if (enableValidationLayers) { + createInfo.enabledLayerCount = + static_cast(validationLayers.size()); + createInfo.ppEnabledLayerNames = validationLayers.data(); + } else { + createInfo.enabledLayerCount = 0; + } + + // Create logical device + if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != + VK_SUCCESS) { + throw std::runtime_error("failed to create logical device!"); + } + + // Whilst we're here we might as well set up our queue + vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue); +} + +void YaveVulkanInstance::destroyLogicalDevice() { + vkDestroyDevice(device, nullptr); +} + } // namespace yave diff --git a/src/yave_vulkan_instance.hpp b/src/yave_vulkan_instance.hpp index d945099..9cbe988 100644 --- a/src/yave_vulkan_instance.hpp +++ b/src/yave_vulkan_instance.hpp @@ -22,6 +22,8 @@ class YaveVulkanInstance { private: VkInstance instance; VkPhysicalDevice physicalDevice; + VkDevice device; + VkQueue graphicsQueue; // Vulkan instance void createInstance(); @@ -33,6 +35,11 @@ private: bool isDeviceSuitable(VkPhysicalDevice device); QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device); + // TODO: Move Logical device code to it's own class + // Logical device + void createLogicalDevice(); + void destroyLogicalDevice(); + // Validation support bool checkLayersValidSuppport(VkInstanceCreateInfo &createInfo); const std::vector validationLayers = {