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_object.h"
30#include "util/list.h"
31#include "util/u_dynarray.h"
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37struct vk_command_pool;
38struct vk_framebuffer;
39struct vk_image_view;
40struct vk_render_pass;
41
42struct vk_attachment_view_state {
43   VkImageLayout layout;
44   VkImageLayout stencil_layout;
45   const VkSampleLocationsInfoEXT *sample_locations;
46};
47
48struct vk_attachment_state {
49   struct vk_image_view *image_view;
50
51   /** A running tally of which views have been loaded */
52   uint32_t views_loaded;
53
54   /** Per-view state */
55   struct vk_attachment_view_state views[MESA_VK_MAX_MULTIVIEW_VIEW_COUNT];
56
57   /** VkRenderPassBeginInfo::pClearValues[i] */
58   VkClearValue clear_value;
59};
60
61struct vk_command_buffer {
62   struct vk_object_base base;
63
64   struct vk_command_pool *pool;
65
66   /** VkCommandBufferAllocateInfo::level */
67   VkCommandBufferLevel level;
68
69   struct vk_dynamic_graphics_state dynamic_graphics_state;
70
71   /** Link in vk_command_pool::command_buffers if pool != NULL */
72   struct list_head pool_link;
73
74   /** Destroys the command buffer
75    *
76    * Used by the common command pool implementation.  This function MUST
77    * call vk_command_buffer_finish().
78    */
79   void (*destroy)(struct vk_command_buffer *);
80
81   /** Command list for emulated secondary command buffers */
82   struct vk_cmd_queue cmd_queue;
83
84   /**
85    * VK_EXT_debug_utils
86    *
87    * The next two fields represent debug labels storage.
88    *
89    * VK_EXT_debug_utils spec requires that upon triggering a debug message
90    * with a command buffer attached to it, all "active" labels will also be
91    * provided to the callback. The spec describes two distinct ways of
92    * attaching a debug label to the command buffer: opening a label region
93    * and inserting a single label.
94    *
95    * Label region is active between the corresponding `*BeginDebugUtilsLabel`
96    * and `*EndDebugUtilsLabel` calls. The spec doesn't mention any limits on
97    * nestedness of label regions. This implementation assumes that there
98    * aren't any.
99    *
100    * The spec, however, doesn't explain the lifetime of a label submitted by
101    * an `*InsertDebugUtilsLabel` call. The LunarG whitepaper [1] (pp 12-15)
102    * provides a more detailed explanation along with some examples. According
103    * to those, such label remains active until the next `*DebugUtilsLabel`
104    * call. This means that there can be no more than one such label at a
105    * time.
106    *
107    * \c labels contains all active labels at this point in order of submission
108    * \c region_begin denotes whether the most recent label opens a new region
109    * If \t labels is empty \t region_begin must be true.
110    *
111    * Anytime we modify labels, we first check for \c region_begin. If it's
112    * false, it means that the most recent label was submitted by
113    * `*InsertDebugUtilsLabel` and we need to remove it before doing anything
114    * else.
115    *
116    * See the discussion here:
117    * https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/10318#note_1061317
118    *
119    * [1] https://www.lunarg.com/wp-content/uploads/2018/05/Vulkan-Debug-Utils_05_18_v1.pdf
120    */
121   struct util_dynarray labels;
122   bool region_begin;
123
124   struct vk_render_pass *render_pass;
125   uint32_t subpass_idx;
126   struct vk_framebuffer *framebuffer;
127   VkRect2D render_area;
128
129   /* This uses the same trick as STACK_ARRAY */
130   struct vk_attachment_state *attachments;
131   struct vk_attachment_state _attachments[8];
132
133   VkRenderPassSampleLocationsBeginInfoEXT *pass_sample_locations;
134};
135
136VK_DEFINE_HANDLE_CASTS(vk_command_buffer, base, VkCommandBuffer,
137                       VK_OBJECT_TYPE_COMMAND_BUFFER)
138
139VkResult MUST_CHECK
140vk_command_buffer_init(struct vk_command_buffer *command_buffer,
141                       struct vk_command_pool *pool,
142                       VkCommandBufferLevel level);
143
144void
145vk_command_buffer_reset_render_pass(struct vk_command_buffer *cmd_buffer);
146
147void
148vk_command_buffer_reset(struct vk_command_buffer *command_buffer);
149
150void
151vk_command_buffer_finish(struct vk_command_buffer *command_buffer);
152
153#ifdef __cplusplus
154}
155#endif
156
157#endif  /* VK_COMMAND_BUFFER_H */
158