1#ifndef NNGN_GRAPHICS_VULKAN_MEMORY_H
2#define NNGN_GRAPHICS_VULKAN_MEMORY_H
23 static constexpr bool required(VkMemoryPropertyFlags
f);
24 template<VkMemoryPropertyFlags f>
29 bool init(VkInstance inst, VkPhysicalDevice physical_dev, VkDevice
dev);
36 template<VkMemoryPropertyFlags
f>
39 template<VkMemoryPropertyFlags
f>
45 template<VkMemoryPropertyFlags
f>
50 template<VkMemoryPropertyFlags
f>
56 template<VkMemoryPropertyFlags
f>
61 template<VkMemoryPropertyFlags
f>
67 template<VkMemoryPropertyFlags
f>
80 std::uint32_t type, const VkMemoryRequirements *req,
84 VkPhysicalDeviceMemoryProperties
props = {};
89 std::vector<Graphics::MemoryHeap>
heaps = {};
91 void init(VkPhysicalDevice dev);
95 constexpr auto device = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT;
96 constexpr auto host = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
97 | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
98 return f & device || (
f & host) == host;
101template<VkMemoryPropertyFlags f>
103 std::uint32_t type, std::uint32_t *p
105 constexpr auto ret = []([[maybe_unused]]
auto b) {
109 for(std::uint32_t i = 0; i < this->props.memoryTypeCount; ++i) {
110 if(!(type & (std::uint32_t{1} << i)))
112 if((this->props.memoryTypes[i].propertyFlags &
f) !=
f)
118 Log::l() <<
"no suitable type found\n";
122template<VkMemoryPropertyFlags f>
126 std::uint32_t type = {};
128 this->find_type<f>(req->memoryTypeBits, &type);
129 else if(!this->find_type<f>(req->memoryTypeBits, &type))
131 return this->
alloc(type, req->size, p);
134template<VkMemoryPropertyFlags f>
139 std::uint32_t type = {};
141 this->find_type<f>(req->memoryTypeBits, &type);
142 else if(!this->find_type<f>(req->memoryTypeBits, &type))
144 return this->
alloc(type, req,
alloc, mem, offset);
147template<VkMemoryPropertyFlags f>
149 VkMemoryRequirements req;
150 vkGetBufferMemoryRequirements(this->
dev, b, &req);
151 return this->alloc<f>(&req, p);
154template<VkMemoryPropertyFlags f>
159 VkMemoryRequirements req;
160 vkGetBufferMemoryRequirements(this->
dev, b, &req);
161 return this->alloc<f>(&req,
alloc, mem, offset);
164template<VkMemoryPropertyFlags f>
166 VkMemoryRequirements req;
167 vkGetImageMemoryRequirements(this->
dev, img, &req);
168 return this->alloc<f>(&req, p);
171template<VkMemoryPropertyFlags f>
176 VkMemoryRequirements req;
177 vkGetImageMemoryRequirements(this->
dev, img, &req);
178 return this->alloc<f>(&req,
alloc, mem, offset);
Manages device memory queries, allocations, and lifetime.
Definition: memory.h:20
bool init(VkInstance inst, VkPhysicalDevice physical_dev, VkDevice dev)
Definition: memory.cpp:125
void dealloc(VkDeviceMemory mem)
Deallocates a block of device memory.
Definition: memory.cpp:155
static constexpr bool required(VkMemoryPropertyFlags f)
Indicates whether a memory type is guaranteed to exist.
Definition: memory.h:94
bool alloc(const VkMemoryRequirements *req, VkDeviceMemory *p)
Allocates device memory from a type that fulfills req.
Definition: memory.h:123
std::conditional_t< required(f), void, bool > find_ret
Definition: memory.h:25
void * allocator
Definition: memory.h:83
VkDevice dev
Definition: memory.h:82
VkPhysicalDeviceMemoryProperties props
Definition: memory.h:84
find_ret< f > find_type(std::uint32_t type, std::uint32_t *p) const
Finds a memory type among those offered by the device.
static std::ostream & l()
Definition: log.cpp:56
#define NNGN_LOG_CONTEXT_CF(c)
Definition: log.h:11
COMMAND_BUFFER DESCRIPTOR_POOL DESCRIPTOR_SET_LAYOUT FENCE VkImage
Definition: types.h:71
COMMAND_BUFFER DESCRIPTOR_POOL DESCRIPTOR_SET_LAYOUT FENCE IMAGE PIPELINE PIPELINE_CACHE RENDER_PASS SEMAPHORE VkBuffer
Definition: types.h:77
std::uint64_t MemoryAllocation
Definition: memory.h:17
COMMAND_BUFFER DESCRIPTOR_POOL DESCRIPTOR_SET_LAYOUT FENCE IMAGE PIPELINE PIPELINE_CACHE RENDER_PASS SEMAPHORE BUFFER COMMAND_POOL DESCRIPTOR_SET VkDeviceMemory
Definition: types.h:77
Aggregate type for information about a device's memory heaps.
Definition: memory.h:88
std::vector< Graphics::MemoryHeap > heaps
Definition: memory.h:89
std::vector< std::vector< Graphics::MemoryType > > memory_types
Definition: memory.h:90
void init(VkPhysicalDevice dev)
Definition: memory.cpp:160
#define NNGN_MOVE_ONLY(x)
Definition: utils.h:39