1 #ifndef _VKBUFFERWITHMEMORY_HPP
2 #define _VKBUFFERWITHMEMORY_HPP
3 /*-------------------------------------------------------------------------
4  * Vulkan CTS Framework
5  * --------------------
6  *
7  * Copyright (c) 2016 The Khronos Group Inc.
8  * Copyright (c) 2016 Google Inc.
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  *//*!
23  * \file
24  * \brief Buffer backed with memory
25  *//*--------------------------------------------------------------------*/
26 
27 #include "vkDefs.hpp"
28 
29 #include "vkMemUtil.hpp"
30 #include "vkQueryUtil.hpp"
31 #include "vkRef.hpp"
32 #include "vkRefUtil.hpp"
33 
34 namespace vk
35 {
36 class BufferWithMemory
37 {
38 public:
BufferWithMemory(const vk::DeviceInterface& vk, const vk::VkDevice device, vk::Allocator& allocator, const vk::VkBufferCreateInfo& bufferCreateInfo, const vk::MemoryRequirement memoryRequirement, const bool bindOnCreation = true)39 										BufferWithMemory	(const vk::DeviceInterface&		vk,
40 															 const vk::VkDevice				device,
41 															 vk::Allocator&					allocator,
42 															 const vk::VkBufferCreateInfo&	bufferCreateInfo,
43 															 const vk::MemoryRequirement	memoryRequirement,
44 															 const bool						bindOnCreation = true)
45 
46 											: m_vk			(vk)
47 											, m_device		(device)
48 											, m_buffer		(createBuffer(vk, device, &bufferCreateInfo))
49 											, m_allocation	(allocator.allocate(getBufferMemoryRequirements(vk, device, *m_buffer), memoryRequirement))
50 											, m_memoryBound	(false)
51 										{
52 											if (bindOnCreation)
53 												bindMemory();
54 										}
55 
get(void) const56 	const vk::VkBuffer&					get				(void) const { return *m_buffer; }
operator *(void) const57 	const vk::VkBuffer&					operator*		(void) const { return get(); }
getAllocation(void) const58 	vk::Allocation&						getAllocation	(void) const { return *m_allocation; }
bindMemory(void)59 	void								bindMemory		(void)
60 	{
61 		if (!m_memoryBound)
62 		{
63 			VK_CHECK(m_vk.bindBufferMemory(m_device, *m_buffer, m_allocation->getMemory(), m_allocation->getOffset()));
64 			m_memoryBound = true;
65 		}
66 	}
67 
68 private:
69 	const vk::DeviceInterface&			m_vk;
70 	const vk::VkDevice					m_device;
71 	const vk::Unique<vk::VkBuffer>		m_buffer;
72 	const de::UniquePtr<vk::Allocation>	m_allocation;
73 	bool								m_memoryBound;
74 
75 	// "deleted"
76 										BufferWithMemory	(const BufferWithMemory&);
77 	BufferWithMemory					operator=			(const BufferWithMemory&);
78 };
79 } // vk
80 
81 #endif // _VKBUFFERWITHMEMORY_HPP
82