Blender V5.0
vk_buffer.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
12
13#include "BLI_utility_mixins.hh"
14#include "vk_common.hh"
15
16namespace blender::gpu {
17class VKContext;
18class VKDevice;
19
23class VKBuffer : public NonCopyable {
24 size_t size_in_bytes_ = 0;
25 size_t alloc_size_in_bytes_ = 0;
26 VkBuffer vk_buffer_ = VK_NULL_HANDLE;
27 VmaAllocation allocation_ = VK_NULL_HANDLE;
28 VkMemoryPropertyFlags vk_memory_property_flags_;
29 TimelineValue async_timeline_ = 0;
31 bool allocation_failed_ = false;
32
33 /* Pointer to the virtually mapped memory. */
34 void *mapped_memory_ = nullptr;
35
36 VkDeviceAddress vk_device_address = 0;
37
38 public:
39 VKBuffer() = default;
40 virtual ~VKBuffer();
41
43 bool is_allocated() const;
44
48 bool create(size_t size,
49 VkBufferUsageFlags buffer_usage,
50 VmaMemoryUsage vma_memory_usage,
51 VmaAllocationCreateFlags vma_allocation_flags,
52 float priority,
53 bool export_memory = false);
54 void clear(VKContext &context, uint32_t clear_value);
55 void update_immediately(const void *data) const;
56 void update_sub_immediately(size_t start_offset, size_t data_size, const void *data) const;
57
62 void update_render_graph(VKContext &context, void *data) const;
63 void flush() const;
64
68 void read(VKContext &context, void *data) const;
69
73 void async_flush_to_host(VKContext &context);
74
81 void read_async(VKContext &context, void *data);
82
89 bool free();
90
94 void free_immediately(VKDevice &device);
95
97 {
98 return size_in_bytes_;
99 }
100
101 VkBuffer vk_handle() const
102 {
103 return vk_buffer_;
104 }
105
111 void *mapped_memory_get() const;
112
113 VkDeviceAddress device_address_get() const
114 {
115 return vk_device_address;
116 }
117
121 bool is_mapped() const;
122
126 VkDeviceMemory export_memory_get(size_t &memory_size);
127
128 private:
130 bool map();
131 void unmap();
132};
133
134inline void *VKBuffer::mapped_memory_get() const
135{
136 BLI_assert_msg(this->is_mapped(), "Cannot access a non-mapped buffer.");
137 return mapped_memory_;
138}
139
140inline bool VKBuffer::is_mapped() const
141{
142 return mapped_memory_ != nullptr;
143}
144
145inline bool VKBuffer::is_allocated() const
146{
147 return allocation_ != VK_NULL_HANDLE;
148}
149
156 const VkBuffer buffer;
157 VkDeviceSize offset;
158};
159
160} // namespace blender::gpu
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
BMesh const char void * data
long long int int64_t
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
NonCopyable(const NonCopyable &other)=delete
void update_immediately(const void *data) const
Definition vk_buffer.cc:123
bool is_allocated() const
Definition vk_buffer.hh:145
VkDeviceAddress device_address_get() const
Definition vk_buffer.hh:113
bool is_mapped() const
Definition vk_buffer.hh:140
VkBuffer vk_handle() const
Definition vk_buffer.hh:101
void free_immediately(VKDevice &device)
Definition vk_buffer.cc:244
void read_async(VKContext &context, void *data)
Definition vk_buffer.cc:170
VkDeviceMemory export_memory_get(size_t &memory_size)
Definition vk_buffer.cc:212
void clear(VKContext &context, uint32_t clear_value)
Definition vk_buffer.cc:153
void update_sub_immediately(size_t start_offset, size_t data_size, const void *data) const
Definition vk_buffer.cc:128
void update_render_graph(VKContext &context, void *data) const
Definition vk_buffer.cc:136
void async_flush_to_host(VKContext &context)
Definition vk_buffer.cc:162
bool create(size_t size, VkBufferUsageFlags buffer_usage, VmaMemoryUsage vma_memory_usage, VmaAllocationCreateFlags vma_allocation_flags, float priority, bool export_memory=false)
Definition vk_buffer.cc:27
void * mapped_memory_get() const
Definition vk_buffer.hh:134
int64_t size_in_bytes() const
Definition vk_buffer.hh:96
uint64_t TimelineValue
Definition vk_common.hh:36