This commit is contained in:
Martin Slachta
2026-07-18 14:31:15 +02:00
commit a04f0dc262
3343 changed files with 1140208 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
#pragma once
#include <string>
#include <volk.h>
#include <memory>
#include "GpuAllocation.h"
class Gpu;
/**
* Buffer
* Device memory suballocation with generic data inside.
*/
struct Buffer {
VkBuffer buf;
GpuAllocation allocation;
Buffer() : Buffer(VK_NULL_HANDLE, {}) {
}
Buffer(VkBuffer buffer, GpuAllocation allocation) :
buf(buffer), allocation(allocation) {
}
Buffer(const Buffer&& a) noexcept :
buf(a.buf), allocation(a.allocation) {
}
Buffer(const Buffer& a) noexcept :
buf(a.buf), allocation(a.allocation) {
}
void set_debug_name(const Gpu* gpu, const std::string& name) const;
/* disable copy */
};
@@ -0,0 +1,37 @@
#pragma once
#include "Gpu.hpp"
#include <vector>
/**
* Writes data to a buffer by using a staging buffer
*/
class BufferBusWriter {
private:
const Gpu* m_gpu;
Buffer m_stagingBuffer;
size_t m_busSize;
VkCommandBuffer m_stagingCommandBuffer;
void *m_pData;
size_t m_unflushedSize;
uint32_t m_numWrites;
std::vector<std::pair<Buffer*, VkBufferCopy>> m_writes;
VkFence m_fence;
int create_staging_command_buffer();
int create_staging_buffer(size_t size);
public:
BufferBusWriter(const Gpu* gpu, size_t size);
~BufferBusWriter();
void write(Buffer* pTarget, void *pData, size_t offset, size_t size);
void flush();
void wait();
};
@@ -0,0 +1,41 @@
#pragma once
#include "resources/GpuAllocator.h"
#include "vk_mem_alloc.h"
class DefaultAllocator : public GpuAllocator {
private:
VmaAllocator m_allocator;
public:
explicit DefaultAllocator(Gpu *pGpu);
int create_image(ImageCreateInfo *pImageInfo,
MemoryAllocationInfo *pAllocInfo,
Image *pOut) override;
int create_buffer(BufferCreateInfo *pBufferInfo,
MemoryAllocationInfo *pAllocInfo,
Buffer *pOut) override;
void destroy_buffer(Buffer *pBuffer) override {
}
void destroy_image(Image *pImage) override {
}
inline void map(GpuAllocation& allocation, void **pData) override {
vmaMapMemory(m_allocator, allocation.allocation, pData);
}
inline void unmap(GpuAllocation& allocation) override {
vmaUnmapMemory(m_allocator, allocation.allocation);
}
inline VkResult flush(GpuAllocation& allocation, size_t offset, size_t size) override {
vmaFlushAllocation(m_allocator, allocation.allocation, offset, size);
return vmaInvalidateAllocation(m_allocator, allocation.allocation, offset, size);
}
};
@@ -0,0 +1,10 @@
#pragma once
#include "vk_mem_alloc.h"
/*
* Gpu memory node.
*/
struct GpuAllocation {
VmaAllocation allocation;
};
@@ -0,0 +1,57 @@
#pragma once
#include "Image.hpp"
#include "Buffer.hpp"
#include "GpuAllocation.h"
struct BufferCreateInfo {
size_t size;
VkBufferUsageFlags usage;
bool isExclusive;
};
struct ImageCreateInfo {
VkExtent2D extent;
VkFormat format;
VkImageUsageFlags usage;
VkImageAspectFlags aspectMask;
uint32_t arrayLayers;
uint32_t mipLevels;
};
enum MemoryUsage {
MEMORY_USAGE_AUTO = 0,
MEMORY_USAGE_AUTO_PREFER_DEVICE = 1,
MEMORY_USAGE_AUTO_PREFER_HOST = 2
};
struct MemoryAllocationInfo {
MemoryUsage usage;
VkMemoryPropertyFlags requiredFlags;
};
class Gpu;
/**
* Allocating of memory
*/
class GpuAllocator {
protected:
public:
virtual int create_buffer(BufferCreateInfo *pBufferInfo,
MemoryAllocationInfo *pAllocInfo, Buffer *pOut) = 0;
virtual int create_image(ImageCreateInfo *pImageInfo,
MemoryAllocationInfo *pAllocInfo, Image *pOut) = 0;
virtual void map(GpuAllocation& allocation, void **pData) = 0;
virtual void unmap(GpuAllocation& allocation) = 0;
virtual void destroy_buffer(Buffer *pBuffer) = 0;
virtual void destroy_image(Image *pImage) = 0;
virtual VkResult flush(GpuAllocation& allocation,
size_t offset, size_t size) = 0;
};
+36
View File
@@ -0,0 +1,36 @@
#pragma once
#include <string>
#include <volk.h>
#include <memory>
#include "resources/GpuAllocation.h"
#include "ImageView.hpp"
class Gpu;
struct Image {
VkImage img;
VkImageAspectFlagBits m_aspect_mask;
uint32_t m_layer_count;
uint32_t m_level_count;
GpuAllocation allocation;
public:
Image() : Image(VK_NULL_HANDLE, {}) {
}
Image(VkImage img, GpuAllocation allocation) :
img(img), allocation(allocation) {
}
ImageView create_view(const Gpu* gpu, VkFormat format,
VkImageSubresourceRange subresource);
void set_debug_name(const Gpu* gpu, const std::string& name) const;
};
@@ -0,0 +1,54 @@
#pragma once
#include "Gpu.hpp"
class ImageBusWriter {
private:
const Gpu* m_gpu;
/**
* Image to which we are writing
*/
Image *m_pTarget;
uint32_t m_formatSize;
uint32_t m_imageSize;
/**
* Intermediate staging buffer to pass data from heap 3 to heap 0
*/
Buffer m_stagingBuffer;
size_t m_stagingBufferSize;
VkCommandBuffer m_stagingCommandBuffer;
/**
* Pointing to where the staging buffer is mapped
*/
void *m_pMappedData;
std::vector<VkBufferImageCopy> m_writes;
uint32_t m_numWrites;
VkFence m_fence;
int create_staging_buffer(size_t size);
int create_staging_command_buffer();
int create_fence();
public:
ImageBusWriter(const Gpu* gpu, Image *pImage,
VkExtent2D extent, uint32_t formatSize,
size_t maxWrites);
void write(VkBufferImageCopy region, void *pData, size_t size);
void set_target(Image *pTarget) {
flush();
m_pTarget = pTarget;
}
void flush();
};
@@ -0,0 +1,22 @@
#pragma once
#include <volk.h>
#include <string>
#include <memory>
#include <vulkan/vulkan_core.h>
class Gpu;
struct ImageView {
VkImageView view;
ImageView() :
view(VK_NULL_HANDLE) {
}
ImageView(VkImageView view) :
view(view) {
}
void set_debug_name(const std::shared_ptr<const Gpu>& gpu, const std::string& name) const;
};
@@ -0,0 +1,24 @@
#pragma once
#include <cstdint>
#include <volk.h>
#include "Gpu.hpp"
class MipmapGenerator {
private:
const Gpu* m_gpu;
VkCommandBuffer m_commandBuffer;
VkFence m_fence;
VkFence create_fence(const Gpu* gpu);
VkCommandBuffer create_command_buffer(const Gpu* gpu);
public:
explicit MipmapGenerator(const Gpu* gpu);
uint32_t generate(Image image, VkImageLayout oldLayout, VkExtent2D extent, VkImageSubresourceRange range);
};