diff options
Diffstat (limited to 'src/window.cpp')
| -rw-r--r-- | src/window.cpp | 75 |
1 files changed, 71 insertions, 4 deletions
diff --git a/src/window.cpp b/src/window.cpp index 0f63349..66473a7 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -13,8 +13,11 @@ GLFWwindow *window = nullptr; VkInstance vkInstance = nullptr; VkPhysicalDevice vkPhysicalDevice = nullptr; +VkDevice vkDevice = nullptr; VkSurfaceKHR vkSurfaceKHR = nullptr; VkDebugReportCallbackEXT vkDebugReport = nullptr; +VkAllocationCallbacks vkAllocatorStruct = {}; +VkAllocationCallbacks *vkAllocator = nullptr; const bool ENABLE_VALIDATION_LAYERS = true; const bool VULKAN_DEBUG_REPORT = true; @@ -192,11 +195,13 @@ void InitVulkan() { assert(physicalDeviceCount > 0); std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount); vkEnumeratePhysicalDevices(vkInstance, &physicalDeviceCount, physicalDevices.data()); + unsigned int graphicsFamilyIndex = 0; + unsigned int presentFamilyIndex = 0; for (const auto &device : physicalDevices) { // check queue families - auto graphicsFamilyIndex = FindQueueFamilyIndex(device, -1, VK_QUEUE_GRAPHICS_BIT); - auto presentFamilyIndex = FindQueueFamilyIndex(device, 1); + graphicsFamilyIndex = FindQueueFamilyIndex(device, -1, VK_QUEUE_GRAPHICS_BIT); + presentFamilyIndex = FindQueueFamilyIndex(device, 1); if (graphicsFamilyIndex == 0xFFFFFFFF || presentFamilyIndex == 0xFFFFFFFF) { continue; } @@ -240,6 +245,66 @@ void InitVulkan() { } assert(vkPhysicalDevice != nullptr && "Failed to find suitable physical device"); + // Create logical device + { + unsigned int transferQueueIndex = FindQueueFamilyIndex(vkPhysicalDevice, 0, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_GRAPHICS_BIT); + if (transferQueueIndex == 0xFFFFFFFF) { + transferQueueIndex = FindQueueFamilyIndex(vkPhysicalDevice, -1, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_GRAPHICS_BIT); + } + if (transferQueueIndex == 0xFFFFFFFF) { + transferQueueIndex = FindQueueFamilyIndex(vkPhysicalDevice, -1, VK_QUEUE_TRANSFER_BIT); + } + if (transferQueueIndex == 0xFFFFFFFF) { + printf("Failed to find transfer queue index"); + throw("Failed to find transfer queue index"); + } + + VkDeviceQueueCreateInfo deviceQueueCreateInfos[2]; + + float graphicsPriorities[1] = { 1.0f }; + deviceQueueCreateInfos[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; + deviceQueueCreateInfos[0].pNext = nullptr; + deviceQueueCreateInfos[0].flags = 0; + deviceQueueCreateInfos[0].queueFamilyIndex = graphicsFamilyIndex; + deviceQueueCreateInfos[0].queueCount = 1l; + deviceQueueCreateInfos[0].pQueuePriorities = graphicsPriorities; + + float transferPriorities[1] = { 0.5f }; + deviceQueueCreateInfos[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; + deviceQueueCreateInfos[1].pNext = nullptr; + deviceQueueCreateInfos[1].flags = 0; + deviceQueueCreateInfos[1].queueFamilyIndex = transferQueueIndex; + deviceQueueCreateInfos[1].queueCount = 1l; + deviceQueueCreateInfos[1].pQueuePriorities = transferPriorities; + + VkPhysicalDeviceFeatures vkPhysicalDeviceFeatures{}; + vkGetPhysicalDeviceFeatures(vkPhysicalDevice, &vkPhysicalDeviceFeatures); + + VkPhysicalDeviceFeatures requestedFeatures{}; // {} to initialize everything to 0 + requestedFeatures.samplerAnisotropy = VK_TRUE; + requestedFeatures.sampleRateShading = VK_TRUE; + requestedFeatures.fillModeNonSolid = vkPhysicalDeviceFeatures.fillModeNonSolid; + + VkDeviceCreateInfo vkDeviceCreateInfo{}; + vkDeviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; + vkDeviceCreateInfo.flags = 0; + vkDeviceCreateInfo.pNext = nullptr; + vkDeviceCreateInfo.queueCreateInfoCount = 2; + vkDeviceCreateInfo.pQueueCreateInfos = deviceQueueCreateInfos; + vkDeviceCreateInfo.enabledExtensionCount = REQUIRED_EXTENSIONS.size(); + vkDeviceCreateInfo.ppEnabledExtensionNames = REQUIRED_EXTENSIONS.data(); + vkDeviceCreateInfo.pEnabledFeatures = &requestedFeatures; + if (ENABLE_VALIDATION_LAYERS) { + vkDeviceCreateInfo.enabledLayerCount = enabledLayerNames.size(); + vkDeviceCreateInfo.ppEnabledLayerNames = enabledLayerNames.data(); + } + + result = vkCreateDevice(vkPhysicalDevice, &vkDeviceCreateInfo, vkAllocator, &vkDevice); + if (result != VK_SUCCESS) { + printf("Failed to create VkInstance! : %d\n", result); + throw result; + } + } } void CreateWindow(PKEWindowProperties *wp) { @@ -251,12 +316,14 @@ void CreateWindow(PKEWindowProperties *wp) { } void DestroyWindow() { + if (vkInstance == nullptr) return; if (VULKAN_DEBUG_REPORT) { auto vkDestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(vkInstance, "vkDestroyDebugReportCallbackEXT"); vkDestroyDebugReportCallbackEXT(vkInstance, vkDebugReport, nullptr); } - if (vkInstance != nullptr) return; - PFN_vkDestroyInstance(vkInstance); + vkDestroySurfaceKHR(vkInstance, vkSurfaceKHR, vkAllocator); + vkDestroyDevice(vkDevice, vkAllocator); + vkDestroyInstance(vkInstance, vkAllocator); glfwDestroyWindow(window); glfwTerminate(); } |
