Blender V5.3
vk_buffer.cc
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#include "vk_buffer.hh"
10#include "vk_backend.hh"
11#include "vk_context.hh"
12#include <vulkan/vulkan_core.h>
13
14#include "CLG_log.h"
15
16namespace blender {
17
18static CLG_LogRef LOG = {"gpu.vulkan"};
19
20namespace gpu {
21
23{
24 if (is_allocated()) {
25 free();
26 }
27}
28
30 VkBufferUsageFlags buffer_usage,
31 VmaMemoryUsage vma_memory_usage,
32 VmaAllocationCreateFlags allocation_flags,
33 float priority,
34 bool export_memory,
35 const char *debug_name,
36 size_t alignment)
37{
39 BLI_assert(resource_.vk_handle == VK_NULL_HANDLE);
40 BLI_assert(mapped_memory_ == nullptr);
41 if (allocation_failed_) {
42 return false;
43 }
44
45 size_in_bytes_ = size_in_bytes;
46 /*
47 * Vulkan doesn't allow empty buffers but some areas (DrawManager Instance data, PyGPU) create
48 * them.
49 */
50 alloc_size_in_bytes_ = ceil_to_multiple_ul(max_ulul(size_in_bytes_, 16), 16);
51 VKDevice &device = VKBackend::get().device;
52
53 /* Precheck max buffer size. */
54 if (device.extensions_get().maintenance4 &&
55 alloc_size_in_bytes_ > device.physical_device_maintenance4_properties_get().maxBufferSize)
56 {
58 &LOG,
59 "Couldn't allocate buffer, requested allocation exceeds the maxBufferSize of the device.");
60 allocation_failed_ = true;
61 size_in_bytes_ = 0;
62 alloc_size_in_bytes_ = 0;
63 return false;
64 }
65
66 VmaAllocator allocator = device.mem_allocator_get();
67 VkBufferCreateInfo create_info = {};
68 create_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
69 create_info.flags = 0;
70 create_info.size = alloc_size_in_bytes_;
71 create_info.usage = buffer_usage;
72 /* We use the same command queue for the compute and graphics pipeline, so it is safe to use
73 * exclusive resource handling. */
74 create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
75 create_info.queueFamilyIndexCount = 1;
76 const uint32_t queue_family_indices[1] = {device.queue_family_get()};
77 create_info.pQueueFamilyIndices = queue_family_indices;
78
79 VkExternalMemoryBufferCreateInfo external_memory_create_info = {
80 VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO, nullptr, 0};
81
82 VmaAllocationCreateInfo vma_create_info = {};
83 vma_create_info.flags = allocation_flags;
84 vma_create_info.priority = priority;
85 vma_create_info.usage = vma_memory_usage;
86
87 if (export_memory) {
88 create_info.pNext = &external_memory_create_info;
89 external_memory_create_info.handleTypes = vk_external_memory_handle_type();
90
91 /* Dedicated allocation for zero offset. */
92 vma_create_info.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
93 vma_create_info.pool = device.vma_pools.external_memory_pixel_buffer.pool;
94 }
95
96 if (debug_name && G.debug & G_DEBUG_GPU) {
97 vma_create_info.flags |= VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT;
98 vma_create_info.pUserData = (void *)debug_name;
99 }
100
101 VkResult result = vmaCreateBufferWithAlignment(allocator,
102 &create_info,
103 &vma_create_info,
104 alignment,
105 &resource_.vk_handle,
106 &allocation_,
107 nullptr);
108 if (result != VK_SUCCESS) {
109 allocation_failed_ = true;
110 size_in_bytes_ = 0;
111 alloc_size_in_bytes_ = 0;
112 return false;
113 }
114
115 resource_.resource_handle = device.resources.add_buffer(resource_.vk_handle, allocation_);
116
117 if (bool(create_info.usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT)) {
118 VkBufferDeviceAddressInfo vk_buffer_device_address_info = {
119 VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO, nullptr, resource_};
120 vk_device_address = device.functions.vkGetBufferDeviceAddressKHR(
121 device.vk_handle(), &vk_buffer_device_address_info);
122 }
123
124 if (debug_name) {
125 debug::object_label(resource_.vk_handle, debug_name);
126 }
127
128 /* Check if the memory is mappable. Although the Vulkan specs allow to map any memory that is
129 * host visible, VMA checks for specific host access flags.
130 *
131 * Source:
132 * https://gpuopen-librariesandsdks.github.io/VulkanMemoryAllocator/html/memory_mapping.html
133 */
134 const bool is_mappable = bool(allocation_flags &
135 (VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT |
136 VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT));
137 if (is_mappable) {
138 return map();
139 }
140
141 return true;
142}
143
144void VKBuffer::update_immediately(const void *data) const
145{
146 update_sub_immediately(0, size_in_bytes_, data);
147}
148
149void VKBuffer::update_sub_immediately(size_t start_offset,
150 size_t data_size,
151 const void *data) const
152{
153 BLI_assert_msg(is_mapped(), "Cannot update a non-mapped buffer.");
154 memcpy(static_cast<uint8_t *>(mapped_memory_) + start_offset, data, data_size);
155}
156
158{
159 BLI_assert(size_in_bytes_ <= 65536 && size_in_bytes_ % 4 == 0);
161 update_buffer.dst_buffer = resource();
162 update_buffer.data_size = size_in_bytes_;
163 update_buffer.data = data;
164 context.render_graph().add_node(update_buffer);
165}
166
167void VKBuffer::flush() const
168{
169 const VKDevice &device = VKBackend::get().device;
170 VmaAllocator allocator = device.mem_allocator_get();
171 vmaFlushAllocation(allocator, allocation_, 0, max_ulul(size_in_bytes(), 1));
172}
173
174void VKBuffer::clear(VKContext &context, uint32_t clear_value)
175{
177 fill_buffer.buffer = resource();
178 fill_buffer.data = clear_value;
179 fill_buffer.size = alloc_size_in_bytes_;
180 context.render_graph().add_node(fill_buffer);
181}
182
184{
185 BLI_assert(async_timeline_ == 0);
186 context.rendering_end();
187 async_timeline_ = context.flush_render_graph(RenderGraphFlushFlags::SUBMIT |
189}
190
192{
193 BLI_assert_msg(is_mapped(), "Cannot read a non-mapped buffer.");
194 if (async_timeline_ == 0) {
195 async_flush_to_host(context);
196 }
197 VKDevice &device = VKBackend::get().device;
198 device.wait_for_timeline(async_timeline_);
199 async_timeline_ = 0;
200 memcpy(data, mapped_memory_, size_in_bytes_);
201}
202
203void VKBuffer::read(VKContext &context, void *data) const
204{
205
206 BLI_assert_msg(is_mapped(), "Cannot read a non-mapped buffer.");
207 BLI_assert(async_timeline_ == 0);
208 context.rendering_end();
209 context.flush_render_graph(RenderGraphFlushFlags::SUBMIT |
212 memcpy(data, mapped_memory_, size_in_bytes_);
213}
214
215bool VKBuffer::map()
216{
218 const VKDevice &device = VKBackend::get().device;
219 VmaAllocator allocator = device.mem_allocator_get();
220 VkResult result = vmaMapMemory(allocator, allocation_, &mapped_memory_);
221 return result == VK_SUCCESS;
222}
223
224void VKBuffer::unmap()
225{
227 const VKDevice &device = VKBackend::get().device;
228 VmaAllocator allocator = device.mem_allocator_get();
229 vmaUnmapMemory(allocator, allocation_);
230 mapped_memory_ = nullptr;
231}
232
233VkDeviceMemory VKBuffer::export_memory_get(size_t &memory_size)
234{
235 const VKDevice &device = VKBackend::get().device;
236 VmaAllocator allocator = device.mem_allocator_get();
237
238 VmaAllocationInfo info = {};
239 vmaGetAllocationInfo(allocator, allocation_, &info);
240
241 /* VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT should ensure this. */
242 if (info.offset != 0) {
243 BLI_assert(!"Failed to get zero offset export memory for Vulkan buffer");
244 return VK_NULL_HANDLE;
245 }
246
247 memory_size = info.size;
248 return info.deviceMemory;
249}
250
252{
253 const VKDevice &device = VKBackend::get().device;
254 VmaAllocator allocator = device.mem_allocator_get();
255 vmaFlushAllocation(allocator, allocation_, 0, size_in_bytes_);
256}
257
259{
260 const VKDevice &device = VKBackend::get().device;
261 VmaAllocator allocator = device.mem_allocator_get();
262 vmaInvalidateAllocation(allocator, allocation_, 0, size_in_bytes_);
263}
264
266{
267 if (is_mapped()) {
268 unmap();
269 }
270
271 /* Release the owner reference; at zero ref count the buffer becomes discardable. */
272 if (resource_.vk_handle != VK_NULL_HANDLE) {
273 VKDevice &device = VKBackend::get().device;
274 device.resources.deref_resource(resource_.resource_handle);
275 }
276 allocation_ = VK_NULL_HANDLE;
277 resource_ = {};
278
279 return true;
280}
281
282} // namespace gpu
283} // namespace blender
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define BLI_assert_msg(a, msg)
static VKBackend & get()
void update_immediately(const void *data) const
Definition vk_buffer.cc:144
bool is_allocated() const
Definition vk_buffer.hh:169
bool is_mapped() const
Definition vk_buffer.hh:164
void read(VKContext &context, void *data) const
Definition vk_buffer.cc:203
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
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
void async_flush_to_host(VKContext &context)
Definition vk_buffer.cc:183
int64_t size_in_bytes() const
Definition vk_buffer.hh:94
uint32_t queue_family_get() const
Definition vk_device.hh:343
render_graph::VKResourceStateTracker resources
Definition vk_device.hh:270
VmaAllocator mem_allocator_get() const
Definition vk_device.hh:348
VkDevice vk_handle() const
Definition vk_device.hh:338
VKMemoryPools vma_pools
Definition vk_device.hh:282
const VKExtensions & extensions_get() const
Definition vk_device.hh:398
const VkPhysicalDeviceMaintenance4Properties & physical_device_maintenance4_properties_get() const
Definition vk_device.hh:300
VolkDeviceTable functions
Definition vk_device.hh:280
void wait_for_timeline(TimelineValue timeline)
ResourceHandle add_buffer(VkBuffer vk_buffer, VmaAllocation allocation=VK_NULL_HANDLE, const char *name=nullptr)
void object_label(GLenum type, GLuint object, const char *name)
Definition gl_debug.cc:331
constexpr VkExternalMemoryHandleTypeFlags vk_external_memory_handle_type()
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE unsigned long long max_ulul(unsigned long long a, unsigned long long b)
Global G
Definition blender.cc:57
unsigned int uint32_t
Definition BKE_text.h:81
MINLINE uint64_t ceil_to_multiple_ul(uint64_t a, uint64_t b)
BMesh const char void * data
unsigned char uint8_t
Definition BKE_text.h:79
static CLG_LogRef LOG
Definition logging.cpp:12
VKMemoryPool external_memory_pixel_buffer