10#include <fmt/ranges.h>
40 " - [%c] fragment shader barycentric\n"
41 " - [%c] wide lines\n"
42 " - [%c] multi draw indirect\n"
43 " - [%c] shader clip distance\n"
45 " - [%c] dynamic rendering local read\n"
46 " - [%c] dynamic rendering unused attachments\n"
47 " - [%c] extended dynamic state\n"
48 " - [%c] external memory\n"
49 " - [%c] graphics pipeline library\n"
50 " - [%c] host image copy\n"
51 " - [%c] line rasterization\n"
52 " - [%c] maintenance4\n"
53 " - [%c] memory priority\n"
54 " - [%c] pageable device local memory\n"
55 " - [%c] provoking vertex\n"
56 " - [%c] shader stencil export\n"
57 " - [%c] shader output viewport index and layer\n"
59 " - [%c] ray queries\n"
60 " - [%c] vertex input dynamic state\n"
61 " - [%c] vertex pipeline stores and atomics",
88 "Activated workarounds\n"
89 " - [%c] Not 16/32 bit aligned image formats\n"
90 " - [%c] Static viewport & scissor state\n"
91 " - [%c] No texture pool\n"
92 " - [%c] Missing sRGB clear conversions",
111 deinit_submission_thread();
127 while (!thread_data_.is_empty()) {
131 thread_data_.clear();
135 descriptor_set_layouts_.deinit();
137 mem_allocator_ = VK_NULL_HANDLE;
139 while (!render_graphs_.is_empty()) {
144 debugging_tools_.deinit(vk_instance_);
146 vk_instance_ = VK_NULL_HANDLE;
147 vk_physical_device_ = VK_NULL_HANDLE;
148 vk_device_ = VK_NULL_HANDLE;
149 vk_queue_family_ = 0;
150 vk_queue_ = VK_NULL_HANDLE;
151 vk_physical_device_properties_ = {};
152 glsl_vert_patch_.clear();
153 glsl_frag_patch_.clear();
154 glsl_geom_patch_.clear();
155 glsl_comp_patch_.clear();
156 is_initialized_ =
false;
162 GHOST_VulkanHandles handles = {};
163 ghost_context->getVulkanHandles(handles);
164 vk_instance_ = handles.instance;
165 vk_physical_device_ = handles.physical_device;
166 vk_device_ = handles.device;
167 vk_queue_family_ = handles.graphic_queue_family;
168 vk_queue_ = handles.queue;
169 mem_allocator_ = handles.vma_allocator;
170 queue_mutex_ =
static_cast<std::mutex *
>(handles.queue_mutex);
172 volkLoadDeviceTable(&
functions, vk_device_);
174 init_physical_device_extensions();
175 init_physical_device_properties();
176 init_physical_device_memory_properties();
177 init_physical_device_features();
180 init_debug_callbacks();
194 init_submission_thread();
195 is_initialized_ =
true;
198void VKDevice::init_debug_callbacks()
200 debugging_tools_.
init(vk_instance_);
203void VKDevice::init_physical_device_properties()
205 BLI_assert(vk_physical_device_ != VK_NULL_HANDLE);
207 VkPhysicalDeviceProperties2 vk_physical_device_properties = {};
208 vk_physical_device_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
209 vk_physical_device_driver_properties_.sType =
210 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES;
211 vk_physical_device_id_properties_.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES;
212 vk_physical_device_properties.pNext = &vk_physical_device_driver_properties_;
213 vk_physical_device_driver_properties_.pNext = &vk_physical_device_id_properties_;
216 vk_physical_device_maintenance4_properties_.pNext = vk_physical_device_properties.pNext;
217 vk_physical_device_properties.pNext = &vk_physical_device_maintenance4_properties_;
221 vk_physical_device_graphics_pipeline_library_properties_.pNext =
222 vk_physical_device_properties.pNext;
223 vk_physical_device_properties.pNext =
224 &vk_physical_device_graphics_pipeline_library_properties_;
227 vk_physical_device_acceleration_structure_properties_.pNext =
228 vk_physical_device_properties.pNext;
229 vk_physical_device_properties.pNext = &vk_physical_device_acceleration_structure_properties_;
232 vkGetPhysicalDeviceProperties2(vk_physical_device_, &vk_physical_device_properties);
233 vk_physical_device_properties_ = vk_physical_device_properties.properties;
236void VKDevice::init_physical_device_memory_properties()
238 BLI_assert(vk_physical_device_ != VK_NULL_HANDLE);
239 vkGetPhysicalDeviceMemoryProperties(vk_physical_device_, &vk_physical_device_memory_properties_);
242void VKDevice::init_physical_device_features()
244 BLI_assert(vk_physical_device_ != VK_NULL_HANDLE);
246 VkPhysicalDeviceFeatures2 features = {};
247 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
248 vk_physical_device_vulkan_11_features_.sType =
249 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
251 features.pNext = &vk_physical_device_vulkan_11_features_;
252 vk_physical_device_vulkan_11_features_.pNext =
253 &vk_physical_device_acceleration_structure_features_;
255 vkGetPhysicalDeviceFeatures2(vk_physical_device_, &features);
256 vk_physical_device_features_ = features.features;
259void VKDevice::init_physical_device_extensions()
262 vkEnumerateDeviceExtensionProperties(vk_physical_device_,
nullptr, &count,
nullptr);
263 device_extensions_ = Array<VkExtensionProperties>(count);
264 vkEnumerateDeviceExtensionProperties(
265 vk_physical_device_,
nullptr, &count, device_extensions_.data());
270 for (
const VkExtensionProperties &vk_extension_properties : device_extensions_) {
271 if (
STREQ(vk_extension_properties.extensionName, extension_name)) {
278void VKDevice::init_dummy_buffer()
281 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
282 VMA_MEMORY_USAGE_AUTO_PREFER_HOST,
283 VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
289 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
295 const bool requires_460 = use_ray_query;
296 return requires_460 ? 460u : 450u;
300 bool use_ray_query)
const
302 std::stringstream ss;
305 ss <<
"#define GPU_ARB_clip_control\n";
306 ss <<
"#define GPU_ARB_derivative_control\n";
308 ss <<
"#define gl_VertexID gl_VertexIndex\n";
311 ss <<
"#define gpu_BaseInstance (0)\n";
312 ss <<
"#define gpu_InstanceIndex (gl_InstanceIndex)\n";
313 ss <<
"#define gl_InstanceID (gpu_InstanceIndex)\n";
315 ss <<
"#extension GL_ARB_shader_viewport_layer_array: enable\n";
317 ss <<
"#extension GL_ARB_shader_stencil_export: enable\n";
318 ss <<
"#define GPU_ARB_shader_stencil_export 1\n";
320 if (extensions_.fragment_shader_barycentric) {
321 ss <<
"#extension GL_EXT_fragment_shader_barycentric : require\n";
322 ss <<
"#define gpu_BaryCoord gl_BaryCoordEXT\n";
323 ss <<
"#define gpu_BaryCoordNoPersp gl_BaryCoordNoPerspEXT\n";
326 ss <<
"#extension GL_EXT_ray_query : enable\n";
327 ss <<
"#define GPU_EXT_RAY_QUERY\n";
329 if (!extensions_.provoking_vertex) {
330 ss <<
"#define GPU_PROVOKING_VERTEX_LAST\n";
341 return fmt::to_string(fmt::join(
348 return fmt::to_string(fmt::join(
356 return fmt::to_string(fmt::join(
364 return fmt::to_string(fmt::join(
380 switch (vk_physical_device_driver_properties_.driverID) {
381 case VK_DRIVER_ID_AMD_PROPRIETARY:
382 case VK_DRIVER_ID_AMD_OPEN_SOURCE:
383 case VK_DRIVER_ID_MESA_RADV:
386 case VK_DRIVER_ID_NVIDIA_PROPRIETARY:
387 case VK_DRIVER_ID_MESA_NVK:
390 case VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS:
391 case VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA:
394 case VK_DRIVER_ID_QUALCOMM_PROPRIETARY:
397 case VK_DRIVER_ID_MOLTENVK:
400 case VK_DRIVER_ID_MESA_LLVMPIPE:
412 switch (vk_physical_device_driver_properties_.driverID) {
413 case VK_DRIVER_ID_AMD_PROPRIETARY:
414 case VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS:
415 case VK_DRIVER_ID_NVIDIA_PROPRIETARY:
416 case VK_DRIVER_ID_QUALCOMM_PROPRIETARY:
419 case VK_DRIVER_ID_AMD_OPEN_SOURCE:
422 case VK_DRIVER_ID_MOLTENVK:
423 case VK_DRIVER_ID_MESA_RADV:
424 case VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA:
425 case VK_DRIVER_ID_MESA_NVK:
428 case VK_DRIVER_ID_MESA_LLVMPIPE:
441 if (vk_physical_device_properties_.vendorID < 0x10000) {
442 switch (vk_physical_device_properties_.vendorID) {
445 return "Advanced Micro Devices";
447 return "NVIDIA Corporation";
449 return "Intel Corporation";
453 return std::to_string(vk_physical_device_properties_.vendorID);
461 return std::to_string(vk_physical_device_properties_.vendorID);
467 return StringRefNull(vk_physical_device_driver_properties_.driverName) +
" " +
468 StringRefNull(vk_physical_device_driver_properties_.driverInfo);
491 pthread_t current_thread_id = pthread_self();
494 if (pthread_equal(thread_data->thread_id, current_thread_id)) {
500 thread_data_.append(thread_data);
506 contexts_.append(std::reference_wrapper(context));
511 if (context.render_graph_.has_value()) {
513 context.render_graph_.reset();
515 "Unregistering a context that still has an unsubmitted render graph.");
525 contexts_.remove(contexts_.first_index_of(std::reference_wrapper(context)));
534 VmaBudget budgets[VK_MAX_MEMORY_HEAPS];
536 VkDeviceSize total_mem = 0;
537 VkDeviceSize used_mem = 0;
539 for (
int memory_heap_index :
IndexRange(vk_physical_device_memory_properties_.memoryHeapCount)) {
540 const VkMemoryHeap &memory_heap =
541 vk_physical_device_memory_properties_.memoryHeaps[memory_heap_index];
542 const VmaBudget &budget = budgets[memory_heap_index];
545 if (!
bool(memory_heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT)) {
549 total_mem += memory_heap.size;
550 used_mem += budget.usage;
553 *r_total_mem_kb = int(total_mem / 1024);
554 *r_free_mem_kb = int((total_mem - used_mem) / 1024);
566 "VKDevice::debug_print can only be called from the main thread.");
569 std::ostream &os = std::cout;
571 os <<
" Graphics: " <<
pipelines.graphics_.size() <<
"\n";
572 os <<
" Compute: " <<
pipelines.compute_.size() <<
"\n";
573 os <<
" VertexInLib: " <<
pipelines.vertex_input_libs_.size() <<
"\n";
574 os <<
" ShaderLib: " <<
pipelines.shaders_libs_.size() <<
"\n";
575 os <<
" FragmentOutLib: " <<
pipelines.fragment_output_libs_.size() <<
"\n";
576 os <<
"Descriptor sets\n";
577 os <<
" VkDescriptorSetLayouts: " << descriptor_set_layouts_.size() <<
"\n";
581 const bool is_main = pthread_equal(thread_data->thread_id, pthread_self());
582 os <<
"ThreadData" << (is_main ?
" (main-thread)" :
"") <<
")\n";
583 os <<
" Rendering_depth: " << thread_data->rendering_depth <<
"\n";
585 os <<
"Discard pool\n";
587 os <<
"Discard pool (render)\n";
591 for (
const std::reference_wrapper<VKContext> &context : contexts_) {
592 os <<
" VKContext \n";
593 os << context.get().discard_pool <<
"\n";
599 os <<
"\nMemory: total=" << total_mem_kb <<
", free=" << free_mem_kb <<
"\n";
#define CLOG_DEBUG(clg_ref,...)
#define GPU_SHADER_FREE_SAFE(shader)
#define BLI_assert_msg(a, msg)
constexpr IndexRange()=default
constexpr StringRefNull()
static void capabilities_init(VKDevice &device)
static void platform_init(const VKDevice &device)
void update_immediately(const void *data) const
bool create(size_t size, VkBufferUsageFlags buffer_usage, VmaMemoryUsage vma_memory_usage, VmaAllocationCreateFlags vma_allocation_flags, float priority, bool export_memory=false, const char *debug_name="VKBuffer", size_t alignment=0)
void init(const VKDevice &vk_device)
render_graph::VKResourceStateTracker resources
GPUDriverType driver_type() const
VmaAllocator mem_allocator_get() const
VkDevice vk_handle() const
bool supports_extension(const char *extension_name) const
void init(GHOST_IContext *ghost_context)
std::string vendor_name() const
std::string driver_version() const
VKDiscardPool orphaned_data
GPUDeviceType device_type() const
VKThreadData & current_thread_data()
VolkDeviceTable functions
shader::GeneratedSource extensions_define(StringRefNull stage_define, bool use_ray_query) const
uint32_t glsl_patch_version_get(bool use_ray_query) const
void context_register(VKContext &context)
bool is_initialized() const
std::string glsl_compute_patch_get(bool use_ray_query) const
void context_unregister(VKContext &context)
std::string glsl_vertex_patch_get(bool use_ray_query) const
void memory_statistics_get(int *r_total_mem_kb, int *r_free_mem_kb) const
std::string glsl_geometry_patch_get() const
std::string glsl_fragment_patch_get(bool use_ray_query) const
Span< std::reference_wrapper< VKContext > > contexts_get() const
VKDiscardPool orphaned_data_render
void move_data(VKDiscardPool &src_pool, TimelineValue timeline)
void discard_resources(Span< render_graph::VKResourceStateTracker::DiscardableResource > discardable, TimelineValue timeline)
void deinit(VKDevice &device)
void free_data(const VKDevice &device)
VKThreadData(VKDevice &device, pthread_t thread_id)
VKDescriptorPools descriptor_pools
bool use_dynamic_rendering_local_read
Vector< DiscardableResource > pop_discardable_resources()
void object_label(GLenum type, GLuint object, const char *name)
Vector< shader::GeneratedSource, 0 > GeneratedSourceList
Vector< StringRefNull > gpu_shader_dependency_get_resolved_source(const StringRefNull shader_source_name, const shader::GeneratedSourceList &generated_sources, const StringRefNull shader_name)
constexpr int32_t PCI_ID_NVIDIA
constexpr int32_t PCI_ID_INTEL
constexpr int32_t PCI_ID_ATI
constexpr int32_t PCI_ID_AMD
constexpr int32_t PCI_ID_APPLE
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ BLI_THREAD_QUEUE_WORK_PRIORITY_NORMAL
bool GPU_vertex_pipeline_stores_and_atomics_support()
bool GPU_ray_query_support()
MatBase< float, 4, 4 > float4x4
BMesh const char void * data
bool GPU_stencil_export_support()
uint64_t BLI_thread_queue_push(ThreadQueue *queue, void *work, ThreadQueueWorkPriority priority)
bool texture_pool_workaround
bool dynamic_rendering_unused_attachments
bool extended_dynamic_state
bool fragment_shader_barycentric
bool pageable_device_local_memory
bool shader_clip_distance
bool graphics_pipeline_library
bool dynamic_rendering_local_read
bool shader_viewport_index_layer
bool vertex_input_dynamic_state
void deinit(VKDevice &device)
void init(VKDevice &device)
bool not_aligned_pixel_formats
bool missing_srgb_clear_conversion
bool static_viewport_scissor