]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/memoryallocator.h
Initial implementation of Vulkan backend
[libs/gl.git] / source / backends / vulkan / memoryallocator.h
1 #ifndef MSP_GL_VULKAN_MEMORYALLOCATOR_H_
2 #define MSP_GL_VULKAN_MEMORYALLOCATOR_H_
3
4 #include <vector>
5 #include <msp/graphics/vulkancontext.h>
6 #include "handles.h"
7
8 namespace Msp {
9 namespace GL {
10
11 class Device;
12
13 enum MemoryType
14 {
15         UNKNOWN_MEMORY,
16         DEVICE_MEMORY,
17         STAGING_MEMORY,
18         STREAMING_MEMORY
19 };
20
21 class MemoryAllocator
22 {
23 private:
24         struct Allocation
25         {
26                 VkDeviceMemory memory = 0;
27                 MemoryType type = UNKNOWN_MEMORY;
28                 std::size_t size = 0;
29                 void *mapped_address = 0;
30         };
31
32         Device &device;
33         VkPhysicalDevice phys_device;
34         std::vector<MemoryType> memory_types;
35         std::vector<Allocation> allocations;
36
37 public:
38         MemoryAllocator(Device &);
39
40 private:
41         unsigned find_memory_type_index(unsigned, MemoryType);
42         unsigned allocate(std::size_t, unsigned, MemoryType);
43         Allocation &get_allocation(unsigned);
44         const Allocation &get_allocation(unsigned) const;
45
46 public:
47         unsigned allocate(VkBuffer, MemoryType);
48         void release(unsigned);
49
50         std::size_t get_allocation_size(unsigned) const;
51
52         void *map(unsigned, std::size_t, std::size_t);
53         void unmap(void *);
54 };
55
56 } // namespace GL
57 } // namespace Msp
58
59 #endif