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 VkCommandPool pool;
35 struct os_mutex mutex;
36};
37
38
39/*
40 *
41 * Functions.
42 *
43 */
44
45/*!
46 * Create a command buffer pool.
47 *
48 * @public @memberof vk_cmd_pool
49 */
50XRT_CHECK_RESULT VkResult
51vk_cmd_pool_init(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandPoolCreateFlags flags);
52
53/*!
54 * Destroy a command buffer pool, lock must not be held, externally
55 * synchronizable with all other pool commands.
56 *
57 * @public @memberof vk_cmd_pool
58 */
59void
61
62/*!
63 * Create a command buffer, call with the pool mutex held.
64 *
65 * @pre Command pool lock must be held, see @ref vk_cmd_pool_lock.
66 *
67 * @public @memberof vk_cmd_pool
68 */
69XRT_CHECK_RESULT VkResult
70vk_cmd_pool_create_cmd_buffer_locked(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandBuffer *out_cmd_buffer);
71
72/*!
73 * Create a command buffer and also begin it, call with the pool mutex held.
74 *
75 * @pre Command pool lock must be held.
76 *
77 * @public @memberof vk_cmd_pool
78 */
79XRT_CHECK_RESULT VkResult
81 struct vk_cmd_pool *pool,
82 VkCommandBufferUsageFlags flags,
83 VkCommandBuffer *out_cmd_buffer);
84
85/*!
86 * Submit to the vulkan queue, will take the queue mutex.
87 *
88 * @pre Command pool lock must be held, see @ref vk_cmd_pool_lock.
89 *
90 * @public @memberof vk_cmd_pool
91 */
92XRT_CHECK_RESULT VkResult
93vk_cmd_pool_submit_cmd_buffer_locked(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandBuffer cmd_buffer);
94
95/*!
96 * A do everything submit function, will take the queue mutex. Will create a
97 * fence and wait on the commands to complete. Will also end and destroy the
98 * passed in command buffer.
99 *
100 * @pre Command pool lock must be held, see @ref vk_cmd_pool_lock.
101 *
102 * Calls:
103 * * vkEndCommandBuffer
104 * * vkCreateFence
105 * * vkWaitForFences
106 * * vkDestroyFence
107 * * vkFreeCommandBuffers
108 *
109 * @public @memberof vk_cmd_pool
110 */
111XRT_CHECK_RESULT static inline VkResult
113 struct vk_cmd_pool *pool,
114 VkCommandBuffer cmd_buffer)
115{
116 return vk_cmd_end_submit_wait_and_free_cmd_buffer_locked(vk, pool->pool, cmd_buffer);
117}
118
119/*!
120 * Lock the command pool, needed for creating command buffers, filling out
121 * commands on any command buffers created from this pool and submitting any
122 * command buffers created from this pool to a VkQueue.
123 *
124 * @public @memberof vk_cmd_pool
125 */
126static inline void
128{
130}
131
132/*!
133 * Unlock the command pool.
134 *
135 * @public @memberof vk_cmd_pool
136 */
137static inline void
139{
141}
142
143/*!
144 * Locks, calls @ref vk_cmd_pool_create_cmd_buffer_locked, and then unlocks the
145 * command pool.
146 *
147 * @public @memberof vk_cmd_pool
148 */
149XRT_CHECK_RESULT static inline VkResult
150vk_cmd_pool_create_cmd_buffer(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandBuffer *out_cmd_buffer)
151{
153 VkResult ret = vk_cmd_pool_create_cmd_buffer_locked(vk, pool, out_cmd_buffer);
155 return ret;
156}
157
158/*!
159 * Locks, calls @ref vk_cmd_pool_create_and_begin_cmd_buffer_locked, and then
160 * unlocks the command pool.
161 *
162 * @public @memberof vk_cmd_pool
163 */
164XRT_CHECK_RESULT static inline VkResult
166 struct vk_cmd_pool *pool,
167 VkCommandBufferUsageFlags flags,
168 VkCommandBuffer *out_cmd_buffer)
169{
171 VkResult ret = vk_cmd_pool_create_and_begin_cmd_buffer_locked(vk, pool, flags, out_cmd_buffer);
173 return ret;
174}
175
176/*!
177 * Locks, calls @ref vk_cmd_submit_locked, and then unlocks the command
178 * pool. Will, during the call, take the queue lock and release it.
179 *
180 * @public @memberof vk_cmd_pool
181 */
182XRT_CHECK_RESULT static inline VkResult
184 struct vk_bundle *vk, struct vk_cmd_pool *pool, uint32_t count, const VkSubmitInfo *infos, VkFence fence)
185{
187 VkResult ret = vk_cmd_submit_locked(vk, count, infos, fence);
189 return ret;
190}
191
192/*!
193 * Locks, calls @ref vk_cmd_pool_submit_cmd_buffer_locked, and then unlocks the
194 * command pool. Will during the call take the queue lock and release it.
195 *
196 * @public @memberof vk_cmd_pool
197 */
198XRT_CHECK_RESULT static inline VkResult
199vk_cmd_pool_submit_cmd_buffer(struct vk_bundle *vk, struct vk_cmd_pool *pool, VkCommandBuffer cmd_buffer)
200{
202 VkResult ret = vk_cmd_pool_submit_cmd_buffer_locked(vk, pool, cmd_buffer);
204 return ret;
205}
206
207/*!
208 * Locks, calls @ref vk_cmd_pool_end_submit_wait_and_free_cmd_buffer_locked, and
209 * then unlocks the command pool. Will during the call take the queue lock and
210 * release it.
211 *
212 * @public @memberof vk_cmd_pool
213 */
214XRT_CHECK_RESULT static inline VkResult
216 struct vk_cmd_pool *pool,
217 VkCommandBuffer cmd_buffer)
218{
220 VkResult ret = vk_cmd_pool_end_submit_wait_and_free_cmd_buffer_locked(vk, pool, cmd_buffer);
222 return ret;
223}
224
225#ifdef VK_EXT_debug_utils
226/*!
227 * Small helper function that creates a command buffer and begins it,
228 * ends it after inserting a debug label into it.
229 *
230 * @pre Command pool lock must be held.
231 *
232 * @public @memberof vk_cmd_pool
233 */
234XRT_CHECK_RESULT VkResult
235vk_cmd_pool_create_begin_insert_label_and_end_cmd_buffer_locked(struct vk_bundle *vk,
236 struct vk_cmd_pool *pool,
237 const char *label_name,
238 VkCommandBuffer *out_cmd_buffer);
239#endif
240
241#ifdef __cplusplus
242}
243#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_end_submit_wait_and_free_cmd_buffer_locked(struct vk_bundle *vk, 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:97
XRT_CHECK_RESULT VkResult vk_cmd_submit_locked(struct vk_bundle *vk, 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
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
A bundle of Vulkan functions and objects, used by both Compositor and Compositor client code.
Definition: vk_helpers.h:49
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:138
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:165
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:183
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:85
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.c:22
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:45
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:112
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:59
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:150
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:215
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:127
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:125
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:199
Command buffer helpers.
Common Vulkan code header.