Blender V5.0
vk_device.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include <atomic>
12
13#include "BLI_task.h"
14#include "BLI_threads.h"
15#include "BLI_utility_mixins.hh"
16#include "BLI_vector.hh"
17
20#include "vk_buffer.hh"
21#include "vk_common.hh"
22#include "vk_debug.hh"
25#include "vk_memory_pool.hh"
26#include "vk_pipeline_pool.hh"
27#include "vk_resource_pool.hh"
28#include "vk_samplers.hh"
29
30namespace blender::gpu {
31class VKBackend;
32
37 bool shader_output_layer = false;
43
48 bool wide_lines = false;
49
54
59
63 bool external_memory = false;
64
66 bool maintenance4 = false;
67
71 bool descriptor_buffer = false;
72
76 bool logic_ops = false;
77
81 bool memory_priority = false;
82
87
89 void log() const;
90};
91
92/* TODO: Split into VKWorkarounds and VKExtensions to remove the negating when an extension isn't
93 * supported. */
102
103 struct {
108 bool r8g8b8 = false;
110};
111
134
135class VKDevice : public NonCopyable {
136 private:
138 VkInstance vk_instance_ = VK_NULL_HANDLE;
139 VkPhysicalDevice vk_physical_device_ = VK_NULL_HANDLE;
140 VkDevice vk_device_ = VK_NULL_HANDLE;
141 uint32_t vk_queue_family_ = 0;
142 VkQueue vk_queue_ = VK_NULL_HANDLE;
143 std::mutex *queue_mutex_ = nullptr;
144
145 bool is_initialized_ = false;
146
154 TaskPool *submission_pool_ = nullptr;
159 ThreadQueue *submitted_render_graphs_ = nullptr;
160 ThreadQueue *unused_render_graphs_ = nullptr;
161 VkSemaphore vk_timeline_semaphore_ = VK_NULL_HANDLE;
167 TimelineValue timeline_value_ = 0;
168
169 VKSamplers samplers_;
170 VKDescriptorSetLayouts descriptor_set_layouts_;
171
182
184 VmaAllocator mem_allocator_ = VK_NULL_HANDLE;
185
187 VkPhysicalDeviceProperties vk_physical_device_properties_ = {};
188 VkPhysicalDeviceDriverProperties vk_physical_device_driver_properties_ = {};
189 VkPhysicalDeviceIDProperties vk_physical_device_id_properties_ = {};
190 VkPhysicalDeviceMemoryProperties vk_physical_device_memory_properties_ = {};
191 VkPhysicalDeviceMaintenance4Properties vk_physical_device_maintenance4_properties_ = {
192 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_4_PROPERTIES};
193 VkPhysicalDeviceDescriptorBufferPropertiesEXT vk_physical_device_descriptor_buffer_properties_ =
194 {};
196 VkPhysicalDeviceFeatures vk_physical_device_features_ = {};
197 VkPhysicalDeviceVulkan11Features vk_physical_device_vulkan_11_features_ = {};
198 VkPhysicalDeviceVulkan12Features vk_physical_device_vulkan_12_features_ = {};
199 Array<VkExtensionProperties> device_extensions_;
200
202 debug::VKDebuggingTools debugging_tools_;
203
204 /* Workarounds */
205 VKWorkarounds workarounds_;
206 VKExtensions extensions_;
207
208 std::string glsl_vert_patch_;
209 std::string glsl_geom_patch_;
210 std::string glsl_frag_patch_;
211 std::string glsl_comp_patch_;
212 Vector<VKThreadData *> thread_data_;
213
214 Shader *vk_backbuffer_blit_sh_ = nullptr;
215
216 public:
224
228 struct {
229 /* Extension: VK_KHR_dynamic_rendering */
230 PFN_vkCmdBeginRendering vkCmdBeginRendering = nullptr;
231 PFN_vkCmdEndRendering vkCmdEndRendering = nullptr;
232
233 /* Extension: VK_EXT_debug_utils */
234 PFN_vkCmdBeginDebugUtilsLabelEXT vkCmdBeginDebugUtilsLabel = nullptr;
235 PFN_vkCmdEndDebugUtilsLabelEXT vkCmdEndDebugUtilsLabel = nullptr;
236 PFN_vkSetDebugUtilsObjectNameEXT vkSetDebugUtilsObjectName = nullptr;
237 PFN_vkCreateDebugUtilsMessengerEXT vkCreateDebugUtilsMessenger = nullptr;
238 PFN_vkDestroyDebugUtilsMessengerEXT vkDestroyDebugUtilsMessenger = nullptr;
239
240 /* Extension: VK_KHR_external_memory_fd */
241 PFN_vkGetMemoryFdKHR vkGetMemoryFd = nullptr;
242
243#ifdef _WIN32
244 /* Extension: VK_KHR_external_memory_win32 */
245 PFN_vkGetMemoryWin32HandleKHR vkGetMemoryWin32Handle = nullptr;
246#endif
247
248 /* Extension: VK_EXT_descriptor_buffer */
249 PFN_vkGetDescriptorSetLayoutSizeEXT vkGetDescriptorSetLayoutSize = nullptr;
250 PFN_vkGetDescriptorSetLayoutBindingOffsetEXT vkGetDescriptorSetLayoutBindingOffset = nullptr;
251 PFN_vkGetDescriptorEXT vkGetDescriptor = nullptr;
252 PFN_vkCmdBindDescriptorBuffersEXT vkCmdBindDescriptorBuffers = nullptr;
253 PFN_vkCmdSetDescriptorBufferOffsetsEXT vkCmdSetDescriptorBufferOffsets = nullptr;
254
256
258
259 const char *extension_name_get(int index) const
260 {
261 return device_extensions_[index].extensionName;
262 }
263
264 VkPhysicalDevice physical_device_get() const
265 {
266 return vk_physical_device_;
267 }
268
269 const VkPhysicalDeviceProperties &physical_device_properties_get() const
270 {
271 return vk_physical_device_properties_;
272 }
273
274 inline const VkPhysicalDeviceMaintenance4Properties &
276 {
277 return vk_physical_device_maintenance4_properties_;
278 }
279
280 const VkPhysicalDeviceIDProperties &physical_device_id_properties_get() const
281 {
282 return vk_physical_device_id_properties_;
283 }
284
285 inline const VkPhysicalDeviceDescriptorBufferPropertiesEXT &
287 {
288 return vk_physical_device_descriptor_buffer_properties_;
289 }
290
291 const VkPhysicalDeviceFeatures &physical_device_features_get() const
292 {
293 return vk_physical_device_features_;
294 }
295
296 const VkPhysicalDeviceVulkan11Features &physical_device_vulkan_11_features_get() const
297 {
298 return vk_physical_device_vulkan_11_features_;
299 }
300
301 const VkPhysicalDeviceVulkan12Features &physical_device_vulkan_12_features_get() const
302 {
303 return vk_physical_device_vulkan_12_features_;
304 }
305
306 VkInstance instance_get() const
307 {
308 return vk_instance_;
309 };
310
311 VkDevice vk_handle() const
312 {
313 return vk_device_;
314 }
315
316 uint32_t queue_family_get() const
317 {
318 return vk_queue_family_;
319 }
320
321 inline VmaAllocator mem_allocator_get() const
322 {
323 return mem_allocator_;
324 }
325
327 {
328 return descriptor_set_layouts_;
329 }
330
332 {
333 return debugging_tools_;
334 }
335
337 {
338 return debugging_tools_;
339 }
340
341 const VKSamplers &samplers() const
342 {
343 return samplers_;
344 }
345
346 void init(void *ghost_context);
347 void reinit();
348 void deinit();
349 bool is_initialized() const
350 {
351 return is_initialized_;
352 }
353
356 std::string vendor_name() const;
357 std::string driver_version() const;
358
365 bool supports_extension(const char *extension_name) const;
366
368 {
369 return workarounds_;
370 }
371 inline const VKExtensions &extensions_get() const
372 {
373 return extensions_;
374 }
375
376 std::string glsl_vertex_patch_get() const;
377 std::string glsl_geometry_patch_get() const;
378 std::string glsl_fragment_patch_get() const;
379 std::string glsl_compute_patch_get() const;
381
382 /* -------------------------------------------------------------------- */
385 static void submission_runner(TaskPool *__restrict pool, void *task_data);
387
389 VKDiscardPool &context_discard_pool,
390 bool submit_to_device,
391 bool wait_for_completion,
392 VkPipelineStageFlags wait_dst_stage_mask,
393 VkSemaphore wait_semaphore,
394 VkSemaphore signal_semaphore,
395 VkFence signal_fence);
396 void wait_for_timeline(TimelineValue timeline);
397 void wait_queue_idle();
398
403 {
404 BLI_assert(vk_timeline_semaphore_ != VK_NULL_HANDLE);
405 TimelineValue current_timeline;
406 VkResult result = vkGetSemaphoreCounterValue(
407 vk_device_, vk_timeline_semaphore_, &current_timeline);
410 result == VK_SUCCESS && current_timeline != UINT64_MAX,
411 "Potential driver crash has happened. Several drivers will report UINT64_MAX when "
412 "requesting a counter value of an timeline semaphore right after/during a driver reset. "
413 "If this happen we should investigate what makes the driver crash. In the past this has "
414 "been detected on QUALCOMM and NVIDIA drivers. The result code of the call is "
415 "VK_SUCCESS.");
416 return current_timeline;
417 }
418
420
421 /* -------------------------------------------------------------------- */
424
429
430#if 0
445 VKDiscardPool &discard_pool_for_current_thread(bool thread_safe = false);
446#endif
447
448 void context_register(VKContext &context);
449 void context_unregister(VKContext &context);
451
452 void memory_statistics_get(int *r_total_mem_kb, int *r_free_mem_kb) const;
453 static void debug_print(std::ostream &os, const VKDiscardPool &discard_pool);
454 void debug_print();
455
457
459 {
460 if (vk_backbuffer_blit_sh_ == nullptr) {
461 /* See #system_extended_srgb_transfer_function in libocio_display_processor.cc for
462 * details on this choice. */
463#if defined(_WIN32) || defined(__APPLE__)
464 vk_backbuffer_blit_sh_ = GPU_shader_create_from_info_name("vk_backbuffer_blit");
465#else
466 vk_backbuffer_blit_sh_ = GPU_shader_create_from_info_name("vk_backbuffer_blit_gamma22");
467#endif
468 }
469 return vk_backbuffer_blit_sh_;
470 }
471
472 private:
473 void init_physical_device_properties();
474 void init_physical_device_memory_properties();
475 void init_physical_device_features();
476 void init_physical_device_extensions();
477 void init_debug_callbacks();
478 void init_submission_pool();
479 void deinit_submission_pool();
483 void init_functions();
484
488 void init_dummy_buffer();
489
490 /* During initialization the backend requires access to update the workarounds. */
491 friend VKBackend;
492};
493
494} // namespace blender::gpu
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
#define UNUSED_VARS(...)
GPUDeviceType
GPUDriverType
blender::gpu::Shader * GPU_shader_create_from_info_name(const char *info_name)
void init()
NonCopyable(const NonCopyable &other)=delete
NonMovable(NonMovable &&other)=delete
uint32_t queue_family_get() const
Definition vk_device.hh:316
VKPipelinePool pipelines
Definition vk_device.hh:221
PFN_vkGetDescriptorSetLayoutBindingOffsetEXT vkGetDescriptorSetLayoutBindingOffset
Definition vk_device.hh:250
const char * extension_name_get(int index) const
Definition vk_device.hh:259
PFN_vkCmdBindDescriptorBuffersEXT vkCmdBindDescriptorBuffers
Definition vk_device.hh:252
render_graph::VKResourceStateTracker resources
Definition vk_device.hh:217
PFN_vkCmdSetDescriptorBufferOffsetsEXT vkCmdSetDescriptorBufferOffsets
Definition vk_device.hh:253
const VkPhysicalDeviceFeatures & physical_device_features_get() const
Definition vk_device.hh:291
PFN_vkGetMemoryFdKHR vkGetMemoryFd
Definition vk_device.hh:241
GPUDriverType driver_type() const
Definition vk_device.cc:385
PFN_vkSetDebugUtilsObjectNameEXT vkSetDebugUtilsObjectName
Definition vk_device.hh:236
VmaAllocator mem_allocator_get() const
Definition vk_device.hh:321
VKDescriptorSetLayouts & descriptor_set_layouts_get()
Definition vk_device.hh:326
VkDevice vk_handle() const
Definition vk_device.hh:311
VKMemoryPools vma_pools
Definition vk_device.hh:257
bool supports_extension(const char *extension_name) const
Definition vk_device.cc:259
const VKExtensions & extensions_get() const
Definition vk_device.hh:371
const VkPhysicalDeviceProperties & physical_device_properties_get() const
Definition vk_device.hh:269
const VKSamplers & samplers() const
Definition vk_device.hh:341
std::string vendor_name() const
Definition vk_device.cc:411
std::string driver_version() const
Definition vk_device.cc:438
TimelineValue submission_finished_timeline_get() const
Definition vk_device.hh:402
const VkPhysicalDeviceMaintenance4Properties & physical_device_maintenance4_properties_get() const
Definition vk_device.hh:275
std::string glsl_fragment_patch_get() const
Definition vk_device.cc:329
PFN_vkCreateDebugUtilsMessengerEXT vkCreateDebugUtilsMessenger
Definition vk_device.hh:237
render_graph::VKRenderGraph * render_graph_new()
PFN_vkGetDescriptorEXT vkGetDescriptor
Definition vk_device.hh:251
Shader * vk_backbuffer_blit_sh_get()
Definition vk_device.hh:458
VKDiscardPool orphaned_data
Definition vk_device.hh:218
debug::VKDebuggingTools & debugging_tools_get()
Definition vk_device.hh:331
GPUDeviceType device_type() const
Definition vk_device.cc:353
VKThreadData & current_thread_data()
Definition vk_device.cc:461
const VkPhysicalDeviceIDProperties & physical_device_id_properties_get() const
Definition vk_device.hh:280
PFN_vkGetDescriptorSetLayoutSizeEXT vkGetDescriptorSetLayoutSize
Definition vk_device.hh:249
static void submission_runner(TaskPool *__restrict pool, void *task_data)
const debug::VKDebuggingTools & debugging_tools_get() const
Definition vk_device.hh:336
void context_register(VKContext &context)
Definition vk_device.cc:477
bool is_initialized() const
Definition vk_device.hh:349
VkPhysicalDevice physical_device_get() const
Definition vk_device.hh:264
shader::GeneratedSource extensions_define(StringRefNull stage_define) const
Definition vk_device.cc:283
const VKWorkarounds & workarounds_get() const
Definition vk_device.hh:367
PFN_vkCmdEndDebugUtilsLabelEXT vkCmdEndDebugUtilsLabel
Definition vk_device.hh:235
std::string glsl_vertex_patch_get() const
Definition vk_device.cc:315
VkInstance instance_get() const
Definition vk_device.hh:306
TimelineValue render_graph_submit(render_graph::VKRenderGraph *render_graph, VKDiscardPool &context_discard_pool, bool submit_to_device, bool wait_for_completion, VkPipelineStageFlags wait_dst_stage_mask, VkSemaphore wait_semaphore, VkSemaphore signal_semaphore, VkFence signal_fence)
PFN_vkDestroyDebugUtilsMessengerEXT vkDestroyDebugUtilsMessenger
Definition vk_device.hh:238
void context_unregister(VKContext &context)
Definition vk_device.cc:482
const VkPhysicalDeviceVulkan12Features & physical_device_vulkan_12_features_get() const
Definition vk_device.hh:301
PFN_vkCmdBeginDebugUtilsLabelEXT vkCmdBeginDebugUtilsLabel
Definition vk_device.hh:234
void memory_statistics_get(int *r_total_mem_kb, int *r_free_mem_kb) const
Definition vk_device.cc:505
std::string glsl_geometry_patch_get() const
Definition vk_device.cc:322
void wait_for_timeline(TimelineValue timeline)
const VkPhysicalDeviceDescriptorBufferPropertiesEXT & physical_device_descriptor_buffer_properties_get() const
Definition vk_device.hh:286
PFN_vkCmdBeginRendering vkCmdBeginRendering
Definition vk_device.hh:230
std::string glsl_compute_patch_get() const
Definition vk_device.cc:336
const VkPhysicalDeviceVulkan11Features & physical_device_vulkan_11_features_get() const
Definition vk_device.hh:296
Span< std::reference_wrapper< VKContext > > contexts_get() const
Definition vk_device.cc:500
PFN_vkCmdEndRendering vkCmdEndRendering
Definition vk_device.hh:231
struct blender::gpu::VKDevice::@152120360333013146246346216002113345357100126073 functions
VKDiscardPool orphaned_data_render
Definition vk_device.hh:220
VKDescriptorSetTracker descriptor_set
Definition vk_device.hh:120
VKThreadData(VKDevice &device, pthread_t thread_id)
Definition vk_device.cc:450
VKDescriptorPools descriptor_pools
Definition vk_device.hh:119
#define UINT64_MAX
uint64_t TimelineValue
Definition vk_common.hh:36
struct blender::gpu::VKWorkarounds::@332200143060210304170234105102153121344114125320 vertex_formats