17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2019 Red Hat.
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#include "lvp_private.h"
257ec681f3Smrg
267ec681f3Smrg#include "vk_util.h"
277ec681f3Smrg
287ec681f3Smrgstatic void
297ec681f3Smrglvp_render_pass_compile(struct lvp_render_pass *pass)
307ec681f3Smrg{
317ec681f3Smrg   for (uint32_t i = 0; i < pass->subpass_count; i++) {
327ec681f3Smrg      struct lvp_subpass *subpass = &pass->subpasses[i];
337ec681f3Smrg
347ec681f3Smrg      for (uint32_t j = 0; j < subpass->attachment_count; j++) {
357ec681f3Smrg         struct lvp_subpass_attachment *subpass_att =
367ec681f3Smrg            &subpass->attachments[j];
377ec681f3Smrg         if (subpass_att->attachment == VK_ATTACHMENT_UNUSED)
387ec681f3Smrg            continue;
397ec681f3Smrg
407ec681f3Smrg         struct lvp_render_pass_attachment *pass_att =
417ec681f3Smrg            &pass->attachments[subpass_att->attachment];
427ec681f3Smrg
437ec681f3Smrg         pass_att->first_subpass_idx = UINT32_MAX;
447ec681f3Smrg      }
457ec681f3Smrg   }
467ec681f3Smrg
477ec681f3Smrg   for (uint32_t i = 0; i < pass->subpass_count; i++) {
487ec681f3Smrg      struct lvp_subpass *subpass = &pass->subpasses[i];
497ec681f3Smrg      uint32_t color_sample_count = 1, depth_sample_count = 1;
507ec681f3Smrg
517ec681f3Smrg      /* We don't allow depth_stencil_attachment to be non-NULL and
527ec681f3Smrg       * be VK_ATTACHMENT_UNUSED.  This way something can just check
537ec681f3Smrg       * for NULL and be guaranteed that they have a valid
547ec681f3Smrg       * attachment.
557ec681f3Smrg       */
567ec681f3Smrg      if (subpass->depth_stencil_attachment &&
577ec681f3Smrg          subpass->depth_stencil_attachment->attachment == VK_ATTACHMENT_UNUSED)
587ec681f3Smrg         subpass->depth_stencil_attachment = NULL;
597ec681f3Smrg
607ec681f3Smrg      if (subpass->ds_resolve_attachment &&
617ec681f3Smrg          subpass->ds_resolve_attachment->attachment == VK_ATTACHMENT_UNUSED)
627ec681f3Smrg         subpass->ds_resolve_attachment = NULL;
637ec681f3Smrg
647ec681f3Smrg      for (uint32_t j = 0; j < subpass->attachment_count; j++) {
657ec681f3Smrg         struct lvp_subpass_attachment *subpass_att =
667ec681f3Smrg            &subpass->attachments[j];
677ec681f3Smrg         if (subpass_att->attachment == VK_ATTACHMENT_UNUSED)
687ec681f3Smrg            continue;
697ec681f3Smrg
707ec681f3Smrg         struct lvp_render_pass_attachment *pass_att =
717ec681f3Smrg            &pass->attachments[subpass_att->attachment];
727ec681f3Smrg
737ec681f3Smrg         if (i < pass_att->first_subpass_idx)
747ec681f3Smrg            pass_att->first_subpass_idx = i;
757ec681f3Smrg         pass_att->last_subpass_idx = i;
767ec681f3Smrg      }
777ec681f3Smrg
787ec681f3Smrg      subpass->has_color_att = false;
797ec681f3Smrg      for (uint32_t j = 0; j < subpass->color_count; j++) {
807ec681f3Smrg         struct lvp_subpass_attachment *subpass_att =
817ec681f3Smrg            &subpass->color_attachments[j];
827ec681f3Smrg         if (subpass_att->attachment == VK_ATTACHMENT_UNUSED)
837ec681f3Smrg            continue;
847ec681f3Smrg
857ec681f3Smrg         subpass->has_color_att = true;
867ec681f3Smrg
877ec681f3Smrg         struct lvp_render_pass_attachment *pass_att =
887ec681f3Smrg            &pass->attachments[subpass_att->attachment];
897ec681f3Smrg
907ec681f3Smrg         color_sample_count = pass_att->samples;
917ec681f3Smrg      }
927ec681f3Smrg
937ec681f3Smrg      if (subpass->depth_stencil_attachment) {
947ec681f3Smrg         const uint32_t a =
957ec681f3Smrg            subpass->depth_stencil_attachment->attachment;
967ec681f3Smrg         struct lvp_render_pass_attachment *pass_att =
977ec681f3Smrg            &pass->attachments[a];
987ec681f3Smrg         depth_sample_count = pass_att->samples;
997ec681f3Smrg      }
1007ec681f3Smrg
1017ec681f3Smrg      subpass->max_sample_count = MAX2(color_sample_count,
1027ec681f3Smrg                                       depth_sample_count);
1037ec681f3Smrg
1047ec681f3Smrg      /* We have to handle resolve attachments specially */
1057ec681f3Smrg      subpass->has_color_resolve = false;
1067ec681f3Smrg      if (subpass->resolve_attachments) {
1077ec681f3Smrg         for (uint32_t j = 0; j < subpass->color_count; j++) {
1087ec681f3Smrg            struct lvp_subpass_attachment *resolve_att =
1097ec681f3Smrg               &subpass->resolve_attachments[j];
1107ec681f3Smrg
1117ec681f3Smrg            if (resolve_att->attachment == VK_ATTACHMENT_UNUSED)
1127ec681f3Smrg               continue;
1137ec681f3Smrg
1147ec681f3Smrg            subpass->has_color_resolve = true;
1157ec681f3Smrg         }
1167ec681f3Smrg      }
1177ec681f3Smrg
1187ec681f3Smrg      for (uint32_t j = 0; j < subpass->input_count; ++j) {
1197ec681f3Smrg         if (subpass->input_attachments[j].attachment == VK_ATTACHMENT_UNUSED)
1207ec681f3Smrg            continue;
1217ec681f3Smrg
1227ec681f3Smrg         for (uint32_t k = 0; k < subpass->color_count; ++k) {
1237ec681f3Smrg            if (subpass->color_attachments[k].attachment == subpass->input_attachments[j].attachment) {
1247ec681f3Smrg               subpass->input_attachments[j].in_render_loop = true;
1257ec681f3Smrg               subpass->color_attachments[k].in_render_loop = true;
1267ec681f3Smrg            }
1277ec681f3Smrg         }
1287ec681f3Smrg
1297ec681f3Smrg         if (subpass->depth_stencil_attachment &&
1307ec681f3Smrg             subpass->depth_stencil_attachment->attachment == subpass->input_attachments[j].attachment) {
1317ec681f3Smrg            subpass->input_attachments[j].in_render_loop = true;
1327ec681f3Smrg            subpass->depth_stencil_attachment->in_render_loop = true;
1337ec681f3Smrg         }
1347ec681f3Smrg      }
1357ec681f3Smrg   }
1367ec681f3Smrg}
1377ec681f3Smrg
1387ec681f3Smrgstatic unsigned
1397ec681f3Smrglvp_num_subpass_attachments2(const VkSubpassDescription2 *desc)
1407ec681f3Smrg{
1417ec681f3Smrg   const VkSubpassDescriptionDepthStencilResolve *ds_resolve =
1427ec681f3Smrg      vk_find_struct_const(desc->pNext,
1437ec681f3Smrg                           SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE);
1447ec681f3Smrg   return desc->inputAttachmentCount +
1457ec681f3Smrg      desc->colorAttachmentCount +
1467ec681f3Smrg      (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
1477ec681f3Smrg      (desc->pDepthStencilAttachment != NULL) +
1487ec681f3Smrg      (ds_resolve && ds_resolve->pDepthStencilResolveAttachment);
1497ec681f3Smrg}
1507ec681f3Smrg
1517ec681f3SmrgVKAPI_ATTR VkResult VKAPI_CALL lvp_CreateRenderPass2(
1527ec681f3Smrg    VkDevice                                    _device,
1537ec681f3Smrg    const VkRenderPassCreateInfo2*              pCreateInfo,
1547ec681f3Smrg    const VkAllocationCallbacks*                pAllocator,
1557ec681f3Smrg    VkRenderPass*                               pRenderPass)
1567ec681f3Smrg{
1577ec681f3Smrg   LVP_FROM_HANDLE(lvp_device, device, _device);
1587ec681f3Smrg   struct lvp_render_pass *pass;
1597ec681f3Smrg   size_t attachments_offset;
1607ec681f3Smrg   size_t size;
1617ec681f3Smrg
1627ec681f3Smrg   size = sizeof(*pass);
1637ec681f3Smrg   size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
1647ec681f3Smrg   attachments_offset = size;
1657ec681f3Smrg   size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
1667ec681f3Smrg
1677ec681f3Smrg   pass = vk_alloc2(&device->vk.alloc, pAllocator, size, 8,
1687ec681f3Smrg                    VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1697ec681f3Smrg   if (pass == NULL)
1707ec681f3Smrg      return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
1717ec681f3Smrg
1727ec681f3Smrg   /* Clear the subpasses along with the parent pass. This required because
1737ec681f3Smrg    * each array member of lvp_subpass must be a valid pointer if not NULL.
1747ec681f3Smrg    */
1757ec681f3Smrg   memset(pass, 0, size);
1767ec681f3Smrg
1777ec681f3Smrg   vk_object_base_init(&device->vk, &pass->base,
1787ec681f3Smrg                       VK_OBJECT_TYPE_RENDER_PASS);
1797ec681f3Smrg   pass->attachment_count = pCreateInfo->attachmentCount;
1807ec681f3Smrg   pass->subpass_count = pCreateInfo->subpassCount;
1817ec681f3Smrg   pass->attachments = (struct lvp_render_pass_attachment *)((char *)pass + attachments_offset);
1827ec681f3Smrg
1837ec681f3Smrg   for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
1847ec681f3Smrg      struct lvp_render_pass_attachment *att = &pass->attachments[i];
1857ec681f3Smrg
1867ec681f3Smrg      att->format = pCreateInfo->pAttachments[i].format;
1877ec681f3Smrg      att->samples = pCreateInfo->pAttachments[i].samples;
1887ec681f3Smrg      att->load_op = pCreateInfo->pAttachments[i].loadOp;
1897ec681f3Smrg      att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
1907ec681f3Smrg      att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
1917ec681f3Smrg      att->first_subpass_idx = UINT32_MAX;
1927ec681f3Smrg
1937ec681f3Smrg      bool is_zs = util_format_is_depth_or_stencil(lvp_vk_format_to_pipe_format(att->format));
1947ec681f3Smrg      pass->has_zs_attachment |= is_zs;
1957ec681f3Smrg      pass->has_color_attachment |= !is_zs;
1967ec681f3Smrg   }
1977ec681f3Smrg   uint32_t subpass_attachment_count = 0;
1987ec681f3Smrg   for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
1997ec681f3Smrg      subpass_attachment_count += lvp_num_subpass_attachments2(&pCreateInfo->pSubpasses[i]);
2007ec681f3Smrg   }
2017ec681f3Smrg
2027ec681f3Smrg   if (subpass_attachment_count) {
2037ec681f3Smrg      pass->subpass_attachments =
2047ec681f3Smrg         vk_alloc2(&device->vk.alloc, pAllocator,
2057ec681f3Smrg                   subpass_attachment_count * sizeof(struct lvp_subpass_attachment), 8,
2067ec681f3Smrg                   VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2077ec681f3Smrg      if (pass->subpass_attachments == NULL) {
2087ec681f3Smrg         vk_free2(&device->vk.alloc, pAllocator, pass);
2097ec681f3Smrg         return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
2107ec681f3Smrg      }
2117ec681f3Smrg   } else
2127ec681f3Smrg      pass->subpass_attachments = NULL;
2137ec681f3Smrg
2147ec681f3Smrg   struct lvp_subpass_attachment *p = pass->subpass_attachments;
2157ec681f3Smrg   for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
2167ec681f3Smrg      const VkSubpassDescription2 *desc = &pCreateInfo->pSubpasses[i];
2177ec681f3Smrg      struct lvp_subpass *subpass = &pass->subpasses[i];
2187ec681f3Smrg
2197ec681f3Smrg      subpass->input_count = desc->inputAttachmentCount;
2207ec681f3Smrg      subpass->color_count = desc->colorAttachmentCount;
2217ec681f3Smrg      subpass->attachment_count = lvp_num_subpass_attachments2(desc);
2227ec681f3Smrg      subpass->attachments = p;
2237ec681f3Smrg      subpass->view_mask = desc->viewMask;
2247ec681f3Smrg
2257ec681f3Smrg      if (desc->inputAttachmentCount > 0) {
2267ec681f3Smrg         subpass->input_attachments = p;
2277ec681f3Smrg         p += desc->inputAttachmentCount;
2287ec681f3Smrg
2297ec681f3Smrg         for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
2307ec681f3Smrg            subpass->input_attachments[j] = (struct lvp_subpass_attachment) {
2317ec681f3Smrg               .attachment = desc->pInputAttachments[j].attachment,
2327ec681f3Smrg               .layout = desc->pInputAttachments[j].layout,
2337ec681f3Smrg            };
2347ec681f3Smrg         }
2357ec681f3Smrg      }
2367ec681f3Smrg
2377ec681f3Smrg      if (desc->colorAttachmentCount > 0) {
2387ec681f3Smrg         subpass->color_attachments = p;
2397ec681f3Smrg         p += desc->colorAttachmentCount;
2407ec681f3Smrg
2417ec681f3Smrg         for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
2427ec681f3Smrg            subpass->color_attachments[j] = (struct lvp_subpass_attachment) {
2437ec681f3Smrg               .attachment = desc->pColorAttachments[j].attachment,
2447ec681f3Smrg               .layout = desc->pColorAttachments[j].layout,
2457ec681f3Smrg            };
2467ec681f3Smrg         }
2477ec681f3Smrg      }
2487ec681f3Smrg
2497ec681f3Smrg      if (desc->pResolveAttachments) {
2507ec681f3Smrg         subpass->resolve_attachments = p;
2517ec681f3Smrg         p += desc->colorAttachmentCount;
2527ec681f3Smrg
2537ec681f3Smrg         for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
2547ec681f3Smrg            subpass->resolve_attachments[j] = (struct lvp_subpass_attachment) {
2557ec681f3Smrg               .attachment = desc->pResolveAttachments[j].attachment,
2567ec681f3Smrg               .layout = desc->pResolveAttachments[j].layout,
2577ec681f3Smrg            };
2587ec681f3Smrg         }
2597ec681f3Smrg      }
2607ec681f3Smrg
2617ec681f3Smrg      if (desc->pDepthStencilAttachment) {
2627ec681f3Smrg         subpass->depth_stencil_attachment = p++;
2637ec681f3Smrg
2647ec681f3Smrg         *subpass->depth_stencil_attachment = (struct lvp_subpass_attachment) {
2657ec681f3Smrg            .attachment = desc->pDepthStencilAttachment->attachment,
2667ec681f3Smrg            .layout = desc->pDepthStencilAttachment->layout,
2677ec681f3Smrg         };
2687ec681f3Smrg      }
2697ec681f3Smrg
2707ec681f3Smrg      const VkSubpassDescriptionDepthStencilResolve *ds_resolve =
2717ec681f3Smrg         vk_find_struct_const(desc->pNext, SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE);
2727ec681f3Smrg
2737ec681f3Smrg      if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment) {
2747ec681f3Smrg         subpass->ds_resolve_attachment = p++;
2757ec681f3Smrg
2767ec681f3Smrg         *subpass->ds_resolve_attachment = (struct lvp_subpass_attachment){
2777ec681f3Smrg            .attachment = ds_resolve->pDepthStencilResolveAttachment->attachment,
2787ec681f3Smrg            .layout = ds_resolve->pDepthStencilResolveAttachment->layout,
2797ec681f3Smrg         };
2807ec681f3Smrg
2817ec681f3Smrg         subpass->depth_resolve_mode = ds_resolve->depthResolveMode;
2827ec681f3Smrg         subpass->stencil_resolve_mode = ds_resolve->stencilResolveMode;
2837ec681f3Smrg      }
2847ec681f3Smrg   }
2857ec681f3Smrg
2867ec681f3Smrg   lvp_render_pass_compile(pass);
2877ec681f3Smrg   *pRenderPass = lvp_render_pass_to_handle(pass);
2887ec681f3Smrg
2897ec681f3Smrg   return VK_SUCCESS;
2907ec681f3Smrg}
2917ec681f3Smrg
2927ec681f3SmrgVKAPI_ATTR void VKAPI_CALL lvp_DestroyRenderPass(
2937ec681f3Smrg   VkDevice                                    _device,
2947ec681f3Smrg   VkRenderPass                                _pass,
2957ec681f3Smrg   const VkAllocationCallbacks*                pAllocator)
2967ec681f3Smrg{
2977ec681f3Smrg   LVP_FROM_HANDLE(lvp_device, device, _device);
2987ec681f3Smrg   LVP_FROM_HANDLE(lvp_render_pass, pass, _pass);
2997ec681f3Smrg
3007ec681f3Smrg   if (!_pass)
3017ec681f3Smrg      return;
3027ec681f3Smrg   vk_object_base_finish(&pass->base);
3037ec681f3Smrg   vk_free2(&device->vk.alloc, pAllocator, pass->subpass_attachments);
3047ec681f3Smrg   vk_free2(&device->vk.alloc, pAllocator, pass);
3057ec681f3Smrg}
3067ec681f3Smrg
3077ec681f3SmrgVKAPI_ATTR void VKAPI_CALL lvp_GetRenderAreaGranularity(
3087ec681f3Smrg   VkDevice                                    device,
3097ec681f3Smrg   VkRenderPass                                renderPass,
3107ec681f3Smrg   VkExtent2D*                                 pGranularity)
3117ec681f3Smrg{
3127ec681f3Smrg   *pGranularity = (VkExtent2D) { 1, 1 };
3137ec681f3Smrg}
314