OSDN Git Service

anv/pass: Store subpass attachment reference list
[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 VkResult anv_CreateRenderPass(
27     VkDevice                                    _device,
28     const VkRenderPassCreateInfo*               pCreateInfo,
29     const VkAllocationCallbacks*                pAllocator,
30     VkRenderPass*                               pRenderPass)
31 {
32    ANV_FROM_HANDLE(anv_device, device, _device);
33    struct anv_render_pass *pass;
34    size_t size;
35    size_t attachments_offset;
36
37    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
38
39    size = sizeof(*pass);
40    size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
41    attachments_offset = size;
42    size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
43
44    pass = vk_alloc2(&device->alloc, pAllocator, size, 8,
45                      VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
46    if (pass == NULL)
47       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
48
49    /* Clear the subpasses along with the parent pass. This required because
50     * each array member of anv_subpass must be a valid pointer if not NULL.
51     */
52    memset(pass, 0, size);
53    pass->attachment_count = pCreateInfo->attachmentCount;
54    pass->subpass_count = pCreateInfo->subpassCount;
55    pass->attachments = (void *) pass + attachments_offset;
56
57    pass->subpass_usages =
58       vk_zalloc2(&device->alloc, pAllocator,
59                  pass->subpass_count * pass->attachment_count *
60                                        sizeof(*pass->subpass_usages),
61                  8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
62    if (pass->subpass_usages == NULL) {
63       vk_free2(&device->alloc, pAllocator, pass);
64       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
65    }
66
67    enum anv_subpass_usage *usages = pass->subpass_usages;
68    for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
69       struct anv_render_pass_attachment *att = &pass->attachments[i];
70
71       att->format = pCreateInfo->pAttachments[i].format;
72       att->samples = pCreateInfo->pAttachments[i].samples;
73       att->usage = 0;
74       att->load_op = pCreateInfo->pAttachments[i].loadOp;
75       att->store_op = pCreateInfo->pAttachments[i].storeOp;
76       att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
77       att->initial_layout = pCreateInfo->pAttachments[i].initialLayout;
78       att->final_layout = pCreateInfo->pAttachments[i].finalLayout;
79       att->subpass_usage = usages;
80       usages += pass->subpass_count;
81    }
82
83    uint32_t subpass_attachment_count = 0;
84    VkAttachmentReference *p;
85    for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
86       const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
87
88       subpass_attachment_count +=
89       pass->subpasses[i].attachment_count =
90          desc->inputAttachmentCount +
91          desc->colorAttachmentCount +
92          (desc->pResolveAttachments ? desc->colorAttachmentCount : 0) +
93          (desc->pDepthStencilAttachment != NULL);
94    }
95
96    pass->subpass_attachments =
97       vk_alloc2(&device->alloc, pAllocator,
98                  subpass_attachment_count * sizeof(VkAttachmentReference), 8,
99                  VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
100    if (pass->subpass_attachments == NULL) {
101       vk_free2(&device->alloc, pAllocator, pass->subpass_usages);
102       vk_free2(&device->alloc, pAllocator, pass);
103       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
104    }
105
106    p = pass->subpass_attachments;
107    for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
108       const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
109       struct anv_subpass *subpass = &pass->subpasses[i];
110
111       subpass->input_count = desc->inputAttachmentCount;
112       subpass->color_count = desc->colorAttachmentCount;
113       subpass->attachments = p;
114
115       if (desc->inputAttachmentCount > 0) {
116          subpass->input_attachments = p;
117          p += desc->inputAttachmentCount;
118
119          for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
120             uint32_t a = desc->pInputAttachments[j].attachment;
121             subpass->input_attachments[j] = desc->pInputAttachments[j];
122             if (a != VK_ATTACHMENT_UNUSED) {
123                pass->attachments[a].usage |= VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
124                pass->attachments[a].subpass_usage[i] |= ANV_SUBPASS_USAGE_INPUT;
125                pass->attachments[a].last_subpass_idx = i;
126
127                if (desc->pDepthStencilAttachment &&
128                    a == desc->pDepthStencilAttachment->attachment)
129                   subpass->has_ds_self_dep = true;
130             }
131          }
132       }
133
134       if (desc->colorAttachmentCount > 0) {
135          subpass->color_attachments = p;
136          p += desc->colorAttachmentCount;
137
138          for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
139             uint32_t a = desc->pColorAttachments[j].attachment;
140             subpass->color_attachments[j] = desc->pColorAttachments[j];
141             if (a != VK_ATTACHMENT_UNUSED) {
142                pass->attachments[a].usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
143                pass->attachments[a].subpass_usage[i] |= ANV_SUBPASS_USAGE_DRAW;
144                pass->attachments[a].last_subpass_idx = i;
145             }
146          }
147       }
148
149       subpass->has_resolve = false;
150       if (desc->pResolveAttachments) {
151          subpass->resolve_attachments = p;
152          p += desc->colorAttachmentCount;
153
154          for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
155             uint32_t a = desc->pResolveAttachments[j].attachment;
156             subpass->resolve_attachments[j] = desc->pResolveAttachments[j];
157             if (a != VK_ATTACHMENT_UNUSED) {
158                subpass->has_resolve = true;
159                uint32_t color_att = desc->pColorAttachments[j].attachment;
160                pass->attachments[color_att].usage |=
161                   VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
162                pass->attachments[a].usage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
163
164                pass->attachments[color_att].subpass_usage[i] |=
165                   ANV_SUBPASS_USAGE_RESOLVE_SRC;
166                pass->attachments[a].subpass_usage[i] |=
167                   ANV_SUBPASS_USAGE_RESOLVE_DST;
168                pass->attachments[a].last_subpass_idx = i;
169             }
170          }
171       }
172
173       if (desc->pDepthStencilAttachment) {
174          uint32_t a = desc->pDepthStencilAttachment->attachment;
175          *p++ = subpass->depth_stencil_attachment =
176             *desc->pDepthStencilAttachment;
177          if (a != VK_ATTACHMENT_UNUSED) {
178             pass->attachments[a].usage |=
179                VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
180             pass->attachments[a].subpass_usage[i] |= ANV_SUBPASS_USAGE_DRAW;
181             pass->attachments[a].last_subpass_idx = i;
182          }
183       } else {
184          subpass->depth_stencil_attachment.attachment = VK_ATTACHMENT_UNUSED;
185          subpass->depth_stencil_attachment.layout = VK_IMAGE_LAYOUT_UNDEFINED;
186       }
187    }
188
189    *pRenderPass = anv_render_pass_to_handle(pass);
190
191    return VK_SUCCESS;
192 }
193
194 void anv_DestroyRenderPass(
195     VkDevice                                    _device,
196     VkRenderPass                                _pass,
197     const VkAllocationCallbacks*                pAllocator)
198 {
199    ANV_FROM_HANDLE(anv_device, device, _device);
200    ANV_FROM_HANDLE(anv_render_pass, pass, _pass);
201
202    if (!pass)
203       return;
204
205    vk_free2(&device->alloc, pAllocator, pass->subpass_attachments);
206    vk_free2(&device->alloc, pAllocator, pass->subpass_usages);
207    vk_free2(&device->alloc, pAllocator, pass);
208 }
209
210 void anv_GetRenderAreaGranularity(
211     VkDevice                                    device,
212     VkRenderPass                                renderPass,
213     VkExtent2D*                                 pGranularity)
214 {
215    ANV_FROM_HANDLE(anv_render_pass, pass, renderPass);
216
217    /* This granularity satisfies HiZ fast clear alignment requirements
218     * for all sample counts.
219     */
220    for (unsigned i = 0; i < pass->subpass_count; ++i) {
221       if (pass->subpasses[i].depth_stencil_attachment.attachment !=
222           VK_ATTACHMENT_UNUSED) {
223          *pGranularity = (VkExtent2D) { .width = 8, .height = 4 };
224          return;
225       }
226    }
227
228    *pGranularity = (VkExtent2D) { 1, 1 };
229 }