Blender V4.5
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 VkMemoryPropertyFlags required_flags,
51 VkMemoryPropertyFlags preferred_flags,
52 VmaAllocationCreateFlags vma_allocation_flags,
53 float priority,
54 bool export_memory = false);
55 void clear(VKContext &context, uint32_t clear_value);
56 void update_immediately(const void *data) const;
57 void update_sub_immediately(size_t start_offset, size_t data_size, const void *data) const;
58
63 void update_render_graph(VKContext &context, void *data) const;
64 void flush() const;
65
69 void read(VKContext &context, void *data) const;
70
74 void async_flush_to_host(VKContext &context);
75
82 void read_async(VKContext &context, void *data);
83
90 bool free();
91
95 void free_immediately(VKDevice &device);
96
98 {
99 return size_in_bytes_;
100 }
101
102 VkBuffer vk_handle() const
103 {
104 return vk_buffer_;
105 }
106
112 void *mapped_memory_get() const;
113
114 VkDeviceAddress device_address_get() const
115 {
116 return vk_device_address;
117 }
118
122 bool is_mapped() const;
123
127 VkDeviceMemory export_memory_get(size_t &memory_size);
128
129 private:
131 bool map();
132 void unmap();
133};
134
135inline void *VKBuffer::mapped_memory_get() const
136{
137 BLI_assert_msg(this->is_mapped(), "Cannot access a non-mapped buffer.");
138 return mapped_memory_;
139}
140
141inline bool VKBuffer::is_mapped() const
142{
143 return mapped_memory_ != nullptr;
144}
145
146inline bool VKBuffer::is_allocated() const
147{
148 return allocation_ != VK_NULL_HANDLE;
149}
150
157 const VkBuffer buffer;
158 VkDeviceSize offset;
159};
160
161} // 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:112
bool is_allocated() const
Definition vk_buffer.hh:146
VkDeviceAddress device_address_get() const
Definition vk_buffer.hh:114
bool create(size_t size, VkBufferUsageFlags buffer_usage, VkMemoryPropertyFlags required_flags, VkMemoryPropertyFlags preferred_flags, VmaAllocationCreateFlags vma_allocation_flags, float priority, bool export_memory=false)
Definition vk_buffer.cc:23
bool is_mapped() const
Definition vk_buffer.hh:141
VkBuffer vk_handle() const
Definition vk_buffer.hh:102
void free_immediately(VKDevice &device)
Definition vk_buffer.cc:233
void read_async(VKContext &context, void *data)
Definition vk_buffer.cc:159
VkDeviceMemory export_memory_get(size_t &memory_size)
Definition vk_buffer.cc:201
void clear(VKContext &context, uint32_t clear_value)
Definition vk_buffer.cc:142
void update_sub_immediately(size_t start_offset, size_t data_size, const void *data) const
Definition vk_buffer.cc:117
void update_render_graph(VKContext &context, void *data) const
Definition vk_buffer.cc:125
void async_flush_to_host(VKContext &context)
Definition vk_buffer.cc:151
void * mapped_memory_get() const
Definition vk_buffer.hh:135
int64_t size_in_bytes() const
Definition vk_buffer.hh:97
uint64_t TimelineValue
Definition vk_common.hh:36
read