| 1 | /**************************************************************************/ |
| 2 | /* rendering_context_driver_vulkan.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #ifdef VULKAN_ENABLED |
| 32 | |
| 33 | #include "rendering_context_driver_vulkan.h" |
| 34 | |
| 35 | #include "vk_enum_string_helper.h" |
| 36 | |
| 37 | #include "core/config/project_settings.h" |
| 38 | #include "core/version.h" |
| 39 | |
| 40 | #include "rendering_device_driver_vulkan.h" |
| 41 | #include "vulkan_hooks.h" |
| 42 | |
| 43 | #if defined(VK_TRACK_DRIVER_MEMORY) |
| 44 | /*************************************************/ |
| 45 | // Driver memory tracking |
| 46 | /*************************************************/ |
| 47 | // Total driver memory and allocation amount. |
| 48 | SafeNumeric<size_t> driver_memory_total_memory; |
| 49 | SafeNumeric<size_t> driver_memory_total_alloc_count; |
| 50 | // Amount of driver memory for every object type. |
| 51 | SafeNumeric<size_t> driver_memory_tracker[RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_COUNT][RenderingContextDriverVulkan::VK_TRACKED_SYSTEM_ALLOCATION_SCOPE_COUNT]; |
| 52 | // Amount of allocations for every object type. |
| 53 | SafeNumeric<uint32_t> driver_memory_allocation_count[RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_COUNT][RenderingContextDriverVulkan::VK_TRACKED_SYSTEM_ALLOCATION_SCOPE_COUNT]; |
| 54 | #endif |
| 55 | |
| 56 | #if defined(VK_TRACK_DEVICE_MEMORY) |
| 57 | /*************************************************/ |
| 58 | // Device memory report |
| 59 | /*************************************************/ |
| 60 | // Total device memory and allocation amount. |
| 61 | HashMap<uint64_t, size_t> memory_report_table; |
| 62 | // Total memory and allocation amount. |
| 63 | SafeNumeric<uint64_t> memory_report_total_memory; |
| 64 | SafeNumeric<uint64_t> memory_report_total_alloc_count; |
| 65 | // Amount of device memory for every object type. |
| 66 | SafeNumeric<size_t> memory_report_mem_usage[RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_COUNT]; |
| 67 | // Amount of device memory allocations for every object type. |
| 68 | SafeNumeric<size_t> memory_report_allocation_count[RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_COUNT]; |
| 69 | #endif |
| 70 | |
| 71 | const char *RenderingContextDriverVulkan::get_tracked_object_name(uint32_t p_type_index) const { |
| 72 | #if defined(VK_TRACK_DRIVER_MEMORY) || defined(VK_TRACK_DEVICE_MEMORY) |
| 73 | static constexpr const char *vkTrackedObjectTypeNames[] = { "UNKNOWN", |
| 74 | "INSTANCE", |
| 75 | "PHYSICAL_DEVICE", |
| 76 | "DEVICE", |
| 77 | "QUEUE", |
| 78 | "SEMAPHORE", |
| 79 | "COMMAND_BUFFER", |
| 80 | "FENCE", |
| 81 | "DEVICE_MEMORY", |
| 82 | "BUFFER", |
| 83 | "IMAGE", |
| 84 | "EVENT", |
| 85 | "QUERY_POOL", |
| 86 | "BUFFER_VIEW", |
| 87 | "IMAGE_VIEW", |
| 88 | "SHADER_MODULE", |
| 89 | "PIPELINE_CACHE", |
| 90 | "PIPELINE_LAYOUT", |
| 91 | "RENDER_PASS", |
| 92 | "PIPELINE", |
| 93 | "DESCRIPTOR_SET_LAYOUT", |
| 94 | "SAMPLER", |
| 95 | "DESCRIPTOR_POOL", |
| 96 | "DESCRIPTOR_SET", |
| 97 | "FRAMEBUFFER", |
| 98 | "COMMAND_POOL", |
| 99 | "DESCRIPTOR_UPDATE_TEMPLATE_KHR", |
| 100 | "SURFACE_KHR", |
| 101 | "SWAPCHAIN_KHR", |
| 102 | "DEBUG_UTILS_MESSENGER_EXT", |
| 103 | "DEBUG_REPORT_CALLBACK_EXT", |
| 104 | "ACCELERATION_STRUCTURE", |
| 105 | "VMA_BUFFER_OR_IMAGE" }; |
| 106 | |
| 107 | return vkTrackedObjectTypeNames[p_type_index]; |
| 108 | #else |
| 109 | return "VK_TRACK_*_MEMORY disabled at build time"; |
| 110 | #endif |
| 111 | } |
| 112 | |
| 113 | #if defined(VK_TRACK_DRIVER_MEMORY) || defined(VK_TRACK_DEVICE_MEMORY) |
| 114 | uint64_t RenderingContextDriverVulkan::get_tracked_object_type_count() const { |
| 115 | return VK_TRACKED_OBJECT_TYPE_COUNT; |
| 116 | } |
| 117 | #endif |
| 118 | |
| 119 | #if defined(VK_TRACK_DRIVER_MEMORY) || defined(VK_TRACK_DEVICE_MEMORY) |
| 120 | RenderingContextDriverVulkan::VkTrackedObjectType vk_object_to_tracked_object(VkObjectType p_type) { |
| 121 | if (p_type > VK_OBJECT_TYPE_COMMAND_POOL && p_type != (VkObjectType)RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_VMA) { |
| 122 | switch (p_type) { |
| 123 | case VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE: |
| 124 | return RenderingContextDriverVulkan::VK_TRACKED_OBJECT_DESCRIPTOR_UPDATE_TEMPLATE_KHR; |
| 125 | case VK_OBJECT_TYPE_SURFACE_KHR: |
| 126 | return RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_SURFACE; |
| 127 | case VK_OBJECT_TYPE_SWAPCHAIN_KHR: |
| 128 | return RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_SWAPCHAIN; |
| 129 | case VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT: |
| 130 | return RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT; |
| 131 | case VK_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT: |
| 132 | return RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT; |
| 133 | case VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_KHR: |
| 134 | case VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_NV: |
| 135 | return RenderingContextDriverVulkan::VK_TRACKED_OBJECT_TYPE_ACCELERATION_STRUCTURE; |
| 136 | default: |
| 137 | _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Unknown VkObjectType enum value " + itos((uint32_t)p_type) + ".Please add it to VkTrackedObjectType, switch statement in " |
| 138 | "vk_object_to_tracked_object and get_tracked_object_name.", |
| 139 | (int)p_type); |
| 140 | return (RenderingContextDriverVulkan::VkTrackedObjectType)VK_OBJECT_TYPE_UNKNOWN; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return (RenderingContextDriverVulkan::VkTrackedObjectType)p_type; |
| 145 | } |
| 146 | #endif |
| 147 | |
| 148 | #if defined(VK_TRACK_DEVICE_MEMORY) |
| 149 | uint64_t RenderingContextDriverVulkan::get_device_total_memory() const { |
| 150 | return memory_report_total_memory.get(); |
| 151 | } |
| 152 | |
| 153 | uint64_t RenderingContextDriverVulkan::get_device_allocation_count() const { |
| 154 | return memory_report_total_alloc_count.get(); |
| 155 | } |
| 156 | |
| 157 | uint64_t RenderingContextDriverVulkan::get_device_memory_by_object_type(uint32_t p_type) const { |
| 158 | return memory_report_mem_usage[p_type].get(); |
| 159 | } |
| 160 | |
| 161 | uint64_t RenderingContextDriverVulkan::get_device_allocs_by_object_type(uint32_t p_type) const { |
| 162 | return memory_report_allocation_count[p_type].get(); |
| 163 | } |
| 164 | #endif |
| 165 | |
| 166 | #if defined(VK_TRACK_DRIVER_MEMORY) |
| 167 | uint64_t RenderingContextDriverVulkan::get_driver_total_memory() const { |
| 168 | return driver_memory_total_memory.get(); |
| 169 | } |
| 170 | |
| 171 | uint64_t RenderingContextDriverVulkan::get_driver_allocation_count() const { |
| 172 | return driver_memory_total_alloc_count.get(); |
| 173 | } |
| 174 | |
| 175 | uint64_t RenderingContextDriverVulkan::get_driver_memory_by_object_type(uint32_t p_type) const { |
| 176 | uint64_t ret = 0; |
| 177 | for (uint32_t i = 0; i < VK_TRACKED_SYSTEM_ALLOCATION_SCOPE_COUNT; i++) { |
| 178 | ret += driver_memory_tracker[p_type][i].get(); |
| 179 | } |
| 180 | |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | uint64_t RenderingContextDriverVulkan::get_driver_allocs_by_object_type(uint32_t p_type) const { |
| 185 | uint64_t ret = 0; |
| 186 | for (uint32_t i = 0; i < VK_TRACKED_SYSTEM_ALLOCATION_SCOPE_COUNT; i++) { |
| 187 | ret += driver_memory_allocation_count[p_type][i].get(); |
| 188 | } |
| 189 | |
| 190 | return ret; |
| 191 | } |
| 192 | #endif |
| 193 | |
| 194 | #if defined(VK_TRACK_DEVICE_MEMORY) |
| 195 | void RenderingContextDriverVulkan::memory_report_callback(const VkDeviceMemoryReportCallbackDataEXT *p_callback_data, void *p_user_data) { |
| 196 | if (!p_callback_data) { |
| 197 | return; |
| 198 | } |
| 199 | const RenderingContextDriverVulkan::VkTrackedObjectType obj_type = vk_object_to_tracked_object(p_callback_data->objectType); |
| 200 | uint64_t obj_id = p_callback_data->memoryObjectId; |
| 201 | |
| 202 | if (p_callback_data->type == VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_ALLOCATE_EXT) { |
| 203 | // Realloc, update size |
| 204 | if (memory_report_table.has(obj_id)) { |
| 205 | memory_report_total_memory.sub(memory_report_table[obj_id]); |
| 206 | memory_report_mem_usage[obj_type].sub(memory_report_table[obj_id]); |
| 207 | |
| 208 | memory_report_total_memory.add(p_callback_data->size); |
| 209 | memory_report_mem_usage[obj_type].add(p_callback_data->size); |
| 210 | |
| 211 | memory_report_table[p_callback_data->memoryObjectId] = p_callback_data->size; |
| 212 | } else { |
| 213 | memory_report_table[obj_id] = p_callback_data->size; |
| 214 | |
| 215 | memory_report_total_alloc_count.increment(); |
| 216 | memory_report_allocation_count[obj_type].increment(); |
| 217 | memory_report_mem_usage[obj_type].add(p_callback_data->size); |
| 218 | memory_report_total_memory.add(p_callback_data->size); |
| 219 | } |
| 220 | } else if (p_callback_data->type == VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_FREE_EXT) { |
| 221 | if (memory_report_table.has(obj_id)) { |
| 222 | memory_report_total_alloc_count.decrement(); |
| 223 | memory_report_allocation_count[obj_type].decrement(); |
| 224 | memory_report_mem_usage[obj_type].sub(p_callback_data->size); |
| 225 | memory_report_total_memory.sub(p_callback_data->size); |
| 226 | |
| 227 | memory_report_table.remove(memory_report_table.find(obj_id)); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | #endif |
| 232 | |
| 233 | VkAllocationCallbacks *RenderingContextDriverVulkan::get_allocation_callbacks(VkObjectType p_type) { |
| 234 | #if !defined(VK_TRACK_DRIVER_MEMORY) |
| 235 | return nullptr; |
| 236 | #else |
| 237 | if (!Engine::get_singleton()->is_extra_gpu_memory_tracking_enabled()) { |
| 238 | return nullptr; |
| 239 | } |
| 240 | |
| 241 | #ifdef _MSC_VER |
| 242 | #define LAMBDA_VK_CALL_CONV |
| 243 | #else |
| 244 | #define LAMBDA_VK_CALL_CONV VKAPI_PTR |
| 245 | #endif |
| 246 | |
| 247 | struct TrackedMemHeader { |
| 248 | size_t size; |
| 249 | VkSystemAllocationScope allocation_scope; |
| 250 | VkTrackedObjectType type; |
| 251 | }; |
| 252 | VkAllocationCallbacks tracking_callbacks = { |
| 253 | // Allocation function |
| 254 | nullptr, |
| 255 | []( |
| 256 | void *p_user_data, |
| 257 | size_t size, |
| 258 | size_t alignment, |
| 259 | VkSystemAllocationScope allocation_scope) LAMBDA_VK_CALL_CONV -> void * { |
| 260 | static constexpr size_t tracking_data_size = 32; |
| 261 | VkTrackedObjectType type = static_cast<VkTrackedObjectType>(*reinterpret_cast<VkTrackedObjectType *>(p_user_data)); |
| 262 | |
| 263 | driver_memory_total_memory.add(size); |
| 264 | driver_memory_total_alloc_count.increment(); |
| 265 | driver_memory_tracker[type][allocation_scope].add(size); |
| 266 | driver_memory_allocation_count[type][allocation_scope].increment(); |
| 267 | |
| 268 | alignment = MAX(alignment, tracking_data_size); |
| 269 | |
| 270 | uint8_t *ret = reinterpret_cast<uint8_t *>(Memory::alloc_aligned_static(size + alignment, alignment)); |
| 271 | if (ret == nullptr) { |
| 272 | return nullptr; |
| 273 | } |
| 274 | |
| 275 | // Track allocation |
| 276 | TrackedMemHeader *header = reinterpret_cast<TrackedMemHeader *>(ret); |
| 277 | header->size = size; |
| 278 | header->allocation_scope = allocation_scope; |
| 279 | header->type = type; |
| 280 | *reinterpret_cast<size_t *>(ret + alignment - sizeof(size_t)) = alignment; |
| 281 | |
| 282 | // Return first available chunk of memory |
| 283 | return ret + alignment; |
| 284 | }, |
| 285 | |
| 286 | // Reallocation function |
| 287 | []( |
| 288 | void *p_user_data, |
| 289 | void *p_original, |
| 290 | size_t size, |
| 291 | size_t alignment, |
| 292 | VkSystemAllocationScope allocation_scope) LAMBDA_VK_CALL_CONV -> void * { |
| 293 | if (p_original == nullptr) { |
| 294 | VkObjectType type = static_cast<VkObjectType>(*reinterpret_cast<uint32_t *>(p_user_data)); |
| 295 | return get_allocation_callbacks(type)->pfnAllocation(p_user_data, size, alignment, allocation_scope); |
| 296 | } |
| 297 | |
| 298 | uint8_t *mem = reinterpret_cast<uint8_t *>(p_original); |
| 299 | // Retrieve alignment |
| 300 | alignment = *reinterpret_cast<size_t *>(mem - sizeof(size_t)); |
| 301 | // Retrieve allocation data |
| 302 | TrackedMemHeader *header = reinterpret_cast<TrackedMemHeader *>(mem - alignment); |
| 303 | |
| 304 | // Update allocation size |
| 305 | driver_memory_total_memory.sub(header->size); |
| 306 | driver_memory_total_memory.add(size); |
| 307 | driver_memory_tracker[header->type][header->allocation_scope].sub(header->size); |
| 308 | driver_memory_tracker[header->type][header->allocation_scope].add(size); |
| 309 | |
| 310 | uint8_t *ret = reinterpret_cast<uint8_t *>(Memory::realloc_aligned_static(header, size + alignment, header->size + alignment, alignment)); |
| 311 | if (ret == nullptr) { |
| 312 | return nullptr; |
| 313 | } |
| 314 | // Update tracker |
| 315 | header = reinterpret_cast<TrackedMemHeader *>(ret); |
| 316 | header->size = size; |
| 317 | return ret + alignment; |
| 318 | }, |
| 319 | |
| 320 | // Free function |
| 321 | []( |
| 322 | void *p_user_data, |
| 323 | void *p_memory) LAMBDA_VK_CALL_CONV { |
| 324 | if (!p_memory) { |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | uint8_t *mem = reinterpret_cast<uint8_t *>(p_memory); |
| 329 | size_t alignment = *reinterpret_cast<size_t *>(mem - sizeof(size_t)); |
| 330 | TrackedMemHeader *header = reinterpret_cast<TrackedMemHeader *>(mem - alignment); |
| 331 | |
| 332 | driver_memory_total_alloc_count.decrement(); |
| 333 | driver_memory_total_memory.sub(header->size); |
| 334 | driver_memory_tracker[header->type][header->allocation_scope].sub(header->size); |
| 335 | driver_memory_allocation_count[header->type][header->allocation_scope].decrement(); |
| 336 | |
| 337 | Memory::free_aligned_static(header); |
| 338 | }, |
| 339 | // Internal allocation / deallocation. We don't track them as they cannot really be controlled or optimized by the programmer. |
| 340 | []( |
| 341 | void *p_user_data, |
| 342 | size_t size, |
| 343 | VkInternalAllocationType allocation_type, |
| 344 | VkSystemAllocationScope allocation_scope) LAMBDA_VK_CALL_CONV { |
| 345 | }, |
| 346 | []( |
| 347 | void *p_user_data, |
| 348 | size_t size, |
| 349 | VkInternalAllocationType allocation_type, |
| 350 | VkSystemAllocationScope allocation_scope) LAMBDA_VK_CALL_CONV { |
| 351 | }, |
| 352 | }; |
| 353 | |
| 354 | // Create a callback per object type |
| 355 | static VkAllocationCallbacks object_callbacks[VK_TRACKED_OBJECT_TYPE_COUNT] = {}; |
| 356 | static uint32_t object_user_data[VK_TRACKED_OBJECT_TYPE_COUNT] = {}; |
| 357 | |
| 358 | // Only build the first time |
| 359 | if (!object_callbacks[0].pfnAllocation) { |
| 360 | for (uint32_t c = 0; c < VK_TRACKED_OBJECT_TYPE_COUNT; ++c) { |
| 361 | object_callbacks[c] = tracking_callbacks; |
| 362 | object_user_data[c] = c; |
| 363 | object_callbacks[c].pUserData = &object_user_data[c]; |
| 364 | |
| 365 | for (uint32_t i = 0; i < VK_TRACKED_SYSTEM_ALLOCATION_SCOPE_COUNT; i++) { |
| 366 | driver_memory_tracker[c][i].set(0); |
| 367 | driver_memory_allocation_count[c][i].set(0); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | uint32_t type_index = vk_object_to_tracked_object(p_type); |
| 373 | return &object_callbacks[type_index]; |
| 374 | #endif |
| 375 | } |
| 376 | |
| 377 | RenderingContextDriverVulkan::RenderingContextDriverVulkan() { |
| 378 | // Empty constructor. |
| 379 | } |
| 380 | |
| 381 | RenderingContextDriverVulkan::~RenderingContextDriverVulkan() { |
| 382 | if (debug_messenger != VK_NULL_HANDLE && functions.DestroyDebugUtilsMessengerEXT != nullptr) { |
| 383 | functions.DestroyDebugUtilsMessengerEXT(instance, debug_messenger, get_allocation_callbacks(VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT)); |
| 384 | } |
| 385 | |
| 386 | if (debug_report != VK_NULL_HANDLE && functions.DestroyDebugReportCallbackEXT != nullptr) { |
| 387 | functions.DestroyDebugReportCallbackEXT(instance, debug_report, get_allocation_callbacks(VK_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT)); |
| 388 | } |
| 389 | |
| 390 | if (instance != VK_NULL_HANDLE) { |
| 391 | vkDestroyInstance(instance, get_allocation_callbacks(VK_OBJECT_TYPE_INSTANCE)); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | Error RenderingContextDriverVulkan::_initialize_vulkan_version() { |
| 396 | // https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VkApplicationInfo.html#_description |
| 397 | // For Vulkan 1.0 vkEnumerateInstanceVersion is not available, including not in the loader we compile against on Android. |
| 398 | typedef VkResult(VKAPI_PTR * _vkEnumerateInstanceVersion)(uint32_t *); |
| 399 | _vkEnumerateInstanceVersion func = (_vkEnumerateInstanceVersion)vkGetInstanceProcAddr(nullptr, "vkEnumerateInstanceVersion"); |
| 400 | if (func != nullptr) { |
| 401 | uint32_t api_version; |
| 402 | VkResult res = func(&api_version); |
| 403 | if (res == VK_SUCCESS) { |
| 404 | instance_api_version = api_version; |
| 405 | } else { |
| 406 | // According to the documentation this shouldn't fail with anything except a memory allocation error |
| 407 | // in which case we're in deep trouble anyway. |
| 408 | ERR_FAIL_V(ERR_CANT_CREATE); |
| 409 | } |
| 410 | } else { |
| 411 | print_line("vkEnumerateInstanceVersion not available, assuming Vulkan 1.0."); |
| 412 | instance_api_version = VK_API_VERSION_1_0; |
| 413 | } |
| 414 | |
| 415 | return OK; |
| 416 | } |
| 417 | |
| 418 | void RenderingContextDriverVulkan::_register_requested_instance_extension(const CharString &p_extension_name, bool p_required) { |
| 419 | ERR_FAIL_COND(requested_instance_extensions.has(p_extension_name)); |
| 420 | requested_instance_extensions[p_extension_name] = p_required; |
| 421 | } |
| 422 | |
| 423 | Error RenderingContextDriverVulkan::_initialize_instance_extensions() { |
| 424 | enabled_instance_extension_names.clear(); |
| 425 | |
| 426 | // The surface extension and the platform-specific surface extension are core requirements. |
| 427 | _register_requested_instance_extension(VK_KHR_SURFACE_EXTENSION_NAME, true); |
| 428 | if (_get_platform_surface_extension()) { |
| 429 | _register_requested_instance_extension(_get_platform_surface_extension(), true); |
| 430 | } |
| 431 | |
| 432 | if (_use_validation_layers()) { |
| 433 | _register_requested_instance_extension(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, false); |
| 434 | } |
| 435 | |
| 436 | // This extension allows us to use the properties2 features to query additional device capabilities. |
| 437 | _register_requested_instance_extension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, false); |
| 438 | |
| 439 | #if defined(USE_VOLK) && (defined(MACOS_ENABLED) || defined(IOS_ENABLED)) |
| 440 | _register_requested_instance_extension(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME, true); |
| 441 | #endif |
| 442 | |
| 443 | // Only enable debug utils in verbose mode or DEV_ENABLED. |
| 444 | // End users would get spammed with messages of varying verbosity due to the |
| 445 | // mess that thirdparty layers/extensions and drivers seem to leave in their |
| 446 | // wake, making the Windows registry a bottomless pit of broken layer JSON. |
| 447 | #ifdef DEV_ENABLED |
| 448 | bool want_debug_utils = true; |
| 449 | #else |
| 450 | bool want_debug_utils = OS::get_singleton()->is_stdout_verbose(); |
| 451 | #endif |
| 452 | if (want_debug_utils) { |
| 453 | _register_requested_instance_extension(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, false); |
| 454 | } |
| 455 | |
| 456 | // Load instance extensions that are available. |
| 457 | uint32_t instance_extension_count = 0; |
| 458 | VkResult err = vkEnumerateInstanceExtensionProperties(nullptr, &instance_extension_count, nullptr); |
| 459 | ERR_FAIL_COND_V(err != VK_SUCCESS && err != VK_INCOMPLETE, ERR_CANT_CREATE); |
| 460 | ERR_FAIL_COND_V_MSG(instance_extension_count == 0, ERR_CANT_CREATE, "No instance extensions were found."); |
| 461 | |
| 462 | TightLocalVector<VkExtensionProperties> instance_extensions; |
| 463 | instance_extensions.resize(instance_extension_count); |
| 464 | err = vkEnumerateInstanceExtensionProperties(nullptr, &instance_extension_count, instance_extensions.ptr()); |
| 465 | if (err != VK_SUCCESS && err != VK_INCOMPLETE) { |
| 466 | ERR_FAIL_V(ERR_CANT_CREATE); |
| 467 | } |
| 468 | |
| 469 | #ifdef DEV_ENABLED |
| 470 | for (uint32_t i = 0; i < instance_extension_count; i++) { |
| 471 | print_verbose(String("VULKAN: Found instance extension ") + String::utf8(instance_extensions[i].extensionName) + String(".")); |
| 472 | } |
| 473 | #endif |
| 474 | |
| 475 | // Enable all extensions that are supported and requested. |
| 476 | for (uint32_t i = 0; i < instance_extension_count; i++) { |
| 477 | CharString extension_name(instance_extensions[i].extensionName); |
| 478 | if (requested_instance_extensions.has(extension_name)) { |
| 479 | enabled_instance_extension_names.insert(extension_name); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | // Now check our requested extensions. |
| 484 | for (KeyValue<CharString, bool> &requested_extension : requested_instance_extensions) { |
| 485 | if (!enabled_instance_extension_names.has(requested_extension.key)) { |
| 486 | if (requested_extension.value) { |
| 487 | ERR_FAIL_V_MSG(ERR_BUG, String("Required extension ") + String::utf8(requested_extension.key) + String(" not found.")); |
| 488 | } else { |
| 489 | print_verbose(String("Optional extension ") + String::utf8(requested_extension.key) + String(" not found.")); |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | return OK; |
| 495 | } |
| 496 | |
| 497 | Error RenderingContextDriverVulkan::_find_validation_layers(TightLocalVector<const char *> &r_layer_names) const { |
| 498 | r_layer_names.clear(); |
| 499 | |
| 500 | uint32_t instance_layer_count = 0; |
| 501 | VkResult err = vkEnumerateInstanceLayerProperties(&instance_layer_count, nullptr); |
| 502 | ERR_FAIL_COND_V(err != VK_SUCCESS, ERR_CANT_CREATE); |
| 503 | if (instance_layer_count > 0) { |
| 504 | TightLocalVector<VkLayerProperties> layer_properties; |
| 505 | layer_properties.resize(instance_layer_count); |
| 506 | err = vkEnumerateInstanceLayerProperties(&instance_layer_count, layer_properties.ptr()); |
| 507 | ERR_FAIL_COND_V(err != VK_SUCCESS, ERR_CANT_CREATE); |
| 508 | |
| 509 | // Preferred set of validation layers. |
| 510 | const std::initializer_list<const char *> preferred = { "VK_LAYER_KHRONOS_validation" }; |
| 511 | |
| 512 | // Alternative (deprecated, removed in SDK 1.1.126.0) set of validation layers. |
| 513 | const std::initializer_list<const char *> lunarg = { "VK_LAYER_LUNARG_standard_validation" }; |
| 514 | |
| 515 | // Alternative (deprecated, removed in SDK 1.1.121.1) set of validation layers. |
| 516 | const 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" }; |
| 517 | |
| 518 | // Verify all the layers of the list are present. |
| 519 | for (const std::initializer_list<const char *> &list : { preferred, lunarg, google }) { |
| 520 | bool layers_found = false; |
| 521 | for (const char *layer_name : list) { |
| 522 | layers_found = false; |
| 523 | |
| 524 | for (const VkLayerProperties &properties : layer_properties) { |
| 525 | if (!strcmp(properties.layerName, layer_name)) { |
| 526 | layers_found = true; |
| 527 | break; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | if (!layers_found) { |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | if (layers_found) { |
| 537 | r_layer_names.reserve(list.size()); |
| 538 | for (const char *layer_name : list) { |
| 539 | r_layer_names.push_back(layer_name); |
| 540 | } |
| 541 | |
| 542 | break; |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | return OK; |
| 548 | } |
| 549 | |
| 550 | VKAPI_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) { |
| 551 | // This error needs to be ignored because the AMD allocator will mix up memory types on IGP processors. |
| 552 | if (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) { |
| 553 | return VK_FALSE; |
| 554 | } |
| 555 | // This needs to be ignored because Validator is wrong here. |
| 556 | if (strstr(p_callback_data->pMessage, "Invalid SPIR-V binary version 1.3") != nullptr) { |
| 557 | return VK_FALSE; |
| 558 | } |
| 559 | // This needs to be ignored because Validator is wrong here. |
| 560 | if (strstr(p_callback_data->pMessage, "Shader requires flag") != nullptr) { |
| 561 | return VK_FALSE; |
| 562 | } |
| 563 | |
| 564 | // This needs to be ignored because Validator is wrong here. |
| 565 | if (strstr(p_callback_data->pMessage, "SPIR-V module not valid: Pointer operand") != nullptr && strstr(p_callback_data->pMessage, "must be a memory object") != nullptr) { |
| 566 | return VK_FALSE; |
| 567 | } |
| 568 | |
| 569 | if (p_callback_data->pMessageIdName && strstr(p_callback_data->pMessageIdName, "UNASSIGNED-CoreValidation-DrawState-ClearCmdBeforeDraw") != nullptr) { |
| 570 | return VK_FALSE; |
| 571 | } |
| 572 | |
| 573 | String type_string; |
| 574 | switch (p_message_type) { |
| 575 | case (VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT): |
| 576 | type_string = "GENERAL"; |
| 577 | break; |
| 578 | case (VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT): |
| 579 | type_string = "VALIDATION"; |
| 580 | break; |
| 581 | case (VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT): |
| 582 | type_string = "PERFORMANCE"; |
| 583 | break; |
| 584 | case (VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT): |
| 585 | type_string = "VALIDATION|PERFORMANCE"; |
| 586 | break; |
| 587 | } |
| 588 | |
| 589 | String objects_string; |
| 590 | if (p_callback_data->objectCount > 0) { |
| 591 | objects_string = "\n\tObjects - " + String::num_int64(p_callback_data->objectCount); |
| 592 | for (uint32_t object = 0; object < p_callback_data->objectCount; ++object) { |
| 593 | objects_string += |
| 594 | "\n\t\tObject[" + String::num_int64(object) + "]" + |
| 595 | " - " + string_VkObjectType(p_callback_data->pObjects[object].objectType) + |
| 596 | ", Handle " + String::num_int64(p_callback_data->pObjects[object].objectHandle); |
| 597 | |
| 598 | if (p_callback_data->pObjects[object].pObjectName != nullptr && strlen(p_callback_data->pObjects[object].pObjectName) > 0) { |
| 599 | objects_string += ", Name \"" + String(p_callback_data->pObjects[object].pObjectName) + "\""; |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | String labels_string; |
| 605 | if (p_callback_data->cmdBufLabelCount > 0) { |
| 606 | labels_string = "\n\tCommand Buffer Labels - " + String::num_int64(p_callback_data->cmdBufLabelCount); |
| 607 | for (uint32_t cmd_buf_label = 0; cmd_buf_label < p_callback_data->cmdBufLabelCount; ++cmd_buf_label) { |
| 608 | labels_string += |
| 609 | "\n\t\tLabel[" + String::num_int64(cmd_buf_label) + "]" + |
| 610 | " - " + p_callback_data->pCmdBufLabels[cmd_buf_label].pLabelName + |
| 611 | "{ "; |
| 612 | |
| 613 | for (int color_idx = 0; color_idx < 4; ++color_idx) { |
| 614 | labels_string += String::num(p_callback_data->pCmdBufLabels[cmd_buf_label].color[color_idx]); |
| 615 | if (color_idx < 3) { |
| 616 | labels_string += ", "; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | labels_string += " }"; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | String error_message(type_string + |
| 625 | " - Message Id Number: " + String::num_int64(p_callback_data->messageIdNumber) + |
| 626 | " | Message Id Name: " + p_callback_data->pMessageIdName + |
| 627 | "\n\t" + p_callback_data->pMessage + |
| 628 | objects_string + labels_string); |
| 629 | |
| 630 | // Convert VK severity to our own log macros. |
| 631 | switch (p_message_severity) { |
| 632 | case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT: |
| 633 | print_verbose(error_message); |
| 634 | break; |
| 635 | case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT: |
| 636 | print_line(error_message); |
| 637 | break; |
| 638 | case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT: |
| 639 | WARN_PRINT(error_message); |
| 640 | break; |
| 641 | case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT: |
| 642 | ERR_PRINT(error_message); |
| 643 | CRASH_COND_MSG(Engine::get_singleton()->is_abort_on_gpu_errors_enabled(), "Crashing, because abort on GPU errors is enabled."); |
| 644 | break; |
| 645 | case VK_DEBUG_UTILS_MESSAGE_SEVERITY_FLAG_BITS_MAX_ENUM_EXT: |
| 646 | break; // Shouldn't happen, only handling to make compilers happy. |
| 647 | } |
| 648 | |
| 649 | return VK_FALSE; |
| 650 | } |
| 651 | |
| 652 | VKAPI_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) { |
| 653 | String debug_message = String("Vulkan Debug Report: object - ") + String::num_int64(p_object) + "\n" + p_message; |
| 654 | |
| 655 | switch (p_flags) { |
| 656 | case VK_DEBUG_REPORT_DEBUG_BIT_EXT: |
| 657 | case VK_DEBUG_REPORT_INFORMATION_BIT_EXT: |
| 658 | print_line(debug_message); |
| 659 | break; |
| 660 | case VK_DEBUG_REPORT_WARNING_BIT_EXT: |
| 661 | case VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT: |
| 662 | WARN_PRINT(debug_message); |
| 663 | break; |
| 664 | case VK_DEBUG_REPORT_ERROR_BIT_EXT: |
| 665 | ERR_PRINT(debug_message); |
| 666 | break; |
| 667 | } |
| 668 | |
| 669 | return VK_FALSE; |
| 670 | } |
| 671 | |
| 672 | Error RenderingContextDriverVulkan::_initialize_instance() { |
| 673 | Error err; |
| 674 | TightLocalVector<const char *> enabled_extension_names; |
| 675 | enabled_extension_names.reserve(enabled_instance_extension_names.size()); |
| 676 | for (const CharString &extension_name : enabled_instance_extension_names) { |
| 677 | enabled_extension_names.push_back(extension_name.ptr()); |
| 678 | } |
| 679 | |
| 680 | // We'll set application version to the Vulkan version we're developing against, even if our instance is based on an older Vulkan |
| 681 | // version, devices can still support newer versions of Vulkan. The exception is when we're on Vulkan 1.0, we should not set this |
| 682 | // to anything but 1.0. Note that this value is only used by validation layers to warn us about version issues. |
| 683 | uint32_t application_api_version = instance_api_version == VK_API_VERSION_1_0 ? VK_API_VERSION_1_0 : VK_API_VERSION_1_2; |
| 684 | |
| 685 | CharString cs = GLOBAL_GET("application/config/name").operator String().utf8(); |
| 686 | VkApplicationInfo app_info = {}; |
| 687 | app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; |
| 688 | app_info.pApplicationName = cs.get_data(); |
| 689 | app_info.pEngineName = GODOT_VERSION_NAME; |
| 690 | app_info.engineVersion = VK_MAKE_VERSION(GODOT_VERSION_MAJOR, GODOT_VERSION_MINOR, GODOT_VERSION_PATCH); |
| 691 | app_info.apiVersion = application_api_version; |
| 692 | |
| 693 | TightLocalVector<const char *> enabled_layer_names; |
| 694 | if (_use_validation_layers()) { |
| 695 | err = _find_validation_layers(enabled_layer_names); |
| 696 | ERR_FAIL_COND_V(err != OK, err); |
| 697 | } |
| 698 | |
| 699 | VkInstanceCreateInfo instance_info = {}; |
| 700 | instance_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; |
| 701 | |
| 702 | #if defined(USE_VOLK) && (defined(MACOS_ENABLED) || defined(IOS_ENABLED)) |
| 703 | instance_info.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR; |
| 704 | #endif |
| 705 | |
| 706 | instance_info.pApplicationInfo = &app_info; |
| 707 | instance_info.enabledExtensionCount = enabled_extension_names.size(); |
| 708 | instance_info.ppEnabledExtensionNames = enabled_extension_names.ptr(); |
| 709 | instance_info.enabledLayerCount = enabled_layer_names.size(); |
| 710 | instance_info.ppEnabledLayerNames = enabled_layer_names.ptr(); |
| 711 | |
| 712 | // 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. |
| 713 | VkDebugUtilsMessengerCreateInfoEXT debug_messenger_create_info = {}; |
| 714 | VkDebugReportCallbackCreateInfoEXT debug_report_callback_create_info = {}; |
| 715 | const bool has_debug_utils_extension = enabled_instance_extension_names.has(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); |
| 716 | const bool has_debug_report_extension = enabled_instance_extension_names.has(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); |
| 717 | if (has_debug_utils_extension) { |
| 718 | debug_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; |
| 719 | debug_messenger_create_info.pNext = nullptr; |
| 720 | debug_messenger_create_info.flags = 0; |
| 721 | debug_messenger_create_info.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT; |
| 722 | debug_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; |
| 723 | debug_messenger_create_info.pfnUserCallback = _debug_messenger_callback; |
| 724 | debug_messenger_create_info.pUserData = this; |
| 725 | instance_info.pNext = &debug_messenger_create_info; |
| 726 | } else if (has_debug_report_extension) { |
| 727 | debug_report_callback_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT; |
| 728 | debug_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; |
| 729 | debug_report_callback_create_info.pfnCallback = _debug_report_callback; |
| 730 | debug_report_callback_create_info.pUserData = this; |
| 731 | instance_info.pNext = &debug_report_callback_create_info; |
| 732 | } |
| 733 | |
| 734 | err = _create_vulkan_instance(&instance_info, &instance); |
| 735 | ERR_FAIL_COND_V(err != OK, err); |
| 736 | |
| 737 | #ifdef USE_VOLK |
| 738 | volkLoadInstance(instance); |
| 739 | #endif |
| 740 | |
| 741 | // Physical device. |
| 742 | if (enabled_instance_extension_names.has(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME)) { |
| 743 | functions.GetPhysicalDeviceFeatures2 = PFN_vkGetPhysicalDeviceFeatures2(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceFeatures2")); |
| 744 | functions.GetPhysicalDeviceProperties2 = PFN_vkGetPhysicalDeviceProperties2(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceProperties2")); |
| 745 | |
| 746 | // In Vulkan 1.0, the functions might be accessible under their original extension names. |
| 747 | if (functions.GetPhysicalDeviceFeatures2 == nullptr) { |
| 748 | functions.GetPhysicalDeviceFeatures2 = PFN_vkGetPhysicalDeviceFeatures2(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceFeatures2KHR")); |
| 749 | } |
| 750 | |
| 751 | if (functions.GetPhysicalDeviceProperties2 == nullptr) { |
| 752 | functions.GetPhysicalDeviceProperties2 = PFN_vkGetPhysicalDeviceProperties2(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceProperties2KHR")); |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | // Device. |
| 757 | functions.GetDeviceProcAddr = PFN_vkGetDeviceProcAddr(vkGetInstanceProcAddr(instance, "vkGetDeviceProcAddr")); |
| 758 | |
| 759 | // Surfaces. |
| 760 | functions.GetPhysicalDeviceSurfaceSupportKHR = PFN_vkGetPhysicalDeviceSurfaceSupportKHR(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceSurfaceSupportKHR")); |
| 761 | functions.GetPhysicalDeviceSurfaceFormatsKHR = PFN_vkGetPhysicalDeviceSurfaceFormatsKHR(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceSurfaceFormatsKHR")); |
| 762 | functions.GetPhysicalDeviceSurfaceCapabilitiesKHR = PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceSurfaceCapabilitiesKHR")); |
| 763 | functions.GetPhysicalDeviceSurfacePresentModesKHR = PFN_vkGetPhysicalDeviceSurfacePresentModesKHR(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceSurfacePresentModesKHR")); |
| 764 | |
| 765 | // Debug utils and report. |
| 766 | if (has_debug_utils_extension) { |
| 767 | // Setup VK_EXT_debug_utils function pointers always (we use them for debug labels and names). |
| 768 | functions.CreateDebugUtilsMessengerEXT = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT"); |
| 769 | functions.DestroyDebugUtilsMessengerEXT = (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT"); |
| 770 | functions.CmdBeginDebugUtilsLabelEXT = (PFN_vkCmdBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(instance, "vkCmdBeginDebugUtilsLabelEXT"); |
| 771 | functions.CmdEndDebugUtilsLabelEXT = (PFN_vkCmdEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(instance, "vkCmdEndDebugUtilsLabelEXT"); |
| 772 | functions.SetDebugUtilsObjectNameEXT = (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(instance, "vkSetDebugUtilsObjectNameEXT"); |
| 773 | |
| 774 | if (!functions.debug_util_functions_available()) { |
| 775 | ERR_FAIL_V_MSG(ERR_CANT_CREATE, "GetProcAddr: Failed to init VK_EXT_debug_utils\nGetProcAddr: Failure"); |
| 776 | } |
| 777 | |
| 778 | VkResult res = functions.CreateDebugUtilsMessengerEXT(instance, &debug_messenger_create_info, get_allocation_callbacks(VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT), &debug_messenger); |
| 779 | switch (res) { |
| 780 | case VK_SUCCESS: |
| 781 | break; |
| 782 | case VK_ERROR_OUT_OF_HOST_MEMORY: |
| 783 | ERR_FAIL_V_MSG(ERR_CANT_CREATE, "CreateDebugUtilsMessengerEXT: out of host memory\nCreateDebugUtilsMessengerEXT Failure"); |
| 784 | break; |
| 785 | default: |
| 786 | ERR_FAIL_V_MSG(ERR_CANT_CREATE, "CreateDebugUtilsMessengerEXT: unknown failure\nCreateDebugUtilsMessengerEXT Failure"); |
| 787 | break; |
| 788 | } |
| 789 | } else if (has_debug_report_extension) { |
| 790 | functions.CreateDebugReportCallbackEXT = (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugReportCallbackEXT"); |
| 791 | functions.DebugReportMessageEXT = (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(instance, "vkDebugReportMessageEXT"); |
| 792 | functions.DestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(instance, "vkDestroyDebugReportCallbackEXT"); |
| 793 | |
| 794 | if (!functions.debug_report_functions_available()) { |
| 795 | ERR_FAIL_V_MSG(ERR_CANT_CREATE, "GetProcAddr: Failed to init VK_EXT_debug_report\nGetProcAddr: Failure"); |
| 796 | } |
| 797 | |
| 798 | VkResult res = functions.CreateDebugReportCallbackEXT(instance, &debug_report_callback_create_info, get_allocation_callbacks(VK_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT), &debug_report); |
| 799 | switch (res) { |
| 800 | case VK_SUCCESS: |
| 801 | break; |
| 802 | case VK_ERROR_OUT_OF_HOST_MEMORY: |
| 803 | ERR_FAIL_V_MSG(ERR_CANT_CREATE, "CreateDebugReportCallbackEXT: out of host memory\nCreateDebugReportCallbackEXT Failure"); |
| 804 | break; |
| 805 | default: |
| 806 | ERR_FAIL_V_MSG(ERR_CANT_CREATE, "CreateDebugReportCallbackEXT: unknown failure\nCreateDebugReportCallbackEXT Failure"); |
| 807 | break; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | return OK; |
| 812 | } |
| 813 | |
| 814 | Error RenderingContextDriverVulkan::_initialize_devices() { |
| 815 | if (VulkanHooks::get_singleton() != nullptr) { |
| 816 | VkPhysicalDevice physical_device; |
| 817 | bool device_retrieved = VulkanHooks::get_singleton()->get_physical_device(&physical_device); |
| 818 | ERR_FAIL_COND_V(!device_retrieved, ERR_CANT_CREATE); |
| 819 | |
| 820 | // When a hook is active, pretend the device returned by the hook is the only device available. |
| 821 | driver_devices.resize(1); |
| 822 | physical_devices.resize(1); |
| 823 | device_queue_families.resize(1); |
| 824 | physical_devices[0] = physical_device; |
| 825 | |
| 826 | } else { |
| 827 | uint32_t physical_device_count = 0; |
| 828 | VkResult err = vkEnumeratePhysicalDevices(instance, &physical_device_count, nullptr); |
| 829 | ERR_FAIL_COND_V(err != VK_SUCCESS, ERR_CANT_CREATE); |
| 830 | ERR_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."); |
| 831 | |
| 832 | driver_devices.resize(physical_device_count); |
| 833 | physical_devices.resize(physical_device_count); |
| 834 | device_queue_families.resize(physical_device_count); |
| 835 | err = vkEnumeratePhysicalDevices(instance, &physical_device_count, physical_devices.ptr()); |
| 836 | ERR_FAIL_COND_V(err != VK_SUCCESS, ERR_CANT_CREATE); |
| 837 | } |
| 838 | |
| 839 | // Fill the list of driver devices with the properties from the physical devices. |
| 840 | for (uint32_t i = 0; i < physical_devices.size(); i++) { |
| 841 | VkPhysicalDeviceProperties props; |
| 842 | vkGetPhysicalDeviceProperties(physical_devices[i], &props); |
| 843 | |
| 844 | Device &driver_device = driver_devices[i]; |
| 845 | driver_device.name = String::utf8(props.deviceName); |
| 846 | driver_device.vendor = props.vendorID; |
| 847 | driver_device.type = DeviceType(props.deviceType); |
| 848 | driver_device.workarounds = Workarounds(); |
| 849 | |
| 850 | _check_driver_workarounds(props, driver_device); |
| 851 | |
| 852 | uint32_t queue_family_properties_count = 0; |
| 853 | vkGetPhysicalDeviceQueueFamilyProperties(physical_devices[i], &queue_family_properties_count, nullptr); |
| 854 | |
| 855 | if (queue_family_properties_count > 0) { |
| 856 | device_queue_families[i].properties.resize(queue_family_properties_count); |
| 857 | vkGetPhysicalDeviceQueueFamilyProperties(physical_devices[i], &queue_family_properties_count, device_queue_families[i].properties.ptr()); |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | return OK; |
| 862 | } |
| 863 | |
| 864 | void RenderingContextDriverVulkan::_check_driver_workarounds(const VkPhysicalDeviceProperties &p_device_properties, Device &r_device) { |
| 865 | // Workaround for the Adreno 6XX family of devices. |
| 866 | // |
| 867 | // There's a known issue with the Vulkan driver in this family of devices where it'll crash if a dynamic state for drawing is |
| 868 | // used in a command buffer before a dispatch call is issued. As both dynamic scissor and viewport are basic requirements for |
| 869 | // 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. |
| 870 | // |
| 871 | // As the render graph has no built in limitations of whether it'll issue compute work before anything needs to draw on the |
| 872 | // frame, and there's no guarantee that compute work will never be dependent on rasterization in the future, this workaround |
| 873 | // will end recording on the current command buffer any time a compute list is encountered after a draw list was executed. |
| 874 | // A new command buffer will be created afterwards and the appropriate synchronization primitives will be inserted. |
| 875 | // |
| 876 | // Executing this workaround has the added cost of synchronization between all the command buffers that are created as well as |
| 877 | // all the individual submissions. This performance hit is accepted for the sake of being able to support these devices without |
| 878 | // limiting the design of the renderer. |
| 879 | // |
| 880 | // This bug was fixed in driver version 512.503.0, so we only enabled it on devices older than this. |
| 881 | // |
| 882 | r_device.workarounds.avoid_compute_after_draw = |
| 883 | r_device.vendor == Vendor::VENDOR_QUALCOMM && |
| 884 | p_device_properties.deviceID >= 0x6000000 && // Adreno 6xx |
| 885 | p_device_properties.driverVersion < VK_MAKE_VERSION(512, 503, 0) && |
| 886 | r_device.name.find("Turnip") < 0; |
| 887 | } |
| 888 | |
| 889 | bool RenderingContextDriverVulkan::_use_validation_layers() const { |
| 890 | return Engine::get_singleton()->is_validation_layers_enabled(); |
| 891 | } |
| 892 | |
| 893 | Error RenderingContextDriverVulkan::_create_vulkan_instance(const VkInstanceCreateInfo *p_create_info, VkInstance *r_instance) { |
| 894 | if (VulkanHooks::get_singleton() != nullptr) { |
| 895 | return VulkanHooks::get_singleton()->create_vulkan_instance(p_create_info, r_instance) ? OK : ERR_CANT_CREATE; |
| 896 | } else { |
| 897 | VkResult err = vkCreateInstance(p_create_info, get_allocation_callbacks(VK_OBJECT_TYPE_INSTANCE), r_instance); |
| 898 | ERR_FAIL_COND_V_MSG(err == VK_ERROR_INCOMPATIBLE_DRIVER, ERR_CANT_CREATE, |
| 899 | "Cannot find a compatible Vulkan installable client driver (ICD).\n\n" |
| 900 | "vkCreateInstance Failure"); |
| 901 | ERR_FAIL_COND_V_MSG(err == VK_ERROR_EXTENSION_NOT_PRESENT, ERR_CANT_CREATE, |
| 902 | "Cannot find a specified extension library.\n" |
| 903 | "Make sure your layers path is set appropriately.\n" |
| 904 | "vkCreateInstance Failure"); |
| 905 | ERR_FAIL_COND_V_MSG(err, ERR_CANT_CREATE, |
| 906 | "vkCreateInstance failed.\n\n" |
| 907 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 908 | "Please look at the Getting Started guide for additional information.\n" |
| 909 | "vkCreateInstance Failure"); |
| 910 | } |
| 911 | |
| 912 | return OK; |
| 913 | } |
| 914 | |
| 915 | Error RenderingContextDriverVulkan::initialize() { |
| 916 | Error err; |
| 917 | |
| 918 | #ifdef USE_VOLK |
| 919 | if (volkInitialize() != VK_SUCCESS) { |
| 920 | return FAILED; |
| 921 | } |
| 922 | #endif |
| 923 | |
| 924 | err = _initialize_vulkan_version(); |
| 925 | ERR_FAIL_COND_V(err != OK, err); |
| 926 | |
| 927 | err = _initialize_instance_extensions(); |
| 928 | ERR_FAIL_COND_V(err != OK, err); |
| 929 | |
| 930 | err = _initialize_instance(); |
| 931 | ERR_FAIL_COND_V(err != OK, err); |
| 932 | |
| 933 | err = _initialize_devices(); |
| 934 | ERR_FAIL_COND_V(err != OK, err); |
| 935 | |
| 936 | return OK; |
| 937 | } |
| 938 | |
| 939 | const RenderingContextDriver::Device &RenderingContextDriverVulkan::device_get(uint32_t p_device_index) const { |
| 940 | DEV_ASSERT(p_device_index < driver_devices.size()); |
| 941 | return driver_devices[p_device_index]; |
| 942 | } |
| 943 | |
| 944 | uint32_t RenderingContextDriverVulkan::device_get_count() const { |
| 945 | return driver_devices.size(); |
| 946 | } |
| 947 | |
| 948 | bool RenderingContextDriverVulkan::device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const { |
| 949 | DEV_ASSERT(p_device_index < physical_devices.size()); |
| 950 | |
| 951 | // Check if any of the queues supported by the device supports presenting to the window's surface. |
| 952 | const VkPhysicalDevice physical_device = physical_devices[p_device_index]; |
| 953 | const DeviceQueueFamilies &queue_families = device_queue_families[p_device_index]; |
| 954 | for (uint32_t i = 0; i < queue_families.properties.size(); i++) { |
| 955 | if ((queue_families.properties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) && queue_family_supports_present(physical_device, i, p_surface)) { |
| 956 | return true; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | return false; |
| 961 | } |
| 962 | |
| 963 | RenderingDeviceDriver *RenderingContextDriverVulkan::driver_create() { |
| 964 | return memnew(RenderingDeviceDriverVulkan(this)); |
| 965 | } |
| 966 | |
| 967 | void RenderingContextDriverVulkan::driver_free(RenderingDeviceDriver *p_driver) { |
| 968 | memdelete(p_driver); |
| 969 | } |
| 970 | |
| 971 | RenderingContextDriver::SurfaceID RenderingContextDriverVulkan::surface_create(const void *p_platform_data) { |
| 972 | DEV_ASSERT(false && "Surface creation should not be called on the platform-agnostic version of the driver."); |
| 973 | return SurfaceID(); |
| 974 | } |
| 975 | |
| 976 | void RenderingContextDriverVulkan::surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) { |
| 977 | Surface *surface = (Surface *)(p_surface); |
| 978 | surface->width = p_width; |
| 979 | surface->height = p_height; |
| 980 | surface->needs_resize = true; |
| 981 | } |
| 982 | |
| 983 | void RenderingContextDriverVulkan::surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) { |
| 984 | Surface *surface = (Surface *)(p_surface); |
| 985 | surface->vsync_mode = p_vsync_mode; |
| 986 | surface->needs_resize = true; |
| 987 | } |
| 988 | |
| 989 | DisplayServer::VSyncMode RenderingContextDriverVulkan::surface_get_vsync_mode(SurfaceID p_surface) const { |
| 990 | Surface *surface = (Surface *)(p_surface); |
| 991 | return surface->vsync_mode; |
| 992 | } |
| 993 | |
| 994 | uint32_t RenderingContextDriverVulkan::surface_get_width(SurfaceID p_surface) const { |
| 995 | Surface *surface = (Surface *)(p_surface); |
| 996 | return surface->width; |
| 997 | } |
| 998 | |
| 999 | uint32_t RenderingContextDriverVulkan::surface_get_height(SurfaceID p_surface) const { |
| 1000 | Surface *surface = (Surface *)(p_surface); |
| 1001 | return surface->height; |
| 1002 | } |
| 1003 | |
| 1004 | void RenderingContextDriverVulkan::surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) { |
| 1005 | Surface *surface = (Surface *)(p_surface); |
| 1006 | surface->needs_resize = p_needs_resize; |
| 1007 | } |
| 1008 | |
| 1009 | bool RenderingContextDriverVulkan::surface_get_needs_resize(SurfaceID p_surface) const { |
| 1010 | Surface *surface = (Surface *)(p_surface); |
| 1011 | return surface->needs_resize; |
| 1012 | } |
| 1013 | |
| 1014 | void RenderingContextDriverVulkan::surface_destroy(SurfaceID p_surface) { |
| 1015 | Surface *surface = (Surface *)(p_surface); |
| 1016 | vkDestroySurfaceKHR(instance, surface->vk_surface, get_allocation_callbacks(VK_OBJECT_TYPE_SURFACE_KHR)); |
| 1017 | memdelete(surface); |
| 1018 | } |
| 1019 | |
| 1020 | bool RenderingContextDriverVulkan::is_debug_utils_enabled() const { |
| 1021 | return enabled_instance_extension_names.has(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); |
| 1022 | } |
| 1023 | |
| 1024 | VkInstance RenderingContextDriverVulkan::instance_get() const { |
| 1025 | return instance; |
| 1026 | } |
| 1027 | |
| 1028 | VkPhysicalDevice RenderingContextDriverVulkan::physical_device_get(uint32_t p_device_index) const { |
| 1029 | DEV_ASSERT(p_device_index < physical_devices.size()); |
| 1030 | return physical_devices[p_device_index]; |
| 1031 | } |
| 1032 | |
| 1033 | uint32_t RenderingContextDriverVulkan::queue_family_get_count(uint32_t p_device_index) const { |
| 1034 | DEV_ASSERT(p_device_index < physical_devices.size()); |
| 1035 | return device_queue_families[p_device_index].properties.size(); |
| 1036 | } |
| 1037 | |
| 1038 | VkQueueFamilyProperties RenderingContextDriverVulkan::queue_family_get(uint32_t p_device_index, uint32_t p_queue_family_index) const { |
| 1039 | DEV_ASSERT(p_device_index < physical_devices.size()); |
| 1040 | DEV_ASSERT(p_queue_family_index < queue_family_get_count(p_device_index)); |
| 1041 | return device_queue_families[p_device_index].properties[p_queue_family_index]; |
| 1042 | } |
| 1043 | |
| 1044 | bool RenderingContextDriverVulkan::queue_family_supports_present(VkPhysicalDevice p_physical_device, uint32_t p_queue_family_index, SurfaceID p_surface) const { |
| 1045 | DEV_ASSERT(p_physical_device != VK_NULL_HANDLE); |
| 1046 | DEV_ASSERT(p_surface != 0); |
| 1047 | Surface *surface = (Surface *)(p_surface); |
| 1048 | VkBool32 present_supported = false; |
| 1049 | VkResult err = vkGetPhysicalDeviceSurfaceSupportKHR(p_physical_device, p_queue_family_index, surface->vk_surface, &present_supported); |
| 1050 | return err == VK_SUCCESS && present_supported; |
| 1051 | } |
| 1052 | |
| 1053 | const RenderingContextDriverVulkan::Functions &RenderingContextDriverVulkan::functions_get() const { |
| 1054 | return functions; |
| 1055 | } |
| 1056 | |
| 1057 | #endif // VULKAN_ENABLED |