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
45 virtual auto name() const noexcept -> std::string {
46 return "Buffer";
47 }
48
49protected:
50
51 auto map() -> void;
52
53 auto unmap() -> void;
54
55 memory::observer_ptr<void> _mapped_memory;
56
57private:
58
59 size_type _size;
60
61 VkBufferUsageFlags _usage;
62 VkMemoryPropertyFlags _properties;
63
64 VkBuffer _handle;
65 VmaAllocation _allocation;
66 std::uint64_t _address;
67
68}; // class buffer
69
71
72template<typename Type, VkBufferUsageFlags Usage, VkMemoryPropertyFlags Properties>
73class typed_buffer : public buffer {
74
75 using base_type = buffer;
76
77public:
78
79 using value_type = Type;
80 using size_type = base_type::size_type;
81
82 typed_buffer(std::span<const Type> elements, VkMemoryPropertyFlags properties = 0, VkBufferUsageFlags usage = 0u)
83 : base_type{elements.size() * sizeof(Type), (usage | Usage) , (properties | Properties), elements.data()} { }
84
85 typed_buffer(size_type size, VkMemoryPropertyFlags properties = 0, VkBufferUsageFlags usage = 0u)
86 : base_type{size * sizeof(Type), (usage | Usage) , (properties | Properties), nullptr} { }
87
88 ~typed_buffer() override = default;
89
90 auto size() const noexcept -> VkDeviceSize override {
91 return size_in_bytes() / sizeof(Type);
92 }
93
94 auto size_in_bytes() const noexcept -> VkDeviceSize {
95 return base_type::size();
96 }
97
98}; // class typed_buffer
99
100using 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)>;
101
102} // namespace sbx::graphics
103
104#endif // LIBSBX_GRAPHICS_BUFFERS_BUFFER_HPP_
Definition: buffer.hpp:20
Definition: buffer.hpp:73
A non-owning pointer that can be used to observe the value of a pointer.
Definition: observer_ptr.hpp:27
Definition: noncopyable.hpp:6