Blender V5.3
vk_backend.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cstdio>
11#include <sstream>
12
13#include "BLI_path_utils.hh"
14#include "BLI_string.hh"
15#include "BLI_threads.hh"
16
17#include "CLG_log.h"
18
19#include "GPU_capabilities.hh"
20#include "GPU_framebuffer.hh"
23
24#include "vk_batch.hh"
25#include "vk_context.hh"
26#include "vk_fence.hh"
27#include "vk_framebuffer.hh"
28#include "vk_ghost_api.hh"
29#include "vk_index_buffer.hh"
30#include "vk_pixel_buffer.hh"
31#include "vk_query.hh"
32#include "vk_ray_tracing.hh"
33#include "vk_shader.hh"
34#include "vk_state_manager.hh"
35#include "vk_storage_buffer.hh"
36#include "vk_texture.hh"
37#include "vk_texture_pool.hh"
38#include "vk_uniform_buffer.hh"
39#include "vk_vertex_buffer.hh"
40#include "vk_work_in_flight.hh"
41
42#include "vk_backend.hh"
43
44namespace blender {
45
46static CLG_LogRef LOG = {"gpu.vulkan"};
47
48namespace gpu {
49
50static const char *vk_extension_get(int index)
51{
53}
54
55bool GPU_vulkan_is_supported_driver(VkPhysicalDevice vk_physical_device)
56{
57 /* Check for known faulty drivers. */
58 VkPhysicalDeviceProperties2 vk_physical_device_properties = {};
59 VkPhysicalDeviceDriverProperties vk_physical_device_driver_properties = {};
60 vk_physical_device_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
61 vk_physical_device_driver_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES;
62 vk_physical_device_properties.pNext = &vk_physical_device_driver_properties;
63 vkGetPhysicalDeviceProperties2(vk_physical_device, &vk_physical_device_properties);
64
65#ifdef _WIN32
66 /* Intel IRIS on 10th gen CPU (and older) crashes with drivers before 101.2140 due to multiple
67 * driver issues.
68 *
69 * 1) Workbench is working, but EEVEE pipelines are failing. Calling vkCreateGraphicsPipelines
70 * for certain EEVEE shaders (Shadow, Deferred rendering) would return with VK_SUCCESS, but
71 * without a created VkPipeline handle.
72 *
73 * 2) When vkCmdBeginRendering is called some requirements need to be met, that can only be met
74 * when actually calling a vkCmdDraw* command. According to the Vulkan specs the requirements
75 * should only be met when calling a vkCmdDraw* command.
76 */
77 if (vk_physical_device_driver_properties.driverID == VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS &&
78 vk_physical_device_properties.properties.deviceType ==
79 VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU)
80 {
81 const uint32_t driver_version = vk_physical_device_properties.properties.driverVersion;
82 uint32_t driver_version_major = driver_version >> 14u;
83 uint32_t driver_version_minor = driver_version & 0x3fffu;
84 if (driver_version_major < 101 || (driver_version_major == 101 && driver_version_minor < 2140))
85 {
86 return false;
87 }
88 }
89#endif
90
91#ifndef _WIN32
92 /* NVIDIA drivers below 550 don't work on Linux. When sending command to the GPU there is not
93 * always a reply back when they are finished. The issue is reported on the Internet many times,
94 * but there is no mention of a solution. This means that on Linux we can only support GTX900 and
95 * or use MesaNVK.
96 */
97 uint32_t conformance_version = VK_MAKE_API_VERSION(
98 vk_physical_device_driver_properties.conformanceVersion.major,
99 vk_physical_device_driver_properties.conformanceVersion.minor,
100 vk_physical_device_driver_properties.conformanceVersion.subminor,
101 vk_physical_device_driver_properties.conformanceVersion.patch);
102 if (vk_physical_device_driver_properties.driverID == VK_DRIVER_ID_NVIDIA_PROPRIETARY &&
103 conformance_version < VK_MAKE_API_VERSION(1, 3, 7, 2))
104 {
105 return false;
106 }
107
108 /* NVIDIA driver 580.76.05 doesn't start using specific Wayland configurations #144625. There are
109 * multiple reports also not Blender related and NVIDIA mentions that a new driver will be
110 * released. It is unclear if that driver will fix our issue. For now disabling this driver on
111 * Linux. This also disables it for configurations that are working as well (including X11). */
112 if (vk_physical_device_driver_properties.driverID == VK_DRIVER_ID_NVIDIA_PROPRIETARY &&
113 ((StringRefNull(vk_physical_device_driver_properties.driverInfo).find("580.76.5", 0) !=
115 (StringRefNull(vk_physical_device_driver_properties.driverInfo).find("580.76.05", 0) !=
117 {
118 return false;
119 }
120#endif
121
122#ifdef _WIN32
123 if (vk_physical_device_driver_properties.driverID == VK_DRIVER_ID_QUALCOMM_PROPRIETARY) {
124 /* Any Qualcomm driver older than 31.0.112.0 will not be capable of running blender due
125 * to an issue in their semaphore timeline implementation. The driver could return
126 * timelines that have not been provided by Blender. As Blender uses timelines for resource
127 * management this resulted in resources to be destroyed, that are still in use. */
128
129 /* Public version 31.0.112 uses vulkan driver version 512.827.0. */
130 const uint32_t driver_version = vk_physical_device_properties.properties.driverVersion;
131 constexpr uint32_t version_31_0_112 = VK_MAKE_VERSION(512, 827, 0);
132 if (driver_version < version_31_0_112) {
133 CLOG_WARN(&LOG,
134 "Detected qualcomm driver is not supported. To run the Vulkan backend "
135 "driver 31.0.112.0 or later is required. Switching to OpenGL.");
136 return false;
137 }
138 }
139#endif
140
141 return true;
142}
143
144static Vector<StringRefNull> missing_capabilities_get(VkPhysicalDevice vk_physical_device)
145{
146 Vector<StringRefNull> missing_capabilities;
147
148 /* Check device extensions. */
149 uint32_t vk_extension_count;
150 vkEnumerateDeviceExtensionProperties(vk_physical_device, nullptr, &vk_extension_count, nullptr);
151
152 Array<VkExtensionProperties> vk_extensions(vk_extension_count);
153 vkEnumerateDeviceExtensionProperties(
154 vk_physical_device, nullptr, &vk_extension_count, vk_extensions.data());
155 Set<StringRefNull> extensions;
156 for (VkExtensionProperties &vk_extension : vk_extensions) {
157 extensions.add(vk_extension.extensionName);
158 }
159
160 if (!extensions.contains(VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
161 missing_capabilities.append(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
162 }
163 if (!extensions.contains(VK_KHR_DYNAMIC_RENDERING_EXTENSION_NAME)) {
164 missing_capabilities.append(VK_KHR_DYNAMIC_RENDERING_EXTENSION_NAME);
165 }
166 if (!extensions.contains(VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME)) {
167 missing_capabilities.append(VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME);
168 }
169 if (!extensions.contains(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME)) {
170 missing_capabilities.append(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME);
171 }
172 if (!extensions.contains(VK_KHR_SEPARATE_DEPTH_STENCIL_LAYOUTS_EXTENSION_NAME)) {
173 missing_capabilities.append(VK_KHR_SEPARATE_DEPTH_STENCIL_LAYOUTS_EXTENSION_NAME);
174 }
175
176 /* Check device features. */
177 VkPhysicalDeviceFeatures2 features = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2};
178 vkGetPhysicalDeviceFeatures2(vk_physical_device, &features);
179
180#ifndef __APPLE__
181 /* Features currently not supported by Mesa KosmicKrisp. */
182 if (features.features.geometryShader == VK_FALSE) {
183 missing_capabilities.append("geometry shaders");
184 }
185#endif
186 if (features.features.fragmentStoresAndAtomics == VK_FALSE) {
187 missing_capabilities.append("fragment stores and atomics");
188 }
189 if (features.features.dualSrcBlend == VK_FALSE) {
190 missing_capabilities.append("dual source blending");
191 }
192 if (features.features.imageCubeArray == VK_FALSE) {
193 missing_capabilities.append("image cube array");
194 }
195 if (features.features.drawIndirectFirstInstance == VK_FALSE) {
196 missing_capabilities.append("draw indirect first instance");
197 }
198
199 return missing_capabilities;
200}
201
212{
213 std::stringstream allowed_layers;
214 allowed_layers << "VK_LAYER_KHRONOS_*";
215 allowed_layers << ",VK_LAYER_AMD_*";
216 allowed_layers << ",VK_LAYER_INTEL_*";
217 allowed_layers << ",VK_LAYER_NV_*";
218 allowed_layers << ",VK_LAYER_MESA_*";
219 if (bool(G.debug & G_DEBUG_GPU)) {
220 allowed_layers << ",VK_LAYER_LUNARG_*";
221 allowed_layers << ",VK_LAYER_RENDERDOC_*";
222 }
223 BLI_setenv("VK_LOADER_LAYERS_DISABLE", "~implicit~");
224 BLI_setenv("VK_LOADER_LAYERS_ALLOW", allowed_layers.str().c_str());
225}
226
227static bool vk_instance_create_for_platform_checks(VkInstance *r_instance)
228{
230
231 VkResult vk_result = volkInitialize();
232 if (vk_result != VK_SUCCESS) {
234 "Error initializing Vulkan loader: VkResult=%d, most likely cannot find the Vulkan "
235 "Loader provided by GPU driver/OS.",
236 vk_result);
237 return false;
238 }
239
240 /* Initialize an vulkan 1.1 instance. */
241 VkApplicationInfo vk_application_info = {VK_STRUCTURE_TYPE_APPLICATION_INFO};
242 vk_application_info.pApplicationName = "Blender";
243 vk_application_info.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
244 vk_application_info.pEngineName = "Blender";
245 vk_application_info.engineVersion = VK_MAKE_VERSION(1, 0, 0);
246 vk_application_info.apiVersion = VK_API_VERSION_1_1;
247
248 const char *vk_instance_extensions[] = {
249 VK_KHR_SURFACE_EXTENSION_NAME, /* Required dependency for VK_KHR_swapchain. */
250 };
251
252 VkInstanceCreateInfo vk_instance_info = {VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO};
253 vk_instance_info.pApplicationInfo = &vk_application_info;
254 vk_instance_info.ppEnabledExtensionNames = vk_instance_extensions;
255 vk_instance_info.enabledExtensionCount = 1;
256
257 *r_instance = VK_NULL_HANDLE;
258 vkCreateInstance(&vk_instance_info, nullptr, r_instance);
259
260 return *r_instance != VK_NULL_HANDLE;
261}
262
264{
266
267 VkInstance vk_instance = VK_NULL_HANDLE;
268 if (!vk_instance_create_for_platform_checks(&vk_instance)) {
269 CLOG_WARN(&LOG, "Unable to initialize a Vulkan 1.1 instance.");
270 return false;
271 }
272 volkLoadInstanceOnly(vk_instance);
273
274 /* Go over all the devices. */
275 uint32_t physical_devices_count = 0;
276 vkEnumeratePhysicalDevices(vk_instance, &physical_devices_count, nullptr);
277 Array<VkPhysicalDevice> vk_physical_devices(physical_devices_count);
278 vkEnumeratePhysicalDevices(vk_instance, &physical_devices_count, vk_physical_devices.data());
279
280 for (VkPhysicalDevice vk_physical_device : vk_physical_devices) {
281 VkPhysicalDeviceProperties vk_properties = {};
282 vkGetPhysicalDeviceProperties(vk_physical_device, &vk_properties);
283
284 if (!GPU_vulkan_is_supported_driver(vk_physical_device)) {
285 CLOG_WARN(&LOG,
286 "Installed driver for device [%s] has known issues and will not be used. Updating "
287 "driver might improve compatibility.",
288 vk_properties.deviceName);
289 continue;
290 }
291 Vector<StringRefNull> missing_capabilities = missing_capabilities_get(vk_physical_device);
292
293 /* Report result. */
294 if (missing_capabilities.is_empty()) {
295 /* This device meets minimum requirements. */
297 &LOG,
298 "Device [%s] supports minimum requirements. Skip checking other GPUs. Another GPU "
299 "can still be selected during auto-detection.",
300 vk_properties.deviceName);
301
302 vkDestroyInstance(vk_instance, nullptr);
303 return true;
304 }
305
306 std::stringstream ss;
307 ss << "Device [" << vk_properties.deviceName
308 << "] does not meet minimum requirements. Missing features are [";
309 for (StringRefNull &feature : missing_capabilities) {
310 ss << feature << ", ";
311 }
312 ss.seekp(-2, std::ios_base::end);
313 ss << "]";
314 CLOG_WARN(&LOG, "%s", ss.str().c_str());
315 }
316
317 /* No device found meeting the minimum requirements. */
318
319 vkDestroyInstance(vk_instance, nullptr);
321 "No Vulkan device found that meets the minimum requirements. "
322 "Updating GPU driver can improve compatibility.");
323 return false;
324}
325
327{
329
330 VkInstance vk_instance = VK_NULL_HANDLE;
331 if (!vk_instance_create_for_platform_checks(&vk_instance)) {
332 fprintf(fp, "Unable to initialize a Vulkan 1.2 instance.\n");
333 return;
334 }
335 volkLoadInstanceOnly(vk_instance);
336
337 uint32_t physical_devices_count = 0;
338 vkEnumeratePhysicalDevices(vk_instance, &physical_devices_count, nullptr);
339 Array<VkPhysicalDevice> vk_physical_devices(physical_devices_count);
340 vkEnumeratePhysicalDevices(vk_instance, &physical_devices_count, vk_physical_devices.data());
341
342 struct Row {
343 int index;
344 std::string identifier;
345 std::string name;
346 };
347 Vector<Row> rows;
348 int index = 0;
349 for (VkPhysicalDevice vk_physical_device : vk_physical_devices) {
350 if (missing_capabilities_get(vk_physical_device).is_empty() &&
351 GPU_vulkan_is_supported_driver(vk_physical_device))
352 {
353 VkPhysicalDeviceProperties vk_properties = {};
354 vkGetPhysicalDeviceProperties(vk_physical_device, &vk_properties);
355 std::stringstream identifier;
356 identifier << std::hex << vk_properties.vendorID << "/" << vk_properties.deviceID << "/"
357 << index;
358 rows.append({index, identifier.str(), std::string(vk_properties.deviceName)});
359 }
360 index++;
361 }
362
363 if (rows.is_empty()) {
364 fprintf(fp, " (no supported Vulkan devices found)\n");
365 vkDestroyInstance(vk_instance, nullptr);
366 return;
367 }
368
369 const char *col_index = "Index";
370 const char *col_id = "Device-ID";
371 const char *col_name = "Name";
372 size_t w_index = strlen(col_index);
373 size_t w_id = strlen(col_id);
374 for (const Row &row : rows) {
375 char buf[16];
376 w_index = std::max(w_index, SNPRINTF_RLEN(buf, "%d", row.index));
377 w_id = std::max(w_id, row.identifier.size());
378 }
379
380 fprintf(fp, " %-*s %-*s %s\n", int(w_index), col_index, int(w_id), col_id, col_name);
381 for (const Row &row : rows) {
382 fprintf(fp,
383 " %-*d %-*s %s\n",
384 int(w_index),
385 row.index,
386 int(w_id),
387 row.identifier.c_str(),
388 row.name.c_str());
389 }
390
391 vkDestroyInstance(vk_instance, nullptr);
392}
393
395{
396#ifdef _WIN32
397 return GPU_OS_WIN;
398#elif defined(__APPLE__)
399 return GPU_OS_MAC;
400#else
401 return GPU_OS_UNIX;
402#endif
403}
404
406{
412 "",
413 "",
414 "",
416}
417
418static void init_device_list(GHOST_IContext *ghost_context)
419{
420 GHOST_VulkanHandles vulkan_handles = {};
421 ghost_context->getVulkanHandles(vulkan_handles);
422
423 uint32_t physical_devices_count = 0;
424 vkEnumeratePhysicalDevices(vulkan_handles.instance, &physical_devices_count, nullptr);
425 Array<VkPhysicalDevice> vk_physical_devices(physical_devices_count);
426 vkEnumeratePhysicalDevices(
427 vulkan_handles.instance, &physical_devices_count, vk_physical_devices.data());
428 int index = 0;
429 for (VkPhysicalDevice vk_physical_device : vk_physical_devices) {
430 if (missing_capabilities_get(vk_physical_device).is_empty() &&
431 GPU_vulkan_is_supported_driver(vk_physical_device))
432 {
433 VkPhysicalDeviceProperties vk_properties = {};
434 vkGetPhysicalDeviceProperties(vk_physical_device, &vk_properties);
435 std::stringstream identifier;
436 identifier << std::hex << vk_properties.vendorID << "/" << vk_properties.deviceID << "/"
437 << index;
438 GPG.devices.append({identifier.str(),
439 index,
440 vk_properties.vendorID,
441 vk_properties.deviceID,
442 std::string(vk_properties.deviceName)});
443 }
444 index++;
445 }
446
447 std::ranges::sort(GPG.devices, [&](const GPUDevice &a, const GPUDevice &b) {
448 if (a.name == b.name) {
449 return a.index < b.index;
450 }
451 return a.name < b.name;
452 });
453}
454
456{
457 const VkPhysicalDeviceProperties &properties = device.physical_device_properties_get();
458
459 GPUDeviceType device_type = device.device_type();
463
464 std::string vendor_name = device.vendor_name();
465 std::string driver_version = device.driver_version();
466
467 /* GPG has already been initialized, but without a specific device. Calling init twice will
468 * clear the list of devices. Making a copy of the device list and set it after initialization to
469 * make sure the list isn't destroyed at this moment, but only when the backend is destroyed. */
470 Vector<GPUDevice> devices = GPG.devices;
471 GPG.init(device_type,
472 os,
473 driver,
474 support_level,
476 vendor_name.c_str(),
477 properties.deviceName,
478 driver_version.c_str(),
480 GPG.devices = devices;
481
482 const VkPhysicalDeviceIDProperties &id_properties = device.physical_device_id_properties_get();
483
484 GPG.device_uuid = Array<uint8_t, 16>(Span<uint8_t>(id_properties.deviceUUID, VK_UUID_SIZE));
485
486 if (id_properties.deviceLUIDValid) {
487 GPG.device_luid = Array<uint8_t, 8>(Span<uint8_t>(id_properties.deviceUUID, VK_LUID_SIZE));
488 GPG.device_luid_node_mask = id_properties.deviceNodeMask;
489 }
490 else {
493 }
494
495 CLOG_INFO(&LOG,
496 "Using vendor [%s] device [%s] driver version [%s].",
497 vendor_name.c_str(),
498 device.vk_physical_device_properties_.deviceName,
499 driver_version.c_str());
500}
501
502void VKBackend::detect_workarounds(VKDevice &device)
503{
504 VKWorkarounds workarounds;
505 VKExtensions extensions;
506
507 if (G.debug & G_DEBUG_GPU_FORCE_WORKAROUNDS) {
508 CLOG_WARN(&LOG, "Forcing workarounds and disabling features and extensions");
509
510 /* Force workarounds and disable extensions. */
511 workarounds.not_aligned_pixel_formats = true;
512 workarounds.static_viewport_scissor = true;
513 extensions.shader_viewport_index_layer = false;
514 extensions.fragment_shader_barycentric = false;
515 extensions.dynamic_rendering_local_read = false;
516 extensions.dynamic_rendering_unused_attachments = false;
517 extensions.pageable_device_local_memory = false;
518 extensions.wide_lines = false;
519 extensions.line_rasterization = false;
520 extensions.extended_dynamic_state = false;
521 extensions.multi_draw_indirect = false;
522 extensions.provoking_vertex = false;
523 extensions.spirv_1_4 = false;
524 GCaps.ray_query_support = false;
529
530 device.workarounds_ = workarounds;
531 device.extensions_ = extensions;
532 return;
533 }
534
535 if (G.debug & G_DEBUG_GPU_NO_TEXTURE_POOL) {
537 }
538
539 /* Some Qualcomm drivers keep command-buffer-local state written by `vkCmdSetViewport` across
540 * command buffer resets. */
542 workarounds.static_viewport_scissor = true;
543 }
544
546 VK_EXT_SHADER_VIEWPORT_INDEX_LAYER_EXTENSION_NAME);
547 extensions.spirv_1_4 = device.supports_extension(VK_KHR_SPIRV_1_4_EXTENSION_NAME);
548 extensions.wide_lines = device.physical_device_features_get().wideLines;
550 VK_KHR_FRAGMENT_SHADER_BARYCENTRIC_EXTENSION_NAME);
552 VK_KHR_DYNAMIC_RENDERING_LOCAL_READ_EXTENSION_NAME);
554 VK_EXT_DYNAMIC_RENDERING_UNUSED_ATTACHMENTS_EXTENSION_NAME);
555
556 extensions.maintenance4 = device.supports_extension(VK_KHR_MAINTENANCE_4_EXTENSION_NAME);
557 extensions.memory_priority = device.supports_extension(VK_EXT_MEMORY_PRIORITY_EXTENSION_NAME);
559 VK_EXT_PAGEABLE_DEVICE_LOCAL_MEMORY_EXTENSION_NAME);
561 VK_EXT_GRAPHICS_PIPELINE_LIBRARY_EXTENSION_NAME);
563 VK_EXT_LINE_RASTERIZATION_EXTENSION_NAME);
565 VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME);
567 VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
568 extensions.multi_draw_indirect = device.physical_device_features_get().multiDrawIndirect ==
569 VK_TRUE;
570 extensions.provoking_vertex = device.supports_extension(VK_EXT_PROVOKING_VERTEX_EXTENSION_NAME);
571#if 0
572 extensions.host_image_copy = device.supports_extension(VK_EXT_HOST_IMAGE_COPY_EXTENSION_NAME);
573#endif
574#ifdef _WIN32
576 VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME);
577#elif not defined(__APPLE__)
578 extensions.external_memory = device.supports_extension(VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME);
579#else
580 extensions.external_memory = false;
581#endif
582
583 /* AMD GPUs don't support texture formats that use are aligned to 24 or 48 bits. */
586 {
587 workarounds.not_aligned_pixel_formats = true;
588 }
589
590 extensions.shader_clip_distance = device.physical_device_features_get().shaderClipDistance ==
591 VK_TRUE;
592
593 /* During testing graphics pipeline library feature it was detected that it would crash on
594 * official AMD drivers.
595 */
597 bool(G.debug & G_DEBUG_GPU))
598 {
599 extensions.graphics_pipeline_library = false;
600 extensions.vertex_input_dynamic_state = false;
601 }
602
603 /* Disable vertex input dynamic state for Qualcomm devices (#153414).
604 *
605 * TODO: We should re-validate vertex input dynamic state as there are multiple vendors with
606 * similar issues. It might be an oversight. Will wait for feedback from the driver developers
607 * and perform some out of bounds error checks.
608 */
610 extensions.vertex_input_dynamic_state = false;
611 }
612
613 /* Only enable by default dynamic rendering local read on Qualcomm devices. NVIDIA, AMD and Intel
614 * performance is better when disabled (20%). On Qualcomm devices the improvement can be
615 * substantial (16% on shader_balls.blend).
616 *
617 * `--debug-gpu-vulkan-local-read` can be used to use dynamic rendering local read on any
618 * supported platform.
619 *
620 * TODO: Check if bottleneck is during command building. If so we could fine-tune this after the
621 * device command building landed (T132682).
622 */
625 {
626 extensions.dynamic_rendering_local_read = false;
627 }
628
629 /* When using host image copy on certain NVIDIA platforms the allocation of textures that only
630 * use GPU_TEXTURE_USAGE_SHADER_READ/WRITE will fail to allocate the memory. This needs some more
631 * research as this might just be a missing flag when allocating. Another solution is to not
632 * allow host_imag_copy when only these two flags are set as the rest seems to work as expected.
633 *
634 * See #151826
635 */
637 extensions.host_image_copy = false;
638 }
639
640#ifdef _WIN32
644
645 if (gpu_arch == GPUIntelGpuArch::Gen9AndOlder) {
646 /* Intel Gen9 iGPUs (Intel 7th to 10th Gen Processor Graphics driver) show a black screen at
647 * application startup when using VK_EXT_vertex_input_dynamic_state.
648 *
649 * See #147721
650 */
651 extensions.vertex_input_dynamic_state = false;
652
653 /* Using the texture pool causes visual corruptions on Gen9 and older iGPUs (Intel 7th to
654 * 10th Gen Processor Graphics driver; #147721). */
656 }
657 }
658#endif
659
660 /* Legacy AMD drivers (#142019) don't perform the sRGB conversion when storing a clear value into
661 * an sRGB attachment, so a linear clear color is stored into the sRGB attachment and the
662 * background then renders too bright. Only official AMD drivers up to version 31.0.24002.92
663 * (Vulkan 2.0.294) are affected. */
665 const uint32_t driver_version = device.physical_device_properties_get().driverVersion;
666 if (driver_version <= VK_MAKE_API_VERSION(2, 0, 294, 0)) {
667 workarounds.missing_srgb_clear_conversion = true;
668 }
669 }
670
671#ifdef __APPLE__
672 extensions.extended_dynamic_state = false;
673#endif
674
675 device.workarounds_ = workarounds;
676 device.extensions_ = extensions;
677}
678
679void VKBackend::platform_exit()
680{
681 GPG.clear();
682 VKDevice &device = VKBackend::get().device;
683 if (device.is_initialized()) {
684 device.deinit();
685 }
686}
687
689{
690 compiler_ = MEM_new<ShaderCompiler>(
692}
693
695{
696 MEM_delete(compiler_);
697}
698
699void VKBackend::compute_dispatch(int groups_x_len, int groups_y_len, int groups_z_len)
700{
701 VKContext &context = *VKContext::get();
702 render_graph::VKResourceAccessInfo &resources = context.reset_and_get_access_info();
703 render_graph::VKDispatchNode::CreateInfo dispatch_info(resources);
704 context.update_pipeline_data(dispatch_info.dispatch_node.pipeline_data);
705 dispatch_info.dispatch_node.group_count_x = groups_x_len;
706 dispatch_info.dispatch_node.group_count_y = groups_y_len;
707 dispatch_info.dispatch_node.group_count_z = groups_z_len;
708 context.render_graph().add_node(dispatch_info);
709}
710
712{
713 BLI_assert(indirect_buf);
714 VKContext &context = *VKContext::get();
715 VKStorageBuffer &indirect_buffer = *unwrap(indirect_buf);
716 render_graph::VKResourceAccessInfo &resources = context.reset_and_get_access_info();
717 render_graph::VKDispatchIndirectNode::CreateInfo dispatch_indirect_info(resources);
718 context.update_pipeline_data(dispatch_indirect_info.dispatch_indirect_node.pipeline_data);
719 dispatch_indirect_info.dispatch_indirect_node.buffer = indirect_buffer.resource();
720 dispatch_indirect_info.dispatch_indirect_node.offset = 0;
721 context.render_graph().add_node(dispatch_indirect_info);
722}
723
725{
726 if (!device.is_initialized()) {
727 device.init(ghost_context);
728 device.extensions_get().log();
729 device.workarounds_get().log();
730 init_device_list(ghost_context);
731 }
732
733 VKContext *context = new VKContext(ghost_window, ghost_context);
734 device.context_register(*context);
735 ghost_context->setVulkanSwapBuffersCallbacks(
740
741 return context;
742}
743
745{
746 return new VKBatch();
747}
748
750{
751 return new VKFence();
752}
753
755{
756 return new VKWorkInFlight(max_in_flight);
757}
758
760{
761 return new VKFrameBuffer(name);
762}
763
765{
766 return new VKIndexBuffer();
767}
768
770{
771 return new VKPixelBuffer(size);
772}
773
775{
776 return new VKQueryPool();
777}
778
780{
781 return new VKShader(name);
782}
783
785{
786 return new VKTexture(name);
787}
788
790{
792 CLOG_TRACE(&LOG, "Using texture pool \"TexturePoolImpl\".");
793 return new TexturePoolImpl();
794 }
795 CLOG_TRACE(&LOG, "Using texture pool \"VKTexturePool\".");
796 return new VKTexturePool();
797}
798
799UniformBuf *VKBackend::uniformbuf_alloc(size_t size, const char *name)
800{
801 return new VKUniformBuffer(size, name);
802}
803
804StorageBuf *VKBackend::storagebuf_alloc(size_t size, GPUUsageType usage, const char *name)
805{
806 return new VKStorageBuffer(size, usage, name);
807}
808
810{
811 return new VKVertexBuffer();
812}
813
815{
816 return new VKTopLevelAS(name);
817}
819{
820 return new VKBottomLevelAS(name);
821}
822
824{
825 VKThreadData &thread_data = device.current_thread_data();
826 BLI_assert_msg(thread_data.rendering_depth >= 0, "Unbalanced `GPU_render_begin/end`");
827 thread_data.rendering_depth += 1;
828}
829
831{
832 VKThreadData &thread_data = device.current_thread_data();
833 thread_data.rendering_depth -= 1;
834 BLI_assert_msg(thread_data.rendering_depth >= 0, "Unbalanced `GPU_render_begin/end`");
835 if (G.background) {
836 if (thread_data.rendering_depth == 0) {
837 VKContext *context = VKContext::get();
838 if (context != nullptr) {
839 context->flush();
840 }
841 std::scoped_lock lock(device.orphaned_data.mutex_get());
842 device.orphaned_data.move_data(device.orphaned_data_render,
843 device.orphaned_data.timeline_ + 1);
844 }
845 }
846
847 /* When performing animation render we want to release any discarded resources during rendering
848 * after each frame.
849 */
850 if (G.is_rendering && thread_data.rendering_depth == 0 && !BLI_thread_is_main()) {
851 std::scoped_lock lock(device.orphaned_data.mutex_get());
852 device.orphaned_data.move_data(device.orphaned_data_render,
853 device.orphaned_data.timeline_ + 1);
854 }
855}
856
857void VKBackend::render_step(bool force_resource_release)
858{
859 if (force_resource_release) {
860 std::scoped_lock lock(device.orphaned_data.mutex_get());
861 device.orphaned_data.move_data(device.orphaned_data_render,
862 device.orphaned_data.timeline_ + 1);
863 }
864}
865
867{
868 const VkPhysicalDeviceProperties &properties = device.physical_device_properties_get();
869 const VkPhysicalDeviceLimits &limits = properties.limits;
870
871 /* Reset all capabilities from previous context. */
872 GCaps = {};
875 VK_EXT_SHADER_STENCIL_EXPORT_EXTENSION_NAME);
877 device.physical_device_features_get().vertexPipelineStoresAndAtomics;
879 device.supports_extension(VK_KHR_RAY_QUERY_EXTENSION_NAME) &&
883 if (G.debug & G_DEBUG_GPU_NO_HW_RAYTRACING) {
884 GCaps.ray_query_support = false;
885 }
886
888 limits.maxViewports >= GPU_MAX_VIEWPORTS;
889
891
892 GCaps.max_texture_size = max_ii(limits.maxImageDimension1D, limits.maxImageDimension2D);
893 GCaps.max_texture_3d_size = min_uu(limits.maxImageDimension3D, INT_MAX);
894 GCaps.max_buffer_texture_size = min_uu(limits.maxTexelBufferElements, UINT_MAX);
895 GCaps.max_texture_layers = min_uu(limits.maxImageArrayLayers, INT_MAX);
896 GCaps.max_textures = min_uu(limits.maxPerStageDescriptorSampledImages, INT_MAX);
897 GCaps.max_images = min_uu(limits.maxPerStageDescriptorStorageImages, INT_MAX);
898 for (int i = 0; i < 3; i++) {
899 GCaps.max_work_group_count[i] = min_uu(limits.maxComputeWorkGroupCount[i], INT_MAX);
900 GCaps.max_work_group_size[i] = min_uu(limits.maxComputeWorkGroupSize[i], INT_MAX);
901 }
903 limits.maxPerStageDescriptorUniformBuffers, INT_MAX);
904 GCaps.max_batch_indices = min_uu(limits.maxDrawIndirectCount, INT_MAX);
905 GCaps.max_batch_vertices = min_uu(limits.maxDrawIndexedIndexValue, INT_MAX);
906 GCaps.max_vertex_attribs = min_uu(limits.maxVertexInputAttributes, INT_MAX);
907 GCaps.max_varying_floats = min_uu(limits.maxVertexOutputComponents, INT_MAX);
909 limits.maxPerStageDescriptorStorageBuffers, INT_MAX);
910 GCaps.max_uniform_buffer_size = size_t(limits.maxUniformBufferRange);
911 GCaps.max_storage_buffer_size = size_t(limits.maxStorageBufferRange);
912 GCaps.storage_buffer_alignment = limits.minStorageBufferOffsetAlignment;
913
916
917 uint32_t vk_extension_count;
918 vkEnumerateDeviceExtensionProperties(
919 device.physical_device_get(), nullptr, &vk_extension_count, nullptr);
920 GCaps.extensions_len = vk_extension_count;
922
923 detect_workarounds(device);
924}
925
926} // namespace gpu
927} // namespace blender
#define SNPRINTF_RLEN(dst, format,...)
#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
void CLG_logref_init(CLG_LogRef *clg_ref)
Definition clog.cc:981
#define CLOG_TRACE(clg_ref,...)
Definition CLG_log.h:192
#define CLOG_INFO(clg_ref,...)
Definition CLG_log.h:190
volatile int lock
#define BLI_assert_msg(a, msg)
const T * data() const
Definition BLI_array.hh:323
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
static constexpr int64_t not_found
constexpr int64_t find(char c, int64_t pos=0) const
ShaderCompiler * compiler_
void init(GPUDeviceType gpu_device, GPUOSType os_type, GPUDriverType driver_type, GPUSupportLevel gpu_support_level, GPUBackendType backend, const char *vendor_str, const char *renderer_str, const char *version_str, GPUArchitectureType arch_type)
void render_step(bool) override
TexturePool * texturepool_alloc() override
TopLevelAS * tlas_alloc(const char *name) override
Batch * batch_alloc() override
StorageBuf * storagebuf_alloc(size_t size, GPUUsageType usage, const char *name) override
static void capabilities_init(VKDevice &device)
static VKBackend & get()
IndexBuf * indexbuf_alloc() override
BottomLevelAS * blas_alloc(const char *name) override
static void platform_init(const VKDevice &device)
QueryPool * querypool_alloc() override
static bool is_supported()
UniformBuf * uniformbuf_alloc(size_t size, const char *name) override
Texture * texture_alloc(const char *name) override
PixelBuffer * pixelbuf_alloc(size_t size) override
void render_begin() override
Shader * shader_alloc(const char *name) override
void render_end() override
FrameBuffer * framebuffer_alloc(const char *name) override
void compute_dispatch(int groups_x_len, int groups_y_len, int groups_z_len) override
void compute_dispatch_indirect(StorageBuf *indirect_buf) override
void delete_resources() override
void init_resources() override
VertBuf * vertbuf_alloc() override
Context * context_alloc(GHOST_IWindow *ghost_window, GHOST_IContext *ghost_context) override
WorkInFlight * work_in_flight_alloc(unsigned int max_in_flight) override
Fence * fence_alloc() override
static void supported_devices_print(FILE *fp)
static void swap_buffer_draw_callback(const GHOST_VulkanSwapChainData *data, bool wait_for_submission)
VKContext(GHOST_IWindow *ghost_window, GHOST_IContext *ghost_context)
Definition vk_context.cc:28
static void openxr_acquire_framebuffer_image_callback(GHOST_VulkanOpenXRData *data)
static void swap_buffer_acquired_callback()
static void openxr_release_framebuffer_image_callback(GHOST_VulkanOpenXRData *data)
static VKContext * get()
const char * extension_name_get(int index) const
Definition vk_device.hh:284
const VkPhysicalDeviceFeatures & physical_device_features_get() const
Definition vk_device.hh:322
GPUDriverType driver_type() const
Definition vk_device.cc:410
bool supports_extension(const char *extension_name) const
Definition vk_device.cc:268
const VkPhysicalDeviceProperties & physical_device_properties_get() const
Definition vk_device.hh:294
std::string vendor_name() const
Definition vk_device.cc:438
std::string driver_version() const
Definition vk_device.cc:465
GPUDeviceType device_type() const
Definition vk_device.cc:378
const VkPhysicalDeviceIDProperties & physical_device_id_properties_get() const
Definition vk_device.hh:305
const VkPhysicalDeviceAccelerationStructurePropertiesKHR & physical_device_acceleration_structure_properties_get()
Definition vk_device.hh:317
VkPhysicalDevice physical_device_get() const
Definition vk_device.hh:289
VKFrameBuffer(const char *name)
VKShader(const char *name)
Definition vk_shader.cc:505
const VKResourceWithHandle< VkBuffer > & resource() const
VKStorageBuffer(size_t size, GPUUsageType usage, const char *name)
VKTexture(const char *name)
Definition vk_texture.hh:81
VKTopLevelAS(const char *name)
VKUniformBuffer(size_t size, const char *name)
VKWorkInFlight(unsigned int max_in_flight)
#define INT_MAX
#define UINT_MAX
Definition hash_md5.cc:46
GPUPlatformGlobal GPG
static CLG_LogRef LOG
bool GPU_vulkan_is_supported_driver(VkPhysicalDevice vk_physical_device)
Definition vk_backend.cc:55
static void vk_restrict_loader_layers()
static bool vk_instance_create_for_platform_checks(VkInstance *r_instance)
BLI_INLINE MTLTexture * unwrap(Texture *tex)
GPUCapabilities GCaps
static GPUOSType determine_os_type()
static void init_device_list(GHOST_IContext *ghost_context)
static const char * vk_extension_get(int index)
Definition vk_backend.cc:50
static Vector< StringRefNull > missing_capabilities_get(VkPhysicalDevice vk_physical_device)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned int uint
Definition BKE_text.h:55
MINLINE int max_ii(int a, int b)
@ GPU_DRIVER_OFFICIAL
void BLI_setenv(const char *env, const char *val) ATTR_NONNULL(1)
@ G_DEBUG_GPU_NO_HW_RAYTRACING
@ G_DEBUG_GPU_NO_TEXTURE_POOL
@ G_DEBUG_GPU_FORCE_VULKAN_LOCAL_READ
@ G_DEBUG_GPU_FORCE_WORKAROUNDS
@ GPU_SUPPORT_LEVEL_SUPPORTED
Global G
Definition blender.cc:57
int BLI_system_thread_count()
Definition threads.cc:258
unsigned int uint32_t
Definition BKE_text.h:81
bool GPU_type_matches(GPUDeviceType device, GPUOSType os, GPUDriverType driver)
@ GPU_ARCHITECTURE_IMR
GPUIntelGpuArch GPU_platform_get_intel_arch(uint32_t device_id)
int BLI_thread_is_main()
Definition threads.cc:184
static CLG_LogRef LOG
Definition logging.cpp:12
int GPU_max_parallel_compilations()
@ GPU_DEVICE_QUALCOMM
@ GPU_DEVICE_INTEL_UHD
@ GPU_DEVICE_INTEL
@ GPU_DEVICE_NVIDIA
@ GPU_DEVICE_APPLE
MINLINE uint min_uu(uint a, uint b)