Blender V5.3
vk_vertex_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 "MEM_guardedalloc.h"
10
12
14#include "vk_data_conversion.hh"
15#include "vk_shader.hh"
17#include "vk_staging_buffer.hh"
18#include "vk_state_manager.hh"
19#include "vk_vertex_buffer.hh"
20
21#include "CLG_log.h"
22
23namespace blender {
24
25static CLG_LogRef LOG = {"gpu.vulkan"};
26
27namespace gpu {
28
33
35{
36 VKContext &context = *VKContext::get();
37 VKStateManager &state_manager = context.state_manager_get();
39}
40
42{
43 VKContext &context = *VKContext::get();
44 VKStateManager &state_manager = context.state_manager_get();
45 state_manager.texel_buffer_bind(*this, binding);
46}
47
52
54{
55 if (vk_buffer_view_ != VK_NULL_HANDLE) {
56 return;
57 }
58
59 VkBufferViewCreateInfo buffer_view_info = {};
60 buffer_view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
61 buffer_view_info.buffer = buffer_.vk_handle();
62 buffer_view_info.format = to_vk_format();
63 buffer_view_info.range = buffer_.size_in_bytes();
64
65 VKDevice &device = VKBackend::get().device;
66 device.functions.vkCreateBufferView(
67 device.vk_handle(), &buffer_view_info, nullptr, &vk_buffer_view_);
68 debug::object_label(vk_buffer_view_, "VertexBufferView");
69
70 BLI_assert(buffer_.resource().resource_handle != 0);
71 device.resources.register_view(buffer_.resource().resource_handle, vk_buffer_view_);
72}
73
78
79void VKVertexBuffer::update_sub(uint start_offset, uint data_size_in_bytes, const void *data)
80{
81 if (!buffer_.is_allocated()) {
82 /* Allocating huge buffers can fail, in that case we skip copying data. */
83 return;
84 }
85 BLI_assert_msg(start_offset + data_size_in_bytes <= buffer_.size_in_bytes(),
86 "Out of bound write to vertex buffer");
87 if (buffer_.is_mapped()) {
88 buffer_.update_sub_immediately(start_offset, data_size_in_bytes, data);
89 }
90 else {
91 VKContext &context = *VKContext::get();
92 VKStagingBuffer staging_buffer(
93 buffer_, VKStagingBuffer::Direction::HostToDevice, start_offset, data_size_in_bytes);
94 memcpy(staging_buffer.host_buffer_get().mapped_memory_get(), data, data_size_in_bytes);
95 staging_buffer.copy_to_device(context);
96 }
97}
98
100 uint source_first_vertex,
101 uint dest_first_vertex,
103{
104 BLI_assert(format.stride == source_buf.format.stride);
105 BLI_assert_msg(size_t(source_first_vertex) + vertex_len <= source_buf.vertex_len,
106 "Copy source range exceeds the source vertex buffer bounds");
107 BLI_assert_msg(size_t(dest_first_vertex) + vertex_len <= this->vertex_len,
108 "Copy destination range exceeds the vertex buffer bounds");
109 VKVertexBuffer &source_vertex_buffer = unwrap(source_buf);
110 BLI_assert_msg(buffer_.is_allocated(), "GPU_vertbuf_use() not called on this buffer");
111 BLI_assert_msg(source_vertex_buffer.buffer_.is_allocated(),
112 "GPU_vertbuf_use() not called on the source buffer");
113 /* Allocating huge buffers can fail, in that case we skip copying data. */
114 if (!buffer_.is_allocated() || !source_vertex_buffer.buffer_.is_allocated()) {
115 return;
116 }
117 const VkDeviceSize copy_size = VkDeviceSize(vertex_len) * format.stride;
118 if (copy_size == 0) {
119 return;
120 }
121
122 VKContext &context = *VKContext::get();
124 copy_buffer.src_buffer = source_vertex_buffer.buffer_.resource();
125 copy_buffer.dst_buffer = buffer_.resource();
126 copy_buffer.region.srcOffset = VkDeviceSize(source_first_vertex) *
127 source_vertex_buffer.format.stride;
128 copy_buffer.region.dstOffset = VkDeviceSize(dest_first_vertex) * format.stride;
129 copy_buffer.region.size = copy_size;
130 context.render_graph().add_node(copy_buffer);
131}
132
133void VKVertexBuffer::read(void *data) const
134{
135 VKContext &context = *VKContext::get();
136 if (buffer_.is_mapped()) {
137 buffer_.read(context, data);
138 return;
139 }
140
141 /* Allocating huge buffers can fail, in that case we skip copying data. */
142 if (buffer_.is_allocated()) {
144 VKBuffer &buffer = staging_buffer.host_buffer_get();
145 if (buffer.is_mapped()) {
146 staging_buffer.copy_from_device(context);
147 staging_buffer.host_buffer_get().read(context, data);
148 }
149 else {
151 &LOG,
152 "Unable to read data from vertex buffer via a staging buffer as the staging buffer "
153 "could not be allocated. ");
154 }
155 }
156}
157
159{
161 return;
162 }
163
164 /* Discard previous data if any. */
165 /* TODO: Use mapped memory. */
166 MEM_SAFE_DELETE(data_);
168}
169
171{
173 return;
174 }
175
176 data_ = static_cast<uchar *>(
178}
179
181{
182 MEM_SAFE_DELETE(data_);
183}
184
185void VKVertexBuffer::upload_data_direct(const VKBuffer &host_buffer)
186{
187 host_buffer.update_immediately(data_);
188}
189
190void VKVertexBuffer::upload_data_via_staging_buffer(VKContext &context)
191{
192 VKStagingBuffer staging_buffer(
194 VKBuffer &buffer = staging_buffer.host_buffer_get();
195 if (buffer.is_allocated()) {
196 upload_data_direct(buffer);
197 staging_buffer.copy_to_device(context);
198 }
199 else {
201 "Unable to upload data to vertex buffer via a staging buffer as the staging buffer "
202 "could not be allocated. Vertex buffer will be filled with on zeros to reduce "
203 "drawing artifacts due to read from uninitialized memory.");
204 buffer_.clear(context, 0u);
205 }
206}
207
209{
210 if (!buffer_.is_allocated()) {
211 allocate();
212 /* If allocation fails, don't upload. */
213 if (!buffer_.is_allocated()) {
214 CLOG_ERROR(&LOG, "Unable to allocate vertex buffer. Most likely an out of memory issue.");
215 return;
216 }
217 }
218
220 return;
221 }
222
224 if (buffer_.is_mapped() && !data_uploaded_) {
225 upload_data_direct(buffer_);
226 }
227 else {
228 VKContext &context = *VKContext::get();
229 upload_data_via_staging_buffer(context);
230 }
231 if (usage_ == GPU_USAGE_STATIC) {
232 MEM_SAFE_DELETE(data_);
233 }
234 data_uploaded_ = true;
235
238 }
239}
240
241void VKVertexBuffer::allocate()
242{
243 VkBufferUsageFlags vk_buffer_usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
244 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
245 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT |
246 VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT |
247 VK_BUFFER_USAGE_TRANSFER_DST_BIT;
249 vk_buffer_usage |= VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR |
250 VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
251 }
252
253 buffer_.create(size_alloc_get(),
254 vk_buffer_usage,
255 VMA_MEMORY_USAGE_AUTO,
256 VmaAllocationCreateFlags(0),
257 0.8f,
258 false,
259 "VertexBuffer");
260}
261
262} // namespace gpu
263} // namespace blender
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
Read Blender memory allocation and freeing.
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define MEM_realloc_uninitialized(vmemh, len)
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 clear(VKContext &context, uint32_t clear_value)
Definition vk_buffer.cc:174
void * mapped_memory_get() const
Definition vk_buffer.hh:158
static VKContext * get()
render_graph::VKResourceStateTracker resources
Definition vk_device.hh:270
VkDevice vk_handle() const
Definition vk_device.hh:338
VolkDeviceTable functions
Definition vk_device.hh:280
void copy_from_device(VKContext &context)
void copy_to_device(VKContext &context)
void texel_buffer_bind(VKVertexBuffer &vertex_buffer, int binding)
void storage_buffer_bind(BindSpaceStorageBuffers::Type resource_type, void *resource, int binding)
void copy_sub(VertBuf &source_buf, uint source_first_vertex, uint dest_first_vertex, uint vertex_len) override
Copy from part of source vertex buffer into the vertex buffer.
void wrap_handle(uint64_t handle) override
void bind_as_ssbo(uint binding) override
void bind_as_texture(uint binding) override
void update_sub(uint start_offset, uint data_size_in_bytes, const void *data) override
void read(void *data) const override
MutableSpan< T > data()
void register_view(ResourceHandle resource_handle, VkView vk_view)
#define LOG(level)
Definition log.h:97
void object_label(GLenum type, GLuint object, const char *name)
Definition gl_debug.cc:331
BLI_INLINE MTLTexture * unwrap(Texture *tex)
GPUCapabilities GCaps
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
unsigned char uchar
Definition BKE_text.h:58
@ GPU_VERTBUF_DATA_UPLOADED
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