xref: /aosp_15_r20/external/deqp/external/vulkancts/framework/vulkan/vkBufferWithMemory.hpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
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, const vk::VkDevice device, vk::Allocator &allocator,
40                      const vk::VkBufferCreateInfo &bufferCreateInfo, const vk::MemoryRequirement memoryRequirement,
41                      const bool bindOnCreation = true)
42 
43         : m_vk(vk)
44         , m_device(device)
45         , m_buffer(createBuffer(vk, device, &bufferCreateInfo))
46         , m_allocation(allocator.allocate(getBufferMemoryRequirements(vk, device, *m_buffer), memoryRequirement))
47         , m_memoryBound(false)
48     {
49         if (bindOnCreation)
50             bindMemory();
51     }
52 
get(void) const53     const vk::VkBuffer &get(void) const
54     {
55         return *m_buffer;
56     }
operator *(void) const57     const vk::VkBuffer &operator*(void) const
58     {
59         return get();
60     }
getAllocation(void) const61     vk::Allocation &getAllocation(void) const
62     {
63         return *m_allocation;
64     }
bindMemory(void)65     void bindMemory(void)
66     {
67         if (!m_memoryBound)
68         {
69             VK_CHECK(m_vk.bindBufferMemory(m_device, *m_buffer, m_allocation->getMemory(), m_allocation->getOffset()));
70             m_memoryBound = true;
71         }
72     }
73 
74 private:
75     const vk::DeviceInterface &m_vk;
76     const vk::VkDevice m_device;
77     const vk::Unique<vk::VkBuffer> m_buffer;
78     const de::UniquePtr<vk::Allocation> m_allocation;
79     bool m_memoryBound;
80 
81     // "deleted"
82     BufferWithMemory(const BufferWithMemory &);
83     BufferWithMemory operator=(const BufferWithMemory &);
84 };
85 } // namespace vk
86 
87 #endif // _VKBUFFERWITHMEMORY_HPP
88