1/*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
4 *
5 * Copyright (c) 2017 The Khronos Group Inc.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief Implementation of utility classes for various kinds of
22 * allocation memory for buffers and images
23 *//*--------------------------------------------------------------------*/
24
25#include "vktApiBufferAndImageAllocationUtil.hpp"
26
27#include "deInt32.h"
28#include "tcuVectorUtil.hpp"
29#include "vkQueryUtil.hpp"
30#include "vkMemUtil.hpp"
31#include "vkRefUtil.hpp"
32
33#include <sstream>
34
35namespace vkt
36{
37
38namespace api
39{
40
41void BufferSuballocation::createTestBuffer								(const DeviceInterface&		vk,
42																		 VkDevice					vkDevice,
43																		 deUint32					queueFamilyIndex,
44																		 VkDeviceSize				size,
45																		 VkBufferUsageFlags			usage,
46																		 Context&					context,
47																		 Allocator&					allocator,
48																		 Move<VkBuffer>&			buffer,
49																		 const MemoryRequirement&	requirement,
50																		 de::MovePtr<Allocation>&	memory) const
51{
52	DE_UNREF(context);
53
54	const VkBufferCreateInfo			bufferParams
55	{
56		VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,							//	VkStructureType			sType;
57		DE_NULL,														//	const void*				pNext;
58		0u,																//	VkBufferCreateFlags		flags;
59		size,															//	VkDeviceSize			size;
60		usage,															//	VkBufferUsageFlags		usage;
61		VK_SHARING_MODE_EXCLUSIVE,										//	VkSharingMode			sharingMode;
62		1u,																//	deUint32				queueFamilyCount;
63		&queueFamilyIndex,												//	const deUint32*			pQueueFamilyIndices;
64	};
65
66	buffer = vk::createBuffer(vk, vkDevice, &bufferParams, (const VkAllocationCallbacks*)DE_NULL);
67	memory = allocator.allocate(getBufferMemoryRequirements(vk, vkDevice, *buffer), requirement);
68	VK_CHECK(vk.bindBufferMemory(vkDevice, *buffer, memory->getMemory(), 0));
69}
70
71void BufferDedicatedAllocation::createTestBuffer						(const DeviceInterface&		vk,
72																		 VkDevice					vkDevice,
73																		 deUint32					queueFamilyIndex,
74																		 VkDeviceSize				size,
75																		 VkBufferUsageFlags			usage,
76																		 Context&					context,
77																		 Allocator&					allocator,
78																		 Move<VkBuffer>&			buffer,
79																		 const MemoryRequirement&	requirement,
80																		 de::MovePtr<Allocation>&	memory) const
81{
82	DE_UNREF(allocator);
83	if (!context.isDeviceFunctionalitySupported("VK_KHR_dedicated_allocation"))
84		TCU_THROW(NotSupportedError, "Not supported");
85
86	const InstanceInterface&			vkInstance						= context.getInstanceInterface();
87	const VkPhysicalDevice				vkPhysicalDevice				= context.getPhysicalDevice();
88
89	const VkBufferCreateInfo			bufferParams
90	{
91		VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,							//	VkStructureType			sType;
92		DE_NULL,														//	const void*				pNext;
93		0u,																//	VkBufferCreateFlags		flags;
94		size,															//	VkDeviceSize			size;
95		usage,															//	VkBufferUsageFlags		usage;
96		VK_SHARING_MODE_EXCLUSIVE,										//	VkSharingMode			sharingMode;
97		1u,																//	deUint32				queueFamilyCount;
98		&queueFamilyIndex,												//	const deUint32*			pQueueFamilyIndices;
99	};
100
101	buffer = vk::createBuffer(vk, vkDevice, &bufferParams, (const VkAllocationCallbacks*)DE_NULL);
102	memory = allocateDedicated(vkInstance, vk, vkPhysicalDevice, vkDevice, buffer.get(), requirement);
103	VK_CHECK(vk.bindBufferMemory(vkDevice, *buffer, memory->getMemory(), memory->getOffset()));
104}
105
106void ImageSuballocation::createTestImage								(tcu::IVec2					size,
107																		 VkFormat					format,
108																		 Context&					context,
109																		 Allocator&					allocator,
110																		 Move<VkImage>&				image,
111																		 const MemoryRequirement&	requirement,
112																		 de::MovePtr<Allocation>&	memory,
113																		 VkImageTiling				tiling) const
114{
115	const VkDevice						vkDevice						= context.getDevice();
116	const DeviceInterface&				vk								= context.getDeviceInterface();
117	const deUint32						queueFamilyIndex				= context.getUniversalQueueFamilyIndex();
118
119	const VkImageCreateInfo				colorImageParams				=
120	{
121		VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,							// VkStructureType			sType;
122		DE_NULL,														// const void*				pNext;
123		0u,																// VkImageCreateFlags		flags;
124		VK_IMAGE_TYPE_2D,												// VkImageType				imageType;
125		format,															// VkFormat					format;
126		{ (deUint32)size.x(), (deUint32)size.y(), 1u },					// VkExtent3D				extent;
127		1u,																// deUint32					mipLevels;
128		1u,																// deUint32					arraySize;
129		VK_SAMPLE_COUNT_1_BIT,											// deUint32					samples;
130		tiling,															// VkImageTiling			tiling;
131		(vk::VkImageUsageFlags)((tiling == VK_IMAGE_TILING_LINEAR) ? VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT :
132		VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT), // VkImageUsageFlags	usage;
133		VK_SHARING_MODE_EXCLUSIVE,										// VkSharingMode			sharingMode;
134		1u,																// deUint32					queueFamilyCount;
135		&queueFamilyIndex,												// const deUint32*			pQueueFamilyIndices;
136		VK_IMAGE_LAYOUT_UNDEFINED,										// VkImageLayout			initialLayout;
137	};
138
139	image = createImage(vk, vkDevice, &colorImageParams);
140	memory = allocator.allocate(getImageMemoryRequirements(vk, vkDevice, *image), requirement);
141	VK_CHECK(vk.bindImageMemory(vkDevice, *image, memory->getMemory(), memory->getOffset()));
142}
143
144void ImageDedicatedAllocation::createTestImage							(tcu::IVec2					size,
145																		 VkFormat					format,
146																		 Context&					context,
147																		 Allocator&					allocator,
148																		 Move<VkImage>&				image,
149																		 const MemoryRequirement&	requirement,
150																		 de::MovePtr<Allocation>&	memory,
151																		 VkImageTiling				tiling) const
152{
153	DE_UNREF(allocator);
154	if (!context.isDeviceFunctionalitySupported("VK_KHR_dedicated_allocation"))
155		TCU_THROW(NotSupportedError, "Not supported");
156
157	const InstanceInterface&			vkInstance						= context.getInstanceInterface();
158	const VkDevice						vkDevice						= context.getDevice();
159	const VkPhysicalDevice				vkPhysicalDevice				= context.getPhysicalDevice();
160	const DeviceInterface&				vk								= context.getDeviceInterface();
161	const deUint32						queueFamilyIndex				= context.getUniversalQueueFamilyIndex();
162
163	const VkImageCreateInfo				colorImageParams				=
164	{
165		VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,							// VkStructureType			sType;
166		DE_NULL,														// const void*				pNext;
167		0u,																// VkImageCreateFlags		flags;
168		VK_IMAGE_TYPE_2D,												// VkImageType				imageType;
169		format,															// VkFormat					format;
170		{ (deUint32)size.x(), (deUint32)size.y(), 1u },					// VkExtent3D				extent;
171		1u,																// deUint32					mipLevels;
172		1u,																// deUint32					arraySize;
173		VK_SAMPLE_COUNT_1_BIT,											// deUint32					samples;
174		tiling,															// VkImageTiling			tiling;
175		(vk::VkImageUsageFlags)((tiling == VK_IMAGE_TILING_LINEAR) ? VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT :
176		VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT), // VkImageUsageFlags	usage;
177		VK_SHARING_MODE_EXCLUSIVE,										// VkSharingMode			sharingMode;
178		1u,																// deUint32					queueFamilyCount;
179		&queueFamilyIndex,												// const deUint32*			pQueueFamilyIndices;
180		VK_IMAGE_LAYOUT_UNDEFINED,										// VkImageLayout			initialLayout;
181	};
182
183	image = createImage(vk, vkDevice, &colorImageParams);
184	memory = allocateDedicated(vkInstance, vk, vkPhysicalDevice, vkDevice, image.get(), requirement);
185	VK_CHECK(vk.bindImageMemory(vkDevice, *image, memory->getMemory(), memory->getOffset()));
186}
187
188} // api
189} // vkt
190