17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2021 Collabora Ltd.
37ec681f3Smrg *
47ec681f3Smrg * Derived from tu_pass.c which is:
57ec681f3Smrg * Copyright © 2016 Red Hat.
67ec681f3Smrg * Copyright © 2016 Bas Nieuwenhuizen
77ec681f3Smrg * Copyright © 2015 Intel Corporation
87ec681f3Smrg *
97ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
107ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
117ec681f3Smrg * to deal in the Software without restriction, including without limitation
127ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
137ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
147ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
157ec681f3Smrg *
167ec681f3Smrg * The above copyright notice and this permission notice (including the next
177ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
187ec681f3Smrg * Software.
197ec681f3Smrg *
207ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
217ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
227ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
237ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
247ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
257ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
267ec681f3Smrg * DEALINGS IN THE SOFTWARE.
277ec681f3Smrg */
287ec681f3Smrg#include "panvk_private.h"
297ec681f3Smrg
307ec681f3Smrg#include "vk_format.h"
317ec681f3Smrg#include "vk_util.h"
327ec681f3Smrg
337ec681f3SmrgVkResult
347ec681f3Smrgpanvk_CreateRenderPass2(VkDevice _device,
357ec681f3Smrg                        const VkRenderPassCreateInfo2 *pCreateInfo,
367ec681f3Smrg                        const VkAllocationCallbacks *pAllocator,
377ec681f3Smrg                        VkRenderPass *pRenderPass)
387ec681f3Smrg{
397ec681f3Smrg   VK_FROM_HANDLE(panvk_device, device, _device);
407ec681f3Smrg   struct panvk_render_pass *pass;
417ec681f3Smrg   size_t size;
427ec681f3Smrg   size_t attachments_offset;
437ec681f3Smrg   VkRenderPassMultiviewCreateInfo *multiview_info = NULL;
447ec681f3Smrg
457ec681f3Smrg   assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2);
467ec681f3Smrg
477ec681f3Smrg   size = sizeof(*pass);
487ec681f3Smrg   size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
497ec681f3Smrg   attachments_offset = size;
507ec681f3Smrg   size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
517ec681f3Smrg
527ec681f3Smrg   pass = vk_object_zalloc(&device->vk, pAllocator, size,
537ec681f3Smrg                           VK_OBJECT_TYPE_RENDER_PASS);
547ec681f3Smrg   if (pass == NULL)
557ec681f3Smrg      return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
567ec681f3Smrg
577ec681f3Smrg   pass->attachment_count = pCreateInfo->attachmentCount;
587ec681f3Smrg   pass->subpass_count = pCreateInfo->subpassCount;
597ec681f3Smrg   pass->attachments = (void *) pass + attachments_offset;
607ec681f3Smrg
617ec681f3Smrg   vk_foreach_struct(ext, pCreateInfo->pNext) {
627ec681f3Smrg      switch (ext->sType) {
637ec681f3Smrg      case VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO:
647ec681f3Smrg         multiview_info = (VkRenderPassMultiviewCreateInfo *) ext;
657ec681f3Smrg         break;
667ec681f3Smrg      default:
677ec681f3Smrg         break;
687ec681f3Smrg      }
697ec681f3Smrg   }
707ec681f3Smrg
717ec681f3Smrg   for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
727ec681f3Smrg      struct panvk_render_pass_attachment *att = &pass->attachments[i];
737ec681f3Smrg
747ec681f3Smrg      att->format = vk_format_to_pipe_format(pCreateInfo->pAttachments[i].format);
757ec681f3Smrg      att->samples = pCreateInfo->pAttachments[i].samples;
767ec681f3Smrg      att->load_op = pCreateInfo->pAttachments[i].loadOp;
777ec681f3Smrg      att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
787ec681f3Smrg      att->initial_layout = pCreateInfo->pAttachments[i].initialLayout;
797ec681f3Smrg      att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
807ec681f3Smrg      att->store_op = pCreateInfo->pAttachments[i].storeOp;
817ec681f3Smrg      att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
827ec681f3Smrg      att->first_used_in_subpass = ~0;
837ec681f3Smrg   }
847ec681f3Smrg
857ec681f3Smrg   uint32_t subpass_attachment_count = 0;
867ec681f3Smrg   struct panvk_subpass_attachment *p;
877ec681f3Smrg   for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
887ec681f3Smrg      const VkSubpassDescription2 *desc = &pCreateInfo->pSubpasses[i];
897ec681f3Smrg
907ec681f3Smrg      subpass_attachment_count +=
917ec681f3Smrg         desc->inputAttachmentCount + desc->colorAttachmentCount +
927ec681f3Smrg         (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
937ec681f3Smrg         (desc->pDepthStencilAttachment != NULL);
947ec681f3Smrg   }
957ec681f3Smrg
967ec681f3Smrg   if (subpass_attachment_count) {
977ec681f3Smrg      pass->subpass_attachments =
987ec681f3Smrg         vk_alloc2(&device->vk.alloc, pAllocator,
997ec681f3Smrg                   subpass_attachment_count *
1007ec681f3Smrg                   sizeof(struct panvk_subpass_attachment),
1017ec681f3Smrg                   8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1027ec681f3Smrg      if (pass->subpass_attachments == NULL) {
1037ec681f3Smrg         vk_object_free(&device->vk, pAllocator, pass);
1047ec681f3Smrg         return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
1057ec681f3Smrg      }
1067ec681f3Smrg   }
1077ec681f3Smrg
1087ec681f3Smrg   p = pass->subpass_attachments;
1097ec681f3Smrg   for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
1107ec681f3Smrg      const VkSubpassDescription2 *desc = &pCreateInfo->pSubpasses[i];
1117ec681f3Smrg      struct panvk_subpass *subpass = &pass->subpasses[i];
1127ec681f3Smrg
1137ec681f3Smrg      subpass->input_count = desc->inputAttachmentCount;
1147ec681f3Smrg      subpass->color_count = desc->colorAttachmentCount;
1157ec681f3Smrg      if (multiview_info)
1167ec681f3Smrg         subpass->view_mask = multiview_info->pViewMasks[i];
1177ec681f3Smrg
1187ec681f3Smrg      if (desc->inputAttachmentCount > 0) {
1197ec681f3Smrg         subpass->input_attachments = p;
1207ec681f3Smrg         p += desc->inputAttachmentCount;
1217ec681f3Smrg
1227ec681f3Smrg         for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
1237ec681f3Smrg            subpass->input_attachments[j] = (struct panvk_subpass_attachment) {
1247ec681f3Smrg               .idx = desc->pInputAttachments[j].attachment,
1257ec681f3Smrg               .layout = desc->pInputAttachments[j].layout,
1267ec681f3Smrg            };
1277ec681f3Smrg            if (desc->pInputAttachments[j].attachment != VK_ATTACHMENT_UNUSED)
1287ec681f3Smrg               pass->attachments[desc->pInputAttachments[j].attachment]
1297ec681f3Smrg                  .view_mask |= subpass->view_mask;
1307ec681f3Smrg         }
1317ec681f3Smrg      }
1327ec681f3Smrg
1337ec681f3Smrg      if (desc->colorAttachmentCount > 0) {
1347ec681f3Smrg         subpass->color_attachments = p;
1357ec681f3Smrg         p += desc->colorAttachmentCount;
1367ec681f3Smrg
1377ec681f3Smrg         for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
1387ec681f3Smrg            uint32_t idx = desc->pColorAttachments[j].attachment;
1397ec681f3Smrg
1407ec681f3Smrg            subpass->color_attachments[j] = (struct panvk_subpass_attachment) {
1417ec681f3Smrg               .idx = idx,
1427ec681f3Smrg               .layout = desc->pColorAttachments[j].layout,
1437ec681f3Smrg            };
1447ec681f3Smrg
1457ec681f3Smrg            if (idx != VK_ATTACHMENT_UNUSED) {
1467ec681f3Smrg               pass->attachments[idx].view_mask |= subpass->view_mask;
1477ec681f3Smrg               if (pass->attachments[idx].first_used_in_subpass == ~0) {
1487ec681f3Smrg                  pass->attachments[idx].first_used_in_subpass = i;
1497ec681f3Smrg                  if (pass->attachments[idx].load_op == VK_ATTACHMENT_LOAD_OP_CLEAR)
1507ec681f3Smrg                     subpass->color_attachments[j].clear = true;
1517ec681f3Smrg                  else if (pass->attachments[idx].load_op == VK_ATTACHMENT_LOAD_OP_LOAD)
1527ec681f3Smrg                     subpass->color_attachments[j].preload = true;
1537ec681f3Smrg               } else {
1547ec681f3Smrg                  subpass->color_attachments[j].preload = true;
1557ec681f3Smrg               }
1567ec681f3Smrg            }
1577ec681f3Smrg         }
1587ec681f3Smrg      }
1597ec681f3Smrg
1607ec681f3Smrg      if (desc->pResolveAttachments) {
1617ec681f3Smrg         subpass->resolve_attachments = p;
1627ec681f3Smrg         p += desc->colorAttachmentCount;
1637ec681f3Smrg
1647ec681f3Smrg         for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
1657ec681f3Smrg            uint32_t idx = desc->pResolveAttachments[j].attachment;
1667ec681f3Smrg
1677ec681f3Smrg            subpass->resolve_attachments[j] = (struct panvk_subpass_attachment) {
1687ec681f3Smrg               .idx = idx,
1697ec681f3Smrg               .layout = desc->pResolveAttachments[j].layout,
1707ec681f3Smrg            };
1717ec681f3Smrg
1727ec681f3Smrg            if (idx != VK_ATTACHMENT_UNUSED)
1737ec681f3Smrg               pass->attachments[idx].view_mask |= subpass->view_mask;
1747ec681f3Smrg         }
1757ec681f3Smrg      }
1767ec681f3Smrg
1777ec681f3Smrg      unsigned idx = desc->pDepthStencilAttachment ?
1787ec681f3Smrg                     desc->pDepthStencilAttachment->attachment :
1797ec681f3Smrg                     VK_ATTACHMENT_UNUSED;
1807ec681f3Smrg      subpass->zs_attachment.idx = idx;
1817ec681f3Smrg      if (idx != VK_ATTACHMENT_UNUSED) {
1827ec681f3Smrg         subpass->zs_attachment.layout = desc->pDepthStencilAttachment->layout;
1837ec681f3Smrg         pass->attachments[idx].view_mask |= subpass->view_mask;
1847ec681f3Smrg
1857ec681f3Smrg         if (pass->attachments[idx].first_used_in_subpass == ~0) {
1867ec681f3Smrg            pass->attachments[idx].first_used_in_subpass = i;
1877ec681f3Smrg            if (pass->attachments[idx].load_op == VK_ATTACHMENT_LOAD_OP_CLEAR)
1887ec681f3Smrg               subpass->zs_attachment.clear = true;
1897ec681f3Smrg            else if (pass->attachments[idx].load_op == VK_ATTACHMENT_LOAD_OP_LOAD)
1907ec681f3Smrg               subpass->zs_attachment.preload = true;
1917ec681f3Smrg         } else {
1927ec681f3Smrg            subpass->zs_attachment.preload = true;
1937ec681f3Smrg         }
1947ec681f3Smrg      }
1957ec681f3Smrg   }
1967ec681f3Smrg
1977ec681f3Smrg   *pRenderPass = panvk_render_pass_to_handle(pass);
1987ec681f3Smrg   return VK_SUCCESS;
1997ec681f3Smrg}
2007ec681f3Smrg
2017ec681f3Smrgvoid
2027ec681f3Smrgpanvk_DestroyRenderPass(VkDevice _device,
2037ec681f3Smrg                        VkRenderPass _pass,
2047ec681f3Smrg                        const VkAllocationCallbacks *pAllocator)
2057ec681f3Smrg{
2067ec681f3Smrg   VK_FROM_HANDLE(panvk_device, device, _device);
2077ec681f3Smrg   VK_FROM_HANDLE(panvk_render_pass, pass, _pass);
2087ec681f3Smrg
2097ec681f3Smrg   if (!pass)
2107ec681f3Smrg      return;
2117ec681f3Smrg
2127ec681f3Smrg   vk_free2(&device->vk.alloc, pAllocator, pass->subpass_attachments);
2137ec681f3Smrg   vk_object_free(&device->vk, pAllocator, pass);
2147ec681f3Smrg}
2157ec681f3Smrg
2167ec681f3Smrgvoid
2177ec681f3Smrgpanvk_GetRenderAreaGranularity(VkDevice _device,
2187ec681f3Smrg                               VkRenderPass renderPass,
2197ec681f3Smrg                               VkExtent2D *pGranularity)
2207ec681f3Smrg{
2217ec681f3Smrg   panvk_stub();
2227ec681f3Smrg}
223