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 	if (!context.isDeviceFunctionalitySupported("VK_KHR_dedicated_allocation"))
79 		TCU_THROW(NotSupportedError, "Not supported");
80 
81 	const InstanceInterface&			vkInstance						= context.getInstanceInterface();
82 	const VkDevice						vkDevice						= context.getDevice();
83 	const VkPhysicalDevice				vkPhysicalDevice				= context.getPhysicalDevice();
84 	const DeviceInterface&				vk								= context.getDeviceInterface();
85 	const deUint32						queueFamilyIndex				= context.getUniversalQueueFamilyIndex();
86 
87 	const VkBufferCreateInfo			bufferParams					=
88 	{
89 		VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,							//	VkStructureType			sType;
90 		DE_NULL,														//	const void*				pNext;
91 		0u,																//	VkBufferCreateFlags		flags;
92 		size,															//	VkDeviceSize			size;
93 		usage,															//	VkBufferUsageFlags		usage;
94 		VK_SHARING_MODE_EXCLUSIVE,										//	VkSharingMode			sharingMode;
95 		1u,																//	deUint32				queueFamilyCount;
96 		&queueFamilyIndex,												//	const deUint32*			pQueueFamilyIndices;
97 	};
98 
99 	buffer = vk::createBuffer(vk, vkDevice, &bufferParams, (const VkAllocationCallbacks*)DE_NULL);
100 	memory = allocateDedicated(vkInstance, vk, vkPhysicalDevice, vkDevice, buffer.get(), requirement);
101 	VK_CHECK(vk.bindBufferMemory(vkDevice, *buffer, memory->getMemory(), memory->getOffset()));
102 }
103 
createTestImage(tcu::IVec2 size, VkFormat format, Context& context, Allocator& allocator, Move<VkImage>& image, const MemoryRequirement& requirement, de::MovePtr<Allocation>& memory, VkImageTiling tiling) const104 void ImageSuballocation::createTestImage								(tcu::IVec2					size,
105 																		 VkFormat					format,
106 																		 Context&					context,
107 																		 Allocator&					allocator,
108 																		 Move<VkImage>&				image,
109 																		 const MemoryRequirement&	requirement,
110 																		 de::MovePtr<Allocation>&	memory,
111 																		 VkImageTiling				tiling) const
112 {
113 	const VkDevice						vkDevice						= context.getDevice();
114 	const DeviceInterface&				vk								= context.getDeviceInterface();
115 	const deUint32						queueFamilyIndex				= context.getUniversalQueueFamilyIndex();
116 
117 	const VkImageCreateInfo				colorImageParams				=
118 	{
119 		VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,							// VkStructureType			sType;
120 		DE_NULL,														// const void*				pNext;
121 		0u,																// VkImageCreateFlags		flags;
122 		VK_IMAGE_TYPE_2D,												// VkImageType				imageType;
123 		format,															// VkFormat					format;
124 		{ (deUint32)size.x(), (deUint32)size.y(), 1u },					// VkExtent3D				extent;
125 		1u,																// deUint32					mipLevels;
126 		1u,																// deUint32					arraySize;
127 		VK_SAMPLE_COUNT_1_BIT,											// deUint32					samples;
128 		tiling,															// VkImageTiling			tiling;
129 		(vk::VkImageUsageFlags)((tiling == VK_IMAGE_TILING_LINEAR) ? VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT :
130 		VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT), // VkImageUsageFlags	usage;
131 		VK_SHARING_MODE_EXCLUSIVE,										// VkSharingMode			sharingMode;
132 		1u,																// deUint32					queueFamilyCount;
133 		&queueFamilyIndex,												// const deUint32*			pQueueFamilyIndices;
134 		VK_IMAGE_LAYOUT_UNDEFINED,										// VkImageLayout			initialLayout;
135 	};
136 
137 	image = createImage(vk, vkDevice, &colorImageParams);
138 	memory = allocator.allocate(getImageMemoryRequirements(vk, vkDevice, *image), requirement);
139 	VK_CHECK(vk.bindImageMemory(vkDevice, *image, memory->getMemory(), memory->getOffset()));
140 }
141 
createTestImage(tcu::IVec2 size, VkFormat format, Context& context, Allocator& allocator, Move<VkImage>& image, const MemoryRequirement& requirement, de::MovePtr<Allocation>& memory, VkImageTiling tiling) const142 void ImageDedicatedAllocation::createTestImage							(tcu::IVec2					size,
143 																		 VkFormat					format,
144 																		 Context&					context,
145 																		 Allocator&					allocator,
146 																		 Move<VkImage>&				image,
147 																		 const MemoryRequirement&	requirement,
148 																		 de::MovePtr<Allocation>&	memory,
149 																		 VkImageTiling				tiling) const
150 {
151 	DE_UNREF(allocator);
152 	if (!context.isDeviceFunctionalitySupported("VK_KHR_dedicated_allocation"))
153 		TCU_THROW(NotSupportedError, "Not supported");
154 
155 	const InstanceInterface&			vkInstance						= context.getInstanceInterface();
156 	const VkDevice						vkDevice						= context.getDevice();
157 	const VkPhysicalDevice				vkPhysicalDevice				= context.getPhysicalDevice();
158 	const DeviceInterface&				vk								= context.getDeviceInterface();
159 	const deUint32						queueFamilyIndex				= context.getUniversalQueueFamilyIndex();
160 
161 	const VkImageCreateInfo				colorImageParams				=
162 	{
163 		VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,							// VkStructureType			sType;
164 		DE_NULL,														// const void*				pNext;
165 		0u,																// VkImageCreateFlags		flags;
166 		VK_IMAGE_TYPE_2D,												// VkImageType				imageType;
167 		format,															// VkFormat					format;
168 		{ (deUint32)size.x(), (deUint32)size.y(), 1u },					// VkExtent3D				extent;
169 		1u,																// deUint32					mipLevels;
170 		1u,																// deUint32					arraySize;
171 		VK_SAMPLE_COUNT_1_BIT,											// deUint32					samples;
172 		tiling,															// VkImageTiling			tiling;
173 		(vk::VkImageUsageFlags)((tiling == VK_IMAGE_TILING_LINEAR) ? VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT :
174 		VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT), // VkImageUsageFlags	usage;
175 		VK_SHARING_MODE_EXCLUSIVE,										// VkSharingMode			sharingMode;
176 		1u,																// deUint32					queueFamilyCount;
177 		&queueFamilyIndex,												// const deUint32*			pQueueFamilyIndices;
178 		VK_IMAGE_LAYOUT_UNDEFINED,										// VkImageLayout			initialLayout;
179 	};
180 
181 	image = createImage(vk, vkDevice, &colorImageParams);
182 	memory = allocateDedicated(vkInstance, vk, vkPhysicalDevice, vkDevice, image.get(), requirement);
183 	VK_CHECK(vk.bindImageMemory(vkDevice, *image, memory->getMemory(), memory->getOffset()));
184 }
185 
186 } // api
187 } // vkt
188