1/* 2 * Copyright © 2021 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24#ifndef VK_QUEUE_H 25#define VK_QUEUE_H 26 27#include "vk_device.h" 28 29#include "c11/threads.h" 30 31#include "util/list.h" 32#include "util/u_dynarray.h" 33 34#ifdef __cplusplus 35extern "C" { 36#endif 37 38struct vk_command_buffer; 39struct vk_queue_submit; 40struct vk_sync; 41struct vk_sync_wait; 42struct vk_sync_signal; 43struct vk_sync_timeline_point; 44 45struct vk_queue { 46 struct vk_object_base base; 47 48 /* Link in vk_device::queues */ 49 struct list_head link; 50 51 /* VkDeviceQueueCreateInfo::flags */ 52 VkDeviceQueueCreateFlags flags; 53 54 /* VkDeviceQueueCreateInfo::queueFamilyIndex */ 55 uint32_t queue_family_index; 56 57 /* Which queue this is within the queue family */ 58 uint32_t index_in_family; 59 60 /** Driver queue submit hook 61 * 62 * When using the common implementation of vkQueueSubmit(), this function 63 * is called to do the final submit to the kernel driver after all 64 * semaphore dependencies have been resolved. Depending on the timeline 65 * mode and application usage, this function may be called directly from 66 * the client thread on which vkQueueSubmit was called or from a runtime- 67 * managed submit thread. We do, however, guarantee that as long as the 68 * client follows the Vulkan threading rules, this function will never be 69 * called by the runtime concurrently on the same queue. 70 */ 71 VkResult (*driver_submit)(struct vk_queue *queue, 72 struct vk_queue_submit *submit); 73 74 struct { 75 /** Current submit mode 76 * 77 * This represents the exact current submit mode for this specific queue 78 * which may be different from `vk_device::submit_mode`. In particular, 79 * this will never be `VK_QUEUE_SUBMIT_MODE_THREADED_ON_DEMAND`. 80 * Instead, when the device submit mode is 81 * `VK_QUEUE_SUBMIT_MODE_THREADED_ON_DEMAND`, the queue submit mode 82 * will be one of `VK_QUEUE_SUBMIT_MODE_THREADED` or 83 * `VK_QUEUE_SUBMIT_MODE_IMMEDIATE` depending on whether or not a submit 84 * thread is currently running for this queue. If the device submit 85 * mode is `VK_QUEUE_SUBMIT_MODE_DEFERRED`, every queue in the device 86 * will use `VK_QUEUE_SUBMIT_MODE_DEFERRED` because the deferred submit 87 * model depends on regular flushing instead of independent threads. 88 */ 89 enum vk_queue_submit_mode mode; 90 91 mtx_t mutex; 92 cnd_t push; 93 cnd_t pop; 94 95 struct list_head submits; 96 97 bool thread_run; 98 thrd_t thread; 99 } submit; 100 101 struct { 102 /* Only set once atomically by the queue */ 103 int lost; 104 int error_line; 105 const char *error_file; 106 char error_msg[80]; 107 } _lost; 108 109 /** 110 * VK_EXT_debug_utils 111 * 112 * The next two fields represent debug labels storage. 113 * 114 * VK_EXT_debug_utils spec requires that upon triggering a debug message 115 * with a queue attached to it, all "active" labels will also be provided 116 * to the callback. The spec describes two distinct ways of attaching a 117 * debug label to the queue: opening a label region and inserting a single 118 * label. 119 * 120 * Label region is active between the corresponding `*BeginDebugUtilsLabel` 121 * and `*EndDebugUtilsLabel` calls. The spec doesn't mention any limits on 122 * nestedness of label regions. This implementation assumes that there 123 * aren't any. 124 * 125 * The spec, however, doesn't explain the lifetime of a label submitted by 126 * an `*InsertDebugUtilsLabel` call. The LunarG whitepaper [1] (pp 12-15) 127 * provides a more detailed explanation along with some examples. According 128 * to those, such label remains active until the next `*DebugUtilsLabel` 129 * call. This means that there can be no more than one such label at a 130 * time. 131 * 132 * \c labels contains all active labels at this point in order of submission 133 * \c region_begin denotes whether the most recent label opens a new region 134 * If \t labels is empty \t region_begin must be true. 135 * 136 * Anytime we modify labels, we first check for \c region_begin. If it's 137 * false, it means that the most recent label was submitted by 138 * `*InsertDebugUtilsLabel` and we need to remove it before doing anything 139 * else. 140 * 141 * See the discussion here: 142 * https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/10318#note_1061317 143 * 144 * [1] https://www.lunarg.com/wp-content/uploads/2018/05/Vulkan-Debug-Utils_05_18_v1.pdf 145 */ 146 struct util_dynarray labels; 147 bool region_begin; 148}; 149 150VK_DEFINE_HANDLE_CASTS(vk_queue, base, VkQueue, VK_OBJECT_TYPE_QUEUE) 151 152VkResult MUST_CHECK 153vk_queue_init(struct vk_queue *queue, struct vk_device *device, 154 const VkDeviceQueueCreateInfo *pCreateInfo, 155 uint32_t index_in_family); 156 157void 158vk_queue_finish(struct vk_queue *queue); 159 160static inline bool 161vk_queue_is_empty(struct vk_queue *queue) 162{ 163 return list_is_empty(&queue->submit.submits); 164} 165 166/** Enables threaded submit on this queue 167 * 168 * This should be called by the driver if it wants to be able to block inside 169 * `vk_queue::driver_submit`. Once this function has been called, the queue 170 * will always use a submit thread for all submissions. You must have called 171 * vk_device_enabled_threaded_submit() before calling this function. 172 */ 173VkResult vk_queue_enable_submit_thread(struct vk_queue *queue); 174 175VkResult vk_queue_flush(struct vk_queue *queue, uint32_t *submit_count_out); 176 177VkResult vk_queue_wait_before_present(struct vk_queue *queue, 178 const VkPresentInfoKHR *pPresentInfo); 179 180VkResult PRINTFLIKE(4, 5) 181_vk_queue_set_lost(struct vk_queue *queue, 182 const char *file, int line, 183 const char *msg, ...); 184 185#define vk_queue_set_lost(queue, ...) \ 186 _vk_queue_set_lost(queue, __FILE__, __LINE__, __VA_ARGS__) 187 188static inline bool 189vk_queue_is_lost(struct vk_queue *queue) 190{ 191 return queue->_lost.lost; 192} 193 194#define vk_foreach_queue(queue, device) \ 195 list_for_each_entry(struct vk_queue, queue, &(device)->queues, link) 196 197#define vk_foreach_queue_safe(queue, device) \ 198 list_for_each_entry_safe(struct vk_queue, queue, &(device)->queues, link) 199 200struct vk_queue_submit { 201 struct list_head link; 202 203 uint32_t wait_count; 204 uint32_t command_buffer_count; 205 uint32_t signal_count; 206 207 uint32_t buffer_bind_count; 208 uint32_t image_opaque_bind_count; 209 uint32_t image_bind_count; 210 211 struct vk_sync_wait *waits; 212 struct vk_command_buffer **command_buffers; 213 struct vk_sync_signal *signals; 214 215 VkSparseBufferMemoryBindInfo *buffer_binds; 216 VkSparseImageOpaqueMemoryBindInfo *image_opaque_binds; 217 VkSparseImageMemoryBindInfo *image_binds; 218 219 uint32_t perf_pass_index; 220 221 /* Used internally; should be ignored by drivers */ 222 struct vk_sync **_wait_temps; 223 struct vk_sync *_mem_signal_temp; 224 struct vk_sync_timeline_point **_wait_points; 225 struct vk_sync_timeline_point **_signal_points; 226}; 227 228#ifdef __cplusplus 229} 230#endif 231 232#endif /* VK_QUEUE_H */ 233