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 buffer(size_type size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, memory::observer_ptr<const void> memory = nullptr);
30
31 virtual ~buffer();
32
33 auto handle() const noexcept -> handle_type;
34
35 operator handle_type() const noexcept;
36
37 auto address() const noexcept -> address_type;
38
39 auto resize(const size_type new_size) -> void;
40
41 virtual auto size() const noexcept -> size_type;
42
43 virtual auto write(memory::observer_ptr<const void> data, size_type size, size_type offset = 0) -> void;
44
45 // static auto insert_buffer_memory_barrier(command_buffer& command_buffer, buffer& <-- We maybe need this
46
47 virtual auto name() const noexcept -> std::string {
48 return "Buffer";
49 }
50
51 auto map() -> void;
52
53 auto unmap() -> void;
54
55 auto mapped_memory() -> memory::observer_ptr<void> {
56 return _mapped_memory;
57 }
58
59protected:
60
61 memory::observer_ptr<void> _mapped_memory;
62
63private:
64
65 size_type _size;
66
67 VkBufferUsageFlags _usage;
68 VkMemoryPropertyFlags _properties;
69
70 VkBuffer _handle;
71 VmaAllocation _allocation;
72 address_type _address;
73
74}; // class buffer
75
77
78template<typename Type, VkBufferUsageFlags Usage, VkMemoryPropertyFlags Properties>
79class typed_buffer : public buffer {
80
81 using base_type = buffer;
82
83public:
84
85 using value_type = Type;
86 using size_type = base_type::size_type;
87
88 typed_buffer(std::span<const Type> elements, VkMemoryPropertyFlags properties = 0, VkBufferUsageFlags usage = 0u)
89 : base_type{elements.size() * sizeof(Type), (usage | Usage) , (properties | Properties), elements.data()} { }
90
91 typed_buffer(size_type size, VkMemoryPropertyFlags properties = 0, VkBufferUsageFlags usage = 0u)
92 : base_type{size * sizeof(Type), (usage | Usage) , (properties | Properties), nullptr} { }
93
94 ~typed_buffer() override = default;
95
96 auto size() const noexcept -> VkDeviceSize override {
97 return size_in_bytes() / sizeof(Type);
98 }
99
100 auto size_in_bytes() const noexcept -> VkDeviceSize {
101 return base_type::size();
102 }
103
104}; // class typed_buffer
105
106using 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)>;
107
108} // namespace sbx::graphics
109
110#endif // LIBSBX_GRAPHICS_BUFFERS_BUFFER_HPP_
Definition: buffer.hpp:21
Definition: buffer.hpp:79
A non-owning pointer that can be used to observe the value of a pointer.
Definition: observer_ptr.hpp:28
Definition: noncopyable.hpp:7