diff --git a/src/Manager.cpp b/src/Manager.cpp new file mode 100644 index 000000000..522b0e7cf --- /dev/null +++ b/src/Manager.cpp @@ -0,0 +1,112 @@ +#if defined(_WIN32) +#pragma comment(linker, "/subsystem:console") +#endif + +// SPDLOG_ACTIVE_LEVEL must be defined before spdlog.h import +#if DEBUG +#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG +#endif + +#include +#include + +#include + +#include "Manager.hpp" + +namespace kp { + +#if DEBUG +static VKAPI_ATTR VkBool32 VKAPI_CALL +debugMessageCallback(VkDebugReportFlagsEXT flags, + VkDebugReportObjectTypeEXT objectType, + uint64_t object, + size_t location, + int32_t messageCode, + const char* pLayerPrefix, + const char* pMessage, + void* pUserData) +{ + SPDLOG_DEBUG("[VALIDATION]: {} - {}", pLayerPrefix, pMessage); + return VK_FALSE; +} +#endif + +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 +} + +Manager::~Manager() +{ +} + + +} diff --git a/src/Manager.hpp b/src/Manager.hpp index d34dd40b4..efadc67ef 100644 --- a/src/Manager.hpp +++ b/src/Manager.hpp @@ -1,15 +1,29 @@ #pragma once +#include +#include + namespace kp { class Manager { private: - public: Manager(); + virtual ~Manager(); + +private: + vk::Instance mInstance; + + // Create functions + vk::InstanceCreateInfo createInstanceInfo(); + +#if DEBUG + vk::DebugReportCallbackEXT mDebugReportCallback; + vk::DispatchLoaderDynamic mDebugDispatcher; +#endif }; } // End namespace kp diff --git a/src/main.cpp b/src/main.cpp index 0c3274269..41281046a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -115,7 +115,7 @@ class VulkanCompute vk::ApplicationInfo applicationInfo; applicationInfo.pApplicationName = "Vulkan compute"; applicationInfo.pEngineName = "VulkanCompute"; - applicationInfo.apiVersion = VK_API_VERSION_1_0; + applicationInfo.apiVersion = VK_API_VERSION_1_2; std::vector applicationExtensions; applicationExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);