Blender V5.0
vk_uniform_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#include "vk_context.hh"
11#include "vk_shader.hh"
13#include "vk_staging_buffer.hh"
14#include "vk_state_manager.hh"
15
16#include "CLG_log.h"
17
18static CLG_LogRef LOG = {"gpu.vulkan"};
19
20namespace blender::gpu {
21
23{
24 if (!buffer_.is_allocated()) {
25 allocate();
26 }
27
28 if (data) {
29 void *data_copy = MEM_mallocN(size_in_bytes_, __func__);
30 memcpy(data_copy, data, size_in_bytes_);
31 VKContext &context = *VKContext::get();
32 buffer_.update_render_graph(context, data_copy);
33 data_uploaded_ = true;
34 }
35}
36
37void VKUniformBuffer::allocate()
38{
39 buffer_.create(size_in_bytes_,
40 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
41 VK_BUFFER_USAGE_TRANSFER_DST_BIT,
42 VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE,
43 VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
44 0.8f);
46}
47
49{
50 if (!buffer_.is_allocated()) {
51 allocate();
52 }
53 VKContext &context = *VKContext::get();
54 buffer_.clear(context, 0);
55 data_uploaded_ = true;
56}
57
59{
60 if (!buffer_.is_allocated()) {
61 allocate();
62 if (!buffer_.is_allocated()) {
64 "Unable to allocate uniform buffer [%s]. Most likely an out of memory issue.",
65 name_);
66 return;
67 }
68 }
69
70 /* Upload attached data, during bind time. */
71 if (data_) {
72 if (!data_uploaded_ && buffer_.is_mapped()) {
73 buffer_.update_immediately(data_);
75 data_ = nullptr;
76 }
77 else {
78 VKContext &context = *VKContext::get();
79 buffer_.update_render_graph(context, std::move(data_));
80 data_ = nullptr;
81 }
82 data_uploaded_ = true;
83 }
84}
85
87{
88 VKContext &context = *VKContext::get();
89 context.state_manager_get().uniform_buffer_bind(this, slot);
90}
91
93{
94 VKContext &context = *VKContext::get();
95 context.state_manager_get().storage_buffer_bind(
97}
98
100{
101 const VKContext *context = VKContext::get();
102 if (context != nullptr) {
103 VKStateManager &state_manager = context->state_manager_get();
104 state_manager.uniform_buffer_unbind(this);
105 state_manager.storage_buffer_unbind(this);
106 }
107}
108
109} // namespace blender::gpu
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
BMesh const char void * data
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
static VKContext * get()
void uniform_buffer_unbind(VKUniformBuffer *uniform_buffer)
void storage_buffer_unbind(void *resource)
void bind_as_ssbo(int slot) override
void update(const void *data) override
void bind(int slot) override
#define LOG(level)
Definition log.h:97
void * MEM_mallocN(size_t len, const char *str)
Definition mallocn.cc:128
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
void object_label(GLenum type, GLuint object, const char *name)
Definition gl_debug.cc:329
static CLG_LogRef LOG