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
|
|
|
|
2024-12-16 10:09:33 +01:00
|
|
|
if (!data_uploaded_ && buffer_.is_mapped()) {
|
|
|
|
|
buffer_.update_immediately(data_);
|
|
|
|
|
MEM_SAFE_FREE(data_);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
VKContext &context = *VKContext::get();
|
|
|
|
|
VKStagingBuffer staging_buffer(buffer_, VKStagingBuffer::Direction::HostToDevice);
|
|
|
|
|
staging_buffer.host_buffer_get().update_immediately(data_);
|
|
|
|
|
staging_buffer.copy_to_device(context);
|
|
|
|
|
MEM_SAFE_FREE(data_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data_uploaded_ = true;
|
2023-04-26 08:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VKIndexBuffer::upload_data()
|
|
|
|
|
{
|
|
|
|
|
ensure_updated();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VKIndexBuffer::bind_as_ssbo(uint binding)
|
|
|
|
|
{
|
2025-01-23 15:40:45 +01:00
|
|
|
if (is_subrange_) {
|
|
|
|
|
src_->bind_as_ssbo(binding);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-26 10:59:45 +02:00
|
|
|
VKContext::get()->state_manager_get().storage_buffer_bind(
|
|
|
|
|
BindSpaceStorageBuffers::Type::IndexBuffer, this, binding);
|
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);
|
2024-04-19 12:08:57 +02:00
|
|
|
staging_buffer.host_buffer_get().read(context, 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
|
|
|
{
|
2023-11-24 13:52:48 +01:00
|
|
|
buffer_.create(size_get(),
|
|
|
|
|
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT |
|
|
|
|
|
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
2025-01-14 17:41:35 +01:00
|
|
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
2025-01-24 11:54:59 +01:00
|
|
|
VkMemoryPropertyFlags(0),
|
|
|
|
|
VmaAllocationCreateFlags(0));
|
2023-05-04 10:06:48 +02:00
|
|
|
debug::object_label(buffer_.vk_handle(), "IndexBuffer");
|
2023-02-21 15:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
2024-06-14 08:46:33 +02:00
|
|
|
const VKBuffer &VKIndexBuffer::buffer_get() const
|
|
|
|
|
{
|
|
|
|
|
return is_subrange_ ? unwrap(src_)->buffer_ : buffer_;
|
|
|
|
|
}
|
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
|