Blender V5.3
GHOST_ContextVK.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <vulkan/vulkan_core.h>
10
11#define VOLK_NAMESPACE
12#define VOLK_NO_DEVICE_PROTOTYPES
13#define VK_NO_PROTOTYPES
14#ifdef _WIN32
15# define VK_USE_PLATFORM_WIN32_KHR
16#elif defined(__APPLE__)
17# define VK_USE_PLATFORM_METAL_EXT
18#else
19# ifdef WITH_GHOST_X11
20# define VK_USE_PLATFORM_XLIB_KHR
21# endif
22# ifdef WITH_GHOST_WAYLAND
23# define VK_USE_PLATFORM_WAYLAND_KHR
24# endif
25#endif
26#include "volk.h"
27
28#ifdef WITH_GHOST_SDL
29# include <SDL3/SDL_vulkan.h>
30#endif
31
32#include "GHOST_ContextVK.hh"
33#include "GHOST_Types.hh"
34
36
37#if !defined(_WIN32) or defined(_M_ARM64)
38/* Silence compilation warning on non-windows x64 systems. */
39# define VMA_EXTERNAL_MEMORY_WIN32 0
40#endif
41#include "vk_mem_alloc.h"
42
43#include "CLG_log.h"
44
45#include "BLI_string_ref.hh"
46#include "BLI_vector.hh"
47
48#include <algorithm>
49#include <array>
50#include <cassert>
51#include <cinttypes>
52#include <cstdio>
53#include <cstdlib>
54#include <cstring>
55#include <iostream>
56#include <mutex>
57#include <optional>
58#include <sstream>
59#include <vector>
60
61#include <sys/stat.h>
62
63using namespace std;
64
65static CLG_LogRef LOG = {"ghost.context"};
66
67#define __STR(A) "" #A
68#define VK_CHECK(__expression, fail_value) \
69 do { \
70 VkResult r = (__expression); \
71 if (r != VK_SUCCESS) { \
72 CLOG_ERROR(&LOG, \
73 "Vulkan: %s resulted in code %s.", \
74 __STR(__expression), \
75 blender::gpu::to_string(r)); \
76 return fail_value; \
77 } \
78 } while (0)
79
80/* -------------------------------------------------------------------- */
83void GHOST_SwapchainImage::destroy(VkDevice vk_device, const VolkDeviceTable &functions)
84{
85 functions.vkDestroySemaphore(vk_device, present_semaphore, nullptr);
86 present_semaphore = VK_NULL_HANDLE;
87 vk_image = VK_NULL_HANDLE;
88}
89
90void GHOST_FrameDiscard::destroy(VkDevice vk_device, const VolkDeviceTable &functions)
91{
92 while (!swapchains.empty()) {
93 VkSwapchainKHR vk_swapchain = swapchains.back();
94 swapchains.pop_back();
95 functions.vkDestroySwapchainKHR(vk_device, vk_swapchain, nullptr);
96 }
97 while (!semaphores.empty()) {
98 VkSemaphore vk_semaphore = semaphores.back();
99 semaphores.pop_back();
100 functions.vkDestroySemaphore(vk_device, vk_semaphore, nullptr);
101 }
102}
103
104void GHOST_Frame::destroy(VkDevice vk_device, const VolkDeviceTable &functions)
105{
106 functions.vkDestroyFence(vk_device, submission_fence, nullptr);
107 submission_fence = VK_NULL_HANDLE;
108 functions.vkDestroySemaphore(vk_device, acquire_semaphore, nullptr);
109 acquire_semaphore = VK_NULL_HANDLE;
110 discard_pile.destroy(vk_device, functions);
111}
112
114
115/* -------------------------------------------------------------------- */
118
122
123 bool is_supported(const char *extension_name) const
124 {
125 for (const VkExtensionProperties &extension : extensions) {
126 if (STREQ(extension.extensionName, extension_name)) {
127 return true;
128 }
129 }
130 return false;
131 }
132
134 {
135 for (const char *extension_name : extension_names) {
136 if (!is_supported(extension_name)) {
137 return false;
138 }
139 }
140 return true;
141 }
142
143 bool enable(const char *extension_name, bool optional = false)
144 {
145 bool supported = is_supported(extension_name);
146 if (supported) {
148 "Vulkan: %s extension enabled: name=%s",
149 optional ? "optional" : "required",
150 extension_name);
151 enabled.append(extension_name);
152 return true;
153 }
154
156 (optional ? CLG_LEVEL_TRACE : CLG_LEVEL_ERROR),
157 "Vulkan: %s extension not available: name=%s",
158 optional ? "optional" : "required",
159 extension_name);
160
161 return false;
162 }
163
164 bool disable(const char *extension_name)
165 {
166 bool is_extension_enabled = is_enabled(extension_name);
167 if (is_extension_enabled) {
169 &LOG, "Vulkan: extension disabled for compatibility reasons: name=%s", extension_name);
170 enabled.remove(enabled.first_index_of(extension_name));
171 return true;
172 }
173
174 return false;
175 }
176
177 bool enable(const blender::Span<const char *> &extension_names, bool optional = false)
178 {
179 bool failure = false;
180 for (const char *extension_name : extension_names) {
181 failure |= !enable(extension_name, optional);
182 }
183 return !failure;
184 }
185
186 bool is_enabled(const char *extension_name) const
187 {
188 for (const char *enabled_extension_name : enabled) {
189 if (STREQ(enabled_extension_name, extension_name)) {
190 return true;
191 }
192 }
193 return false;
194 }
195};
196
198
199/* -------------------------------------------------------------------- */
202
204 public:
205 VkPhysicalDevice vk_physical_device = VK_NULL_HANDLE;
207
208 VkDevice vk_device = VK_NULL_HANDLE;
209 VolkDeviceTable functions = {};
210
212 VkQueue generic_queue = VK_NULL_HANDLE;
213 VmaAllocator vma_allocator = VK_NULL_HANDLE;
214
215 VkPhysicalDeviceProperties2 properties = {
216 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
217 };
218 VkPhysicalDeviceDriverProperties device_driver_properties = {
219 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES,
220 };
221 VkPhysicalDeviceFeatures2 features = {};
222 VkPhysicalDeviceVulkan11Features features_11 = {};
223 VkPhysicalDeviceTimelineSemaphoreFeatures features_timeline_semaphore = {
224 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TIMELINE_SEMAPHORE_FEATURES};
225 VkPhysicalDeviceBufferDeviceAddressFeatures features_buffer_device_address = {
226 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES};
227 VkPhysicalDeviceRobustness2FeaturesEXT features_robustness2 = {
228 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_FEATURES_EXT};
229 VkPhysicalDeviceAccelerationStructureFeaturesKHR features_acceleration_structure = {
230 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR};
231
232 int users = 0;
233
235 std::mutex queue_mutex;
236
239
240 public:
244 {
246 volk::vkGetPhysicalDeviceProperties2(vk_physical_device, &properties);
247
248 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
249 features_11.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
250 features.pNext = &features_11;
255
256 volk::vkGetPhysicalDeviceFeatures2(vk_physical_device, &features);
258 }
259
261 {
262 if (vma_allocator != VK_NULL_HANDLE) {
263 vmaDestroyAllocator(vma_allocator);
264 vma_allocator = VK_NULL_HANDLE;
265 }
266 if (vk_device != VK_NULL_HANDLE) {
267 functions.vkDestroyDevice(vk_device, nullptr);
268 vk_device = VK_NULL_HANDLE;
269 }
270 }
271
273 {
274 uint32_t extensions_count;
275 VK_CHECK(volk::vkEnumerateDeviceExtensionProperties(
276 vk_physical_device, nullptr, &extensions_count, nullptr),
277 false);
278 extensions.extensions.resize(extensions_count);
279 VK_CHECK(volk::vkEnumerateDeviceExtensionProperties(
280 vk_physical_device, nullptr, &extensions_count, extensions.extensions.data()),
281 false);
282 return true;
283 }
284
286 {
287 if (vk_device) {
288 std::scoped_lock lock(queue_mutex);
289 functions.vkDeviceWaitIdle(vk_device);
290 }
291 }
292
294 {
295 uint32_t queue_family_count = 0;
296 volk::vkGetPhysicalDeviceQueueFamilyProperties(
297 vk_physical_device, &queue_family_count, nullptr);
298
299 vector<VkQueueFamilyProperties> queue_families(queue_family_count);
300 volk::vkGetPhysicalDeviceQueueFamilyProperties(
301 vk_physical_device, &queue_family_count, queue_families.data());
302
304 for (const VkQueueFamilyProperties &queue_family : queue_families) {
305 /* Every VULKAN implementation by spec must have one queue family that support both graphics
306 * and compute pipelines. We select this one; compute only queue family hints at asynchronous
307 * compute implementations. */
308 if ((queue_family.queueFlags & VK_QUEUE_GRAPHICS_BIT) &&
309 (queue_family.queueFlags & VK_QUEUE_COMPUTE_BIT))
310 {
311 return;
312 }
314 }
315 }
316
318 {
320 }
321
322 void init_memory_allocator(VkInstance vk_instance)
323 {
324 VmaAllocatorCreateInfo vma_allocator_create_info = {};
325 vma_allocator_create_info.vulkanApiVersion = VK_API_VERSION_1_1;
326 vma_allocator_create_info.physicalDevice = vk_physical_device;
327 vma_allocator_create_info.device = vk_device;
328 vma_allocator_create_info.instance = vk_instance;
329 vma_allocator_create_info.flags = VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT;
330 if (extensions.is_enabled(VK_EXT_MEMORY_PRIORITY_EXTENSION_NAME)) {
331 vma_allocator_create_info.flags |= VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT;
332 }
333 if (extensions.is_enabled(VK_KHR_MAINTENANCE_4_EXTENSION_NAME)) {
334 vma_allocator_create_info.flags |= VMA_ALLOCATOR_CREATE_KHR_MAINTENANCE4_BIT;
335 }
336 VmaVulkanFunctions vma_vulkan_functions = {};
337 vmaImportVulkanFunctionsFromVolk(&vma_allocator_create_info, &vma_vulkan_functions);
338 vma_allocator_create_info.pVulkanFunctions = &vma_vulkan_functions;
339 vmaCreateAllocator(&vma_allocator_create_info, &vma_allocator);
340 }
341};
342
344
345/* -------------------------------------------------------------------- */
348
350 VkInstance vk_instance = VK_NULL_HANDLE;
351 VkPhysicalDevice vk_physical_device = VK_NULL_HANDLE;
352
354
355 std::optional<GHOST_DeviceVK> device;
356
358 {
359 /* volk is initialized in vk_instance_create_for_platform_checks during Vulkan backend support
360 * detection when not skipped via "--debug-gpu-backend-no-fallback". So only initialize it here
361 * as needed. */
362 if (volk::vkGetInstanceProcAddr == nullptr) {
363 VkResult vk_result = volkInitialize();
364 if (vk_result != VK_SUCCESS) {
366 &LOG,
367 "Error initializing Vulkan loader: VkResult=%d, most likely cannot find the Vulkan "
368 "Loader provided by GPU driver/OS.",
369 vk_result);
370 /* Not recoverable when using "--debug-gpu-backend-no-fallback". */
371 exit(EXIT_FAILURE);
372 }
373 }
375 }
376
378 {
379 device.reset();
380 volk::vkDestroyInstance(vk_instance, nullptr);
381 vk_physical_device = VK_NULL_HANDLE;
382 vk_instance = VK_NULL_HANDLE;
383 }
384
386 {
387 uint32_t extension_count = 0;
388 VK_CHECK(volk::vkEnumerateInstanceExtensionProperties(nullptr, &extension_count, nullptr),
389 false);
390 extensions.extensions.resize(extension_count);
391 VK_CHECK(volk::vkEnumerateInstanceExtensionProperties(
392 nullptr, &extension_count, extensions.extensions.data()),
393 false);
394 return true;
395 }
396
397 bool create_instance(uint32_t vulkan_api_version)
398 {
399 VkApplicationInfo vk_application_info = {VK_STRUCTURE_TYPE_APPLICATION_INFO,
400 nullptr,
401 "Blender",
402 VK_MAKE_VERSION(1, 0, 0),
403 "Blender",
404 VK_MAKE_VERSION(1, 0, 0),
405 vulkan_api_version};
406 VkInstanceCreateInfo vk_instance_create_info = {VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
407 nullptr,
408 0,
409 &vk_application_info,
410 0,
411 nullptr,
412 uint32_t(extensions.enabled.size()),
414
415 };
416
417 VK_CHECK(volk::vkCreateInstance(&vk_instance_create_info, nullptr, &vk_instance), false);
418 return true;
419 }
420
421 bool select_physical_device(const GHOST_GPUDevice &preferred_device,
422 const blender::Span<const char *> required_extensions)
423 {
424 VkPhysicalDevice requested_physical_device = VK_NULL_HANDLE;
425 VkPhysicalDevice fallback_physical_device = VK_NULL_HANDLE;
426
427 uint32_t device_count = 0;
428 volk::vkEnumeratePhysicalDevices(vk_instance, &device_count, nullptr);
429
430 vector<VkPhysicalDevice> physical_devices(device_count);
431 volk::vkEnumeratePhysicalDevices(vk_instance, &device_count, physical_devices.data());
432
433 int best_device_score = -1;
434 /* Index of the device in the full physical-device enumeration. Matches the trailing
435 * `/index` component of `GPUDevice::identifier` (see `init_device_list` in `vk_backend.cc`).
436 */
437 int device_index = -1;
438 for (const VkPhysicalDevice &physical_device : physical_devices) {
439 GHOST_DeviceVK device_vk(physical_device, false);
440 device_index++;
441
442 if (!device_vk.extensions.is_supported(required_extensions)) {
443 continue;
444 }
445 if (!blender::gpu::GPU_vulkan_is_supported_driver(physical_device)) {
446 continue;
447 }
448
449 if (
450#ifndef __APPLE__
451 !device_vk.features.features.geometryShader ||
452#endif
453 !device_vk.features.features.fragmentStoresAndAtomics ||
454 !device_vk.features.features.imageCubeArray ||
455 !device_vk.features.features.dualSrcBlend || !device_vk.features.features.imageCubeArray)
456 {
457 continue;
458 }
459
460 const VkPhysicalDeviceProperties &vk_props = device_vk.properties.properties;
461 if (preferred_device.is_override) {
462 /* Requested device: vendor/device IDs of `uint(-1)` act as wildcards for by-index form. */
463 const bool match = preferred_device.index == device_index &&
464 (preferred_device.vendor_id == uint(-1) ||
465 preferred_device.vendor_id == vk_props.vendorID) &&
466 (preferred_device.device_id == uint(-1) ||
467 preferred_device.device_id == vk_props.deviceID);
468 if (match) {
469 requested_physical_device = physical_device;
470 break;
471 }
472 }
473
474 int device_score = 0;
475 switch (device_vk.properties.properties.deviceType) {
476 case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
477 device_score = 400;
478 break;
479 case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
480 device_score = 300;
481 break;
482 case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
483 device_score = 200;
484 break;
485 case VK_PHYSICAL_DEVICE_TYPE_CPU:
486 device_score = 100;
487 break;
488 default:
489 break;
490 }
491
492 /* User has configured a preferred device. Add bonus score when vendor and device match.
493 * Driver id isn't considered as drivers update more frequently and can break the device
494 * selection. */
495 if (vk_props.deviceID == preferred_device.fallback_device_id &&
496 vk_props.vendorID == preferred_device.fallback_vendor_id)
497 {
498 device_score += 500;
499 if (preferred_device.fallback_index == device_index) {
500 device_score += 10;
501 }
502 }
503 if (device_score > best_device_score) {
504 fallback_physical_device = physical_device;
505 best_device_score = device_score;
506 }
507 }
508
509 if (requested_physical_device != VK_NULL_HANDLE) {
510 vk_physical_device = requested_physical_device;
511 return GHOST_kSuccess;
512 }
513
514 if (fallback_physical_device == VK_NULL_HANDLE) {
515 CLOG_ERROR(&LOG, "No suitable Vulkan Device found!");
516 return GHOST_kFailure;
517 }
518
519 if (preferred_device.is_override) {
520 if (preferred_device.fail_on_invalid_override) {
521 if (preferred_device.vendor_id == uint(-1)) {
523 "Requested Vulkan GPU '%d' is unavailable or unsupported. "
524 "Run with '--gpu-device help' to list available devices.",
525 preferred_device.index);
526 }
527 else {
529 "Requested Vulkan GPU '%x/%x/%x' is unavailable or unsupported. "
530 "Run with '--gpu-device help' to list available devices.",
531 preferred_device.vendor_id,
532 preferred_device.device_id,
533 uint(preferred_device.index));
534 }
535 return GHOST_kFailure;
536 }
537 if (preferred_device.vendor_id == uint(-1)) {
538 CLOG_WARN(&LOG,
539 "Requested Vulkan GPU '%d' is unavailable or unsupported. "
540 "Falling back to the saved GPU preference.",
541 preferred_device.index);
542 }
543 else {
544 CLOG_WARN(&LOG,
545 "Requested Vulkan GPU '%x/%x/%x' is unavailable or unsupported. "
546 "Falling back to the saved GPU preference.",
547 preferred_device.vendor_id,
548 preferred_device.device_id,
549 uint(preferred_device.index));
550 }
551 }
552
553 vk_physical_device = fallback_physical_device;
554
555 return GHOST_kSuccess;
556 }
557
558 bool create_device(const bool use_vk_ext_swapchain_maintenance1,
559 const bool is_debug,
560 const bool no_hw_rt,
561 blender::Span<const char *> required_device_extensions,
562 blender::Span<const char *> optional_device_extensions)
563 {
564 device.emplace(vk_physical_device, use_vk_ext_swapchain_maintenance1);
565 GHOST_DeviceVK &device = *this->device;
566
567 device.extensions.enable(required_device_extensions);
568 device.extensions.enable(optional_device_extensions, true);
569
570 /* Disabling pipeline libraries and dynamic vertex input on AMD drivers due to random crashes
571 * that are also happening when enabling the extension, but not using it at all. This needs
572 * more investigation as it could be related to development workflows.
573 *
574 * This seems to affect the pro drivers more than the `Adrenalin` ones.
575 * But as both share the same code-base it is better to disable them until
576 * it is clear what causes the crashes and when these were fixed.
577 *
578 * Ref #151103
579 */
580 const bool is_amd_driver = device.device_driver_properties.driverID ==
581 VK_DRIVER_ID_AMD_PROPRIETARY ||
583 VK_DRIVER_ID_AMD_OPEN_SOURCE;
584 if (is_amd_driver && is_debug) {
585 device.extensions.disable(VK_KHR_PIPELINE_LIBRARY_EXTENSION_NAME);
586 device.extensions.disable(VK_EXT_GRAPHICS_PIPELINE_LIBRARY_EXTENSION_NAME);
587 device.extensions.disable(VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
588 }
589
590 /* Disable ray query & acceleration structure extensions.
591 * Some driver have issue with capture replay in renderdoc with these specific extensions.
592 */
593 if (no_hw_rt) {
594 device.extensions.disable(VK_KHR_RAY_QUERY_EXTENSION_NAME);
595 device.extensions.disable(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME);
596 }
597
598#ifdef _WIN32
599 /* Intel 7th to 10th Gen Processor iGPUs show a black screen at application startup when using
600 * VK_EXT_vertex_input_dynamic_state. The used driver version for these iGPUs is 101.2xxx or
601 * older.
602 *
603 * Ref: #147721
604 */
605 if (device.device_driver_properties.driverID == VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS &&
606 device.properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU)
607 {
608 const uint32_t driver_version = device.properties.properties.driverVersion;
609 uint32_t driver_version_major = driver_version >> 14u;
610 uint32_t driver_version_minor = driver_version & 0x3fffu;
611 if (driver_version_major < 101 ||
612 (driver_version_major == 101 && driver_version_minor < 3000))
613 {
614 device.extensions.disable(VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
615 }
616 }
617#endif
618
620
621 float queue_priorities[] = {1.0f};
622 vector<VkDeviceQueueCreateInfo> queue_create_infos;
623 VkDeviceQueueCreateInfo graphic_queue_create_info = {};
624 graphic_queue_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
625 graphic_queue_create_info.queueFamilyIndex = device.generic_queue_family;
626 graphic_queue_create_info.queueCount = 1;
627 graphic_queue_create_info.pQueuePriorities = queue_priorities;
628 queue_create_infos.push_back(graphic_queue_create_info);
629
630 VkPhysicalDeviceFeatures device_features = {};
631#ifndef __APPLE__
632 device_features.geometryShader = VK_TRUE;
633#endif
634 device_features.vertexPipelineStoresAndAtomics =
635 device.features.features.vertexPipelineStoresAndAtomics;
636 device_features.multiViewport = device.features.features.multiViewport;
637 device_features.shaderClipDistance = device.features.features.shaderClipDistance;
638 device_features.fragmentStoresAndAtomics = VK_TRUE;
639
640 device_features.dualSrcBlend = VK_TRUE;
641 device_features.imageCubeArray = VK_TRUE;
642 device_features.multiDrawIndirect = device.features.features.multiDrawIndirect;
643 device_features.drawIndirectFirstInstance = VK_TRUE;
644 device_features.samplerAnisotropy = device.features.features.samplerAnisotropy;
645 device_features.wideLines = device.features.features.wideLines;
646
647 VkDeviceCreateInfo device_create_info = {};
648 device_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
649 device_create_info.queueCreateInfoCount = uint32_t(queue_create_infos.size());
650 device_create_info.pQueueCreateInfos = queue_create_infos.data();
651 device_create_info.enabledExtensionCount = uint32_t(device.extensions.enabled.size());
652 device_create_info.ppEnabledExtensionNames = device.extensions.enabled.data();
653 device_create_info.pEnabledFeatures = &device_features;
654
655 std::vector<void *> feature_struct_ptr;
656
657 /* Enable timeline semaphore. */
658 VkPhysicalDeviceTimelineSemaphoreFeatures timeline_semaphore_features = {};
659 timeline_semaphore_features.sType =
660 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TIMELINE_SEMAPHORE_FEATURES;
661 timeline_semaphore_features.timelineSemaphore = VK_TRUE;
662 feature_struct_ptr.push_back(&timeline_semaphore_features);
663
664 /* Enable buffer device address. */
665 VkPhysicalDeviceBufferDeviceAddressFeatures buffer_device_address_features = {};
666 buffer_device_address_features.sType =
667 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES;
668 buffer_device_address_features.bufferDeviceAddress = VK_TRUE;
669 feature_struct_ptr.push_back(&buffer_device_address_features);
670
671 /* Enable separate depth stencil layouts. */
672 VkPhysicalDeviceSeparateDepthStencilLayoutsFeatures separate_depth_stencil_layouts_features =
673 {};
674 separate_depth_stencil_layouts_features.sType =
675 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SEPARATE_DEPTH_STENCIL_LAYOUTS_FEATURES;
676 separate_depth_stencil_layouts_features.separateDepthStencilLayouts = VK_TRUE;
677 feature_struct_ptr.push_back(&separate_depth_stencil_layouts_features);
678
679 /* Enable provoking vertex if available. */
680 VkPhysicalDeviceProvokingVertexFeaturesEXT provoking_vertex_features = {};
681 if (device.extensions.is_supported(VK_EXT_PROVOKING_VERTEX_EXTENSION_NAME)) {
682 provoking_vertex_features.sType =
683 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROVOKING_VERTEX_FEATURES_EXT;
684 provoking_vertex_features.provokingVertexLast = VK_TRUE;
685 feature_struct_ptr.push_back(&provoking_vertex_features);
686 }
687
688 /* Enable dynamic rendering. */
689 VkPhysicalDeviceDynamicRenderingFeatures dynamic_rendering = {};
690 dynamic_rendering.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DYNAMIC_RENDERING_FEATURES;
691 dynamic_rendering.dynamicRendering = VK_TRUE;
692 feature_struct_ptr.push_back(&dynamic_rendering);
693
694 VkPhysicalDeviceDynamicRenderingUnusedAttachmentsFeaturesEXT
695 dynamic_rendering_unused_attachments = {};
696 dynamic_rendering_unused_attachments.sType =
697 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DYNAMIC_RENDERING_UNUSED_ATTACHMENTS_FEATURES_EXT;
698 dynamic_rendering_unused_attachments.dynamicRenderingUnusedAttachments = VK_TRUE;
699 if (device.extensions.is_enabled(VK_EXT_DYNAMIC_RENDERING_UNUSED_ATTACHMENTS_EXTENSION_NAME)) {
700 feature_struct_ptr.push_back(&dynamic_rendering_unused_attachments);
701 }
702
703 VkPhysicalDeviceDynamicRenderingLocalReadFeaturesKHR dynamic_rendering_local_read = {};
704 dynamic_rendering_local_read.sType =
705 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DYNAMIC_RENDERING_LOCAL_READ_FEATURES_KHR;
706 dynamic_rendering_local_read.dynamicRenderingLocalRead = VK_TRUE;
707 if (device.extensions.is_enabled(VK_KHR_DYNAMIC_RENDERING_LOCAL_READ_EXTENSION_NAME)) {
708 feature_struct_ptr.push_back(&dynamic_rendering_local_read);
709 }
710
711 /* VK_EXT_robustness2 */
712 VkPhysicalDeviceRobustness2FeaturesEXT robustness_2_features = {
713 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_FEATURES_EXT};
714 if (device.extensions.is_enabled(VK_EXT_ROBUSTNESS_2_EXTENSION_NAME)) {
715 robustness_2_features.nullDescriptor = device.features_robustness2.nullDescriptor;
716 feature_struct_ptr.push_back(&robustness_2_features);
717 }
718
719 /* Query for Mainenance4 (core in Vulkan 1.3). */
720 VkPhysicalDeviceMaintenance4FeaturesKHR maintenance_4 = {};
721 maintenance_4.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_4_FEATURES_KHR;
722 maintenance_4.maintenance4 = VK_TRUE;
723 if (device.extensions.is_enabled(VK_KHR_MAINTENANCE_4_EXTENSION_NAME)) {
724 feature_struct_ptr.push_back(&maintenance_4);
725 }
726
727 /* Swap-chain maintenance 1 is optional. */
728 VkPhysicalDeviceSwapchainMaintenance1FeaturesEXT swapchain_maintenance_1 = {
729 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SWAPCHAIN_MAINTENANCE_1_FEATURES_EXT, nullptr, VK_TRUE};
730 if (device.extensions.is_enabled(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME)) {
731 feature_struct_ptr.push_back(&swapchain_maintenance_1);
733 }
734
735 /* Query and enable Fragment Shader Barycentrics. */
736 VkPhysicalDeviceFragmentShaderBarycentricFeaturesKHR fragment_shader_barycentric = {};
737 fragment_shader_barycentric.sType =
738 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADER_BARYCENTRIC_FEATURES_KHR;
739 fragment_shader_barycentric.fragmentShaderBarycentric = VK_TRUE;
740 if (device.extensions.is_enabled(VK_KHR_FRAGMENT_SHADER_BARYCENTRIC_EXTENSION_NAME)) {
741 feature_struct_ptr.push_back(&fragment_shader_barycentric);
742 }
743
744 /* VK_EXT_memory_priority */
745 VkPhysicalDeviceMemoryPriorityFeaturesEXT memory_priority = {
746 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PRIORITY_FEATURES_EXT, nullptr, VK_TRUE};
747 if (device.extensions.is_enabled(VK_EXT_MEMORY_PRIORITY_EXTENSION_NAME)) {
748 feature_struct_ptr.push_back(&memory_priority);
749 }
750
751 /* VK_EXT_pageable_device_local_memory */
752 VkPhysicalDevicePageableDeviceLocalMemoryFeaturesEXT pageable_device_local_memory = {
753 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PAGEABLE_DEVICE_LOCAL_MEMORY_FEATURES_EXT,
754 nullptr,
755 VK_TRUE};
756 if (device.extensions.is_enabled(VK_EXT_PAGEABLE_DEVICE_LOCAL_MEMORY_EXTENSION_NAME)) {
757 feature_struct_ptr.push_back(&pageable_device_local_memory);
758 }
759
760 /* VK_EXT_graphics_pipeline_library */
761 VkPhysicalDeviceGraphicsPipelineLibraryFeaturesEXT graphics_pipeline_library = {
762 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_GRAPHICS_PIPELINE_LIBRARY_FEATURES_EXT,
763 nullptr,
764 VK_TRUE};
765 if (device.extensions.is_enabled(VK_EXT_GRAPHICS_PIPELINE_LIBRARY_EXTENSION_NAME)) {
766 feature_struct_ptr.push_back(&graphics_pipeline_library);
767 }
768
769 /* VK_EXT_line_rasterization */
770 VkPhysicalDeviceLineRasterizationFeaturesKHR line_rasterization_features = {};
771 line_rasterization_features.sType =
772 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_LINE_RASTERIZATION_FEATURES_EXT;
773 line_rasterization_features.bresenhamLines = VK_TRUE;
774 if (device.extensions.is_enabled(VK_EXT_LINE_RASTERIZATION_EXTENSION_NAME)) {
775 feature_struct_ptr.push_back(&line_rasterization_features);
776 }
777
778 /* VK_EXT_extended_dynamic_state */
779 VkPhysicalDeviceExtendedDynamicStateFeaturesEXT extended_dynamic_state = {
780 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_FEATURES_EXT, nullptr, VK_TRUE};
781 if (device.extensions.is_enabled(VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME)) {
782 feature_struct_ptr.push_back(&extended_dynamic_state);
783 }
784
785 /* VK_EXT_vertex_input_dynamic_state */
786 VkPhysicalDeviceVertexInputDynamicStateFeaturesEXT vertex_input_dynamic_state = {
787 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_INPUT_DYNAMIC_STATE_FEATURES_EXT,
788 nullptr,
789 VK_TRUE};
790 if (device.extensions.is_enabled(VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME)) {
791 feature_struct_ptr.push_back(&vertex_input_dynamic_state);
792 }
793
794 /* VK_EXT_host_image_copy */
795 VkPhysicalDeviceHostImageCopyFeaturesEXT host_image_copy = {
796 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_IMAGE_COPY_FEATURES_EXT, nullptr, VK_TRUE};
797 if (device.extensions.is_enabled(VK_EXT_HOST_IMAGE_COPY_EXTENSION_NAME)) {
798 feature_struct_ptr.push_back(&host_image_copy);
799 }
800
801 /* VK_KHR_acceleration_structure */
802 VkPhysicalDeviceAccelerationStructureFeaturesKHR acceleration_structure = {
803 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR,
804 nullptr,
805 device.features_acceleration_structure.accelerationStructure,
806 VK_FALSE,
807 VK_FALSE,
808 VK_FALSE,
809 VK_FALSE};
810 if (device.extensions.is_enabled(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME)) {
811 feature_struct_ptr.push_back(&acceleration_structure);
812 }
813
814 /* VK_KHR_ray_query */
815 VkPhysicalDeviceRayQueryFeaturesKHR ray_query = {
816 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_QUERY_FEATURES_KHR, nullptr, VK_TRUE};
817 if (device.extensions.is_enabled(VK_KHR_RAY_QUERY_EXTENSION_NAME)) {
818 feature_struct_ptr.push_back(&ray_query);
819 }
820
821 /* Link all registered feature structs. */
822 for (int i = 1; i < feature_struct_ptr.size(); i++) {
823 ((VkBaseInStructure *)(feature_struct_ptr[i - 1]))->pNext =
824 (VkBaseInStructure *)(feature_struct_ptr[i]);
825 }
826
827 device_create_info.pNext = feature_struct_ptr[0];
828
829 VkResult result = volk::vkCreateDevice(
830 vk_physical_device, &device_create_info, nullptr, &device.vk_device);
831
832 if (result != VK_SUCCESS) {
833#ifndef __has_feature
834# define __has_feature(x) 0
835#endif
836#if (defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)) && defined(__linux__)
837 if (device.device_driver_properties.driverID == VK_DRIVER_ID_NVIDIA_PROPRIETARY) {
839 &LOG,
840 "Vulkan: vkCreateDevice resulted in code %s.\n"
841 "Address Sanitizer has known incompatibilities with Nvidia Vulkan drivers on Linux,"
842 "see https://developer.blender.org/docs/handbook/tooling/asan/"
843 "#shadow-memory-issue-with-vulkan-on-nvidia-linux-drivers for more info.",
845 return GHOST_kFailure;
846 }
847#endif
849 &LOG, "Vulkan: vkCreateDevice resulted in code %s.", blender::gpu::to_string(result));
850 return GHOST_kFailure;
851 }
852
853 volkLoadDeviceTable(&device.functions, device.vk_device);
856 return true;
857 }
858};
859
861
862/* -------------------------------------------------------------------- */
865
873static std::optional<GHOST_InstanceVK> vulkan_instance;
874
876{
877 if (!vulkan_instance.has_value()) {
878 return false;
879 }
880 return vulkan_instance->extensions.is_enabled(extension_name.c_str());
881}
882
884{
885 if (!vulkan_instance.has_value()) {
886 return false;
887 }
888 if (!vulkan_instance->device.has_value()) {
889 return false;
890 }
891 return vulkan_instance->device->extensions.is_enabled(extension_name.c_str());
892}
893
895
897#if defined(WITH_GHOST_SDL)
898 /* SDL */
899 SDL_Window *sdl_window,
900#elif defined(_WIN32)
901 HWND hwnd,
902#elif defined(__APPLE__)
903 void *metal_layer,
904#else
906 /* X11 */
907 Window window,
908 Display *display,
909 /* Wayland */
910 wl_surface *wayland_surface,
911 wl_display *wayland_display,
912 const GHOST_ContextVK_WindowInfo *wayland_window_info,
913#endif
914 int contextMajorVersion,
915 int contextMinorVersion,
916 const GHOST_GPUDevice &preferred_device,
917 const GHOST_WindowHDRInfo *hdr_info)
918 : GHOST_Context(context_params),
919#if defined(WITH_GHOST_SDL)
920 /* SDL */
921 sdl_window_(sdl_window),
922#elif defined(_WIN32)
923 hwnd_(hwnd),
924#elif defined(__APPLE__)
925 metal_layer_(metal_layer),
926#else
927 platform_(platform),
928 /* X11 */
929 display_(display),
930 window_(window),
931 /* Wayland */
932 wayland_surface_(wayland_surface),
933 wayland_display_(wayland_display),
934 wayland_window_info_(wayland_window_info),
935#endif
936 context_major_version_(contextMajorVersion),
937 context_minor_version_(contextMinorVersion),
938 preferred_device_(preferred_device),
939 hdr_info_(hdr_info),
940 surface_(VK_NULL_HANDLE),
941 swapchain_(VK_NULL_HANDLE),
942 frame_data_(2),
943 render_frame_(0),
944 use_hdr_swapchain_(false)
945{
946 frame_data_.reserve(6);
947}
948
950{
951 if (vulkan_instance.has_value()) {
952 GHOST_InstanceVK &instance_vk = vulkan_instance.value();
953 if (!instance_vk.device.has_value() || instance_vk.device->vk_device == VK_NULL_HANDLE) {
954 if (surface_ != VK_NULL_HANDLE) {
955 volk::vkDestroySurfaceKHR(instance_vk.vk_instance, surface_, nullptr);
956 surface_ = VK_NULL_HANDLE;
957 }
958 vulkan_instance.reset();
959 return;
960 }
961
962 GHOST_DeviceVK &device_vk = instance_vk.device.value();
963 device_vk.wait_idle();
964 for (VkFence fence : fence_pile_) {
965 device_vk.functions.vkDestroyFence(device_vk.vk_device, fence, nullptr);
966 }
967 fence_pile_.clear();
968 destroySwapchain();
969
970 if (surface_ != VK_NULL_HANDLE) {
971 volk::vkDestroySurfaceKHR(instance_vk.vk_instance, surface_, nullptr);
972 surface_ = VK_NULL_HANDLE;
973 }
974
975 if (device_vk.users > 0) {
976 device_vk.users--;
977 }
978 if (device_vk.users == 0) {
979 vulkan_instance.reset();
980 }
981 }
982}
983
985{
986 if (acquired_swapchain_image_index_.has_value()) {
987 assert(false);
988 return GHOST_kFailure;
989 }
990
991 GHOST_DeviceVK &device_vk = vulkan_instance->device.value();
992 VkDevice vk_device = device_vk.vk_device;
993
994 /* This method is called after all the draw calls in the application, and it signals that
995 * we are ready to both (1) submit commands for those draw calls to the device and
996 * (2) begin building the next frame. It is assumed as an invariant that the submission fence
997 * in the current GHOST_Frame has been signaled. So, we wait for the *next* GHOST_Frame's
998 * submission fence to be signaled, to ensure the invariant holds for the next call to
999 * `swapBuffers`.
1000 *
1001 * We will pass the current GHOST_Frame to the swap_buffer_draw_callback_ for command buffer
1002 * submission, and it is the responsibility of that callback to use the current GHOST_Frame's
1003 * fence for it's submission fence. Since the callback is called after we wait for the next frame
1004 * to be complete, it is also safe in the callback to clean up resources associated with the next
1005 * frame.
1006 */
1007 render_frame_ = (render_frame_ + 1) % frame_data_.size();
1008 GHOST_Frame &submission_frame_data = frame_data_[render_frame_];
1009 /* Wait for previous time that the frame was used to finish rendering. Presenting can
1010 * still happen in parallel, but acquiring needs can only happen when the frame acquire semaphore
1011 * has been signaled and waited for. */
1012 if (submission_frame_data.submission_fence) {
1013 device_vk.functions.vkWaitForFences(
1014 vk_device, 1, &submission_frame_data.submission_fence, true, UINT64_MAX);
1015 }
1016 for (VkSwapchainKHR swapchain : submission_frame_data.discard_pile.swapchains) {
1017 this->destroySwapchainPresentFences(swapchain);
1018 }
1019 submission_frame_data.discard_pile.destroy(vk_device, device_vk.functions);
1020
1021 const bool use_hdr_swapchain = hdr_info_ &&
1022 (hdr_info_->wide_gamut_enabled || hdr_info_->hdr_enabled) &&
1024 if (use_hdr_swapchain != use_hdr_swapchain_) {
1025 /* Re-create swapchain if HDR mode was toggled in the system settings. */
1026 recreateSwapchain(use_hdr_swapchain);
1027 }
1028 else {
1029#ifdef WITH_GHOST_WAYLAND
1030 /* Wayland doesn't provide a WSI with windowing capabilities, therefore cannot detect whether
1031 * the swap-chain needs to be recreated. But as a side effect we can recreate the swap-chain
1032 * before presenting. */
1033 if (wayland_window_info_) {
1034 const bool recreate_swapchain =
1035 ((wayland_window_info_->size[0] !=
1036 std::max(render_extent_.width, render_extent_min_.width)) ||
1037 (wayland_window_info_->size[1] !=
1038 std::max(render_extent_.height, render_extent_min_.height)));
1039
1040 if (recreate_swapchain) {
1041 /* Swap-chain is out of date. Recreate swap-chain. */
1042 recreateSwapchain(use_hdr_swapchain);
1043 }
1044 }
1045#endif
1046 }
1047 /* there is no valid swapchain when the previous window was minimized. User can have maximized
1048 * the window so we need to check if the swapchain has to be created. */
1049 if (swapchain_ == VK_NULL_HANDLE) {
1050 recreateSwapchain(use_hdr_swapchain);
1051 }
1052
1053 /* Acquire next image, swapchain can be (or become) invalid when minimizing window. */
1054 uint32_t image_index = 0;
1055 if (swapchain_ != VK_NULL_HANDLE) {
1056 /* Some platforms (NVIDIA/Wayland) can receive an out of date swapchain when acquiring the next
1057 * swapchain image. Other do it when calling vkQueuePresent. */
1058 VkResult acquire_result = VK_ERROR_OUT_OF_DATE_KHR;
1059 while (swapchain_ != VK_NULL_HANDLE &&
1060 (ELEM(acquire_result, VK_ERROR_OUT_OF_DATE_KHR, VK_SUBOPTIMAL_KHR)))
1061 {
1062 acquire_result = device_vk.functions.vkAcquireNextImageKHR(
1063 vk_device,
1064 swapchain_,
1065 UINT64_MAX,
1066 submission_frame_data.acquire_semaphore,
1067 VK_NULL_HANDLE,
1068 &image_index);
1069 if (ELEM(acquire_result, VK_ERROR_OUT_OF_DATE_KHR, VK_SUBOPTIMAL_KHR)) {
1070 recreateSwapchain(use_hdr_swapchain);
1071 }
1072 }
1073 }
1074
1075 /* Acquired callback is also called when there is no swapchain.
1076 *
1077 * When acquiring swap chain (image) and the swap chain is discarded (window has been minimized).
1078 * We have trigger a last acquired callback to reduce the attachments of the GPUFramebuffer.
1079 * Vulkan backend will retrieve the data (getVulkanSwapChainFormat) containing a render extent of
1080 * 0,0.
1081 *
1082 * The next frame window manager will detect that the window is minimized and doesn't draw the
1083 * window at all.
1084 */
1085 if (swap_buffer_acquired_callback_) {
1086 swap_buffer_acquired_callback_();
1087 }
1088
1089 if (swapchain_ == VK_NULL_HANDLE) {
1090 CLOG_TRACE(&LOG, "Swap-chain unavailable (minimized window).");
1091 return GHOST_kSuccess;
1092 }
1093
1094 CLOG_DEBUG(&LOG,
1095 "Acquired swap-chain image (render_frame=%" PRIu64 ", image_index=%u)",
1096 render_frame_,
1097 image_index);
1098 acquired_swapchain_image_index_ = image_index;
1099
1100 return GHOST_kSuccess;
1101}
1102VkFence GHOST_ContextVK::getFence()
1103{
1104 if (!fence_pile_.empty()) {
1105 VkFence fence = fence_pile_.back();
1106 fence_pile_.pop_back();
1107 return fence;
1108 }
1109 GHOST_DeviceVK &device_vk = vulkan_instance->device.value();
1110 VkFence fence = VK_NULL_HANDLE;
1111 const VkFenceCreateInfo fence_create_info = {VK_STRUCTURE_TYPE_FENCE_CREATE_INFO};
1112 device_vk.functions.vkCreateFence(device_vk.vk_device, &fence_create_info, nullptr, &fence);
1113 return fence;
1114}
1115
1116void GHOST_ContextVK::setPresentFence(VkSwapchainKHR swapchain, VkFence present_fence)
1117{
1118 if (present_fence == VK_NULL_HANDLE) {
1119 return;
1120 }
1121 present_fences_[swapchain].push_back(present_fence);
1122 GHOST_DeviceVK &device_vk = vulkan_instance->device.value();
1124 for (std::pair<const VkSwapchainKHR, std::vector<VkFence>> &item : present_fences_) {
1125 std::vector<VkFence>::iterator end = item.second.end();
1126 std::vector<VkFence>::iterator it = std::remove_if(
1127 item.second.begin(), item.second.end(), [&](const VkFence fence) {
1128 if (device_vk.functions.vkGetFenceStatus(device_vk.vk_device, fence) == VK_NOT_READY) {
1129 return false;
1130 }
1131 device_vk.functions.vkResetFences(device_vk.vk_device, 1, &fence);
1132 fence_pile_.push_back(fence);
1133 return true;
1134 });
1135 item.second.erase(it, end);
1136 }
1137}
1138
1140{
1141 /* Minimized windows don't have a swapchain and swapchain image. In this case we perform the draw
1142 * to release render graph and discarded resources. */
1143 if (swapchain_ == VK_NULL_HANDLE) {
1144 GHOST_VulkanSwapChainData swap_chain_data = {};
1145 if (swap_buffer_draw_callback_) {
1146 swap_buffer_draw_callback_(&swap_chain_data, true);
1147 }
1148 return GHOST_kSuccess;
1149 }
1150
1151 if (!acquired_swapchain_image_index_.has_value()) {
1152 assert(false);
1153 return GHOST_kFailure;
1154 }
1155 GHOST_DeviceVK &device_vk = vulkan_instance->device.value();
1156 VkDevice vk_device = device_vk.vk_device;
1157
1158 uint32_t image_index = acquired_swapchain_image_index_.value();
1159 GHOST_SwapchainImage &swapchain_image = swapchain_images_[image_index];
1160 GHOST_Frame &submission_frame_data = frame_data_[render_frame_];
1161 const bool use_hdr_swapchain = hdr_info_ &&
1162 (hdr_info_->wide_gamut_enabled || hdr_info_->hdr_enabled) &&
1164
1165 GHOST_VulkanSwapChainData swap_chain_data;
1166 swap_chain_data.image = swapchain_image.vk_image;
1167 swap_chain_data.surface_format = surface_format_;
1168 swap_chain_data.extent = render_extent_;
1169 swap_chain_data.submission_fence = submission_frame_data.submission_fence;
1170 swap_chain_data.acquire_semaphore = submission_frame_data.acquire_semaphore;
1171 swap_chain_data.present_semaphore = swapchain_image.present_semaphore;
1172 swap_chain_data.sdr_scale = (hdr_info_) ? hdr_info_->sdr_white_level : 1.0f;
1173
1174 device_vk.functions.vkResetFences(vk_device, 1, &submission_frame_data.submission_fence);
1175 if (swap_buffer_draw_callback_) {
1176 swap_buffer_draw_callback_(&swap_chain_data, true);
1177 }
1178
1179 VkPresentInfoKHR present_info = {};
1180 present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
1181 present_info.waitSemaphoreCount = 1;
1182 present_info.pWaitSemaphores = &swapchain_image.present_semaphore;
1183 present_info.swapchainCount = 1;
1184 present_info.pSwapchains = &swapchain_;
1185 present_info.pImageIndices = &image_index;
1186 present_info.pResults = nullptr;
1187
1188 VkResult present_result = VK_SUCCESS;
1189 {
1190 std::scoped_lock lock(device_vk.queue_mutex);
1191 VkSwapchainPresentFenceInfoEXT fence_info{VK_STRUCTURE_TYPE_SWAPCHAIN_PRESENT_FENCE_INFO_EXT};
1192 VkFence present_fence = VK_NULL_HANDLE;
1193 if (device_vk.use_vk_ext_swapchain_maintenance_1) {
1194 present_fence = this->getFence();
1195
1196 fence_info.swapchainCount = 1;
1197 fence_info.pFences = &present_fence;
1198
1199 present_info.pNext = &fence_info;
1200 }
1201 present_result = device_vk.functions.vkQueuePresentKHR(device_vk.generic_queue, &present_info);
1202 this->setPresentFence(swapchain_, present_fence);
1203 }
1204 acquired_swapchain_image_index_.reset();
1205
1206 if (ELEM(present_result, VK_ERROR_OUT_OF_DATE_KHR, VK_SUBOPTIMAL_KHR)) {
1207 recreateSwapchain(use_hdr_swapchain);
1208 return GHOST_kSuccess;
1209 }
1210 if (present_result != VK_SUCCESS) {
1211 CLOG_ERROR(&LOG,
1212 "Vulkan: failed to present swap-chain image : %s",
1213 blender::gpu::to_string(present_result));
1214 return GHOST_kFailure;
1215 }
1216
1217 return GHOST_kSuccess;
1218}
1219
1221 GHOST_VulkanSwapChainData *r_swap_chain_data)
1222{
1223 r_swap_chain_data->image = VK_NULL_HANDLE;
1224 r_swap_chain_data->surface_format = surface_format_;
1225 r_swap_chain_data->extent = render_extent_;
1226 r_swap_chain_data->sdr_scale = (hdr_info_) ? hdr_info_->sdr_white_level : 1.0f;
1227
1228 return GHOST_kSuccess;
1229}
1230
1232{
1233 r_handles = {
1234 VK_NULL_HANDLE, /* instance */
1235 VK_NULL_HANDLE, /* physical_device */
1236 VK_NULL_HANDLE, /* device */
1237 0, /* queue_family */
1238 VK_NULL_HANDLE, /* queue */
1239 nullptr, /* queue_mutex */
1240 VK_NULL_HANDLE, /* vma_allocator */
1241 };
1242
1243 if (vulkan_instance.has_value() && vulkan_instance.value().device.has_value()) {
1244 GHOST_InstanceVK &instance_vk = vulkan_instance.value();
1245 GHOST_DeviceVK &device_vk = instance_vk.device.value();
1246 r_handles = {
1247 instance_vk.vk_instance,
1248 device_vk.vk_physical_device,
1249 device_vk.vk_device,
1250 device_vk.generic_queue_family,
1251 device_vk.generic_queue,
1252 &device_vk.queue_mutex,
1253 device_vk.vma_allocator,
1254 };
1255 }
1256
1257 return GHOST_kSuccess;
1258}
1259
1261 std::function<void(const GHOST_VulkanSwapChainData *, bool)> swap_buffer_draw_callback,
1262 std::function<void(void)> swap_buffer_acquired_callback,
1263 std::function<void(GHOST_VulkanOpenXRData *)> openxr_acquire_framebuffer_image_callback,
1264 std::function<void(GHOST_VulkanOpenXRData *)> openxr_release_framebuffer_image_callback)
1265{
1266 swap_buffer_draw_callback_ = swap_buffer_draw_callback;
1267 swap_buffer_acquired_callback_ = swap_buffer_acquired_callback;
1268 openxr_acquire_framebuffer_image_callback_ = openxr_acquire_framebuffer_image_callback;
1269 openxr_release_framebuffer_image_callback_ = openxr_release_framebuffer_image_callback;
1270 return GHOST_kSuccess;
1271}
1272
1278
1284
1285static const char *to_string_vk_format(VkFormat format)
1286{
1287 switch (format) {
1290 case VK_FORMAT_B8G8R8A8_UNORM:
1291 return STRINGIFY(VK_FORMAT_B8G8R8A8_UNORM);
1292 case VK_FORMAT_R16G16B16A16_SFLOAT:
1293 return STRINGIFY(VK_FORMAT_R16G16B16A16_SFLOAT);
1294 default:
1295 return STRINGIFY_ARG(format);
1296 }
1297}
1298
1299static const char *to_string_vk_color_space(VkColorSpaceKHR color_space)
1300{
1301 switch (color_space) {
1302 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
1303 return STRINGIFY(VK_COLOR_SPACE_SRGB_NONLINEAR_KHR);
1304 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
1305 return STRINGIFY(VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT);
1306 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
1307 return STRINGIFY(VK_COLOR_SPACE_PASS_THROUGH_EXT);
1308 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
1309 return STRINGIFY(VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT);
1310 case VK_COLOR_SPACE_DISPLAY_P3_LINEAR_EXT:
1311 return STRINGIFY(VK_COLOR_SPACE_DISPLAY_P3_LINEAR_EXT);
1312 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
1313 return STRINGIFY(VK_COLOR_SPACE_BT709_LINEAR_EXT);
1314 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
1315 return STRINGIFY(VK_COLOR_SPACE_BT709_NONLINEAR_EXT);
1316 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
1317 return STRINGIFY(VK_COLOR_SPACE_BT2020_LINEAR_EXT);
1318 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
1319 return STRINGIFY(VK_COLOR_SPACE_HDR10_ST2084_EXT);
1320 case VK_COLOR_SPACE_DISPLAY_NATIVE_AMD:
1321 return STRINGIFY(VK_COLOR_SPACE_DISPLAY_NATIVE_AMD);
1322 default:
1323 return STRINGIFY_ARG(color_space);
1324 }
1325}
1326
1327static const char *to_string_vk_present_mode(VkPresentModeKHR present_mode)
1328{
1329 switch (present_mode) {
1330 case VK_PRESENT_MODE_FIFO_KHR:
1331 return STRINGIFY(VK_PRESENT_MODE_FIFO_KHR);
1332 case VK_PRESENT_MODE_MAILBOX_KHR:
1333 return STRINGIFY(VK_PRESENT_MODE_MAILBOX_KHR);
1334 case VK_PRESENT_MODE_IMMEDIATE_KHR:
1335 return STRINGIFY(VK_PRESENT_MODE_IMMEDIATE_KHR);
1336 case VK_PRESENT_MODE_FIFO_RELAXED_KHR:
1337 return STRINGIFY(VK_PRESENT_MODE_FIFO_RELAXED_KHR);
1338 default:
1339 return STRINGIFY_ARG(present_mode);
1340 }
1341}
1342
1344 VkPhysicalDevice device,
1345 VkSurfaceKHR surface,
1346 VkPresentModeKHR *r_presentMode)
1347{
1348 uint32_t present_count;
1349 volk::vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &present_count, nullptr);
1350 vector<VkPresentModeKHR> presents(present_count);
1351 volk::vkGetPhysicalDeviceSurfacePresentModesKHR(
1352 device, surface, &present_count, presents.data());
1353
1354 if (vsync != GHOST_kVSyncModeUnset) {
1355 const bool vsync_off = (vsync == GHOST_kVSyncModeOff);
1356 if (vsync_off) {
1357 for (const VkPresentModeKHR present_mode : presents) {
1358 if (present_mode == VK_PRESENT_MODE_IMMEDIATE_KHR) {
1359 *r_presentMode = present_mode;
1360 return GHOST_kSuccess;
1361 }
1362 }
1363 CLOG_WARN(&LOG,
1364 "Vulkan: VSync off was requested via --gpu-vsync, "
1365 "but VK_PRESENT_MODE_IMMEDIATE_KHR is not supported.");
1366 }
1367 }
1368
1369 /* MAILBOX is the lowest latency V-Sync enabled mode. We will use it if available as it fixes
1370 * some lag on NVIDIA/Intel GPUs. */
1371 /* TODO: select the correct presentation mode based on the actual being performed by the user.
1372 * When low latency is required (paint cursor) we should select mailbox, otherwise we can do FIFO
1373 * to reduce CPU/GPU usage. */
1374 for (const VkPresentModeKHR present_mode : presents) {
1375 if (present_mode == VK_PRESENT_MODE_MAILBOX_KHR) {
1376 *r_presentMode = present_mode;
1377 return GHOST_kSuccess;
1378 }
1379 }
1380
1381 /* FIFO present mode is always available and we (should) prefer it as it will keep the main loop
1382 * running along the monitor refresh rate. Mailbox and FIFO relaxed can generate a lot of frames
1383 * that will never be displayed. */
1384 *r_presentMode = VK_PRESENT_MODE_FIFO_KHR;
1385 return GHOST_kSuccess;
1386}
1387
1393static bool selectSurfaceFormat(const VkPhysicalDevice physical_device,
1394 const VkSurfaceKHR surface,
1395 bool use_hdr_swapchain,
1396 VkSurfaceFormatKHR &r_surfaceFormat)
1397{
1398 uint32_t format_count;
1399 volk::vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &format_count, nullptr);
1400 vector<VkSurfaceFormatKHR> formats(format_count);
1401 volk::vkGetPhysicalDeviceSurfaceFormatsKHR(
1402 physical_device, surface, &format_count, formats.data());
1403
1404 array<VkSurfaceFormatKHR, 3> selection_order = {{
1405#if defined(_WIN32) || defined(__APPLE__)
1406 {VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT},
1407#else
1408 {VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_PASS_THROUGH_EXT},
1409#endif
1410 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
1411 {VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}}};
1412
1413 for (const VkSurfaceFormatKHR &config : selection_order) {
1414 if (!use_hdr_swapchain && config.format == VK_FORMAT_R16G16B16A16_SFLOAT) {
1415 continue;
1416 }
1417
1418 for (const VkSurfaceFormatKHR &format : formats) {
1419 if (format.format == config.format && format.colorSpace == config.colorSpace) {
1420 r_surfaceFormat = format;
1421 return true;
1422 }
1423 }
1424 }
1425
1426 return false;
1427}
1428
1429GHOST_TSuccess GHOST_ContextVK::initializeFrameData()
1430{
1431 GHOST_DeviceVK &device_vk = vulkan_instance.value().device.value();
1432 VkDevice device = device_vk.vk_device;
1433
1434 const VkSemaphoreCreateInfo vk_semaphore_create_info = {
1435 VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, nullptr, 0};
1436 const VkFenceCreateInfo vk_fence_create_info = {
1437 VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, nullptr, VK_FENCE_CREATE_SIGNALED_BIT};
1438 for (GHOST_SwapchainImage &swapchain_image : swapchain_images_) {
1439 /* VK_EXT_swapchain_maintenance1 reuses present semaphores. */
1440 if (swapchain_image.present_semaphore == VK_NULL_HANDLE) {
1441 VK_CHECK(device_vk.functions.vkCreateSemaphore(
1442 device, &vk_semaphore_create_info, nullptr, &swapchain_image.present_semaphore),
1444 }
1445 }
1446
1447 for (int index = 0; index < frame_data_.size(); index++) {
1448 GHOST_Frame &frame_data = frame_data_[index];
1449 /* VK_EXT_swapchain_maintenance1 reuses acquire semaphores. */
1450 if (frame_data.acquire_semaphore == VK_NULL_HANDLE) {
1451 VK_CHECK(device_vk.functions.vkCreateSemaphore(
1452 device, &vk_semaphore_create_info, nullptr, &frame_data.acquire_semaphore),
1454 }
1455 if (frame_data.submission_fence == VK_NULL_HANDLE) {
1456 VK_CHECK(device_vk.functions.vkCreateFence(
1457 device, &vk_fence_create_info, nullptr, &frame_data.submission_fence),
1459 }
1460 }
1461
1462 return GHOST_kSuccess;
1463}
1464
1465GHOST_TSuccess GHOST_ContextVK::recreateSwapchain(bool use_hdr_swapchain)
1466{
1467 GHOST_InstanceVK &instance_vk = vulkan_instance.value();
1468 GHOST_DeviceVK &device_vk = instance_vk.device.value();
1469
1470 surface_format_ = {};
1472 device_vk.vk_physical_device, surface_, use_hdr_swapchain, surface_format_))
1473 {
1474 return GHOST_kFailure;
1475 }
1476
1477 VkPresentModeKHR present_mode;
1478 if (!selectPresentMode(getVSync(), device_vk.vk_physical_device, surface_, &present_mode)) {
1479 return GHOST_kFailure;
1480 }
1481
1482 /* Query the surface capabilities for the given present mode on the surface. */
1483 VkSurfacePresentScalingCapabilitiesEXT vk_surface_present_scaling_capabilities = {
1484 VK_STRUCTURE_TYPE_SURFACE_PRESENT_SCALING_CAPABILITIES_EXT,
1485 };
1486 VkSurfaceCapabilities2KHR vk_surface_capabilities = {
1487 VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR,
1488 &vk_surface_present_scaling_capabilities,
1489 };
1490 VkSurfacePresentModeEXT vk_surface_present_mode = {
1491 VK_STRUCTURE_TYPE_SURFACE_PRESENT_MODE_EXT, nullptr, present_mode};
1492 VkPhysicalDeviceSurfaceInfo2KHR vk_physical_device_surface_info = {
1493 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SURFACE_INFO_2_KHR, &vk_surface_present_mode, surface_};
1494 VkSurfaceCapabilitiesKHR capabilities = {};
1495
1496 if (device_vk.use_vk_ext_swapchain_maintenance_1) {
1497 VK_CHECK(volk::vkGetPhysicalDeviceSurfaceCapabilities2KHR(device_vk.vk_physical_device,
1498 &vk_physical_device_surface_info,
1499 &vk_surface_capabilities),
1501 capabilities = vk_surface_capabilities.surfaceCapabilities;
1502 }
1503 else {
1504 VK_CHECK(volk::vkGetPhysicalDeviceSurfaceCapabilitiesKHR(
1505 device_vk.vk_physical_device, surface_, &capabilities),
1507 }
1508
1509 use_hdr_swapchain_ = use_hdr_swapchain;
1510 render_extent_ = capabilities.currentExtent;
1511 render_extent_min_ = capabilities.minImageExtent;
1512 if (render_extent_.width == UINT32_MAX) {
1513 /* Window Manager is going to set the surface size based on the given size.
1514 * Choose something between minImageExtent and maxImageExtent. */
1515 int width = 0;
1516 int height = 0;
1517
1518#ifdef WITH_GHOST_WAYLAND
1519 /* Wayland doesn't provide a windowing API via WSI. */
1520 if (wayland_window_info_) {
1521 width = wayland_window_info_->size[0];
1522 height = wayland_window_info_->size[1];
1523 }
1524#endif
1525
1526 if (width == 0 || height == 0) {
1527 width = 1280;
1528 height = 720;
1529 }
1530
1531 render_extent_.width = width;
1532 render_extent_.height = height;
1533
1534 if (capabilities.minImageExtent.width > render_extent_.width) {
1535 render_extent_.width = capabilities.minImageExtent.width;
1536 }
1537 if (capabilities.minImageExtent.height > render_extent_.height) {
1538 render_extent_.height = capabilities.minImageExtent.height;
1539 }
1540 }
1541
1542 if (device_vk.use_vk_ext_swapchain_maintenance_1) {
1543 if (vk_surface_present_scaling_capabilities.minScaledImageExtent.width > render_extent_.width)
1544 {
1545 render_extent_.width = vk_surface_present_scaling_capabilities.minScaledImageExtent.width;
1546 }
1547 if (vk_surface_present_scaling_capabilities.minScaledImageExtent.height >
1548 render_extent_.height)
1549 {
1550 render_extent_.height = vk_surface_present_scaling_capabilities.minScaledImageExtent.height;
1551 }
1552 }
1553
1554 /* Discard swapchain resources of current swapchain. */
1555 GHOST_FrameDiscard &discard_pile = frame_data_[render_frame_].discard_pile;
1556 for (GHOST_SwapchainImage &swapchain_image : swapchain_images_) {
1557 swapchain_image.vk_image = VK_NULL_HANDLE;
1558 if (swapchain_image.present_semaphore != VK_NULL_HANDLE) {
1559 discard_pile.semaphores.push_back(swapchain_image.present_semaphore);
1560 swapchain_image.present_semaphore = VK_NULL_HANDLE;
1561 }
1562 }
1563
1564 /* Swap-chains with out any resolution should not be created. In the case the render extent is
1565 * zero we should not use the swap-chain.
1566 *
1567 * VUID-VkSwapchainCreateInfoKHR-imageExtent-01689
1568 */
1569 if (render_extent_.width == 0 || render_extent_.height == 0) {
1570 if (swapchain_) {
1571 discard_pile.swapchains.push_back(swapchain_);
1572 swapchain_ = VK_NULL_HANDLE;
1573 }
1574 return GHOST_kFailure;
1575 }
1576
1577 /* Use double buffering when using FIFO. Increasing the number of images could stall when doing
1578 * actions that require low latency (paint cursor, UI resizing). MAILBOX prefers triple
1579 * buffering. */
1580 uint32_t image_count_requested = present_mode == VK_PRESENT_MODE_MAILBOX_KHR ? 3 : 2;
1581 /* NOTE: maxImageCount == 0 means no limit. */
1582 if (capabilities.minImageCount != 0 && image_count_requested < capabilities.minImageCount) {
1583 image_count_requested = capabilities.minImageCount;
1584 }
1585 if (capabilities.maxImageCount != 0 && image_count_requested > capabilities.maxImageCount) {
1586 image_count_requested = capabilities.maxImageCount;
1587 }
1588
1589 VkSwapchainKHR old_swapchain = swapchain_;
1590
1591 /* First time we stretch the swapchain image as it can happen that the first frame size isn't
1592 * correctly reported by the initial swapchain. All subsequent creations will use one to one as
1593 * that can reduce resizing artifacts. */
1594 VkPresentScalingFlagBitsEXT vk_present_scaling = old_swapchain == VK_NULL_HANDLE ?
1595 VK_PRESENT_SCALING_STRETCH_BIT_EXT :
1596 VK_PRESENT_SCALING_ONE_TO_ONE_BIT_EXT;
1597
1598 VkSwapchainPresentModesCreateInfoEXT vk_swapchain_present_modes = {
1599 VK_STRUCTURE_TYPE_SWAPCHAIN_PRESENT_MODES_CREATE_INFO_EXT, nullptr, 1, &present_mode};
1600 VkSwapchainPresentScalingCreateInfoEXT vk_swapchain_present_scaling = {
1601 VK_STRUCTURE_TYPE_SWAPCHAIN_PRESENT_SCALING_CREATE_INFO_EXT,
1602 &vk_swapchain_present_modes,
1603 vk_surface_present_scaling_capabilities.supportedPresentScaling & vk_present_scaling,
1604 vk_surface_present_scaling_capabilities.supportedPresentGravityX &
1605 VK_PRESENT_GRAVITY_MIN_BIT_EXT,
1606 vk_surface_present_scaling_capabilities.supportedPresentGravityY &
1607 VK_PRESENT_GRAVITY_MAX_BIT_EXT,
1608 };
1609
1610 VkSwapchainCreateInfoKHR create_info = {};
1611 create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
1612 if (device_vk.use_vk_ext_swapchain_maintenance_1) {
1613 create_info.pNext = &vk_swapchain_present_scaling;
1614 }
1615 create_info.surface = surface_;
1616 create_info.minImageCount = image_count_requested;
1617 create_info.imageFormat = surface_format_.format;
1618 create_info.imageColorSpace = surface_format_.colorSpace;
1619 create_info.imageExtent = render_extent_;
1620 create_info.imageArrayLayers = 1;
1621 create_info.imageUsage = VK_IMAGE_USAGE_TRANSFER_DST_BIT |
1622 (use_hdr_swapchain ? VK_IMAGE_USAGE_STORAGE_BIT : 0);
1623 create_info.preTransform = capabilities.currentTransform;
1624 /* Alpha lets the compositor blend the surface against the desktop, needed by CSD for rounded
1625 * window corners. Not all surfaces support it, so fall back to opaque in those cases. The caller
1626 * handles the lack of transparency by drawing square corners, see #GHOST_Context::hasAlpha. */
1628 (capabilities.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR))
1629 {
1630 create_info.compositeAlpha = VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
1631 }
1632 else {
1633 create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1634 context_params_.use_alpha = false;
1635 }
1636 create_info.presentMode = present_mode;
1637 create_info.clipped = VK_TRUE;
1638 create_info.oldSwapchain = old_swapchain;
1639 create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
1640 create_info.queueFamilyIndexCount = 0;
1641 create_info.pQueueFamilyIndices = nullptr;
1642
1643 VK_CHECK(device_vk.functions.vkCreateSwapchainKHR(
1644 device_vk.vk_device, &create_info, nullptr, &swapchain_),
1646
1647 /* image_count may not be what we requested! Getter for final value. */
1648 uint32_t actual_image_count = 0;
1649 device_vk.functions.vkGetSwapchainImagesKHR(
1650 device_vk.vk_device, swapchain_, &actual_image_count, nullptr);
1651 /* Some platforms require a minimum amount of render frames that is larger than we expect. When
1652 * that happens we should increase the number of frames in flight. We could also consider
1653 * splitting the frame in flight and image specific data. */
1654 if (actual_image_count > frame_data_.size()) {
1655 CLOG_TRACE(&LOG, "Vulkan: Increasing frame data to %u frames", actual_image_count);
1656 assert(actual_image_count <= frame_data_.capacity());
1657 frame_data_.resize(actual_image_count);
1658 }
1659 swapchain_images_.resize(actual_image_count);
1660 std::vector<VkImage> swapchain_images(actual_image_count);
1661 device_vk.functions.vkGetSwapchainImagesKHR(
1662 device_vk.vk_device, swapchain_, &actual_image_count, swapchain_images.data());
1663 for (int index = 0; index < actual_image_count; index++) {
1664 swapchain_images_[index].vk_image = swapchain_images[index];
1665 }
1666 CLOG_DEBUG(&LOG,
1667 "Vulkan: recreating swapchain: width=%u, height=%u, format=%s, colorSpace=%s, "
1668 "present_mode=%s, image_count_requested=%u, image_count_acquired=%u, "
1669 "swapchain=%" PRIx64 ", old_swapchain=%" PRIx64 "",
1670 render_extent_.width,
1671 render_extent_.height,
1672 to_string_vk_format(surface_format_.format),
1673 to_string_vk_color_space(surface_format_.colorSpace),
1674 to_string_vk_present_mode(present_mode),
1675 image_count_requested,
1676 actual_image_count,
1677 uint64_t(swapchain_),
1678 uint64_t(old_swapchain));
1679 /* Construct new semaphores. It can be that image_count is larger than previously. We only need
1680 * to fill in where the handle is `VK_NULL_HANDLE`. */
1681 /* Previous handles from the frame data cannot be used and should be discarded. */
1682 for (GHOST_Frame &frame : frame_data_) {
1683 if (frame.acquire_semaphore != VK_NULL_HANDLE) {
1684 discard_pile.semaphores.push_back(frame.acquire_semaphore);
1685 }
1686 frame.acquire_semaphore = VK_NULL_HANDLE;
1687 }
1688 if (old_swapchain) {
1689 discard_pile.swapchains.push_back(old_swapchain);
1690 }
1691 initializeFrameData();
1692
1693 image_count_ = actual_image_count;
1694
1695 return GHOST_kSuccess;
1696}
1697
1698void GHOST_ContextVK::destroySwapchainPresentFences(VkSwapchainKHR swapchain)
1699{
1700 GHOST_DeviceVK &device_vk = vulkan_instance.value().device.value();
1701 const std::vector<VkFence> &fences = present_fences_[swapchain];
1702 if (!fences.empty()) {
1703 device_vk.functions.vkWaitForFences(
1704 device_vk.vk_device, fences.size(), fences.data(), VK_TRUE, UINT64_MAX);
1705 for (VkFence fence : fences) {
1706 device_vk.functions.vkDestroyFence(device_vk.vk_device, fence, nullptr);
1707 }
1708 }
1709 present_fences_.erase(swapchain);
1710}
1711
1712GHOST_TSuccess GHOST_ContextVK::destroySwapchain()
1713{
1714 GHOST_DeviceVK &device_vk = vulkan_instance.value().device.value();
1715
1716 if (swapchain_ != VK_NULL_HANDLE) {
1717 this->destroySwapchainPresentFences(swapchain_);
1718 device_vk.functions.vkDestroySwapchainKHR(device_vk.vk_device, swapchain_, nullptr);
1719 }
1720 device_vk.wait_idle();
1721 for (GHOST_SwapchainImage &swapchain_image : swapchain_images_) {
1722 swapchain_image.destroy(device_vk.vk_device, device_vk.functions);
1723 }
1724 swapchain_images_.clear();
1725 for (GHOST_Frame &frame_data : frame_data_) {
1726 for (VkSwapchainKHR swapchain : frame_data.discard_pile.swapchains) {
1727 this->destroySwapchainPresentFences(swapchain);
1728 }
1729 frame_data.destroy(device_vk.vk_device, device_vk.functions);
1730 }
1731 frame_data_.clear();
1732
1733 return GHOST_kSuccess;
1734}
1735
1736std::vector<const char *> GHOST_ContextVK::getPlatformSpecificSurfaceExtensions() const
1737{
1738 std::vector<const char *> extensions;
1739#if defined(WITH_GHOST_SDL)
1740 /* SDL provides the set of instance extensions its window backend requires. */
1741 Uint32 sdl_extension_count = 0;
1742 const char *const *sdl_extensions = SDL_Vulkan_GetInstanceExtensions(&sdl_extension_count);
1743 for (Uint32 i = 0; i < sdl_extension_count; i++) {
1744 extensions.push_back(sdl_extensions[i]);
1745 }
1746#elif defined(_WIN32)
1747 extensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
1748#elif defined(__APPLE__)
1749 extensions.push_back(VK_EXT_METAL_SURFACE_EXTENSION_NAME);
1750#else /* UNIX/Linux */
1751 switch (platform_) {
1752# ifdef WITH_GHOST_X11
1753 case GHOST_kVulkanPlatformX11:
1754 extensions.push_back(VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
1755 break;
1756# endif
1757# ifdef WITH_GHOST_WAYLAND
1758 case GHOST_kVulkanPlatformWayland:
1759 extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME);
1760 break;
1761# endif
1763 break;
1764 }
1765#endif
1766 return extensions;
1767}
1768
1770{
1771 bool use_vk_ext_swapchain_colorspace = false;
1772#if defined(WITH_GHOST_SDL)
1773 const bool use_window_surface = (sdl_window_ != nullptr);
1774#elif defined(_WIN32)
1775 const bool use_window_surface = (hwnd_ != nullptr);
1776#elif defined(__APPLE__)
1777 const bool use_window_surface = (metal_layer_ != nullptr);
1778#else /* UNIX/Linux */
1779 bool use_window_surface = false;
1780 switch (platform_) {
1781# ifdef WITH_GHOST_X11
1782 case GHOST_kVulkanPlatformX11:
1783 use_window_surface = (display_ != nullptr) && (window_ != (Window) nullptr);
1784 break;
1785# endif
1786# ifdef WITH_GHOST_WAYLAND
1787 case GHOST_kVulkanPlatformWayland:
1788 use_window_surface = (wayland_display_ != nullptr) && (wayland_surface_ != nullptr);
1789 break;
1790# endif
1792 use_window_surface = false;
1793 break;
1794 }
1795#endif
1796
1797 blender::Vector<const char *> required_device_extensions;
1798 blender::Vector<const char *> optional_device_extensions;
1799
1800 /* Initialize VkInstance */
1801 if (!vulkan_instance.has_value()) {
1802 vulkan_instance.emplace();
1803 GHOST_InstanceVK &instance_vk = vulkan_instance.value();
1804 instance_vk.extensions.enable(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, true);
1805 instance_vk.extensions.enable(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
1806
1807 /* Some XR platforms load functions without knowing if they were replaced by a core
1808 * function. Monado for example always uses the extension functions. Due to maintenance changes
1809 * drivers now only return the function pointer when the extension is enabled.
1810 *
1811 * We work around this by requesting Vulkan promoted extensions.
1812 */
1813#ifdef WITH_XR_OPENXR
1814 /* Vulkan 1.1 promoted instance extensions, enabled for OpenXR usage. */
1815 instance_vk.extensions.enable(VK_KHR_EXTERNAL_FENCE_CAPABILITIES_EXTENSION_NAME);
1816 instance_vk.extensions.enable(VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME);
1817 instance_vk.extensions.enable(VK_KHR_EXTERNAL_SEMAPHORE_CAPABILITIES_EXTENSION_NAME);
1818
1819 /* SteamVR requests both NVIDIA and KHR rectified extension. */
1820 instance_vk.extensions.enable(VK_NV_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME, true);
1821
1822 /* Has been promoted to VK_EXT_debug_utils. */
1823 instance_vk.extensions.enable(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, true);
1824#endif
1825
1826 if (use_window_surface) {
1827 const std::vector<const char *> surface_extensions = getPlatformSpecificSurfaceExtensions();
1828 instance_vk.extensions.enable(VK_KHR_SURFACE_EXTENSION_NAME);
1829
1830 for (const char *extension : surface_extensions) {
1831 if (!instance_vk.extensions.is_enabled(extension)) {
1832 instance_vk.extensions.enable(extension);
1833 }
1834 }
1835 /* X11 doesn't use the correct swapchain offset, flipping can squash the first frames. */
1836 const bool use_vk_ext_swapchain_maintenance1 =
1837#ifdef WITH_GHOST_X11
1838 platform_ != GHOST_kVulkanPlatformX11 &&
1839#endif
1840 instance_vk.extensions.is_supported(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME) &&
1841 instance_vk.extensions.is_supported(VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME);
1842 if (use_vk_ext_swapchain_maintenance1) {
1843 instance_vk.extensions.enable(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME);
1844 instance_vk.extensions.enable(VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME);
1845 optional_device_extensions.append(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME);
1846 }
1847
1848 use_vk_ext_swapchain_colorspace = instance_vk.extensions.enable(
1849 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME, true);
1850
1851 required_device_extensions.append(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
1852 }
1853
1854 if (!instance_vk.create_instance(
1855 VK_MAKE_VERSION(context_major_version_, context_minor_version_, 0)))
1856 {
1857 vulkan_instance.reset();
1858 return GHOST_kFailure;
1859 }
1860 volkLoadInstanceOnly(instance_vk.vk_instance);
1861 }
1862 GHOST_InstanceVK &instance_vk = vulkan_instance.value();
1863
1864 /* Initialize VkSurface */
1865 if (use_window_surface) {
1866#if defined(WITH_GHOST_SDL)
1867 if (!SDL_Vulkan_CreateSurface(sdl_window_, instance_vk.vk_instance, nullptr, &surface_)) {
1868 CLOG_ERROR(&LOG, "SDL_Vulkan_CreateSurface failed: %s", SDL_GetError());
1869 return GHOST_kFailure;
1870 }
1871#elif defined(_WIN32)
1872 VkWin32SurfaceCreateInfoKHR surface_create_info = {};
1873 surface_create_info.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
1874 surface_create_info.hinstance = GetModuleHandle(nullptr);
1875 surface_create_info.hwnd = hwnd_;
1876 VK_CHECK(volk::vkCreateWin32SurfaceKHR(
1877 instance_vk.vk_instance, &surface_create_info, nullptr, &surface_),
1879#elif defined(__APPLE__)
1880 VkMetalSurfaceCreateInfoEXT info = {};
1881 info.sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT;
1882 info.pNext = nullptr;
1883 info.flags = 0;
1884 info.pLayer = static_cast<CAMetalLayer *>(metal_layer_);
1885 VK_CHECK(volk::vkCreateMetalSurfaceEXT(instance_vk.vk_instance, &info, nullptr, &surface_),
1887#else
1888 switch (platform_) {
1889# ifdef WITH_GHOST_X11
1890 case GHOST_kVulkanPlatformX11: {
1891 VkXlibSurfaceCreateInfoKHR surface_create_info = {};
1892 surface_create_info.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
1893 surface_create_info.dpy = display_;
1894 surface_create_info.window = window_;
1895 VK_CHECK(volk::vkCreateXlibSurfaceKHR(
1896 instance_vk.vk_instance, &surface_create_info, nullptr, &surface_),
1898 break;
1899 }
1900# endif
1901# ifdef WITH_GHOST_WAYLAND
1902 case GHOST_kVulkanPlatformWayland: {
1903 VkWaylandSurfaceCreateInfoKHR surface_create_info = {};
1904 surface_create_info.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
1905 surface_create_info.display = wayland_display_;
1906 surface_create_info.surface = wayland_surface_;
1907 VK_CHECK(vkCreateWaylandSurfaceKHR(
1908 instance_vk.vk_instance, &surface_create_info, nullptr, &surface_),
1910 break;
1911 }
1912# endif
1914 surface_ = VK_NULL_HANDLE;
1915 break;
1916 }
1917 }
1918
1919#endif
1920 }
1921
1922 /* Initialize VkDevice */
1923 if (!vulkan_instance->device.has_value()) {
1924 /* External memory extensions. */
1925#ifdef _WIN32
1926 optional_device_extensions.append(VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME);
1927#elif defined(__APPLE__)
1928#else /* Linux */
1929 optional_device_extensions.append(VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME);
1930#endif
1931
1932 optional_device_extensions.append(VK_EXT_PROVOKING_VERTEX_EXTENSION_NAME);
1933 required_device_extensions.append(VK_KHR_DYNAMIC_RENDERING_EXTENSION_NAME);
1934 required_device_extensions.append(VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME);
1935 required_device_extensions.append(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME);
1936 required_device_extensions.append(VK_KHR_DEPTH_STENCIL_RESOLVE_EXTENSION_NAME);
1937 required_device_extensions.append(VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME);
1938 required_device_extensions.append(VK_KHR_SEPARATE_DEPTH_STENCIL_LAYOUTS_EXTENSION_NAME);
1939 optional_device_extensions.append(VK_KHR_DYNAMIC_RENDERING_LOCAL_READ_EXTENSION_NAME);
1940 optional_device_extensions.append(VK_EXT_DYNAMIC_RENDERING_UNUSED_ATTACHMENTS_EXTENSION_NAME);
1941 optional_device_extensions.append(VK_EXT_SHADER_STENCIL_EXPORT_EXTENSION_NAME);
1942 optional_device_extensions.append(VK_EXT_SHADER_VIEWPORT_INDEX_LAYER_EXTENSION_NAME);
1943 optional_device_extensions.append(VK_KHR_MAINTENANCE_4_EXTENSION_NAME);
1944 optional_device_extensions.append(VK_KHR_FRAGMENT_SHADER_BARYCENTRIC_EXTENSION_NAME);
1945 optional_device_extensions.append(VK_EXT_ROBUSTNESS_2_EXTENSION_NAME);
1946 optional_device_extensions.append(VK_KHR_SYNCHRONIZATION_2_EXTENSION_NAME);
1947 optional_device_extensions.append(VK_EXT_MEMORY_PRIORITY_EXTENSION_NAME);
1948 optional_device_extensions.append(VK_EXT_PAGEABLE_DEVICE_LOCAL_MEMORY_EXTENSION_NAME);
1949 optional_device_extensions.append(VK_KHR_PIPELINE_LIBRARY_EXTENSION_NAME);
1950 optional_device_extensions.append(VK_EXT_GRAPHICS_PIPELINE_LIBRARY_EXTENSION_NAME);
1951 optional_device_extensions.append(VK_EXT_LINE_RASTERIZATION_EXTENSION_NAME);
1952 /* Disabled as the extension is available, but without any features set. */
1953#ifndef __APPLE__
1954 optional_device_extensions.append(VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME);
1955#endif
1956 optional_device_extensions.append(VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
1957 optional_device_extensions.append(VK_KHR_COPY_COMMANDS_2_EXTENSION_NAME);
1958 optional_device_extensions.append(VK_KHR_FORMAT_FEATURE_FLAGS_2_EXTENSION_NAME);
1959#if 0
1960 /* VK_EXT_host_image_copy isn't supported by Renderdoc and also isn't working as expected. */
1961 optional_device_extensions.append(VK_EXT_HOST_IMAGE_COPY_EXTENSION_NAME);
1962#endif
1963
1964#ifdef WITH_XR_OPENXR
1965 optional_device_extensions.extend({
1966# ifdef _WIN32
1967 VK_KHR_EXTERNAL_FENCE_WIN32_EXTENSION_NAME,
1968 VK_KHR_EXTERNAL_SEMAPHORE_WIN32_EXTENSION_NAME,
1969
1970 VK_KHR_WIN32_KEYED_MUTEX_EXTENSION_NAME,
1971# elif defined(__APPLE__)
1972# else
1973 VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME,
1974 VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME,
1975# endif
1976 /* Vulkan 1.1 promoted device extensions, enabled for OpenXR usage. */
1977 VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME,
1978 VK_KHR_BIND_MEMORY_2_EXTENSION_NAME,
1979 VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME,
1980 VK_KHR_EXTERNAL_FENCE_EXTENSION_NAME,
1981 VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
1982 VK_KHR_EXTERNAL_SEMAPHORE_EXTENSION_NAME,
1983 VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
1984 VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME,
1985 VK_KHR_MULTIVIEW_EXTENSION_NAME,
1986 VK_KHR_MAINTENANCE_1_EXTENSION_NAME,
1987 VK_KHR_MAINTENANCE_2_EXTENSION_NAME,
1988
1989 /* Vulkan 1.2 promoted device extensions, enabled for OpenXR usage. */
1990 VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME,
1991 VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME,
1992
1993 /* Vulkan 1.3 promoted device extensions, enabled for OpenXR usage. */
1994 VK_EXT_PIPELINE_CREATION_CACHE_CONTROL_EXTENSION_NAME,
1995
1996 /* Vulkan 1.4 promoted device extensions, enabled for OpenXR usage. */
1997 VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME,
1998
1999 /* Has been promoted to VK_EXT_debug_utils */
2000 VK_EXT_DEBUG_MARKER_EXTENSION_NAME});
2001#endif
2002
2003 optional_device_extensions.append(VK_KHR_RAY_QUERY_EXTENSION_NAME);
2004 optional_device_extensions.append(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME);
2005 optional_device_extensions.append(VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME);
2006 optional_device_extensions.append(VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME);
2007 optional_device_extensions.append(VK_KHR_SPIRV_1_4_EXTENSION_NAME);
2008 optional_device_extensions.append(VK_KHR_SHADER_FLOAT_CONTROLS_EXTENSION_NAME);
2009
2010 if (!instance_vk.select_physical_device(preferred_device_, required_device_extensions)) {
2011 return GHOST_kFailure;
2012 }
2013
2014 if (!instance_vk.create_device(use_vk_ext_swapchain_colorspace,
2017 required_device_extensions,
2018 optional_device_extensions))
2019 {
2020 return GHOST_kFailure;
2021 }
2022 }
2023 GHOST_DeviceVK &device_vk = instance_vk.device.value();
2024
2025 device_vk.users++;
2026
2027 render_extent_ = {0, 0};
2028 render_extent_min_ = {0, 0};
2029 surface_format_ = {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR};
2030
2031 active_context_ = this;
2032 return GHOST_kSuccess;
2033}
2034
2039
2040#ifdef WITH_GHOST_WAYLAND
2041GHOST_TSuccess GHOST_ContextVK::supportsWaylandColorManagement()
2042{
2043 if (!vulkan_instance.has_value()) {
2044 return GHOST_kFailure;
2045 }
2046 if (!vulkan_instance->device.has_value()) {
2047 return GHOST_kFailure;
2048 }
2049
2050 GHOST_DeviceVK &device_vk = vulkan_instance->device.value();
2051 if (device_vk.device_driver_properties.driverID != VK_DRIVER_ID_NVIDIA_PROPRIETARY) {
2052 return GHOST_kSuccess;
2053 }
2054
2055 uint32_t driver_version = device_vk.properties.properties.driverVersion;
2056 uint32_t major_version = (driver_version >> 22) & 0x3ff;
2057 /* NVIDIA has a well known implementation of wayland color management protocol since driver
2058 * version 595. Using the color management protocol leads to very bright output when using on
2059 * older driver. The output isn't influenced by surface configuration.
2060 */
2061 if (major_version < 595) {
2062 return GHOST_kFailure;
2063 }
2064
2065 return GHOST_kSuccess;
2066}
2067#endif
#define UINT64_MAX
#define UINT32_MAX
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
@ CLG_LEVEL_ERROR
Definition CLG_log.h:56
@ CLG_LEVEL_TRACE
Definition CLG_log.h:64
#define CLOG_AT_LEVEL(clg_ref, verbose_level,...)
Definition CLG_log.h:150
#define CLOG_TRACE(clg_ref,...)
Definition CLG_log.h:192
static bool selectSurfaceFormat(const VkPhysicalDevice physical_device, const VkSurfaceKHR surface, bool use_hdr_swapchain, VkSurfaceFormatKHR &r_surfaceFormat)
static std::optional< GHOST_InstanceVK > vulkan_instance
#define VK_CHECK(__expression, fail_value)
static const char * to_string_vk_present_mode(VkPresentModeKHR present_mode)
static const char * to_string_vk_color_space(VkColorSpaceKHR color_space)
static const char * to_string_vk_format(VkFormat format)
static GHOST_TSuccess selectPresentMode(const GHOST_TVSyncModes vsync, VkPhysicalDevice device, VkSurfaceKHR surface, VkPresentModeKHR *r_presentMode)
static CLG_LogRef LOG
#define Window
GHOST_TVulkanPlatformType
@ GHOST_kVulkanPlatformHeadless
#define wl_display
#define Display
#define wl_surface
GHOST_TSuccess
@ GHOST_kFailure
@ GHOST_kSuccess
GHOST_TVSyncModes
@ GHOST_kVSyncModeUnset
@ GHOST_kVSyncModeOff
volatile int lock
#define STRINGIFY(x)
#define STRINGIFY_ARG(x)
#define ELEM(...)
#define STREQ(a, b)
unsigned long long int uint64_t
GHOST_TSuccess swapBufferRelease() override
GHOST_TSuccess activateDrawingContext() override
GHOST_TSuccess getVulkanSwapChainFormat(GHOST_VulkanSwapChainData *r_swap_chain_data) override
GHOST_ContextVK(const GHOST_ContextParams &context_params, GHOST_TVulkanPlatformType platform, Window window, Display *display, wl_surface *wayland_surface, wl_display *wayland_display, const GHOST_ContextVK_WindowInfo *wayland_window_info, int contextMajorVersion, int contextMinorVersion, const GHOST_GPUDevice &preferred_device, const GHOST_WindowHDRInfo *hdr_info_=nullptr)
GHOST_TSuccess initializeDrawingContext() override
GHOST_TSuccess getVulkanHandles(GHOST_VulkanHandles &r_handles) override
static bool is_instance_extension_enabled(blender::StringRefNull extension_name)
Is the given extension name enabled on instance level?
GHOST_TSuccess releaseDrawingContext() override
GHOST_TSuccess swapBufferAcquire() override
GHOST_TSuccess releaseNativeHandles() override
~GHOST_ContextVK() override
GHOST_TSuccess setVulkanSwapBuffersCallbacks(std::function< void(const GHOST_VulkanSwapChainData *, bool)> swap_buffer_draw_callback, std::function< void(void)> swap_buffer_acquired_callback, std::function< void(GHOST_VulkanOpenXRData *)> openxr_acquire_framebuffer_image_callback, std::function< void(GHOST_VulkanOpenXRData *)> openxr_release_framebuffer_image_callback) override
static bool is_device_extension_enabled(blender::StringRefNull extension_name)
Is the given extension name enabled on device level?
GHOST_Context(const GHOST_ContextParams &context_params)
static GHOST_Context * active_context_
virtual GHOST_TVSyncModes getVSync()
GHOST_ContextParams context_params_
VkPhysicalDevice vk_physical_device
VkPhysicalDeviceDriverProperties device_driver_properties
VmaAllocator vma_allocator
void init_generic_queue_family()
VkPhysicalDeviceTimelineSemaphoreFeatures features_timeline_semaphore
VkPhysicalDeviceRobustness2FeaturesEXT features_robustness2
bool use_vk_ext_swapchain_maintenance_1
VolkDeviceTable functions
void init_memory_allocator(VkInstance vk_instance)
VkPhysicalDeviceFeatures2 features
uint32_t generic_queue_family
VkPhysicalDeviceAccelerationStructureFeaturesKHR features_acceleration_structure
VkPhysicalDeviceProperties2 properties
GHOST_ExtensionsVK extensions
std::mutex queue_mutex
VkPhysicalDeviceBufferDeviceAddressFeatures features_buffer_device_address
bool use_vk_ext_swapchain_colorspace
VkPhysicalDeviceVulkan11Features features_11
GHOST_DeviceVK(VkPhysicalDevice vk_physical_device, const bool use_vk_ext_swapchain_colorspace)
constexpr const char * c_str() const
int64_t size() const
void append(const T &value)
void remove(const int64_t index)
void resize(const int64_t new_size)
int64_t first_index_of(const T &value) const
void extend(Span< T > array)
#define assert(assertion)
#define acceleration_structure(slot)
#define index(i)
#define PRIx64
Definition inttypes.h:133
#define PRIu64
Definition inttypes.h:132
if(state< num_states)
#define VK_FORMAT_R8G8B8A8_UNORM
#define LOG(level)
Definition log.h:97
const char * to_string(ShaderStage stage)
Definition mtl_shader.mm:52
bool GPU_vulkan_is_supported_driver(VkPhysicalDevice vk_physical_device)
Definition vk_backend.cc:55
unsigned int uint32_t
Definition BKE_text.h:81
blender::Vector< VkExtensionProperties > extensions
bool is_supported(const char *extension_name) const
bool is_enabled(const char *extension_name) const
bool enable(const char *extension_name, bool optional=false)
blender::Vector< const char * > enabled
bool disable(const char *extension_name)
bool enable(const blender::Span< const char * > &extension_names, bool optional=false)
bool is_supported(blender::Span< const char * > extension_names)
void destroy(VkDevice vk_device, const volk::VolkDeviceTable &functions)
std::vector< VkSwapchainKHR > swapchains
std::vector< VkSemaphore > semaphores
VkFence submission_fence
VkSemaphore acquire_semaphore
GHOST_FrameDiscard discard_pile
void destroy(VkDevice vk_device, const volk::VolkDeviceTable &functions)
bool fail_on_invalid_override
VkPhysicalDevice vk_physical_device
std::optional< GHOST_DeviceVK > device
bool select_physical_device(const GHOST_GPUDevice &preferred_device, const blender::Span< const char * > required_extensions)
GHOST_ExtensionsVK extensions
bool create_instance(uint32_t vulkan_api_version)
bool create_device(const bool use_vk_ext_swapchain_maintenance1, const bool is_debug, const bool no_hw_rt, blender::Span< const char * > required_device_extensions, blender::Span< const char * > optional_device_extensions)
void destroy(VkDevice vk_device, const volk::VolkDeviceTable &functions)
VkSemaphore present_semaphore