2023-05-31 16:19:06 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2022 Blender Foundation
|
|
|
|
|
*
|
|
|
|
|
* 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-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
|
|
|
}
|
2023-03-09 09:27:42 +01:00
|
|
|
buffer_.update(data);
|
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-05-04 10:06:48 +02:00
|
|
|
buffer_.create(size_in_bytes_, GPU_USAGE_STATIC, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VKUniformBuffer::bind(int slot, shader::ShaderCreateInfo::Resource::BindType bind_type)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
VKShader *shader = static_cast<VKShader *>(context.shader);
|
|
|
|
|
const VKShaderInterface &shader_interface = shader->interface_get();
|
2023-05-11 08:44:57 +02:00
|
|
|
const std::optional<VKDescriptorSet::Location> location =
|
|
|
|
|
shader_interface.descriptor_set_location(bind_type, slot);
|
|
|
|
|
if (location) {
|
|
|
|
|
VKDescriptorSetTracker &descriptor_set = shader->pipeline_get().descriptor_set_get();
|
2023-06-15 08:14:37 +02:00
|
|
|
/* TODO: move to descriptor set. */
|
|
|
|
|
if (bind_type == shader::ShaderCreateInfo::Resource::BindType::UNIFORM_BUFFER) {
|
|
|
|
|
descriptor_set.bind(*this, *location);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
descriptor_set.bind_as_ssbo(*this, *location);
|
|
|
|
|
}
|
2023-05-11 08:44:57 +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();
|
|
|
|
|
context.state_manager_get().storage_buffer_bind(*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);
|
|
|
|
|
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
|