wren
Vulkan-based game engine
Loading...
Searching...
No Matches
buffer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <vk_mem_alloc.h>
4#include <vulkan/vulkan_core.h>
5
6#include <memory>
7#include <span>
8#include <tl/expected.hpp>
9#include <vulkan/vulkan.hpp>
10#include <wren/vk/errors.hpp>
11
12namespace wren::vk {
13
14class Buffer {
15 public:
16 static auto create(const VmaAllocator &allocator, size_t size,
17 VkBufferUsageFlags usage,
18 const std::optional<VmaAllocationCreateFlags> &flags = {})
19 -> std::shared_ptr<Buffer>;
20
21 static auto copy_buffer(const ::vk::Device &device,
22 const ::vk::Queue &submit_queue,
23 const ::vk::CommandPool &command_pool,
24 const std::shared_ptr<Buffer> &src,
25 const std::shared_ptr<Buffer> &dst, size_t size)
27
28 Buffer(const VmaAllocator &allocator) : allocator_(allocator) {}
29 ~Buffer();
30
31 Buffer(const Buffer &) = delete;
32 Buffer(Buffer &&) = delete;
33 auto operator=(const Buffer &) = delete;
34 auto operator=(Buffer &&) = delete;
35
36 template <typename T>
37 auto set_data_raw(std::span<const T> data) -> expected<void>;
38
39 auto set_data_raw(const void *data, std::size_t size) -> expected<void>;
40
41 auto map() {
42 vmaMapMemory(allocator_, allocation_, &mapped_ptr_);
43 return mapped_ptr_;
44 }
45
46 auto unmap() {
47 if (mapped_ptr_ == nullptr) return;
48 vmaUnmapMemory(allocator_, allocation_);
49 mapped_ptr_ = nullptr;
50 }
51
52 [[nodiscard]] auto get() const { return buffer_; }
53
54 private:
55 ::vk::Buffer buffer_{};
56 VmaAllocator allocator_ = nullptr;
57 VmaAllocation allocation_{};
58
59 void *mapped_ptr_ = nullptr;
60};
61
62template <typename T>
63auto Buffer::set_data_raw(std::span<const T> data) -> expected<void> {
64 VK_ERR_PROP_VOID(static_cast<::vk::Result>(vmaCopyMemoryToAllocation(
65 allocator_, data.data(), allocation_, {0}, {data.size_bytes()})));
66 return {};
67}
68
69} // namespace wren::vk
Definition buffer.hpp:14
static auto copy_buffer(const ::vk::Device &device, const ::vk::Queue &submit_queue, const ::vk::CommandPool &command_pool, const std::shared_ptr< Buffer > &src, const std::shared_ptr< Buffer > &dst, size_t size) -> expected< void >
Definition buffer.cpp:55
~Buffer()
Definition buffer.cpp:86
auto unmap()
Definition buffer.hpp:46
Buffer(Buffer &&)=delete
static auto create(const VmaAllocator &allocator, size_t size, VkBufferUsageFlags usage, const std::optional< VmaAllocationCreateFlags > &flags={}) -> std::shared_ptr< Buffer >
Definition buffer.cpp:14
auto set_data_raw(std::span< const T > data) -> expected< void >
Definition buffer.hpp:63
auto operator=(const Buffer &)=delete
Buffer(const Buffer &)=delete
Buffer(const VmaAllocator &allocator)
Definition buffer.hpp:28
auto map()
Definition buffer.hpp:41
auto get() const
Definition buffer.hpp:52
auto operator=(Buffer &&)=delete
Definition render_pass.hpp:19
tl::expected< T, Err > expected
Definition errors.hpp:49
#define VK_ERR_PROP_VOID(err)
Definition errors.hpp:83