Blender V5.0
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
9#include "vk_index_buffer.hh"
10#include "vk_shader.hh"
12#include "vk_staging_buffer.hh"
13#include "vk_state_manager.hh"
14
15#include "CLG_log.h"
16
17static CLG_LogRef LOG = {"gpu.vulkan"};
18
19namespace blender::gpu {
20
22{
23 if (is_subrange_) {
24 src_->upload_data();
25 return;
26 }
27
28 if (!buffer_.is_allocated()) {
29 allocate();
30 if (!buffer_.is_allocated()) {
31 CLOG_ERROR(&LOG, "Unable to allocate index buffer. Most likely an out of memory issue.");
32 return;
33 }
34 }
35
36 if (data_ == nullptr) {
37 return;
38 }
39
40 if (!data_uploaded_ && buffer_.is_mapped()) {
41 buffer_.update_immediately(data_);
43 }
44 else {
45 VKContext &context = *VKContext::get();
47 VKBuffer &buffer = staging_buffer.host_buffer_get();
48 if (buffer.is_allocated()) {
50 staging_buffer.copy_to_device(context);
51 }
52 else {
53 buffer_.clear(context, 0u);
55 &LOG,
56 "Unable to upload data to index buffer via a staging buffer as the staging buffer "
57 "could not be allocated. Index buffer will be filled with on zeros to reduce "
58 "drawing artifacts due to read from uninitialized memory.");
59 buffer_.clear(context, 0u);
60 }
62 }
63
64 data_uploaded_ = true;
65}
66
71
73{
74 if (is_subrange_) {
75 src_->bind_as_ssbo(binding);
76 return;
77 }
78
81}
82
83void VKIndexBuffer::read(uint32_t *data) const
84{
85 VKContext &context = *VKContext::get();
87 VKBuffer &buffer = staging_buffer.host_buffer_get();
88 if (buffer.is_mapped()) {
89 staging_buffer.copy_from_device(context);
90 staging_buffer.host_buffer_get().read(context, data);
91 }
92 else {
94 "Unable to read data from index buffer via a staging buffer as the staging buffer "
95 "could not be allocated. ");
96 }
97}
98
99void VKIndexBuffer::update_sub(uint /*start*/, uint /*len*/, const void * /*data*/)
100{
102}
103
104void VKIndexBuffer::strip_restart_indices()
105{
107}
108
109void VKIndexBuffer::allocate()
110{
111 buffer_.create(size_get(),
112 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT |
113 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
114 VMA_MEMORY_USAGE_AUTO,
115 VmaAllocationCreateFlags(0),
116 0.8f);
117 debug::object_label(buffer_.vk_handle(), "IndexBuffer");
118}
119
120const VKBuffer &VKIndexBuffer::buffer_get() const
121{
122 return is_subrange_ ? unwrap(src_)->buffer_ : buffer_;
123}
124VKBuffer &VKIndexBuffer::buffer_get()
125{
126 return is_subrange_ ? unwrap(src_)->buffer_ : buffer_;
127}
128
129} // namespace blender::gpu
unsigned int uint
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define MEM_SAFE_FREE(v)
BMesh const char void * data
void update_immediately(const void *data) const
Definition vk_buffer.cc:123
bool is_allocated() const
Definition vk_buffer.hh:145
bool is_mapped() const
Definition vk_buffer.hh:140
void read(VKContext &context, void *data) const
Definition vk_buffer.cc:182
VkBuffer vk_handle() const
Definition vk_buffer.hh:101
bool create(size_t size, VkBufferUsageFlags buffer_usage, VmaMemoryUsage vma_memory_usage, VmaAllocationCreateFlags vma_allocation_flags, float priority, bool export_memory=false)
Definition vk_buffer.cc:27
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 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)
#define LOG(level)
Definition log.h:97
void object_label(GLenum type, GLuint object, const char *name)
Definition gl_debug.cc:329
static Context * unwrap(GPUContext *ctx)
static CLG_LogRef LOG
#define NOT_YET_IMPLEMENTED
Definition vk_common.hh:149