Blender V5.3
vk_index_buffer.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "vk_index_buffer.hh"
12#include "vk_shader.hh"
14#include "vk_staging_buffer.hh"
15#include "vk_state_manager.hh"
16
17#include "CLG_log.h"
18
19namespace blender {
20
21static CLG_LogRef LOG = {"gpu.vulkan"};
22
23namespace gpu {
24
26{
27 if (is_subrange_) {
28 src_->upload_data();
29 return;
30 }
31
32 if (!buffer_.is_allocated()) {
33 allocate();
34 if (!buffer_.is_allocated()) {
35 CLOG_ERROR(&LOG, "Unable to allocate index buffer. Most likely an out of memory issue.");
36 return;
37 }
38 }
39
40 if (data_ == nullptr) {
41 return;
42 }
43
44 if (!data_uploaded_ && buffer_.is_mapped()) {
45 buffer_.update_immediately(data_);
46 MEM_SAFE_DELETE_VOID(data_);
47 }
48 else {
49 VKContext &context = *VKContext::get();
51 VKBuffer &buffer = staging_buffer.host_buffer_get();
52 if (buffer.is_allocated()) {
53 staging_buffer.host_buffer_get().update_immediately(data_);
54 staging_buffer.copy_to_device(context);
55 }
56 else {
57 buffer_.clear(context, 0u);
59 &LOG,
60 "Unable to upload data to index buffer via a staging buffer as the staging buffer "
61 "could not be allocated. Index buffer will be filled with on zeros to reduce "
62 "drawing artifacts due to read from uninitialized memory.");
63 buffer_.clear(context, 0u);
64 }
65 MEM_SAFE_DELETE_VOID(data_);
66 }
67
68 data_uploaded_ = true;
69}
70
75
77{
78 if (is_subrange_) {
79 src_->bind_as_ssbo(binding);
80 return;
81 }
82
85}
86
88{
89 VKContext &context = *VKContext::get();
91 VKBuffer &buffer = staging_buffer.host_buffer_get();
92 if (buffer.is_mapped()) {
93 staging_buffer.copy_from_device(context);
94 staging_buffer.host_buffer_get().read(context, data);
95 }
96 else {
98 "Unable to read data from index buffer via a staging buffer as the staging buffer "
99 "could not be allocated. ");
100 }
101}
102
103void VKIndexBuffer::update_sub(uint start, uint len, const void *data)
104{
105 if (!buffer_.is_allocated()) {
106 /* Allocating huge buffers can fail, in that case we skip copying data. */
107 return;
108 }
109 BLI_assert_msg(start + len <= buffer_.size_in_bytes(), "Out of bound write to index buffer");
110 if (buffer_.is_mapped()) {
111 buffer_.update_sub_immediately(start, len, data);
112 }
113 else {
114 VKContext &context = *VKContext::get();
115 VKStagingBuffer staging_buffer(buffer_, VKStagingBuffer::Direction::HostToDevice, start, len);
116 memcpy(staging_buffer.host_buffer_get().mapped_memory_get(), data, len);
117 staging_buffer.copy_to_device(context);
118 }
119}
120
122 uint source_first_index,
123 uint dest_first_index,
124 uint index_len)
125{
126 VKIndexBuffer &src = static_cast<VKIndexBuffer &>(source_buf);
130 "Index type mismatch between source and destination");
131 BLI_assert_msg(size_t(source_first_index) + index_len <= src.index_len_,
132 "Copy source range exceeds the source index buffer bounds");
133 BLI_assert_msg(size_t(dest_first_index) + index_len <= index_len_,
134 "Copy destination range exceeds the destination index buffer bounds");
135 BLI_assert_msg(buffer_.is_allocated(), "GPU_indexbuf_use() not called on this buffer");
136 BLI_assert_msg(src.buffer_.is_allocated(), "GPU_indexbuf_use() not called on the source buffer");
137
138 VKContext &context = *VKContext::get();
140 copy_buffer.src_buffer = src.buffer_.resource();
141 copy_buffer.dst_buffer = buffer_.resource();
142 copy_buffer.region.srcOffset = source_first_index * to_bytesize(src.index_type_);
143 copy_buffer.region.dstOffset = dest_first_index * to_bytesize(index_type_);
144 copy_buffer.region.size = index_len * to_bytesize(index_type_);
145 context.render_graph().add_node(copy_buffer);
146}
147
148void VKIndexBuffer::strip_restart_indices()
149{
151}
152
153void VKIndexBuffer::allocate()
154{
155 VkBufferUsageFlags vk_buffer_usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
156 VK_BUFFER_USAGE_INDEX_BUFFER_BIT |
157 VK_BUFFER_USAGE_TRANSFER_DST_BIT |
158 VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
160 vk_buffer_usage |= VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR |
161 VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
162 }
163
164 buffer_.create(size_get(),
165 vk_buffer_usage,
166 VMA_MEMORY_USAGE_AUTO,
167 VmaAllocationCreateFlags(0),
168 0.8f,
169 false,
170 "IndexBuffer");
171}
172
173const VKBuffer &VKIndexBuffer::buffer_get() const
174{
175 return is_subrange_ ? unwrap(src_)->buffer_ : buffer_;
176}
177VKBuffer &VKIndexBuffer::buffer_get()
178{
179 return is_subrange_ ? unwrap(src_)->buffer_ : buffer_;
180}
181
182} // namespace gpu
183} // namespace blender
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define BLI_assert_msg(a, msg)
GPUIndexBufType index_type_
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 * mapped_memory_get() const
Definition vk_buffer.hh:158
VKStateManager & state_manager_get() const
static VKContext * get()
void read(uint32_t *data) const override
void update_sub(uint start, uint len, const void *data) override
void copy_sub(IndexBuf &source_buf, uint source_first_index, uint dest_first_index, uint index_len) override
Copy part of source index buffer into this index buffer.
void bind_as_ssbo(uint binding) override
void copy_from_device(VKContext &context)
void copy_to_device(VKContext &context)
void storage_buffer_bind(BindSpaceStorageBuffers::Type resource_type, void *resource, int binding)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BLI_INLINE MTLTexture * unwrap(Texture *tex)
int to_bytesize(const DataFormat format)
GPUCapabilities GCaps
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned int uint
Definition BKE_text.h:55
unsigned int uint32_t
Definition BKE_text.h:81
BMesh const char void * data
static CLG_LogRef LOG
Definition logging.cpp:12
VKResourceWithHandle< VkBuffer > dst_buffer
VKResourceWithHandle< VkBuffer > src_buffer
#define NOT_YET_IMPLEMENTED
Definition vk_common.hh:196