Monado OpenXR Runtime
vk_helpers.h
Go to the documentation of this file.
1// Copyright 2019-2024, Collabora, Ltd.
2// Copyright 2024-2025, NVIDIA CORPORATION.
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Common Vulkan code header.
7 *
8 * Note that some sections of this are generated
9 * by `scripts/generate_vk_helpers.py` - lists of functions
10 * and of optional extensions to check for. In those,
11 * please update the script and run it, instead of editing
12 * directly in this file. The generated parts are delimited
13 * by special comments.
14 *
15 * @author Jakob Bornecrantz <jakob@collabora.com>
16 * @author Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
17 * @author Moshi Turner <moshiturner@protonmail.com>
18 * @author Korcan Hussein <korcan.hussein@collabora.com>
19 * @ingroup aux_vk
20 */
21
22#pragma once
23
24#include "xrt/xrt_compiler.h"
25#include "xrt/xrt_compositor.h"
27#include "xrt/xrt_handles.h"
28#include "util/u_logging.h"
29#include "util/u_string_list.h"
30#include "os/os_threading.h"
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36#define VK_BUNDLE_MAX_QUEUES 2
37
38/*
39 *
40 * Structs
41 *
42 */
43
45{
46 //! The queue family index
47 uint32_t family_index;
48 //! The queue (instance) index
49 uint32_t index;
50};
51
52#define VK_NULL_QUEUE_PAIR \
53 XRT_C11_COMPOUND(struct vk_queue_pair) \
54 { \
55 .family_index = VK_QUEUE_FAMILY_IGNORED, .index = (uint32_t)-1, \
56 }
57
59{
60 //! The Vulkan queue handle
61 VkQueue queue;
62 //! The queue family index
63 uint32_t family_index;
64 //! The queue (instance) index
65 uint32_t index;
66 //! The queue mutex - @see vk_queue_lock, vk_queue_unlock
68};
69
70/*!
71 * A bundle of Vulkan functions and objects, used by both @ref comp and @ref
72 * comp_client. Note that they both have different instances of the object, and
73 * thus different VkInstance, etc.
74 *
75 * @ingroup aux_vk
76 */
78{
79 enum u_logging_level log_level;
80
81 VkInstance instance;
82 uint32_t version;
83 VkPhysicalDevice physical_device;
84 int physical_device_index;
85 VkDevice device;
86
87 /*!
88 * @brief queues - a free list of **unique** queues
89 *
90 * One per uniquely identifiable vk queue (family x instance index),
91 * duplicate entries must not be stored.
92 *
93 * Should not be used directly, @see main_queue, encode_queue
94 */
95 struct vk_bundle_queue queues[VK_BUNDLE_MAX_QUEUES];
96
97 struct vk_bundle_queue *main_queue;
98#if defined(VK_KHR_video_encode_queue)
99 struct vk_bundle_queue *encode_queue;
100#endif
101
102 struct
103 {
104#if defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_WIN32_HANDLE)
105 bool color_image_import_opaque_win32;
106 bool color_image_export_opaque_win32;
107 bool depth_image_import_opaque_win32;
108 bool depth_image_export_opaque_win32;
109
110 bool color_image_import_d3d11;
111 bool color_image_export_d3d11;
112 bool depth_image_import_d3d11;
113 bool depth_image_export_d3d11;
114
115#elif defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_FD)
116 bool color_image_import_opaque_fd;
117 bool color_image_export_opaque_fd;
118 bool depth_image_import_opaque_fd;
119 bool depth_image_export_opaque_fd;
120
121#elif defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_AHARDWAREBUFFER)
122 bool color_image_import_opaque_fd;
123 bool color_image_export_opaque_fd;
124 bool depth_image_import_opaque_fd;
125 bool depth_image_export_opaque_fd;
126
127 bool color_image_import_ahardwarebuffer;
128 bool color_image_export_ahardwarebuffer;
129 bool depth_image_import_ahardwarebuffer;
130 bool depth_image_export_ahardwarebuffer;
131#endif
132
133#if defined(XRT_GRAPHICS_SYNC_HANDLE_IS_FD)
134 bool fence_sync_fd;
135 bool fence_opaque_fd;
136
137 bool binary_semaphore_sync_fd;
138 bool binary_semaphore_opaque_fd;
139
140 bool timeline_semaphore_sync_fd;
141 bool timeline_semaphore_opaque_fd;
142#elif defined(XRT_GRAPHICS_SYNC_HANDLE_IS_WIN32_HANDLE)
143 bool fence_win32_handle;
144
145 bool binary_semaphore_d3d12_fence;
146 bool binary_semaphore_win32_handle;
147
148 bool timeline_semaphore_d3d12_fence;
149 bool timeline_semaphore_win32_handle;
150#else
151#error "Need port for fence sync handles checkers"
152#endif
153 } external;
154
155 // beginning of GENERATED instance extension code - do not modify - used by scripts
156 bool has_KHR_external_memory_capabilities;
157 bool has_EXT_display_surface_counter;
158 bool has_EXT_swapchain_colorspace;
159 bool has_EXT_debug_utils;
160 // end of GENERATED instance extension code - do not modify - used by scripts
161
162 // beginning of GENERATED device extension code - do not modify - used by scripts
163 bool has_KHR_8bit_storage;
164 bool has_KHR_external_fence_fd;
165 bool has_KHR_external_memory;
166 bool has_KHR_external_semaphore_fd;
167 bool has_KHR_format_feature_flags2;
168 bool has_KHR_global_priority;
169 bool has_KHR_image_format_list;
170 bool has_KHR_maintenance1;
171 bool has_KHR_maintenance2;
172 bool has_KHR_maintenance3;
173 bool has_KHR_maintenance4;
174 bool has_KHR_present_wait;
175 bool has_KHR_synchronization2;
176 bool has_KHR_timeline_semaphore;
177 bool has_KHR_video_maintenance1;
178 bool has_EXT_calibrated_timestamps;
179 bool has_EXT_display_control;
180 bool has_EXT_external_memory_dma_buf;
181 bool has_EXT_external_memory_host;
182 bool has_EXT_global_priority;
183 bool has_EXT_image_drm_format_modifier;
184 bool has_EXT_robustness2;
185 bool has_ANDROID_external_format_resolve;
186 bool has_GOOGLE_display_timing;
187 // end of GENERATED device extension code - do not modify - used by scripts
188
189 struct
190 {
191 //! Are timestamps available for compute and graphics queues?
193
194 //! Nanoseconds per gpu tick.
196
197 //! Valid bits in the queue selected.
199
200 //! Were timeline semaphore requested, available, and enabled?
202
203 //! Was synchronization2 requested, available, and enabled?
205
206 //! Was KHR_present_wait requested, available, and enabled?
208
209 //! Was KHR_video_maintenance1 requested, available, and enabled?
211 } features;
212
213 struct
214 {
215 //! Maximum number of sampler objects, as created by vkCreateSampler, which can simultaneously exist on
217
218 //! Maximum number of descriptor sets that can be simultaneously used by a pipeline.
220
221 //! Maximum number of samplers that can be included in a pipeline layout.
223
224 //! Maximum number of sampled images that can be included in a pipeline layout.
226
227 //! Maximum number of samplers that can be accessible to a single shader stage in a pipeline layout.
229
230 //! Per stage limit on sampled images (includes combined).
232
233 //! Per stage limit on storage images.
235 } limits;
236
237 //! Is the GPU a tegra device.
239
240
241 VkDebugReportCallbackEXT debug_report_cb;
242
243 VkPhysicalDeviceMemoryProperties device_memory_props;
244
245 // Loader functions
246 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr;
247 PFN_vkCreateInstance vkCreateInstance;
248 PFN_vkEnumerateInstanceExtensionProperties vkEnumerateInstanceExtensionProperties;
249
250 // beginning of GENERATED instance loader code - do not modify - used by scripts
251 PFN_vkDestroyInstance vkDestroyInstance;
252 PFN_vkGetDeviceProcAddr vkGetDeviceProcAddr;
253 PFN_vkCreateDevice vkCreateDevice;
254 PFN_vkDestroySurfaceKHR vkDestroySurfaceKHR;
255
256 PFN_vkCreateDebugReportCallbackEXT vkCreateDebugReportCallbackEXT;
257 PFN_vkDestroyDebugReportCallbackEXT vkDestroyDebugReportCallbackEXT;
258
259 PFN_vkEnumeratePhysicalDevices vkEnumeratePhysicalDevices;
260 PFN_vkGetPhysicalDeviceProperties vkGetPhysicalDeviceProperties;
261 PFN_vkGetPhysicalDeviceProperties2KHR vkGetPhysicalDeviceProperties2;
262 PFN_vkGetPhysicalDeviceFeatures2KHR vkGetPhysicalDeviceFeatures2;
263 PFN_vkGetPhysicalDeviceMemoryProperties vkGetPhysicalDeviceMemoryProperties;
264 PFN_vkGetPhysicalDeviceQueueFamilyProperties vkGetPhysicalDeviceQueueFamilyProperties;
265 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR vkGetPhysicalDeviceSurfaceCapabilitiesKHR;
266 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR vkGetPhysicalDeviceSurfaceFormatsKHR;
267 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR vkGetPhysicalDeviceSurfacePresentModesKHR;
268 PFN_vkGetPhysicalDeviceSurfaceSupportKHR vkGetPhysicalDeviceSurfaceSupportKHR;
269 PFN_vkGetPhysicalDeviceFormatProperties vkGetPhysicalDeviceFormatProperties;
270 PFN_vkGetPhysicalDeviceFormatProperties2KHR vkGetPhysicalDeviceFormatProperties2;
271 PFN_vkGetPhysicalDeviceImageFormatProperties2KHR vkGetPhysicalDeviceImageFormatProperties2;
272 PFN_vkGetPhysicalDeviceExternalBufferPropertiesKHR vkGetPhysicalDeviceExternalBufferPropertiesKHR;
273 PFN_vkGetPhysicalDeviceExternalFencePropertiesKHR vkGetPhysicalDeviceExternalFencePropertiesKHR;
274 PFN_vkGetPhysicalDeviceExternalSemaphorePropertiesKHR vkGetPhysicalDeviceExternalSemaphorePropertiesKHR;
275 PFN_vkEnumerateDeviceExtensionProperties vkEnumerateDeviceExtensionProperties;
276 PFN_vkEnumerateDeviceLayerProperties vkEnumerateDeviceLayerProperties;
277
278#if defined(VK_EXT_calibrated_timestamps)
279 PFN_vkGetPhysicalDeviceCalibrateableTimeDomainsEXT vkGetPhysicalDeviceCalibrateableTimeDomainsEXT;
280
281#endif // defined(VK_EXT_calibrated_timestamps)
282
283#if defined(VK_USE_PLATFORM_DISPLAY_KHR)
284 PFN_vkCreateDisplayPlaneSurfaceKHR vkCreateDisplayPlaneSurfaceKHR;
285 PFN_vkGetDisplayPlaneCapabilitiesKHR vkGetDisplayPlaneCapabilitiesKHR;
286 PFN_vkGetPhysicalDeviceDisplayPropertiesKHR vkGetPhysicalDeviceDisplayPropertiesKHR;
287 PFN_vkGetPhysicalDeviceDisplayPlanePropertiesKHR vkGetPhysicalDeviceDisplayPlanePropertiesKHR;
288 PFN_vkGetDisplayModePropertiesKHR vkGetDisplayModePropertiesKHR;
289 PFN_vkReleaseDisplayEXT vkReleaseDisplayEXT;
290
291#endif // defined(VK_USE_PLATFORM_DISPLAY_KHR)
292
293#if defined(VK_USE_PLATFORM_XCB_KHR)
294 PFN_vkCreateXcbSurfaceKHR vkCreateXcbSurfaceKHR;
295
296#endif // defined(VK_USE_PLATFORM_XCB_KHR)
297
298#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
299 PFN_vkCreateWaylandSurfaceKHR vkCreateWaylandSurfaceKHR;
300
301#endif // defined(VK_USE_PLATFORM_WAYLAND_KHR)
302
303#if defined(VK_USE_PLATFORM_WAYLAND_KHR) && defined(VK_EXT_acquire_drm_display)
304 PFN_vkAcquireDrmDisplayEXT vkAcquireDrmDisplayEXT;
305 PFN_vkGetDrmDisplayEXT vkGetDrmDisplayEXT;
306
307#endif // defined(VK_USE_PLATFORM_WAYLAND_KHR) && defined(VK_EXT_acquire_drm_display)
308
309#if defined(VK_USE_PLATFORM_XLIB_XRANDR_EXT)
310 PFN_vkGetRandROutputDisplayEXT vkGetRandROutputDisplayEXT;
311 PFN_vkAcquireXlibDisplayEXT vkAcquireXlibDisplayEXT;
312
313#endif // defined(VK_USE_PLATFORM_XLIB_XRANDR_EXT)
314
315#if defined(VK_USE_PLATFORM_ANDROID_KHR)
316 PFN_vkCreateAndroidSurfaceKHR vkCreateAndroidSurfaceKHR;
317
318#endif // defined(VK_USE_PLATFORM_ANDROID_KHR)
319
320#if defined(VK_USE_PLATFORM_WIN32_KHR)
321 PFN_vkCreateWin32SurfaceKHR vkCreateWin32SurfaceKHR;
322
323#endif // defined(VK_USE_PLATFORM_WIN32_KHR)
324
325#if defined(VK_EXT_display_surface_counter)
326 PFN_vkGetPhysicalDeviceSurfaceCapabilities2EXT vkGetPhysicalDeviceSurfaceCapabilities2EXT;
327
328#endif // defined(VK_EXT_display_surface_counter)
329
330#if defined(VK_EXT_debug_utils)
331 PFN_vkCreateDebugUtilsMessengerEXT vkCreateDebugUtilsMessengerEXT;
332 PFN_vkSubmitDebugUtilsMessageEXT vkSubmitDebugUtilsMessageEXT;
333 PFN_vkDestroyDebugUtilsMessengerEXT vkDestroyDebugUtilsMessengerEXT;
334#endif // defined(VK_EXT_debug_utils)
335
336 // end of GENERATED instance loader code - do not modify - used by scripts
337
338 // beginning of GENERATED device loader code - do not modify - used by scripts
339 PFN_vkDestroyDevice vkDestroyDevice;
340 PFN_vkDeviceWaitIdle vkDeviceWaitIdle;
341 PFN_vkAllocateMemory vkAllocateMemory;
342 PFN_vkFreeMemory vkFreeMemory;
343 PFN_vkMapMemory vkMapMemory;
344 PFN_vkUnmapMemory vkUnmapMemory;
345
346 PFN_vkCreateBuffer vkCreateBuffer;
347 PFN_vkDestroyBuffer vkDestroyBuffer;
348 PFN_vkBindBufferMemory vkBindBufferMemory;
349
350 PFN_vkCreateImage vkCreateImage;
351 PFN_vkDestroyImage vkDestroyImage;
352 PFN_vkBindImageMemory vkBindImageMemory;
353
354 PFN_vkGetBufferMemoryRequirements vkGetBufferMemoryRequirements;
355 PFN_vkFlushMappedMemoryRanges vkFlushMappedMemoryRanges;
356 PFN_vkGetImageMemoryRequirements vkGetImageMemoryRequirements;
357 PFN_vkGetImageMemoryRequirements2KHR vkGetImageMemoryRequirements2;
358 PFN_vkGetImageSubresourceLayout vkGetImageSubresourceLayout;
359
360 PFN_vkCreateImageView vkCreateImageView;
361 PFN_vkDestroyImageView vkDestroyImageView;
362
363 PFN_vkCreateSampler vkCreateSampler;
364 PFN_vkDestroySampler vkDestroySampler;
365
366 PFN_vkCreateShaderModule vkCreateShaderModule;
367 PFN_vkDestroyShaderModule vkDestroyShaderModule;
368
369 PFN_vkCreateQueryPool vkCreateQueryPool;
370 PFN_vkDestroyQueryPool vkDestroyQueryPool;
371 PFN_vkGetQueryPoolResults vkGetQueryPoolResults;
372
373 PFN_vkCreateCommandPool vkCreateCommandPool;
374 PFN_vkDestroyCommandPool vkDestroyCommandPool;
375 PFN_vkResetCommandPool vkResetCommandPool;
376
377 PFN_vkAllocateCommandBuffers vkAllocateCommandBuffers;
378 PFN_vkBeginCommandBuffer vkBeginCommandBuffer;
379 PFN_vkCmdBeginQuery vkCmdBeginQuery;
380 PFN_vkCmdCopyQueryPoolResults vkCmdCopyQueryPoolResults;
381 PFN_vkCmdEndQuery vkCmdEndQuery;
382 PFN_vkCmdResetQueryPool vkCmdResetQueryPool;
383 PFN_vkCmdWriteTimestamp vkCmdWriteTimestamp;
384 PFN_vkCmdPipelineBarrier vkCmdPipelineBarrier;
385 PFN_vkCmdBeginRenderPass vkCmdBeginRenderPass;
386 PFN_vkCmdSetScissor vkCmdSetScissor;
387 PFN_vkCmdSetViewport vkCmdSetViewport;
388 PFN_vkCmdClearColorImage vkCmdClearColorImage;
389 PFN_vkCmdEndRenderPass vkCmdEndRenderPass;
390 PFN_vkCmdBindDescriptorSets vkCmdBindDescriptorSets;
391 PFN_vkCmdBindPipeline vkCmdBindPipeline;
392 PFN_vkCmdBindVertexBuffers vkCmdBindVertexBuffers;
393 PFN_vkCmdBindIndexBuffer vkCmdBindIndexBuffer;
394 PFN_vkCmdDraw vkCmdDraw;
395 PFN_vkCmdDrawIndexed vkCmdDrawIndexed;
396 PFN_vkCmdDispatch vkCmdDispatch;
397 PFN_vkCmdCopyBuffer vkCmdCopyBuffer;
398 PFN_vkCmdCopyBufferToImage vkCmdCopyBufferToImage;
399 PFN_vkCmdCopyImage vkCmdCopyImage;
400 PFN_vkCmdCopyImageToBuffer vkCmdCopyImageToBuffer;
401 PFN_vkCmdBlitImage vkCmdBlitImage;
402 PFN_vkCmdPushConstants vkCmdPushConstants;
403 PFN_vkEndCommandBuffer vkEndCommandBuffer;
404 PFN_vkFreeCommandBuffers vkFreeCommandBuffers;
405
406 PFN_vkCreateRenderPass vkCreateRenderPass;
407 PFN_vkDestroyRenderPass vkDestroyRenderPass;
408
409 PFN_vkCreateFramebuffer vkCreateFramebuffer;
410 PFN_vkDestroyFramebuffer vkDestroyFramebuffer;
411
412 PFN_vkCreatePipelineCache vkCreatePipelineCache;
413 PFN_vkDestroyPipelineCache vkDestroyPipelineCache;
414
415 PFN_vkResetDescriptorPool vkResetDescriptorPool;
416 PFN_vkCreateDescriptorPool vkCreateDescriptorPool;
417 PFN_vkDestroyDescriptorPool vkDestroyDescriptorPool;
418
419 PFN_vkAllocateDescriptorSets vkAllocateDescriptorSets;
420 PFN_vkFreeDescriptorSets vkFreeDescriptorSets;
421
422 PFN_vkCreateComputePipelines vkCreateComputePipelines;
423 PFN_vkCreateGraphicsPipelines vkCreateGraphicsPipelines;
424 PFN_vkDestroyPipeline vkDestroyPipeline;
425
426 PFN_vkCreatePipelineLayout vkCreatePipelineLayout;
427 PFN_vkDestroyPipelineLayout vkDestroyPipelineLayout;
428
429 PFN_vkCreateDescriptorSetLayout vkCreateDescriptorSetLayout;
430 PFN_vkUpdateDescriptorSets vkUpdateDescriptorSets;
431 PFN_vkDestroyDescriptorSetLayout vkDestroyDescriptorSetLayout;
432
433 PFN_vkGetDeviceQueue vkGetDeviceQueue;
434 PFN_vkQueueSubmit vkQueueSubmit;
435 PFN_vkQueueWaitIdle vkQueueWaitIdle;
436
437 PFN_vkCreateSemaphore vkCreateSemaphore;
438#if defined(VK_KHR_timeline_semaphore)
439 PFN_vkSignalSemaphoreKHR vkSignalSemaphore;
440 PFN_vkWaitSemaphoresKHR vkWaitSemaphores;
441 PFN_vkGetSemaphoreCounterValueKHR vkGetSemaphoreCounterValue;
442#endif // defined(VK_KHR_timeline_semaphore)
443
444 PFN_vkDestroySemaphore vkDestroySemaphore;
445
446 PFN_vkCreateFence vkCreateFence;
447 PFN_vkWaitForFences vkWaitForFences;
448 PFN_vkGetFenceStatus vkGetFenceStatus;
449 PFN_vkDestroyFence vkDestroyFence;
450 PFN_vkResetFences vkResetFences;
451
452 PFN_vkCreateSwapchainKHR vkCreateSwapchainKHR;
453 PFN_vkDestroySwapchainKHR vkDestroySwapchainKHR;
454 PFN_vkGetSwapchainImagesKHR vkGetSwapchainImagesKHR;
455 PFN_vkAcquireNextImageKHR vkAcquireNextImageKHR;
456 PFN_vkQueuePresentKHR vkQueuePresentKHR;
457
458#if defined(VK_USE_PLATFORM_WIN32_KHR)
459 PFN_vkGetMemoryWin32HandleKHR vkGetMemoryWin32HandleKHR;
460 PFN_vkGetFenceWin32HandleKHR vkGetFenceWin32HandleKHR;
461 PFN_vkGetSemaphoreWin32HandleKHR vkGetSemaphoreWin32HandleKHR;
462 PFN_vkImportFenceWin32HandleKHR vkImportFenceWin32HandleKHR;
463 PFN_vkImportSemaphoreWin32HandleKHR vkImportSemaphoreWin32HandleKHR;
464
465#endif // defined(VK_USE_PLATFORM_WIN32_KHR)
466
467#if !defined(VK_USE_PLATFORM_WIN32_KHR)
468 PFN_vkGetMemoryFdKHR vkGetMemoryFdKHR;
469 PFN_vkGetFenceFdKHR vkGetFenceFdKHR;
470 PFN_vkGetSemaphoreFdKHR vkGetSemaphoreFdKHR;
471 PFN_vkImportFenceFdKHR vkImportFenceFdKHR;
472 PFN_vkImportSemaphoreFdKHR vkImportSemaphoreFdKHR;
473
474#endif // !defined(VK_USE_PLATFORM_WIN32_KHR)
475
476#if defined(VK_USE_PLATFORM_ANDROID_KHR)
477 PFN_vkGetMemoryAndroidHardwareBufferANDROID vkGetMemoryAndroidHardwareBufferANDROID;
478 PFN_vkGetAndroidHardwareBufferPropertiesANDROID vkGetAndroidHardwareBufferPropertiesANDROID;
479
480#endif // defined(VK_USE_PLATFORM_ANDROID_KHR)
481
482#if defined(VK_EXT_external_memory_host)
483 PFN_vkGetMemoryHostPointerPropertiesEXT vkGetMemoryHostPointerPropertiesEXT;
484
485#endif // defined(VK_EXT_external_memory_host)
486
487#if defined(VK_EXT_calibrated_timestamps)
488 PFN_vkGetCalibratedTimestampsEXT vkGetCalibratedTimestampsEXT;
489
490#endif // defined(VK_EXT_calibrated_timestamps)
491
492 PFN_vkGetPastPresentationTimingGOOGLE vkGetPastPresentationTimingGOOGLE;
493
494#if defined(VK_EXT_display_control)
495 PFN_vkGetSwapchainCounterEXT vkGetSwapchainCounterEXT;
496 PFN_vkRegisterDeviceEventEXT vkRegisterDeviceEventEXT;
497 PFN_vkRegisterDisplayEventEXT vkRegisterDisplayEventEXT;
498
499#endif // defined(VK_EXT_display_control)
500
501#if defined(VK_EXT_image_drm_format_modifier)
502 PFN_vkGetImageDrmFormatModifierPropertiesEXT vkGetImageDrmFormatModifierPropertiesEXT;
503
504#endif // defined(VK_EXT_image_drm_format_modifier)
505
506#if defined(VK_EXT_debug_utils)
507 PFN_vkCmdBeginDebugUtilsLabelEXT vkCmdBeginDebugUtilsLabelEXT;
508 PFN_vkCmdEndDebugUtilsLabelEXT vkCmdEndDebugUtilsLabelEXT;
509 PFN_vkCmdInsertDebugUtilsLabelEXT vkCmdInsertDebugUtilsLabelEXT;
510 PFN_vkQueueBeginDebugUtilsLabelEXT vkQueueBeginDebugUtilsLabelEXT;
511 PFN_vkQueueEndDebugUtilsLabelEXT vkQueueEndDebugUtilsLabelEXT;
512 PFN_vkQueueInsertDebugUtilsLabelEXT vkQueueInsertDebugUtilsLabelEXT;
513 PFN_vkSetDebugUtilsObjectNameEXT vkSetDebugUtilsObjectNameEXT;
514 PFN_vkSetDebugUtilsObjectTagEXT vkSetDebugUtilsObjectTagEXT;
515
516#endif // defined(VK_EXT_debug_utils)
517
518#if defined(VK_KHR_present_wait)
519 PFN_vkWaitForPresentKHR vkWaitForPresentKHR;
520#endif // defined(VK_KHR_present_wait)
521
522 // end of GENERATED device loader code - do not modify - used by scripts
523};
524
526{
527 VkBuffer handle;
528 VkDeviceMemory memory;
529 uint32_t size;
530 void *data;
531};
532
533
534/*
535 *
536 * Helper defines.
537 *
538 */
539
540/*!
541 * This define will error if `RET` is not `VK_SUCCESS`, printing out that the
542 * @p FUNC_STR string has failed, then returns @p RET. The implicit argument
543 * @p vk will be used for the @ref vk_print_result call.
544 *
545 * Use this macro when your function returns a `VkResult`.
546 *
547 * @param RET The @p VkResult to check.
548 * @param FUNC_STR String literal with the function name, used for logging.
549 *
550 * @ingroup aux_vk
551 *
552 * @see VK_CHK_WITH_RET
553 * @see VK_CHK_WITH_GOTO
554 */
555#define VK_CHK_AND_RET(RET, FUNC_STR) \
556 do { \
557 VkResult _ret = RET; \
558 if (_ret != VK_SUCCESS) { \
559 vk_print_result(vk, __FILE__, __LINE__, __func__, _ret, FUNC_STR); \
560 return _ret; \
561 } \
562 } while (false)
563
564/*!
565 * This define will error if @p RET is not @p VK_SUCCESS, printing out that the
566 * @p FUNC_STR string has failed, then returns @p TO_RET. The implicit argument
567 * @p vk will be used for the @ref vk_print_result call.
568 *
569 * Use this macro when your function doesn't return a `VkResult`.
570 *
571 * @param RET The @p VkResult to check.
572 * @param FUNC_STR String literal with the function name, used for logging.
573 * @param TO_RET Value to return, upon error
574 *
575 * @ingroup aux_vk
576 *
577 * @see VK_CHK_AND_RET
578 * @see VK_CHK_WITH_GOTO
579 */
580#define VK_CHK_WITH_RET(RET, FUNC_STR, TO_RET) \
581 do { \
582 VkResult _ret = RET; \
583 if (_ret != VK_SUCCESS) { \
584 vk_print_result(vk, __FILE__, __LINE__, __func__, _ret, FUNC_STR); \
585 return TO_RET; \
586 } \
587 } while (false)
588
589/*!
590 * This define will error if @p RET is not @p VK_SUCCESS, printing out that the
591 * @p FUNC_STR string has failed, then goto @p GOTO. The implicit argument @p vk
592 * will be used for the @ref vk_print_result call.
593 *
594 * Use this macro when your function needs to `goto` some cleanup code and
595 * return from there.
596 *
597 * @param RET The @p VkResult to check.
598 * @param FUNC_STR String literal with the function name, used for logging.
599 * @param GOTO Label to jump to, upon error
600 *
601 * @ingroup aux_vk
602 *
603 * @see VK_CHK_AND_RET
604 * @see VK_CHK_WITH_RET
605 */
606#define VK_CHK_WITH_GOTO(RET, FUNC_STR, GOTO) \
607 do { \
608 VkResult _ret = RET; \
609 if (_ret != VK_SUCCESS) { \
610 vk_print_result(vk, __FILE__, __LINE__, __func__, _ret, FUNC_STR); \
611 goto GOTO; \
612 } \
613 } while (false)
614
615
616static inline void
617vk_append_to_pnext_chain(VkBaseInStructure *head, VkBaseInStructure *new_struct)
618{
619 assert(new_struct->pNext == NULL);
620 // Insert ourselves between head and its previous pNext
621 new_struct->pNext = head->pNext;
622 head->pNext = new_struct;
623}
624
625/*
626 *
627 * String helper functions.
628 *
629 */
630
631XRT_CHECK_RESULT const char *
632vk_result_string(VkResult code);
633
634XRT_CHECK_RESULT const char *
635vk_object_type_string(VkObjectType type);
636
637XRT_CHECK_RESULT const char *
638vk_physical_device_type_string(VkPhysicalDeviceType device_type);
639
640XRT_CHECK_RESULT const char *
641vk_format_string(VkFormat code);
642
643XRT_CHECK_RESULT const char *
644vk_sharing_mode_string(VkSharingMode code);
645
646XRT_CHECK_RESULT const char *
647vk_present_mode_string(VkPresentModeKHR code);
648
649XRT_CHECK_RESULT const char *
650vk_color_space_string(VkColorSpaceKHR code);
651
652XRT_CHECK_RESULT const char *
653vk_power_state_string(VkDisplayPowerStateEXT code);
654
655
656/*
657 *
658 * Flag bits string functions.
659 *
660 */
661
662/*!
663 * Returns the format feature flag if one valid bit is set,
664 * if multiple bits are set, will return 'MULTIPLE BIT SET'.
665 */
666XRT_CHECK_RESULT const char *
667vk_format_feature_flag_string(VkFormatFeatureFlagBits bits, bool null_on_unknown);
668
669/*!
670 * Returns the image usage flag if one valid bit is set,
671 * if multiple bits are set, will return 'MULTIPLE BIT SET'.
672 */
673XRT_CHECK_RESULT const char *
674vk_image_usage_flag_string(VkImageUsageFlagBits bits, bool null_on_unknown);
675
676/*!
677 * Returns the composite alpha flag if one valid bit is set,
678 * if multiple bits are set, will return 'MULTIPLE BIT SET'.
679 */
680XRT_CHECK_RESULT const char *
681vk_composite_alpha_flag_string(VkCompositeAlphaFlagBitsKHR bits, bool null_on_unknown);
682
683/*!
684 * Returns the surface transform flag if one valid bit is set,
685 * if multiple bits are set, will return 'MULTIPLE BIT SET'.
686 */
687XRT_CHECK_RESULT const char *
688vk_surface_transform_flag_string(VkSurfaceTransformFlagBitsKHR bits, bool null_on_unknown);
689
690#ifdef VK_KHR_display
691/*!
692 * Returns the display plane alpha flag if one valid bit is set,
693 * if multiple bits are set, will return 'MULTIPLE BIT SET'.
694 */
695XRT_CHECK_RESULT const char *
696vk_display_plane_alpha_flag_string(VkDisplayPlaneAlphaFlagBitsKHR bits, bool null_on_unknown);
697#endif
698
699/*!
700 * Returns xrt swapchain_usage flag if one valid bit is set,
701 * if multiple bits are set, will return 'MULTIPLE BIT SET'.
702 */
703XRT_CHECK_RESULT const char *
704xrt_swapchain_usage_flag_string(enum xrt_swapchain_usage_bits bits, bool null_on_unknown);
705
706
707/*
708 *
709 * Function and helpers.
710 *
711 */
712
713#define VK_TRACE(d, ...) U_LOG_IFL_T(d->log_level, __VA_ARGS__)
714#define VK_DEBUG(d, ...) U_LOG_IFL_D(d->log_level, __VA_ARGS__)
715#define VK_INFO(d, ...) U_LOG_IFL_I(d->log_level, __VA_ARGS__)
716#define VK_WARN(d, ...) U_LOG_IFL_W(d->log_level, __VA_ARGS__)
717#define VK_ERROR(d, ...) U_LOG_IFL_E(d->log_level, __VA_ARGS__)
718
719
720/*
721 *
722 * Debug helper functions, in the vk_debug.c file.
723 *
724 */
725
726#if defined(VK_EXT_debug_utils) || defined(XRT_DOXYGEN)
727
728/*!
729 * Uses VK_EXT_debug_utils to set a name for an object, for easier debugging.
730 *
731 * @ingroup aux_vk
732 */
733void
734vk_name_object(struct vk_bundle *vk, VkObjectType type, uint64_t object, const char *name);
735
736/*!
737 * Small helper for @ref vk_name_object that makes use of pre-process to avoid
738 * writing out long type names.
739 *
740 * @ingroup aux_vk
741 */
742#define VK_NAME_OBJ(VK, TYPE, SUFFIX, OBJ, NAME) \
743 do { \
744 if ((VK)->has_EXT_debug_utils) { \
745 XRT_MAYBE_UNUSED TYPE _thing = (TYPE)(OBJ); \
746 vk_name_object(VK, VK_OBJECT_TYPE_##SUFFIX, (uint64_t)OBJ, NAME); \
747 } \
748 } while (false)
749
750
751#else
752
753#define VK_NAME_OBJ(VK, TYPE, SUFFIX, OBJ, NAME) VK_NAME_OBJ_DISABLED(VK, TYPE, OBJ)
754
755#endif
756
757/*!
758 * Some combinations of Vulkan implementation and types are broken, we still
759 * want type safety so we have this define. Examples of broken combinations:
760 *
761 * 1. Both Mesa and the Vulkan loader didn't support setting names on the
762 * VkInstance, loader got support in 1.3.261 and Mesa hasn't as of writing.
763 * 2. For Mesa drivers we can not name VkSurfaceKHR objects on some systems as
764 * it causes memory corruption, asserts, crashes or functions failing. This
765 * is as of writing broken on the 23.2.1 release, fixed in main and scheduled
766 * for the 23.2.2 release.
767 * 3. Mesa RADV leaks the name strings for VkDescriptorSet objects for pools
768 * that we use the reset function.
769 *
770 * @ingroup aux_vk
771 */
772#define VK_NAME_OBJ_DISABLED(VK, TYPE, OBJ) \
773 do { \
774 XRT_MAYBE_UNUSED TYPE _thing = (TYPE)(OBJ); \
775 } while (false)
776
777
778// clang-format off
779// VK_DEFINE_HANDLE types are always pointers
780#define VK_NAME_INSTANCE(VK, OBJ, NAME) VK_NAME_OBJ_DISABLED(VK, VkInstance, (uintptr_t)OBJ)
781#define VK_NAME_PHYSICAL_DEVICE(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkPhysicalDevice, PHYSICAL_DEVICE, (uintptr_t)OBJ, NAME)
782#define VK_NAME_DEVICE(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkDevice, DEVICE, (uintptr_t)OBJ, NAME)
783#define VK_NAME_QUEUE(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkQueue, QUEUE, (uintptr_t)OBJ, NAME)
784#define VK_NAME_COMMAND_BUFFER(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkCommandBuffer, COMMAND_BUFFER, (uintptr_t)OBJ, NAME)
785// VK_DEFINE_NON_DISPATCHABLE_HANDLE types are pointers in 64-bits and uint64_t in 32-bits
786#define VK_NAME_SEMAPHORE(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkSemaphore, SEMAPHORE, OBJ, NAME)
787#define VK_NAME_FENCE(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkFence, FENCE, OBJ, NAME)
788#define VK_NAME_DEVICE_MEMORY(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkDeviceMemory, DEVICE_MEMORY, OBJ, NAME)
789#define VK_NAME_BUFFER(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkBuffer, BUFFER, OBJ, NAME)
790#define VK_NAME_IMAGE(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkImage, IMAGE, OBJ, NAME)
791#define VK_NAME_EVENT(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkEvent, EVENT, OBJ, NAME)
792#define VK_NAME_QUERY_POOL(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkQueryPool, QUERY_POOL, OBJ, NAME)
793#define VK_NAME_BUFFER_VIEW(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkBufferView, BUFFER_VIEW, OBJ, NAME)
794#define VK_NAME_IMAGE_VIEW(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkImageView, IMAGE_VIEW, OBJ, NAME)
795#define VK_NAME_SHADER_MODULE(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkShaderModule, SHADER_MODULE, OBJ, NAME)
796#define VK_NAME_PIPELINE_CACHE(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkPipelineCache, PIPELINE_CACHE, OBJ, NAME)
797#define VK_NAME_PIPELINE_LAYOUT(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkPipelineLayout, PIPELINE_LAYOUT, OBJ, NAME)
798#define VK_NAME_RENDER_PASS(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkRenderPass, RENDER_PASS, OBJ, NAME)
799#define VK_NAME_PIPELINE(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkPipeline, PIPELINE, OBJ, NAME)
800#define VK_NAME_DESCRIPTOR_SET_LAYOUT(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkDescriptorSetLayout, DESCRIPTOR_SET_LAYOUT, OBJ, NAME)
801#define VK_NAME_SAMPLER(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkSampler, SAMPLER, OBJ, NAME)
802#define VK_NAME_DESCRIPTOR_POOL(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkDescriptorPool, DESCRIPTOR_POOL, OBJ, NAME)
803#define VK_NAME_DESCRIPTOR_SET(VK, OBJ, NAME) VK_NAME_OBJ_DISABLED(VK, VkDescriptorSet, OBJ)
804#define VK_NAME_FRAMEBUFFER(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkFramebuffer, FRAMEBUFFER, OBJ, NAME)
805#define VK_NAME_COMMAND_POOL(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkCommandPool, COMMAND_POOL, OBJ, NAME)
806
807#define VK_NAME_SURFACE(VK, OBJ, NAME) VK_NAME_OBJ_DISABLED(VK, VkSurfaceKHR, OBJ)
808#define VK_NAME_SWAPCHAIN(VK, OBJ, NAME) VK_NAME_OBJ(VK, VkSwapchainKHR, SWAPCHAIN_KHR, OBJ, NAME)
809// clang-format on
810
811
812/*
813 *
814 * Printing helpers, in the vk_print.c file.
815 *
816 */
817
818/*!
819 * Print the result of a function, info level if ret == `VK_SUCCESS` and error
820 * level otherwise. Also prints file and line.
821 *
822 * @ingroup aux_vk
823 */
824void
826 struct vk_bundle *vk, const char *file, int line, const char *calling_func, VkResult ret, const char *called_func);
827
828/*!
829 * Print device information to the logger at the given logging level,
830 * if the vk_bundle has that level enabled.
831 *
832 * @ingroup aux_vk
833 */
834void
836 enum u_logging_level log_level,
837 const VkPhysicalDeviceProperties *pdp,
838 uint32_t gpu_index,
839 const char *title);
840
841/*!
842 * Print device information about the device that bundle manages at the given
843 * logging level if the vk_bundle has that level enabled.
844 *
845 * @ingroup aux_vk
846 */
847void
848vk_print_opened_device_info(struct vk_bundle *vk, enum u_logging_level log_level);
849
850/*!
851 * Print device features to the logger at the given logging level, if the
852 * vk_bundle has that level enabled.
853 */
854void
855vk_print_features_info(struct vk_bundle *vk, enum u_logging_level log_level);
856
857/*!
858 * Print external handle features to the logger at the given logging level,
859 * if the vk_bundle has that level enabled.
860 */
861void
863
864/*!
865 * Print a @p VkSwapchainCreateInfoKHR, used to log during creation.
866 */
867void
868vk_print_swapchain_create_info(struct vk_bundle *vk, VkSwapchainCreateInfoKHR *i, enum u_logging_level log_level);
869
870#ifdef VK_KHR_display
871/*!
872 * Print a @p VkDisplaySurfaceCreateInfoKHR, used to log during creation.
873 */
874void
875vk_print_display_surface_create_info(struct vk_bundle *vk,
876 VkDisplaySurfaceCreateInfoKHR *i,
877 enum u_logging_level log_level);
878#endif
879
880/*!
881 * Print queue info to the logger at the given logging level,
882 * if the vk_bundle has that level enabled.
883 */
884void
885vk_print_queues_info(const struct vk_bundle *vk, enum u_logging_level log_level);
886
887
888/*
889 *
890 * Enumeration helpers, in the vk_enumerate.c file.
891 *
892 */
893
894/*!
895 * Return the @p VkExtensionProperties of the given @p layer_name, NULL means
896 * the "base" driver instance.
897 *
898 * @ingroup aux_vk
899 */
900VkResult
902 const char *layer_name,
903 uint32_t *out_prop_count,
904 VkExtensionProperties **out_props);
905
906/*!
907 * Enumerate the physical devices of the @p VkInstance that has been opened on
908 * the given @ref vk_bundle.
909 *
910 * @ingroup aux_vk
911 */
912VkResult
914 uint32_t *out_physical_device_count,
915 VkPhysicalDevice **out_physical_devices);
916
917/*!
918 * Enumerate the extension properties of the given @p VkPhysicalDevice for the
919 * named @p layer_name, NULL means the "base" driver physical device.
920 *
921 * @ingroup aux_vk
922 */
923VkResult
925 VkPhysicalDevice physical_device,
926 const char *layer_name,
927 uint32_t *out_prop_count,
928 VkExtensionProperties **out_props);
929
930#if defined(VK_KHR_surface) || defined(XRT_DOXYGEN)
931/*!
932 * Enumerate the surface formats of the given @p VkSurfaceKHR,
933 * returns a list of @p VkSurfaceFormatKHR, not @p VkFormat.
934 *
935 * @ingroup aux_vk
936 */
937VkResult
939 VkSurfaceKHR surface,
940 uint32_t *out_format_count,
941 VkSurfaceFormatKHR **out_formats);
942
943/*!
944 * Enumerate the present modes of the given @p VkSurfaceKHR.
945 *
946 * @ingroup aux_vk
947 */
948VkResult
950 VkSurfaceKHR surface,
951 uint32_t *out_present_mode_count,
952 VkPresentModeKHR **out_present_modes);
953#endif
954
955#if defined(VK_KHR_swapchain) || defined(XRT_DOXYGEN)
956/*!
957 * Enumerate the images of the given @p VkSwapchainKHR.
958 *
959 * @ingroup aux_vk
960 */
961VkResult
963 VkSwapchainKHR swapchain,
964 uint32_t *out_image_count,
965 VkImage **out_images);
966#endif
967
968#if defined(VK_USE_PLATFORM_DISPLAY_KHR) || defined(XRT_DOXYGEN)
969/*!
970 * Enumerate the display properties of the given @p VkPhysicalDevice.
971 *
972 * @ingroup aux_vk
973 */
974VkResult
976 VkPhysicalDevice physical_device,
977 uint32_t *out_prop_count,
978 VkDisplayPropertiesKHR **out_props);
979
980/*!
981 * Enumerate the display plane properties of the given @p VkPhysicalDevice.
982 *
983 * @ingroup aux_vk
984 */
985VkResult
987 VkPhysicalDevice physical_device,
988 uint32_t *out_prop_count,
989 VkDisplayPlanePropertiesKHR **out_props);
990
991/*!
992 * Enumerate the mode properties of the given @p VkDisplayKHR, which belongs
993 * to the given @p VkPhysicalDevice.
994 *
995 * @ingroup aux_vk
996 */
997VkResult
999 VkPhysicalDevice physical_device,
1000 VkDisplayKHR display,
1001 uint32_t *out_prop_count,
1002 VkDisplayModePropertiesKHR **out_props);
1003#endif
1004
1005
1006/*
1007 *
1008 * Struct init functions, in the vk_function_loaders.c file.
1009 *
1010 */
1011
1012/*!
1013 * Can be done on a completely bare bundle.
1014 *
1015 * @ingroup aux_vk
1016 */
1017VkResult
1018vk_get_loader_functions(struct vk_bundle *vk, PFN_vkGetInstanceProcAddr g);
1019
1020/*!
1021 * Requires a instance to have been created and set on the bundle.
1022 *
1023 * @ingroup aux_vk
1024 */
1025VkResult
1027
1028/*!
1029 * Requires a device to have been created and set on the bundle.
1030 *
1031 * @ingroup aux_vk
1032 */
1033VkResult
1035
1036
1037/*
1038 *
1039 * Bundle init functions, in the vk_bundle_init.c file.
1040 *
1041 */
1042
1043/*!
1044 * Check if the required instance extensions are supported, if not print error
1045 * message with all extensions missing, returns VK_ERROR_EXTENSION_NOT_PRESENT
1046 * if not all extensions are supported.
1047 *
1048 * @ingroup aux_vk
1049 */
1050VkResult
1051vk_check_required_instance_extensions(struct vk_bundle *vk, struct u_string_list *required_instance_ext_list);
1052
1053/*!
1054 * Only requires @ref vk_get_loader_functions to have been called.
1055 *
1056 * @ingroup aux_vk
1057 */
1058struct u_string_list *
1060 struct u_string_list *required_instance_ext_list,
1061 struct u_string_list *optional_instance_ext_list);
1062
1063/*!
1064 * Fills in has_* in vk_bundle given a string of prefiltered instance extensions
1065 */
1066void
1067vk_fill_in_has_instance_extensions(struct vk_bundle *vk, struct u_string_list *ext_list);
1068
1069/*!
1070 * Setup the physical device, this is called by vk_create_device but has uses
1071 * for outside of that.
1072 *
1073 * @ingroup aux_vk
1074 */
1075VkResult
1076vk_select_physical_device(struct vk_bundle *vk, int forced_index);
1077
1078/*!
1079 * Used to enable device features as a argument @ref vk_create_device.
1080 *
1081 * @ingroup aux_vk
1082 */
1084{
1085 bool shader_image_gather_extended;
1086 bool shader_storage_image_write_without_format;
1087 bool null_descriptor;
1088 bool timeline_semaphore;
1089 bool synchronization_2;
1090 bool ext_fmt_resolve;
1091 bool storage_buffer_8bit_access;
1092 bool present_wait;
1093 bool video_maintenance_1;
1094};
1095
1096/*!
1097 * Creates a VkDevice and initialises the VkQueue.
1098 *
1099 * The @p vk_bundle must have been zero initialized, have the instance functions
1100 * loaded and a valid instance.
1101 *
1102 * @ingroup aux_vk
1103 */
1104XRT_CHECK_RESULT VkResult
1105vk_create_device(struct vk_bundle *vk,
1106 int forced_index,
1107 bool only_compute,
1108 VkQueueGlobalPriorityEXT global_priority,
1109 struct u_string_list *required_device_ext_list,
1110 struct u_string_list *optional_device_ext_list,
1111 const struct vk_device_features *optional_device_features);
1112
1113/*!
1114 * @brief Initialize mutexes in the @ref vk_bundle.
1115 *
1116 * Not required for all uses, but a precondition for some.
1117 *
1118 * @ingroup aux_vk
1119 */
1120VkResult
1121vk_init_mutex(struct vk_bundle *vk);
1122
1123/*!
1124 * @brief De-initialize mutexes in the @ref vk_bundle.
1125 *
1126 * @ingroup aux_vk
1127 */
1128VkResult
1129vk_deinit_mutex(struct vk_bundle *vk);
1130
1131static inline void
1132vk_queue_lock(struct vk_bundle_queue *q)
1133{
1134 assert(q != NULL);
1135 os_mutex_lock(&q->mutex);
1136}
1137
1138static inline int
1139vk_queue_trylock(struct vk_bundle_queue *q)
1140{
1141 assert(q != NULL);
1142 return os_mutex_trylock(&q->mutex);
1143}
1144
1145static inline void
1146vk_queue_unlock(struct vk_bundle_queue *q)
1147{
1148 assert(q != NULL);
1150}
1151
1152/*!
1153 * Initialize a bundle with objects given to us by client code,
1154 * used by @ref client_vk_compositor in @ref comp_client.
1155 *
1156 * @ingroup aux_vk
1157 */
1158XRT_CHECK_RESULT VkResult
1159vk_init_from_given(struct vk_bundle *vk,
1160 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr,
1161 VkInstance instance,
1162 VkPhysicalDevice physical_device,
1163 VkDevice device,
1164 uint32_t queue_family_index,
1165 uint32_t queue_index,
1166 bool external_fence_fd_enabled,
1167 bool external_semaphore_fd_enabled,
1168 bool timeline_semaphore_enabled,
1169 bool image_format_list_enabled,
1170 bool debug_utils_enabled,
1171 enum u_logging_level log_level);
1172
1173
1174/*
1175 *
1176 * Other functions.
1177 *
1178 */
1179
1180/*!
1181 * @ingroup aux_vk
1182 */
1183bool
1184vk_get_memory_type(struct vk_bundle *vk, uint32_t type_bits, VkMemoryPropertyFlags memory_props, uint32_t *out_type_id);
1185
1186/*!
1187 * Allocate memory for an image and bind it to that image.
1188 *
1189 * Handles the following steps:
1190 *
1191 * - calling vkGetImageMemoryRequirements
1192 * - comparing against the max_size
1193 * - getting the memory type (as dictated by the VkMemoryRequirements and
1194 * VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)
1195 * - calling vkAllocateMemory
1196 * - calling vkBindImageMemory
1197 * - calling vkDestroyMemory in case of an error.
1198 *
1199 * If this fails, it cleans up the VkDeviceMemory.
1200 *
1201 * @param vk Vulkan bundle
1202 * @param image The VkImage to allocate for and bind.
1203 * @param requirements Memory requirements used for finding the memory type and the size.
1204 * @param pNext_for_allocate (Optional) a pointer to use in the pNext chain of
1205 * VkMemoryAllocateInfo.
1206 * @param caller_name Used for error printing, this function is called from
1207 * various sources and takes next chains that could influence the result
1208 * of various calls inside of it. Since it's up to this function to print
1209 * any errors it will add the caller name to error messages.
1210 * @param out_mem Output parameter: will be set to the allocated memory if
1211 * everything succeeds. Not modified if there is an error.
1212 * @param out_size (Optional) pointer to receive the value of
1213 * VkMemoryRequirements::size.
1214 *
1215 * If this fails, you may want to destroy your VkImage as well, since this
1216 * routine is usually used in combination with vkCreateImage.
1217 *
1218 * @ingroup aux_vk
1219 */
1220XRT_CHECK_RESULT VkResult
1222 VkImage image,
1223 const VkMemoryRequirements *requirements,
1224 const void *pNext_for_allocate,
1225 const char *caller_name,
1226 VkDeviceMemory *out_mem);
1227
1228/*!
1229 *
1230 * @brief Creates a Vulkan device memory and image from a native graphics buffer handle.
1231 *
1232 * In case of error, ownership is never transferred and the caller should close the handle themselves.
1233 *
1234 * In case of success, the underlying Vulkan functionality's ownership semantics apply: ownership of the @p image_native
1235 * handle may have transferred, a reference may have been added, or the Vulkan objects may rely on the caller to keep
1236 * the native handle alive until the Vulkan objects are destroyed. Which option applies depends on the particular native
1237 * handle type used.
1238 *
1239 * See the corresponding specification texts:
1240 *
1241 * - Windows:
1242 * https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#VkImportMemoryWin32HandleInfoKHR
1243 * - Linux: https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#VkImportMemoryFdInfoKHR
1244 * - Android:
1245 * https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#VkImportAndroidHardwareBufferInfoANDROID
1246 *
1247 * @ingroup aux_vk
1248 */
1249XRT_CHECK_RESULT VkResult
1251 const struct xrt_swapchain_create_info *info,
1252 struct xrt_image_native *image_native,
1253 VkImage *out_image,
1254 VkDeviceMemory *out_mem);
1255
1256/*!
1257 * Given a DeviceMemory handle created to be exportable, outputs the native buffer type (FD on desktop Linux)
1258 * equivalent.
1259 *
1260 * Caller assumes ownership of handle which should be unreferenced with @ref u_graphics_buffer_unref when no longer
1261 * needed.
1262 *
1263 * @param vk Vulkan bundle
1264 * @param device_memory The memory to get the handle of
1265 * @param[out] out_handle A pointer to the handle to populate
1266 *
1267 * @ingroup aux_vk
1268 */
1269XRT_CHECK_RESULT VkResult
1271 VkDeviceMemory device_memory,
1272 xrt_graphics_buffer_handle_t *out_handle);
1273
1274/*!
1275 * @ingroup aux_vk
1276 * Helper to create a VkImage
1277 */
1278VkResult
1280 VkExtent2D extent,
1281 VkFormat format,
1282 VkImageUsageFlags usage,
1283 VkDeviceMemory *out_mem,
1284 VkImage *out_image);
1285
1286/*!
1287 * Helper to create a mutable RG88B8A8 VkImage that specializes in the two
1288 * UNORM and SRGB variants of that formats.
1289 *
1290 * @ingroup aux_vk
1291 */
1292VkResult
1294 struct vk_bundle *vk, VkExtent2D extent, VkImageUsageFlags usage, VkDeviceMemory *out_mem, VkImage *out_image);
1295
1296/*!
1297 * @ingroup aux_vk
1298 * Helper to create a VkImage, with more options for tiling and memory storage.
1299 */
1300VkResult
1302 VkExtent3D extent,
1303 VkFormat format,
1304 VkImageTiling image_tiling,
1305 VkImageUsageFlags image_usage_flags,
1306 VkMemoryPropertyFlags memory_property_flags,
1307 VkDeviceMemory *out_mem,
1308 VkImage *out_image);
1309
1310/*!
1311 * @ingroup aux_vk
1312 */
1313VkResult
1314vk_create_sampler(struct vk_bundle *vk, VkSamplerAddressMode clamp_mode, VkSampler *out_sampler);
1315
1316
1317/*
1318 *
1319 * Helpers for creating ímage views.
1320 *
1321 */
1322
1323/*!
1324 * @ingroup aux_vk
1325 */
1326VkResult
1327vk_create_view(struct vk_bundle *vk,
1328 VkImage image,
1329 VkImageViewType type,
1330 VkFormat format,
1331 VkImageSubresourceRange subresource_range,
1332 VkImageView *out_view);
1333
1334/*!
1335 * @ingroup aux_vk
1336 */
1337VkResult
1338vk_create_view_swizzle(struct vk_bundle *vk,
1339 VkImage image,
1340 VkImageViewType type,
1341 VkFormat format,
1342 VkImageSubresourceRange subresource_range,
1343 VkComponentMapping components,
1344 VkImageView *out_view);
1345
1346/*!
1347 * Creates a image with a specific subset of usage, useful for a mutable images
1348 * where one format might not support all usages defined by the image.
1349 *
1350 * @ingroup aux_vk
1351 */
1352VkResult
1354 VkImage image,
1355 VkImageViewType type,
1356 VkFormat format,
1357 VkImageUsageFlags image_usage,
1358 VkImageSubresourceRange subresource_range,
1359 VkImageView *out_view);
1360
1361
1362/*
1363 *
1364 * Helpers for creating descriptor pools and sets.
1365 *
1366 */
1367
1368bool
1369vk_init_descriptor_pool(struct vk_bundle *vk,
1370 const VkDescriptorPoolSize *pool_sizes,
1371 uint32_t pool_size_count,
1372 uint32_t set_count,
1373 VkDescriptorPool *out_descriptor_pool);
1374
1375bool
1376vk_allocate_descriptor_sets(struct vk_bundle *vk,
1377 VkDescriptorPool descriptor_pool,
1378 uint32_t count,
1379 const VkDescriptorSetLayout *set_layout,
1380 VkDescriptorSet *sets);
1381
1382
1383/*
1384 *
1385 * Helpers for creating buffers.
1386 *
1387 */
1388
1389bool
1390vk_buffer_init(struct vk_bundle *vk,
1391 VkDeviceSize size,
1392 VkBufferUsageFlags usage,
1393 VkMemoryPropertyFlags properties,
1394 VkBuffer *out_buffer,
1395 VkDeviceMemory *out_mem);
1396
1397void
1398vk_buffer_destroy(struct vk_buffer *self, struct vk_bundle *vk);
1399
1400bool
1401vk_update_buffer(struct vk_bundle *vk, float *buffer, size_t buffer_size, VkDeviceMemory memory);
1402
1403
1404/*
1405 *
1406 * Helpers for writing command buffers, in the vk_helpers.c file.
1407 *
1408 */
1409
1410/*!
1411 * Inserts a image barrier command, doesn't take any locks, the calling code
1412 * will need hold the lock for the pool that cmd_buffer is from or ensure it is
1413 * externally synchronized.
1414 *
1415 * @ingroup aux_vk
1416 */
1417void
1419 VkCommandBuffer cmd_buffer,
1420 VkImage image,
1421 VkAccessFlags src_access_mask,
1422 VkAccessFlags dst_access_mask,
1423 VkImageLayout old_image_layout,
1424 VkImageLayout new_image_layout,
1425 VkPipelineStageFlags src_stage_mask,
1426 VkPipelineStageFlags dst_stage_mask,
1427 VkImageSubresourceRange subresource_range);
1428
1429/*!
1430 * Inserts a image barrier command specifically for GPU commands, doesn't take
1431 * any locks, the calling code will need hold the lock for the pool that
1432 * cmd_buffer is from or ensure it is externally synchronized.
1433 *
1434 * @ingroup aux_vk
1435 */
1436void
1438 VkCommandBuffer cmd_buffer,
1439 VkImage image,
1440 VkAccessFlags src_access_mask,
1441 VkAccessFlags dst_access_mask,
1442 VkImageLayout old_layout,
1443 VkImageLayout new_layout,
1444 VkImageSubresourceRange subresource_range);
1445
1446#if defined(VK_EXT_debug_utils) || defined(XRT_DOXYGEN)
1447/*!
1448 * Uses VK_EXT_debug_utils to insert debug label into a VkCommandBuffer.
1449 *
1450 * In the vk_debug.c file.
1451 *
1452 * @ingroup aux_vk
1453 */
1454void
1455vk_cmd_insert_label(struct vk_bundle *vk, VkCommandBuffer cmd_buffer, const char *name);
1456#endif
1457
1458
1459/*
1460 *
1461 * State creation helpers, in the vk_state_creators.c file.
1462 *
1463 */
1464
1465/*!
1466 * Arguments to @ref vk_create_descriptor_pool function.
1467 */
1469{
1470 uint32_t uniform_per_descriptor_count; //!< VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
1471 uint32_t sampler_per_descriptor_count; //!< VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
1472 uint32_t storage_image_per_descriptor_count; //!< VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
1473 uint32_t storage_buffer_per_descriptor_count; //!< VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
1474
1475 //! The max count of created descriptors.
1477
1478 //! Are descriptors freeable, or must vkResetDescriptorPool be used.
1480};
1481
1482/*!
1483 * Creates a descriptor pool, made for a single layout.
1484 *
1485 * Does error logging.
1486 */
1487VkResult
1489 const struct vk_descriptor_pool_info *info,
1490 VkDescriptorPool *out_descriptor_pool);
1491
1492/*!
1493 * Creates a descriptor set.
1494 *
1495 * Does error logging.
1496 */
1497VkResult
1499 VkDescriptorPool descriptor_pool,
1500 VkDescriptorSetLayout descriptor_layout,
1501 VkDescriptorSet *out_descriptor_set);
1502
1503/*!
1504 * Creates a pipeline layout from a single descriptor set layout.
1505 *
1506 * Does error logging.
1507 */
1508VkResult
1510 VkDescriptorSetLayout descriptor_set_layout,
1511 VkPipelineLayout *out_pipeline_layout);
1512
1513/*!
1514 * Creates a pipeline cache.
1515 *
1516 * Does error logging.
1517 */
1518VkResult
1519vk_create_pipeline_cache(struct vk_bundle *vk, VkPipelineCache *out_pipeline_cache);
1520
1521/*!
1522 * Creates a compute pipeline, assumes entry function is called 'main'.
1523 *
1524 * Does error logging.
1525 */
1526VkResult
1528 VkPipelineCache pipeline_cache,
1529 VkShaderModule shader,
1530 VkPipelineLayout pipeline_layout,
1531 const VkSpecializationInfo *specialization_info,
1532 VkPipeline *out_compute_pipeline);
1533
1534
1535/*
1536 *
1537 * Compositor buffer and swapchain image flags helpers, in the vk_compositor_flags.c file.
1538 *
1539 */
1540
1541/*!
1542 * Return the extern handle type that a buffer should be created with.
1543 *
1544 * cb = Compositor Buffer.
1545 */
1546VkExternalMemoryHandleTypeFlags
1548
1549/*!
1550 * Helper for all of the supported formats to check support for.
1551 *
1552 * These are the available formats we will expose to our clients.
1553 *
1554 * In order of what we prefer. Start with a SRGB format that works on
1555 * both OpenGL and Vulkan. The two linear formats that works on both
1556 * OpenGL and Vulkan. A SRGB format that only works on Vulkan. The last
1557 * two formats should not be used as they are linear but doesn't have
1558 * enough bits to express it without resulting in banding.
1559 *
1560 * The format VK_FORMAT_A2B10G10R10_UNORM_PACK32 is not listed since
1561 * 10 bits are not considered enough to do linear colors without
1562 * banding. If there was a sRGB variant of it then we would have used it
1563 * instead but there isn't. Since it's not a popular format it's best
1564 * not to list it rather then listing it and people falling into the
1565 * trap. The absolute minimum is R11G11B10, but is a really weird format
1566 * so we are not exposing it.
1567 *
1568 * CSCI = Compositor SwapChain Images.
1569 *
1570 * @ingroup aux_vk
1571 */
1572#define VK_CSCI_FORMATS(THING_COLOR, THING_DS, THING_D, THING_S) \
1573 /* color formats */ \
1574 THING_COLOR(R16G16B16A16_UNORM) /* OGL VK */ \
1575 THING_COLOR(R16G16B16A16_SFLOAT) /* OGL VK */ \
1576 THING_COLOR(R16G16B16_UNORM) /* OGL VK - Uncommon. */ \
1577 THING_COLOR(R16G16B16_SFLOAT) /* OGL VK - Uncommon. */ \
1578 THING_COLOR(R8G8B8A8_SRGB) /* OGL VK */ \
1579 THING_COLOR(B8G8R8A8_SRGB) /* VK */ \
1580 THING_COLOR(R8G8B8_SRGB) /* OGL VK - Uncommon. */ \
1581 THING_COLOR(R8G8B8A8_UNORM) /* OGL VK - Bad color precision. */ \
1582 THING_COLOR(B8G8R8A8_UNORM) /* VK - Bad color precision. */ \
1583 THING_COLOR(R8G8B8_UNORM) /* OGL VK - Uncommon. Bad color precision. */ \
1584 THING_COLOR(B8G8R8_UNORM) /* VK - Uncommon. Bad color precision. */ \
1585 THING_COLOR(R5G6B5_UNORM_PACK16) /* OLG VK - Bad color precision. */ \
1586 THING_COLOR(R32_SFLOAT) /* OGL VK */ \
1587 /* depth formats */ \
1588 THING_D(D32_SFLOAT) /* OGL VK */ \
1589 THING_D(D16_UNORM) /* OGL VK */ \
1590 THING_D(X8_D24_UNORM_PACK32) /* OGL VK */ \
1591 /* depth stencil formats */ \
1592 THING_DS(D24_UNORM_S8_UINT) /* OGL VK */ \
1593 THING_DS(D32_SFLOAT_S8_UINT) /* OGL VK */ \
1594 /* stencil format */ \
1595 THING_S(S8_UINT)
1596
1597/*!
1598 * Returns the access flags for the compositor to app barriers.
1599 *
1600 * CSCI = Compositor SwapChain Images.
1601 */
1602VkAccessFlags
1604
1605/*!
1606 * Return the optimal layout for this format, this is the layout as given to the
1607 * app so is bound to the OpenXR spec.
1608 *
1609 * CSCI = Compositor SwapChain Images.
1610 */
1611VkImageLayout
1612vk_csci_get_barrier_optimal_layout(VkFormat format);
1613
1614/*!
1615 * Return the barrier aspect mask for this format, this is intended for the
1616 * barriers that flush the data out before and after transfers between the
1617 * application and compositor.
1618 *
1619 * CSCI = Compositor SwapChain Images.
1620 */
1621VkImageAspectFlags
1622vk_csci_get_barrier_aspect_mask(VkFormat format);
1623
1624/*!
1625 * Returns the usage bits for a given selected format and usage.
1626 *
1627 * For color formats always adds:
1628 * * `VK_IMAGE_USAGE_SAMPLED_BIT` for compositor reading in shaders.
1629 *
1630 * For depth & stencil formats always adds:
1631 * * `VK_IMAGE_USAGE_SAMPLED_BIT` for compositor reading in shaders.
1632 *
1633 * For depth formats always adds:
1634 * * `VK_IMAGE_USAGE_SAMPLED_BIT` for compositor reading in shaders.
1635 *
1636 * For stencil formats always adds:
1637 * * `VK_IMAGE_USAGE_SAMPLED_BIT` for compositor reading in shaders.
1638 *
1639 * CSCI = Compositor SwapChain Images.
1640 */
1641VkImageUsageFlags
1642vk_csci_get_image_usage_flags(struct vk_bundle *vk, VkFormat format, enum xrt_swapchain_usage_bits bits);
1643
1644/*!
1645 * For images views created by the compositor to sample the images, what aspect
1646 * should be set. For color it's the color, for depth and stencil it's only
1647 * depth as both are disallowed by the Vulkan spec, for depth only depth, and
1648 * for stencil only it's stencil.
1649 *
1650 * CSCI = Compositor SwapChain Images.
1651 */
1652VkImageAspectFlags
1654
1655/*!
1656 * Return the extern handle type that a image should be created with.
1657 *
1658 * CSCI = Compositor SwapChain Images.
1659 */
1660VkExternalMemoryHandleTypeFlags
1662
1663/*!
1664 * Get whether a given image can be imported/exported for a handle type.
1665 *
1666 * CSCI = Compositor SwapChain Images.
1667 */
1668void
1670 VkFormat image_format,
1671 enum xrt_swapchain_usage_bits bits,
1672 VkExternalMemoryHandleTypeFlags handle_type,
1673 bool *out_importable,
1674 bool *out_exportable);
1675
1676/*!
1677 * Verify if a format is supported for a specific usage
1678 *
1679 * CSCI = Compositor SwapChain Images.
1680 */
1681bool
1682vk_csci_is_format_supported(struct vk_bundle *vk, VkFormat format, enum xrt_swapchain_usage_bits xbits);
1683
1684/*
1685 *
1686 * Sync objects, in the vk_sync_objects.c file.
1687 *
1688 */
1689
1690/*!
1691 * Is there a good likelihood that the import/export of a timeline semaphore
1692 * will succeed, in other words will the below functions work.
1693 *
1694 * @ingroup aux_vk
1695 */
1696XRT_CHECK_RESULT bool
1698
1699/*!
1700 * @brief Creates a Vulkan fence, submits it to the default VkQueue and return
1701 * its native graphics sync handle.
1702 *
1703 * In case of error, out_native is not touched by the function.
1704 *
1705 * See @ref vk_create_fence_sync_from_native for ownership semantics on import.
1706 *
1707 * @ingroup aux_vk
1708 */
1709XRT_CHECK_RESULT VkResult
1711
1712/*!
1713 * @brief Creates a Vulkan fence from a native graphics sync handle.
1714 *
1715 * In case of error, ownership is never transferred and the caller should close the handle themselves.
1716 *
1717 * In case of success, the underlying Vulkan functionality's ownership semantics apply: ownership of the @p native
1718 * handle may have transferred, a reference may have been added, or the Vulkan object may rely on the caller to keep the
1719 * native handle alive until the Vulkan object is destroyed. Which option applies depends on the particular native
1720 * handle type used.
1721 *
1722 * See the corresponding Vulkan specification text:
1723 * https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#synchronization-fences-importing
1724 *
1725 * @ingroup aux_vk
1726 */
1727XRT_CHECK_RESULT VkResult
1728vk_create_fence_sync_from_native(struct vk_bundle *vk, xrt_graphics_sync_handle_t native, VkFence *out_fence);
1729
1730/*!
1731 * Creates a Vulkan semaphore and a native graphics sync handle.
1732 *
1733 * In case of success, the underlying Vulkan functionality's ownership semantics
1734 * apply: ownership of the @p native handle may have transferred, a reference
1735 * may have been added, or the Vulkan object may rely on the caller to keep the
1736 * native handle alive until the Vulkan object is destroyed. Which option
1737 * applies depends on the particular native handle type used.
1738 *
1739 * In case of error, neither @p out_sem and @p out_native is not touched by the
1740 * function so the caller only becomes responsible for the output on success.
1741 *
1742 * See the corresponding Vulkan specification text:
1743 * https://www.khronos.org/registry/vulkan/specs/1.3-extensions/html/vkspec.html#synchronization-semaphores
1744 *
1745 * @ingroup aux_vk
1746 */
1747XRT_CHECK_RESULT VkResult
1748vk_create_semaphore_and_native(struct vk_bundle *vk, VkSemaphore *out_sem, xrt_graphics_sync_handle_t *out_native);
1749
1750#if defined(VK_KHR_timeline_semaphore) || defined(XRT_DOXYGEN)
1751/*
1752 * Creates a Vulkan timeline semaphore and a native graphics sync
1753 * handle, see @ref vk_create_semaphore_and_native for more details.
1754 *
1755 * @ingroup aux_vk
1756 */
1757XRT_CHECK_RESULT VkResult
1758vk_create_timeline_semaphore_and_native(struct vk_bundle *vk,
1759 VkSemaphore *out_sem,
1760 xrt_graphics_sync_handle_t *out_native);
1761#endif
1762
1763/*!
1764 * @brief Creates a Vulkan semaphore from a native graphics sync handle.
1765 *
1766 * In case of error, ownership is never transferred and the caller should close the handle themselves.
1767 *
1768 * In case of success, the underlying Vulkan functionality's ownership semantics apply: ownership of the @p native
1769 * handle may have transferred, a reference may have been added, or the Vulkan object may rely on the caller to keep the
1770 * native handle alive until the Vulkan object is destroyed. Which option applies depends on the particular native
1771 * handle type used.
1772 *
1773 * @ingroup aux_vk
1774 */
1775XRT_CHECK_RESULT VkResult
1776vk_create_semaphore_from_native(struct vk_bundle *vk, xrt_graphics_sync_handle_t native, VkSemaphore *out_sem);
1777
1778#if defined(VK_KHR_timeline_semaphore) || defined(XRT_DOXYGEN)
1779/*!
1780 * @brief Creates a Vulkan timeline semaphore from a native graphics sync
1781 * handle, see @ref vk_create_semaphore_from_native for more details.
1782 *
1783 * @ingroup aux_vk
1784 */
1785XRT_CHECK_RESULT VkResult
1787#endif
1788
1789
1790/*
1791 *
1792 * Time function(s), in the vk_time.c file.
1793 *
1794 */
1795
1796#if defined(VK_EXT_calibrated_timestamps) || defined(XRT_DOXYGEN)
1797/*!
1798 * Convert timestamps in GPU ticks (as return by VkQueryPool timestamp queries)
1799 * into host CPU nanoseconds, same time domain as @ref os_monotonic_get_ns.
1800 *
1801 * Note the timestamp needs to be in the past and not to old, this is because
1802 * not all GPU has full 64 bit timer resolution. For instance a Intel GPU "only"
1803 * have 36 bits of valid timestamp and a tick period 83.3333 nanosecond,
1804 * equating to an epoch of 5726 seconds before overflowing. The function can
1805 * handle overflows happening between the given timestamps and when it is called
1806 * but only for one such epoch overflow, any more will only be treated as one
1807 * such overflow. So timestamps needs to be converted reasonably soon after they
1808 * have been captured.
1809 *
1810 * @param vk The Vulkan bundle.
1811 * @param count Number of timestamps to be converted.
1812 * @param[in,out] in_out_timestamps Array of timestamps to be converted, done in place.
1813 *
1814 * @ingroup aux_vk
1815 */
1816XRT_CHECK_RESULT VkResult
1817vk_convert_timestamps_to_host_ns(struct vk_bundle *vk, uint32_t count, uint64_t *in_out_timestamps);
1818#endif
1819
1820
1821#ifdef __cplusplus
1822}
1823#endif
u_logging_level
Logging level enum.
Definition: u_logging.h:45
static void os_mutex_lock(struct os_mutex *om)
Lock.
Definition: os_threading.h:86
static int os_mutex_trylock(struct os_mutex *om)
Try to lock, but do not block.
Definition: os_threading.h:98
static void os_mutex_unlock(struct os_mutex *om)
Unlock.
Definition: os_threading.h:110
VkResult vk_enumerate_display_mode_properties(struct vk_bundle *vk, VkPhysicalDevice physical_device, VkDisplayKHR display, uint32_t *out_prop_count, VkDisplayModePropertiesKHR **out_props)
Enumerate the mode properties of the given VkDisplayKHR, which belongs to the given VkPhysicalDevice.
VkResult vk_enumerate_physical_device_extension_properties(struct vk_bundle *vk, VkPhysicalDevice physical_device, const char *layer_name, uint32_t *out_prop_count, VkExtensionProperties **out_props)
Enumerate the extension properties of the given VkPhysicalDevice for the named layer_name,...
Definition: vk_enumerate.c:102
VkResult vk_create_image_advanced(struct vk_bundle *vk, VkExtent3D extent, VkFormat format, VkImageTiling image_tiling, VkImageUsageFlags image_usage_flags, VkMemoryPropertyFlags memory_property_flags, VkDeviceMemory *out_mem, VkImage *out_image)
Helper to create a VkImage, with more options for tiling and memory storage.
Definition: vk_helpers.c:974
VkResult vk_enumerate_physical_display_plane_properties(struct vk_bundle *vk, VkPhysicalDevice physical_device, uint32_t *out_prop_count, VkDisplayPlanePropertiesKHR **out_props)
Enumerate the display plane properties of the given VkPhysicalDevice.
XRT_CHECK_RESULT VkResult vk_create_device(struct vk_bundle *vk, int forced_index, bool only_compute, VkQueueGlobalPriorityEXT global_priority, struct u_string_list *required_device_ext_list, struct u_string_list *optional_device_ext_list, const struct vk_device_features *optional_device_features)
Creates a VkDevice and initialises the VkQueue.
Definition: vk_bundle_init.c:1322
void vk_cmd_image_barrier_gpu_locked(struct vk_bundle *vk, VkCommandBuffer cmd_buffer, VkImage image, VkAccessFlags src_access_mask, VkAccessFlags dst_access_mask, VkImageLayout old_layout, VkImageLayout new_layout, VkImageSubresourceRange subresource_range)
Inserts a image barrier command specifically for GPU commands, doesn't take any locks,...
Definition: vk_helpers.c:1725
XRT_CHECK_RESULT VkResult vk_init_from_given(struct vk_bundle *vk, PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr, VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, uint32_t queue_family_index, uint32_t queue_index, bool external_fence_fd_enabled, bool external_semaphore_fd_enabled, bool timeline_semaphore_enabled, bool image_format_list_enabled, bool debug_utils_enabled, enum u_logging_level log_level)
Initialize a bundle with objects given to us by client code, used by client_vk_compositor in Composit...
Definition: vk_bundle_init.c:1662
VkResult vk_create_image_mutable_rgba(struct vk_bundle *vk, VkExtent2D extent, VkImageUsageFlags usage, VkDeviceMemory *out_mem, VkImage *out_image)
Helper to create a mutable RG88B8A8 VkImage that specializes in the two UNORM and SRGB variants of th...
Definition: vk_helpers.c:937
XRT_CHECK_RESULT VkResult vk_convert_timestamps_to_host_ns(struct vk_bundle *vk, uint32_t count, uint64_t *in_out_timestamps)
Convert timestamps in GPU ticks (as return by VkQueryPool timestamp queries) into host CPU nanosecond...
XRT_CHECK_RESULT VkResult vk_create_semaphore_from_native(struct vk_bundle *vk, xrt_graphics_sync_handle_t native, VkSemaphore *out_sem)
Creates a Vulkan semaphore from a native graphics sync handle.
Definition: vk_sync_objects.c:461
void vk_cmd_image_barrier_locked(struct vk_bundle *vk, VkCommandBuffer cmd_buffer, VkImage image, VkAccessFlags src_access_mask, VkAccessFlags dst_access_mask, VkImageLayout old_image_layout, VkImageLayout new_image_layout, VkPipelineStageFlags src_stage_mask, VkPipelineStageFlags dst_stage_mask, VkImageSubresourceRange subresource_range)
Inserts a image barrier command, doesn't take any locks, the calling code will need hold the lock for...
Definition: vk_helpers.c:1688
VkResult vk_enumerate_physical_device_display_properties(struct vk_bundle *vk, VkPhysicalDevice physical_device, uint32_t *out_prop_count, VkDisplayPropertiesKHR **out_props)
Enumerate the display properties of the given VkPhysicalDevice.
XRT_CHECK_RESULT VkResult vk_create_timeline_semaphore_from_native(struct vk_bundle *vk, xrt_graphics_sync_handle_t native, VkSemaphore *out_sem)
Creates a Vulkan timeline semaphore from a native graphics sync handle, see vk_create_semaphore_from_...
VkResult vk_create_image_simple(struct vk_bundle *vk, VkExtent2D extent, VkFormat format, VkImageUsageFlags usage, VkDeviceMemory *out_mem, VkImage *out_image)
Helper to create a VkImage.
Definition: vk_helpers.c:916
VkResult vk_select_physical_device(struct vk_bundle *vk, int forced_index)
Setup the physical device, this is called by vk_create_device but has uses for outside of that.
Definition: vk_bundle_init.c:1316
void vk_print_device_info(struct vk_bundle *vk, enum u_logging_level log_level, const VkPhysicalDeviceProperties *pdp, uint32_t gpu_index, const char *title)
Print device information to the logger at the given logging level, if the vk_bundle has that level en...
Definition: vk_print.c:100
VkResult vk_enumerate_swapchain_images(struct vk_bundle *vk, VkSwapchainKHR swapchain, uint32_t *out_image_count, VkImage **out_images)
Enumerate the images of the given VkSwapchainKHR.
VkResult vk_deinit_mutex(struct vk_bundle *vk)
De-initialize mutexes in the vk_bundle.
Definition: vk_bundle_init.c:1643
XRT_CHECK_RESULT VkResult vk_create_semaphore_and_native(struct vk_bundle *vk, VkSemaphore *out_sem, xrt_graphics_sync_handle_t *out_native)
Creates a Vulkan semaphore and a native graphics sync handle.
Definition: vk_sync_objects.c:282
XRT_CHECK_RESULT VkResult vk_create_and_submit_fence_native(struct vk_bundle *vk, xrt_graphics_sync_handle_t *out_native)
Creates a Vulkan fence, submits it to the default VkQueue and return its native graphics sync handle.
Definition: vk_sync_objects.c:105
VkResult vk_enumerate_surface_present_modes(struct vk_bundle *vk, VkSurfaceKHR surface, uint32_t *out_present_mode_count, VkPresentModeKHR **out_present_modes)
Enumerate the present modes of the given VkSurfaceKHR.
XRT_CHECK_RESULT VkResult vk_create_image_from_native(struct vk_bundle *vk, const struct xrt_swapchain_create_info *info, struct xrt_image_native *image_native, VkImage *out_image, VkDeviceMemory *out_mem)
Creates a Vulkan device memory and image from a native graphics buffer handle.
Definition: vk_helpers.c:1077
VkResult vk_get_loader_functions(struct vk_bundle *vk, PFN_vkGetInstanceProcAddr g)
Can be done on a completely bare bundle.
Definition: vk_function_loaders.c:43
XRT_CHECK_RESULT bool vk_can_import_and_export_timeline_semaphore(struct vk_bundle *vk)
Is there a good likelihood that the import/export of a timeline semaphore will succeed,...
Definition: vk_sync_objects.c:77
VkResult vk_enumerate_physical_devices(struct vk_bundle *vk, uint32_t *out_physical_device_count, VkPhysicalDevice **out_physical_devices)
Enumerate the physical devices of the VkInstance that has been opened on the given vk_bundle.
Definition: vk_enumerate.c:79
XRT_CHECK_RESULT VkResult vk_create_fence_sync_from_native(struct vk_bundle *vk, xrt_graphics_sync_handle_t native, VkFence *out_fence)
Creates a Vulkan fence from a native graphics sync handle.
Definition: vk_sync_objects.c:338
VkResult vk_enumerate_instance_extensions_properties(struct vk_bundle *vk, const char *layer_name, uint32_t *out_prop_count, VkExtensionProperties **out_props)
Return the VkExtensionProperties of the given layer_name, NULL means the "base" driver instance.
Definition: vk_enumerate.c:55
VkResult vk_create_view_usage(struct vk_bundle *vk, VkImage image, VkImageViewType type, VkFormat format, VkImageUsageFlags image_usage, VkImageSubresourceRange subresource_range, VkImageView *out_view)
Creates a image with a specific subset of usage, useful for a mutable images where one format might n...
Definition: vk_helpers.c:1510
struct u_string_list * vk_build_instance_extensions(struct vk_bundle *vk, struct u_string_list *required_instance_ext_list, struct u_string_list *optional_instance_ext_list)
Only requires vk_get_loader_functions to have been called.
Definition: vk_bundle_init.c:128
XRT_CHECK_RESULT VkResult vk_alloc_and_bind_image_memory(struct vk_bundle *vk, VkImage image, const VkMemoryRequirements *requirements, const void *pNext_for_allocate, const char *caller_name, VkDeviceMemory *out_mem)
Allocate memory for an image and bind it to that image.
Definition: vk_helpers.c:808
VkResult vk_enumerate_surface_formats(struct vk_bundle *vk, VkSurfaceKHR surface, uint32_t *out_format_count, VkSurfaceFormatKHR **out_formats)
Enumerate the surface formats of the given VkSurfaceKHR, returns a list of VkSurfaceFormatKHR,...
void vk_name_object(struct vk_bundle *vk, VkObjectType type, uint64_t object, const char *name)
Uses VK_EXT_debug_utils to set a name for an object, for easier debugging.
void vk_print_opened_device_info(struct vk_bundle *vk, enum u_logging_level log_level)
Print device information about the device that bundle manages at the given logging level if the vk_bu...
Definition: vk_print.c:128
VkResult vk_check_required_instance_extensions(struct vk_bundle *vk, struct u_string_list *required_instance_ext_list)
Check if the required instance extensions are supported, if not print error message with all extensio...
Definition: vk_bundle_init.c:74
VkResult vk_get_device_functions(struct vk_bundle *vk)
Requires a device to have been created and set on the bundle.
Definition: vk_function_loaders.c:152
XRT_CHECK_RESULT VkResult vk_get_native_handle_from_device_memory(struct vk_bundle *vk, VkDeviceMemory device_memory, xrt_graphics_buffer_handle_t *out_handle)
Given a DeviceMemory handle created to be exportable, outputs the native buffer type (FD on desktop L...
Definition: vk_helpers.c:1367
VkResult vk_get_instance_functions(struct vk_bundle *vk)
Requires a instance to have been created and set on the bundle.
Definition: vk_function_loaders.c:57
void vk_cmd_insert_label(struct vk_bundle *vk, VkCommandBuffer cmd_buffer, const char *name)
Uses VK_EXT_debug_utils to insert debug label into a VkCommandBuffer.
void vk_print_result(struct vk_bundle *vk, const char *file, int line, const char *calling_func, VkResult ret, const char *called_func)
Print the result of a function, info level if ret == VK_SUCCESS and error level otherwise.
Definition: vk_print.c:74
VkResult vk_init_mutex(struct vk_bundle *vk)
Initialize mutexes in the vk_bundle.
Definition: vk_bundle_init.c:1627
xrt_swapchain_usage_bits
Usage of the swapchain images.
Definition: xrt_compositor.h:510
Wrapper around OS threading native functions.
A wrapper around a native mutex.
Definition: os_threading.h:55
Definition: u_string_list.cpp:19
Definition: vk_helpers.h:526
Definition: vk_helpers.h:59
uint32_t family_index
The queue family index.
Definition: vk_helpers.h:63
VkQueue queue
The Vulkan queue handle.
Definition: vk_helpers.h:61
struct os_mutex mutex
The queue mutex -.
Definition: vk_helpers.h:67
uint32_t index
The queue (instance) index.
Definition: vk_helpers.h:65
A bundle of Vulkan functions and objects, used by both Compositor and Compositor client code.
Definition: vk_helpers.h:78
uint32_t max_bound_descriptor_sets
Maximum number of descriptor sets that can be simultaneously used by a pipeline.
Definition: vk_helpers.h:219
bool synchronization_2
Was synchronization2 requested, available, and enabled?
Definition: vk_helpers.h:204
uint32_t max_descriptor_set_samplers
Maximum number of samplers that can be included in a pipeline layout.
Definition: vk_helpers.h:222
bool timeline_semaphore
Were timeline semaphore requested, available, and enabled?
Definition: vk_helpers.h:201
uint32_t max_per_stage_descriptor_samplers
Maximum number of samplers that can be accessible to a single shader stage in a pipeline layout.
Definition: vk_helpers.h:228
bool video_maintenance_1
Was KHR_video_maintenance1 requested, available, and enabled?
Definition: vk_helpers.h:210
struct vk_bundle_queue queues[2]
queues - a free list of unique queues
Definition: vk_helpers.h:95
uint32_t timestamp_valid_bits
Valid bits in the queue selected.
Definition: vk_helpers.h:198
uint32_t max_sampler_allocation_count
Maximum number of sampler objects, as created by vkCreateSampler, which can simultaneously exist on.
Definition: vk_helpers.h:216
uint32_t max_descriptor_set_sampled_images
Maximum number of sampled images that can be included in a pipeline layout.
Definition: vk_helpers.h:225
uint32_t max_per_stage_descriptor_storage_images
Per stage limit on storage images.
Definition: vk_helpers.h:234
bool is_tegra
Is the GPU a tegra device.
Definition: vk_helpers.h:238
bool timestamp_compute_and_graphics
Are timestamps available for compute and graphics queues?
Definition: vk_helpers.h:192
bool present_wait
Was KHR_present_wait requested, available, and enabled?
Definition: vk_helpers.h:207
uint32_t max_per_stage_descriptor_sampled_images
Per stage limit on sampled images (includes combined).
Definition: vk_helpers.h:231
float timestamp_period
Nanoseconds per gpu tick.
Definition: vk_helpers.h:195
Arguments to vk_create_descriptor_pool function.
Definition: vk_helpers.h:1469
uint32_t descriptor_count
The max count of created descriptors.
Definition: vk_helpers.h:1476
uint32_t uniform_per_descriptor_count
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER.
Definition: vk_helpers.h:1470
bool freeable
Are descriptors freeable, or must vkResetDescriptorPool be used.
Definition: vk_helpers.h:1479
uint32_t sampler_per_descriptor_count
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER.
Definition: vk_helpers.h:1471
uint32_t storage_image_per_descriptor_count
VK_DESCRIPTOR_TYPE_STORAGE_IMAGE.
Definition: vk_helpers.h:1472
uint32_t storage_buffer_per_descriptor_count
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER.
Definition: vk_helpers.h:1473
Used to enable device features as a argument vk_create_device.
Definition: vk_helpers.h:1084
Definition: vk_helpers.h:45
uint32_t index
The queue (instance) index.
Definition: vk_helpers.h:49
uint32_t family_index
The queue family index.
Definition: vk_helpers.h:47
A single image of a swapchain based on native buffer handles.
Definition: xrt_compositor.h:2161
Swapchain creation info.
Definition: xrt_compositor.h:871
Basic logging functionality.
A collection of strings, like a list of extensions to enable.
VkImageAspectFlags vk_csci_get_barrier_aspect_mask(VkFormat format)
Return the barrier aspect mask for this format, this is intended for the barriers that flush the data...
Definition: vk_compositor_flags.c:131
VkResult vk_create_descriptor_set(struct vk_bundle *vk, VkDescriptorPool descriptor_pool, VkDescriptorSetLayout descriptor_layout, VkDescriptorSet *out_descriptor_set)
Creates a descriptor set.
Definition: vk_state_creators.c:88
void vk_print_external_handles_info(struct vk_bundle *vk, enum u_logging_level log_level)
Print external handle features to the logger at the given logging level, if the vk_bundle has that le...
Definition: vk_print.c:152
VkExternalMemoryHandleTypeFlags vk_cb_get_buffer_external_handle_type(struct vk_bundle *vk)
Return the extern handle type that a buffer should be created with.
Definition: vk_compositor_flags.c:65
void vk_fill_in_has_instance_extensions(struct vk_bundle *vk, struct u_string_list *ext_list)
Fills in has_* in vk_bundle given a string of prefiltered instance extensions.
Definition: vk_bundle_init.c:179
XRT_CHECK_RESULT const char * vk_surface_transform_flag_string(VkSurfaceTransformFlagBitsKHR bits, bool null_on_unknown)
Returns the surface transform flag if one valid bit is set, if multiple bits are set,...
Definition: vk_helpers.c:699
bool vk_csci_is_format_supported(struct vk_bundle *vk, VkFormat format, enum xrt_swapchain_usage_bits xbits)
Verify if a format is supported for a specific usage.
Definition: vk_compositor_flags.c:344
XRT_CHECK_RESULT const char * xrt_swapchain_usage_flag_string(enum xrt_swapchain_usage_bits bits, bool null_on_unknown)
Returns xrt swapchain_usage flag if one valid bit is set, if multiple bits are set,...
Definition: vk_helpers.c:744
VkExternalMemoryHandleTypeFlags vk_csci_get_image_external_handle_type(struct vk_bundle *vk, struct xrt_image_native *xin)
Return the extern handle type that a image should be created with.
Definition: vk_compositor_flags.c:214
void vk_print_queues_info(const struct vk_bundle *vk, enum u_logging_level log_level)
Print queue info to the logger at the given logging level, if the vk_bundle has that level enabled.
Definition: vk_print.c:307
VkResult vk_create_pipeline_cache(struct vk_bundle *vk, VkPipelineCache *out_pipeline_cache)
Creates a pipeline cache.
Definition: vk_state_creators.c:147
VkResult vk_create_descriptor_pool(struct vk_bundle *vk, const struct vk_descriptor_pool_info *info, VkDescriptorPool *out_descriptor_pool)
Creates a descriptor pool, made for a single layout.
Definition: vk_state_creators.c:16
void vk_print_swapchain_create_info(struct vk_bundle *vk, VkSwapchainCreateInfoKHR *i, enum u_logging_level log_level)
Print a VkSwapchainCreateInfoKHR, used to log during creation.
Definition: vk_print.c:253
XRT_CHECK_RESULT const char * vk_image_usage_flag_string(VkImageUsageFlagBits bits, bool null_on_unknown)
Returns the image usage flag if one valid bit is set, if multiple bits are set, will return 'MULTIPLE...
Definition: vk_helpers.c:628
VkAccessFlags vk_csci_get_barrier_access_mask(enum xrt_swapchain_usage_bits bits)
Returns the access flags for the compositor to app barriers.
Definition: vk_compositor_flags.c:79
XRT_CHECK_RESULT const char * vk_format_feature_flag_string(VkFormatFeatureFlagBits bits, bool null_on_unknown)
Returns the format feature flag if one valid bit is set, if multiple bits are set,...
Definition: vk_helpers.c:607
void vk_csci_get_image_external_support(struct vk_bundle *vk, VkFormat image_format, enum xrt_swapchain_usage_bits bits, VkExternalMemoryHandleTypeFlags handle_type, bool *out_importable, bool *out_exportable)
Get whether a given image can be imported/exported for a handle type.
Definition: vk_compositor_flags.c:229
XRT_CHECK_RESULT const char * vk_composite_alpha_flag_string(VkCompositeAlphaFlagBitsKHR bits, bool null_on_unknown)
Returns the composite alpha flag if one valid bit is set, if multiple bits are set,...
Definition: vk_helpers.c:680
VkResult vk_create_compute_pipeline(struct vk_bundle *vk, VkPipelineCache pipeline_cache, VkShaderModule shader, VkPipelineLayout pipeline_layout, const VkSpecializationInfo *specialization_info, VkPipeline *out_compute_pipeline)
Creates a compute pipeline, assumes entry function is called 'main'.
Definition: vk_state_creators.c:172
void vk_print_features_info(struct vk_bundle *vk, enum u_logging_level log_level)
Print device features to the logger at the given logging level, if the vk_bundle has that level enabl...
Definition: vk_print.c:137
VkImageLayout vk_csci_get_barrier_optimal_layout(VkFormat format)
Return the optimal layout for this format, this is the layout as given to the app so is bound to the ...
Definition: vk_compositor_flags.c:112
VkImageUsageFlags vk_csci_get_image_usage_flags(struct vk_bundle *vk, VkFormat format, enum xrt_swapchain_usage_bits bits)
Returns the usage bits for a given selected format and usage.
Definition: vk_compositor_flags.c:177
VkResult vk_create_pipeline_layout(struct vk_bundle *vk, VkDescriptorSetLayout descriptor_set_layout, VkPipelineLayout *out_pipeline_layout)
Creates a pipeline layout from a single descriptor set layout.
Definition: vk_state_creators.c:118
VkImageAspectFlags vk_csci_get_image_view_aspect(VkFormat format, enum xrt_swapchain_usage_bits bits)
For images views created by the compositor to sample the images, what aspect should be set.
Definition: vk_compositor_flags.c:154
Header holding common defines.
Header declaring XRT graphics interfaces.
Native handle types.
int xrt_graphics_buffer_handle_t
The type underlying buffers shared between compositor clients and the main compositor.
Definition: xrt_handles.h:246
int xrt_graphics_sync_handle_t
The type underlying synchronization primitives (semaphores, etc) shared between compositor clients an...
Definition: xrt_handles.h:348
Include all of the Vulkan headers in one place, and cope with any "messy" includes implied by it.