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 
35 namespace vkt
36 {
37 
38 namespace api
39 {
40 
createTestBuffer(VkDeviceSize size,VkBufferUsageFlags usage,Context & context,Allocator & allocator,Move<VkBuffer> & buffer,const MemoryRequirement & requirement,de::MovePtr<Allocation> & memory) const41 void BufferSuballocation::createTestBuffer								(VkDeviceSize				size,
42 																		 VkBufferUsageFlags			usage,
43 																		 Context&					context,
44 																		 Allocator&					allocator,
45 																		 Move<VkBuffer>&			buffer,
46 																		 const MemoryRequirement&	requirement,
47 																		 de::MovePtr<Allocation>&	memory) const
48 {
49 	const VkDevice						vkDevice						= context.getDevice();
50 	const DeviceInterface&				vk								= context.getDeviceInterface();
51 	const deUint32						queueFamilyIndex				= context.getUniversalQueueFamilyIndex();
52 	const VkBufferCreateInfo			bufferParams					=
53 	{
54 		VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,							//	VkStructureType			sType;
55 		DE_NULL,														//	const void*				pNext;
56 		0u,																//	VkBufferCreateFlags		flags;
57 		size,															//	VkDeviceSize			size;
58 		usage,															//	VkBufferUsageFlags		usage;
59 		VK_SHARING_MODE_EXCLUSIVE,										//	VkSharingMode			sharingMode;
60 		1u,																//	deUint32				queueFamilyCount;
61 		&queueFamilyIndex,												//	const deUint32*			pQueueFamilyIndices;
62 	};
63 
64 	buffer = vk::createBuffer(vk, vkDevice, &bufferParams, (const VkAllocationCallbacks*)DE_NULL);
65 	memory = allocator.allocate(getBufferMemoryRequirements(vk, vkDevice, *buffer), requirement);
66 	VK_CHECK(vk.bindBufferMemory(vkDevice, *buffer, memory->getMemory(), 0));
67 }
68 
createTestBuffer(VkDeviceSize size,VkBufferUsageFlags usage,Context & context,Allocator & allocator,Move<VkBuffer> & buffer,const MemoryRequirement & requirement,de::MovePtr<Allocation> & memory) const69 void BufferDedicatedAllocation::createTestBuffer						(VkDeviceSize				size,
70 																		 VkBufferUsageFlags			usage,
71 																		 Context&					context,
72 																		 Allocator&					allocator,
73 																		 Move<VkBuffer>&			buffer,
74 																		 const MemoryRequirement&	requirement,
75 																		 de::MovePtr<Allocation>&	memory) const
76 {
77 	DE_UNREF(allocator);
78 	const std::vector<std::string>&	extensions							= context.getDeviceExtensions();
79 	const deBool					isSupported							= isDeviceExtensionSupported(context.getUsedApiVersion(), extensions, "VK_KHR_dedicated_allocation");
80 	if (!isSupported)
81 	{
82 		TCU_THROW(NotSupportedError, "Not supported");
83 	}
84 
85 	const InstanceInterface&			vkInstance						= context.getInstanceInterface();
86 	const VkDevice						vkDevice						= context.getDevice();
87 	const VkPhysicalDevice				vkPhysicalDevice				= context.getPhysicalDevice();
88 	const DeviceInterface&				vk								= context.getDeviceInterface();
89 	const deUint32						queueFamilyIndex				= context.getUniversalQueueFamilyIndex();
90 
91 	const VkBufferCreateInfo			bufferParams					=
92 	{
93 		VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,							//	VkStructureType			sType;
94 		DE_NULL,														//	const void*				pNext;
95 		0u,																//	VkBufferCreateFlags		flags;
96 		size,															//	VkDeviceSize			size;
97 		usage,															//	VkBufferUsageFlags		usage;
98 		VK_SHARING_MODE_EXCLUSIVE,										//	VkSharingMode			sharingMode;
99 		1u,																//	deUint32				queueFamilyCount;
100 		&queueFamilyIndex,												//	const deUint32*			pQueueFamilyIndices;
101 	};
102 
103 	buffer = vk::createBuffer(vk, vkDevice, &bufferParams, (const VkAllocationCallbacks*)DE_NULL);
104 	memory = allocateDedicated(vkInstance, vk, vkPhysicalDevice, vkDevice, buffer.get(), requirement);
105 	VK_CHECK(vk.bindBufferMemory(vkDevice, *buffer, memory->getMemory(), memory->getOffset()));
106 }
107 
createTestImage(tcu::IVec2 size,VkFormat format,Context & context,Allocator & allocator,Move<VkImage> & image,const MemoryRequirement & requirement,de::MovePtr<Allocation> & memory,VkImageTiling tiling) const108 void ImageSuballocation::createTestImage								(tcu::IVec2					size,
109 																		 VkFormat					format,
110 																		 Context&					context,
111 																		 Allocator&					allocator,
112 																		 Move<VkImage>&				image,
113 																		 const MemoryRequirement&	requirement,
114 																		 de::MovePtr<Allocation>&	memory,
115 																		 VkImageTiling				tiling) const
116 {
117 	const VkDevice						vkDevice						= context.getDevice();
118 	const DeviceInterface&				vk								= context.getDeviceInterface();
119 	const deUint32						queueFamilyIndex				= context.getUniversalQueueFamilyIndex();
120 
121 	const VkImageCreateInfo				colorImageParams				=
122 	{
123 		VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,							// VkStructureType			sType;
124 		DE_NULL,														// const void*				pNext;
125 		0u,																// VkImageCreateFlags		flags;
126 		VK_IMAGE_TYPE_2D,												// VkImageType				imageType;
127 		format,															// VkFormat					format;
128 		{ (deUint32)size.x(), (deUint32)size.y(), 1u },					// VkExtent3D				extent;
129 		1u,																// deUint32					mipLevels;
130 		1u,																// deUint32					arraySize;
131 		VK_SAMPLE_COUNT_1_BIT,											// deUint32					samples;
132 		tiling,															// VkImageTiling			tiling;
133 		(vk::VkImageUsageFlags)((tiling == VK_IMAGE_TILING_LINEAR) ? VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT :
134 		VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT), // VkImageUsageFlags	usage;
135 		VK_SHARING_MODE_EXCLUSIVE,										// VkSharingMode			sharingMode;
136 		1u,																// deUint32					queueFamilyCount;
137 		&queueFamilyIndex,												// const deUint32*			pQueueFamilyIndices;
138 		VK_IMAGE_LAYOUT_UNDEFINED,										// VkImageLayout			initialLayout;
139 	};
140 
141 	image = createImage(vk, vkDevice, &colorImageParams);
142 	memory = allocator.allocate(getImageMemoryRequirements(vk, vkDevice, *image), requirement);
143 	VK_CHECK(vk.bindImageMemory(vkDevice, *image, memory->getMemory(), memory->getOffset()));
144 }
145 
createTestImage(tcu::IVec2 size,VkFormat format,Context & context,Allocator & allocator,Move<VkImage> & image,const MemoryRequirement & requirement,de::MovePtr<Allocation> & memory,VkImageTiling tiling) const146 void ImageDedicatedAllocation::createTestImage							(tcu::IVec2					size,
147 																		 VkFormat					format,
148 																		 Context&					context,
149 																		 Allocator&					allocator,
150 																		 Move<VkImage>&				image,
151 																		 const MemoryRequirement&	requirement,
152 																		 de::MovePtr<Allocation>&	memory,
153 																		 VkImageTiling				tiling) const
154 {
155 	DE_UNREF(allocator);
156 	const std::vector<std::string>&		extensions						= context.getDeviceExtensions();
157 	const deBool						isSupported						= isDeviceExtensionSupported(context.getUsedApiVersion(), extensions, "VK_KHR_dedicated_allocation");
158 	if (!isSupported)
159 	{
160 		TCU_THROW(NotSupportedError, "Not supported");
161 	}
162 
163 	const InstanceInterface&			vkInstance						= context.getInstanceInterface();
164 	const VkDevice						vkDevice						= context.getDevice();
165 	const VkPhysicalDevice				vkPhysicalDevice				= context.getPhysicalDevice();
166 	const DeviceInterface&				vk								= context.getDeviceInterface();
167 	const deUint32						queueFamilyIndex				= context.getUniversalQueueFamilyIndex();
168 
169 	const VkImageCreateInfo				colorImageParams				=
170 	{
171 		VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,							// VkStructureType			sType;
172 		DE_NULL,														// const void*				pNext;
173 		0u,																// VkImageCreateFlags		flags;
174 		VK_IMAGE_TYPE_2D,												// VkImageType				imageType;
175 		format,															// VkFormat					format;
176 		{ (deUint32)size.x(), (deUint32)size.y(), 1u },					// VkExtent3D				extent;
177 		1u,																// deUint32					mipLevels;
178 		1u,																// deUint32					arraySize;
179 		VK_SAMPLE_COUNT_1_BIT,											// deUint32					samples;
180 		tiling,															// VkImageTiling			tiling;
181 		(vk::VkImageUsageFlags)((tiling == VK_IMAGE_TILING_LINEAR) ? VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT :
182 		VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT), // VkImageUsageFlags	usage;
183 		VK_SHARING_MODE_EXCLUSIVE,										// VkSharingMode			sharingMode;
184 		1u,																// deUint32					queueFamilyCount;
185 		&queueFamilyIndex,												// const deUint32*			pQueueFamilyIndices;
186 		VK_IMAGE_LAYOUT_UNDEFINED,										// VkImageLayout			initialLayout;
187 	};
188 
189 	image = createImage(vk, vkDevice, &colorImageParams);
190 	memory = allocateDedicated(vkInstance, vk, vkPhysicalDevice, vkDevice, image.get(), requirement);
191 	VK_CHECK(vk.bindImageMemory(vkDevice, *image, memory->getMemory(), memory->getOffset()));
192 }
193 
194 } // api
195 } // vkt
196