Path: blob/21.2-virgl/src/intel/vulkan/anv_pass.c
4547 views
/*1* Copyright © 2015 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "anv_private.h"2425#include "vk_format.h"26#include "vk_util.h"2728static void29anv_render_pass_add_subpass_dep(struct anv_device *device,30struct anv_render_pass *pass,31const VkSubpassDependency2KHR *dep)32{33if (dep->dstSubpass == VK_SUBPASS_EXTERNAL) {34pass->subpass_flushes[pass->subpass_count] |=35anv_pipe_invalidate_bits_for_access_flags(device, dep->dstAccessMask);36} else {37assert(dep->dstSubpass < pass->subpass_count);38pass->subpass_flushes[dep->dstSubpass] |=39anv_pipe_invalidate_bits_for_access_flags(device, dep->dstAccessMask);40}4142if (dep->srcSubpass == VK_SUBPASS_EXTERNAL) {43pass->subpass_flushes[0] |=44anv_pipe_flush_bits_for_access_flags(device, dep->srcAccessMask);45} else {46assert(dep->srcSubpass < pass->subpass_count);47pass->subpass_flushes[dep->srcSubpass + 1] |=48anv_pipe_flush_bits_for_access_flags(device, dep->srcAccessMask);49}50}5152/* Do a second "compile" step on a render pass */53static void54anv_render_pass_compile(struct anv_render_pass *pass)55{56/* The CreateRenderPass code zeros the entire render pass and also uses a57* designated initializer for filling these out. There's no need for us to58* do it again.59*60* for (uint32_t i = 0; i < pass->attachment_count; i++) {61* pass->attachments[i].usage = 0;62* pass->attachments[i].first_subpass_layout = VK_IMAGE_LAYOUT_UNDEFINED;63* }64*/6566VkImageUsageFlags all_usage = 0;67for (uint32_t i = 0; i < pass->subpass_count; i++) {68struct anv_subpass *subpass = &pass->subpasses[i];6970/* We don't allow depth_stencil_attachment to be non-NULL and be71* VK_ATTACHMENT_UNUSED. This way something can just check for NULL72* and be guaranteed that they have a valid attachment.73*/74if (subpass->depth_stencil_attachment &&75subpass->depth_stencil_attachment->attachment == VK_ATTACHMENT_UNUSED)76subpass->depth_stencil_attachment = NULL;7778if (subpass->ds_resolve_attachment &&79subpass->ds_resolve_attachment->attachment == VK_ATTACHMENT_UNUSED)80subpass->ds_resolve_attachment = NULL;8182for (uint32_t j = 0; j < subpass->attachment_count; j++) {83struct anv_subpass_attachment *subpass_att = &subpass->attachments[j];84if (subpass_att->attachment == VK_ATTACHMENT_UNUSED)85continue;8687struct anv_render_pass_attachment *pass_att =88&pass->attachments[subpass_att->attachment];8990pass_att->usage |= subpass_att->usage;91pass_att->last_subpass_idx = i;9293all_usage |= subpass_att->usage;9495if (pass_att->first_subpass_layout == VK_IMAGE_LAYOUT_UNDEFINED) {96pass_att->first_subpass_layout = subpass_att->layout;97assert(pass_att->first_subpass_layout != VK_IMAGE_LAYOUT_UNDEFINED);98}99100if (subpass_att->usage == VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT &&101subpass->depth_stencil_attachment &&102subpass_att->attachment == subpass->depth_stencil_attachment->attachment)103subpass->has_ds_self_dep = true;104}105106/* We have to handle resolve attachments specially */107subpass->has_color_resolve = false;108if (subpass->resolve_attachments) {109for (uint32_t j = 0; j < subpass->color_count; j++) {110struct anv_subpass_attachment *color_att =111&subpass->color_attachments[j];112struct anv_subpass_attachment *resolve_att =113&subpass->resolve_attachments[j];114if (resolve_att->attachment == VK_ATTACHMENT_UNUSED)115continue;116117subpass->has_color_resolve = true;118119assert(color_att->attachment < pass->attachment_count);120struct anv_render_pass_attachment *color_pass_att =121&pass->attachments[color_att->attachment];122123assert(resolve_att->usage == VK_IMAGE_USAGE_TRANSFER_DST_BIT);124assert(color_att->usage == VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);125color_pass_att->usage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;126}127}128129if (subpass->ds_resolve_attachment) {130struct anv_subpass_attachment *ds_att =131subpass->depth_stencil_attachment;132UNUSED struct anv_subpass_attachment *resolve_att =133subpass->ds_resolve_attachment;134135assert(ds_att->attachment < pass->attachment_count);136struct anv_render_pass_attachment *ds_pass_att =137&pass->attachments[ds_att->attachment];138139assert(resolve_att->usage == VK_IMAGE_USAGE_TRANSFER_DST_BIT);140assert(ds_att->usage == VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);141ds_pass_att->usage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;142}143144for (uint32_t j = 0; j < subpass->attachment_count; j++)145assert(__builtin_popcount(subpass->attachments[j].usage) == 1);146}147148/* From the Vulkan 1.0.39 spec:149*150* If there is no subpass dependency from VK_SUBPASS_EXTERNAL to the151* first subpass that uses an attachment, then an implicit subpass152* dependency exists from VK_SUBPASS_EXTERNAL to the first subpass it is153* used in. The subpass dependency operates as if defined with the154* following parameters:155*156* VkSubpassDependency implicitDependency = {157* .srcSubpass = VK_SUBPASS_EXTERNAL;158* .dstSubpass = firstSubpass; // First subpass attachment is used in159* .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;160* .dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;161* .srcAccessMask = 0;162* .dstAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |163* VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |164* VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |165* VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |166* VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;167* .dependencyFlags = 0;168* };169*170* Similarly, if there is no subpass dependency from the last subpass171* that uses an attachment to VK_SUBPASS_EXTERNAL, then an implicit172* subpass dependency exists from the last subpass it is used in to173* VK_SUBPASS_EXTERNAL. The subpass dependency operates as if defined174* with the following parameters:175*176* VkSubpassDependency implicitDependency = {177* .srcSubpass = lastSubpass; // Last subpass attachment is used in178* .dstSubpass = VK_SUBPASS_EXTERNAL;179* .srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;180* .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;181* .srcAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |182* VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |183* VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |184* VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |185* VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;186* .dstAccessMask = 0;187* .dependencyFlags = 0;188* };189*190* We could implement this by walking over all of the attachments and191* subpasses and checking to see if any of them don't have an external192* dependency. Or, we could just be lazy and add a couple extra flushes.193* We choose to be lazy.194*195* From the documentation for vkCmdNextSubpass:196*197* "Moving to the next subpass automatically performs any multisample198* resolve operations in the subpass being ended. End-of-subpass199* multisample resolves are treated as color attachment writes for the200* purposes of synchronization. This applies to resolve operations for201* both color and depth/stencil attachments. That is, they are202* considered to execute in the203* VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT pipeline stage and204* their writes are synchronized with205* VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT."206*207* Therefore, the above flags concerning color attachments also apply to208* color and depth/stencil resolve attachments.209*/210if (all_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {211pass->subpass_flushes[0] |=212ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;213}214if (all_usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |215VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {216pass->subpass_flushes[pass->subpass_count] |=217ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;218}219if (all_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {220pass->subpass_flushes[pass->subpass_count] |=221ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;222}223}224225static unsigned226num_subpass_attachments2(const VkSubpassDescription2KHR *desc)227{228const VkSubpassDescriptionDepthStencilResolveKHR *ds_resolve =229vk_find_struct_const(desc->pNext,230SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE_KHR);231232return desc->inputAttachmentCount +233desc->colorAttachmentCount +234(desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +235(desc->pDepthStencilAttachment != NULL) +236(ds_resolve && ds_resolve->pDepthStencilResolveAttachment);237}238239static bool240vk_image_layout_depth_only(VkImageLayout layout)241{242switch (layout) {243case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL:244case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL:245return true;246247default:248return false;249}250}251252/* From the Vulkan Specification 1.2.166 - VkAttachmentReference2:253*254* "If layout only specifies the layout of the depth aspect of the255* attachment, the layout of the stencil aspect is specified by the256* stencilLayout member of a VkAttachmentReferenceStencilLayout structure257* included in the pNext chain. Otherwise, layout describes the layout for258* all relevant image aspects."259*/260static VkImageLayout261stencil_ref_layout(const VkAttachmentReference2KHR *att_ref)262{263if (!vk_image_layout_depth_only(att_ref->layout))264return att_ref->layout;265266const VkAttachmentReferenceStencilLayoutKHR *stencil_ref =267vk_find_struct_const(att_ref->pNext,268ATTACHMENT_REFERENCE_STENCIL_LAYOUT_KHR);269if (!stencil_ref)270return VK_IMAGE_LAYOUT_UNDEFINED;271return stencil_ref->stencilLayout;272}273274/* From the Vulkan Specification 1.2.166 - VkAttachmentDescription2:275*276* "If format is a depth/stencil format, and initialLayout only specifies277* the initial layout of the depth aspect of the attachment, the initial278* layout of the stencil aspect is specified by the stencilInitialLayout279* member of a VkAttachmentDescriptionStencilLayout structure included in280* the pNext chain. Otherwise, initialLayout describes the initial layout281* for all relevant image aspects."282*/283static VkImageLayout284stencil_desc_layout(const VkAttachmentDescription2KHR *att_desc, bool final)285{286if (!vk_format_has_stencil(att_desc->format))287return VK_IMAGE_LAYOUT_UNDEFINED;288289const VkImageLayout main_layout =290final ? att_desc->finalLayout : att_desc->initialLayout;291if (!vk_image_layout_depth_only(main_layout))292return main_layout;293294const VkAttachmentDescriptionStencilLayoutKHR *stencil_desc =295vk_find_struct_const(att_desc->pNext,296ATTACHMENT_DESCRIPTION_STENCIL_LAYOUT_KHR);297assert(stencil_desc);298return final ?299stencil_desc->stencilFinalLayout :300stencil_desc->stencilInitialLayout;301}302303VkResult anv_CreateRenderPass2(304VkDevice _device,305const VkRenderPassCreateInfo2KHR* pCreateInfo,306const VkAllocationCallbacks* pAllocator,307VkRenderPass* pRenderPass)308{309ANV_FROM_HANDLE(anv_device, device, _device);310311assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2_KHR);312313VK_MULTIALLOC(ma);314VK_MULTIALLOC_DECL(&ma, struct anv_render_pass, pass, 1);315VK_MULTIALLOC_DECL(&ma, struct anv_subpass, subpasses,316pCreateInfo->subpassCount);317VK_MULTIALLOC_DECL(&ma, struct anv_render_pass_attachment, attachments,318pCreateInfo->attachmentCount);319VK_MULTIALLOC_DECL(&ma, enum anv_pipe_bits, subpass_flushes,320pCreateInfo->subpassCount + 1);321322uint32_t subpass_attachment_count = 0;323for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {324subpass_attachment_count +=325num_subpass_attachments2(&pCreateInfo->pSubpasses[i]);326}327VK_MULTIALLOC_DECL(&ma, struct anv_subpass_attachment, subpass_attachments,328subpass_attachment_count);329330if (!vk_object_multizalloc(&device->vk, &ma, pAllocator,331VK_OBJECT_TYPE_RENDER_PASS))332return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);333334/* Clear the subpasses along with the parent pass. This required because335* each array member of anv_subpass must be a valid pointer if not NULL.336*/337pass->attachment_count = pCreateInfo->attachmentCount;338pass->subpass_count = pCreateInfo->subpassCount;339pass->attachments = attachments;340pass->subpass_flushes = subpass_flushes;341342for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {343pass->attachments[i] = (struct anv_render_pass_attachment) {344.format = pCreateInfo->pAttachments[i].format,345.samples = pCreateInfo->pAttachments[i].samples,346.load_op = pCreateInfo->pAttachments[i].loadOp,347.store_op = pCreateInfo->pAttachments[i].storeOp,348.stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp,349.initial_layout = pCreateInfo->pAttachments[i].initialLayout,350.final_layout = pCreateInfo->pAttachments[i].finalLayout,351352.stencil_initial_layout = stencil_desc_layout(&pCreateInfo->pAttachments[i],353false),354.stencil_final_layout = stencil_desc_layout(&pCreateInfo->pAttachments[i],355true),356};357}358359for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {360const VkSubpassDescription2KHR *desc = &pCreateInfo->pSubpasses[i];361struct anv_subpass *subpass = &pass->subpasses[i];362363subpass->input_count = desc->inputAttachmentCount;364subpass->color_count = desc->colorAttachmentCount;365subpass->attachment_count = num_subpass_attachments2(desc);366subpass->attachments = subpass_attachments;367subpass->view_mask = desc->viewMask;368369if (desc->inputAttachmentCount > 0) {370subpass->input_attachments = subpass_attachments;371subpass_attachments += desc->inputAttachmentCount;372373for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {374subpass->input_attachments[j] = (struct anv_subpass_attachment) {375.usage = VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT,376.attachment = desc->pInputAttachments[j].attachment,377.layout = desc->pInputAttachments[j].layout,378.stencil_layout = stencil_ref_layout(&desc->pInputAttachments[j]),379};380}381}382383if (desc->colorAttachmentCount > 0) {384subpass->color_attachments = subpass_attachments;385subpass_attachments += desc->colorAttachmentCount;386387for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {388subpass->color_attachments[j] = (struct anv_subpass_attachment) {389.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,390.attachment = desc->pColorAttachments[j].attachment,391.layout = desc->pColorAttachments[j].layout,392};393}394}395396if (desc->pResolveAttachments) {397subpass->resolve_attachments = subpass_attachments;398subpass_attachments += desc->colorAttachmentCount;399400for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {401subpass->resolve_attachments[j] = (struct anv_subpass_attachment) {402.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT,403.attachment = desc->pResolveAttachments[j].attachment,404.layout = desc->pResolveAttachments[j].layout,405};406}407}408409if (desc->pDepthStencilAttachment) {410subpass->depth_stencil_attachment = subpass_attachments++;411412*subpass->depth_stencil_attachment = (struct anv_subpass_attachment) {413.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,414.attachment = desc->pDepthStencilAttachment->attachment,415.layout = desc->pDepthStencilAttachment->layout,416.stencil_layout = stencil_ref_layout(desc->pDepthStencilAttachment),417};418}419420const VkSubpassDescriptionDepthStencilResolveKHR *ds_resolve =421vk_find_struct_const(desc->pNext,422SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE_KHR);423424if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment) {425subpass->ds_resolve_attachment = subpass_attachments++;426427*subpass->ds_resolve_attachment = (struct anv_subpass_attachment) {428.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT,429.attachment = ds_resolve->pDepthStencilResolveAttachment->attachment,430.layout = ds_resolve->pDepthStencilResolveAttachment->layout,431.stencil_layout = stencil_ref_layout(ds_resolve->pDepthStencilResolveAttachment),432};433subpass->depth_resolve_mode = ds_resolve->depthResolveMode;434subpass->stencil_resolve_mode = ds_resolve->stencilResolveMode;435}436}437438for (uint32_t i = 0; i < pCreateInfo->dependencyCount; i++) {439anv_render_pass_add_subpass_dep(device, pass,440&pCreateInfo->pDependencies[i]);441}442443vk_foreach_struct(ext, pCreateInfo->pNext) {444switch (ext->sType) {445default:446anv_debug_ignored_stype(ext->sType);447}448}449450anv_render_pass_compile(pass);451452*pRenderPass = anv_render_pass_to_handle(pass);453454return VK_SUCCESS;455}456457void anv_DestroyRenderPass(458VkDevice _device,459VkRenderPass _pass,460const VkAllocationCallbacks* pAllocator)461{462ANV_FROM_HANDLE(anv_device, device, _device);463ANV_FROM_HANDLE(anv_render_pass, pass, _pass);464465if (!pass)466return;467468vk_object_free(&device->vk, pAllocator, pass);469}470471void anv_GetRenderAreaGranularity(472VkDevice device,473VkRenderPass renderPass,474VkExtent2D* pGranularity)475{476ANV_FROM_HANDLE(anv_render_pass, pass, renderPass);477478/* This granularity satisfies HiZ fast clear alignment requirements479* for all sample counts.480*/481for (unsigned i = 0; i < pass->subpass_count; ++i) {482if (pass->subpasses[i].depth_stencil_attachment) {483*pGranularity = (VkExtent2D) { .width = 8, .height = 4 };484return;485}486}487488*pGranularity = (VkExtent2D) { 1, 1 };489}490491492