/
memory.hpp
176 lines (148 loc) · 4.21 KB
/
memory.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* @file liblava/base/memory.hpp
* @brief Vulkan allocator
* @authors Lava Block OÜ and contributors
* @copyright Copyright (c) 2018-present, MIT License
*/
#pragma once
// clang-format off
#include "liblava/base/base.hpp"
#include "liblava/fwd.hpp"
#include "vk_mem_alloc.h"
#include <memory>
// clang-format on
namespace lava {
/**
* @brief Vulkan allocator
*/
struct allocator {
/// Shared pointer to a allocator
using s_ptr = std::shared_ptr<allocator>;
/// Const pointer to device
using device_c_ptr = device const*;
/**
* @brief Make a new allocator
* @return s_ptr Shared pointer to allocator
*/
static s_ptr make() {
return std::make_shared<allocator>();
}
/**
* @brief Construct a new allocator
*/
allocator() = default;
/**
* @brief Construct a new allocator
* @param allocator VMA allocator
*/
explicit allocator(VmaAllocator allocator)
: vma_allocator(allocator) {}
/**
* @brief Create a new allocator
* @param device Vulkan device
* @param flags VMA allocator create flags
* @return Create was successful or failed
*/
bool create(device_c_ptr device,
VmaAllocatorCreateFlags flags = 0);
/**
* @brief Destroy the allocator
*/
void destroy();
/**
* @brief Check if allocator is valid
* @return Allocator is valid or not
*/
bool valid() const {
return vma_allocator != nullptr;
}
/**
* @brief Get the VMA allocator
* @return VmaAllocator VMA allocator
*/
VmaAllocator get() const {
return vma_allocator;
}
private:
/// VMA allocator
VmaAllocator vma_allocator = nullptr;
};
/**
* @brief Create a allocator
* @param device Vulkan device
* @param flags VMA allocator create flags
* @return allocator::s_ptr Allocator
*/
inline allocator::s_ptr create_allocator(allocator::device_c_ptr device,
VmaAllocatorCreateFlags flags = 0) {
auto result = allocator::make();
if (!result->create(device, flags))
return nullptr;
return result;
}
/**
* @brief Vulkan memory
*/
struct memory : no_copy_no_move {
/**
* @brief Construct a new memory
*/
memory();
/**
* @brief Get memory instance
* @return memory& Memory
*/
static memory& instance() {
static memory memory;
return memory;
}
/**
* @brief Get allocation callback
* @return VkAllocationCallbacks* Allocation callbacks
*/
VkAllocationCallbacks* alloc() {
if (use_custom_cpu_callbacks)
return &vk_callbacks;
return nullptr;
}
/**
* @brief Set the callbacks object
* @param callbacks Allocation Callbacks
*/
void set_callbacks(VkAllocationCallbacks const& callbacks) {
vk_callbacks = callbacks;
}
/**
* @brief Set use custom cpu callbacks
* @param value Value state
*/
void set_use_custom_cpu_callbacks(bool value) {
use_custom_cpu_callbacks = value;
}
private:
/// Use custom cpu callbacks
bool use_custom_cpu_callbacks = true;
/// @see https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VkAllocationCallbacks.html
VkAllocationCallbacks vk_callbacks = {};
};
/**
* @brief Find the memory type with properties
* @param properties Physical device memory properties
* @param type_bits Type bits
* @param required_properties Memory property flags
* @return type Result type
*/
index find_memory_type_with_properties(VkPhysicalDeviceMemoryProperties properties,
ui32 type_bits,
VkMemoryPropertyFlags required_properties);
/**
* @brief Find the memory type
* @param gpu Physical device
* @param properties Memory properties flags
* @param type_bits Type bits
* @return type Result type
*/
index find_memory_type(VkPhysicalDevice gpu,
VkMemoryPropertyFlags properties,
ui32 type_bits);
} // namespace lava