diff --git a/.ccls b/.ccls index 87cbe6e53..e9776a234 100644 --- a/.ccls +++ b/.ccls @@ -10,5 +10,7 @@ %h -x %h c++-header +-DDEBUG=1 + -I./external/ diff --git a/Makefile b/Makefile index 2e40b9329..c40038861 100644 --- a/Makefile +++ b/Makefile @@ -12,11 +12,12 @@ SCMP=/c/VulkanSDK/1.2.141.2/Bin32/glslangValidator.exe ####### Main Target Rules ####### -build: format build_shaders +build: build_shaders $(CC) \ -Wall \ src/* \ -std=c++11 \ + -DDEBUG=1 \ -I"./src/" \ -I"C:\\VulkanSDK\\1.2.141.2\\Include\\" \ -L"C:\\VulkanSDK\\1.2.141.2\\Lib\\" \ diff --git a/src/main.cpp b/src/main.cpp index d650ff4d9..5f4eff07d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #endif #include +#include #include #include #include @@ -12,8 +13,11 @@ #include "VulkanTools.h" #include +#include +#ifndef DEBUG #define DEBUG (!NDEBUG) +#endif #define BUFFER_ELEMENTS 32 @@ -53,6 +57,7 @@ class VulkanExample VkShaderModule shaderModule; VkDebugReportCallbackEXT debugReportCallback{}; + vk::DebugReportCallbackEXT computeDebugReportCallback{}; VkResult createBuffer(VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, @@ -111,82 +116,72 @@ class VulkanExample { LOG("Running headless compute example\n"); - VkApplicationInfo appInfo = {}; - appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; - appInfo.pApplicationName = "Vulkan headless example"; - appInfo.pEngineName = "VulkanExample"; - appInfo.apiVersion = VK_API_VERSION_1_0; + vk::ApplicationInfo applicationInfo; + applicationInfo.pApplicationName = "Vulkan compute"; + applicationInfo.pEngineName = "VulkanCompute"; + applicationInfo.apiVersion = VK_API_VERSION_1_0; - /* - Vulkan this->instance creation (without surface extensions) - */ - VkInstanceCreateInfo instanceCreateInfo = {}; - instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; - instanceCreateInfo.pApplicationInfo = &appInfo; + std::vector applicationExtensions; + applicationExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); - uint32_t layerCount = 0; - const char* validationLayers[] = { + 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" }; - layerCount = 1; -#if DEBUG - // Check if layers are available - uint32_t instanceLayerCount; - vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr); - std::vector instanceLayers(instanceLayerCount); - vkEnumerateInstanceLayerProperties(&instanceLayerCount, - instanceLayers.data()); - - bool layersAvailable = true; - for (auto layerName : validationLayers) { - bool layerAvailable = false; - for (auto instanceLayer : instanceLayers) { - if (strcmp(instanceLayer.layerName, layerName) == 0) { - layerAvailable = true; - break; + // 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 (!layerAvailable) { - layersAvailable = false; - break; - } } - if (layersAvailable) { - instanceCreateInfo.ppEnabledLayerNames = validationLayers; - const char* validationExt = VK_EXT_DEBUG_REPORT_EXTENSION_NAME; - instanceCreateInfo.enabledLayerCount = layerCount; - instanceCreateInfo.enabledExtensionCount = 1; - instanceCreateInfo.ppEnabledExtensionNames = &validationExt; + if (validLayerNames.size() > 0) { + computeInstanceCreateInfo.enabledLayerCount = (uint32_t)validLayerNames.size(); + computeInstanceCreateInfo.ppEnabledLayerNames = validLayerNames.data(); } #endif - VK_CHECK_RESULT( - vkCreateInstance(&instanceCreateInfo, nullptr, &this->instance)); + + vk::Instance computeInstance = vk::createInstance(computeInstanceCreateInfo); + this->instance = static_cast(computeInstance); #if DEBUG - if (layersAvailable) { - VkDebugReportCallbackCreateInfoEXT debugReportCreateInfo = {}; - debugReportCreateInfo.sType = - VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT; - debugReportCreateInfo.flags = - VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT; - debugReportCreateInfo.pfnCallback = - (PFN_vkDebugReportCallbackEXT)debugMessageCallback; + if (validLayerNames.size() > 0) { + vk::DebugReportFlagsEXT debugFlags = + vk::DebugReportFlagBitsEXT::eError | vk::DebugReportFlagBitsEXT::eWarning; + vk::DebugReportCallbackCreateInfoEXT debugCreateInfo = {}; + debugCreateInfo.pfnCallback = (PFN_vkDebugReportCallbackEXT)debugMessageCallback; + debugCreateInfo.flags = debugFlags; - // We have to explicitly load this function. - PFN_vkCreateDebugReportCallbackEXT vkCreateDebugReportCallbackEXT = - reinterpret_cast( - vkGetInstanceProcAddr(this->instance, - "vkCreateDebugReportCallbackEXT")); - assert(vkCreateDebugReportCallbackEXT); - VK_CHECK_RESULT( - vkCreateDebugReportCallbackEXT(this->instance, - &debugReportCreateInfo, - nullptr, - &debugReportCallback)); + vk::DispatchLoaderDynamic dispatcher; + dispatcher.init(computeInstance, &vkGetInstanceProcAddr); + this->computeDebugReportCallback = computeInstance.createDebugReportCallbackEXT(debugCreateInfo, nullptr, dispatcher); + this->debugReportCallback = this->computeDebugReportCallback; } #endif + +// /* +// C API Vulkan +// */ + /* Vulkan this->device creation */ @@ -593,14 +588,14 @@ class VulkanExample vkDestroyShaderModule(this->device, this->shaderModule, nullptr); vkDestroyDevice(this->device, nullptr); #if DEBUG - if (debugReportCallback) { + if (this->computeDebugReportCallback) { PFN_vkDestroyDebugReportCallbackEXT vkDestroyDebugReportCallback = reinterpret_cast( vkGetInstanceProcAddr(this->instance, "vkDestroyDebugReportCallbackEXT")); assert(vkDestroyDebugReportCallback); vkDestroyDebugReportCallback( - this->instance, debugReportCallback, nullptr); + this->instance, this->computeDebugReportCallback, nullptr); } #endif vkDestroyInstance(this->instance, nullptr);