Blender V5.3
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#include <thread>
13
14#include "BLI_threads.hh"
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"
30
31namespace blender::gpu {
32class VKBackend;
33
40
45 bool wide_lines = false;
46
51
56
60 bool external_memory = false;
61
63 bool maintenance4 = false;
64
68 bool memory_priority = false;
69
74
79
83 bool line_rasterization = false;
84
89
94
98 bool provoking_vertex = false;
99
103 bool host_image_copy = false;
104
109
113 bool spirv_1_4 = false;
114
120
125
127 void log() const;
128};
129
138
147
152
154 void log() const;
155};
156
179
180class VKDevice : public NonCopyable {
181 private:
183 VkInstance vk_instance_ = VK_NULL_HANDLE;
184 VkPhysicalDevice vk_physical_device_ = VK_NULL_HANDLE;
185 VkDevice vk_device_ = VK_NULL_HANDLE;
186 uint32_t vk_queue_family_ = 0;
187 VkQueue vk_queue_ = VK_NULL_HANDLE;
188 std::mutex *queue_mutex_ = nullptr;
189
190 bool is_initialized_ = false;
191
198 std::thread submission_thread_;
199 std::atomic<bool> submission_thread_should_exit_ = false;
200
205 ThreadQueue *submitted_render_graphs_ = nullptr;
206 ThreadQueue *unused_render_graphs_ = nullptr;
207 VkSemaphore vk_timeline_semaphore_ = VK_NULL_HANDLE;
213 TimelineValue timeline_value_ = 0;
214
215 VKSamplers samplers_;
216 VKDescriptorSetLayouts descriptor_set_layouts_;
217
228
230 VmaAllocator mem_allocator_ = VK_NULL_HANDLE;
231
233 VkPhysicalDeviceProperties vk_physical_device_properties_ = {};
234 VkPhysicalDeviceDriverProperties vk_physical_device_driver_properties_ = {};
235 VkPhysicalDeviceIDProperties vk_physical_device_id_properties_ = {};
236 VkPhysicalDeviceMemoryProperties vk_physical_device_memory_properties_ = {};
237 VkPhysicalDeviceMaintenance4Properties vk_physical_device_maintenance4_properties_ = {
238 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_4_PROPERTIES};
239 VkPhysicalDeviceGraphicsPipelineLibraryPropertiesEXT
240 vk_physical_device_graphics_pipeline_library_properties_ = {
241 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_GRAPHICS_PIPELINE_LIBRARY_PROPERTIES_EXT};
242 VkPhysicalDeviceAccelerationStructurePropertiesKHR
243 vk_physical_device_acceleration_structure_properties_ = {
244 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_PROPERTIES_KHR};
246 VkPhysicalDeviceFeatures vk_physical_device_features_ = {};
247 VkPhysicalDeviceVulkan11Features vk_physical_device_vulkan_11_features_ = {};
248 VkPhysicalDeviceAccelerationStructureFeaturesKHR
249 vk_physical_device_acceleration_structure_features_ = {
250 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR,
251 };
252 Array<VkExtensionProperties> device_extensions_;
253
255 debug::VKDebuggingTools debugging_tools_;
256
257 /* Workarounds */
258 VKWorkarounds workarounds_;
259 VKExtensions extensions_;
260
261 std::string glsl_vert_patch_;
262 std::string glsl_geom_patch_;
263 std::string glsl_frag_patch_;
264 std::string glsl_comp_patch_;
265 Vector<VKThreadData *> thread_data_;
266
267 Shader *vk_backbuffer_blit_sh_ = nullptr;
268
269 public:
276
279
280 VolkDeviceTable functions = {};
281
283
284 const char *extension_name_get(int index) const
285 {
286 return device_extensions_[index].extensionName;
287 }
288
289 VkPhysicalDevice physical_device_get() const
290 {
291 return vk_physical_device_;
292 }
293
294 const VkPhysicalDeviceProperties &physical_device_properties_get() const
295 {
296 return vk_physical_device_properties_;
297 }
298
299 inline const VkPhysicalDeviceMaintenance4Properties &
301 {
302 return vk_physical_device_maintenance4_properties_;
303 }
304
305 const VkPhysicalDeviceIDProperties &physical_device_id_properties_get() const
306 {
307 return vk_physical_device_id_properties_;
308 }
309
310 inline const VkPhysicalDeviceGraphicsPipelineLibraryPropertiesEXT &
312 {
313 return vk_physical_device_graphics_pipeline_library_properties_;
314 }
315
316 inline const VkPhysicalDeviceAccelerationStructurePropertiesKHR &
318 {
319 return vk_physical_device_acceleration_structure_properties_;
320 }
321
322 const VkPhysicalDeviceFeatures &physical_device_features_get() const
323 {
324 return vk_physical_device_features_;
325 }
326
327 inline const VkPhysicalDeviceAccelerationStructureFeaturesKHR &
329 {
330 return vk_physical_device_acceleration_structure_features_;
331 }
332
333 VkInstance instance_get() const
334 {
335 return vk_instance_;
336 };
337
338 VkDevice vk_handle() const
339 {
340 return vk_device_;
341 }
342
344 {
345 return vk_queue_family_;
346 }
347
348 inline VmaAllocator mem_allocator_get() const
349 {
350 return mem_allocator_;
351 }
352
354 {
355 return descriptor_set_layouts_;
356 }
357
359 {
360 return debugging_tools_;
361 }
362
364 {
365 return debugging_tools_;
366 }
367
368 const VKSamplers &samplers() const
369 {
370 return samplers_;
371 }
372
373 void init(GHOST_IContext *ghost_context);
374 void reinit();
375 void deinit();
376 bool is_initialized() const
377 {
378 return is_initialized_;
379 }
380
383 std::string vendor_name() const;
384 std::string driver_version() const;
385
392 bool supports_extension(const char *extension_name) const;
393
395 {
396 return workarounds_;
397 }
398 inline const VKExtensions &extensions_get() const
399 {
400 return extensions_;
401 }
402
403 std::string glsl_vertex_patch_get(bool use_ray_query) const;
404 std::string glsl_geometry_patch_get() const;
405 std::string glsl_fragment_patch_get(bool use_ray_query) const;
406 std::string glsl_compute_patch_get(bool use_ray_query) const;
407 shader::GeneratedSource extensions_define(StringRefNull stage_define, bool use_ray_query) const;
408 uint32_t glsl_patch_version_get(bool use_ray_query) const;
409
410 /* -------------------------------------------------------------------- */
413 static void submission_runner(VKDevice *device);
415
417 VKDiscardPool &context_discard_pool,
418 bool submit_to_device,
419 bool wait_for_submission,
420 bool wait_for_completion,
421 VkPipelineStageFlags wait_dst_stage_mask,
422 VkSemaphore wait_semaphore,
423 VkSemaphore signal_semaphore,
424 VkFence signal_fence);
425 void wait_for_timeline(TimelineValue timeline);
426 void wait_queue_idle();
427
432 {
433 BLI_assert(vk_timeline_semaphore_ != VK_NULL_HANDLE);
434 TimelineValue current_timeline;
435 VkResult result = functions.vkGetSemaphoreCounterValueKHR(
436 vk_device_, vk_timeline_semaphore_, &current_timeline);
439 result == VK_SUCCESS && current_timeline != UINT64_MAX,
440 "Potential driver crash has happened. Several drivers will report UINT64_MAX when "
441 "requesting a counter value of an timeline semaphore right after/during a driver reset. "
442 "If this happen we should investigate what makes the driver crash. In the past this has "
443 "been detected on QUALCOMM and NVIDIA drivers. The result code of the call is "
444 "VK_SUCCESS.");
445 return current_timeline;
446 }
447
449
450 /* -------------------------------------------------------------------- */
453
458
459 void context_register(VKContext &context);
460 void context_unregister(VKContext &context);
462
463 void memory_statistics_get(int *r_total_mem_kb, int *r_free_mem_kb) const;
464 void debug_print() const;
465
467
469 {
470 if (vk_backbuffer_blit_sh_ == nullptr) {
471 /* See #system_extended_srgb_transfer_function in libocio_display_processor.cc for
472 * details on this choice. */
473#if defined(_WIN32) || defined(__APPLE__)
474 vk_backbuffer_blit_sh_ = GPU_shader_create_from_info_name("vk_backbuffer_blit");
475#else
476 vk_backbuffer_blit_sh_ = GPU_shader_create_from_info_name("vk_backbuffer_blit_gamma22");
477#endif
478 }
479 return vk_backbuffer_blit_sh_;
480 }
481
482 private:
483 void init_physical_device_properties();
484 void init_physical_device_memory_properties();
485 void init_physical_device_features();
486 void init_physical_device_extensions();
487 void init_debug_callbacks();
488 void init_submission_thread();
489 void deinit_submission_thread();
493 void init_functions();
494
498 void init_dummy_buffer();
499
500 /* During initialization the backend requires access to update the workarounds. */
501 friend VKBackend;
502};
503
504} // namespace blender::gpu
#define UINT64_MAX
#define UNUSED_VARS(...)
#define BLI_assert_msg(a, msg)
NonCopyable(const NonCopyable &other)=delete
NonMovable(NonMovable &&other)=delete
uint32_t queue_family_get() const
Definition vk_device.hh:343
VKPipelinePool pipelines
Definition vk_device.hh:274
const char * extension_name_get(int index) const
Definition vk_device.hh:284
render_graph::VKResourceStateTracker resources
Definition vk_device.hh:270
const VkPhysicalDeviceFeatures & physical_device_features_get() const
Definition vk_device.hh:322
GPUDriverType driver_type() const
Definition vk_device.cc:410
VmaAllocator mem_allocator_get() const
Definition vk_device.hh:348
VKDescriptorSetLayouts & descriptor_set_layouts_get()
Definition vk_device.hh:353
VkDevice vk_handle() const
Definition vk_device.hh:338
void debug_print() const
Definition vk_device.cc:563
VKMemoryPools vma_pools
Definition vk_device.hh:282
bool supports_extension(const char *extension_name) const
Definition vk_device.cc:268
const VKExtensions & extensions_get() const
Definition vk_device.hh:398
const VkPhysicalDeviceProperties & physical_device_properties_get() const
Definition vk_device.hh:294
const VKSamplers & samplers() const
Definition vk_device.hh:368
std::string vendor_name() const
Definition vk_device.cc:438
std::string driver_version() const
Definition vk_device.cc:465
TimelineValue submission_finished_timeline_get() const
Definition vk_device.hh:431
const VkPhysicalDeviceMaintenance4Properties & physical_device_maintenance4_properties_get() const
Definition vk_device.hh:300
render_graph::VKRenderGraph * render_graph_new()
static void submission_runner(VKDevice *device)
Shader * vk_backbuffer_blit_sh_get()
Definition vk_device.hh:468
VKDiscardPool orphaned_data
Definition vk_device.hh:271
debug::VKDebuggingTools & debugging_tools_get()
Definition vk_device.hh:358
GPUDeviceType device_type() const
Definition vk_device.cc:378
const VkPhysicalDeviceAccelerationStructureFeaturesKHR & physical_device_acceleration_structure_features_get() const
Definition vk_device.hh:328
VKThreadData & current_thread_data()
Definition vk_device.cc:488
const VkPhysicalDeviceIDProperties & physical_device_id_properties_get() const
Definition vk_device.hh:305
VolkDeviceTable functions
Definition vk_device.hh:280
shader::GeneratedSource extensions_define(StringRefNull stage_define, bool use_ray_query) const
Definition vk_device.cc:299
const VkPhysicalDeviceAccelerationStructurePropertiesKHR & physical_device_acceleration_structure_properties_get()
Definition vk_device.hh:317
uint32_t glsl_patch_version_get(bool use_ray_query) const
Definition vk_device.cc:293
const debug::VKDebuggingTools & debugging_tools_get() const
Definition vk_device.hh:363
void context_register(VKContext &context)
Definition vk_device.cc:504
bool is_initialized() const
Definition vk_device.hh:376
VkPhysicalDevice physical_device_get() const
Definition vk_device.hh:289
std::string glsl_compute_patch_get(bool use_ray_query) const
Definition vk_device.cc:360
const VKWorkarounds & workarounds_get() const
Definition vk_device.hh:394
VkInstance instance_get() const
Definition vk_device.hh:333
void context_unregister(VKContext &context)
Definition vk_device.cc:509
std::string glsl_vertex_patch_get(bool use_ray_query) const
Definition vk_device.cc:337
void memory_statistics_get(int *r_total_mem_kb, int *r_free_mem_kb) const
Definition vk_device.cc:532
std::string glsl_geometry_patch_get() const
Definition vk_device.cc:345
void wait_for_timeline(TimelineValue timeline)
VKVertexInputDescriptionPool vertex_input_descriptions
Definition vk_device.hh:275
std::string glsl_fragment_patch_get(bool use_ray_query) const
Definition vk_device.cc:352
const VkPhysicalDeviceGraphicsPipelineLibraryPropertiesEXT & physical_device_graphics_pipeline_properties_get()
Definition vk_device.hh:311
TimelineValue render_graph_submit(render_graph::VKRenderGraph *render_graph, VKDiscardPool &context_discard_pool, bool submit_to_device, bool wait_for_submission, bool wait_for_completion, VkPipelineStageFlags wait_dst_stage_mask, VkSemaphore wait_semaphore, VkSemaphore signal_semaphore, VkFence signal_fence)
Span< std::reference_wrapper< VKContext > > contexts_get() const
Definition vk_device.cc:527
VKDiscardPool orphaned_data_render
Definition vk_device.hh:273
VKDescriptorSetTracker descriptor_set
Definition vk_device.hh:165
VKThreadData(VKDevice &device, pthread_t thread_id)
Definition vk_device.cc:477
VKDescriptorPools descriptor_pools
Definition vk_device.hh:164
Pool with all used vertex input descriptions.
uint64_t TimelineValue
Definition vk_common.hh:39
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
gpu::Shader * GPU_shader_create_from_info_name(const char *info_name)
unsigned int uint32_t
Definition BKE_text.h:81
signed int int32_t
Definition BKE_text.h:78