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_COMMAND_BUFFER_H
25 #define VK_COMMAND_BUFFER_H
26
27 #include "vk_cmd_queue.h"
28 #include "vk_graphics_state.h"
29 #include "vk_log.h"
30 #include "vk_meta_object_list.h"
31 #include "vk_object.h"
32 #include "util/list.h"
33 #include "util/u_dynarray.h"
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 struct vk_command_pool;
40 struct vk_framebuffer;
41 struct vk_image_view;
42 struct vk_render_pass;
43
44 struct vk_attachment_view_state {
45 VkImageLayout layout;
46 VkImageLayout stencil_layout;
47 const VkSampleLocationsInfoEXT *sample_locations;
48 };
49
50 struct vk_attachment_state {
51 struct vk_image_view *image_view;
52
53 /** A running tally of which views have been loaded */
54 uint32_t views_loaded;
55
56 /** Per-view state */
57 struct vk_attachment_view_state views[MESA_VK_MAX_MULTIVIEW_VIEW_COUNT];
58
59 /** VkRenderPassBeginInfo::pClearValues[i] */
60 VkClearValue clear_value;
61 };
62
63 /** Command buffer ops */
64 struct vk_command_buffer_ops {
65 /** Creates a command buffer
66 *
67 * Used by the common command pool implementation. This function MUST
68 * call `vk_command_buffer_finish()`. Notably, this function does not
69 * receive any additional parameters such as the level. The level will be
70 * set by `vk_common_AllocateCommandBuffers()` and the driver must not rely
71 * on it until `vkBeginCommandBuffer()` time.
72 */
73 VkResult (*create)(struct vk_command_pool *, VkCommandBufferLevel,
74 struct vk_command_buffer **);
75
76 /** Resets the command buffer
77 *
78 * Used by the common command pool implementation. This function MUST
79 * call `vk_command_buffer_reset()`. Unlike `vkResetCommandBuffer()`,
80 * this function does not have a return value because it may be called on
81 * destruction paths.
82 */
83 void (*reset)(struct vk_command_buffer *, VkCommandBufferResetFlags);
84
85 /** Destroys the command buffer
86 *
87 * Used by the common command pool implementation. This function MUST
88 * call `vk_command_buffer_finish()`.
89 */
90 void (*destroy)(struct vk_command_buffer *);
91 };
92
93 enum mesa_vk_command_buffer_state {
94 MESA_VK_COMMAND_BUFFER_STATE_INVALID,
95 MESA_VK_COMMAND_BUFFER_STATE_INITIAL,
96 MESA_VK_COMMAND_BUFFER_STATE_RECORDING,
97 MESA_VK_COMMAND_BUFFER_STATE_EXECUTABLE,
98 MESA_VK_COMMAND_BUFFER_STATE_PENDING,
99 };
100
101 /* this needs spec fixes */
102 #define MESA_VK_SHADER_STAGE_WORKGRAPH_HACK_BIT_FIXME (1<<30)
103 VkShaderStageFlags vk_shader_stages_from_bind_point(VkPipelineBindPoint pipelineBindPoint);
104
105 struct vk_command_buffer {
106 struct vk_object_base base;
107
108 struct vk_command_pool *pool;
109
110 /** VkCommandBufferAllocateInfo::level */
111 VkCommandBufferLevel level;
112
113 const struct vk_command_buffer_ops *ops;
114
115 struct vk_dynamic_graphics_state dynamic_graphics_state;
116
117 /** State of the command buffer */
118 enum mesa_vk_command_buffer_state state;
119
120 /** Command buffer recording error state. */
121 VkResult record_result;
122
123 /** Link in vk_command_pool::command_buffers if pool != NULL */
124 struct list_head pool_link;
125
126 /** Command list for emulated secondary command buffers */
127 struct vk_cmd_queue cmd_queue;
128
129 /** Object list for meta objects */
130 struct vk_meta_object_list meta_objects;
131
132 /**
133 * VK_EXT_debug_utils
134 *
135 * The next two fields represent debug labels storage.
136 *
137 * VK_EXT_debug_utils spec requires that upon triggering a debug message
138 * with a command buffer attached to it, all "active" labels will also be
139 * provided to the callback. The spec describes two distinct ways of
140 * attaching a debug label to the command buffer: opening a label region
141 * and inserting a single label.
142 *
143 * Label region is active between the corresponding `*BeginDebugUtilsLabel`
144 * and `*EndDebugUtilsLabel` calls. The spec doesn't mention any limits on
145 * nestedness of label regions. This implementation assumes that there
146 * aren't any.
147 *
148 * The spec, however, doesn't explain the lifetime of a label submitted by
149 * an `*InsertDebugUtilsLabel` call. The LunarG whitepaper [1] (pp 12-15)
150 * provides a more detailed explanation along with some examples. According
151 * to those, such label remains active until the next `*DebugUtilsLabel`
152 * call. This means that there can be no more than one such label at a
153 * time.
154 *
155 * ``labels`` contains all active labels at this point in order of
156 * submission ``region_begin`` denotes whether the most recent label opens
157 * a new region If ``labels`` is empty ``region_begin`` must be true.
158 *
159 * Anytime we modify labels, we first check for ``region_begin``. If it's
160 * false, it means that the most recent label was submitted by
161 * `*InsertDebugUtilsLabel` and we need to remove it before doing anything
162 * else.
163 *
164 * See the discussion here:
165 * https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/10318#note_1061317
166 *
167 * [1] https://www.lunarg.com/wp-content/uploads/2018/05/Vulkan-Debug-Utils_05_18_v1.pdf
168 */
169 struct util_dynarray labels;
170 bool region_begin;
171
172 struct vk_render_pass *render_pass;
173 uint32_t subpass_idx;
174 struct vk_framebuffer *framebuffer;
175 VkRect2D render_area;
176
177 /**
178 * True if we are currently inside a CmdPipelineBarrier() is inserted by
179 * the runtime's vk_render_pass.c
180 */
181 bool runtime_rp_barrier;
182
183 /* This uses the same trick as STACK_ARRAY */
184 struct vk_attachment_state *attachments;
185 struct vk_attachment_state _attachments[8];
186
187 VkRenderPassSampleLocationsBeginInfoEXT *pass_sample_locations;
188
189 /**
190 * Bitmask of shader stages bound via a vk_pipeline since the last call to
191 * vkBindShadersEXT().
192 *
193 * Used by the common vk_pipeline implementation
194 */
195 VkShaderStageFlags pipeline_shader_stages;
196 };
197
198 VK_DEFINE_HANDLE_CASTS(vk_command_buffer, base, VkCommandBuffer,
199 VK_OBJECT_TYPE_COMMAND_BUFFER)
200
201 VkResult MUST_CHECK
202 vk_command_buffer_init(struct vk_command_pool *pool,
203 struct vk_command_buffer *command_buffer,
204 const struct vk_command_buffer_ops *ops,
205 VkCommandBufferLevel level);
206
207 void
208 vk_command_buffer_reset_render_pass(struct vk_command_buffer *cmd_buffer);
209
210 void
211 vk_command_buffer_reset(struct vk_command_buffer *command_buffer);
212
213 void
214 vk_command_buffer_recycle(struct vk_command_buffer *command_buffer);
215
216 void
217 vk_command_buffer_begin(struct vk_command_buffer *command_buffer,
218 const VkCommandBufferBeginInfo *pBeginInfo);
219
220 VkResult
221 vk_command_buffer_end(struct vk_command_buffer *command_buffer);
222
223 void
224 vk_command_buffer_finish(struct vk_command_buffer *command_buffer);
225
226 static inline VkResult
__vk_command_buffer_set_error(struct vk_command_buffer * command_buffer,VkResult error,const char * file,int line)227 __vk_command_buffer_set_error(struct vk_command_buffer *command_buffer,
228 VkResult error, const char *file, int line)
229 {
230 assert(error != VK_SUCCESS);
231 error = __vk_errorf(command_buffer, error, file, line, NULL);
232 if (command_buffer->record_result == VK_SUCCESS)
233 command_buffer->record_result = error;
234 return error;
235 }
236
237 #define vk_command_buffer_set_error(command_buffer, error) \
238 __vk_command_buffer_set_error(command_buffer, error, __FILE__, __LINE__)
239
240 static inline VkResult
vk_command_buffer_get_record_result(struct vk_command_buffer * command_buffer)241 vk_command_buffer_get_record_result(struct vk_command_buffer *command_buffer)
242 {
243 return command_buffer->record_result;
244 }
245
246 #define vk_command_buffer_has_error(command_buffer) \
247 unlikely((command_buffer)->record_result != VK_SUCCESS)
248
249 #ifdef __cplusplus
250 }
251 #endif
252
253 #endif /* VK_COMMAND_BUFFER_H */
254