92 lines
2.6 KiB
C++
92 lines
2.6 KiB
C++
#pragma once
|
|
|
|
#include "forward.hpp"
|
|
#include <vector>
|
|
#include <optional>
|
|
#include <memory>
|
|
|
|
namespace tracy {
|
|
class VkCtx;
|
|
}
|
|
typedef tracy::VkCtx* TracyVkCtx;
|
|
namespace render::vk {
|
|
|
|
class Allocator {
|
|
private:
|
|
struct Allocation;
|
|
public:
|
|
Allocator(VkDevice, const PhysicalDeviceInfo&);
|
|
~Allocator();
|
|
|
|
struct memory_area {
|
|
VkDeviceMemory ref;
|
|
VkDeviceSize size;
|
|
VkDeviceSize offset;
|
|
void *ptr = nullptr;
|
|
|
|
void write(const void*, VkDeviceSize size, VkDeviceSize offset = 0);
|
|
void read(void*, VkDeviceSize size, VkDeviceSize offset = 0);
|
|
};
|
|
|
|
class MemoryDeleter {
|
|
public:
|
|
MemoryDeleter(Allocation *owner): owner(owner) { }
|
|
void operator()(memory_area *);
|
|
private:
|
|
Allocation *owner;
|
|
};
|
|
|
|
using memory_ptr = std::unique_ptr<memory_area, MemoryDeleter>;
|
|
|
|
memory_ptr allocate(VkMemoryRequirements, VkMemoryPropertyFlags);
|
|
bool deallocate(const memory_area&);
|
|
|
|
struct buffer_info {
|
|
VkBuffer buffer = nullptr;
|
|
};
|
|
memory_ptr createBuffer(VkDeviceSize, VkMemoryPropertyFlags, VkBufferUsageFlags, buffer_info&);
|
|
struct buffer_requirement {
|
|
VkDeviceSize size;
|
|
VkBufferUsageFlags usage;
|
|
};
|
|
memory_ptr createBuffers(const std::vector<buffer_requirement> &, VkMemoryPropertyFlags, std::vector<buffer_info> &);
|
|
//TODO: create Buffer{MemoryArea + VkBuffer}
|
|
//TODO: create readonly buffer with data
|
|
|
|
void copyBuffer(buffer_info srcBuffer, buffer_info dstBuffer, VkDeviceSize size);
|
|
|
|
std::pair<VkBuffer, VkBuffer> getBuffer() { return {vertexBuffer.buffer, indexBuffer.buffer}; }
|
|
void setTracyZone(const char* name);
|
|
|
|
private:
|
|
std::optional<uint32_t> findMemory(uint32_t, VkMemoryPropertyFlags, VkDeviceSize size = 0) const;
|
|
|
|
struct Allocation {
|
|
Allocation(VkDevice, VkDeviceMemory, VkDeviceSize, uint32_t, void *ptr);
|
|
~Allocation();
|
|
|
|
const VkDevice device;
|
|
const VkDeviceMemory memory;
|
|
const VkDeviceSize size;
|
|
const uint32_t memoryType;
|
|
const void *ptr = nullptr;
|
|
const MemoryDeleter deleter;
|
|
|
|
struct area { VkDeviceSize size; VkDeviceSize offset; };
|
|
std::vector<area> areas;
|
|
};
|
|
|
|
VkDevice device;
|
|
VkPhysicalDeviceMemoryProperties properties;
|
|
|
|
VkQueue transferQueue;
|
|
VkCommandPool transferPool;
|
|
VkCommandBuffer transferBuffer; // MAYBE: parallel upload
|
|
TracyVkCtx tracyCtx;
|
|
|
|
buffer_info vertexBuffer;
|
|
buffer_info indexBuffer;
|
|
memory_ptr indexedBufferMemory;
|
|
std::vector<std::unique_ptr<Allocation>> allocations;
|
|
};
|
|
} |