|
17 | 17 | */ |
18 | 18 |
|
19 | 19 | #include "allocated.h" |
| 20 | +#include "common/error.h" |
20 | 21 |
|
21 | 22 | namespace vkb |
22 | 23 | { |
@@ -52,182 +53,5 @@ void shutdown() |
52 | 53 | } |
53 | 54 | } |
54 | 55 |
|
55 | | -AllocatedBase::AllocatedBase(const VmaAllocationCreateInfo &alloc_create_info) : |
56 | | - alloc_create_info(alloc_create_info) |
57 | | -{ |
58 | | -} |
59 | | - |
60 | | -AllocatedBase::AllocatedBase(AllocatedBase &&other) noexcept : |
61 | | - alloc_create_info(std::exchange(other.alloc_create_info, {})), |
62 | | - allocation(std::exchange(other.allocation, {})), |
63 | | - mapped_data(std::exchange(other.mapped_data, {})), |
64 | | - coherent(std::exchange(other.coherent, {})), |
65 | | - persistent(std::exchange(other.persistent, {})) |
66 | | -{ |
67 | | -} |
68 | | - |
69 | | -const uint8_t *AllocatedBase::get_data() const |
70 | | -{ |
71 | | - return mapped_data; |
72 | | -} |
73 | | - |
74 | | -VkDeviceMemory AllocatedBase::get_memory() const |
75 | | -{ |
76 | | - VmaAllocationInfo alloc_info; |
77 | | - vmaGetAllocationInfo(get_memory_allocator(), allocation, &alloc_info); |
78 | | - return alloc_info.deviceMemory; |
79 | | -} |
80 | | - |
81 | | -void AllocatedBase::flush(VkDeviceSize offset, VkDeviceSize size) |
82 | | -{ |
83 | | - if (!coherent) |
84 | | - { |
85 | | - vmaFlushAllocation(get_memory_allocator(), allocation, offset, size); |
86 | | - } |
87 | | -} |
88 | | - |
89 | | -uint8_t *AllocatedBase::map() |
90 | | -{ |
91 | | - if (!persistent && !mapped()) |
92 | | - { |
93 | | - VK_CHECK(vmaMapMemory(get_memory_allocator(), allocation, reinterpret_cast<void **>(&mapped_data))); |
94 | | - assert(mapped_data); |
95 | | - } |
96 | | - return mapped_data; |
97 | | -} |
98 | | - |
99 | | -void AllocatedBase::unmap() |
100 | | -{ |
101 | | - if (!persistent && mapped()) |
102 | | - { |
103 | | - vmaUnmapMemory(get_memory_allocator(), allocation); |
104 | | - mapped_data = nullptr; |
105 | | - } |
106 | | -} |
107 | | - |
108 | | -size_t AllocatedBase::update(const uint8_t *data, size_t size, size_t offset) |
109 | | -{ |
110 | | - if (persistent) |
111 | | - { |
112 | | - std::copy(data, data + size, mapped_data + offset); |
113 | | - flush(); |
114 | | - } |
115 | | - else |
116 | | - { |
117 | | - map(); |
118 | | - std::copy(data, data + size, mapped_data + offset); |
119 | | - flush(); |
120 | | - unmap(); |
121 | | - } |
122 | | - return size; |
123 | | -} |
124 | | - |
125 | | -size_t AllocatedBase::update(void const *data, size_t size, size_t offset) |
126 | | -{ |
127 | | - return update(reinterpret_cast<const uint8_t *>(data), size, offset); |
128 | | -} |
129 | | - |
130 | | -void AllocatedBase::post_create(VmaAllocationInfo const &allocation_info) |
131 | | -{ |
132 | | - VkMemoryPropertyFlags memory_properties; |
133 | | - vmaGetAllocationMemoryProperties(get_memory_allocator(), allocation, &memory_properties); |
134 | | - coherent = (memory_properties & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) == VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; |
135 | | - mapped_data = static_cast<uint8_t *>(allocation_info.pMappedData); |
136 | | - persistent = mapped(); |
137 | | -} |
138 | | - |
139 | | -[[nodiscard]] VkBuffer AllocatedBase::create_buffer(VkBufferCreateInfo const &create_info) |
140 | | -{ |
141 | | - VkBuffer handleResult = VK_NULL_HANDLE; |
142 | | - VmaAllocationInfo allocation_info{}; |
143 | | - |
144 | | - auto result = vmaCreateBuffer( |
145 | | - get_memory_allocator(), |
146 | | - &create_info, |
147 | | - &alloc_create_info, |
148 | | - &handleResult, |
149 | | - &allocation, |
150 | | - &allocation_info); |
151 | | - |
152 | | - if (result != VK_SUCCESS) |
153 | | - { |
154 | | - throw VulkanException{result, "Cannot create Buffer"}; |
155 | | - } |
156 | | - post_create(allocation_info); |
157 | | - return handleResult; |
158 | | -} |
159 | | - |
160 | | -[[nodiscard]] VkImage AllocatedBase::create_image(VkImageCreateInfo const &create_info) |
161 | | -{ |
162 | | - assert(0 < create_info.mipLevels && "Images should have at least one level"); |
163 | | - assert(0 < create_info.arrayLayers && "Images should have at least one layer"); |
164 | | - assert(0 < create_info.usage && "Images should have at least one usage type"); |
165 | | - |
166 | | - VkImage handleResult = VK_NULL_HANDLE; |
167 | | - VmaAllocationInfo allocation_info{}; |
168 | | - |
169 | | -#if 0 |
170 | | - // If the image is an attachment, prefer dedicated memory |
171 | | - constexpr VkImageUsageFlags attachment_only_flags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT; |
172 | | - if (create_info.usage & attachment_only_flags) |
173 | | - { |
174 | | - alloc_create_info.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT; |
175 | | - } |
176 | | - |
177 | | - if (create_info.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) |
178 | | - { |
179 | | - alloc_create_info.preferredFlags |= VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT; |
180 | | - } |
181 | | -#endif |
182 | | - |
183 | | - auto result = vmaCreateImage( |
184 | | - get_memory_allocator(), |
185 | | - &create_info, |
186 | | - &alloc_create_info, |
187 | | - &handleResult, |
188 | | - &allocation, |
189 | | - &allocation_info); |
190 | | - |
191 | | - if (result != VK_SUCCESS) |
192 | | - { |
193 | | - throw VulkanException{result, "Cannot create Image"}; |
194 | | - } |
195 | | - |
196 | | - post_create(allocation_info); |
197 | | - return handleResult; |
198 | | -} |
199 | | - |
200 | | -void AllocatedBase::destroy_buffer(VkBuffer handle) |
201 | | -{ |
202 | | - if (handle != VK_NULL_HANDLE && allocation != VK_NULL_HANDLE) |
203 | | - { |
204 | | - unmap(); |
205 | | - vmaDestroyBuffer(get_memory_allocator(), handle, allocation); |
206 | | - clear(); |
207 | | - } |
208 | | -} |
209 | | - |
210 | | -void AllocatedBase::destroy_image(VkImage image) |
211 | | -{ |
212 | | - if (image != VK_NULL_HANDLE && allocation != VK_NULL_HANDLE) |
213 | | - { |
214 | | - unmap(); |
215 | | - vmaDestroyImage(get_memory_allocator(), image, allocation); |
216 | | - clear(); |
217 | | - } |
218 | | -} |
219 | | - |
220 | | -bool AllocatedBase::mapped() const |
221 | | -{ |
222 | | - return mapped_data != nullptr; |
223 | | -} |
224 | | - |
225 | | -void AllocatedBase::clear() |
226 | | -{ |
227 | | - mapped_data = nullptr; |
228 | | - persistent = false; |
229 | | - alloc_create_info = {}; |
230 | | -} |
231 | | - |
232 | 56 | } // namespace allocated |
233 | 57 | } // namespace vkb |
0 commit comments