sandbox
Loading...
Searching...
No Matches
buffer.hpp
1#ifndef LIBSBX_GRAPHICS_BUFFERS_BUFFER_HPP_
2#define LIBSBX_GRAPHICS_BUFFERS_BUFFER_HPP_
3
4#include <utility>
5#include <span>
6#include <cinttypes>
7
8#include <vk_mem_alloc.h>
9
10#include <vulkan/vulkan.hpp>
11
12#include <libsbx/utility/noncopyable.hpp>
13
14#include <libsbx/memory/observer_ptr.hpp>
15
16#include <libsbx/graphics/resource_storage.hpp>
17
18namespace sbx::graphics {
19
21
22public:
23
24 using handle_type = VkBuffer;
25 using size_type = VkDeviceSize;
26
27 buffer(size_type size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, memory::observer_ptr<const void> memory = nullptr);
28
29 virtual ~buffer();
30
31 auto handle() const noexcept -> handle_type;
32
33 operator handle_type() const noexcept;
34
35 auto address() const noexcept -> std::uint64_t;
36
37 auto resize(const size_type new_size) -> void;
38
39 virtual auto size() const noexcept -> size_type;
40
41 virtual auto write(memory::observer_ptr<const void> data, size_type size, size_type offset = 0) -> void;
42
43 // static auto insert_buffer_memory_barrier(command_buffer& command_buffer, buffer& <-- We maybe need this
44
45protected:
46
47 auto map() -> void;
48
49 auto unmap() -> void;
50
51 memory::observer_ptr<void> _mapped_memory;
52
53private:
54
55 size_type _size;
56
57 VkBufferUsageFlags _usage;
58 VkMemoryPropertyFlags _properties;
59
60 VkBuffer _handle;
61 VmaAllocation _allocation;
62 std::uint64_t _address;
63
64}; // class buffer
65
67
68template<typename Type, VkBufferUsageFlags Usage, VkMemoryPropertyFlags Properties>
69class typed_buffer : public buffer {
70
71 using base_type = buffer;
72
73public:
74
75 using value_type = Type;
76 using size_type = base_type::size_type;
77
78 typed_buffer(std::span<const Type> elements, VkMemoryPropertyFlags properties = 0, VkBufferUsageFlags usage = 0u)
79 : base_type{elements.size() * sizeof(Type), (usage | Usage) , (properties | Properties), elements.data()} { }
80
81 typed_buffer(size_type size, VkMemoryPropertyFlags properties = 0, VkBufferUsageFlags usage = 0u)
82 : base_type{size * sizeof(Type), (usage | Usage) , (properties | Properties), nullptr} { }
83
84 ~typed_buffer() override = default;
85
86 auto size() const noexcept -> VkDeviceSize override {
87 return size_in_bytes() / sizeof(Type);
88 }
89
90 auto size_in_bytes() const noexcept -> VkDeviceSize {
91 return base_type::size();
92 }
93
94}; // class typed_buffer
95
96using staging_buffer = typed_buffer<std::uint8_t, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)>;
97
98} // namespace sbx::graphics
99
100#endif // LIBSBX_GRAPHICS_BUFFERS_BUFFER_HPP_
Definition: buffer.hpp:20
Definition: buffer.hpp:69
A non-owning pointer that can be used to observe the value of a pointer.
Definition: observer_ptr.hpp:27
Definition: noncopyable.hpp:6