2022-10-31 16:01:02 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later
|
2023-02-22 10:16:42 +01:00
|
|
|
* Copyright 2022 Blender Foundation */
|
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();
|
|
|
|
|
descriptor_set.bind(*this, *location);
|
|
|
|
|
}
|
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
|
|
|
/* Uniform buffers can be bound without an shader. */
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
bind(slot, shader::ShaderCreateInfo::Resource::BindType::STORAGE_BUFFER);
|
|
|
|
|
}
|
2022-11-15 16:23:18 +01:00
|
|
|
|
2023-05-11 13:01:56 +02:00
|
|
|
void VKUniformBuffer::unbind()
|
|
|
|
|
{
|
|
|
|
|
VKContext &context = *VKContext::get();
|
|
|
|
|
context.state_manager_get().uniform_buffer_unbind(this);
|
|
|
|
|
}
|
2022-10-31 16:01:02 +01:00
|
|
|
|
2023-01-31 15:49:04 +11:00
|
|
|
} // namespace blender::gpu
|