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 <vulkan/vulkan.hpp>
9#include <wren/vk/result.hpp>
10
11namespace wren::vk {
12
13class Buffer {
14 public:
15 static auto create(const VmaAllocator &allocator, size_t size,
16 VkBufferUsageFlags usage,
17 const std::optional<VmaAllocationCreateFlags> &flags = {})
18 -> std::shared_ptr<Buffer>;
19
20 static auto copy_buffer(const ::vk::Device &device,
21 const ::vk::Queue &submit_queue,
22 const ::vk::CommandPool &command_pool,
23 const std::shared_ptr<Buffer> &src,
24 const std::shared_ptr<Buffer> &dst, size_t size)
26
27 Buffer(const VmaAllocator &allocator) : allocator_(allocator) {}
28 ~Buffer();
29
30 Buffer(const Buffer &) = delete;
31 Buffer(Buffer &&) = delete;
32 auto operator=(const Buffer &) = delete;
33 auto operator=(Buffer &&) = delete;
34
35 template <typename T>
36 auto set_data_raw(std::span<const T> data) -> expected<void>;
37
38 auto set_data_raw(const void *data, std::size_t size) -> expected<void>;
39
40 auto map() {
41 vmaMapMemory(allocator_, allocation_, &mapped_ptr_);
42 return mapped_ptr_;
43 }
44
45 auto unmap() {
46 if (mapped_ptr_ == nullptr) return;
47 vmaUnmapMemory(allocator_, allocation_);
48 mapped_ptr_ = nullptr;
49 }
50
51 [[nodiscard]] auto get() const { return buffer_; }
52
53 private:
54 ::vk::Buffer buffer_{};
55 VmaAllocator allocator_ = nullptr;
56 VmaAllocation allocation_{};
57
58 void *mapped_ptr_ = nullptr;
59};
60
61template <typename T>
62auto Buffer::set_data_raw(std::span<const T> data) -> expected<void> {
63 VK_ERR_PROP_VOID(static_cast<::vk::Result>(vmaCopyMemoryToAllocation(
64 allocator_, data.data(), allocation_, {0}, {data.size_bytes()})));
65 return {};
66}
67
68} // namespace wren::vk
Definition buffer.hpp:13
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:54
~Buffer()
Definition buffer.cpp:85
auto unmap()
Definition buffer.hpp:45
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:13
void * mapped_ptr_
Definition buffer.hpp:58
VmaAllocation allocation_
Definition buffer.hpp:56
auto set_data_raw(std::span< const T > data) -> expected< void >
Definition buffer.hpp:62
auto operator=(const Buffer &)=delete
::vk::Buffer buffer_
Definition buffer.hpp:54
VmaAllocator allocator_
Definition buffer.hpp:55
Buffer(const Buffer &)=delete
Buffer(const VmaAllocator &allocator)
Definition buffer.hpp:27
auto map()
Definition buffer.hpp:40
auto get() const
Definition buffer.hpp:51
auto operator=(Buffer &&)=delete
Definition render_pass.hpp:19
std::expected< T, Err > expected
Definition result.hpp:49
#define VK_ERR_PROP_VOID(err)
Definition result.hpp:84