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_index_buffer.hh"
|
2023-02-21 15:03:12 +01:00
|
|
|
#include "vk_shader.hh"
|
|
|
|
|
#include "vk_shader_interface.hh"
|
2023-11-24 13:52:48 +01:00
|
|
|
#include "vk_staging_buffer.hh"
|
2023-06-15 08:14:37 +02:00
|
|
|
#include "vk_state_manager.hh"
|
2022-10-31 16:01:02 +01:00
|
|
|
|
|
|
|
|
namespace blender::gpu {
|
|
|
|
|
|
2023-04-26 08:09:28 +02:00
|
|
|
void VKIndexBuffer::ensure_updated()
|
2022-10-31 16:01:02 +01:00
|
|
|
{
|
2023-04-26 08:09:28 +02:00
|
|
|
if (is_subrange_) {
|
|
|
|
|
src_->upload_data();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
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-11-24 13:52:48 +01:00
|
|
|
if (data_ == nullptr) {
|
|
|
|
|
return;
|
2023-04-26 08:09:28 +02:00
|
|
|
}
|
2023-11-24 13:52:48 +01:00
|
|
|
|
|
|
|
|
VKContext &context = *VKContext::get();
|
|
|
|
|
VKStagingBuffer staging_buffer(buffer_, VKStagingBuffer::Direction::HostToDevice);
|
|
|
|
|
staging_buffer.host_buffer_get().update(data_);
|
|
|
|
|
staging_buffer.copy_to_device(context);
|
|
|
|
|
MEM_SAFE_FREE(data_);
|
2023-04-26 08:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VKIndexBuffer::upload_data()
|
|
|
|
|
{
|
|
|
|
|
ensure_updated();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VKIndexBuffer::bind(VKContext &context)
|
|
|
|
|
{
|
2023-11-20 15:48:06 +01:00
|
|
|
context.command_buffers_get().bind(buffer_get(), to_vk_index_type(index_type_));
|
2023-04-26 08:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VKIndexBuffer::bind_as_ssbo(uint binding)
|
|
|
|
|
{
|
2023-06-15 08:14:37 +02:00
|
|
|
VKContext::get()->state_manager_get().storage_buffer_bind(*this, binding);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-14 13:55:37 +01:00
|
|
|
void VKIndexBuffer::bind(int binding,
|
|
|
|
|
shader::ShaderCreateInfo::Resource::BindType bind_type,
|
|
|
|
|
const GPUSamplerState /*sampler_state*/)
|
2023-06-15 08:14:37 +02:00
|
|
|
{
|
|
|
|
|
BLI_assert(bind_type == shader::ShaderCreateInfo::Resource::BindType::STORAGE_BUFFER);
|
2023-04-26 08:09:28 +02:00
|
|
|
ensure_updated();
|
|
|
|
|
|
|
|
|
|
VKContext &context = *VKContext::get();
|
2023-02-21 15:03:12 +01: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 =
|
2023-06-15 08:14:37 +02:00
|
|
|
shader_interface.descriptor_set_location(bind_type, binding);
|
|
|
|
|
if (location) {
|
2023-11-16 15:03:47 +01:00
|
|
|
context.descriptor_set_get().bind_as_ssbo(*this, *location);
|
2023-06-15 08:14:37 +02:00
|
|
|
}
|
2022-10-31 16:01:02 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-21 15:03:12 +01:00
|
|
|
void VKIndexBuffer::read(uint32_t *data) const
|
2022-10-31 16:01:02 +01:00
|
|
|
{
|
2023-02-21 15:03:12 +01:00
|
|
|
VKContext &context = *VKContext::get();
|
2023-11-24 13:52:48 +01:00
|
|
|
VKStagingBuffer staging_buffer(buffer_, VKStagingBuffer::Direction::DeviceToHost);
|
|
|
|
|
staging_buffer.copy_from_device(context);
|
|
|
|
|
staging_buffer.host_buffer_get().read(data);
|
2022-10-31 16:01:02 +01:00
|
|
|
}
|
|
|
|
|
|
2023-05-12 12:12:43 +02:00
|
|
|
void VKIndexBuffer::update_sub(uint /*start*/, uint /*len*/, const void * /*data*/)
|
|
|
|
|
{
|
|
|
|
|
NOT_YET_IMPLEMENTED
|
|
|
|
|
}
|
2022-10-31 16:01:02 +01:00
|
|
|
|
2023-05-12 12:12:43 +02:00
|
|
|
void VKIndexBuffer::strip_restart_indices()
|
|
|
|
|
{
|
|
|
|
|
NOT_YET_IMPLEMENTED
|
|
|
|
|
}
|
2022-10-31 16:01:02 +01:00
|
|
|
|
2023-05-04 10:06:48 +02:00
|
|
|
void VKIndexBuffer::allocate()
|
2023-02-21 15:03:12 +01:00
|
|
|
{
|
|
|
|
|
GPUUsageType usage = data_ == nullptr ? GPU_USAGE_DEVICE_ONLY : GPU_USAGE_STATIC;
|
2023-11-24 13:52:48 +01:00
|
|
|
buffer_.create(size_get(),
|
|
|
|
|
usage,
|
|
|
|
|
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT |
|
|
|
|
|
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
|
|
|
|
false);
|
2023-05-04 10:06:48 +02:00
|
|
|
debug::object_label(buffer_.vk_handle(), "IndexBuffer");
|
2023-02-21 15:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-20 15:48:06 +01:00
|
|
|
VKBuffer &VKIndexBuffer::buffer_get()
|
2023-05-12 12:12:43 +02:00
|
|
|
{
|
2023-11-20 15:48:06 +01:00
|
|
|
return is_subrange_ ? unwrap(src_)->buffer_ : buffer_;
|
2023-05-12 12:12:43 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-31 15:49:04 +11:00
|
|
|
} // namespace blender::gpu
|