From 45ba5a5b5e23f66783db3b4027f6f8429578b330 Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Sun, 16 Aug 2020 11:03:54 +0100 Subject: [PATCH] Initial creation classes in manager --- README.md | 1 + src/Manager.cpp | 200 ++++++++++++++++++++++++++++++++---------------- src/Manager.hpp | 18 ++++- 3 files changed, 147 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index 0a5dfb504..9da291d03 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ ## Principles * Non-vulkan naming convention to disambiguate Vulkan vs Kompute components +* BYOV: It would play nicely with existing applications with a bring-your-own-Vulkan design * TODO ## Getting Started diff --git a/src/Manager.cpp b/src/Manager.cpp index 522b0e7cf..f5e9fe0e5 100644 --- a/src/Manager.cpp +++ b/src/Manager.cpp @@ -34,79 +34,143 @@ debugMessageCallback(VkDebugReportFlagsEXT flags, Manager::Manager() { - vk::ApplicationInfo applicationInfo; - applicationInfo.pApplicationName = "Vulkan compute"; - applicationInfo.pEngineName = "VulkanCompute"; - applicationInfo.apiVersion = VK_API_VERSION_1_2; - - std::vector applicationExtensions; - applicationExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); - - vk::InstanceCreateInfo computeInstanceCreateInfo; - computeInstanceCreateInfo.pApplicationInfo = &applicationInfo; - if (!applicationExtensions.empty()) { - computeInstanceCreateInfo.enabledExtensionCount = - (uint32_t)applicationExtensions.size(); - computeInstanceCreateInfo.ppEnabledExtensionNames = - applicationExtensions.data(); - } - -#if DEBUG - // We'll identify the layers that are supported - std::vector validLayerNames; - std::vector desiredLayerNames = { - "VK_LAYER_LUNARG_assistant_layer", - "VK_LAYER_LUNARG_standard_validation" - }; - // Identify the valid layer names based on the desiredLayerNames - { - std::set uniqueLayerNames; - std::vector availableLayerProperties = - vk::enumerateInstanceLayerProperties(); - for (vk::LayerProperties layerProperties : - availableLayerProperties) { - std::string layerName(layerProperties.layerName); - uniqueLayerNames.insert(layerName); - } - for (const char* desiredLayerName : desiredLayerNames) { - if (uniqueLayerNames.count(desiredLayerName) != 0) { - validLayerNames.push_back(desiredLayerName); - } - } - } - - if (validLayerNames.size() > 0) { - computeInstanceCreateInfo.enabledLayerCount = - (uint32_t)validLayerNames.size(); - computeInstanceCreateInfo.ppEnabledLayerNames = - validLayerNames.data(); - } -#endif - - this->mInstance = vk::createInstance(computeInstanceCreateInfo); - -#if DEBUG - if (validLayerNames.size() > 0) { - vk::DebugReportFlagsEXT debugFlags = - vk::DebugReportFlagBitsEXT::eError | - vk::DebugReportFlagBitsEXT::eWarning; - vk::DebugReportCallbackCreateInfoEXT debugCreateInfo = {}; - debugCreateInfo.pfnCallback = - (PFN_vkDebugReportCallbackEXT)debugMessageCallback; - debugCreateInfo.flags = debugFlags; - - this->mDebugDispatcher.init(this->mInstance, - &vkGetInstanceProcAddr); - this->mDebugReportCallback = - this->mInstance.createDebugReportCallbackEXT( - debugCreateInfo, nullptr, this->mDebugDispatcher); - } -#endif + this->createInstance(); + this->createDevice(); } Manager::~Manager() { } +void Manager::createInstance() { + + this->mFreeInstance = true; + + vk::ApplicationInfo applicationInfo; + applicationInfo.pApplicationName = "Vulkan compute"; + applicationInfo.pEngineName = "VulkanCompute"; + applicationInfo.apiVersion = VK_API_VERSION_1_2; + + std::vector applicationExtensions; + applicationExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); + + vk::InstanceCreateInfo computeInstanceCreateInfo; + computeInstanceCreateInfo.pApplicationInfo = &applicationInfo; + if (!applicationExtensions.empty()) { + computeInstanceCreateInfo.enabledExtensionCount = + (uint32_t)applicationExtensions.size(); + computeInstanceCreateInfo.ppEnabledExtensionNames = + applicationExtensions.data(); + } + +#if DEBUG + // We'll identify the layers that are supported + std::vector validLayerNames; + std::vector desiredLayerNames = { + "VK_LAYER_LUNARG_assistant_layer", + "VK_LAYER_LUNARG_standard_validation" + }; + // Identify the valid layer names based on the desiredLayerNames + { + std::set uniqueLayerNames; + std::vector availableLayerProperties = + vk::enumerateInstanceLayerProperties(); + for (vk::LayerProperties layerProperties : + availableLayerProperties) { + std::string layerName(layerProperties.layerName); + uniqueLayerNames.insert(layerName); + } + for (const char* desiredLayerName : desiredLayerNames) { + if (uniqueLayerNames.count(desiredLayerName) != 0) { + validLayerNames.push_back(desiredLayerName); + } + } + } + + if (validLayerNames.size() > 0) { + computeInstanceCreateInfo.enabledLayerCount = + (uint32_t)validLayerNames.size(); + computeInstanceCreateInfo.ppEnabledLayerNames = + validLayerNames.data(); + } +#endif + + vk::Instance instance = vk::createInstance(computeInstanceCreateInfo, nullptr, this->mInstance); + +#if DEBUG + if (validLayerNames.size() > 0) { + vk::DebugReportFlagsEXT debugFlags = + vk::DebugReportFlagBitsEXT::eError | + vk::DebugReportFlagBitsEXT::eWarning; + vk::DebugReportCallbackCreateInfoEXT debugCreateInfo = {}; + debugCreateInfo.pfnCallback = + (PFN_vkDebugReportCallbackEXT)debugMessageCallback; + debugCreateInfo.flags = debugFlags; + + this->mDebugDispatcher.init(*this->mInstance, + &vkGetInstanceProcAddr); + this->mDebugReportCallback = + this->mInstance->createDebugReportCallbackEXT( + debugCreateInfo, nullptr, this->mDebugDispatcher); + } +#endif +} + +void Manager::createDevice() { + + if (this->mInstance == nullptr) { + throw std::runtime_error("Manager instance is null"); + } + + this->mFreeDevice = true; + this->mFreeComputeQueue = true; + + std::vector physicalDevices = + this->mInstance->enumeratePhysicalDevices(); + + vk::PhysicalDevice physicalDevice = physicalDevices[0]; + + vk::PhysicalDeviceProperties physicalDeviceProperties = + physicalDevice.getProperties(); + + spdlog::info("Using device {}", physicalDeviceProperties.deviceName); + + // Find compute queue + std::vector allQueueFamilyProperties = + physicalDevice.getQueueFamilyProperties(); + + this->mComputeQueueFamilyIndex = -1; + for (uint32_t i = 0; i < allQueueFamilyProperties.size(); i++) { + vk::QueueFamilyProperties queueFamilyProperties = + allQueueFamilyProperties[i]; + + if (queueFamilyProperties.queueFlags & + vk::QueueFlagBits::eCompute) { + this->mComputeQueueFamilyIndex = i; + break; + } + } + + if (this->mComputeQueueFamilyIndex < 0) { + spdlog::critical("Compute queue is not supported"); + } + + 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); + + physicalDevice.createDevice(deviceCreateInfo, nullptr, this->mDevice); + + this->mDevice->getQueue(this->mComputeQueueFamilyIndex, 0, this->mComputeQueue); +} } diff --git a/src/Manager.hpp b/src/Manager.hpp index efadc67ef..069575abc 100644 --- a/src/Manager.hpp +++ b/src/Manager.hpp @@ -14,16 +14,26 @@ public: virtual ~Manager(); -private: - vk::Instance mInstance; + // Templated Commands - // Create functions - vk::InstanceCreateInfo createInstanceInfo(); +private: + vk::Instance* mInstance = nullptr; + bool mFreeInstance = false; + vk::Device* mDevice = nullptr; + bool mFreeDevice = false; + uint32_t mComputeQueueFamilyIndex = -1; + vk::Queue* mComputeQueue = nullptr; + bool mFreeComputeQueue = false; #if DEBUG vk::DebugReportCallbackEXT mDebugReportCallback; vk::DispatchLoaderDynamic mDebugDispatcher; #endif + + // Create functions + void createInstance(); + void createDevice(); + }; } // End namespace kp