1/*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * IN THE SOFTWARE.
26 */
27#include "radv_private.h"
28
29VkResult radv_CreateRenderPass(
30	VkDevice                                    _device,
31	const VkRenderPassCreateInfo*               pCreateInfo,
32	const VkAllocationCallbacks*                pAllocator,
33	VkRenderPass*                               pRenderPass)
34{
35	RADV_FROM_HANDLE(radv_device, device, _device);
36	struct radv_render_pass *pass;
37	size_t size;
38	size_t attachments_offset;
39
40	assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
41
42	size = sizeof(*pass);
43	size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
44	attachments_offset = size;
45	size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
46
47	pass = vk_alloc2(&device->alloc, pAllocator, size, 8,
48			   VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
49	if (pass == NULL)
50		return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
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 radv_render_pass_attachment *att = &pass->attachments[i];
59
60		att->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->initial_layout =  pCreateInfo->pAttachments[i].initialLayout;
65		att->final_layout =  pCreateInfo->pAttachments[i].finalLayout;
66		// att->store_op = pCreateInfo->pAttachments[i].storeOp;
67		// att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
68	}
69	uint32_t subpass_attachment_count = 0;
70	VkAttachmentReference *p;
71	for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
72		const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
73
74		subpass_attachment_count +=
75			desc->inputAttachmentCount +
76			desc->colorAttachmentCount +
77			/* Count colorAttachmentCount again for resolve_attachments */
78			desc->colorAttachmentCount;
79	}
80
81	if (subpass_attachment_count) {
82		pass->subpass_attachments =
83			vk_alloc2(&device->alloc, pAllocator,
84				    subpass_attachment_count * sizeof(VkAttachmentReference), 8,
85				    VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
86		if (pass->subpass_attachments == NULL) {
87			vk_free2(&device->alloc, pAllocator, pass);
88			return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
89		}
90	} else
91		pass->subpass_attachments = NULL;
92
93	p = pass->subpass_attachments;
94	for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
95		const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
96		struct radv_subpass *subpass = &pass->subpasses[i];
97
98		subpass->input_count = desc->inputAttachmentCount;
99		subpass->color_count = desc->colorAttachmentCount;
100
101		if (desc->inputAttachmentCount > 0) {
102			subpass->input_attachments = p;
103			p += desc->inputAttachmentCount;
104
105			for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
106				subpass->input_attachments[j]
107					= desc->pInputAttachments[j];
108			}
109		}
110
111		if (desc->colorAttachmentCount > 0) {
112			subpass->color_attachments = p;
113			p += desc->colorAttachmentCount;
114
115			for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
116				subpass->color_attachments[j]
117					= desc->pColorAttachments[j];
118			}
119		}
120
121		subpass->has_resolve = false;
122		if (desc->pResolveAttachments) {
123			subpass->resolve_attachments = p;
124			p += desc->colorAttachmentCount;
125
126			for (uint32_t j = 0; j < desc->colorAttachmentCount; j++) {
127				uint32_t a = desc->pResolveAttachments[j].attachment;
128				subpass->resolve_attachments[j]
129					= desc->pResolveAttachments[j];
130				if (a != VK_ATTACHMENT_UNUSED)
131					subpass->has_resolve = true;
132			}
133		}
134
135		if (desc->pDepthStencilAttachment) {
136			subpass->depth_stencil_attachment =
137				*desc->pDepthStencilAttachment;
138		} else {
139			subpass->depth_stencil_attachment.attachment = VK_ATTACHMENT_UNUSED;
140		}
141	}
142
143	for (unsigned i = 0; i < pCreateInfo->dependencyCount; ++i) {
144		uint32_t dst = pCreateInfo->pDependencies[i].dstSubpass;
145		if (dst == VK_SUBPASS_EXTERNAL) {
146			pass->end_barrier.src_stage_mask = pCreateInfo->pDependencies[i].srcStageMask;
147			pass->end_barrier.src_access_mask = pCreateInfo->pDependencies[i].srcAccessMask;
148			pass->end_barrier.dst_access_mask = pCreateInfo->pDependencies[i].dstAccessMask;
149		} else {
150			pass->subpasses[dst].start_barrier.src_stage_mask = pCreateInfo->pDependencies[i].srcStageMask;
151			pass->subpasses[dst].start_barrier.src_access_mask = pCreateInfo->pDependencies[i].srcAccessMask;
152			pass->subpasses[dst].start_barrier.dst_access_mask = pCreateInfo->pDependencies[i].dstAccessMask;
153		}
154	}
155
156	*pRenderPass = radv_render_pass_to_handle(pass);
157
158	return VK_SUCCESS;
159}
160
161void radv_DestroyRenderPass(
162	VkDevice                                    _device,
163	VkRenderPass                                _pass,
164	const VkAllocationCallbacks*                pAllocator)
165{
166	RADV_FROM_HANDLE(radv_device, device, _device);
167	RADV_FROM_HANDLE(radv_render_pass, pass, _pass);
168
169	if (!_pass)
170		return;
171	vk_free2(&device->alloc, pAllocator, pass->subpass_attachments);
172	vk_free2(&device->alloc, pAllocator, pass);
173}
174
175void radv_GetRenderAreaGranularity(
176    VkDevice                                    device,
177    VkRenderPass                                renderPass,
178    VkExtent2D*                                 pGranularity)
179{
180	pGranularity->width = 1;
181	pGranularity->height = 1;
182}
183
184