Blender V5.3
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
18namespace blender {
19
20static CLG_LogRef LOG = {"gpu.vulkan"};
21
22namespace gpu {
23
25{
26 if (!buffer_.is_allocated()) {
27 allocate();
28 }
29
30 if (data) {
31 void *data_copy = MEM_new_uninitialized(size_in_bytes_, __func__);
32 memcpy(data_copy, data, size_in_bytes_);
33 VKContext &context = *VKContext::get();
34 buffer_.update_render_graph(context, data_copy);
35 data_uploaded_ = true;
36 }
37}
38
39void VKUniformBuffer::allocate()
40{
41 buffer_.create(size_in_bytes_,
42 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
43 VK_BUFFER_USAGE_TRANSFER_DST_BIT,
44 VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE,
45 VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
46 0.8f,
47 false,
48 name_);
49}
50
52{
53 if (!buffer_.is_allocated()) {
54 allocate();
55 }
56 VKContext &context = *VKContext::get();
57 buffer_.clear(context, 0);
58 data_uploaded_ = true;
59}
60
62{
63 if (!buffer_.is_allocated()) {
64 allocate();
65 if (!buffer_.is_allocated()) {
67 "Unable to allocate uniform buffer [%s]. Most likely an out of memory issue.",
68 name_);
69 return;
70 }
71 }
72
73 /* Upload attached data, during bind time. */
74 if (data_) {
75 if (!data_uploaded_ && buffer_.is_mapped()) {
76 buffer_.update_immediately(data_);
78 data_ = nullptr;
79 }
80 else {
81 VKContext &context = *VKContext::get();
82 buffer_.update_render_graph(context, std::move(data_));
83 data_ = nullptr;
84 }
85 data_uploaded_ = true;
86 }
87}
88
90{
91 VKContext &context = *VKContext::get();
92 context.state_manager_get().uniform_buffer_bind(this, slot);
93}
94
96{
97 VKContext &context = *VKContext::get();
98 context.state_manager_get().storage_buffer_bind(
100}
101
103{
104 const VKContext *context = VKContext::get();
105 if (context != nullptr) {
106 VKStateManager &state_manager = context->state_manager_get();
107 state_manager.uniform_buffer_unbind(this);
108 state_manager.storage_buffer_unbind(this);
109 }
110}
111
112} // namespace gpu
113} // namespace blender
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
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()
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
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void MEM_delete_void(void *vmemh)
BMesh const char void * data
static CLG_LogRef LOG
Definition logging.cpp:12