Blender V5.3
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
18namespace blender {
19
20static CLG_LogRef LOG = {"gpu.vulkan"};
21
22namespace gpu {
23
24VKStorageBuffer::VKStorageBuffer(size_t size, GPUUsageType usage, const char *name)
25 : StorageBuf(size, name), usage_(usage)
26{
27 UNUSED_VARS(usage_);
28}
30{
31 if (async_read_buffer_) {
32 MEM_delete(async_read_buffer_);
33 async_read_buffer_ = nullptr;
34 }
35}
36
38{
39 VKContext &context = *VKContext::get();
41 if (!buffer_.is_allocated()) {
43 "Unable to upload data to storage buffer as the storage buffer could not be "
44 "allocated on GPU.");
45 return;
46 }
47
48 if (usage_ == GPU_USAGE_STREAM) {
49 const VKDevice &device = VKBackend::get().device;
50 VKStreamingBuffer &streaming_buffer = *context.get_or_create_streaming_buffer(
51 buffer_, device.physical_device_properties_get().limits.minStorageBufferOffsetAlignment);
52 offset_ = streaming_buffer.update(context, data, usage_size_in_bytes_);
53 return;
54 }
55
56 VKStagingBuffer staging_buffer(
58 VKBuffer &buffer = staging_buffer.host_buffer_get();
59 if (buffer.is_allocated()) {
61 staging_buffer.copy_to_device(context);
62 }
63 else {
65 &LOG,
66 "Unable to upload data to storage buffer via a staging buffer as the staging buffer "
67 "could not be allocated. Storage buffer will be filled with on zeros to reduce "
68 "drawing artifacts due to read from uninitialized memory.");
69 buffer_.clear(context, 0u);
70 }
71}
72
74{
75 if (!buffer_.is_allocated()) {
76 allocate();
77 }
78}
79
80void VKStorageBuffer::allocate()
81{
82 const VkBufferUsageFlags buffer_usage_flags = VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT |
83 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
84 VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
85 VK_BUFFER_USAGE_TRANSFER_DST_BIT;
86 buffer_.create(size_in_bytes_,
87 buffer_usage_flags,
88 VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE,
89 VmaAllocationCreateFlags(0),
90 0.8f,
91 false,
92 name_);
93}
94
96{
97 VKContext &context = *VKContext::get();
98 context.state_manager_get().storage_buffer_bind(
100}
101
103{
104 VKContext *context = VKContext::get();
105 if (context) {
106 context->state_manager_get().storage_buffer_unbind(this);
107 }
108}
109
111{
113 VKContext &context = *VKContext::get();
114 buffer_.clear(context, clear_value);
115}
116
117void VKStorageBuffer::copy_sub(VertBuf *src, uint dst_offset, uint src_offset, uint copy_size)
118{
120
121 VKVertexBuffer &src_vertex_buffer = *unwrap(src);
122 src_vertex_buffer.upload();
123
125 copy_buffer.src_buffer = src_vertex_buffer.resource();
126 copy_buffer.dst_buffer = resource();
127 copy_buffer.region.srcOffset = src_offset;
128 copy_buffer.region.dstOffset = dst_offset;
129 copy_buffer.region.size = copy_size;
130
131 VKContext &context = *VKContext::get();
132 context.render_graph().add_node(copy_buffer);
133}
134
136{
137 if (async_read_buffer_ != nullptr) {
138 return;
139 }
141 VKContext &context = *VKContext::get();
142
143 async_read_buffer_ = MEM_new<VKStagingBuffer>(
145 async_read_buffer_->copy_from_device(context);
146 async_read_buffer_->host_buffer_get().async_flush_to_host(context);
147}
148
150{
151 if (async_read_buffer_ == nullptr) {
153 }
154
155 VKContext &context = *VKContext::get();
156 async_read_buffer_->host_buffer_get().read_async(context, data);
157 MEM_delete(async_read_buffer_);
158 async_read_buffer_ = nullptr;
159}
160
161} // namespace gpu
162} // namespace blender
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define UNUSED_VARS(...)
StorageBuf(size_t size, const char *name)
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 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
static VKContext * get()
const VkPhysicalDeviceProperties & physical_device_properties_get() const
Definition vk_device.hh:294
void copy_to_device(VKContext &context)
const VKResourceWithHandle< VkBuffer > & resource() const
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
VkDeviceSize update(VKContext &context, const void *data, size_t data_size)
const VKResourceWithHandle< VkBuffer > & resource() const
BLI_INLINE MTLTexture * unwrap(Texture *tex)
BMesh Iterators.
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