vk/0.210.0: Rework allocation to use the new pAllocator's
[mesa.git] / src / 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 = anv_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 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
58 struct anv_render_pass_attachment *att = &pass->attachments[i];
59
60 att->format = anv_format_for_vk_format(pCreateInfo->pAttachments[i].format);
61 att->samples = pCreateInfo->pAttachments[i].samples;
62 att->load_op = pCreateInfo->pAttachments[i].loadOp;
63 att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
64 // att->store_op = pCreateInfo->pAttachments[i].storeOp;
65 // att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
66 }
67
68 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
69 const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
70 struct anv_subpass *subpass = &pass->subpasses[i];
71
72 subpass->input_count = desc->inputAttachmentCount;
73 subpass->color_count = desc->colorAttachmentCount;
74
75 if (desc->inputAttachmentCount > 0) {
76 subpass->input_attachments =
77 anv_alloc2(&device->alloc, pAllocator,
78 desc->inputAttachmentCount * sizeof(uint32_t), 8,
79 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
80
81 for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
82 subpass->input_attachments[j]
83 = desc->pInputAttachments[j].attachment;
84 }
85 }
86
87 if (desc->colorAttachmentCount > 0) {
88 subpass->color_attachments =
89 anv_alloc2(&device->alloc, pAllocator,
90 desc->colorAttachmentCount * sizeof(uint32_t), 8,
91 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
92
93 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
94 subpass->color_attachments[j]
95 = desc->pColorAttachments[j].attachment;
96 }
97 }
98
99 if (desc->pResolveAttachments) {
100 subpass->resolve_attachments =
101 anv_alloc2(&device->alloc, pAllocator,
102 desc->colorAttachmentCount * sizeof(uint32_t), 8,
103 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
104
105 for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
106 subpass->resolve_attachments[j]
107 = desc->pResolveAttachments[j].attachment;
108 }
109 }
110
111 if (desc->pDepthStencilAttachment) {
112 subpass->depth_stencil_attachment =
113 desc->pDepthStencilAttachment->attachment;
114 } else {
115 subpass->depth_stencil_attachment = VK_ATTACHMENT_UNUSED;
116 }
117 }
118
119 *pRenderPass = anv_render_pass_to_handle(pass);
120
121 return VK_SUCCESS;
122 }
123
124 void anv_DestroyRenderPass(
125 VkDevice _device,
126 VkRenderPass _pass,
127 const VkAllocationCallbacks* pAllocator)
128 {
129 ANV_FROM_HANDLE(anv_device, device, _device);
130 ANV_FROM_HANDLE(anv_render_pass, pass, _pass);
131
132 for (uint32_t i = 0; i < pass->subpass_count; i++) {
133 /* In VkSubpassCreateInfo, each of the attachment arrays may be null.
134 * Don't free the null arrays.
135 */
136 struct anv_subpass *subpass = &pass->subpasses[i];
137
138 anv_free2(&device->alloc, pAllocator, subpass->input_attachments);
139 anv_free2(&device->alloc, pAllocator, subpass->color_attachments);
140 anv_free2(&device->alloc, pAllocator, subpass->resolve_attachments);
141 }
142
143 anv_free2(&device->alloc, pAllocator, pass);
144 }
145
146 void anv_GetRenderAreaGranularity(
147 VkDevice device,
148 VkRenderPass renderPass,
149 VkExtent2D* pGranularity)
150 {
151 *pGranularity = (VkExtent2D) { 1, 1 };
152 }