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