Correlation 4.10.0
An Analysis Tool for Liquids and for Amorphous Solids
Loading...
Searching...
No Matches
VulkanBuffer.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#ifdef CORRELATION_USE_VULKAN
12#include <vulkan/vulkan.h>
13#endif
14
15#include <cstddef>
16#include <span>
17#include <utility>
18
20
21#ifdef CORRELATION_USE_VULKAN
22
27struct BufferAllocationDesc {
28 VkDeviceSize size{0};
29 VkBufferUsageFlags usage{0};
30 VkMemoryPropertyFlags properties{0};
31};
32
37class VulkanBuffer {
38public:
39 VulkanBuffer() noexcept = default;
40 ~VulkanBuffer() noexcept { destroy(); }
41
42 VulkanBuffer(const VulkanBuffer &) = delete;
43 VulkanBuffer &operator=(const VulkanBuffer &) = delete;
44
45 VulkanBuffer(VulkanBuffer &&other) noexcept { moveFrom(other); }
46
47 VulkanBuffer &operator=(VulkanBuffer &&other) noexcept {
48 if (this != &other) {
49 destroy();
50 moveFrom(other);
51 }
52 return *this;
53 }
54
58 void create(VkDevice device, VkPhysicalDevice physical_device, const BufferAllocationDesc &desc);
59
63 void destroy() noexcept;
64
69 [[nodiscard]] void *map();
70
74 void unmap() noexcept;
75
80 void upload(std::span<const std::byte> data);
81
86 bool ensureCapacity(VkDevice device, VkPhysicalDevice physical_device,
87 const BufferAllocationDesc &desc);
88
89 [[nodiscard]] VkBuffer buffer() const noexcept { return buffer_; }
90 [[nodiscard]] VkDeviceMemory memory() const noexcept { return memory_; }
91 [[nodiscard]] VkDeviceSize size() const noexcept { return size_; }
92 [[nodiscard]] void *mappedData() const noexcept { return mapped_data_; }
93 [[nodiscard]] bool isValid() const noexcept { return buffer_ != VK_NULL_HANDLE; }
94
95private:
96 void moveFrom(VulkanBuffer &other) noexcept {
97 device_ = std::exchange(other.device_, VK_NULL_HANDLE);
98 buffer_ = std::exchange(other.buffer_, VK_NULL_HANDLE);
99 memory_ = std::exchange(other.memory_, VK_NULL_HANDLE);
100 size_ = std::exchange(other.size_, 0);
101 mapped_data_ = std::exchange(other.mapped_data_, nullptr);
102 }
103
104 VkDevice device_{VK_NULL_HANDLE};
105 VkBuffer buffer_{VK_NULL_HANDLE};
106 VkDeviceMemory memory_{VK_NULL_HANDLE};
107 VkDeviceSize size_{0};
108 void *mapped_data_{nullptr};
109};
110
111#endif // CORRELATION_USE_VULKAN
112
113} // namespace correlation::render3d::vulkan
Definition VulkanComputeService.hpp:24