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