1 #ifndef MSP_GL_VULKAN_MEMORYALLOCATOR_H_
2 #define MSP_GL_VULKAN_MEMORYALLOCATOR_H_
5 #include <msp/core/noncopyable.h>
6 #include <msp/graphics/vulkancontext.h>
22 class MemoryAllocator: public NonCopyable
34 MemoryType type = UNKNOWN_MEMORY;
35 std::vector<unsigned> free_blocks;
36 int largest_free_buffer = -1;
37 int largest_free_image = -1;
38 bool can_consolidate = false;
45 VkDeviceMemory memory = 0;
47 void *mapped_address = 0;
48 unsigned map_count = 0;
54 bool allocated = false;
55 BlockType type = UNDECIDED;
56 std::size_t offset = 0;
63 VkPhysicalDevice phys_device;
64 std::size_t total_device_memory = 0;
65 std::size_t default_region_size = 0;
66 std::size_t direct_alloc_threshold = 0;
67 std::size_t min_alignment = 256;
68 std::size_t buffer_image_granularity = 131072;
69 std::vector<Pool> pools;
70 std::vector<Region> regions;
71 std::vector<Block> blocks;
74 MemoryAllocator(Device &);
78 unsigned find_memory_pool(unsigned, MemoryType) const;
79 unsigned create_region(unsigned, size_t, bool);
80 std::vector<unsigned>::iterator lower_bound_by_size(std::vector<unsigned> &, std::size_t) const;
81 std::size_t get_alloc_offset(const Block &, std::size_t, std::size_t, BlockType) const;
82 void update_largest_free(Pool &);
83 unsigned allocate(std::size_t, std::size_t, unsigned, MemoryType, BlockType);
84 unsigned split_block(unsigned, std::size_t);
85 void consolidate(unsigned);
86 void merge_block_with_next(unsigned);
89 unsigned allocate(VkBuffer, MemoryType);
90 unsigned allocate(VkImage, MemoryType);
91 void release(unsigned);
96 std::string get_debug() const;