46 while (!thread_data_.is_empty()) {
48 thread_data->
deinit(*
this);
55 descriptor_set_layouts_.
deinit();
56 vmaDestroyAllocator(mem_allocator_);
57 mem_allocator_ = VK_NULL_HANDLE;
59 debugging_tools_.
deinit(vk_instance_);
61 vk_instance_ = VK_NULL_HANDLE;
62 vk_physical_device_ = VK_NULL_HANDLE;
63 vk_device_ = VK_NULL_HANDLE;
65 vk_queue_ = VK_NULL_HANDLE;
66 vk_physical_device_properties_ = {};
72 return vk_device_ != VK_NULL_HANDLE;
78 void *queue_mutex =
nullptr;
79 GHOST_GetVulkanHandles((GHOST_ContextHandle)ghost_context,
86 queue_mutex_ =
static_cast<std::mutex *
>(queue_mutex);
88 init_physical_device_properties();
89 init_physical_device_memory_properties();
90 init_physical_device_features();
91 init_physical_device_extensions();
92 VKBackend::platform_init(*
this);
95 init_debug_callbacks();
96 init_memory_allocator();
108void VKDevice::init_functions()
110#define LOAD_FUNCTION(name) (PFN_##name) vkGetInstanceProcAddr(vk_instance_, STRINGIFY(name))
124void VKDevice::init_debug_callbacks()
126 debugging_tools_.
init(vk_instance_);
129void VKDevice::init_physical_device_properties()
131 BLI_assert(vk_physical_device_ != VK_NULL_HANDLE);
133 VkPhysicalDeviceProperties2 vk_physical_device_properties = {};
134 vk_physical_device_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
135 vk_physical_device_driver_properties_.sType =
136 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES;
137 vk_physical_device_properties.pNext = &vk_physical_device_driver_properties_;
139 vkGetPhysicalDeviceProperties2(vk_physical_device_, &vk_physical_device_properties);
140 vk_physical_device_properties_ = vk_physical_device_properties.properties;
143void VKDevice::init_physical_device_memory_properties()
145 BLI_assert(vk_physical_device_ != VK_NULL_HANDLE);
146 vkGetPhysicalDeviceMemoryProperties(vk_physical_device_, &vk_physical_device_memory_properties_);
149void VKDevice::init_physical_device_features()
151 BLI_assert(vk_physical_device_ != VK_NULL_HANDLE);
153 VkPhysicalDeviceFeatures2 features = {};
154 features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
155 vk_physical_device_vulkan_11_features_.sType =
156 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
157 vk_physical_device_vulkan_12_features_.sType =
158 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
160 features.pNext = &vk_physical_device_vulkan_11_features_;
161 vk_physical_device_vulkan_11_features_.pNext = &vk_physical_device_vulkan_12_features_;
163 vkGetPhysicalDeviceFeatures2(vk_physical_device_, &features);
164 vk_physical_device_features_ = features.features;
167void VKDevice::init_physical_device_extensions()
170 vkEnumerateDeviceExtensionProperties(vk_physical_device_,
nullptr, &
count,
nullptr);
171 device_extensions_ = Array<VkExtensionProperties>(
count);
172 vkEnumerateDeviceExtensionProperties(
173 vk_physical_device_,
nullptr, &
count, device_extensions_.
data());
178 for (
const VkExtensionProperties &vk_extension_properties : device_extensions_) {
179 if (
STREQ(vk_extension_properties.extensionName, extension_name)) {
186void VKDevice::init_memory_allocator()
189 VmaAllocatorCreateInfo info = {};
190 info.vulkanApiVersion = VK_API_VERSION_1_2;
191 info.physicalDevice = vk_physical_device_;
192 info.device = vk_device_;
193 info.instance = vk_instance_;
194 info.pAllocationCallbacks = vk_allocation_callbacks;
195 vmaCreateAllocator(&info, &mem_allocator_);
198void VKDevice::init_dummy_buffer()
202 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT);
206 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};
212 std::stringstream ss;
214 ss <<
"#version 450\n";
216 ss <<
"#extension GL_ARB_shader_draw_parameters : enable\n";
217 ss <<
"#define GPU_ARB_shader_draw_parameters\n";
218 ss <<
"#define gpu_BaseInstance (gl_BaseInstanceARB)\n";
221 ss <<
"#define gl_VertexID gl_VertexIndex\n";
222 ss <<
"#define gpu_InstanceIndex (gl_InstanceIndex)\n";
223 ss <<
"#define gl_InstanceID (gpu_InstanceIndex - gpu_BaseInstance)\n";
225 ss <<
"#extension GL_ARB_shader_viewport_layer_array: enable\n";
227 ss <<
"#extension GL_ARB_shader_stencil_export: enable\n";
228 ss <<
"#define GPU_ARB_shader_stencil_export 1\n";
231 ss <<
"#define gpu_Layer gl_Layer\n";
234 ss <<
"#define gpu_ViewportIndex gl_ViewportIndex\n";
237 ss <<
"#extension GL_EXT_fragment_shader_barycentric : require\n";
238 ss <<
"#define gpu_BaryCoord gl_BaryCoordEXT\n";
239 ss <<
"#define gpu_BaryCoordNoPersp gl_BaryCoordNoPerspEXT\n";
242 ss <<
"#define DFDX_SIGN 1.0\n";
243 ss <<
"#define DFDY_SIGN 1.0\n";
247 glsl_patch_ = ss.str();
253 return glsl_patch_.c_str();
268 switch (vk_physical_device_driver_properties_.driverID) {
269 case VK_DRIVER_ID_AMD_PROPRIETARY:
270 case VK_DRIVER_ID_AMD_OPEN_SOURCE:
271 case VK_DRIVER_ID_MESA_RADV:
274 case VK_DRIVER_ID_NVIDIA_PROPRIETARY:
275 case VK_DRIVER_ID_MESA_NVK:
278 case VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS:
279 case VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA:
282 case VK_DRIVER_ID_QUALCOMM_PROPRIETARY:
285 case VK_DRIVER_ID_MOLTENVK:
288 case VK_DRIVER_ID_MESA_LLVMPIPE:
300 switch (vk_physical_device_driver_properties_.driverID) {
301 case VK_DRIVER_ID_AMD_PROPRIETARY:
302 case VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS:
303 case VK_DRIVER_ID_NVIDIA_PROPRIETARY:
304 case VK_DRIVER_ID_QUALCOMM_PROPRIETARY:
307 case VK_DRIVER_ID_MOLTENVK:
308 case VK_DRIVER_ID_AMD_OPEN_SOURCE:
309 case VK_DRIVER_ID_MESA_RADV:
310 case VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA:
311 case VK_DRIVER_ID_MESA_NVK:
314 case VK_DRIVER_ID_MESA_LLVMPIPE:
327 if (vk_physical_device_properties_.vendorID < 0x10000) {
328 switch (vk_physical_device_properties_.vendorID) {
331 return "Advanced Micro Devices";
333 return "NVIDIA Corporation";
335 return "Intel Corporation";
339 return std::to_string(vk_physical_device_properties_.vendorID);
347 return std::to_string(vk_physical_device_properties_.vendorID);
353 return StringRefNull(vk_physical_device_driver_properties_.driverName) +
" " +
354 StringRefNull(vk_physical_device_driver_properties_.driverInfo);
366 resource_pool.init(device);
373 resource_pool.deinit(device);
386 pthread_t current_thread_id = pthread_self();
389 if (pthread_equal(thread_data->thread_id, current_thread_id)) {
395 thread_data_.append(thread_data);
402 pthread_t current_thread_id = pthread_self();
405 if (pthread_equal(thread_data->thread_id, current_thread_id)) {
406 return thread_data->resource_pool_get().discard_pool;
416 contexts_.append(std::reference_wrapper(context));
421 contexts_.remove(contexts_.first_index_of(std::reference_wrapper(context)));
430 VmaBudget budgets[VK_MAX_MEMORY_HEAPS];
432 VkDeviceSize total_mem = 0;
433 VkDeviceSize used_mem = 0;
435 for (
int memory_heap_index :
IndexRange(vk_physical_device_memory_properties_.memoryHeapCount)) {
436 const VkMemoryHeap &memory_heap =
437 vk_physical_device_memory_properties_.memoryHeaps[memory_heap_index];
438 const VmaBudget &budget = budgets[memory_heap_index];
441 if (!
bool(memory_heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT)) {
445 total_mem += memory_heap.size;
446 used_mem += budget.usage;
449 *r_total_mem_kb =
int(total_mem / 1024);
450 *r_free_mem_kb =
int((total_mem - used_mem) / 1024);
462 discard_pool.image_views_.
is_empty() && discard_pool.shader_modules_.
is_empty() &&
463 discard_pool.pipeline_layouts_.
is_empty())
467 os <<
" Discardable resources: ";
468 if (!discard_pool.images_.
is_empty()) {
469 os <<
"VkImage=" << discard_pool.images_.
size() <<
" ";
471 if (!discard_pool.image_views_.
is_empty()) {
472 os <<
"VkImageView=" << discard_pool.image_views_.
size() <<
" ";
474 if (!discard_pool.buffers_.
is_empty()) {
475 os <<
"VkBuffer=" << discard_pool.buffers_.
size() <<
" ";
477 if (!discard_pool.shader_modules_.
is_empty()) {
478 os <<
"VkShaderModule=" << discard_pool.shader_modules_.
size() <<
" ";
480 if (!discard_pool.pipeline_layouts_.
is_empty()) {
481 os <<
"VkPipelineLayout=" << discard_pool.pipeline_layouts_.
size();
489 "VKDevice::debug_print can only be called from the main thread.");
491 std::ostream &os = std::cout;
494 os <<
" Graphics: " <<
pipelines.graphic_pipelines_.size() <<
"\n";
495 os <<
" Compute: " <<
pipelines.compute_pipelines_.size() <<
"\n";
496 os <<
"Descriptor sets\n";
497 os <<
" VkDescriptorSetLayouts: " << descriptor_set_layouts_.
size() <<
"\n";
501 const bool is_main = pthread_equal(thread_data->thread_id, pthread_self());
502 os <<
"ThreadData" << (is_main ?
" (main-thread)" :
"") <<
")\n";
503 os <<
" Rendering_depth: " << thread_data->rendering_depth <<
"\n";
504 for (
int resource_pool_index :
IndexRange(thread_data->resource_pools.size())) {
505 const VKResourcePool &resource_pool = thread_data->resource_pools[resource_pool_index];
506 const bool is_active = thread_data->resource_pool_index == resource_pool_index;
507 os <<
" Resource Pool (index=" << resource_pool_index << (is_active ?
" active" :
"")
512 os <<
"Orphaned data\n";
#define BLI_assert_msg(a, msg)
int BLI_thread_is_main(void)
GHOST C-API function and type declarations.
bool GPU_stencil_export_support()
bool GPU_shader_draw_parameters_support()
static void capabilities_init(VKDevice &device)
void update_immediately(const void *data) const
bool create(size_t size, GPUUsageType usage, VkBufferUsageFlags buffer_usage, bool is_host_visible=true)
VkBuffer vk_handle() const
void init(void *ghost_context)
render_graph::VKResourceStateTracker resources
VmaAllocator mem_allocator_get() const
VkQueue queue_get() const
VkDevice vk_handle() const
bool supports_extension(const char *extension_name) const
std::string vendor_name() const
std::string driver_version() const
VKDiscardPool orphaned_data
VKThreadData & current_thread_data()
eGPUDriverType driver_type() const
VKDiscardPool & discard_pool_for_current_thread()
const char * glsl_patch_get() const
void context_register(VKContext &context)
bool is_initialized() const
void context_unregister(VKContext &context)
void memory_statistics_get(int *r_total_mem_kb, int *r_free_mem_kb) const
struct blender::gpu::VKDevice::@670 functions
Span< std::reference_wrapper< VKContext > > contexts_get() const
eGPUDeviceType device_type() const
VKDiscardPool discard_pool
std::array< VKResourcePool, resource_pools_count > resource_pools
VKThreadData(VKDevice &device, pthread_t thread_id)
void deinit(VKDevice &device)
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
char datatoc_glsl_shader_defines_glsl[]
void object_label(GLenum type, GLuint object, const char *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
MatBase< float, 4, 4 > float4x4
bool shader_output_viewport_index
bool fragment_shader_barycentric
char datatoc_glsl_shader_defines_glsl[]
#define LOAD_FUNCTION(name)
#define VK_ALLOCATION_CALLBACKS