Blender V4.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
10#include "vk_context.hh"
11#include "vk_shader.hh"
13#include "vk_staging_buffer.hh"
14#include "vk_state_manager.hh"
15
16namespace blender::gpu {
17
18void VKUniformBuffer::update(const void *data)
19{
20 if (!buffer_.is_allocated()) {
21 allocate();
22 }
23
24 /* TODO: when buffer is mapped and newly created we should use `buffer_.update_immediately`. */
25 void *data_copy = MEM_mallocN(size_in_bytes_, __func__);
26 memcpy(data_copy, data, size_in_bytes_);
27 VKContext &context = *VKContext::get();
28 buffer_.update_render_graph(context, data_copy);
29}
30
31void VKUniformBuffer::allocate()
32{
33 buffer_.create(size_in_bytes_,
35 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
36 VK_BUFFER_USAGE_TRANSFER_DST_BIT,
37 false);
39}
40
42{
43 if (!buffer_.is_allocated()) {
44 allocate();
45 }
46 VKContext &context = *VKContext::get();
47 buffer_.clear(context, 0);
48}
49
51{
52 if (!buffer_.is_allocated()) {
53 allocate();
54 }
55
56 /* Upload attached data, during bind time. */
57 if (data_) {
58 /* TODO: when buffer is mapped and newly created we should use `buffer_.update_immediately`. */
59 VKContext &context = *VKContext::get();
60 buffer_.update_render_graph(context, std::move(data_));
61 data_ = nullptr;
62 }
63}
64
66{
67 VKContext &context = *VKContext::get();
68 context.state_manager_get().uniform_buffer_bind(this, slot);
69}
70
72{
73 VKContext &context = *VKContext::get();
74 context.state_manager_get().storage_buffer_bind(
76}
77
79{
80 const VKContext *context = VKContext::get();
81 if (context != nullptr) {
82 VKStateManager &state_manager = context->state_manager_get();
83 state_manager.uniform_buffer_unbind(this);
84 state_manager.storage_buffer_unbind(this);
85 }
86}
87
88} // namespace blender::gpu
@ GPU_USAGE_STATIC
bool is_allocated() const
Definition vk_buffer.cc:22
bool create(size_t size, GPUUsageType usage, VkBufferUsageFlags buffer_usage, bool is_host_visible=true)
Definition vk_buffer.cc:53
VkBuffer vk_handle() const
Definition vk_buffer.hh:69
void clear(VKContext &context, uint32_t clear_value)
Definition vk_buffer.cc:133
void update_render_graph(VKContext &context, void *data) const
Definition vk_buffer.cc:116
static VKContext * get()
Definition vk_context.hh:97
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
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
void object_label(GLenum type, GLuint object, const char *name)
Definition gl_debug.cc:344