1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2021 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#ifndef VK_COMMAND_BUFFER_H
25bf215546Sopenharmony_ci#define VK_COMMAND_BUFFER_H
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "vk_cmd_queue.h"
28bf215546Sopenharmony_ci#include "vk_graphics_state.h"
29bf215546Sopenharmony_ci#include "vk_object.h"
30bf215546Sopenharmony_ci#include "util/list.h"
31bf215546Sopenharmony_ci#include "util/u_dynarray.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#ifdef __cplusplus
34bf215546Sopenharmony_ciextern "C" {
35bf215546Sopenharmony_ci#endif
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cistruct vk_command_pool;
38bf215546Sopenharmony_cistruct vk_framebuffer;
39bf215546Sopenharmony_cistruct vk_image_view;
40bf215546Sopenharmony_cistruct vk_render_pass;
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_cistruct vk_attachment_view_state {
43bf215546Sopenharmony_ci   VkImageLayout layout;
44bf215546Sopenharmony_ci   VkImageLayout stencil_layout;
45bf215546Sopenharmony_ci   const VkSampleLocationsInfoEXT *sample_locations;
46bf215546Sopenharmony_ci};
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_cistruct vk_attachment_state {
49bf215546Sopenharmony_ci   struct vk_image_view *image_view;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   /** A running tally of which views have been loaded */
52bf215546Sopenharmony_ci   uint32_t views_loaded;
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci   /** Per-view state */
55bf215546Sopenharmony_ci   struct vk_attachment_view_state views[MESA_VK_MAX_MULTIVIEW_VIEW_COUNT];
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   /** VkRenderPassBeginInfo::pClearValues[i] */
58bf215546Sopenharmony_ci   VkClearValue clear_value;
59bf215546Sopenharmony_ci};
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_cistruct vk_command_buffer {
62bf215546Sopenharmony_ci   struct vk_object_base base;
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   struct vk_command_pool *pool;
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   /** VkCommandBufferAllocateInfo::level */
67bf215546Sopenharmony_ci   VkCommandBufferLevel level;
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci   struct vk_dynamic_graphics_state dynamic_graphics_state;
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci   /** Link in vk_command_pool::command_buffers if pool != NULL */
72bf215546Sopenharmony_ci   struct list_head pool_link;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   /** Destroys the command buffer
75bf215546Sopenharmony_ci    *
76bf215546Sopenharmony_ci    * Used by the common command pool implementation.  This function MUST
77bf215546Sopenharmony_ci    * call vk_command_buffer_finish().
78bf215546Sopenharmony_ci    */
79bf215546Sopenharmony_ci   void (*destroy)(struct vk_command_buffer *);
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   /** Command list for emulated secondary command buffers */
82bf215546Sopenharmony_ci   struct vk_cmd_queue cmd_queue;
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci   /**
85bf215546Sopenharmony_ci    * VK_EXT_debug_utils
86bf215546Sopenharmony_ci    *
87bf215546Sopenharmony_ci    * The next two fields represent debug labels storage.
88bf215546Sopenharmony_ci    *
89bf215546Sopenharmony_ci    * VK_EXT_debug_utils spec requires that upon triggering a debug message
90bf215546Sopenharmony_ci    * with a command buffer attached to it, all "active" labels will also be
91bf215546Sopenharmony_ci    * provided to the callback. The spec describes two distinct ways of
92bf215546Sopenharmony_ci    * attaching a debug label to the command buffer: opening a label region
93bf215546Sopenharmony_ci    * and inserting a single label.
94bf215546Sopenharmony_ci    *
95bf215546Sopenharmony_ci    * Label region is active between the corresponding `*BeginDebugUtilsLabel`
96bf215546Sopenharmony_ci    * and `*EndDebugUtilsLabel` calls. The spec doesn't mention any limits on
97bf215546Sopenharmony_ci    * nestedness of label regions. This implementation assumes that there
98bf215546Sopenharmony_ci    * aren't any.
99bf215546Sopenharmony_ci    *
100bf215546Sopenharmony_ci    * The spec, however, doesn't explain the lifetime of a label submitted by
101bf215546Sopenharmony_ci    * an `*InsertDebugUtilsLabel` call. The LunarG whitepaper [1] (pp 12-15)
102bf215546Sopenharmony_ci    * provides a more detailed explanation along with some examples. According
103bf215546Sopenharmony_ci    * to those, such label remains active until the next `*DebugUtilsLabel`
104bf215546Sopenharmony_ci    * call. This means that there can be no more than one such label at a
105bf215546Sopenharmony_ci    * time.
106bf215546Sopenharmony_ci    *
107bf215546Sopenharmony_ci    * \c labels contains all active labels at this point in order of submission
108bf215546Sopenharmony_ci    * \c region_begin denotes whether the most recent label opens a new region
109bf215546Sopenharmony_ci    * If \t labels is empty \t region_begin must be true.
110bf215546Sopenharmony_ci    *
111bf215546Sopenharmony_ci    * Anytime we modify labels, we first check for \c region_begin. If it's
112bf215546Sopenharmony_ci    * false, it means that the most recent label was submitted by
113bf215546Sopenharmony_ci    * `*InsertDebugUtilsLabel` and we need to remove it before doing anything
114bf215546Sopenharmony_ci    * else.
115bf215546Sopenharmony_ci    *
116bf215546Sopenharmony_ci    * See the discussion here:
117bf215546Sopenharmony_ci    * https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/10318#note_1061317
118bf215546Sopenharmony_ci    *
119bf215546Sopenharmony_ci    * [1] https://www.lunarg.com/wp-content/uploads/2018/05/Vulkan-Debug-Utils_05_18_v1.pdf
120bf215546Sopenharmony_ci    */
121bf215546Sopenharmony_ci   struct util_dynarray labels;
122bf215546Sopenharmony_ci   bool region_begin;
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci   struct vk_render_pass *render_pass;
125bf215546Sopenharmony_ci   uint32_t subpass_idx;
126bf215546Sopenharmony_ci   struct vk_framebuffer *framebuffer;
127bf215546Sopenharmony_ci   VkRect2D render_area;
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci   /* This uses the same trick as STACK_ARRAY */
130bf215546Sopenharmony_ci   struct vk_attachment_state *attachments;
131bf215546Sopenharmony_ci   struct vk_attachment_state _attachments[8];
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci   VkRenderPassSampleLocationsBeginInfoEXT *pass_sample_locations;
134bf215546Sopenharmony_ci};
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ciVK_DEFINE_HANDLE_CASTS(vk_command_buffer, base, VkCommandBuffer,
137bf215546Sopenharmony_ci                       VK_OBJECT_TYPE_COMMAND_BUFFER)
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ciVkResult MUST_CHECK
140bf215546Sopenharmony_civk_command_buffer_init(struct vk_command_buffer *command_buffer,
141bf215546Sopenharmony_ci                       struct vk_command_pool *pool,
142bf215546Sopenharmony_ci                       VkCommandBufferLevel level);
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_civoid
145bf215546Sopenharmony_civk_command_buffer_reset_render_pass(struct vk_command_buffer *cmd_buffer);
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_civoid
148bf215546Sopenharmony_civk_command_buffer_reset(struct vk_command_buffer *command_buffer);
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_civoid
151bf215546Sopenharmony_civk_command_buffer_finish(struct vk_command_buffer *command_buffer);
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci#ifdef __cplusplus
154bf215546Sopenharmony_ci}
155bf215546Sopenharmony_ci#endif
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci#endif  /* VK_COMMAND_BUFFER_H */
158