#include "VkBootstrap.h" #include #include namespace vkb { namespace detail { template void get_inst_proc_addr ( T& out_ptr, const char* func_name, VkInstance instance, PFN_vkGetInstanceProcAddr ptr_vkGetInstanceProcAddr) { out_ptr = reinterpret_cast (ptr_vkGetInstanceProcAddr (instance, func_name)); } // Helper for robustly executing the two-call pattern template auto get_vector (F&& f, Ts&&... ts) -> Expected, VkResult> { uint32_t count = 0; std::vector results; VkResult err; do { err = f (ts..., &count, nullptr); if (err) { return err; }; results.resize (count); err = f (ts..., &count, results.data ()); } while (err == VK_INCOMPLETE); if (err != VK_SUCCESS) { return err; }; return results; } template auto get_vector_noerror (F&& f, Ts&&... ts) -> std::vector { uint32_t count = 0; std::vector results; f (ts..., &count, nullptr); results.resize (count); f (ts..., &count, results.data ()); return results; } } // namespace detail const char* to_string_message_severity (VkDebugUtilsMessageSeverityFlagBitsEXT s) { switch (s) { case VkDebugUtilsMessageSeverityFlagBitsEXT::VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT: return "VERBOSE"; case VkDebugUtilsMessageSeverityFlagBitsEXT::VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT: return "ERROR"; case VkDebugUtilsMessageSeverityFlagBitsEXT::VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT: return "WARNING"; case VkDebugUtilsMessageSeverityFlagBitsEXT::VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT: return "INFO"; default: return "UNKNOWN"; } } const char* to_string_message_type (VkDebugUtilsMessageTypeFlagsEXT s) { if (s == 7) return "General | Validation | Performance"; if (s == 6) return "Validation | Performance"; if (s == 5) return "General | Performance"; if (s == 4 /*VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT*/) return "Performance"; if (s == 3) return "General | Validation"; if (s == 2 /*VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT*/) return "Validation"; if (s == 1 /*VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT*/) return "General"; return "Unknown"; } VkResult create_debug_utils_messenger (VkInstance instance, PFN_vkDebugUtilsMessengerCallbackEXT debug_callback, VkDebugUtilsMessageSeverityFlagsEXT severity, VkDebugUtilsMessageTypeFlagsEXT type, VkDebugUtilsMessengerEXT* pDebugMessenger, VkAllocationCallbacks* allocation_callbacks) { if (debug_callback == nullptr) debug_callback = default_debug_callback; VkDebugUtilsMessengerCreateInfoEXT messengerCreateInfo = {}; messengerCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; messengerCreateInfo.pNext = nullptr; messengerCreateInfo.messageSeverity = severity; messengerCreateInfo.messageType = type; messengerCreateInfo.pfnUserCallback = debug_callback; PFN_vkCreateDebugUtilsMessengerEXT createMessengerFunc; detail::get_inst_proc_addr ( createMessengerFunc, "vkCreateDebugUtilsMessengerEXT", instance, vkGetInstanceProcAddr); if (createMessengerFunc != nullptr) { return createMessengerFunc (instance, &messengerCreateInfo, allocation_callbacks, pDebugMessenger); } else { return VK_ERROR_EXTENSION_NOT_PRESENT; } } void destroy_debug_utils_messenger ( VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger, VkAllocationCallbacks* allocation_callbacks) { PFN_vkDestroyDebugUtilsMessengerEXT deleteMessengerFunc; detail::get_inst_proc_addr ( deleteMessengerFunc, "vkDestroyDebugUtilsMessengerEXT", instance, vkGetInstanceProcAddr); if (deleteMessengerFunc != nullptr) { deleteMessengerFunc (instance, debugMessenger, allocation_callbacks); } } VkBool32 default_debug_callback (VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageType, const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, void*) { auto ms = to_string_message_severity (messageSeverity); auto mt = to_string_message_type (messageType); printf ("[%s: %s]\n%s\n", ms, mt, pCallbackData->pMessage); return VK_FALSE; } namespace detail { bool check_layer_supported (std::vector available_layers, const char* layer_name) { if (!layer_name) return false; for (const auto& layer_properties : available_layers) { if (strcmp (layer_name, layer_properties.layerName) == 0) { return true; } } return false; } bool check_layers_supported (std::vector available_layers, std::vector layer_names) { bool all_found = true; for (const auto& layer_name : layer_names) { bool found = check_layer_supported (available_layers, layer_name); if (!found) all_found = false; } return all_found; } bool check_extension_supported (std::vector available_extensions, const char* extension_name) { if (!extension_name) return false; for (const auto& layer_properties : available_extensions) { if (strcmp (extension_name, layer_properties.extensionName) == 0) { return true; } } return false; } bool check_extensions_supported (std::vector available_extensions, std::vector extension_names) { bool all_found = true; for (const auto& extension_name : extension_names) { bool found = check_extension_supported (available_extensions, extension_name); if (!found) all_found = false; } return all_found; } template void setup_pNext_chain (T& structure, std::vector const& structs) { structure.pNext = nullptr; if (structs.size () <= 0) return; for (size_t i = 0; i < structs.size () - 1; i++) { structs.at (i)->pNext = structs.at (i + 1); } structure.pNext = structs.at (0); } const char* validation_layer_name = "VK_LAYER_KHRONOS_validation"; } // namespace detail const char* to_string (InstanceError err) { switch (err) { case InstanceError::vulkan_unavailable: return "vulkan_unavailable"; case InstanceError::vulkan_version_unavailable: return "vulkan_version_unavailable"; case InstanceError::vulkan_version_1_1_unavailable: return "vulkan_version_1_1_unavailable"; case InstanceError::vulkan_version_1_2_unavailable: return "vulkan_version_1_2_unavailable"; case InstanceError::failed_create_debug_messenger: return "failed_create_debug_messenger"; case InstanceError::failed_create_instance: return "failed_create_instance"; case InstanceError::requested_layers_not_present: return "requested_layers_not_present"; case InstanceError::requested_extensions_not_present: return "requested_extensions_not_present"; default: return ""; } } const char* to_string (PhysicalDeviceError err) { switch (err) { case PhysicalDeviceError::no_surface_provided: return "no_surface_provided"; case PhysicalDeviceError::failed_enumerate_physical_devices: return "failed_enumerate_physical_devices"; case PhysicalDeviceError::no_physical_devices_found: return "no_physical_devices_found"; case PhysicalDeviceError::no_suitable_device: return "no_suitable_device"; default: return ""; } } const char* to_string (QueueError err) { switch (err) { case QueueError::present_unavailable: return "present_unavailable"; case QueueError::graphics_unavailable: return "graphics_unavailable"; case QueueError::compute_unavailable: return "compute_unavailable"; case QueueError::transfer_unavailable: return "transfer_unavailable"; case QueueError::queue_index_out_of_range: return "queue_index_out_of_range"; case QueueError::invalid_queue_family_index: return "invalid_queue_family_index"; default: return ""; } } const char* to_string (DeviceError err) { switch (err) { case DeviceError::failed_create_device: return "failed_create_device"; default: return ""; } } const char* to_string (SwapchainError err) { switch (err) { case SwapchainError::failed_query_surface_support_details: return "failed_query_surface_support_details"; case SwapchainError::failed_create_swapchain: return "failed_create_swapchain"; case SwapchainError::failed_get_swapchain_images: return "failed_get_swapchain_images"; case SwapchainError::failed_create_swapchain_image_views: return "failed_create_swapchain_image_views"; default: return ""; } } SystemInfo::SystemInfo () { auto available_extensions_ret = detail::get_vector (vkEnumerateInstanceExtensionProperties, nullptr); if (available_extensions_ret.has_value ()) { this->available_extensions = available_extensions_ret.value (); } for (auto& ext : this->available_extensions) if (strcmp (ext.extensionName, VK_EXT_DEBUG_UTILS_EXTENSION_NAME) == 0) debug_messenger_available = true; auto available_layers_ret = detail::get_vector (vkEnumerateInstanceLayerProperties); if (available_layers_ret.has_value ()) { this->available_layers = available_layers_ret.value (); } for (auto& layer : this->available_layers) if (strcmp (layer.layerName, detail::validation_layer_name) == 0) validation_layers_available = true; } bool SystemInfo::is_extension_available (const char* extension_name) const { if (!extension_name) return false; return detail::check_extension_supported (available_extensions, extension_name); } bool SystemInfo::is_layer_available (const char* layer_name) const { if (!layer_name) return false; return detail::check_layer_supported (available_layers, layer_name); } void destroy_instance (Instance instance) { if (instance.instance != VK_NULL_HANDLE) { if (instance.debug_messenger != nullptr) destroy_debug_utils_messenger (instance.instance, instance.debug_messenger, instance.allocation_callbacks); vkDestroyInstance (instance.instance, instance.allocation_callbacks); } } SystemInfo InstanceBuilder::get_system_info () const { return system; } detail::Expected> InstanceBuilder::build () const { uint32_t api_version = VK_MAKE_VERSION (1, 0, 0); if (info.required_api_version > VK_MAKE_VERSION (1, 0, 0) || info.desired_api_version > VK_MAKE_VERSION (1, 0, 0)) { PFN_vkEnumerateInstanceVersion pfn_vkEnumerateInstanceVersion; detail::get_inst_proc_addr ( pfn_vkEnumerateInstanceVersion, "vkEnumerateInstanceVersion", nullptr, vkGetInstanceProcAddr); uint32_t queried_api_version = VK_MAKE_VERSION (1, 0, 0); if (pfn_vkEnumerateInstanceVersion != nullptr) { VkResult res = pfn_vkEnumerateInstanceVersion (&queried_api_version); // Should always return VK_SUCCESS if (res != VK_SUCCESS && info.required_api_version > 0) return detail::Error{ InstanceError::vulkan_version_unavailable }; } if (pfn_vkEnumerateInstanceVersion == nullptr || queried_api_version < info.required_api_version) { if (VK_VERSION_MINOR (info.required_api_version) == 2) return detail::Error{ InstanceError::vulkan_version_1_2_unavailable }; else if (VK_VERSION_MINOR (info.required_api_version)) return detail::Error{ InstanceError::vulkan_version_1_1_unavailable }; else return detail::Error{ InstanceError::vulkan_version_unavailable }; } if (info.required_api_version > VK_MAKE_VERSION (1, 0, 0)) { api_version = info.required_api_version; } else if (info.desired_api_version > VK_MAKE_VERSION (1, 0, 0)) { if (queried_api_version >= info.desired_api_version) api_version = info.desired_api_version; else api_version = queried_api_version; } } VkApplicationInfo app_info = {}; app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; app_info.pNext = nullptr; app_info.pApplicationName = info.app_name != nullptr ? info.app_name : ""; app_info.applicationVersion = info.application_version; app_info.pEngineName = info.engine_name != nullptr ? info.engine_name : ""; app_info.engineVersion = info.engine_version; app_info.apiVersion = api_version; std::vector extensions; for (auto& ext : info.extensions) extensions.push_back (ext); if (info.debug_callback != nullptr) { extensions.push_back (VK_EXT_DEBUG_UTILS_EXTENSION_NAME); } if (!info.headless_context) { extensions.push_back ("VK_KHR_surface"); #if defined(_WIN32) extensions.push_back ("VK_KHR_win32_surface"); #elif defined(__ANDROID__) extensions.push_back ("VK_KHR_android_surface"); #elif defined(_DIRECT2DISPLAY) extensions.push_back ("VK_KHR_display"); #elif defined(__linux__) extensions.push_back ("VK_KHR_xcb_surface"); extensions.push_back ("VK_KHR_xlib_surface"); extensions.push_back ("VK_KHR_wayland_surface"); #elif defined(__APPLE__) extensions.push_back ("VK_KHR_metal_surface"); #endif } bool all_extensions_supported = detail::check_extensions_supported (system.available_extensions, extensions); if (!all_extensions_supported) { return detail::Error{ InstanceError::requested_extensions_not_present }; } std::vector layers; for (auto& layer : info.layers) layers.push_back (layer); if (info.enable_validation_layers) { layers.push_back (detail::validation_layer_name); } bool all_layers_supported = detail::check_layers_supported (system.available_layers, layers); if (!all_layers_supported) { return detail::Error{ InstanceError::requested_layers_not_present }; } std::vector pNext_chain; VkDebugUtilsMessengerCreateInfoEXT messengerCreateInfo = {}; if (info.use_debug_messenger) { messengerCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; messengerCreateInfo.pNext = nullptr; messengerCreateInfo.messageSeverity = info.debug_message_severity; messengerCreateInfo.messageType = info.debug_message_type; messengerCreateInfo.pfnUserCallback = info.debug_callback; pNext_chain.push_back (reinterpret_cast (&messengerCreateInfo)); } VkValidationFeaturesEXT features{}; if (info.enabled_validation_features.size () != 0 || info.disabled_validation_features.size ()) { features.sType = VK_STRUCTURE_TYPE_VALIDATION_FEATURES_EXT; features.pNext = nullptr; features.enabledValidationFeatureCount = static_cast (info.enabled_validation_features.size ()); features.pEnabledValidationFeatures = info.enabled_validation_features.data (); features.disabledValidationFeatureCount = static_cast (info.disabled_validation_features.size ()); features.pDisabledValidationFeatures = info.disabled_validation_features.data (); pNext_chain.push_back (reinterpret_cast (&features)); } VkValidationFlagsEXT checks{}; if (info.disabled_validation_checks.size () != 0) { checks.sType = VK_STRUCTURE_TYPE_VALIDATION_FLAGS_EXT; checks.pNext = nullptr; checks.disabledValidationCheckCount = static_cast (info.disabled_validation_checks.size ()); checks.pDisabledValidationChecks = info.disabled_validation_checks.data (); pNext_chain.push_back (reinterpret_cast (&checks)); } VkInstanceCreateInfo instance_create_info = {}; instance_create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; detail::setup_pNext_chain (instance_create_info, pNext_chain); instance_create_info.flags = info.flags; instance_create_info.pApplicationInfo = &app_info; instance_create_info.enabledExtensionCount = static_cast (extensions.size ()); instance_create_info.ppEnabledExtensionNames = extensions.data (); instance_create_info.enabledLayerCount = static_cast (layers.size ()); instance_create_info.ppEnabledLayerNames = layers.data (); Instance instance; VkResult res = vkCreateInstance (&instance_create_info, info.allocation_callbacks, &instance.instance); if (res != VK_SUCCESS) return detail::Error{ InstanceError::failed_create_instance, res }; if (info.use_debug_messenger) { res = create_debug_utils_messenger (instance.instance, info.debug_callback, info.debug_message_severity, info.debug_message_type, &instance.debug_messenger, info.allocation_callbacks); if (res != VK_SUCCESS) { return detail::Error{ InstanceError::failed_create_debug_messenger, res }; } } if (info.headless_context) { instance.headless = true; } instance.allocation_callbacks = info.allocation_callbacks; instance.instance_version = api_version; return instance; } InstanceBuilder& InstanceBuilder::set_app_name (const char* app_name) { if (!app_name) return *this; info.app_name = app_name; return *this; } InstanceBuilder& InstanceBuilder::set_engine_name (const char* engine_name) { if (!engine_name) return *this; info.engine_name = engine_name; return *this; } InstanceBuilder& InstanceBuilder::set_app_version (uint32_t major, uint32_t minor, uint32_t patch) { info.application_version = VK_MAKE_VERSION (major, minor, patch); return *this; } InstanceBuilder& InstanceBuilder::set_engine_version (uint32_t major, uint32_t minor, uint32_t patch) { info.engine_version = VK_MAKE_VERSION (major, minor, patch); return *this; } InstanceBuilder& InstanceBuilder::require_api_version (uint32_t major, uint32_t minor, uint32_t patch) { info.required_api_version = VK_MAKE_VERSION (major, minor, patch); return *this; } InstanceBuilder& InstanceBuilder::desire_api_version (uint32_t major, uint32_t minor, uint32_t patch) { info.desired_api_version = VK_MAKE_VERSION (major, minor, patch); return *this; } InstanceBuilder& InstanceBuilder::enable_layer (const char* layer_name) { if (!layer_name) return *this; info.layers.push_back (layer_name); return *this; } InstanceBuilder& InstanceBuilder::enable_extension (const char* extension_name) { if (!extension_name) return *this; info.extensions.push_back (extension_name); return *this; } InstanceBuilder& InstanceBuilder::enable_validation_layers (bool enable_validation) { info.enable_validation_layers = enable_validation; return *this; } InstanceBuilder& InstanceBuilder::request_validation_layers (bool enable_validation) { info.enable_validation_layers = enable_validation && detail::check_layer_supported (system.available_layers, detail::validation_layer_name); return *this; } InstanceBuilder& InstanceBuilder::use_default_debug_messenger () { info.use_debug_messenger = true; info.debug_callback = default_debug_callback; return *this; } InstanceBuilder& InstanceBuilder::set_debug_callback (PFN_vkDebugUtilsMessengerCallbackEXT callback) { info.use_debug_messenger = true; info.debug_callback = callback; return *this; } InstanceBuilder& InstanceBuilder::set_headless (bool headless) { info.headless_context = headless; return *this; } InstanceBuilder& InstanceBuilder::set_debug_messenger_severity (VkDebugUtilsMessageSeverityFlagsEXT severity) { info.debug_message_severity = severity; return *this; } InstanceBuilder& InstanceBuilder::add_debug_messenger_severity (VkDebugUtilsMessageSeverityFlagsEXT severity) { info.debug_message_severity = info.debug_message_severity | severity; return *this; } InstanceBuilder& InstanceBuilder::set_debug_messenger_type (VkDebugUtilsMessageTypeFlagsEXT type) { info.debug_message_type = type; return *this; } InstanceBuilder& InstanceBuilder::add_debug_messenger_type (VkDebugUtilsMessageTypeFlagsEXT type) { info.debug_message_type = info.debug_message_type | type; return *this; } InstanceBuilder& InstanceBuilder::add_validation_disable (VkValidationCheckEXT check) { info.disabled_validation_checks.push_back (check); return *this; } InstanceBuilder& InstanceBuilder::add_validation_feature_enable (VkValidationFeatureEnableEXT enable) { info.enabled_validation_features.push_back (enable); return *this; } InstanceBuilder& InstanceBuilder::add_validation_feature_disable (VkValidationFeatureDisableEXT disable) { info.disabled_validation_features.push_back (disable); return *this; } InstanceBuilder& InstanceBuilder::set_allocation_callbacks (VkAllocationCallbacks* callbacks) { info.allocation_callbacks = callbacks; return *this; } // ---- Physical Device ---- // namespace detail { std::vector check_device_extension_support ( VkPhysicalDevice device, std::vector desired_extensions) { auto available_extensions = detail::get_vector (vkEnumerateDeviceExtensionProperties, device, nullptr); if (!available_extensions.has_value ()) return {}; std::vector extensions_to_enable; for (const auto& extension : available_extensions.value ()) { for (auto& req_ext : desired_extensions) { if (strcmp (req_ext, extension.extensionName) == 0) { extensions_to_enable.push_back (req_ext); break; } } } return extensions_to_enable; } bool supports_features (VkPhysicalDeviceFeatures supported, VkPhysicalDeviceFeatures requested) { // clang-format off if (requested.robustBufferAccess && !supported.robustBufferAccess) return false; if (requested.fullDrawIndexUint32 && !supported.fullDrawIndexUint32) return false; if (requested.imageCubeArray && !supported.imageCubeArray) return false; if (requested.independentBlend && !supported.independentBlend) return false; if (requested.geometryShader && !supported.geometryShader) return false; if (requested.tessellationShader && !supported.tessellationShader) return false; if (requested.sampleRateShading && !supported.sampleRateShading) return false; if (requested.dualSrcBlend && !supported.dualSrcBlend) return false; if (requested.logicOp && !supported.logicOp) return false; if (requested.multiDrawIndirect && !supported.multiDrawIndirect) return false; if (requested.drawIndirectFirstInstance && !supported.drawIndirectFirstInstance) return false; if (requested.depthClamp && !supported.depthClamp) return false; if (requested.depthBiasClamp && !supported.depthBiasClamp) return false; if (requested.fillModeNonSolid && !supported.fillModeNonSolid) return false; if (requested.depthBounds && !supported.depthBounds) return false; if (requested.wideLines && !supported.wideLines) return false; if (requested.largePoints && !supported.largePoints) return false; if (requested.alphaToOne && !supported.alphaToOne) return false; if (requested.multiViewport && !supported.multiViewport) return false; if (requested.samplerAnisotropy && !supported.samplerAnisotropy) return false; if (requested.textureCompressionETC2 && !supported.textureCompressionETC2) return false; if (requested.textureCompressionASTC_LDR && !supported.textureCompressionASTC_LDR) return false; if (requested.textureCompressionBC && !supported.textureCompressionBC) return false; if (requested.occlusionQueryPrecise && !supported.occlusionQueryPrecise) return false; if (requested.pipelineStatisticsQuery && !supported.pipelineStatisticsQuery) return false; if (requested.vertexPipelineStoresAndAtomics && !supported.vertexPipelineStoresAndAtomics) return false; if (requested.fragmentStoresAndAtomics && !supported.fragmentStoresAndAtomics) return false; if (requested.shaderTessellationAndGeometryPointSize && !supported.shaderTessellationAndGeometryPointSize) return false; if (requested.shaderImageGatherExtended && !supported.shaderImageGatherExtended) return false; if (requested.shaderStorageImageExtendedFormats && !supported.shaderStorageImageExtendedFormats) return false; if (requested.shaderStorageImageMultisample && !supported.shaderStorageImageMultisample) return false; if (requested.shaderStorageImageReadWithoutFormat && !supported.shaderStorageImageReadWithoutFormat) return false; if (requested.shaderStorageImageWriteWithoutFormat && !supported.shaderStorageImageWriteWithoutFormat) return false; if (requested.shaderUniformBufferArrayDynamicIndexing && !supported.shaderUniformBufferArrayDynamicIndexing) return false; if (requested.shaderSampledImageArrayDynamicIndexing && !supported.shaderSampledImageArrayDynamicIndexing) return false; if (requested.shaderStorageBufferArrayDynamicIndexing && !supported.shaderStorageBufferArrayDynamicIndexing) return false; if (requested.shaderStorageImageArrayDynamicIndexing && !supported.shaderStorageImageArrayDynamicIndexing) return false; if (requested.shaderClipDistance && !supported.shaderClipDistance) return false; if (requested.shaderCullDistance && !supported.shaderCullDistance) return false; if (requested.shaderFloat64 && !supported.shaderFloat64) return false; if (requested.shaderInt64 && !supported.shaderInt64) return false; if (requested.shaderInt16 && !supported.shaderInt16) return false; if (requested.shaderResourceResidency && !supported.shaderResourceResidency) return false; if (requested.shaderResourceMinLod && !supported.shaderResourceMinLod) return false; if (requested.sparseBinding && !supported.sparseBinding) return false; if (requested.sparseResidencyBuffer && !supported.sparseResidencyBuffer) return false; if (requested.sparseResidencyImage2D && !supported.sparseResidencyImage2D) return false; if (requested.sparseResidencyImage3D && !supported.sparseResidencyImage3D) return false; if (requested.sparseResidency2Samples && !supported.sparseResidency2Samples) return false; if (requested.sparseResidency4Samples && !supported.sparseResidency4Samples) return false; if (requested.sparseResidency8Samples && !supported.sparseResidency8Samples) return false; if (requested.sparseResidency16Samples && !supported.sparseResidency16Samples) return false; if (requested.sparseResidencyAliased && !supported.sparseResidencyAliased) return false; if (requested.variableMultisampleRate && !supported.variableMultisampleRate) return false; if (requested.inheritedQueries && !supported.inheritedQueries) return false; // clang-format on return true; } // finds the first queue which supports graphics operations. returns -1 if none is found int get_graphics_queue_index (std::vector const& families) { for (size_t i = 0; i < families.size (); i++) { if (families[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) return static_cast (i); } return -1; } // finds a compute queue which is separate from the graphics queue and tries to find one without // transfer support returns -1 if none is found int get_separate_compute_queue_index (std::vector const& families) { int compute = -1; for (size_t i = 0; i < families.size (); i++) { if ((families[i].queueFlags & VK_QUEUE_COMPUTE_BIT) && ((families[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) == 0)) { if ((families[i].queueFlags & VK_QUEUE_TRANSFER_BIT) == 0) { return static_cast (i); } else { compute = static_cast (i); } } } return compute; } // finds a transfer queue which is separate from the graphics queue and tries to find one without // compute support returns -1 if none is found int get_separate_transfer_queue_index (std::vector const& families) { int transfer = -1; for (size_t i = 0; i < families.size (); i++) { if ((families[i].queueFlags & VK_QUEUE_TRANSFER_BIT) && ((families[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) == 0)) { if ((families[i].queueFlags & VK_QUEUE_COMPUTE_BIT) == 0) { return static_cast (i); } else { transfer = static_cast (i); } } } return transfer; } // finds the first queue which supports only compute (not graphics or transfer). returns -1 if none is found int get_dedicated_compute_queue_index (std::vector const& families) { for (size_t i = 0; i < families.size (); i++) { if ((families[i].queueFlags & VK_QUEUE_COMPUTE_BIT) && (families[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) == 0 && (families[i].queueFlags & VK_QUEUE_TRANSFER_BIT) == 0) return static_cast (i); } return -1; } // finds the first queue which supports only transfer (not graphics or compute). returns -1 if none is found int get_dedicated_transfer_queue_index (std::vector const& families) { for (size_t i = 0; i < families.size (); i++) { if ((families[i].queueFlags & VK_QUEUE_TRANSFER_BIT) && (families[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) == 0 && (families[i].queueFlags & VK_QUEUE_COMPUTE_BIT) == 0) return static_cast (i); } return -1; } // finds the first queue which supports presenting. returns -1 if none is found int get_present_queue_index (VkPhysicalDevice const phys_device, VkSurfaceKHR const surface, std::vector const& families) { for (size_t i = 0; i < families.size (); i++) { VkBool32 presentSupport = false; if (surface != VK_NULL_HANDLE) { VkResult res = vkGetPhysicalDeviceSurfaceSupportKHR ( phys_device, static_cast (i), surface, &presentSupport); if (res != VK_SUCCESS) return -1; // TODO: determine if this should fail another way } if (presentSupport == VK_TRUE) return static_cast (i); } return -1; } } // namespace detail PhysicalDeviceSelector::PhysicalDeviceDesc PhysicalDeviceSelector::populate_device_details ( VkPhysicalDevice phys_device) const { PhysicalDeviceSelector::PhysicalDeviceDesc desc{}; desc.phys_device = phys_device; auto queue_families = detail::get_vector_noerror ( vkGetPhysicalDeviceQueueFamilyProperties, phys_device); desc.queue_families = queue_families; vkGetPhysicalDeviceProperties (phys_device, &desc.device_properties); vkGetPhysicalDeviceFeatures (phys_device, &desc.device_features); vkGetPhysicalDeviceMemoryProperties (phys_device, &desc.mem_properties); return desc; } PhysicalDeviceSelector::Suitable PhysicalDeviceSelector::is_device_suitable (PhysicalDeviceDesc pd) const { Suitable suitable = Suitable::yes; if (criteria.required_version > pd.device_properties.apiVersion) return Suitable::no; if (criteria.desired_version > pd.device_properties.apiVersion) suitable = Suitable::partial; bool dedicated_compute = detail::get_dedicated_compute_queue_index (pd.queue_families) >= 0; bool dedicated_transfer = detail::get_dedicated_transfer_queue_index (pd.queue_families) >= 0; bool separate_compute = detail::get_separate_compute_queue_index (pd.queue_families) >= 0; bool separate_transfer = detail::get_separate_transfer_queue_index (pd.queue_families) >= 0; bool present_queue = detail::get_present_queue_index (pd.phys_device, system_info.surface, pd.queue_families) >= 0; if (criteria.require_dedicated_compute_queue && !dedicated_compute) return Suitable::no; if (criteria.require_dedicated_transfer_queue && !dedicated_transfer) return Suitable::no; if (criteria.require_separate_compute_queue && !separate_compute) return Suitable::no; if (criteria.require_separate_transfer_queue && !separate_transfer) return Suitable::no; if (criteria.require_present && !present_queue && !criteria.defer_surface_initialization) return Suitable::no; auto required_extensions_supported = detail::check_device_extension_support (pd.phys_device, criteria.required_extensions); if (required_extensions_supported.size () != criteria.required_extensions.size ()) return Suitable::no; auto desired_extensions_supported = detail::check_device_extension_support (pd.phys_device, criteria.desired_extensions); if (desired_extensions_supported.size () != criteria.desired_extensions.size ()) suitable = Suitable::partial; bool swapChainAdequate = false; if (criteria.defer_surface_initialization) { swapChainAdequate = true; } else if (!system_info.headless) { auto formats = detail::get_vector ( vkGetPhysicalDeviceSurfaceFormatsKHR, pd.phys_device, system_info.surface); auto present_modes = detail::get_vector ( vkGetPhysicalDeviceSurfacePresentModesKHR, pd.phys_device, system_info.surface); if (formats.has_value () && present_modes.has_value ()) { swapChainAdequate = !formats.value ().empty () && !present_modes.value ().empty (); } } if (criteria.require_present && !swapChainAdequate) return Suitable::no; if (pd.device_properties.deviceType != static_cast (criteria.preferred_type)) { if (criteria.allow_any_type) suitable = Suitable::partial; else return Suitable::no; } bool required_features_supported = detail::supports_features (pd.device_features, criteria.required_features); if (!required_features_supported) return Suitable::no; bool has_required_memory = false; bool has_preferred_memory = false; for (uint32_t i = 0; i < pd.mem_properties.memoryHeapCount; i++) { if (pd.mem_properties.memoryHeaps[i].flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) { if (pd.mem_properties.memoryHeaps[i].size > criteria.required_mem_size) { has_required_memory = true; } if (pd.mem_properties.memoryHeaps[i].size > criteria.desired_mem_size) { has_preferred_memory = true; } } } if (!has_required_memory) return Suitable::no; if (!has_preferred_memory) suitable = Suitable::partial; return suitable; } PhysicalDeviceSelector::PhysicalDeviceSelector (Instance const& instance) { system_info.instance = instance.instance; system_info.headless = instance.headless; criteria.require_present = !instance.headless; criteria.required_version = instance.instance_version; criteria.desired_version = instance.instance_version; } detail::Expected> PhysicalDeviceSelector::select () const { if (!system_info.headless && !criteria.defer_surface_initialization) { if (system_info.surface == nullptr) return detail::Error{ PhysicalDeviceError::no_surface_provided }; } auto physical_devices = detail::get_vector (vkEnumeratePhysicalDevices, system_info.instance); if (!physical_devices.has_value ()) { return detail::Error{ PhysicalDeviceError::failed_enumerate_physical_devices, physical_devices.error () }; } if (physical_devices.value ().size () == 0) { return detail::Error{ PhysicalDeviceError::no_physical_devices_found }; } std::vector phys_device_descriptions; for (auto& phys_device : physical_devices.value ()) { phys_device_descriptions.push_back (populate_device_details (phys_device)); } PhysicalDeviceDesc selected_device{}; if (criteria.use_first_gpu_unconditionally) { selected_device = phys_device_descriptions.at (0); } else { for (const auto& device : phys_device_descriptions) { auto suitable = is_device_suitable (device); if (suitable == Suitable::yes) { selected_device = device; break; } else if (suitable == Suitable::partial) { selected_device = device; } } } if (selected_device.phys_device == VK_NULL_HANDLE) { return detail::Error{ PhysicalDeviceError::no_suitable_device }; } PhysicalDevice out_device{}; out_device.physical_device = selected_device.phys_device; out_device.surface = system_info.surface; out_device.features = criteria.required_features; out_device.properties = selected_device.device_properties; out_device.memory_properties = selected_device.mem_properties; out_device.queue_families = selected_device.queue_families; out_device.defer_surface_initialization = criteria.defer_surface_initialization; out_device.extensions_to_enable.insert (out_device.extensions_to_enable.end (), criteria.required_extensions.begin (), criteria.required_extensions.end ()); auto desired_extensions_supported = detail::check_device_extension_support (out_device.physical_device, criteria.desired_extensions); out_device.extensions_to_enable.insert (out_device.extensions_to_enable.end (), desired_extensions_supported.begin (), desired_extensions_supported.end ()); return out_device; } PhysicalDeviceSelector& PhysicalDeviceSelector::set_surface (VkSurfaceKHR surface) { system_info.surface = surface; system_info.headless = false; return *this; } PhysicalDeviceSelector& PhysicalDeviceSelector::prefer_gpu_device_type (PreferredDeviceType type) { criteria.preferred_type = type; return *this; } PhysicalDeviceSelector& PhysicalDeviceSelector::allow_any_gpu_device_type (bool allow_any_type) { criteria.allow_any_type = allow_any_type; return *this; } PhysicalDeviceSelector& PhysicalDeviceSelector::require_present (bool require) { criteria.require_present = require; return *this; } PhysicalDeviceSelector& PhysicalDeviceSelector::require_dedicated_transfer_queue () { criteria.require_dedicated_transfer_queue = true; return *this; } PhysicalDeviceSelector& PhysicalDeviceSelector::require_dedicated_compute_queue () { criteria.require_dedicated_compute_queue = true; return *this; } PhysicalDeviceSelector& PhysicalDeviceSelector::require_separate_transfer_queue () { criteria.require_separate_transfer_queue = true; return *this; } PhysicalDeviceSelector& PhysicalDeviceSelector::require_separate_compute_queue () { criteria.require_separate_compute_queue = true; return *this; } PhysicalDeviceSelector& PhysicalDeviceSelector::required_device_memory_size (VkDeviceSize size) { criteria.required_mem_size = size; return *this; } PhysicalDeviceSelector& PhysicalDeviceSelector::desired_device_memory_size (VkDeviceSize size) { criteria.desired_mem_size = size; return *this; } PhysicalDeviceSelector& PhysicalDeviceSelector::add_required_extension (const char* extension) { criteria.required_extensions.push_back (extension); return *this; } PhysicalDeviceSelector& PhysicalDeviceSelector::add_required_extensions (std::vector extensions) { criteria.required_extensions.insert ( criteria.required_extensions.end (), extensions.begin (), extensions.end ()); return *this; } PhysicalDeviceSelector& PhysicalDeviceSelector::add_desired_extension (const char* extension) { criteria.desired_extensions.push_back (extension); return *this; } PhysicalDeviceSelector& PhysicalDeviceSelector::add_desired_extensions (std::vector extensions) { criteria.desired_extensions.insert ( criteria.desired_extensions.end (), extensions.begin (), extensions.end ()); return *this; } PhysicalDeviceSelector& PhysicalDeviceSelector::set_minimum_version (uint32_t major, uint32_t minor) { criteria.required_version = VK_MAKE_VERSION (major, minor, 0); return *this; } PhysicalDeviceSelector& PhysicalDeviceSelector::set_desired_version (uint32_t major, uint32_t minor) { criteria.desired_version = VK_MAKE_VERSION (major, minor, 0); return *this; } PhysicalDeviceSelector& PhysicalDeviceSelector::set_required_features (VkPhysicalDeviceFeatures features) { criteria.required_features = features; return *this; } PhysicalDeviceSelector& PhysicalDeviceSelector::defer_surface_initialization () { criteria.defer_surface_initialization = true; return *this; } PhysicalDeviceSelector& PhysicalDeviceSelector::select_first_device_unconditionally (bool unconditionally) { criteria.use_first_gpu_unconditionally = unconditionally; return *this; } bool PhysicalDevice::has_dedicated_compute_queue () const { return detail::get_dedicated_compute_queue_index (queue_families) >= 0; } bool PhysicalDevice::has_separate_compute_queue () const { return detail::get_separate_compute_queue_index (queue_families) >= 0; } bool PhysicalDevice::has_dedicated_transfer_queue () const { return detail::get_dedicated_transfer_queue_index (queue_families) >= 0; } bool PhysicalDevice::has_separate_transfer_queue () const { return detail::get_separate_transfer_queue_index (queue_families) >= 0; } std::vector PhysicalDevice::get_queue_families () const { return queue_families; } // ---- Queues ---- // detail::Expected> Device::get_queue_index (QueueType type) const { int index = -1; switch (type) { case QueueType::present: index = detail::get_present_queue_index (physical_device.physical_device, surface, queue_families); if (index < 0) return detail::Error{ QueueError::present_unavailable }; break; case QueueType::graphics: index = detail::get_graphics_queue_index (queue_families); if (index < 0) return detail::Error{ QueueError::graphics_unavailable }; break; case QueueType::compute: index = detail::get_separate_compute_queue_index (queue_families); if (index < 0) return detail::Error{ QueueError::compute_unavailable }; break; case QueueType::transfer: index = detail::get_separate_transfer_queue_index (queue_families); if (index < 0) return detail::Error{ QueueError::transfer_unavailable }; break; default: return detail::Error{ QueueError::invalid_queue_family_index }; } return static_cast (index); } detail::Expected> Device::get_dedicated_queue_index (QueueType type) const { int index = -1; switch (type) { case QueueType::compute: index = detail::get_dedicated_compute_queue_index (queue_families); if (index < 0) return detail::Error{ QueueError::compute_unavailable }; break; case QueueType::transfer: index = detail::get_dedicated_transfer_queue_index (queue_families); if (index < 0) return detail::Error{ QueueError::transfer_unavailable }; break; default: return detail::Error{ QueueError::invalid_queue_family_index }; } return static_cast (index); } namespace detail { VkQueue get_queue (VkDevice device, uint32_t family) { VkQueue out_queue; vkGetDeviceQueue (device, family, 0, &out_queue); return out_queue; } } // namespace detail detail::Expected> Device::get_queue (QueueType type) const { auto index = get_queue_index (type); if (!index.has_value ()) return index.error (); return detail::get_queue (device, index.value ()); } detail::Expected> Device::get_dedicated_queue (QueueType type) const { auto index = get_dedicated_queue_index (type); if (!index.has_value ()) return index.error (); return detail::get_queue (device, index.value ()); } // ---- Device ---- // CustomQueueDescription::CustomQueueDescription (uint32_t index, uint32_t count, std::vector priorities) : index (index), count (count), priorities (priorities) { assert (count == priorities.size ()); } void destroy_device (Device device) { vkDestroyDevice (device.device, device.allocation_callbacks); } DeviceBuilder::DeviceBuilder (PhysicalDevice phys_device) { info.physical_device = phys_device; info.surface = phys_device.surface; info.queue_families = phys_device.queue_families; info.features = phys_device.features; info.extensions_to_enable = phys_device.extensions_to_enable; info.defer_surface_initialization = phys_device.defer_surface_initialization; } detail::Expected> DeviceBuilder::build () const { std::vector queue_descriptions; queue_descriptions.insert ( queue_descriptions.end (), info.queue_descriptions.begin (), info.queue_descriptions.end ()); if (queue_descriptions.size () == 0) { for (uint32_t i = 0; i < info.queue_families.size (); i++) { queue_descriptions.push_back ({ i, 1, std::vector{ 1.0f } }); } } std::vector queueCreateInfos; for (auto& desc : queue_descriptions) { VkDeviceQueueCreateInfo queue_create_info = {}; queue_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; queue_create_info.queueFamilyIndex = desc.index; queue_create_info.queueCount = desc.count; queue_create_info.pQueuePriorities = desc.priorities.data (); queueCreateInfos.push_back (queue_create_info); } std::vector extensions = info.extensions_to_enable; if (info.surface != VK_NULL_HANDLE || info.defer_surface_initialization) extensions.push_back ({ VK_KHR_SWAPCHAIN_EXTENSION_NAME }); // VUID-VkDeviceCreateInfo-pNext-00373 - don't add pEnabledFeatures if the phys_dev_features_2 is present bool has_phys_dev_features_2 = false; for (auto& pNext_struct : info.pNext_chain) { if (pNext_struct->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) { has_phys_dev_features_2 = true; } } VkDeviceCreateInfo device_create_info = {}; device_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; detail::setup_pNext_chain (device_create_info, info.pNext_chain); device_create_info.flags = info.flags; device_create_info.queueCreateInfoCount = static_cast (queueCreateInfos.size ()); device_create_info.pQueueCreateInfos = queueCreateInfos.data (); device_create_info.enabledExtensionCount = static_cast (extensions.size ()); device_create_info.ppEnabledExtensionNames = extensions.data (); if (!has_phys_dev_features_2) { device_create_info.pEnabledFeatures = &info.features; } Device device; VkResult res = vkCreateDevice (info.physical_device.physical_device, &device_create_info, info.allocation_callbacks, &device.device); if (res != VK_SUCCESS) { return detail::Error{ DeviceError::failed_create_device, res }; } device.physical_device = info.physical_device; device.surface = info.surface; device.queue_families = info.queue_families; device.allocation_callbacks = info.allocation_callbacks; return device; } DeviceBuilder& DeviceBuilder::custom_queue_setup (std::vector queue_descriptions) { info.queue_descriptions = queue_descriptions; return *this; } DeviceBuilder& DeviceBuilder::set_allocation_callbacks (VkAllocationCallbacks* callbacks) { info.allocation_callbacks = callbacks; return *this; } // ---- Swapchain ---- // namespace detail { struct SurfaceSupportDetails { VkSurfaceCapabilitiesKHR capabilities; std::vector formats; std::vector present_modes; }; enum class SurfaceSupportError { surface_handle_null, failed_get_surface_capabilities, failed_enumerate_surface_formats, failed_enumerate_present_modes }; Expected> query_surface_support_details ( VkPhysicalDevice phys_device, VkSurfaceKHR surface) { if (surface == VK_NULL_HANDLE) return detail::Error{ SurfaceSupportError::surface_handle_null }; VkSurfaceCapabilitiesKHR capabilities; VkResult res = vkGetPhysicalDeviceSurfaceCapabilitiesKHR (phys_device, surface, &capabilities); if (res != VK_SUCCESS) { return detail::Error{ SurfaceSupportError::failed_get_surface_capabilities, res }; } auto formats = detail::get_vector ( vkGetPhysicalDeviceSurfaceFormatsKHR, phys_device, surface); if (!formats.has_value ()) return detail::Error{ SurfaceSupportError::failed_enumerate_surface_formats, formats.error () }; auto present_modes = detail::get_vector ( vkGetPhysicalDeviceSurfacePresentModesKHR, phys_device, surface); if (!present_modes.has_value ()) return detail::Error{ SurfaceSupportError::failed_enumerate_present_modes, formats.error () }; return SurfaceSupportDetails{ capabilities, formats.value (), present_modes.value () }; } VkSurfaceFormatKHR find_surface_format (std::vector const& available_formats, std::vector const& desired_formats) { for (auto const& desired_format : desired_formats) { for (auto const& available_format : available_formats) { // finds the first format that is desired and available if (desired_format.format == available_format.format && desired_format.colorSpace == available_format.colorSpace) { return desired_format; } } } // use the first available one if any desired formats aren't found return available_formats[0]; } VkPresentModeKHR find_present_mode (std::vector const& available_resent_modes, std::vector const& desired_present_modes) { for (auto const& desired_pm : desired_present_modes) { for (auto const& available_pm : available_resent_modes) { // finds the first present mode that is desired and available if (desired_pm == available_pm) return desired_pm; } } // only present mode required, use as a fallback return VK_PRESENT_MODE_FIFO_KHR; } template T minimum (T a, T b) { return a < b ? a : b; } template T maximum (T a, T b) { return a > b ? a : b; } VkExtent2D find_extent ( VkSurfaceCapabilitiesKHR const& capabilities, uint32_t desired_width, uint32_t desired_height) { if (capabilities.currentExtent.width != UINT32_MAX) { return capabilities.currentExtent; } else { VkExtent2D actualExtent = { desired_width, desired_height }; actualExtent.width = maximum (capabilities.minImageExtent.width, minimum (capabilities.maxImageExtent.width, actualExtent.width)); actualExtent.height = maximum (capabilities.minImageExtent.height, minimum (capabilities.maxImageExtent.height, actualExtent.height)); return actualExtent; } } } // namespace detail SwapchainBuilder::SwapchainBuilder (Device const& device) { info.device = device.device; info.physical_device = device.physical_device.physical_device; info.surface = device.surface; auto present = device.get_queue_index (QueueType::present); auto graphics = device.get_queue_index (QueueType::graphics); // TODO: handle error of queue's not available info.graphics_queue_index = present.value (); info.present_queue_index = graphics.value (); } SwapchainBuilder::SwapchainBuilder ( VkPhysicalDevice const physical_device, VkDevice const device, VkSurfaceKHR const surface) { info.physical_device = physical_device; info.device = device; info.surface = surface; auto queue_families = detail::get_vector_noerror ( vkGetPhysicalDeviceQueueFamilyProperties, physical_device); int graphics_queue_index = detail::get_graphics_queue_index (queue_families); int present_queue_index = detail::get_present_queue_index (physical_device, surface, queue_families); // TODO: handle queue indexes being below zero info.graphics_queue_index = static_cast (graphics_queue_index); info.present_queue_index = static_cast (present_queue_index); } detail::Expected> SwapchainBuilder::build () const { return build (VK_NULL_HANDLE); } detail::Expected> SwapchainBuilder::build ( VkSwapchainKHR old_swapchain) const { auto desired_formats = info.desired_formats; if (desired_formats.size () == 0) add_desired_formats (desired_formats); auto desired_present_modes = info.desired_present_modes; if (desired_present_modes.size () == 0) add_desired_present_modes (desired_present_modes); auto surface_support = detail::query_surface_support_details (info.physical_device, info.surface); if (!surface_support.has_value ()) return detail::Error{ SwapchainError::failed_query_surface_support_details, surface_support.error ().vk_result }; VkSurfaceFormatKHR surface_format = detail::find_surface_format (surface_support.value ().formats, desired_formats); VkPresentModeKHR present_mode = detail::find_present_mode (surface_support.value ().present_modes, desired_present_modes); VkExtent2D extent = detail::find_extent ( surface_support.value ().capabilities, info.desired_width, info.desired_height); uint32_t imageCount = surface_support.value ().capabilities.minImageCount + 1; if (surface_support.value ().capabilities.maxImageCount > 0 && imageCount > surface_support.value ().capabilities.maxImageCount) { imageCount = surface_support.value ().capabilities.maxImageCount; } VkSwapchainCreateInfoKHR swapchain_create_info = {}; swapchain_create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; detail::setup_pNext_chain (swapchain_create_info, info.pNext_elements); swapchain_create_info.surface = info.surface; swapchain_create_info.minImageCount = imageCount; swapchain_create_info.imageFormat = surface_format.format; swapchain_create_info.imageColorSpace = surface_format.colorSpace; swapchain_create_info.imageExtent = extent; swapchain_create_info.imageArrayLayers = 1; swapchain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; uint32_t queue_family_indices[] = { info.graphics_queue_index, info.present_queue_index }; if (info.graphics_queue_index != info.present_queue_index) { swapchain_create_info.imageSharingMode = VK_SHARING_MODE_CONCURRENT; swapchain_create_info.queueFamilyIndexCount = 2; swapchain_create_info.pQueueFamilyIndices = queue_family_indices; } else { swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; } swapchain_create_info.preTransform = surface_support.value ().capabilities.currentTransform; swapchain_create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; swapchain_create_info.presentMode = present_mode; swapchain_create_info.clipped = VK_TRUE; swapchain_create_info.oldSwapchain = old_swapchain; Swapchain swapchain{}; VkResult res = vkCreateSwapchainKHR ( info.device, &swapchain_create_info, info.allocation_callbacks, &swapchain.swapchain); if (res != VK_SUCCESS) { return detail::Error{ SwapchainError::failed_create_swapchain, res }; } swapchain.device = info.device; swapchain.image_format = surface_format.format; swapchain.extent = extent; auto images = swapchain.get_images (); if (!images) { return detail::Error{ SwapchainError::failed_get_swapchain_images }; } swapchain.image_count = static_cast (images.value ().size ()); swapchain.allocation_callbacks = info.allocation_callbacks; return swapchain; } detail::Expected> SwapchainBuilder::recreate ( Swapchain const& swapchain) const { return build (swapchain.swapchain); } detail::Expected, detail::Error> Swapchain::get_images () { auto swapchain_images = detail::get_vector (vkGetSwapchainImagesKHR, device, swapchain); if (!swapchain_images) { return detail::Error{ SwapchainError::failed_get_swapchain_images, swapchain_images.error () }; } return swapchain_images.value (); } detail::Expected, detail::Error> Swapchain::get_image_views () { auto swapchain_images_ret = get_images (); if (!swapchain_images_ret) return swapchain_images_ret.error (); auto swapchain_images = swapchain_images_ret.value (); std::vector views{ swapchain_images.size () }; for (size_t i = 0; i < swapchain_images.size (); i++) { VkImageViewCreateInfo createInfo = {}; createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; createInfo.image = swapchain_images[i]; createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; createInfo.format = image_format; createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY; createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY; createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY; createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY; createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; createInfo.subresourceRange.baseMipLevel = 0; createInfo.subresourceRange.levelCount = 1; createInfo.subresourceRange.baseArrayLayer = 0; createInfo.subresourceRange.layerCount = 1; VkResult res = vkCreateImageView (device, &createInfo, allocation_callbacks, &views[i]); if (res != VK_SUCCESS) return detail::Error{ SwapchainError::failed_create_swapchain_image_views, res }; } return views; } void Swapchain::destroy_image_views (std::vector const& image_views) { for (auto& image_view : image_views) { vkDestroyImageView (device, image_view, allocation_callbacks); } } void destroy_swapchain (Swapchain const& swapchain) { if (swapchain.device != VK_NULL_HANDLE && swapchain.swapchain != VK_NULL_HANDLE) vkDestroySwapchainKHR (swapchain.device, swapchain.swapchain, swapchain.allocation_callbacks); } SwapchainBuilder& SwapchainBuilder::set_desired_extent (uint32_t width, uint32_t height) { info.desired_width = width; info.desired_height = height; return *this; } SwapchainBuilder& SwapchainBuilder::set_desired_format (VkSurfaceFormatKHR format) { info.desired_formats.insert (info.desired_formats.begin (), format); return *this; } SwapchainBuilder& SwapchainBuilder::add_fallback_format (VkSurfaceFormatKHR format) { info.desired_formats.push_back (format); return *this; } SwapchainBuilder& SwapchainBuilder::use_default_format_selection () { add_desired_formats (info.desired_formats); return *this; } SwapchainBuilder& SwapchainBuilder::set_desired_present_mode (VkPresentModeKHR present_mode) { info.desired_present_modes.insert (info.desired_present_modes.begin (), present_mode); return *this; } SwapchainBuilder& SwapchainBuilder::add_fallback_present_mode (VkPresentModeKHR present_mode) { info.desired_present_modes.push_back (present_mode); return *this; } SwapchainBuilder& SwapchainBuilder::use_default_present_mode_selection () { add_desired_present_modes (info.desired_present_modes); return *this; } SwapchainBuilder& SwapchainBuilder::set_allocation_callbacks (VkAllocationCallbacks* callbacks) { info.allocation_callbacks = callbacks; return *this; } void SwapchainBuilder::add_desired_formats (std::vector& formats) const { formats.push_back ({ VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR }); formats.push_back ({ VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR }); } void SwapchainBuilder::add_desired_present_modes (std::vector& modes) const { modes.push_back (VK_PRESENT_MODE_MAILBOX_KHR); modes.push_back (VK_PRESENT_MODE_FIFO_KHR); } } // namespace vkb