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
|
|
|
|
|
*/
|
2023-02-21 15:03:12 +01:00
|
|
|
#include "vk_shader.hh"
|
|
|
|
|
#include "vk_shader_interface.hh"
|
2023-06-15 08:14:37 +02:00
|
|
|
#include "vk_state_manager.hh"
|
2022-10-31 16:01:02 +01:00
|
|
|
#include "vk_vertex_buffer.hh"
|
|
|
|
|
|
|
|
|
|
#include "vk_storage_buffer.hh"
|
|
|
|
|
|
|
|
|
|
namespace blender::gpu {
|
|
|
|
|
|
2023-06-15 08:14:37 +02:00
|
|
|
VKStorageBuffer::VKStorageBuffer(int size, GPUUsageType usage, const char *name)
|
|
|
|
|
: StorageBuf(size, name), usage_(usage)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 15:03:12 +01:00
|
|
|
void VKStorageBuffer::update(const void *data)
|
2023-06-12 14:56:38 +02:00
|
|
|
{
|
|
|
|
|
ensure_allocated();
|
|
|
|
|
buffer_.update(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VKStorageBuffer::ensure_allocated()
|
2023-02-21 15:03:12 +01:00
|
|
|
{
|
|
|
|
|
if (!buffer_.is_allocated()) {
|
2023-05-04 10:06:48 +02:00
|
|
|
allocate();
|
2023-02-21 15:03:12 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 10:06:48 +02:00
|
|
|
void VKStorageBuffer::allocate()
|
2022-10-31 16:01:02 +01:00
|
|
|
{
|
2023-05-04 10:06:48 +02:00
|
|
|
buffer_.create(size_in_bytes_,
|
2023-03-17 13:48:39 +01:00
|
|
|
usage_,
|
2023-06-12 14:56:38 +02:00
|
|
|
static_cast<VkBufferUsageFlagBits>(VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT |
|
|
|
|
|
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
|
2023-03-17 13:48:39 +01:00
|
|
|
VK_BUFFER_USAGE_TRANSFER_DST_BIT));
|
2023-05-04 10:06:48 +02:00
|
|
|
debug::object_label(buffer_.vk_handle(), name_);
|
2022-10-31 16:01:02 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-21 15:03:12 +01:00
|
|
|
void VKStorageBuffer::bind(int slot)
|
2022-10-31 16:01:02 +01:00
|
|
|
{
|
2023-06-15 08:14:37 +02:00
|
|
|
VKContext &context = *VKContext::get();
|
|
|
|
|
context.state_manager_get().storage_buffer_bind(*this, slot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VKStorageBuffer::bind(int slot, shader::ShaderCreateInfo::Resource::BindType bind_type)
|
|
|
|
|
{
|
2023-02-21 15:03:12 +01:00
|
|
|
VKContext &context = *VKContext::get();
|
|
|
|
|
VKShader *shader = static_cast<VKShader *>(context.shader);
|
2023-06-15 08:14:37 +02:00
|
|
|
ensure_allocated();
|
2023-02-21 15:03:12 +01:00
|
|
|
const VKShaderInterface &shader_interface = shader->interface_get();
|
2023-05-11 08:44:57 +02:00
|
|
|
const std::optional<VKDescriptorSet::Location> location =
|
2023-06-15 08:14:37 +02:00
|
|
|
shader_interface.descriptor_set_location(bind_type, slot);
|
|
|
|
|
if (location) {
|
|
|
|
|
shader->pipeline_get().descriptor_set_get().bind(*this, *location);
|
|
|
|
|
}
|
2022-10-31 16:01:02 +01:00
|
|
|
}
|
|
|
|
|
|
2023-06-15 08:14:37 +02:00
|
|
|
void VKStorageBuffer::unbind()
|
|
|
|
|
{
|
|
|
|
|
unbind_from_active_context();
|
|
|
|
|
}
|
2022-10-31 16:01:02 +01:00
|
|
|
|
2023-03-17 13:48:39 +01:00
|
|
|
void VKStorageBuffer::clear(uint32_t clear_value)
|
2022-10-31 16:01:02 +01:00
|
|
|
{
|
2023-06-12 14:56:38 +02:00
|
|
|
ensure_allocated();
|
2023-03-17 13:48:39 +01:00
|
|
|
VKContext &context = *VKContext::get();
|
|
|
|
|
buffer_.clear(context, clear_value);
|
2022-10-31 16:01:02 +01:00
|
|
|
}
|
2023-03-09 18:46:28 +01:00
|
|
|
|
2023-09-18 13:44:03 +02:00
|
|
|
void VKStorageBuffer::copy_sub(VertBuf *src, uint dst_offset, uint src_offset, uint copy_size)
|
2022-10-31 16:01:02 +01:00
|
|
|
{
|
2023-09-18 13:44:03 +02:00
|
|
|
ensure_allocated();
|
|
|
|
|
|
|
|
|
|
VKVertexBuffer &src_vertex_buffer = *unwrap(src);
|
|
|
|
|
src_vertex_buffer.upload();
|
|
|
|
|
|
|
|
|
|
VkBufferCopy region = {};
|
|
|
|
|
region.srcOffset = src_offset;
|
|
|
|
|
region.dstOffset = dst_offset;
|
|
|
|
|
region.size = copy_size;
|
|
|
|
|
|
|
|
|
|
VKContext &context = *VKContext::get();
|
|
|
|
|
VKCommandBuffer &command_buffer = context.command_buffer_get();
|
|
|
|
|
command_buffer.copy(buffer_, src_vertex_buffer.vk_handle(), Span<VkBufferCopy>(®ion, 1));
|
|
|
|
|
command_buffer.submit();
|
2022-10-31 16:01:02 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-21 15:03:12 +01:00
|
|
|
void VKStorageBuffer::read(void *data)
|
2022-10-31 16:01:02 +01:00
|
|
|
{
|
2023-06-12 14:56:38 +02:00
|
|
|
ensure_allocated();
|
2023-05-04 10:06:48 +02:00
|
|
|
VKContext &context = *VKContext::get();
|
2023-02-21 15:03:12 +01:00
|
|
|
VKCommandBuffer &command_buffer = context.command_buffer_get();
|
|
|
|
|
command_buffer.submit();
|
|
|
|
|
|
2023-03-09 09:27:42 +01:00
|
|
|
buffer_.read(data);
|
2022-10-31 16:01:02 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-17 15:58:30 +11:00
|
|
|
} // namespace blender::gpu
|