Blender V4.5
vk_storage_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#include "vk_shader.hh"
10#include "vk_staging_buffer.hh"
11#include "vk_state_manager.hh"
12#include "vk_vertex_buffer.hh"
13
14#include "vk_storage_buffer.hh"
15
16#include "CLG_log.h"
17
18static CLG_LogRef LOG = {"gpu.vulkan"};
19
20namespace blender::gpu {
21
22VKStorageBuffer::VKStorageBuffer(size_t size, GPUUsageType usage, const char *name)
23 : StorageBuf(size, name), usage_(usage)
24{
25 UNUSED_VARS(usage_);
26}
28{
29 if (async_read_buffer_) {
30 MEM_delete(async_read_buffer_);
31 async_read_buffer_ = nullptr;
32 }
33}
34
36{
37 VKContext &context = *VKContext::get();
40 VKBuffer &buffer = staging_buffer.host_buffer_get();
41 if (buffer.is_allocated()) {
43 staging_buffer.copy_to_device(context);
44 }
45 else {
47 &LOG,
48 "Unable to upload data to storage buffer via a staging buffer as the staging buffer "
49 "could not be allocated. Storage buffer will be filled with on zeros to reduce "
50 "drawing artifacts due to read from uninitialized memory.");
51 buffer_.clear(context, 0u);
52 }
53}
54
56{
57 if (!buffer_.is_allocated()) {
58 allocate();
59 }
60}
61
62void VKStorageBuffer::allocate()
63{
64 const VkBufferUsageFlags buffer_usage_flags = VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT |
65 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
66 VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
67 VK_BUFFER_USAGE_TRANSFER_DST_BIT;
68 buffer_.create(size_in_bytes_,
69 buffer_usage_flags,
70 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
71 VkMemoryPropertyFlags(0),
72 VmaAllocationCreateFlags(0),
73 0.8f);
74 BLI_assert(buffer_.is_allocated());
76}
77
79{
80 VKContext &context = *VKContext::get();
81 context.state_manager_get().storage_buffer_bind(
83}
84
86{
87 VKContext *context = VKContext::get();
88 if (context) {
89 context->state_manager_get().storage_buffer_unbind(this);
90 }
91}
92
93void VKStorageBuffer::clear(uint32_t clear_value)
94{
96 VKContext &context = *VKContext::get();
97 buffer_.clear(context, clear_value);
98}
99
100void VKStorageBuffer::copy_sub(VertBuf *src, uint dst_offset, uint src_offset, uint copy_size)
101{
103
104 VKVertexBuffer &src_vertex_buffer = *unwrap(src);
105 src_vertex_buffer.upload();
106
108 copy_buffer.src_buffer = src_vertex_buffer.vk_handle();
109 copy_buffer.dst_buffer = vk_handle();
110 copy_buffer.region.srcOffset = src_offset;
111 copy_buffer.region.dstOffset = dst_offset;
112 copy_buffer.region.size = copy_size;
113
114 VKContext &context = *VKContext::get();
115 context.render_graph().add_node(copy_buffer);
116}
117
119{
120 if (async_read_buffer_ != nullptr) {
121 return;
122 }
124 VKContext &context = *VKContext::get();
125
126 async_read_buffer_ = MEM_new<VKStagingBuffer>(
128 async_read_buffer_->copy_from_device(context);
129 async_read_buffer_->host_buffer_get().async_flush_to_host(context);
130}
131
133{
134 if (async_read_buffer_ == nullptr) {
136 }
137
138 VKContext &context = *VKContext::get();
139 async_read_buffer_->host_buffer_get().read_async(context, data);
140 MEM_delete(async_read_buffer_);
141 async_read_buffer_ = nullptr;
142}
143
144} // namespace blender::gpu
#define BLI_assert(a)
Definition BLI_assert.h:46
unsigned int uint
#define UNUSED_VARS(...)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:182
BMesh const char void * data
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
StorageBuf(size_t size, const char *name)
void update_immediately(const void *data) const
Definition vk_buffer.cc:112
bool is_allocated() const
Definition vk_buffer.hh:146
bool create(size_t size, VkBufferUsageFlags buffer_usage, VkMemoryPropertyFlags required_flags, VkMemoryPropertyFlags preferred_flags, VmaAllocationCreateFlags vma_allocation_flags, float priority, bool export_memory=false)
Definition vk_buffer.cc:23
VkBuffer vk_handle() const
Definition vk_buffer.hh:102
static VKContext * get()
void copy_to_device(VKContext &context)
void update(const void *data) override
void clear(uint32_t clear_value) override
void copy_sub(VertBuf *src, uint dst_offset, uint src_offset, uint copy_size) override
void read(void *data) override
VKStorageBuffer(size_t size, GPUUsageType usage, const char *name)
void bind(int slot) override
#define LOG(severity)
Definition log.h:32
void object_label(GLenum type, GLuint object, const char *name)
Definition gl_debug.cc:329
static Context * unwrap(GPUContext *ctx)
static CLG_LogRef LOG