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#ifndef VK_RENDER_PASS_H
24#define VK_RENDER_PASS_H
25
26#include "vk_object.h"
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/**
33 * Pseudo-extension struct that may be chained into VkRenderingInfo,
34 * VkCommandBufferInheritanceRenderingInfo, or VkPipelineRenderingCreateInfo
35 * to provide self-dependency information.
36 */
37typedef struct VkRenderingSelfDependencyInfoMESA {
38    VkStructureType    sType;
39#define VK_STRUCTURE_TYPE_RENDERING_SELF_DEPENDENCY_INFO_MESA (VkStructureType)1000044900
40    const void*        pNext;
41
42    /** Bitset of which color attachments have self-dependencies */
43    uint32_t           colorSelfDependencies;
44
45    /** True if there is a depth self-dependency */
46    VkBool32           depthSelfDependency;
47
48    /** True if there is a stencil self-dependency */
49    VkBool32           stencilSelfDependency;
50} VkRenderingSelfDependencyInfoMESA;
51
52/**
53 * Pseudo-extension struct that may be chained into VkRenderingAttachmentInfo
54 * to indicate an initial layout for the attachment.  This is only allowed if
55 * all of the following conditions are met:
56 *
57 *    1. VkRenderingAttachmentInfo::loadOp == LOAD_OP_CLEAR
58 *
59 *    2. VkRenderingInfo::renderArea is tne entire image view LOD
60 *
61 *    3. For 3D image attachments, VkRenderingInfo::viewMask == 0 AND
62 *       VkRenderingInfo::layerCount references the entire bound image view
63 *       OR VkRenderingInfo::viewMask is dense (no holes) and references the
64 *       entire bound image view.  (2D and 2D array images have no such
65 *       requirement.)
66 *
67 * If this struct is included in the pNext chain of a
68 * VkRenderingAttachmentInfo, the driver is responsible for transitioning the
69 * bound region of the image from
70 * VkRenderingAttachmentInitialLayoutInfoMESA::initialLayout to
71 * VkRenderingAttachmentInfo::imageLayout prior to rendering.
72 */
73typedef struct VkRenderingAttachmentInitialLayoutInfoMESA {
74    VkStructureType    sType;
75#define VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INITIAL_LAYOUT_INFO_MESA (VkStructureType)1000044901
76    const void*        pNext;
77
78    /** Initial layout of the attachment */
79    VkImageLayout      initialLayout;
80} VkRenderingAttachmentInitialLayoutInfoMESA;
81
82#define VK_IMAGE_LAYOUT_SUBPASS_SELF_DEPENDENCY_MESA (VkImageLayout)1000044902
83
84struct vk_subpass_attachment {
85   /** VkAttachmentReference2::attachment */
86   uint32_t attachment;
87
88   /** Aspects referenced by this attachment
89    *
90    * For an input attachment, this is VkAttachmentReference2::aspectMask.
91    * For all others, it's equal to the vk_render_pass_attachment::aspects.
92    */
93   VkImageAspectFlags aspects;
94
95   /** Usage for this attachment
96    *
97    * This is a single VK_IMAGE_USAGE_* describing the usage of this subpass
98    * attachment.  Resolve attachments are VK_IMAGE_USAGE_TRANSFER_DST_BIT.
99    */
100   VkImageUsageFlagBits usage;
101
102   /** VkAttachmentReference2::layout */
103   VkImageLayout layout;
104
105   /** VkAttachmentReferenceStencilLayout::stencilLayout
106    *
107    * If VK_KHR_separate_depth_stencil_layouts is not used, this will be
108    * layout if the attachment contains stencil and VK_IMAGE_LAYOUT_UNDEFINED
109    * otherwise.
110    */
111   VkImageLayout stencil_layout;
112
113   /** A per-view mask for if this is the last use of this attachment
114    *
115    * If the same render pass attachment is used multiple ways within a
116    * subpass, corresponding last_subpass bits will be set in all of them.
117    * For the non-multiview case, only the first bit is used.
118    */
119   uint32_t last_subpass;
120
121   /** Resolve attachment, if any */
122   struct vk_subpass_attachment *resolve;
123};
124
125struct vk_subpass {
126   /** Count of all attachments referenced by this subpass */
127   uint32_t attachment_count;
128
129   /** Array of all attachments referenced by this subpass */
130   struct vk_subpass_attachment *attachments;
131
132   /** VkSubpassDescription2::inputAttachmentCount */
133   uint32_t input_count;
134
135   /** VkSubpassDescription2::pInputAttachments */
136   struct vk_subpass_attachment *input_attachments;
137
138   /** VkSubpassDescription2::colorAttachmentCount */
139   uint32_t color_count;
140
141   /** VkSubpassDescription2::pColorAttachments */
142   struct vk_subpass_attachment *color_attachments;
143
144   /** VkSubpassDescription2::colorAttachmentCount or zero */
145   uint32_t color_resolve_count;
146
147   /** VkSubpassDescription2::pResolveAttachments */
148   struct vk_subpass_attachment *color_resolve_attachments;
149
150   /** VkSubpassDescription2::pDepthStencilAttachment */
151   struct vk_subpass_attachment *depth_stencil_attachment;
152
153   /** VkSubpassDescriptionDepthStencilResolve::pDepthStencilResolveAttachment */
154   struct vk_subpass_attachment *depth_stencil_resolve_attachment;
155
156   /** VkFragmentShadingRateAttachmentInfoKHR::pFragmentShadingRateAttachment */
157   struct vk_subpass_attachment *fragment_shading_rate_attachment;
158
159   /** VkSubpassDescription2::viewMask or 1 for non-multiview
160    *
161    * For all view masks in the vk_render_pass data structure, we use a mask
162    * of 1 for non-multiview instead of a mask of 0.  To determine if the
163    * render pass is multiview or not, see vk_render_pass::is_multiview.
164    */
165   uint32_t view_mask;
166
167   /** VkSubpassDescriptionDepthStencilResolve::depthResolveMode */
168   VkResolveModeFlagBits depth_resolve_mode;
169
170   /** VkSubpassDescriptionDepthStencilResolve::stencilResolveMode */
171   VkResolveModeFlagBits stencil_resolve_mode;
172
173   /** VkFragmentShadingRateAttachmentInfoKHR::shadingRateAttachmentTexelSize */
174   VkExtent2D fragment_shading_rate_attachment_texel_size;
175
176   /** VkRenderingSelfDependencyInfoMESA for this subpass
177    *
178    * This is in the pNext chain of pipeline_info and inheritance_info.
179    */
180   VkRenderingSelfDependencyInfoMESA self_dep_info;
181
182   /** VkAttachmentSampleCountInfoAMD for this subpass
183    *
184    * This is in the pNext chain of pipeline_info and inheritance_info.
185    */
186   VkAttachmentSampleCountInfoAMD sample_count_info_amd;
187
188   /** VkPipelineRenderingCreateInfo for this subpass
189    *
190    * Returned by vk_get_pipeline_rendering_create_info() if
191    * VkGraphicsPipelineCreateInfo::renderPass != VK_NULL_HANDLE.
192    */
193   VkPipelineRenderingCreateInfo pipeline_info;
194
195   /** VkCommandBufferInheritanceRenderingInfo for this subpass
196    *
197    * Returned by vk_get_command_buffer_inheritance_rendering_info() if
198    * VkCommandBufferInheritanceInfo::renderPass != VK_NULL_HANDLE.
199    */
200   VkCommandBufferInheritanceRenderingInfo inheritance_info;
201
202   /** VkMultisampledRenderToSingleSampledInfoEXT for this subpass */
203   VkMultisampledRenderToSingleSampledInfoEXT mrtss;
204};
205
206struct vk_render_pass_attachment {
207   /** VkAttachmentDescription2::format */
208   VkFormat format;
209
210   /** Aspects contained in format */
211   VkImageAspectFlags aspects;
212
213   /** VkAttachmentDescription2::samples */
214   uint32_t samples;
215
216   /** Views in which this attachment is used, 0 for unused
217    *
218    * For non-multiview, this will be 1 if the attachment is used.
219    */
220   uint32_t view_mask;
221
222   /** VkAttachmentDescription2::loadOp */
223   VkAttachmentLoadOp load_op;
224
225   /** VkAttachmentDescription2::storeOp */
226   VkAttachmentStoreOp store_op;
227
228   /** VkAttachmentDescription2::stencilLoadOp */
229   VkAttachmentLoadOp stencil_load_op;
230
231   /** VkAttachmentDescription2::stencilStoreOp */
232   VkAttachmentStoreOp stencil_store_op;
233
234   /** VkAttachmentDescription2::initialLayout */
235   VkImageLayout initial_layout;
236
237   /** VkAttachmentDescription2::finalLayout */
238   VkImageLayout final_layout;
239
240   /** VkAttachmentDescriptionStencilLayout::stencilInitialLayout
241    *
242    * If VK_KHR_separate_depth_stencil_layouts is not used, this will be
243    * initial_layout if format contains stencil and VK_IMAGE_LAYOUT_UNDEFINED
244    * otherwise.
245    */
246   VkImageLayout initial_stencil_layout;
247
248   /** VkAttachmentDescriptionStencilLayout::stencilFinalLayout
249    *
250    * If VK_KHR_separate_depth_stencil_layouts is not used, this will be
251    * final_layout if format contains stencil and VK_IMAGE_LAYOUT_UNDEFINED
252    * otherwise.
253    */
254   VkImageLayout final_stencil_layout;
255};
256
257struct vk_subpass_dependency {
258   /** VkSubpassDependency2::dependencyFlags */
259   VkDependencyFlags flags;
260
261   /** VkSubpassDependency2::srcSubpass */
262   uint32_t src_subpass;
263
264   /** VkSubpassDependency2::dstSubpass */
265   uint32_t dst_subpass;
266
267   /** VkSubpassDependency2::srcStageMask */
268   VkPipelineStageFlags2 src_stage_mask;
269
270   /** VkSubpassDependency2::dstStageMask */
271   VkPipelineStageFlags2 dst_stage_mask;
272
273   /** VkSubpassDependency2::srcAccessMask */
274   VkAccessFlags2 src_access_mask;
275
276   /** VkSubpassDependency2::dstAccessMask */
277   VkAccessFlags2 dst_access_mask;
278
279   /** VkSubpassDependency2::viewOffset */
280   int32_t view_offset;
281};
282
283struct vk_render_pass {
284   struct vk_object_base base;
285
286   /** True if this render pass uses multiview
287    *
288    * This is true if all subpasses have viewMask != 0.
289    */
290   bool is_multiview;
291
292   /** Views used by this render pass or 1 for non-multiview */
293   uint32_t view_mask;
294
295   /** VkRenderPassCreateInfo2::attachmentCount */
296   uint32_t attachment_count;
297
298   /** VkRenderPassCreateInfo2::pAttachments */
299   struct vk_render_pass_attachment *attachments;
300
301   /** VkRenderPassCreateInfo2::subpassCount */
302   uint32_t subpass_count;
303
304   /** VkRenderPassCreateInfo2::subpasses */
305   struct vk_subpass *subpasses;
306
307   /** VkRenderPassCreateInfo2::dependencyCount */
308   uint32_t dependency_count;
309
310   /** VkRenderPassCreateInfo2::pDependencies */
311   struct vk_subpass_dependency *dependencies;
312};
313
314VK_DEFINE_NONDISP_HANDLE_CASTS(vk_render_pass, base, VkRenderPass,
315                               VK_OBJECT_TYPE_RENDER_PASS);
316
317/** Returns the VkPipelineRenderingCreateInfo for a graphics pipeline
318 *
319 * For render-pass-free drivers, this can be used in the implementation of
320 * vkCreateGraphicsPipelines to get the VkPipelineRenderingCreateInfo.  If
321 * VkGraphicsPipelineCreateInfo::renderPass is not VK_NULL_HANDLE, it will
322 * return a representation of the specified subpass as a
323 * VkPipelineRenderingCreateInfo.  If VkGraphicsPipelineCreateInfo::renderPass
324 * is VK_NULL_HANDLE and there is a VkPipelineRenderingCreateInfo in the pNext
325 * chain of VkGraphicsPipelineCreateInfo, it will return that.
326 *
327 * @param[in]  info  One of the pCreateInfos from vkCreateGraphicsPipelines
328 */
329const VkPipelineRenderingCreateInfo *
330vk_get_pipeline_rendering_create_info(const VkGraphicsPipelineCreateInfo *info);
331
332/** Returns the VkAttachmentSampleCountInfoAMD for a graphics pipeline
333 *
334 * For render-pass-free drivers, this can be used in the implementaiton of
335 * vkCreateGraphicsPipelines to get the VkAttachmentSampleCountInfoAMD.  If
336 * VkGraphicsPipelineCreateInfo::renderPass is not VK_NULL_HANDLE, it will
337 * return the sample counts from the specified subpass as a
338 * VkAttachmentSampleCountInfoAMD.  If VkGraphicsPipelineCreateInfo::renderPass
339 * is VK_NULL_HANDLE and there is a VkAttachmentSampleCountInfoAMD in the pNext
340 * chain of VkGraphicsPipelineCreateInfo, it will return that.
341 *
342 * @param[in]  info  One of the pCreateInfos from vkCreateGraphicsPipelines
343 */
344const VkAttachmentSampleCountInfoAMD *
345vk_get_pipeline_sample_count_info_amd(const VkGraphicsPipelineCreateInfo *info);
346
347/**
348 * Returns the VkCommandBufferInheritanceRenderingInfo for secondary command
349 * buffer execution
350 *
351 * For render-pass-free drivers, this can be used in the implementation of
352 * vkCmdExecuteCommands to get the VkCommandBufferInheritanceRenderingInfo.
353 * If VkCommandBufferInheritanceInfo::renderPass is not VK_NULL_HANDLE, it
354 * will return a representation of the specified subpass as a
355 * VkCommandBufferInheritanceRenderingInfo.  If
356 * VkCommandBufferInheritanceInfo::renderPass is not VK_NULL_HANDLE and there
357 * is a VkCommandBufferInheritanceRenderingInfo in the pNext chain of
358 * VkCommandBufferBeginInfo, it will return that.
359 *
360 * @param[in]  level       The nesting level of this command buffer
361 * @param[in]  pBeginInfo  The pBeginInfo from vkBeginCommandBuffer
362 */
363const VkCommandBufferInheritanceRenderingInfo *
364vk_get_command_buffer_inheritance_rendering_info(
365   VkCommandBufferLevel level,
366   const VkCommandBufferBeginInfo *pBeginInfo);
367
368struct vk_gcbiarr_data {
369   VkRenderingInfo rendering;
370   VkRenderingFragmentShadingRateAttachmentInfoKHR fsr_att;
371   VkRenderingAttachmentInfo attachments[];
372};
373
374#define VK_GCBIARR_DATA_SIZE(max_color_rts) (\
375   sizeof(struct vk_gcbiarr_data) + \
376   sizeof(VkRenderingAttachmentInfo) * ((max_color_rts) + 2) \
377)
378
379/**
380 * Constructs a VkRenderingInfo for the inheritance rendering info
381 *
382 * For render-pass-free drivers, this can be used in the implementaiton of
383 * vkCmdExecuteCommands to get a VkRenderingInfo representing the subpass and
384 * framebuffer provided via the inheritance info for a command buffer created
385 * with VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT.  The mental model
386 * here is that VkExecuteCommands() implicitly suspends the render pass and
387 * VkBeginCommandBuffer() resumes it.  If a VkRenderingInfo cannot be
388 * constructed due to a missing framebuffer or similar, NULL will be
389 * returned.
390 *
391 * @param[in]  level       The nesting level of this command buffer
392 * @param[in]  pBeginInfo  The pBeginInfo from vkBeginCommandBuffer
393 * @param[out] stack_data  An opaque blob of data which will be overwritten by
394 *                         this function, passed in from the caller to avoid
395 *                         heap allocations.  It must be at least
396 *                         VK_GCBIARR_DATA_SIZE(max_color_rts) bytes.
397 */
398const VkRenderingInfo *
399vk_get_command_buffer_inheritance_as_rendering_resume(
400   VkCommandBufferLevel level,
401   const VkCommandBufferBeginInfo *pBeginInfo,
402   void *stack_data);
403
404#ifdef __cplusplus
405}
406#endif
407
408#endif /* VK_RENDER_PASS_H */
409