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
35 extern "C" {
36 #endif
37 
38 struct vk_command_buffer;
39 struct vk_queue_submit;
40 struct vk_sync;
41 struct vk_sync_wait;
42 struct vk_sync_signal;
43 struct vk_sync_timeline_point;
44 
45 struct 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 #ifdef ANDROID
150    /** SYNC_FD signal semaphore for vkQueueSignalReleaseImageANDROID
151     *
152     * VK_ANDROID_native_buffer enforces explicit fencing on the present api
153     * boundary. To avoid assuming all waitSemaphores exportable to sync file
154     * and to capture pending cmds in the queue, we do a simple submission and
155     * signal a SYNC_FD handle type external sempahore for native fence export.
156     *
157     * This plays the same role as wsi_swapchain::dma_buf_semaphore for WSI.
158     * The VK_ANDROID_native_buffer spec hides the swapchain object from the
159     * icd, so we have to cache the semaphore in common vk_queue.
160     *
161     * This also makes it easier to add additional cmds to prepare the wsi
162     * image for implementations requiring such (e.g. for layout transition).
163     */
164    VkSemaphore anb_semaphore;
165 #endif
166 };
167 
168 VK_DEFINE_HANDLE_CASTS(vk_queue, base, VkQueue, VK_OBJECT_TYPE_QUEUE)
169 
170 VkResult MUST_CHECK
171 vk_queue_init(struct vk_queue *queue, struct vk_device *device,
172               const VkDeviceQueueCreateInfo *pCreateInfo,
173               uint32_t index_in_family);
174 
175 void
176 vk_queue_finish(struct vk_queue *queue);
177 
178 static inline bool
vk_queue_is_empty(struct vk_queue * queue)179 vk_queue_is_empty(struct vk_queue *queue)
180 {
181    return list_is_empty(&queue->submit.submits);
182 }
183 
184 /** Enables threaded submit on this queue
185  *
186  * This should be called by the driver if it wants to be able to block inside
187  * `vk_queue::driver_submit`.  Once this function has been called, the queue
188  * will always use a submit thread for all submissions.  You must have called
189  * vk_device_enabled_threaded_submit() before calling this function.
190  */
191 VkResult vk_queue_enable_submit_thread(struct vk_queue *queue);
192 
193 VkResult vk_queue_flush(struct vk_queue *queue, uint32_t *submit_count_out);
194 
195 VkResult vk_queue_wait_before_present(struct vk_queue *queue,
196                                       const VkPresentInfoKHR *pPresentInfo);
197 
198 VkResult PRINTFLIKE(4, 5)
199 _vk_queue_set_lost(struct vk_queue *queue,
200                    const char *file, int line,
201                    const char *msg, ...);
202 
203 #define vk_queue_set_lost(queue, ...) \
204    _vk_queue_set_lost(queue, __FILE__, __LINE__, __VA_ARGS__)
205 
206 static inline bool
vk_queue_is_lost(struct vk_queue * queue)207 vk_queue_is_lost(struct vk_queue *queue)
208 {
209    return queue->_lost.lost;
210 }
211 
212 #define vk_foreach_queue(queue, device) \
213    list_for_each_entry(struct vk_queue, queue, &(device)->queues, link)
214 
215 #define vk_foreach_queue_safe(queue, device) \
216    list_for_each_entry_safe(struct vk_queue, queue, &(device)->queues, link)
217 
218 struct vk_queue_submit {
219    struct list_head link;
220 
221    uint32_t wait_count;
222    uint32_t command_buffer_count;
223    uint32_t signal_count;
224 
225    uint32_t buffer_bind_count;
226    uint32_t image_opaque_bind_count;
227    uint32_t image_bind_count;
228 
229    struct vk_sync_wait *waits;
230    struct vk_command_buffer **command_buffers;
231    struct vk_sync_signal *signals;
232 
233    VkSparseBufferMemoryBindInfo *buffer_binds;
234    VkSparseImageOpaqueMemoryBindInfo *image_opaque_binds;
235    VkSparseImageMemoryBindInfo *image_binds;
236 
237    uint32_t perf_pass_index;
238 
239    /* Used internally; should be ignored by drivers */
240    struct vk_sync **_wait_temps;
241    struct vk_sync *_mem_signal_temp;
242    struct vk_sync_timeline_point **_wait_points;
243    struct vk_sync_timeline_point **_signal_points;
244 };
245 
246 #ifdef __cplusplus
247 }
248 #endif
249 
250 #endif  /* VK_QUEUE_H */
251