]> git.tdb.fi Git - libs/gl.git/blobdiff - source/backends/vulkan/memoryallocator.h
Initial implementation of Vulkan backend
[libs/gl.git] / source / backends / vulkan / memoryallocator.h
diff --git a/source/backends/vulkan/memoryallocator.h b/source/backends/vulkan/memoryallocator.h
new file mode 100644 (file)
index 0000000..b7603f1
--- /dev/null
@@ -0,0 +1,59 @@
+#ifndef MSP_GL_VULKAN_MEMORYALLOCATOR_H_
+#define MSP_GL_VULKAN_MEMORYALLOCATOR_H_
+
+#include <vector>
+#include <msp/graphics/vulkancontext.h>
+#include "handles.h"
+
+namespace Msp {
+namespace GL {
+
+class Device;
+
+enum MemoryType
+{
+       UNKNOWN_MEMORY,
+       DEVICE_MEMORY,
+       STAGING_MEMORY,
+       STREAMING_MEMORY
+};
+
+class MemoryAllocator
+{
+private:
+       struct Allocation
+       {
+               VkDeviceMemory memory = 0;
+               MemoryType type = UNKNOWN_MEMORY;
+               std::size_t size = 0;
+               void *mapped_address = 0;
+       };
+
+       Device &device;
+       VkPhysicalDevice phys_device;
+       std::vector<MemoryType> memory_types;
+       std::vector<Allocation> allocations;
+
+public:
+       MemoryAllocator(Device &);
+
+private:
+       unsigned find_memory_type_index(unsigned, MemoryType);
+       unsigned allocate(std::size_t, unsigned, MemoryType);
+       Allocation &get_allocation(unsigned);
+       const Allocation &get_allocation(unsigned) const;
+
+public:
+       unsigned allocate(VkBuffer, MemoryType);
+       void release(unsigned);
+
+       std::size_t get_allocation_size(unsigned) const;
+
+       void *map(unsigned, std::size_t, std::size_t);
+       void unmap(void *);
+};
+
+} // namespace GL
+} // namespace Msp
+
+#endif