From 44eface5f33bbb6ed277080a1d48317125179037 Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Mon, 10 Aug 2020 09:11:30 +0100 Subject: [PATCH] migrated queue creation and physical device to header cpp --- Makefile | 6 +++-- src/main.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 921801fdf..60c5ba661 100644 --- a/Makefile +++ b/Makefile @@ -16,8 +16,9 @@ build: build_shaders $(CC) \ -Wall \ src/* \ - -std=c++11 \ + -std=c++17 \ -DDEBUG=1 \ + -I"./external/" \ -I"./src/" \ -I"C:\\VulkanSDK\\1.2.141.2\\Include\\" \ -L"C:\\VulkanSDK\\1.2.141.2\\Lib\\" \ @@ -28,7 +29,8 @@ build_linux: g++ \ -Wall \ src/* \ - -std=c++11 \ + -std=c++17 \ + -I"./external/" \ -I"./src" \ -lvulkan \ -o ./bin/main diff --git a/src/main.cpp b/src/main.cpp index 22c7b3695..73b307bc8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,9 +11,11 @@ #include #include -#include "VulkanTools.h" #include #include +#include + +#include "VulkanTools.h" #ifndef DEBUG #define DEBUG (!NDEBUG) @@ -41,6 +43,12 @@ class VulkanCompute { public: vk::Instance mInstance; + vk::PhysicalDevice mPhysicalDevice; + vk::Device mDevice; + vk::Queue mComputeQueue; + + uint32_t mComputeQueueFamilyIndex; + vk::DebugReportCallbackEXT mDebugReportCallback{}; /* @@ -49,7 +57,7 @@ class VulkanCompute VkInstance instance; VkPhysicalDevice physicalDevice; VkDevice device; - uint32_t queueFamilyIndex; + //uint32_t queueFamilyIndex; VkPipelineCache pipelineCache; VkQueue queue; VkCommandPool commandPool; @@ -181,6 +189,55 @@ class VulkanCompute } #endif + // Find device (currently only pick first device) + { + std::vector physicalDevices = + this->mInstance.enumeratePhysicalDevices(); + + this->mPhysicalDevice = physicalDevices[0]; + + vk::PhysicalDeviceProperties physicalDeviceProperties = + this->mPhysicalDevice.getProperties(); + + spdlog::info("Device {}", physicalDeviceProperties.deviceName); + } + + { + // Find compute queue + std::vector allQueueFamilyProperties = + this->mPhysicalDevice.getQueueFamilyProperties(); + + this->mComputeQueueFamilyIndex = 0; + for (; this->mComputeQueueFamilyIndex < allQueueFamilyProperties.size(); this->mComputeQueueFamilyIndex++) { + vk::QueueFamilyProperties queueFamilyProperties = + allQueueFamilyProperties[this->mComputeQueueFamilyIndex]; + if (queueFamilyProperties.queueFlags & vk::QueueFlagBits::eCompute) { + break; + } + } + + spdlog::info("ComputeQueueFamilyIndex: {}, Size: {}", this->mComputeQueueFamilyIndex, allQueueFamilyProperties.size()); + if (this->mComputeQueueFamilyIndex == allQueueFamilyProperties.size() - 1) { + spdlog::critical("Compute queue is not supported"); + } + spdlog::info("Running after critical"); + + const float defaultQueuePriority(0.0f); + const uint32_t defaultQueueCount(1); + vk::DeviceQueueCreateInfo deviceQueueCreateInfo( + vk::DeviceQueueCreateFlags(), + this->mComputeQueueFamilyIndex, + defaultQueueCount, + &defaultQueuePriority); + + vk::DeviceCreateInfo deviceCreateInfo( + vk::DeviceCreateFlags(), + 1, // Number of deviceQueueCreateInfo + &deviceQueueCreateInfo); + + this->mDevice = this->mPhysicalDevice.createDevice(deviceCreateInfo); + this->mComputeQueue = this->mDevice.getQueue(this->mComputeQueueFamilyIndex, 0); + } // /* // C API Vulkan @@ -190,6 +247,8 @@ class VulkanCompute Vulkan this->device creation */ // Physical this->device (always use first) + // + /******** Vulkan Device Creation C API ********************* uint32_t deviceCount = 0; VK_CHECK_RESULT( vkEnumeratePhysicalDevices(this->instance, &deviceCount, nullptr)); @@ -205,7 +264,7 @@ class VulkanCompute LOG("GPU: %s\n", deviceProperties.deviceName); // Request a single compute this->queue - const float defaultQueuePriority(0.0f); + //const float defaultQueuePriority(0.0f); VkDeviceQueueCreateInfo queueCreateInfo = {}; uint32_t queueFamilyCount; vkGetPhysicalDeviceQueueFamilyProperties( @@ -217,11 +276,12 @@ class VulkanCompute &queueFamilyCount, queueFamilyProperties.data()); + const float defaultQueuePriority(0.0f); for (uint32_t i = 0; i < static_cast(queueFamilyProperties.size()); i++) { if (queueFamilyProperties[i].queueFlags & VK_QUEUE_COMPUTE_BIT) { - queueFamilyIndex = i; + this->queueFamilyIndex = i; queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; queueCreateInfo.queueFamilyIndex = i; @@ -240,6 +300,11 @@ class VulkanCompute // Get a compute this->queue vkGetDeviceQueue(this->device, this->queueFamilyIndex, 0, &this->queue); + **********************************************************/ + + this->physicalDevice = this->mPhysicalDevice; + this->device = this->mDevice; + this->queue = this->mComputeQueue; /* Prepare storage buffers @@ -397,7 +462,7 @@ class VulkanCompute // Compute command pool VkCommandPoolCreateInfo cmdPoolInfo = {}; cmdPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; - cmdPoolInfo.queueFamilyIndex = this->queueFamilyIndex; + cmdPoolInfo.queueFamilyIndex = this->mComputeQueueFamilyIndex; cmdPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; VK_CHECK_RESULT(vkCreateCommandPool( this->device, &cmdPoolInfo, nullptr, &this->commandPool));