1 /*
2  * Copyright © 2019 Red Hat.
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 #include "lvp_private.h"
25 
26 #include "vk_util.h"
27 
28 static void
lvp_render_pass_compile(struct lvp_render_pass * pass)29 lvp_render_pass_compile(struct lvp_render_pass *pass)
30 {
31    for (uint32_t i = 0; i < pass->subpass_count; i++) {
32       struct lvp_subpass *subpass = &pass->subpasses[i];
33 
34       for (uint32_t j = 0; j < subpass->attachment_count; j++) {
35          struct lvp_subpass_attachment *subpass_att =
36             &subpass->attachments[j];
37          if (subpass_att->attachment == VK_ATTACHMENT_UNUSED)
38             continue;
39 
40          struct lvp_render_pass_attachment *pass_att =
41             &pass->attachments[subpass_att->attachment];
42 
43          pass_att->first_subpass_idx = UINT32_MAX;
44       }
45    }
46 
47    for (uint32_t i = 0; i < pass->subpass_count; i++) {
48       struct lvp_subpass *subpass = &pass->subpasses[i];
49       uint32_t color_sample_count = 1, depth_sample_count = 1;
50 
51       /* We don't allow depth_stencil_attachment to be non-NULL and
52        * be VK_ATTACHMENT_UNUSED.  This way something can just check
53        * for NULL and be guaranteed that they have a valid
54        * attachment.
55        */
56       if (subpass->depth_stencil_attachment &&
57           subpass->depth_stencil_attachment->attachment == VK_ATTACHMENT_UNUSED)
58          subpass->depth_stencil_attachment = NULL;
59 
60       if (subpass->ds_resolve_attachment &&
61           subpass->ds_resolve_attachment->attachment == VK_ATTACHMENT_UNUSED)
62          subpass->ds_resolve_attachment = NULL;
63 
64       for (uint32_t j = 0; j < subpass->attachment_count; j++) {
65          struct lvp_subpass_attachment *subpass_att =
66             &subpass->attachments[j];
67          if (subpass_att->attachment == VK_ATTACHMENT_UNUSED)
68             continue;
69 
70          struct lvp_render_pass_attachment *pass_att =
71             &pass->attachments[subpass_att->attachment];
72 
73          if (i < pass_att->first_subpass_idx)
74             pass_att->first_subpass_idx = i;
75          pass_att->last_subpass_idx = i;
76       }
77 
78       subpass->has_color_att = false;
79       for (uint32_t j = 0; j < subpass->color_count; j++) {
80          struct lvp_subpass_attachment *subpass_att =
81             &subpass->color_attachments[j];
82          if (subpass_att->attachment == VK_ATTACHMENT_UNUSED)
83             continue;
84 
85          subpass->has_color_att = true;
86 
87          struct lvp_render_pass_attachment *pass_att =
88             &pass->attachments[subpass_att->attachment];
89 
90          color_sample_count = pass_att->samples;
91       }
92 
93       if (subpass->depth_stencil_attachment) {
94          const uint32_t a =
95             subpass->depth_stencil_attachment->attachment;
96          struct lvp_render_pass_attachment *pass_att =
97             &pass->attachments[a];
98          depth_sample_count = pass_att->samples;
99       }
100 
101       subpass->max_sample_count = MAX2(color_sample_count,
102                                        depth_sample_count);
103 
104       /* We have to handle resolve attachments specially */
105       subpass->has_color_resolve = false;
106       if (subpass->resolve_attachments) {
107          for (uint32_t j = 0; j < subpass->color_count; j++) {
108             struct lvp_subpass_attachment *resolve_att =
109                &subpass->resolve_attachments[j];
110 
111             if (resolve_att->attachment == VK_ATTACHMENT_UNUSED)
112                continue;
113 
114             subpass->has_color_resolve = true;
115          }
116       }
117 
118       for (uint32_t j = 0; j < subpass->input_count; ++j) {
119          if (subpass->input_attachments[j].attachment == VK_ATTACHMENT_UNUSED)
120             continue;
121 
122          for (uint32_t k = 0; k < subpass->color_count; ++k) {
123             if (subpass->color_attachments[k].attachment == subpass->input_attachments[j].attachment) {
124                subpass->input_attachments[j].in_render_loop = true;
125                subpass->color_attachments[k].in_render_loop = true;
126             }
127          }
128 
129          if (subpass->depth_stencil_attachment &&
130              subpass->depth_stencil_attachment->attachment == subpass->input_attachments[j].attachment) {
131             subpass->input_attachments[j].in_render_loop = true;
132             subpass->depth_stencil_attachment->in_render_loop = true;
133          }
134       }
135    }
136 }
137 
138 static unsigned
lvp_num_subpass_attachments2(const VkSubpassDescription2 * desc)139 lvp_num_subpass_attachments2(const VkSubpassDescription2 *desc)
140 {
141    const VkSubpassDescriptionDepthStencilResolve *ds_resolve =
142       vk_find_struct_const(desc->pNext,
143                            SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE);
144    return desc->inputAttachmentCount +
145       desc->colorAttachmentCount +
146       (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
147       (desc->pDepthStencilAttachment != NULL) +
148       (ds_resolve && ds_resolve->pDepthStencilResolveAttachment);
149 }
150 
lvp_CreateRenderPass2(VkDevice _device,const VkRenderPassCreateInfo2 * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkRenderPass * pRenderPass)151 VKAPI_ATTR VkResult VKAPI_CALL lvp_CreateRenderPass2(
152     VkDevice                                    _device,
153     const VkRenderPassCreateInfo2*              pCreateInfo,
154     const VkAllocationCallbacks*                pAllocator,
155     VkRenderPass*                               pRenderPass)
156 {
157    LVP_FROM_HANDLE(lvp_device, device, _device);
158    struct lvp_render_pass *pass;
159    size_t attachments_offset;
160    size_t size;
161 
162    size = sizeof(*pass);
163    size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
164    attachments_offset = size;
165    size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
166 
167    pass = vk_alloc2(&device->vk.alloc, pAllocator, size, 8,
168                     VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
169    if (pass == NULL)
170       return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
171 
172    /* Clear the subpasses along with the parent pass. This required because
173     * each array member of lvp_subpass must be a valid pointer if not NULL.
174     */
175    memset(pass, 0, size);
176 
177    vk_object_base_init(&device->vk, &pass->base,
178                        VK_OBJECT_TYPE_RENDER_PASS);
179    pass->attachment_count = pCreateInfo->attachmentCount;
180    pass->subpass_count = pCreateInfo->subpassCount;
181    pass->attachments = (struct lvp_render_pass_attachment *)((char *)pass + attachments_offset);
182 
183    for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
184       struct lvp_render_pass_attachment *att = &pass->attachments[i];
185 
186       att->format = pCreateInfo->pAttachments[i].format;
187       att->samples = pCreateInfo->pAttachments[i].samples;
188       att->load_op = pCreateInfo->pAttachments[i].loadOp;
189       att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
190       att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
191       att->first_subpass_idx = UINT32_MAX;
192 
193       bool is_zs = util_format_is_depth_or_stencil(lvp_vk_format_to_pipe_format(att->format));
194       pass->has_zs_attachment |= is_zs;
195       pass->has_color_attachment |= !is_zs;
196    }
197    uint32_t subpass_attachment_count = 0;
198    for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
199       subpass_attachment_count += lvp_num_subpass_attachments2(&pCreateInfo->pSubpasses[i]);
200    }
201 
202    if (subpass_attachment_count) {
203       pass->subpass_attachments =
204          vk_alloc2(&device->vk.alloc, pAllocator,
205                    subpass_attachment_count * sizeof(struct lvp_subpass_attachment), 8,
206                    VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
207       if (pass->subpass_attachments == NULL) {
208          vk_free2(&device->vk.alloc, pAllocator, pass);
209          return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
210       }
211    } else
212       pass->subpass_attachments = NULL;
213 
214    struct lvp_subpass_attachment *p = pass->subpass_attachments;
215    for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
216       const VkSubpassDescription2 *desc = &pCreateInfo->pSubpasses[i];
217       struct lvp_subpass *subpass = &pass->subpasses[i];
218 
219       subpass->input_count = desc->inputAttachmentCount;
220       subpass->color_count = desc->colorAttachmentCount;
221       subpass->attachment_count = lvp_num_subpass_attachments2(desc);
222       subpass->attachments = p;
223       subpass->view_mask = desc->viewMask;
224 
225       if (desc->inputAttachmentCount > 0) {
226          subpass->input_attachments = p;
227          p += desc->inputAttachmentCount;
228 
229          for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
230             subpass->input_attachments[j] = (struct lvp_subpass_attachment) {
231                .attachment = desc->pInputAttachments[j].attachment,
232                .layout = desc->pInputAttachments[j].layout,
233             };
234          }
235       }
236 
237       if (desc->colorAttachmentCount > 0) {
238          subpass->color_attachments = p;
239          p += desc->colorAttachmentCount;
240 
241          for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
242             subpass->color_attachments[j] = (struct lvp_subpass_attachment) {
243                .attachment = desc->pColorAttachments[j].attachment,
244                .layout = desc->pColorAttachments[j].layout,
245             };
246          }
247       }
248 
249       if (desc->pResolveAttachments) {
250          subpass->resolve_attachments = p;
251          p += desc->colorAttachmentCount;
252 
253          for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
254             subpass->resolve_attachments[j] = (struct lvp_subpass_attachment) {
255                .attachment = desc->pResolveAttachments[j].attachment,
256                .layout = desc->pResolveAttachments[j].layout,
257             };
258          }
259       }
260 
261       if (desc->pDepthStencilAttachment) {
262          subpass->depth_stencil_attachment = p++;
263 
264          *subpass->depth_stencil_attachment = (struct lvp_subpass_attachment) {
265             .attachment = desc->pDepthStencilAttachment->attachment,
266             .layout = desc->pDepthStencilAttachment->layout,
267          };
268       }
269 
270       const VkSubpassDescriptionDepthStencilResolve *ds_resolve =
271          vk_find_struct_const(desc->pNext, SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE);
272 
273       if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment) {
274          subpass->ds_resolve_attachment = p++;
275 
276          *subpass->ds_resolve_attachment = (struct lvp_subpass_attachment){
277             .attachment = ds_resolve->pDepthStencilResolveAttachment->attachment,
278             .layout = ds_resolve->pDepthStencilResolveAttachment->layout,
279          };
280 
281          subpass->depth_resolve_mode = ds_resolve->depthResolveMode;
282          subpass->stencil_resolve_mode = ds_resolve->stencilResolveMode;
283       }
284    }
285 
286    lvp_render_pass_compile(pass);
287    *pRenderPass = lvp_render_pass_to_handle(pass);
288 
289    return VK_SUCCESS;
290 }
291 
lvp_DestroyRenderPass(VkDevice _device,VkRenderPass _pass,const VkAllocationCallbacks * pAllocator)292 VKAPI_ATTR void VKAPI_CALL lvp_DestroyRenderPass(
293    VkDevice                                    _device,
294    VkRenderPass                                _pass,
295    const VkAllocationCallbacks*                pAllocator)
296 {
297    LVP_FROM_HANDLE(lvp_device, device, _device);
298    LVP_FROM_HANDLE(lvp_render_pass, pass, _pass);
299 
300    if (!_pass)
301       return;
302    vk_object_base_finish(&pass->base);
303    vk_free2(&device->vk.alloc, pAllocator, pass->subpass_attachments);
304    vk_free2(&device->vk.alloc, pAllocator, pass);
305 }
306 
lvp_GetRenderAreaGranularity(VkDevice device,VkRenderPass renderPass,VkExtent2D * pGranularity)307 VKAPI_ATTR void VKAPI_CALL lvp_GetRenderAreaGranularity(
308    VkDevice                                    device,
309    VkRenderPass                                renderPass,
310    VkExtent2D*                                 pGranularity)
311 {
312    *pGranularity = (VkExtent2D) { 1, 1 };
313 }
314