OSDN Git Service

anv/pass: Store the per-subpass view mask
[android-x86/external-mesa.git] / src / intel / vulkan / anv_pass.c
1 /*
2  * Copyright © 2015 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
24 #include "anv_private.h"
25
26 #include "util/vk_util.h"
27
28 static unsigned
29 num_subpass_attachments(const VkSubpassDescription *desc)
30 {
31    return desc->inputAttachmentCount +
32           desc->colorAttachmentCount +
33           (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
34           (desc->pDepthStencilAttachment != NULL);
35 }
36
37 VkResult anv_CreateRenderPass(
38     VkDevice                                    _device,
39     const VkRenderPassCreateInfo*               pCreateInfo,
40     const VkAllocationCallbacks*                pAllocator,
41     VkRenderPass*                               pRenderPass)
42 {
43    ANV_FROM_HANDLE(anv_device, device, _device);
44
45    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
46
47    struct anv_render_pass *pass;
48    struct anv_subpass *subpasses;
49    struct anv_render_pass_attachment *attachments;
50    enum anv_pipe_bits *subpass_flushes;
51
52    ANV_MULTIALLOC(ma);
53    anv_multialloc_add(&ma, &pass, 1);
54    anv_multialloc_add(&ma, &subpasses, pCreateInfo->subpassCount);
55    anv_multialloc_add(&ma, &attachments, pCreateInfo->attachmentCount);
56    anv_multialloc_add(&ma, &subpass_flushes, pCreateInfo->subpassCount + 1);
57
58    VkAttachmentReference *subpass_attachments;
59    uint32_t subpass_attachment_count = 0;
60    for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
61       subpass_attachment_count +=
62          num_subpass_attachments(&pCreateInfo->pSubpasses[i]);
63    }
64    anv_multialloc_add(&ma, &subpass_attachments, subpass_attachment_count);
65
66    enum anv_subpass_usage *subpass_usages;
67    anv_multialloc_add(&ma, &subpass_usages,
68                       pCreateInfo->subpassCount * pCreateInfo->attachmentCount);
69
70    if (!anv_multialloc_alloc2(&ma, &device->alloc, pAllocator,
71                               VK_SYSTEM_ALLOCATION_SCOPE_OBJECT))
72       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
73
74    /* Clear the subpasses along with the parent pass. This required because
75     * each array member of anv_subpass must be a valid pointer if not NULL.
76     */
77    memset(pass, 0, ma.size);
78    pass->attachment_count = pCreateInfo->attachmentCount;
79    pass->subpass_count = pCreateInfo->subpassCount;
80    pass->attachments = attachments;
81    pass->subpass_flushes = subpass_flushes;
82
83    for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
84       struct anv_render_pass_attachment *att = &pass->attachments[i];
85
86       att->format = pCreateInfo->pAttachments[i].format;
87       att->samples = pCreateInfo->pAttachments[i].samples;
88       att->usage = 0;
89       att->load_op = pCreateInfo->pAttachments[i].loadOp;
90       att->store_op = pCreateInfo->pAttachments[i].storeOp;
91       att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
92       att->initial_layout = pCreateInfo->pAttachments[i].initialLayout;
93       att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
94       att->subpass_usage = subpass_usages;
95       subpass_usages += pass->subpass_count;
96    }
97
98    bool has_color = false, has_depth = false, has_input = false;
99    for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
100       const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
101       struct anv_subpass *subpass = &pass->subpasses[i];
102
103       subpass->input_count = desc->inputAttachmentCount;
104       subpass->color_count = desc->colorAttachmentCount;
105       subpass->attachment_count = num_subpass_attachments(desc);
106       subpass->attachments = subpass_attachments;
107       subpass->view_mask = 0;
108
109       if (desc->inputAttachmentCount > 0) {
110          subpass->input_attachments = subpass_attachments;
111          subpass_attachments += desc->inputAttachmentCount;
112
113          for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
114             uint32_t a = desc->pInputAttachments[j].attachment;
115             subpass->input_attachments[j] = desc->pInputAttachments[j];
116             if (a != VK_ATTACHMENT_UNUSED) {
117                has_input = true;
118                pass->attachments[a].usage |= VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
119                pass->attachments[a].subpass_usage[i] |= ANV_SUBPASS_USAGE_INPUT;
120                pass->attachments[a].last_subpass_idx = i;
121
122                if (desc->pDepthStencilAttachment &&
123                    a == desc->pDepthStencilAttachment->attachment)
124                   subpass->has_ds_self_dep = true;
125             }
126          }
127       }
128
129       if (desc->colorAttachmentCount > 0) {
130          subpass->color_attachments = subpass_attachments;
131          subpass_attachments += desc->colorAttachmentCount;
132
133          for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
134             uint32_t a = desc->pColorAttachments[j].attachment;
135             subpass->color_attachments[j] = desc->pColorAttachments[j];
136             if (a != VK_ATTACHMENT_UNUSED) {
137                has_color = true;
138                pass->attachments[a].usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
139                pass->attachments[a].subpass_usage[i] |= ANV_SUBPASS_USAGE_DRAW;
140                pass->attachments[a].last_subpass_idx = i;
141             }
142          }
143       }
144
145       subpass->has_resolve = false;
146       if (desc->pResolveAttachments) {
147          subpass->resolve_attachments = subpass_attachments;
148          subpass_attachments += desc->colorAttachmentCount;
149
150          for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
151             uint32_t a = desc->pResolveAttachments[j].attachment;
152             subpass->resolve_attachments[j] = desc->pResolveAttachments[j];
153             if (a != VK_ATTACHMENT_UNUSED) {
154                subpass->has_resolve = true;
155                uint32_t color_att = desc->pColorAttachments[j].attachment;
156                pass->attachments[color_att].usage |=
157                   VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
158                pass->attachments[a].usage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
159
160                pass->attachments[color_att].subpass_usage[i] |=
161                   ANV_SUBPASS_USAGE_RESOLVE_SRC;
162                pass->attachments[a].subpass_usage[i] |=
163                   ANV_SUBPASS_USAGE_RESOLVE_DST;
164                pass->attachments[a].last_subpass_idx = i;
165             }
166          }
167       }
168
169       if (desc->pDepthStencilAttachment) {
170          uint32_t a = desc->pDepthStencilAttachment->attachment;
171          *subpass_attachments++ = subpass->depth_stencil_attachment =
172             *desc->pDepthStencilAttachment;
173          if (a != VK_ATTACHMENT_UNUSED) {
174             has_depth = true;
175             pass->attachments[a].usage |=
176                VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
177             pass->attachments[a].subpass_usage[i] |= ANV_SUBPASS_USAGE_DRAW;
178             pass->attachments[a].last_subpass_idx = i;
179          }
180       } else {
181          subpass->depth_stencil_attachment.attachment = VK_ATTACHMENT_UNUSED;
182          subpass->depth_stencil_attachment.layout = VK_IMAGE_LAYOUT_UNDEFINED;
183       }
184    }
185
186    for (uint32_t i = 0; i < pCreateInfo->dependencyCount; i++) {
187       const VkSubpassDependency *dep = &pCreateInfo->pDependencies[i];
188       if (dep->dstSubpass == VK_SUBPASS_EXTERNAL) {
189          pass->subpass_flushes[pass->subpass_count] |=
190             anv_pipe_invalidate_bits_for_access_flags(dep->dstAccessMask);
191       } else {
192          assert(dep->dstSubpass < pass->subpass_count);
193          pass->subpass_flushes[dep->dstSubpass] |=
194             anv_pipe_invalidate_bits_for_access_flags(dep->dstAccessMask);
195       }
196
197       if (dep->srcSubpass == VK_SUBPASS_EXTERNAL) {
198          pass->subpass_flushes[0] |=
199             anv_pipe_flush_bits_for_access_flags(dep->srcAccessMask);
200       } else {
201          assert(dep->srcSubpass < pass->subpass_count);
202          pass->subpass_flushes[dep->srcSubpass + 1] |=
203             anv_pipe_flush_bits_for_access_flags(dep->srcAccessMask);
204       }
205    }
206
207    /* From the Vulkan 1.0.39 spec:
208     *
209     *    If there is no subpass dependency from VK_SUBPASS_EXTERNAL to the
210     *    first subpass that uses an attachment, then an implicit subpass
211     *    dependency exists from VK_SUBPASS_EXTERNAL to the first subpass it is
212     *    used in. The subpass dependency operates as if defined with the
213     *    following parameters:
214     *
215     *    VkSubpassDependency implicitDependency = {
216     *        .srcSubpass = VK_SUBPASS_EXTERNAL;
217     *        .dstSubpass = firstSubpass; // First subpass attachment is used in
218     *        .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
219     *        .dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
220     *        .srcAccessMask = 0;
221     *        .dstAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
222     *                         VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
223     *                         VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
224     *                         VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
225     *                         VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
226     *        .dependencyFlags = 0;
227     *    };
228     *
229     *    Similarly, if there is no subpass dependency from the last subpass
230     *    that uses an attachment to VK_SUBPASS_EXTERNAL, then an implicit
231     *    subpass dependency exists from the last subpass it is used in to
232     *    VK_SUBPASS_EXTERNAL. The subpass dependency operates as if defined
233     *    with the following parameters:
234     *
235     *    VkSubpassDependency implicitDependency = {
236     *        .srcSubpass = lastSubpass; // Last subpass attachment is used in
237     *        .dstSubpass = VK_SUBPASS_EXTERNAL;
238     *        .srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
239     *        .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
240     *        .srcAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
241     *                         VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
242     *                         VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
243     *                         VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
244     *                         VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
245     *        .dstAccessMask = 0;
246     *        .dependencyFlags = 0;
247     *    };
248     *
249     * We could implement this by walking over all of the attachments and
250     * subpasses and checking to see if any of them don't have an external
251     * dependency.  Or, we could just be lazy and add a couple extra flushes.
252     * We choose to be lazy.
253     */
254    if (has_input) {
255       pass->subpass_flushes[0] |=
256          ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT;
257    }
258    if (has_color) {
259       pass->subpass_flushes[pass->subpass_count] |=
260          ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT;
261    }
262    if (has_depth) {
263       pass->subpass_flushes[pass->subpass_count] |=
264          ANV_PIPE_DEPTH_CACHE_FLUSH_BIT;
265    }
266
267    vk_foreach_struct(ext, pCreateInfo->pNext) {
268       switch (ext->sType) {
269       case VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO_KHX: {
270          VkRenderPassMultiviewCreateInfoKHX *mv = (void *)ext;
271
272          for (uint32_t i = 0; i < mv->subpassCount; i++) {
273             pass->subpasses[i].view_mask = mv->pViewMasks[i];
274          }
275          break;
276       }
277
278       default:
279          anv_debug_ignored_stype(ext->sType);
280       }
281    }
282
283    *pRenderPass = anv_render_pass_to_handle(pass);
284
285    return VK_SUCCESS;
286 }
287
288 void anv_DestroyRenderPass(
289     VkDevice                                    _device,
290     VkRenderPass                                _pass,
291     const VkAllocationCallbacks*                pAllocator)
292 {
293    ANV_FROM_HANDLE(anv_device, device, _device);
294    ANV_FROM_HANDLE(anv_render_pass, pass, _pass);
295
296    vk_free2(&device->alloc, pAllocator, pass);
297 }
298
299 void anv_GetRenderAreaGranularity(
300     VkDevice                                    device,
301     VkRenderPass                                renderPass,
302     VkExtent2D*                                 pGranularity)
303 {
304    ANV_FROM_HANDLE(anv_render_pass, pass, renderPass);
305
306    /* This granularity satisfies HiZ fast clear alignment requirements
307     * for all sample counts.
308     */
309    for (unsigned i = 0; i < pass->subpass_count; ++i) {
310       if (pass->subpasses[i].depth_stencil_attachment.attachment !=
311           VK_ATTACHMENT_UNUSED) {
312          *pGranularity = (VkExtent2D) { .width = 8, .height = 4 };
313          return;
314       }
315    }
316
317    *pGranularity = (VkExtent2D) { 1, 1 };
318 }