• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-------------------------------------------------------------------------
2  * Vulkan CTS Framework
3  * --------------------
4  *
5  * Copyright (c) 2015 Google 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 Memory management utilities.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "vkMemUtil.hpp"
25 #include "vkStrUtil.hpp"
26 #include "vkQueryUtil.hpp"
27 #include "vkRef.hpp"
28 #include "vkRefUtil.hpp"
29 #include "deInt32.h"
30 
31 #include <sstream>
32 
33 namespace vk
34 {
35 
36 using de::UniquePtr;
37 using de::MovePtr;
38 
39 namespace
40 {
41 
42 class HostPtr
43 {
44 public:
45 								HostPtr		(const DeviceInterface& vkd, VkDevice device, VkDeviceMemory memory, VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags);
46 								~HostPtr	(void);
47 
get(void) const48 	void*						get			(void) const { return m_ptr; }
49 
50 private:
51 	const DeviceInterface&		m_vkd;
52 	const VkDevice				m_device;
53 	const VkDeviceMemory		m_memory;
54 	void* const					m_ptr;
55 };
56 
HostPtr(const DeviceInterface & vkd,VkDevice device,VkDeviceMemory memory,VkDeviceSize offset,VkDeviceSize size,VkMemoryMapFlags flags)57 HostPtr::HostPtr (const DeviceInterface& vkd, VkDevice device, VkDeviceMemory memory, VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags)
58 	: m_vkd		(vkd)
59 	, m_device	(device)
60 	, m_memory	(memory)
61 	, m_ptr		(mapMemory(vkd, device, memory, offset, size, flags))
62 {
63 }
64 
~HostPtr(void)65 HostPtr::~HostPtr (void)
66 {
67 	m_vkd.unmapMemory(m_device, m_memory);
68 }
69 
selectMatchingMemoryType(const VkPhysicalDeviceMemoryProperties & deviceMemProps,deUint32 allowedMemTypeBits,MemoryRequirement requirement)70 deUint32 selectMatchingMemoryType (const VkPhysicalDeviceMemoryProperties& deviceMemProps, deUint32 allowedMemTypeBits, MemoryRequirement requirement)
71 {
72 	const deUint32	compatibleTypes	= getCompatibleMemoryTypes(deviceMemProps, requirement);
73 	const deUint32	candidates		= allowedMemTypeBits & compatibleTypes;
74 
75 	if (candidates == 0)
76 		TCU_THROW(NotSupportedError, "No compatible memory type found");
77 
78 	return (deUint32)deCtz32(candidates);
79 }
80 
isHostVisibleMemory(const VkPhysicalDeviceMemoryProperties & deviceMemProps,deUint32 memoryTypeNdx)81 bool isHostVisibleMemory (const VkPhysicalDeviceMemoryProperties& deviceMemProps, deUint32 memoryTypeNdx)
82 {
83 	DE_ASSERT(memoryTypeNdx < deviceMemProps.memoryTypeCount);
84 	return (deviceMemProps.memoryTypes[memoryTypeNdx].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) != 0u;
85 }
86 
87 } // anonymous
88 
89 // Allocation
90 
Allocation(VkDeviceMemory memory,VkDeviceSize offset,void * hostPtr)91 Allocation::Allocation (VkDeviceMemory memory, VkDeviceSize offset, void* hostPtr)
92 	: m_memory	(memory)
93 	, m_offset	(offset)
94 	, m_hostPtr	(hostPtr)
95 {
96 }
97 
~Allocation(void)98 Allocation::~Allocation (void)
99 {
100 }
101 
102 // MemoryRequirement
103 
104 const MemoryRequirement MemoryRequirement::Any				= MemoryRequirement(0x0u);
105 const MemoryRequirement MemoryRequirement::HostVisible		= MemoryRequirement(MemoryRequirement::FLAG_HOST_VISIBLE);
106 const MemoryRequirement MemoryRequirement::Coherent			= MemoryRequirement(MemoryRequirement::FLAG_COHERENT);
107 const MemoryRequirement MemoryRequirement::LazilyAllocated	= MemoryRequirement(MemoryRequirement::FLAG_LAZY_ALLOCATION);
108 const MemoryRequirement MemoryRequirement::Protected		= MemoryRequirement(MemoryRequirement::FLAG_PROTECTED);
109 
matchesHeap(VkMemoryPropertyFlags heapFlags) const110 bool MemoryRequirement::matchesHeap (VkMemoryPropertyFlags heapFlags) const
111 {
112 	// sanity check
113 	if ((m_flags & FLAG_COHERENT) && !(m_flags & FLAG_HOST_VISIBLE))
114 		DE_FATAL("Coherent memory must be host-visible");
115 	if ((m_flags & FLAG_HOST_VISIBLE) && (m_flags & FLAG_LAZY_ALLOCATION))
116 		DE_FATAL("Lazily allocated memory cannot be mappable");
117 	if ((m_flags & FLAG_PROTECTED) && (m_flags & FLAG_HOST_VISIBLE))
118 		DE_FATAL("Protected memory cannot be mappable");
119 
120 	// host-visible
121 	if ((m_flags & FLAG_HOST_VISIBLE) && !(heapFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT))
122 		return false;
123 
124 	// coherent
125 	if ((m_flags & FLAG_COHERENT) && !(heapFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))
126 		return false;
127 
128 	// lazy
129 	if ((m_flags & FLAG_LAZY_ALLOCATION) && !(heapFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT))
130 		return false;
131 
132 	// protected
133 	if ((m_flags & FLAG_PROTECTED) && !(heapFlags & VK_MEMORY_PROPERTY_PROTECTED_BIT))
134 		return false;
135 
136 	return true;
137 }
138 
MemoryRequirement(deUint32 flags)139 MemoryRequirement::MemoryRequirement (deUint32 flags)
140 	: m_flags(flags)
141 {
142 }
143 
144 // SimpleAllocator
145 
146 class SimpleAllocation : public Allocation
147 {
148 public:
149 									SimpleAllocation	(Move<VkDeviceMemory> mem, MovePtr<HostPtr> hostPtr);
150 	virtual							~SimpleAllocation	(void);
151 
152 private:
153 	const Unique<VkDeviceMemory>	m_memHolder;
154 	const UniquePtr<HostPtr>		m_hostPtr;
155 };
156 
SimpleAllocation(Move<VkDeviceMemory> mem,MovePtr<HostPtr> hostPtr)157 SimpleAllocation::SimpleAllocation (Move<VkDeviceMemory> mem, MovePtr<HostPtr> hostPtr)
158 	: Allocation	(*mem, (VkDeviceSize)0, hostPtr ? hostPtr->get() : DE_NULL)
159 	, m_memHolder	(mem)
160 	, m_hostPtr		(hostPtr)
161 {
162 }
163 
~SimpleAllocation(void)164 SimpleAllocation::~SimpleAllocation (void)
165 {
166 }
167 
SimpleAllocator(const DeviceInterface & vk,VkDevice device,const VkPhysicalDeviceMemoryProperties & deviceMemProps)168 SimpleAllocator::SimpleAllocator (const DeviceInterface& vk, VkDevice device, const VkPhysicalDeviceMemoryProperties& deviceMemProps)
169 	: m_vk		(vk)
170 	, m_device	(device)
171 	, m_memProps(deviceMemProps)
172 {
173 }
174 
allocate(const VkMemoryAllocateInfo & allocInfo,VkDeviceSize alignment)175 MovePtr<Allocation> SimpleAllocator::allocate (const VkMemoryAllocateInfo& allocInfo, VkDeviceSize alignment)
176 {
177 	DE_UNREF(alignment);
178 
179 	Move<VkDeviceMemory>	mem		= allocateMemory(m_vk, m_device, &allocInfo);
180 	MovePtr<HostPtr>		hostPtr;
181 
182 	if (isHostVisibleMemory(m_memProps, allocInfo.memoryTypeIndex))
183 		hostPtr = MovePtr<HostPtr>(new HostPtr(m_vk, m_device, *mem, 0u, allocInfo.allocationSize, 0u));
184 
185 	return MovePtr<Allocation>(new SimpleAllocation(mem, hostPtr));
186 }
187 
allocate(const VkMemoryRequirements & memReqs,MemoryRequirement requirement)188 MovePtr<Allocation> SimpleAllocator::allocate (const VkMemoryRequirements& memReqs, MemoryRequirement requirement)
189 {
190 	const deUint32				memoryTypeNdx	= selectMatchingMemoryType(m_memProps, memReqs.memoryTypeBits, requirement);
191 	const VkMemoryAllocateInfo	allocInfo		=
192 	{
193 		VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,	//	VkStructureType			sType;
194 		DE_NULL,								//	const void*				pNext;
195 		memReqs.size,							//	VkDeviceSize			allocationSize;
196 		memoryTypeNdx,							//	deUint32				memoryTypeIndex;
197 	};
198 
199 	Move<VkDeviceMemory>		mem				= allocateMemory(m_vk, m_device, &allocInfo);
200 	MovePtr<HostPtr>			hostPtr;
201 
202 	if (requirement & MemoryRequirement::HostVisible)
203 	{
204 		DE_ASSERT(isHostVisibleMemory(m_memProps, allocInfo.memoryTypeIndex));
205 		hostPtr = MovePtr<HostPtr>(new HostPtr(m_vk, m_device, *mem, 0u, allocInfo.allocationSize, 0u));
206 	}
207 
208 	return MovePtr<Allocation>(new SimpleAllocation(mem, hostPtr));
209 }
210 
allocateDedicated(const InstanceInterface & vki,const DeviceInterface & vkd,const VkPhysicalDevice & physDevice,const VkDevice device,const VkMemoryRequirements & memReqs,const MemoryRequirement requirement,const void * pNext)211 static MovePtr<Allocation> allocateDedicated (const InstanceInterface&		vki,
212 											  const DeviceInterface&		vkd,
213 											  const VkPhysicalDevice&		physDevice,
214 											  const VkDevice				device,
215 											  const VkMemoryRequirements&	memReqs,
216 											  const MemoryRequirement		requirement,
217 											  const void*					pNext)
218 {
219 	const VkPhysicalDeviceMemoryProperties	memoryProperties	= getPhysicalDeviceMemoryProperties(vki, physDevice);
220 	const deUint32							memoryTypeNdx		= selectMatchingMemoryType(memoryProperties, memReqs.memoryTypeBits, requirement);
221 	const VkMemoryAllocateInfo				allocInfo			=
222 	{
223 		VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,	//	VkStructureType	sType
224 		pNext,									//	const void*		pNext
225 		memReqs.size,							//	VkDeviceSize	allocationSize
226 		memoryTypeNdx,							//	deUint32		memoryTypeIndex
227 	};
228 	Move<VkDeviceMemory>					mem					= allocateMemory(vkd, device, &allocInfo);
229 	MovePtr<HostPtr>						hostPtr;
230 
231 	if (requirement & MemoryRequirement::HostVisible)
232 	{
233 		DE_ASSERT(isHostVisibleMemory(memoryProperties, allocInfo.memoryTypeIndex));
234 		hostPtr = MovePtr<HostPtr>(new HostPtr(vkd, device, *mem, 0u, allocInfo.allocationSize, 0u));
235 	}
236 
237 	return MovePtr<Allocation>(new SimpleAllocation(mem, hostPtr));
238 }
239 
allocateDedicated(const InstanceInterface & vki,const DeviceInterface & vkd,const VkPhysicalDevice & physDevice,const VkDevice device,const VkBuffer buffer,MemoryRequirement requirement)240 de::MovePtr<Allocation> allocateDedicated (const InstanceInterface&	vki,
241 										   const DeviceInterface&	vkd,
242 										   const VkPhysicalDevice&	physDevice,
243 										   const VkDevice			device,
244 										   const VkBuffer			buffer,
245 										   MemoryRequirement		requirement)
246 {
247 	const VkMemoryRequirements				memoryRequirements		= getBufferMemoryRequirements(vkd, device, buffer);
248 	const VkMemoryDedicatedAllocateInfo		dedicatedAllocationInfo	=
249 	{
250 		VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR,				// VkStructureType		sType
251 		DE_NULL,															// const void*			pNext
252 		DE_NULL,															// VkImage				image
253 		buffer																// VkBuffer				buffer
254 	};
255 
256 	return allocateDedicated(vki, vkd, physDevice, device, memoryRequirements, requirement, &dedicatedAllocationInfo);
257 }
258 
allocateDedicated(const InstanceInterface & vki,const DeviceInterface & vkd,const VkPhysicalDevice & physDevice,const VkDevice device,const VkImage image,MemoryRequirement requirement)259 de::MovePtr<Allocation> allocateDedicated (const InstanceInterface&	vki,
260 										   const DeviceInterface&	vkd,
261 										   const VkPhysicalDevice&	physDevice,
262 										   const VkDevice			device,
263 										   const VkImage			image,
264 										   MemoryRequirement		requirement)
265 {
266 	const VkMemoryRequirements				memoryRequirements		= getImageMemoryRequirements(vkd, device, image);
267 	const VkMemoryDedicatedAllocateInfo		dedicatedAllocationInfo	=
268 	{
269 		VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR,			// VkStructureType		sType
270 		DE_NULL,														// const void*			pNext
271 		image,															// VkImage				image
272 		DE_NULL															// VkBuffer				buffer
273 	};
274 
275 	return allocateDedicated(vki, vkd, physDevice, device, memoryRequirements, requirement, &dedicatedAllocationInfo);
276 }
277 
mapMemory(const DeviceInterface & vkd,VkDevice device,VkDeviceMemory mem,VkDeviceSize offset,VkDeviceSize size,VkMemoryMapFlags flags)278 void* mapMemory (const DeviceInterface& vkd, VkDevice device, VkDeviceMemory mem, VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags)
279 {
280 	void* hostPtr = DE_NULL;
281 	VK_CHECK(vkd.mapMemory(device, mem, offset, size, flags, &hostPtr));
282 	TCU_CHECK(hostPtr);
283 	return hostPtr;
284 }
285 
flushMappedMemoryRange(const DeviceInterface & vkd,VkDevice device,VkDeviceMemory memory,VkDeviceSize offset,VkDeviceSize size)286 void flushMappedMemoryRange (const DeviceInterface& vkd, VkDevice device, VkDeviceMemory memory, VkDeviceSize offset, VkDeviceSize size)
287 {
288 	const VkMappedMemoryRange	range	=
289 	{
290 		VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
291 		DE_NULL,
292 		memory,
293 		offset,
294 		size
295 	};
296 
297 	VK_CHECK(vkd.flushMappedMemoryRanges(device, 1u, &range));
298 }
299 
invalidateMappedMemoryRange(const DeviceInterface & vkd,VkDevice device,VkDeviceMemory memory,VkDeviceSize offset,VkDeviceSize size)300 void invalidateMappedMemoryRange (const DeviceInterface& vkd, VkDevice device, VkDeviceMemory memory, VkDeviceSize offset, VkDeviceSize size)
301 {
302 	const VkMappedMemoryRange	range	=
303 	{
304 		VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
305 		DE_NULL,
306 		memory,
307 		offset,
308 		size
309 	};
310 
311 	VK_CHECK(vkd.invalidateMappedMemoryRanges(device, 1u, &range));
312 }
313 
getCompatibleMemoryTypes(const VkPhysicalDeviceMemoryProperties & deviceMemProps,MemoryRequirement requirement)314 deUint32 getCompatibleMemoryTypes (const VkPhysicalDeviceMemoryProperties& deviceMemProps, MemoryRequirement requirement)
315 {
316 	deUint32	compatibleTypes	= 0u;
317 
318 	for (deUint32 memoryTypeNdx = 0; memoryTypeNdx < deviceMemProps.memoryTypeCount; memoryTypeNdx++)
319 	{
320 		if (requirement.matchesHeap(deviceMemProps.memoryTypes[memoryTypeNdx].propertyFlags))
321 			compatibleTypes |= (1u << memoryTypeNdx);
322 	}
323 
324 	return compatibleTypes;
325 }
326 
bindImagePlaneMemory(const DeviceInterface & vkd,VkDevice device,VkImage image,VkDeviceMemory memory,VkDeviceSize memoryOffset,VkImageAspectFlagBits planeAspect)327 void bindImagePlaneMemory (const DeviceInterface&	vkd,
328 						   VkDevice					device,
329 						   VkImage					image,
330 						   VkDeviceMemory			memory,
331 						   VkDeviceSize				memoryOffset,
332 						   VkImageAspectFlagBits	planeAspect)
333 {
334 	const VkBindImagePlaneMemoryInfo	planeInfo	=
335 	{
336 		VK_STRUCTURE_TYPE_BIND_IMAGE_PLANE_MEMORY_INFO_KHR,
337 		DE_NULL,
338 		planeAspect
339 	};
340 	const VkBindImageMemoryInfo			coreInfo	=
341 	{
342 		VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO_KHR,
343 		&planeInfo,
344 		image,
345 		memory,
346 		memoryOffset,
347 	};
348 
349 	VK_CHECK(vkd.bindImageMemory2(device, 1u, &coreInfo));
350 }
351 
352 } // vk
353