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