Overte C++ Documentation
Allocation.h
1 //
2 // Created by Bradley Austin Davis on 2018/10/29
3 // Adapted for Vulkan in 2022-2025 by dr Karol Suprynowicz.
4 // Copyright 2013-2018 High Fidelity, Inc.
5 // Copyright 2023-2025 Overte e.V.
6 //
7 // Distributed under the Apache License, Version 2.0.
8 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
9 // SPDX-License-Identifier: Apache-2.0
10 //
11 // Contains parts of Vulkan Samples, Copyright (c) 2018, Sascha Willems, distributed on MIT License.
12 //
13 
14 #pragma once
15 
16 #include "Config.h"
17 #include <cstring>
18 
19 namespace vks {
20 
21  // A wrapper class for an allocation, either an Image or Buffer. Not intended to be used used directly
22  // but only as a base class providing common functionality for the classes below.
23  //
24  // Provides easy to use mechanisms for mapping, unmapping and copying host data to the device memory
25  struct Allocation {
26  static void initAllocator(const VkPhysicalDevice&, const VkDevice&);
27 
28  void* rawmap(size_t offset = 0, VkDeviceSize size = VK_WHOLE_SIZE);
29  void unmap();
30 
31  template <typename T = void>
32  inline T* map(size_t offset = 0, VkDeviceSize size = VK_WHOLE_SIZE) {
33  return (T*)rawmap(offset, size);
34  }
35 
36  inline void copy(size_t size, const void* data, VkDeviceSize offset = 0) const {
37  memcpy((uint8_t*)mapped + offset, data, size);
38  }
39 
40  template<typename T>
41  inline void copy(const T& data, VkDeviceSize offset = 0) const {
42  copy(sizeof(T), &data, offset);
43  }
44 
45  template<typename T>
46  inline void copy(const std::vector<T>& data, VkDeviceSize offset = 0) const {
47  copy(sizeof(T) * data.size(), data.data(), offset);
48  }
49 
50  void flush(VkDeviceSize size, VkDeviceSize offset = 0);
51  void invalidate(VkDeviceSize size, VkDeviceSize offset = 0);
52  virtual void destroy();
53 
54 
55  VkDevice device;
56  VkDeviceSize size{ 0 };
57  VkDeviceSize alignment{ 0 };
58  VkDeviceSize allocSize{ 0 };
59 
60 #if VULKAN_USE_VMA
61  static VmaAllocator& getAllocator();
62 
63  VmaAllocation allocation;
65  VkMemoryPropertyFlags memoryPropertyFlags;
66 #else
67  VkDeviceMemory memory;
68 #endif
69  void* mapped{ nullptr };
70 
71  };
72 }