Blender V5.3
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
16#include "vk_common.hh"
17
18namespace blender::gpu {
19class VKContext;
20class VKDevice;
21
25class VKBuffer : public NonCopyable {
26 size_t size_in_bytes_ = 0;
27 size_t alloc_size_in_bytes_ = 0;
29 VmaAllocation allocation_ = VK_NULL_HANDLE;
30 TimelineValue async_timeline_ = 0;
32 bool allocation_failed_ = false;
33
34 /* Pointer to the virtually mapped memory. */
35 void *mapped_memory_ = nullptr;
36
37 VkDeviceAddress vk_device_address = 0;
38
39 public:
40 VKBuffer() = default;
41 virtual ~VKBuffer();
42
44 bool is_allocated() const;
45
49 bool create(size_t size,
50 VkBufferUsageFlags buffer_usage,
51 VmaMemoryUsage vma_memory_usage,
52 VmaAllocationCreateFlags vma_allocation_flags,
53 float priority,
54 bool export_memory = false,
55 const char *debug_name = "VKBuffer",
56 size_t alignment = 0);
57 void clear(VKContext &context, uint32_t clear_value);
58 void update_immediately(const void *data) const;
59 void update_sub_immediately(size_t start_offset, size_t data_size, const void *data) const;
60
65 void update_render_graph(VKContext &context, void *data) const;
66 void flush() const;
67
71 void read(VKContext &context, void *data) const;
72
76 void async_flush_to_host(VKContext &context);
77
84 void read_async(VKContext &context, void *data);
85
92 bool free();
93
95 {
96 return size_in_bytes_;
97 }
98
100 {
101 return alloc_size_in_bytes_;
102 }
103
105 {
106 return resource_;
107 }
108 VkBuffer vk_handle() const
109 {
110 return resource_.vk_handle;
111 }
112
118 void *mapped_memory_get() const;
119
120 inline VkDeviceAddress device_address_get() const
121 {
122 return vk_device_address;
123 }
124
125 inline bool has_device_address() const
126 {
127 return vk_device_address != 0;
128 }
129
133 bool is_mapped() const;
134
138 VkDeviceMemory export_memory_get(size_t &memory_size);
139
144 void flush_mapped_memory();
145
151
152 private:
154 bool map();
155 void unmap();
156};
157
158inline void *VKBuffer::mapped_memory_get() const
159{
160 BLI_assert_msg(this->is_mapped(), "Cannot access a non-mapped buffer.");
161 return mapped_memory_;
162}
163
164inline bool VKBuffer::is_mapped() const
165{
166 return mapped_memory_ != nullptr;
167}
168
169inline bool VKBuffer::is_allocated() const
170{
171 return allocation_ != VK_NULL_HANDLE;
172}
173
183
184} // namespace blender::gpu
#define BLI_assert_msg(a, msg)
NonCopyable(const NonCopyable &other)=delete
void update_immediately(const void *data) const
Definition vk_buffer.cc:144
bool is_allocated() const
Definition vk_buffer.hh:169
VkDeviceAddress device_address_get() const
Definition vk_buffer.hh:120
bool is_mapped() const
Definition vk_buffer.hh:164
void read(VKContext &context, void *data) const
Definition vk_buffer.cc:203
VkBuffer vk_handle() const
Definition vk_buffer.hh:108
bool create(size_t size, VkBufferUsageFlags buffer_usage, VmaMemoryUsage vma_memory_usage, VmaAllocationCreateFlags vma_allocation_flags, float priority, bool export_memory=false, const char *debug_name="VKBuffer", size_t alignment=0)
Definition vk_buffer.cc:29
const VKResourceWithHandle< VkBuffer > & resource() const
Definition vk_buffer.hh:104
void read_async(VKContext &context, void *data)
Definition vk_buffer.cc:191
int64_t allocated_size_in_bytes() const
Definition vk_buffer.hh:99
VkDeviceMemory export_memory_get(size_t &memory_size)
Definition vk_buffer.cc:233
void clear(VKContext &context, uint32_t clear_value)
Definition vk_buffer.cc:174
void update_sub_immediately(size_t start_offset, size_t data_size, const void *data) const
Definition vk_buffer.cc:149
void update_render_graph(VKContext &context, void *data) const
Definition vk_buffer.cc:157
bool has_device_address() const
Definition vk_buffer.hh:125
void async_flush_to_host(VKContext &context)
Definition vk_buffer.cc:183
void * mapped_memory_get() const
Definition vk_buffer.hh:158
int64_t size_in_bytes() const
Definition vk_buffer.hh:94
uint64_t TimelineValue
Definition vk_common.hh:39
__int64 int64_t
Definition BKE_text.h:90
unsigned int uint32_t
Definition BKE_text.h:81
BMesh const char void * data
VKResourceWithHandle< VkBuffer > buffer
Definition vk_buffer.hh:180