Monado OpenXR Runtime
vk_cmd_pool.h
Go to the documentation of this file.
1// Copyright 2019-2023, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Command pool helpers.
6 * @author Jakob Bornecrantz <jakob@collabora.com>
7 * @author Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
8 * @ingroup aux_vk
9 */
10
11#pragma once
12
13#include "vk/vk_helpers.h"
14#include "vk/vk_cmd.h"
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
20
21/*
22 *
23 * Struct(s)
24 *
25 */
26
27/*!
28 * Small helper to manage lock around a command pool.
29 *
30 * @ingroup aux_vk
31 */
33{
34 //! The command pool for command buffers
35 VkCommandPool pool;
36
37 /*!
38 * @brief Queue (family) associated with @ref vk_cmd_pool::pool,
39 *
40 * weak reference to any queue in @ref vk_bundle (e.g. vk_bundle::[graphics|compute]_queue)
41 * should not live longer than the @ref vk_bundle instance.
42 */
44
45 //! Command Pool mutex
47};
48
49
50/*
51 *
52 * Functions.
53 *
54 */
55
56/*!
57 * Create a command buffer pool.
58 *
59 * @public @memberof vk_cmd_pool
60 */
61XRT_CHECK_RESULT VkResult
63 struct vk_cmd_pool *pool,
64 VkCommandPoolCreateFlags flags,
65 struct vk_bundle_queue *queue);
66
67/*!
68 * Create a command buffer pool.
69 *
70 * @public @memberof vk_cmd_pool
71 */
72static inline XRT_CHECK_RESULT VkResult
73vk_cmd_pool_init(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandPoolCreateFlags flags)
74{
75 return vk_cmd_pool_init_for_queue(vk, pool, flags, &vk->main_queue);
76}
77
78/*!
79 * Destroy a command buffer pool, lock must not be held, externally
80 * synchronizable with all other pool commands.
81 *
82 * @public @memberof vk_cmd_pool
83 */
84void
86
87/*!
88 * Create a command buffer, call with the pool mutex held.
89 *
90 * @pre Command pool lock must be held, see @ref vk_cmd_pool_lock.
91 *
92 * @public @memberof vk_cmd_pool
93 */
94XRT_CHECK_RESULT VkResult
95vk_cmd_pool_create_cmd_buffer_locked(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandBuffer *out_cmd_buffer);
96
97/*!
98 * Create a command buffer and also begin it, call with the pool mutex held.
99 *
100 * @pre Command pool lock must be held.
101 *
102 * @public @memberof vk_cmd_pool
103 */
104XRT_CHECK_RESULT VkResult
106 struct vk_cmd_pool *pool,
107 VkCommandBufferUsageFlags flags,
108 VkCommandBuffer *out_cmd_buffer);
109
110/*!
111 * Submit to the vulkan queue, will take the queue mutex.
112 *
113 * @pre Command pool lock must be held, see @ref vk_cmd_pool_lock.
114 *
115 * @public @memberof vk_cmd_pool
116 */
117XRT_CHECK_RESULT VkResult
118vk_cmd_pool_submit_cmd_buffer_locked(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandBuffer cmd_buffer);
119
120/*!
121 * A do everything submit function, will take the queue mutex. Will create a
122 * fence and wait on the commands to complete. Will also end and destroy the
123 * passed in command buffer.
124 *
125 * @pre Command pool lock must be held, see @ref vk_cmd_pool_lock.
126 *
127 * Calls:
128 * * vkEndCommandBuffer
129 * * vkCreateFence
130 * * vkWaitForFences
131 * * vkDestroyFence
132 * * vkFreeCommandBuffers
133 *
134 * @public @memberof vk_cmd_pool
135 */
136XRT_CHECK_RESULT static inline VkResult
138 struct vk_cmd_pool *pool,
139 VkCommandBuffer cmd_buffer)
140{
141 return vk_cmd_end_submit_wait_and_free_cmd_buffer_locked(vk, pool->queue, pool->pool, cmd_buffer);
142}
143
144/*!
145 * Lock the command pool, needed for creating command buffers, filling out
146 * commands on any command buffers created from this pool and submitting any
147 * command buffers created from this pool to a VkQueue.
148 *
149 * @public @memberof vk_cmd_pool
150 */
151static inline void
153{
155}
156
157/*!
158 * Unlock the command pool.
159 *
160 * @public @memberof vk_cmd_pool
161 */
162static inline void
164{
166}
167
168/*!
169 * Locks, calls @ref vk_cmd_pool_create_cmd_buffer_locked, and then unlocks the
170 * command pool.
171 *
172 * @public @memberof vk_cmd_pool
173 */
174XRT_CHECK_RESULT static inline VkResult
175vk_cmd_pool_create_cmd_buffer(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandBuffer *out_cmd_buffer)
176{
178 VkResult ret = vk_cmd_pool_create_cmd_buffer_locked(vk, pool, out_cmd_buffer);
180 return ret;
181}
182
183/*!
184 * Locks, calls @ref vk_cmd_pool_create_and_begin_cmd_buffer_locked, and then
185 * unlocks the command pool.
186 *
187 * @public @memberof vk_cmd_pool
188 */
189XRT_CHECK_RESULT static inline VkResult
191 struct vk_cmd_pool *pool,
192 VkCommandBufferUsageFlags flags,
193 VkCommandBuffer *out_cmd_buffer)
194{
196 VkResult ret = vk_cmd_pool_create_and_begin_cmd_buffer_locked(vk, pool, flags, out_cmd_buffer);
198 return ret;
199}
200
201/*!
202 * Locks, calls @ref vk_cmd_submit_locked, and then unlocks the command
203 * pool. Will, during the call, take the queue lock and release it.
204 *
205 * @public @memberof vk_cmd_pool
206 */
207XRT_CHECK_RESULT static inline VkResult
209 struct vk_bundle *vk, struct vk_cmd_pool *pool, uint32_t count, const VkSubmitInfo *infos, VkFence fence)
210{
212 VkResult ret = vk_cmd_submit_locked(vk, pool->queue, count, infos, fence);
214 return ret;
215}
216
217/*!
218 * Locks, calls @ref vk_cmd_pool_submit_cmd_buffer_locked, and then unlocks the
219 * command pool. Will during the call take the queue lock and release it.
220 *
221 * @public @memberof vk_cmd_pool
222 */
223XRT_CHECK_RESULT static inline VkResult
224vk_cmd_pool_submit_cmd_buffer(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandBuffer cmd_buffer)
225{
227 VkResult ret = vk_cmd_pool_submit_cmd_buffer_locked(vk, pool, cmd_buffer);
229 return ret;
230}
231
232/*!
233 * Locks, calls @ref vk_cmd_pool_end_submit_wait_and_free_cmd_buffer_locked, and
234 * then unlocks the command pool. Will during the call take the queue lock and
235 * release it.
236 *
237 * @public @memberof vk_cmd_pool
238 */
239XRT_CHECK_RESULT static inline VkResult
241 struct vk_cmd_pool *pool,
242 VkCommandBuffer cmd_buffer)
243{
245 VkResult ret = vk_cmd_pool_end_submit_wait_and_free_cmd_buffer_locked(vk, pool, cmd_buffer);
247 return ret;
248}
249
250#ifdef VK_EXT_debug_utils
251/*!
252 * Small helper function that creates a command buffer and begins it,
253 * ends it after inserting a debug label into it.
254 *
255 * @pre Command pool lock must be held.
256 *
257 * @public @memberof vk_cmd_pool
258 */
259XRT_CHECK_RESULT VkResult
260vk_cmd_pool_create_begin_insert_label_and_end_cmd_buffer_locked(struct vk_bundle *vk,
261 struct vk_cmd_pool *pool,
262 const char *label_name,
263 VkCommandBuffer *out_cmd_buffer);
264#endif
265
266#ifdef __cplusplus
267}
268#endif
static void os_mutex_lock(struct os_mutex *om)
Lock.
Definition: os_threading.h:86
static void os_mutex_unlock(struct os_mutex *om)
Unlock.
Definition: os_threading.h:110
XRT_CHECK_RESULT VkResult vk_cmd_submit_locked(struct vk_bundle *vk, struct vk_bundle_queue *queue, uint32_t count, const VkSubmitInfo *infos, VkFence fence)
Very small helper to submit a command buffer, the _locked suffix refers to the command pool not the q...
Definition: vk_cmd.c:81
XRT_CHECK_RESULT VkResult vk_cmd_end_submit_wait_and_free_cmd_buffer_locked(struct vk_bundle *vk, struct vk_bundle_queue *queue, VkCommandPool pool, VkCommandBuffer cmd_buffer)
A do everything command buffer submission function, the _locked suffix refers to the command pool not...
Definition: vk_cmd.c:98
A very simple implementation of a fence primitive.
Definition: comp_sync.c:36
Definition: m_space.cpp:87
A wrapper around a native mutex.
Definition: os_threading.h:55
Definition: u_worker.c:49
struct os_mutex mutex
Big contenious mutex.
Definition: u_worker.c:53
Definition: vk_helpers.h:44
A bundle of Vulkan functions and objects, used by both Compositor and Compositor client code.
Definition: vk_helpers.h:67
Small helper to manage lock around a command pool.
Definition: vk_cmd_pool.h:33
static void vk_cmd_pool_unlock(struct vk_cmd_pool *pool)
Unlock the command pool.
Definition: vk_cmd_pool.h:163
static XRT_CHECK_RESULT VkResult vk_cmd_pool_create_and_begin_cmd_buffer(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandBufferUsageFlags flags, VkCommandBuffer *out_cmd_buffer)
Locks, calls vk_cmd_pool_create_and_begin_cmd_buffer_locked, and then unlocks the command pool.
Definition: vk_cmd_pool.h:190
XRT_CHECK_RESULT VkResult vk_cmd_pool_init_for_queue(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandPoolCreateFlags flags, struct vk_bundle_queue *queue)
Create a command buffer pool.
Definition: vk_cmd_pool.c:22
struct os_mutex mutex
Command Pool mutex.
Definition: vk_cmd_pool.h:46
static XRT_CHECK_RESULT VkResult vk_cmd_pool_submit(struct vk_bundle *vk, struct vk_cmd_pool *pool, uint32_t count, const VkSubmitInfo *infos, VkFence fence)
Locks, calls vk_cmd_submit_locked, and then unlocks the command pool.
Definition: vk_cmd_pool.h:208
XRT_CHECK_RESULT VkResult vk_cmd_pool_create_and_begin_cmd_buffer_locked(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandBufferUsageFlags flags, VkCommandBuffer *out_cmd_buffer)
Create a command buffer and also begin it, call with the pool mutex held.
Definition: vk_cmd_pool.c:91
static XRT_CHECK_RESULT VkResult vk_cmd_pool_init(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandPoolCreateFlags flags)
Create a command buffer pool.
Definition: vk_cmd_pool.h:73
struct vk_bundle_queue * queue
Queue (family) associated with vk_cmd_pool::pool,.
Definition: vk_cmd_pool.h:43
void vk_cmd_pool_destroy(struct vk_bundle *vk, struct vk_cmd_pool *pool)
Destroy a command buffer pool, lock must not be held, externally synchronizable with all other pool c...
Definition: vk_cmd_pool.c:51
static XRT_CHECK_RESULT VkResult vk_cmd_pool_end_submit_wait_and_free_cmd_buffer_locked(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandBuffer cmd_buffer)
A do everything submit function, will take the queue mutex.
Definition: vk_cmd_pool.h:137
XRT_CHECK_RESULT VkResult vk_cmd_pool_create_cmd_buffer_locked(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandBuffer *out_cmd_buffer)
Create a command buffer, call with the pool mutex held.
Definition: vk_cmd_pool.c:65
static XRT_CHECK_RESULT VkResult vk_cmd_pool_create_cmd_buffer(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandBuffer *out_cmd_buffer)
Locks, calls vk_cmd_pool_create_cmd_buffer_locked, and then unlocks the command pool.
Definition: vk_cmd_pool.h:175
static XRT_CHECK_RESULT VkResult vk_cmd_pool_end_submit_wait_and_free_cmd_buffer(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandBuffer cmd_buffer)
Locks, calls vk_cmd_pool_end_submit_wait_and_free_cmd_buffer_locked, and then unlocks the command poo...
Definition: vk_cmd_pool.h:240
static void vk_cmd_pool_lock(struct vk_cmd_pool *pool)
Lock the command pool, needed for creating command buffers, filling out commands on any command buffe...
Definition: vk_cmd_pool.h:152
XRT_CHECK_RESULT VkResult vk_cmd_pool_submit_cmd_buffer_locked(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandBuffer cmd_buffer)
Submit to the vulkan queue, will take the queue mutex.
Definition: vk_cmd_pool.c:131
VkCommandPool pool
The command pool for command buffers.
Definition: vk_cmd_pool.h:35
static XRT_CHECK_RESULT VkResult vk_cmd_pool_submit_cmd_buffer(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandBuffer cmd_buffer)
Locks, calls vk_cmd_pool_submit_cmd_buffer_locked, and then unlocks the command pool.
Definition: vk_cmd_pool.h:224
Command buffer helpers.
Common Vulkan code header.