Blender V5.3
vk_buffer_pool.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_math_base_c.hh"
10
11#include "vk_buffer.hh"
12#include "vk_buffer_pool.hh"
13#include "vk_context.hh"
14#include "vk_debug.hh"
15
16#include <memory>
17
18namespace blender::gpu {
19
21{
22 BLI_assert(buffers_.is_empty());
23}
24
26{
27 const bool should_allocate_new_buffer = buffers_.is_empty() ||
28 (buffers_.last()->size_in_bytes() - buffer_offset_) <
29 data.size();
30 /* Flush last buffer as all data is available. */
31 if (should_allocate_new_buffer && !buffers_.is_empty() && buffer_offset_ != 0) {
32 finalize_active_buffer();
33 }
34 if (should_allocate_new_buffer) {
35 VkDeviceSize allocation_size = std::max(VkDeviceSize(data.size()), default_buffer_size_);
36 std::unique_ptr<VKBuffer> new_buffer = std::make_unique<VKBuffer>();
37 new_buffer->create(allocation_size,
38 vk_buffer_usage_,
39 vma_memory_usage_,
40 vma_allocation_create_flags_,
41 priority_,
42 false,
43 name_.c_str());
44 debug::object_label(new_buffer->vk_handle(), name_);
45 buffers_.append(std::move(new_buffer));
46 buffer_offset_ = 0;
47 }
48 VKBufferWithOffset result = {.buffer = buffers_.last()->resource(), .offset = buffer_offset_};
49
50 /* Resize data_ when more space is needed than the default_buffer_size_. */
51 if (buffer_offset_ + data.size() > data_.size()) {
52 data_.resize(buffer_offset_ + data.size());
53 }
54 data_.as_mutable_span()
55 .slice(IndexRange::from_begin_size(buffer_offset_, data.size()))
56 .copy_from(data);
57
58 buffer_offset_ += data.size();
59 buffer_offset_ = ceil_to_multiple_ul(buffer_offset_, alignment_);
60
61 return result;
62}
63
65{
66 if (!buffers_.is_empty() && buffer_offset_ != 0) {
67 finalize_active_buffer();
68 }
69}
70
72{
73 buffers_.clear();
74 buffer_offset_ = 0;
75}
76
77void VKBufferPool::finalize_active_buffer()
78{
79 BLI_assert(!buffers_.is_empty());
80 std::unique_ptr<VKBuffer> &active_buffer = buffers_.last();
81 if (active_buffer->is_mapped()) {
82 active_buffer->update_sub_immediately(0, buffer_offset_, data_.data());
83 }
84 else {
85 VKContext &context = *VKContext::get();
86 void *data_copy = MEM_new_uninitialized(active_buffer->allocated_size_in_bytes(), __func__);
87 memcpy(data_copy, data_.data(), std::min(data_.size(), int64_t(buffer_offset_)));
88 active_buffer->update_render_graph(context, data_copy);
89 }
90}
91
92} // namespace blender::gpu
static constexpr IndexRange from_begin_size(const int64_t begin, const int64_t size)
void discard()
Discard the current buffers to the discard pool.
void ensure_uploaded()
Ensure that all the data is uploaded.
VKBufferWithOffset append(Span< uint8_t > data)
Append the given data.
static VKContext * get()
void object_label(GLenum type, GLuint object, const char *name)
Definition gl_debug.cc:331
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
MINLINE uint64_t ceil_to_multiple_ul(uint64_t a, uint64_t b)
BMesh const char void * data
int size() const
Definition vector.hh:130