VapourSynth-llvmexpr
Loading...
Searching...
No Matches
VulkanMemory.hpp
Go to the documentation of this file.
1
19
20#ifndef LLVMEXPR_RUNTIME_VULKAN_VULKANMEMORY_HPP
21#define LLVMEXPR_RUNTIME_VULKAN_VULKANMEMORY_HPP
22
23#define VK_NO_PROTOTYPES
24
25// NOLINTBEGIN(cppcoreguidelines-macro-usage,cppcoreguidelines-macro-to-enum,modernize-macro-to-enum)
26
27#define VULKAN_HPP_DISPATCH_LOADER_DYNAMIC 1
28
29// NOLINTEND(cppcoreguidelines-macro-usage,cppcoreguidelines-macro-to-enum,modernize-macro-to-enum)
30
31#include <vk_mem_alloc.h>
32#include <vulkan/vulkan_raii.hpp>
33
34#include <cstddef>
35
36namespace vkexpr {
37
38class VulkanContext;
39
41 VkBuffer buffer = VK_NULL_HANDLE;
42 VmaAllocation allocation = VK_NULL_HANDLE;
43 VmaAllocationInfo alloc_info = {};
44 VkDeviceSize size = 0;
45
46 VulkanBuffer() = default;
47 VulkanBuffer(VkBuffer buf, VmaAllocation alloc, VmaAllocationInfo info,
48 VkDeviceSize sz)
49 : buffer(buf), allocation(alloc), alloc_info(info), size(sz) {}
50
51 VulkanBuffer(const VulkanBuffer&) = delete;
53 ~VulkanBuffer() = default;
54
55 VulkanBuffer(VulkanBuffer&& other) noexcept
56 : buffer(other.buffer), allocation(other.allocation),
57 alloc_info(other.alloc_info), size(other.size) {
58 other.buffer = VK_NULL_HANDLE;
59 other.allocation = VK_NULL_HANDLE;
60 other.size = 0;
61 }
62
63 VulkanBuffer& operator=(VulkanBuffer&& other) noexcept {
64 if (this != &other) {
65 buffer = other.buffer;
66 allocation = other.allocation;
67 alloc_info = other.alloc_info;
68 size = other.size;
69 other.buffer = VK_NULL_HANDLE;
70 other.allocation = VK_NULL_HANDLE;
71 other.size = 0;
72 }
73 return *this;
74 }
75
76 [[nodiscard]] bool isValid() const { return buffer != VK_NULL_HANDLE; }
77 [[nodiscard]] void* getMappedData() const { return alloc_info.pMappedData; }
78};
79
81 public:
82 explicit VulkanMemory(VulkanContext& ctx);
84
85 VulkanMemory(const VulkanMemory&) = delete;
89
91 VkDeviceSize size,
92 VkBufferUsageFlags usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
93 VK_BUFFER_USAGE_TRANSFER_DST_BIT |
94 VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
95
96 VulkanBuffer createStagingBuffer(VkDeviceSize size, bool for_upload = true);
97
98 void destroyBuffer(VulkanBuffer& buffer);
99
100 void uploadToBuffer(VulkanBuffer& gpu_buffer, const void* data,
101 VkDeviceSize size, VkDeviceSize offset = 0);
102
103 void downloadFromBuffer(VulkanBuffer& gpu_buffer, void* data,
104 VkDeviceSize size, VkDeviceSize offset = 0);
105
106 void copyBuffer(VulkanBuffer& src, VulkanBuffer& dst, VkDeviceSize size);
107
108 [[nodiscard]] VmaAllocator getAllocator() const { return allocator; }
109
110 void flushBuffer(const VulkanBuffer& buffer, VkDeviceSize size,
111 VkDeviceSize offset = 0) const;
112 void invalidateBuffer(const VulkanBuffer& buffer, VkDeviceSize size,
113 VkDeviceSize offset = 0) const;
114
115 private:
116 VulkanContext& context;
117 VmaAllocator allocator = VK_NULL_HANDLE;
118 vk::raii::CommandPool transfer_pool = nullptr;
119 vk::raii::CommandBuffer transfer_cmd = nullptr;
120 vk::raii::Fence transfer_fence = nullptr;
121};
122
123} // namespace vkexpr
124
125#endif // LLVMEXPR_RUNTIME_VULKAN_VULKANMEMORY_HPP
VulkanMemory(VulkanContext &ctx)
VulkanMemory(const VulkanMemory &)=delete
VulkanMemory(VulkanMemory &&)=delete
VulkanBuffer createStagingBuffer(VkDeviceSize size, bool for_upload=true)
VulkanMemory & operator=(const VulkanMemory &)=delete
VulkanBuffer createGPUBuffer(VkDeviceSize size, VkBufferUsageFlags usage=VK_BUFFER_USAGE_STORAGE_BUFFER_BIT|VK_BUFFER_USAGE_TRANSFER_DST_BIT|VK_BUFFER_USAGE_TRANSFER_SRC_BIT)
void destroyBuffer(VulkanBuffer &buffer)
VmaAllocator getAllocator() const
VulkanMemory & operator=(VulkanMemory &&)=delete
void copyBuffer(VulkanBuffer &src, VulkanBuffer &dst, VkDeviceSize size)
void uploadToBuffer(VulkanBuffer &gpu_buffer, const void *data, VkDeviceSize size, VkDeviceSize offset=0)
void flushBuffer(const VulkanBuffer &buffer, VkDeviceSize size, VkDeviceSize offset=0) const
void downloadFromBuffer(VulkanBuffer &gpu_buffer, void *data, VkDeviceSize size, VkDeviceSize offset=0)
void invalidateBuffer(const VulkanBuffer &buffer, VkDeviceSize size, VkDeviceSize offset=0) const
VulkanBuffer(const VulkanBuffer &)=delete
VulkanBuffer & operator=(VulkanBuffer &&other) noexcept
VmaAllocation allocation
void * getMappedData() const
VulkanBuffer & operator=(const VulkanBuffer &)=delete
VulkanBuffer(VulkanBuffer &&other) noexcept
VulkanBuffer(VkBuffer buf, VmaAllocation alloc, VmaAllocationInfo info, VkDeviceSize sz)
VmaAllocationInfo alloc_info