2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2022 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2022-10-31 16:01:02 +01:00
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
|
* \ingroup gpu
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "vk_uniform_buffer.hh"
|
2023-02-23 14:51:34 +01:00
|
|
|
#include "vk_context.hh"
|
2023-04-25 15:30:55 +02:00
|
|
|
#include "vk_shader.hh"
|
|
|
|
|
#include "vk_shader_interface.hh"
|
2023-11-24 13:52:48 +01:00
|
|
|
#include "vk_staging_buffer.hh"
|
2023-05-11 13:01:56 +02:00
|
|
|
#include "vk_state_manager.hh"
|
2022-10-31 16:01:02 +01:00
|
|
|
|
|
|
|
|
namespace blender::gpu {
|
|
|
|
|
|
2023-02-23 14:51:34 +01:00
|
|
|
void VKUniformBuffer::update(const void *data)
|
2022-10-31 16:01:02 +01:00
|
|
|
{
|
2023-02-23 14:51:34 +01:00
|
|
|
if (!buffer_.is_allocated()) {
|
2023-05-04 10:06:48 +02:00
|
|
|
allocate();
|
2023-02-23 14:51:34 +01:00
|
|
|
}
|
2024-10-01 14:22:56 +02:00
|
|
|
|
|
|
|
|
/* TODO: when buffer is mapped and newly created we should use `buffer_.update_immediately`. */
|
|
|
|
|
void *data_copy = MEM_mallocN(size_in_bytes_, __func__);
|
|
|
|
|
memcpy(data_copy, data, size_in_bytes_);
|
2023-11-24 13:52:48 +01:00
|
|
|
VKContext &context = *VKContext::get();
|
2024-10-01 14:22:56 +02:00
|
|
|
buffer_.update_render_graph(context, data_copy);
|
2023-02-23 14:51:34 +01:00
|
|
|
}
|
|
|
|
|
|
2023-05-04 10:06:48 +02:00
|
|
|
void VKUniformBuffer::allocate()
|
2023-02-23 14:51:34 +01:00
|
|
|
{
|
2023-09-18 13:43:16 +02:00
|
|
|
buffer_.create(size_in_bytes_,
|
|
|
|
|
GPU_USAGE_STATIC,
|
2023-11-03 14:42:12 +01:00
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
|
2023-11-24 13:52:48 +01:00
|
|
|
VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
2024-06-14 09:44:59 +02:00
|
|
|
false);
|
2023-05-04 10:06:48 +02:00
|
|
|
debug::object_label(buffer_.vk_handle(), name_);
|
2022-10-31 16:01:02 +01:00
|
|
|
}
|
|
|
|
|
|
2023-04-25 15:30:55 +02:00
|
|
|
void VKUniformBuffer::clear_to_zero()
|
|
|
|
|
{
|
|
|
|
|
if (!buffer_.is_allocated()) {
|
2023-05-04 10:06:48 +02:00
|
|
|
allocate();
|
2023-04-25 15:30:55 +02:00
|
|
|
}
|
2023-05-04 10:06:48 +02:00
|
|
|
VKContext &context = *VKContext::get();
|
2023-04-25 15:30:55 +02:00
|
|
|
buffer_.clear(context, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-26 10:59:45 +02:00
|
|
|
void VKUniformBuffer::ensure_updated()
|
2023-04-25 15:30:55 +02:00
|
|
|
{
|
|
|
|
|
if (!buffer_.is_allocated()) {
|
2023-05-04 10:06:48 +02:00
|
|
|
allocate();
|
2023-04-25 15:30:55 +02:00
|
|
|
}
|
|
|
|
|
|
2023-09-18 13:43:16 +02:00
|
|
|
/* Upload attached data, during bind time. */
|
|
|
|
|
if (data_) {
|
2024-10-01 14:22:56 +02:00
|
|
|
/* TODO: when buffer is mapped and newly created we should use `buffer_.update_immediately`. */
|
|
|
|
|
VKContext &context = *VKContext::get();
|
|
|
|
|
buffer_.update_render_graph(context, std::move(data_));
|
|
|
|
|
data_ = nullptr;
|
2023-09-18 13:43:16 +02:00
|
|
|
}
|
2023-04-25 15:30:55 +02:00
|
|
|
}
|
2022-11-15 20:15:04 +01:00
|
|
|
|
2023-04-25 15:30:55 +02:00
|
|
|
void VKUniformBuffer::bind(int slot)
|
|
|
|
|
{
|
2023-05-11 13:01:56 +02:00
|
|
|
VKContext &context = *VKContext::get();
|
|
|
|
|
context.state_manager_get().uniform_buffer_bind(this, slot);
|
2023-04-25 15:30:55 +02:00
|
|
|
}
|
2022-10-31 16:01:02 +01:00
|
|
|
|
2023-04-25 15:30:55 +02:00
|
|
|
void VKUniformBuffer::bind_as_ssbo(int slot)
|
|
|
|
|
{
|
2023-06-15 08:14:37 +02:00
|
|
|
VKContext &context = *VKContext::get();
|
2024-09-26 10:59:45 +02:00
|
|
|
context.state_manager_get().storage_buffer_bind(
|
|
|
|
|
BindSpaceStorageBuffers::Type::UniformBuffer, this, slot);
|
2023-04-25 15:30:55 +02:00
|
|
|
}
|
2022-11-15 16:23:18 +01:00
|
|
|
|
2023-05-11 13:01:56 +02:00
|
|
|
void VKUniformBuffer::unbind()
|
|
|
|
|
{
|
2023-06-15 08:14:37 +02:00
|
|
|
const VKContext *context = VKContext::get();
|
|
|
|
|
if (context != nullptr) {
|
|
|
|
|
VKStateManager &state_manager = context->state_manager_get();
|
|
|
|
|
state_manager.uniform_buffer_unbind(this);
|
2024-09-26 10:59:45 +02:00
|
|
|
state_manager.storage_buffer_unbind(this);
|
2023-06-08 09:46:00 +02:00
|
|
|
}
|
2023-05-11 13:01:56 +02:00
|
|
|
}
|
2022-10-31 16:01:02 +01:00
|
|
|
|
2023-01-31 15:49:04 +11:00
|
|
|
} // namespace blender::gpu
|