Blender V4.5
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 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
43 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
44 VmaAllocationCreateFlags(0),
45 0.8f);
47}
48
50{
51 if (!buffer_.is_allocated()) {
52 allocate();
53 }
54 VKContext &context = *VKContext::get();
55 buffer_.clear(context, 0);
56 data_uploaded_ = true;
57}
58
60{
61 if (!buffer_.is_allocated()) {
62 allocate();
63 if (!buffer_.is_allocated()) {
65 "Unable to allocate uniform buffer [%s]. Most likely an out of memory issue.",
66 name_);
67 return;
68 }
69 }
70
71 /* Upload attached data, during bind time. */
72 if (data_) {
73 if (!data_uploaded_ && buffer_.is_mapped()) {
74 buffer_.update_immediately(data_);
76 data_ = nullptr;
77 }
78 else {
79 VKContext &context = *VKContext::get();
80 buffer_.update_render_graph(context, std::move(data_));
81 data_ = nullptr;
82 }
83 data_uploaded_ = true;
84 }
85}
86
88{
89 VKContext &context = *VKContext::get();
90 context.state_manager_get().uniform_buffer_bind(this, slot);
91}
92
94{
95 VKContext &context = *VKContext::get();
96 context.state_manager_get().storage_buffer_bind(
98}
99
101{
102 const VKContext *context = VKContext::get();
103 if (context != nullptr) {
104 VKStateManager &state_manager = context->state_manager_get();
105 state_manager.uniform_buffer_unbind(this);
106 state_manager.storage_buffer_unbind(this);
107 }
108}
109
110} // namespace blender::gpu
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:182
BMesh const char void * data
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 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(severity)
Definition log.h:32
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