Path: blob/master/drivers/vulkan/rendering_context_driver_vulkan.cpp
9903 views
/**************************************************************************/1/* rendering_context_driver_vulkan.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#ifdef VULKAN_ENABLED3132#include "rendering_context_driver_vulkan.h"3334#include "vk_enum_string_helper.h"3536#include "core/config/project_settings.h"37#include "core/version.h"3839#include "rendering_device_driver_vulkan.h"40#include "vulkan_hooks.h"4142#if defined(VK_TRACK_DRIVER_MEMORY)43/*************************************************/44// Driver memory tracking45/*************************************************/46// Total driver memory and allocation amount.47SafeNumeric<size_t> driver_memory_total_memory;48SafeNumeric<size_t> driver_memory_total_alloc_count;49// Amount of driver memory for every object type.50SafeNumeric<size_t> driver_memory_tracker[RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_COUNT][RenderingContextDriverVulkan::VK_TRACKED_SYSTEM_ALLOCATION_SCOPE_COUNT];51// Amount of allocations for every object type.52SafeNumeric<uint32_t> driver_memory_allocation_count[RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_COUNT][RenderingContextDriverVulkan::VK_TRACKED_SYSTEM_ALLOCATION_SCOPE_COUNT];53#endif5455#if defined(VK_TRACK_DEVICE_MEMORY)56/*************************************************/57// Device memory report58/*************************************************/59// Total device memory and allocation amount.60HashMap<uint64_t, size_t> memory_report_table;61// Total memory and allocation amount.62SafeNumeric<uint64_t> memory_report_total_memory;63SafeNumeric<uint64_t> memory_report_total_alloc_count;64// Amount of device memory for every object type.65SafeNumeric<size_t> memory_report_mem_usage[RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_COUNT];66// Amount of device memory allocations for every object type.67SafeNumeric<size_t> memory_report_allocation_count[RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_COUNT];68#endif6970const char *RenderingContextDriverVulkan::get_tracked_object_name(uint32_t p_type_index) const {71#if defined(VK_TRACK_DRIVER_MEMORY) || defined(VK_TRACK_DEVICE_MEMORY)72static constexpr const char *vkTrackedObjectTypeNames[] = { "UNKNOWN",73"INSTANCE",74"PHYSICAL_DEVICE",75"DEVICE",76"QUEUE",77"SEMAPHORE",78"COMMAND_BUFFER",79"FENCE",80"DEVICE_MEMORY",81"BUFFER",82"IMAGE",83"EVENT",84"QUERY_POOL",85"BUFFER_VIEW",86"IMAGE_VIEW",87"SHADER_MODULE",88"PIPELINE_CACHE",89"PIPELINE_LAYOUT",90"RENDER_PASS",91"PIPELINE",92"DESCRIPTOR_SET_LAYOUT",93"SAMPLER",94"DESCRIPTOR_POOL",95"DESCRIPTOR_SET",96"FRAMEBUFFER",97"COMMAND_POOL",98"DESCRIPTOR_UPDATE_TEMPLATE_KHR",99"SURFACE_KHR",100"SWAPCHAIN_KHR",101"DEBUG_UTILS_MESSENGER_EXT",102"DEBUG_REPORT_CALLBACK_EXT",103"ACCELERATION_STRUCTURE",104"VMA_BUFFER_OR_IMAGE" };105106return vkTrackedObjectTypeNames[p_type_index];107#else108return "VK_TRACK_*_MEMORY disabled at build time";109#endif110}111112#if defined(VK_TRACK_DRIVER_MEMORY) || defined(VK_TRACK_DEVICE_MEMORY)113uint64_t RenderingContextDriverVulkan::get_tracked_object_type_count() const {114return VK_TRACKED_OBJECT_TYPE_COUNT;115}116#endif117118#if defined(VK_TRACK_DRIVER_MEMORY) || defined(VK_TRACK_DEVICE_MEMORY)119RenderingContextDriverVulkan::VkTrackedObjectType vk_object_to_tracked_object(VkObjectType p_type) {120if (p_type > VK_OBJECT_TYPE_COMMAND_POOL && p_type != (VkObjectType)RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_VMA) {121switch (p_type) {122case VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE:123return RenderingContextDriverVulkan::VK_TRACKED_OBJECT_DESCRIPTOR_UPDATE_TEMPLATE_KHR;124case VK_OBJECT_TYPE_SURFACE_KHR:125return RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_SURFACE;126case VK_OBJECT_TYPE_SWAPCHAIN_KHR:127return RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_SWAPCHAIN;128case VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT:129return RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT;130case VK_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT:131return RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT;132case VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_KHR:133case VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_NV:134return RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_ACCELERATION_STRUCTURE;135default:136_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Unknown VkObjectType enum value " + itos((uint32_t)p_type) + ".Please add it to VkTrackedObjectType, switch statement in "137"vk_object_to_tracked_object and get_tracked_object_name.",138(int)p_type);139return (RenderingContextDriverVulkan::VkTrackedObjectType)VK_OBJECT_TYPE_UNKNOWN;140}141}142143return (RenderingContextDriverVulkan::VkTrackedObjectType)p_type;144}145#endif146147#if defined(VK_TRACK_DEVICE_MEMORY)148uint64_t RenderingContextDriverVulkan::get_device_total_memory() const {149return memory_report_total_memory.get();150}151152uint64_t RenderingContextDriverVulkan::get_device_allocation_count() const {153return memory_report_total_alloc_count.get();154}155156uint64_t RenderingContextDriverVulkan::get_device_memory_by_object_type(uint32_t p_type) const {157return memory_report_mem_usage[p_type].get();158}159160uint64_t RenderingContextDriverVulkan::get_device_allocs_by_object_type(uint32_t p_type) const {161return memory_report_allocation_count[p_type].get();162}163#endif164165#if defined(VK_TRACK_DRIVER_MEMORY)166uint64_t RenderingContextDriverVulkan::get_driver_total_memory() const {167return driver_memory_total_memory.get();168}169170uint64_t RenderingContextDriverVulkan::get_driver_allocation_count() const {171return driver_memory_total_alloc_count.get();172}173174uint64_t RenderingContextDriverVulkan::get_driver_memory_by_object_type(uint32_t p_type) const {175uint64_t ret = 0;176for (uint32_t i = 0; i < VK_TRACKED_SYSTEM_ALLOCATION_SCOPE_COUNT; i++) {177ret += driver_memory_tracker[p_type][i].get();178}179180return ret;181}182183uint64_t RenderingContextDriverVulkan::get_driver_allocs_by_object_type(uint32_t p_type) const {184uint64_t ret = 0;185for (uint32_t i = 0; i < VK_TRACKED_SYSTEM_ALLOCATION_SCOPE_COUNT; i++) {186ret += driver_memory_allocation_count[p_type][i].get();187}188189return ret;190}191#endif192193#if defined(VK_TRACK_DEVICE_MEMORY)194void RenderingContextDriverVulkan::memory_report_callback(const VkDeviceMemoryReportCallbackDataEXT *p_callback_data, void *p_user_data) {195if (!p_callback_data) {196return;197}198const RenderingContextDriverVulkan::VkTrackedObjectType obj_type = vk_object_to_tracked_object(p_callback_data->objectType);199uint64_t obj_id = p_callback_data->memoryObjectId;200201if (p_callback_data->type == VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_ALLOCATE_EXT) {202// Realloc, update size203if (memory_report_table.has(obj_id)) {204memory_report_total_memory.sub(memory_report_table[obj_id]);205memory_report_mem_usage[obj_type].sub(memory_report_table[obj_id]);206207memory_report_total_memory.add(p_callback_data->size);208memory_report_mem_usage[obj_type].add(p_callback_data->size);209210memory_report_table[p_callback_data->memoryObjectId] = p_callback_data->size;211} else {212memory_report_table[obj_id] = p_callback_data->size;213214memory_report_total_alloc_count.increment();215memory_report_allocation_count[obj_type].increment();216memory_report_mem_usage[obj_type].add(p_callback_data->size);217memory_report_total_memory.add(p_callback_data->size);218}219} else if (p_callback_data->type == VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_FREE_EXT) {220if (memory_report_table.has(obj_id)) {221memory_report_total_alloc_count.decrement();222memory_report_allocation_count[obj_type].decrement();223memory_report_mem_usage[obj_type].sub(p_callback_data->size);224memory_report_total_memory.sub(p_callback_data->size);225226memory_report_table.remove(memory_report_table.find(obj_id));227}228}229}230#endif231232VkAllocationCallbacks *RenderingContextDriverVulkan::get_allocation_callbacks(VkObjectType p_type) {233#if !defined(VK_TRACK_DRIVER_MEMORY)234return nullptr;235#else236if (!Engine::get_singleton()->is_extra_gpu_memory_tracking_enabled()) {237return nullptr;238}239240#ifdef _MSC_VER241#define LAMBDA_VK_CALL_CONV242#else243#define LAMBDA_VK_CALL_CONV VKAPI_PTR244#endif245246struct TrackedMemHeader {247size_t size;248VkSystemAllocationScope allocation_scope;249VkTrackedObjectType type;250};251VkAllocationCallbacks tracking_callbacks = {252// Allocation function253nullptr,254[](255void *p_user_data,256size_t size,257size_t alignment,258VkSystemAllocationScope allocation_scope) LAMBDA_VK_CALL_CONV -> void * {259static constexpr size_t tracking_data_size = 32;260VkTrackedObjectType type = static_cast<VkTrackedObjectType>(*reinterpret_cast<VkTrackedObjectType *>(p_user_data));261262driver_memory_total_memory.add(size);263driver_memory_total_alloc_count.increment();264driver_memory_tracker[type][allocation_scope].add(size);265driver_memory_allocation_count[type][allocation_scope].increment();266267alignment = MAX(alignment, tracking_data_size);268269uint8_t *ret = reinterpret_cast<uint8_t *>(Memory::alloc_aligned_static(size + alignment, alignment));270if (ret == nullptr) {271return nullptr;272}273274// Track allocation275TrackedMemHeader *header = reinterpret_cast<TrackedMemHeader *>(ret);276header->size = size;277header->allocation_scope = allocation_scope;278header->type = type;279*reinterpret_cast<size_t *>(ret + alignment - sizeof(size_t)) = alignment;280281// Return first available chunk of memory282return ret + alignment;283},284285// Reallocation function286[](287void *p_user_data,288void *p_original,289size_t size,290size_t alignment,291VkSystemAllocationScope allocation_scope) LAMBDA_VK_CALL_CONV -> void * {292if (p_original == nullptr) {293VkObjectType type = static_cast<VkObjectType>(*reinterpret_cast<uint32_t *>(p_user_data));294return get_allocation_callbacks(type)->pfnAllocation(p_user_data, size, alignment, allocation_scope);295}296297uint8_t *mem = reinterpret_cast<uint8_t *>(p_original);298// Retrieve alignment299alignment = *reinterpret_cast<size_t *>(mem - sizeof(size_t));300// Retrieve allocation data301TrackedMemHeader *header = reinterpret_cast<TrackedMemHeader *>(mem - alignment);302303// Update allocation size304driver_memory_total_memory.sub(header->size);305driver_memory_total_memory.add(size);306driver_memory_tracker[header->type][header->allocation_scope].sub(header->size);307driver_memory_tracker[header->type][header->allocation_scope].add(size);308309uint8_t *ret = reinterpret_cast<uint8_t *>(Memory::realloc_aligned_static(header, size + alignment, header->size + alignment, alignment));310if (ret == nullptr) {311return nullptr;312}313// Update tracker314header = reinterpret_cast<TrackedMemHeader *>(ret);315header->size = size;316return ret + alignment;317},318319// Free function320[](321void *p_user_data,322void *p_memory) LAMBDA_VK_CALL_CONV {323if (!p_memory) {324return;325}326327uint8_t *mem = reinterpret_cast<uint8_t *>(p_memory);328size_t alignment = *reinterpret_cast<size_t *>(mem - sizeof(size_t));329TrackedMemHeader *header = reinterpret_cast<TrackedMemHeader *>(mem - alignment);330331driver_memory_total_alloc_count.decrement();332driver_memory_total_memory.sub(header->size);333driver_memory_tracker[header->type][header->allocation_scope].sub(header->size);334driver_memory_allocation_count[header->type][header->allocation_scope].decrement();335336Memory::free_aligned_static(header);337},338// Internal allocation / deallocation. We don't track them as they cannot really be controlled or optimized by the programmer.339[](340void *p_user_data,341size_t size,342VkInternalAllocationType allocation_type,343VkSystemAllocationScope allocation_scope) LAMBDA_VK_CALL_CONV {344},345[](346void *p_user_data,347size_t size,348VkInternalAllocationType allocation_type,349VkSystemAllocationScope allocation_scope) LAMBDA_VK_CALL_CONV {350},351};352353// Create a callback per object type354static VkAllocationCallbacks object_callbacks[VK_TRACKED_OBJECT_TYPE_COUNT] = {};355static uint32_t object_user_data[VK_TRACKED_OBJECT_TYPE_COUNT] = {};356357// Only build the first time358if (!object_callbacks[0].pfnAllocation) {359for (uint32_t c = 0; c < VK_TRACKED_OBJECT_TYPE_COUNT; ++c) {360object_callbacks[c] = tracking_callbacks;361object_user_data[c] = c;362object_callbacks[c].pUserData = &object_user_data[c];363364for (uint32_t i = 0; i < VK_TRACKED_SYSTEM_ALLOCATION_SCOPE_COUNT; i++) {365driver_memory_tracker[c][i].set(0);366driver_memory_allocation_count[c][i].set(0);367}368}369}370371uint32_t type_index = vk_object_to_tracked_object(p_type);372return &object_callbacks[type_index];373#endif374}375376RenderingContextDriverVulkan::RenderingContextDriverVulkan() {377// Empty constructor.378}379380RenderingContextDriverVulkan::~RenderingContextDriverVulkan() {381if (debug_messenger != VK_NULL_HANDLE && functions.DestroyDebugUtilsMessengerEXT != nullptr) {382functions.DestroyDebugUtilsMessengerEXT(instance, debug_messenger, get_allocation_callbacks(VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT));383}384385if (debug_report != VK_NULL_HANDLE && functions.DestroyDebugReportCallbackEXT != nullptr) {386functions.DestroyDebugReportCallbackEXT(instance, debug_report, get_allocation_callbacks(VK_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT));387}388389if (instance != VK_NULL_HANDLE) {390vkDestroyInstance(instance, get_allocation_callbacks(VK_OBJECT_TYPE_INSTANCE));391}392}393394Error RenderingContextDriverVulkan::_initialize_vulkan_version() {395// https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VkApplicationInfo.html#_description396// For Vulkan 1.0 vkEnumerateInstanceVersion is not available, including not in the loader we compile against on Android.397typedef VkResult(VKAPI_PTR * _vkEnumerateInstanceVersion)(uint32_t *);398_vkEnumerateInstanceVersion func = (_vkEnumerateInstanceVersion)vkGetInstanceProcAddr(nullptr, "vkEnumerateInstanceVersion");399if (func != nullptr) {400uint32_t api_version;401VkResult res = func(&api_version);402if (res == VK_SUCCESS) {403instance_api_version = api_version;404} else {405// According to the documentation this shouldn't fail with anything except a memory allocation error406// in which case we're in deep trouble anyway.407ERR_FAIL_V(ERR_CANT_CREATE);408}409} else {410print_line("vkEnumerateInstanceVersion not available, assuming Vulkan 1.0.");411instance_api_version = VK_API_VERSION_1_0;412}413414return OK;415}416417void RenderingContextDriverVulkan::_register_requested_instance_extension(const CharString &p_extension_name, bool p_required) {418ERR_FAIL_COND(requested_instance_extensions.has(p_extension_name));419requested_instance_extensions[p_extension_name] = p_required;420}421422Error RenderingContextDriverVulkan::_initialize_instance_extensions() {423enabled_instance_extension_names.clear();424425// The surface extension and the platform-specific surface extension are core requirements.426_register_requested_instance_extension(VK_KHR_SURFACE_EXTENSION_NAME, true);427if (_get_platform_surface_extension()) {428_register_requested_instance_extension(_get_platform_surface_extension(), true);429}430431if (_use_validation_layers()) {432_register_requested_instance_extension(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, false);433}434435// This extension allows us to use the properties2 features to query additional device capabilities.436_register_requested_instance_extension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, false);437438#if defined(USE_VOLK) && (defined(MACOS_ENABLED) || defined(IOS_ENABLED))439_register_requested_instance_extension(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME, true);440#endif441442// Only enable debug utils in verbose mode or DEV_ENABLED.443// End users would get spammed with messages of varying verbosity due to the444// mess that thirdparty layers/extensions and drivers seem to leave in their445// wake, making the Windows registry a bottomless pit of broken layer JSON.446#ifdef DEV_ENABLED447bool want_debug_utils = true;448#else449bool want_debug_utils = OS::get_singleton()->is_stdout_verbose();450#endif451if (want_debug_utils) {452_register_requested_instance_extension(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, false);453}454455// Load instance extensions that are available.456uint32_t instance_extension_count = 0;457VkResult err = vkEnumerateInstanceExtensionProperties(nullptr, &instance_extension_count, nullptr);458ERR_FAIL_COND_V(err != VK_SUCCESS && err != VK_INCOMPLETE, ERR_CANT_CREATE);459ERR_FAIL_COND_V_MSG(instance_extension_count == 0, ERR_CANT_CREATE, "No instance extensions were found.");460461TightLocalVector<VkExtensionProperties> instance_extensions;462instance_extensions.resize(instance_extension_count);463err = vkEnumerateInstanceExtensionProperties(nullptr, &instance_extension_count, instance_extensions.ptr());464if (err != VK_SUCCESS && err != VK_INCOMPLETE) {465ERR_FAIL_V(ERR_CANT_CREATE);466}467468#ifdef DEV_ENABLED469for (uint32_t i = 0; i < instance_extension_count; i++) {470print_verbose(String("VULKAN: Found instance extension ") + String::utf8(instance_extensions[i].extensionName) + String("."));471}472#endif473474// Enable all extensions that are supported and requested.475for (uint32_t i = 0; i < instance_extension_count; i++) {476CharString extension_name(instance_extensions[i].extensionName);477if (requested_instance_extensions.has(extension_name)) {478enabled_instance_extension_names.insert(extension_name);479}480}481482// Now check our requested extensions.483for (KeyValue<CharString, bool> &requested_extension : requested_instance_extensions) {484if (!enabled_instance_extension_names.has(requested_extension.key)) {485if (requested_extension.value) {486ERR_FAIL_V_MSG(ERR_BUG, String("Required extension ") + String::utf8(requested_extension.key) + String(" not found."));487} else {488print_verbose(String("Optional extension ") + String::utf8(requested_extension.key) + String(" not found."));489}490}491}492493return OK;494}495496Error RenderingContextDriverVulkan::_find_validation_layers(TightLocalVector<const char *> &r_layer_names) const {497r_layer_names.clear();498499uint32_t instance_layer_count = 0;500VkResult err = vkEnumerateInstanceLayerProperties(&instance_layer_count, nullptr);501ERR_FAIL_COND_V(err != VK_SUCCESS, ERR_CANT_CREATE);502if (instance_layer_count > 0) {503TightLocalVector<VkLayerProperties> layer_properties;504layer_properties.resize(instance_layer_count);505err = vkEnumerateInstanceLayerProperties(&instance_layer_count, layer_properties.ptr());506ERR_FAIL_COND_V(err != VK_SUCCESS, ERR_CANT_CREATE);507508// Preferred set of validation layers.509const std::initializer_list<const char *> preferred = { "VK_LAYER_KHRONOS_validation" };510511// Alternative (deprecated, removed in SDK 1.1.126.0) set of validation layers.512const std::initializer_list<const char *> lunarg = { "VK_LAYER_LUNARG_standard_validation" };513514// Alternative (deprecated, removed in SDK 1.1.121.1) set of validation layers.515const std::initializer_list<const char *> google = { "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation", "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation", "VK_LAYER_GOOGLE_unique_objects" };516517// Verify all the layers of the list are present.518for (const std::initializer_list<const char *> &list : { preferred, lunarg, google }) {519bool layers_found = false;520for (const char *layer_name : list) {521layers_found = false;522523for (const VkLayerProperties &properties : layer_properties) {524if (!strcmp(properties.layerName, layer_name)) {525layers_found = true;526break;527}528}529530if (!layers_found) {531break;532}533}534535if (layers_found) {536r_layer_names.reserve(list.size());537for (const char *layer_name : list) {538r_layer_names.push_back(layer_name);539}540541break;542}543}544}545546return OK;547}548549VKAPI_ATTR VkBool32 VKAPI_CALL RenderingContextDriverVulkan::_debug_messenger_callback(VkDebugUtilsMessageSeverityFlagBitsEXT p_message_severity, VkDebugUtilsMessageTypeFlagsEXT p_message_type, const VkDebugUtilsMessengerCallbackDataEXT *p_callback_data, void *p_user_data) {550// This error needs to be ignored because the AMD allocator will mix up memory types on IGP processors.551if (strstr(p_callback_data->pMessage, "Mapping an image with layout") != nullptr && strstr(p_callback_data->pMessage, "can result in undefined behavior if this memory is used by the device") != nullptr) {552return VK_FALSE;553}554// This needs to be ignored because Validator is wrong here.555if (strstr(p_callback_data->pMessage, "Invalid SPIR-V binary version 1.3") != nullptr) {556return VK_FALSE;557}558// This needs to be ignored because Validator is wrong here.559if (strstr(p_callback_data->pMessage, "Shader requires flag") != nullptr) {560return VK_FALSE;561}562563// This needs to be ignored because Validator is wrong here.564if (strstr(p_callback_data->pMessage, "SPIR-V module not valid: Pointer operand") != nullptr && strstr(p_callback_data->pMessage, "must be a memory object") != nullptr) {565return VK_FALSE;566}567568if (p_callback_data->pMessageIdName && strstr(p_callback_data->pMessageIdName, "UNASSIGNED-CoreValidation-DrawState-ClearCmdBeforeDraw") != nullptr) {569return VK_FALSE;570}571572String type_string;573switch (p_message_type) {574case (VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT):575type_string = "GENERAL";576break;577case (VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT):578type_string = "VALIDATION";579break;580case (VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT):581type_string = "PERFORMANCE";582break;583case (VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT):584type_string = "VALIDATION|PERFORMANCE";585break;586}587588String objects_string;589if (p_callback_data->objectCount > 0) {590objects_string = "\n\tObjects - " + String::num_int64(p_callback_data->objectCount);591for (uint32_t object = 0; object < p_callback_data->objectCount; ++object) {592objects_string +=593"\n\t\tObject[" + String::num_int64(object) + "]" +594" - " + string_VkObjectType(p_callback_data->pObjects[object].objectType) +595", Handle " + String::num_int64(p_callback_data->pObjects[object].objectHandle);596597if (p_callback_data->pObjects[object].pObjectName != nullptr && strlen(p_callback_data->pObjects[object].pObjectName) > 0) {598objects_string += ", Name \"" + String(p_callback_data->pObjects[object].pObjectName) + "\"";599}600}601}602603String labels_string;604if (p_callback_data->cmdBufLabelCount > 0) {605labels_string = "\n\tCommand Buffer Labels - " + String::num_int64(p_callback_data->cmdBufLabelCount);606for (uint32_t cmd_buf_label = 0; cmd_buf_label < p_callback_data->cmdBufLabelCount; ++cmd_buf_label) {607labels_string +=608"\n\t\tLabel[" + String::num_int64(cmd_buf_label) + "]" +609" - " + p_callback_data->pCmdBufLabels[cmd_buf_label].pLabelName +610"{ ";611612for (int color_idx = 0; color_idx < 4; ++color_idx) {613labels_string += String::num(p_callback_data->pCmdBufLabels[cmd_buf_label].color[color_idx]);614if (color_idx < 3) {615labels_string += ", ";616}617}618619labels_string += " }";620}621}622623String error_message(type_string +624" - Message Id Number: " + String::num_int64(p_callback_data->messageIdNumber) +625" | Message Id Name: " + p_callback_data->pMessageIdName +626"\n\t" + p_callback_data->pMessage +627objects_string + labels_string);628629// Convert VK severity to our own log macros.630switch (p_message_severity) {631case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT:632print_verbose(error_message);633break;634case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT:635print_line(error_message);636break;637case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT:638WARN_PRINT(error_message);639break;640case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT:641ERR_PRINT(error_message);642CRASH_COND_MSG(Engine::get_singleton()->is_abort_on_gpu_errors_enabled(), "Crashing, because abort on GPU errors is enabled.");643break;644case VK_DEBUG_UTILS_MESSAGE_SEVERITY_FLAG_BITS_MAX_ENUM_EXT:645break; // Shouldn't happen, only handling to make compilers happy.646}647648return VK_FALSE;649}650651VKAPI_ATTR VkBool32 VKAPI_CALL RenderingContextDriverVulkan::_debug_report_callback(VkDebugReportFlagsEXT p_flags, VkDebugReportObjectTypeEXT p_object_type, uint64_t p_object, size_t p_location, int32_t p_message_code, const char *p_layer_prefix, const char *p_message, void *p_user_data) {652String debug_message = String("Vulkan Debug Report: object - ") + String::num_int64(p_object) + "\n" + p_message;653654switch (p_flags) {655case VK_DEBUG_REPORT_DEBUG_BIT_EXT:656case VK_DEBUG_REPORT_INFORMATION_BIT_EXT:657print_line(debug_message);658break;659case VK_DEBUG_REPORT_WARNING_BIT_EXT:660case VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT:661WARN_PRINT(debug_message);662break;663case VK_DEBUG_REPORT_ERROR_BIT_EXT:664ERR_PRINT(debug_message);665break;666}667668return VK_FALSE;669}670671Error RenderingContextDriverVulkan::_initialize_instance() {672Error err;673TightLocalVector<const char *> enabled_extension_names;674enabled_extension_names.reserve(enabled_instance_extension_names.size());675for (const CharString &extension_name : enabled_instance_extension_names) {676enabled_extension_names.push_back(extension_name.ptr());677}678679// We'll set application version to the Vulkan version we're developing against, even if our instance is based on an older Vulkan680// version, devices can still support newer versions of Vulkan. The exception is when we're on Vulkan 1.0, we should not set this681// to anything but 1.0. Note that this value is only used by validation layers to warn us about version issues.682uint32_t application_api_version = instance_api_version == VK_API_VERSION_1_0 ? VK_API_VERSION_1_0 : VK_API_VERSION_1_2;683684CharString cs = GLOBAL_GET("application/config/name").operator String().utf8();685VkApplicationInfo app_info = {};686app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;687app_info.pApplicationName = cs.get_data();688app_info.pEngineName = GODOT_VERSION_NAME;689app_info.engineVersion = VK_MAKE_VERSION(GODOT_VERSION_MAJOR, GODOT_VERSION_MINOR, GODOT_VERSION_PATCH);690app_info.apiVersion = application_api_version;691692TightLocalVector<const char *> enabled_layer_names;693if (_use_validation_layers()) {694err = _find_validation_layers(enabled_layer_names);695ERR_FAIL_COND_V(err != OK, err);696}697698VkInstanceCreateInfo instance_info = {};699instance_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;700701#if defined(USE_VOLK) && (defined(MACOS_ENABLED) || defined(IOS_ENABLED))702instance_info.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;703#endif704705instance_info.pApplicationInfo = &app_info;706instance_info.enabledExtensionCount = enabled_extension_names.size();707instance_info.ppEnabledExtensionNames = enabled_extension_names.ptr();708instance_info.enabledLayerCount = enabled_layer_names.size();709instance_info.ppEnabledLayerNames = enabled_layer_names.ptr();710711// This is info for a temp callback to use during CreateInstance. After the instance is created, we use the instance-based function to register the final callback.712VkDebugUtilsMessengerCreateInfoEXT debug_messenger_create_info = {};713VkDebugReportCallbackCreateInfoEXT debug_report_callback_create_info = {};714const bool has_debug_utils_extension = enabled_instance_extension_names.has(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);715const bool has_debug_report_extension = enabled_instance_extension_names.has(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);716if (has_debug_utils_extension) {717debug_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;718debug_messenger_create_info.pNext = nullptr;719debug_messenger_create_info.flags = 0;720debug_messenger_create_info.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;721debug_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;722debug_messenger_create_info.pfnUserCallback = _debug_messenger_callback;723debug_messenger_create_info.pUserData = this;724instance_info.pNext = &debug_messenger_create_info;725} else if (has_debug_report_extension) {726debug_report_callback_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT;727debug_report_callback_create_info.flags = VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT | VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;728debug_report_callback_create_info.pfnCallback = _debug_report_callback;729debug_report_callback_create_info.pUserData = this;730instance_info.pNext = &debug_report_callback_create_info;731}732733err = _create_vulkan_instance(&instance_info, &instance);734ERR_FAIL_COND_V(err != OK, err);735736#ifdef USE_VOLK737volkLoadInstance(instance);738#endif739740// Physical device.741if (enabled_instance_extension_names.has(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME)) {742functions.GetPhysicalDeviceFeatures2 = PFN_vkGetPhysicalDeviceFeatures2(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceFeatures2"));743functions.GetPhysicalDeviceProperties2 = PFN_vkGetPhysicalDeviceProperties2(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceProperties2"));744745// In Vulkan 1.0, the functions might be accessible under their original extension names.746if (functions.GetPhysicalDeviceFeatures2 == nullptr) {747functions.GetPhysicalDeviceFeatures2 = PFN_vkGetPhysicalDeviceFeatures2(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceFeatures2KHR"));748}749750if (functions.GetPhysicalDeviceProperties2 == nullptr) {751functions.GetPhysicalDeviceProperties2 = PFN_vkGetPhysicalDeviceProperties2(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceProperties2KHR"));752}753}754755// Device.756functions.GetDeviceProcAddr = PFN_vkGetDeviceProcAddr(vkGetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));757758// Surfaces.759functions.GetPhysicalDeviceSurfaceSupportKHR = PFN_vkGetPhysicalDeviceSurfaceSupportKHR(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceSurfaceSupportKHR"));760functions.GetPhysicalDeviceSurfaceFormatsKHR = PFN_vkGetPhysicalDeviceSurfaceFormatsKHR(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceSurfaceFormatsKHR"));761functions.GetPhysicalDeviceSurfaceCapabilitiesKHR = PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceSurfaceCapabilitiesKHR"));762functions.GetPhysicalDeviceSurfacePresentModesKHR = PFN_vkGetPhysicalDeviceSurfacePresentModesKHR(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceSurfacePresentModesKHR"));763764// Debug utils and report.765if (has_debug_utils_extension) {766// Setup VK_EXT_debug_utils function pointers always (we use them for debug labels and names).767functions.CreateDebugUtilsMessengerEXT = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");768functions.DestroyDebugUtilsMessengerEXT = (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT");769functions.CmdBeginDebugUtilsLabelEXT = (PFN_vkCmdBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(instance, "vkCmdBeginDebugUtilsLabelEXT");770functions.CmdEndDebugUtilsLabelEXT = (PFN_vkCmdEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(instance, "vkCmdEndDebugUtilsLabelEXT");771functions.SetDebugUtilsObjectNameEXT = (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(instance, "vkSetDebugUtilsObjectNameEXT");772773if (!functions.debug_util_functions_available()) {774ERR_FAIL_V_MSG(ERR_CANT_CREATE, "GetProcAddr: Failed to init VK_EXT_debug_utils\nGetProcAddr: Failure");775}776777VkResult res = functions.CreateDebugUtilsMessengerEXT(instance, &debug_messenger_create_info, get_allocation_callbacks(VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT), &debug_messenger);778switch (res) {779case VK_SUCCESS:780break;781case VK_ERROR_OUT_OF_HOST_MEMORY:782ERR_FAIL_V_MSG(ERR_CANT_CREATE, "CreateDebugUtilsMessengerEXT: out of host memory\nCreateDebugUtilsMessengerEXT Failure");783break;784default:785ERR_FAIL_V_MSG(ERR_CANT_CREATE, "CreateDebugUtilsMessengerEXT: unknown failure\nCreateDebugUtilsMessengerEXT Failure");786break;787}788} else if (has_debug_report_extension) {789functions.CreateDebugReportCallbackEXT = (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugReportCallbackEXT");790functions.DebugReportMessageEXT = (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(instance, "vkDebugReportMessageEXT");791functions.DestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(instance, "vkDestroyDebugReportCallbackEXT");792793if (!functions.debug_report_functions_available()) {794ERR_FAIL_V_MSG(ERR_CANT_CREATE, "GetProcAddr: Failed to init VK_EXT_debug_report\nGetProcAddr: Failure");795}796797VkResult res = functions.CreateDebugReportCallbackEXT(instance, &debug_report_callback_create_info, get_allocation_callbacks(VK_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT), &debug_report);798switch (res) {799case VK_SUCCESS:800break;801case VK_ERROR_OUT_OF_HOST_MEMORY:802ERR_FAIL_V_MSG(ERR_CANT_CREATE, "CreateDebugReportCallbackEXT: out of host memory\nCreateDebugReportCallbackEXT Failure");803break;804default:805ERR_FAIL_V_MSG(ERR_CANT_CREATE, "CreateDebugReportCallbackEXT: unknown failure\nCreateDebugReportCallbackEXT Failure");806break;807}808}809810return OK;811}812813Error RenderingContextDriverVulkan::_initialize_devices() {814if (VulkanHooks::get_singleton() != nullptr) {815VkPhysicalDevice physical_device;816bool device_retrieved = VulkanHooks::get_singleton()->get_physical_device(&physical_device);817ERR_FAIL_COND_V(!device_retrieved, ERR_CANT_CREATE);818819// When a hook is active, pretend the device returned by the hook is the only device available.820driver_devices.resize(1);821physical_devices.resize(1);822device_queue_families.resize(1);823physical_devices[0] = physical_device;824825} else {826uint32_t physical_device_count = 0;827VkResult err = vkEnumeratePhysicalDevices(instance, &physical_device_count, nullptr);828ERR_FAIL_COND_V(err != VK_SUCCESS, ERR_CANT_CREATE);829ERR_FAIL_COND_V_MSG(physical_device_count == 0, ERR_CANT_CREATE, "vkEnumeratePhysicalDevices reported zero accessible devices.\n\nDo you have a compatible Vulkan installable client driver (ICD) installed?\nvkEnumeratePhysicalDevices Failure.");830831driver_devices.resize(physical_device_count);832physical_devices.resize(physical_device_count);833device_queue_families.resize(physical_device_count);834err = vkEnumeratePhysicalDevices(instance, &physical_device_count, physical_devices.ptr());835ERR_FAIL_COND_V(err != VK_SUCCESS, ERR_CANT_CREATE);836}837838// Fill the list of driver devices with the properties from the physical devices.839for (uint32_t i = 0; i < physical_devices.size(); i++) {840VkPhysicalDeviceProperties props;841vkGetPhysicalDeviceProperties(physical_devices[i], &props);842843Device &driver_device = driver_devices[i];844driver_device.name = String::utf8(props.deviceName);845driver_device.vendor = props.vendorID;846driver_device.type = DeviceType(props.deviceType);847driver_device.workarounds = Workarounds();848849_check_driver_workarounds(props, driver_device);850851uint32_t queue_family_properties_count = 0;852vkGetPhysicalDeviceQueueFamilyProperties(physical_devices[i], &queue_family_properties_count, nullptr);853854if (queue_family_properties_count > 0) {855device_queue_families[i].properties.resize(queue_family_properties_count);856vkGetPhysicalDeviceQueueFamilyProperties(physical_devices[i], &queue_family_properties_count, device_queue_families[i].properties.ptr());857}858}859860return OK;861}862863void RenderingContextDriverVulkan::_check_driver_workarounds(const VkPhysicalDeviceProperties &p_device_properties, Device &r_device) {864// Workaround for the Adreno 6XX family of devices.865//866// There's a known issue with the Vulkan driver in this family of devices where it'll crash if a dynamic state for drawing is867// used in a command buffer before a dispatch call is issued. As both dynamic scissor and viewport are basic requirements for868// the engine to not bake this state into the PSO, the only known way to fix this issue is to reset the command buffer entirely.869//870// As the render graph has no built in limitations of whether it'll issue compute work before anything needs to draw on the871// frame, and there's no guarantee that compute work will never be dependent on rasterization in the future, this workaround872// will end recording on the current command buffer any time a compute list is encountered after a draw list was executed.873// A new command buffer will be created afterwards and the appropriate synchronization primitives will be inserted.874//875// Executing this workaround has the added cost of synchronization between all the command buffers that are created as well as876// all the individual submissions. This performance hit is accepted for the sake of being able to support these devices without877// limiting the design of the renderer.878//879// This bug was fixed in driver version 512.503.0, so we only enabled it on devices older than this.880//881r_device.workarounds.avoid_compute_after_draw =882r_device.vendor == Vendor::VENDOR_QUALCOMM &&883p_device_properties.deviceID >= 0x6000000 && // Adreno 6xx884p_device_properties.driverVersion < VK_MAKE_VERSION(512, 503, 0) &&885r_device.name.find("Turnip") < 0;886}887888bool RenderingContextDriverVulkan::_use_validation_layers() const {889return Engine::get_singleton()->is_validation_layers_enabled();890}891892Error RenderingContextDriverVulkan::_create_vulkan_instance(const VkInstanceCreateInfo *p_create_info, VkInstance *r_instance) {893if (VulkanHooks::get_singleton() != nullptr) {894return VulkanHooks::get_singleton()->create_vulkan_instance(p_create_info, r_instance) ? OK : ERR_CANT_CREATE;895} else {896VkResult err = vkCreateInstance(p_create_info, get_allocation_callbacks(VK_OBJECT_TYPE_INSTANCE), r_instance);897ERR_FAIL_COND_V_MSG(err == VK_ERROR_INCOMPATIBLE_DRIVER, ERR_CANT_CREATE,898"Cannot find a compatible Vulkan installable client driver (ICD).\n\n"899"vkCreateInstance Failure");900ERR_FAIL_COND_V_MSG(err == VK_ERROR_EXTENSION_NOT_PRESENT, ERR_CANT_CREATE,901"Cannot find a specified extension library.\n"902"Make sure your layers path is set appropriately.\n"903"vkCreateInstance Failure");904ERR_FAIL_COND_V_MSG(err, ERR_CANT_CREATE,905"vkCreateInstance failed.\n\n"906"Do you have a compatible Vulkan installable client driver (ICD) installed?\n"907"Please look at the Getting Started guide for additional information.\n"908"vkCreateInstance Failure");909}910911return OK;912}913914Error RenderingContextDriverVulkan::initialize() {915Error err;916917#ifdef USE_VOLK918if (volkInitialize() != VK_SUCCESS) {919return FAILED;920}921#endif922923err = _initialize_vulkan_version();924ERR_FAIL_COND_V(err != OK, err);925926err = _initialize_instance_extensions();927ERR_FAIL_COND_V(err != OK, err);928929err = _initialize_instance();930ERR_FAIL_COND_V(err != OK, err);931932err = _initialize_devices();933ERR_FAIL_COND_V(err != OK, err);934935return OK;936}937938const RenderingContextDriver::Device &RenderingContextDriverVulkan::device_get(uint32_t p_device_index) const {939DEV_ASSERT(p_device_index < driver_devices.size());940return driver_devices[p_device_index];941}942943uint32_t RenderingContextDriverVulkan::device_get_count() const {944return driver_devices.size();945}946947bool RenderingContextDriverVulkan::device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const {948DEV_ASSERT(p_device_index < physical_devices.size());949950// Check if any of the queues supported by the device supports presenting to the window's surface.951const VkPhysicalDevice physical_device = physical_devices[p_device_index];952const DeviceQueueFamilies &queue_families = device_queue_families[p_device_index];953for (uint32_t i = 0; i < queue_families.properties.size(); i++) {954if ((queue_families.properties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) && queue_family_supports_present(physical_device, i, p_surface)) {955return true;956}957}958959return false;960}961962RenderingDeviceDriver *RenderingContextDriverVulkan::driver_create() {963return memnew(RenderingDeviceDriverVulkan(this));964}965966void RenderingContextDriverVulkan::driver_free(RenderingDeviceDriver *p_driver) {967memdelete(p_driver);968}969970RenderingContextDriver::SurfaceID RenderingContextDriverVulkan::surface_create(const void *p_platform_data) {971DEV_ASSERT(false && "Surface creation should not be called on the platform-agnostic version of the driver.");972return SurfaceID();973}974975void RenderingContextDriverVulkan::surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) {976Surface *surface = (Surface *)(p_surface);977surface->width = p_width;978surface->height = p_height;979surface->needs_resize = true;980}981982void RenderingContextDriverVulkan::surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) {983Surface *surface = (Surface *)(p_surface);984surface->vsync_mode = p_vsync_mode;985surface->needs_resize = true;986}987988DisplayServer::VSyncMode RenderingContextDriverVulkan::surface_get_vsync_mode(SurfaceID p_surface) const {989Surface *surface = (Surface *)(p_surface);990return surface->vsync_mode;991}992993uint32_t RenderingContextDriverVulkan::surface_get_width(SurfaceID p_surface) const {994Surface *surface = (Surface *)(p_surface);995return surface->width;996}997998uint32_t RenderingContextDriverVulkan::surface_get_height(SurfaceID p_surface) const {999Surface *surface = (Surface *)(p_surface);1000return surface->height;1001}10021003void RenderingContextDriverVulkan::surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) {1004Surface *surface = (Surface *)(p_surface);1005surface->needs_resize = p_needs_resize;1006}10071008bool RenderingContextDriverVulkan::surface_get_needs_resize(SurfaceID p_surface) const {1009Surface *surface = (Surface *)(p_surface);1010return surface->needs_resize;1011}10121013void RenderingContextDriverVulkan::surface_destroy(SurfaceID p_surface) {1014Surface *surface = (Surface *)(p_surface);1015vkDestroySurfaceKHR(instance, surface->vk_surface, get_allocation_callbacks(VK_OBJECT_TYPE_SURFACE_KHR));1016memdelete(surface);1017}10181019bool RenderingContextDriverVulkan::is_debug_utils_enabled() const {1020return enabled_instance_extension_names.has(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);1021}10221023VkInstance RenderingContextDriverVulkan::instance_get() const {1024return instance;1025}10261027VkPhysicalDevice RenderingContextDriverVulkan::physical_device_get(uint32_t p_device_index) const {1028DEV_ASSERT(p_device_index < physical_devices.size());1029return physical_devices[p_device_index];1030}10311032uint32_t RenderingContextDriverVulkan::queue_family_get_count(uint32_t p_device_index) const {1033DEV_ASSERT(p_device_index < physical_devices.size());1034return device_queue_families[p_device_index].properties.size();1035}10361037VkQueueFamilyProperties RenderingContextDriverVulkan::queue_family_get(uint32_t p_device_index, uint32_t p_queue_family_index) const {1038DEV_ASSERT(p_device_index < physical_devices.size());1039DEV_ASSERT(p_queue_family_index < queue_family_get_count(p_device_index));1040return device_queue_families[p_device_index].properties[p_queue_family_index];1041}10421043bool RenderingContextDriverVulkan::queue_family_supports_present(VkPhysicalDevice p_physical_device, uint32_t p_queue_family_index, SurfaceID p_surface) const {1044DEV_ASSERT(p_physical_device != VK_NULL_HANDLE);1045DEV_ASSERT(p_surface != 0);1046Surface *surface = (Surface *)(p_surface);1047VkBool32 present_supported = false;1048VkResult err = vkGetPhysicalDeviceSurfaceSupportKHR(p_physical_device, p_queue_family_index, surface->vk_surface, &present_supported);1049return err == VK_SUCCESS && present_supported;1050}10511052const RenderingContextDriverVulkan::Functions &RenderingContextDriverVulkan::functions_get() const {1053return functions;1054}10551056#endif // VULKAN_ENABLED105710581059