This adds initial support for ReBAR capable platforms. It ensures that when allocating buffers that should not be host visible, still tries to allocate in host visible memory. When there is space in this memory heap the buffer will be automatically mapped to host memory. When mapped staging buffers can be skipped when the buffer was newly created. In order to make better usage of ReBAR the `VKBuffer::create` function will need to be revisit. It currently hides to much options to allocate in the correct memory heap. This change isn't part of this PR. Using shader_balls.blend rendering the first 50 frames in main takes 1516ms. When using ReBAR it takes 1416ms. ``` Operating system: Linux-6.8.0-49-generic-x86_64-with-glibc2.39 64 Bits, X11 UI Graphics card: AMD Radeon Pro W7700 (RADV NAVI32) Advanced Micro Devices radv Mesa 24.3.1 - kisak-mesa PPA Vulkan Backend ``` Pull Request: https://projects.blender.org/blender/blender/pulls/131856
102 lines
2.4 KiB
C++
102 lines
2.4 KiB
C++
/* SPDX-FileCopyrightText: 2022 Blender Authors
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
/** \file
|
|
* \ingroup gpu
|
|
*/
|
|
|
|
#include "vk_uniform_buffer.hh"
|
|
#include "vk_context.hh"
|
|
#include "vk_shader.hh"
|
|
#include "vk_shader_interface.hh"
|
|
#include "vk_staging_buffer.hh"
|
|
#include "vk_state_manager.hh"
|
|
|
|
namespace blender::gpu {
|
|
|
|
void VKUniformBuffer::update(const void *data)
|
|
{
|
|
if (!buffer_.is_allocated()) {
|
|
allocate();
|
|
}
|
|
|
|
if (!data_uploaded_ && buffer_.is_mapped()) {
|
|
buffer_.update_immediately(data);
|
|
}
|
|
else {
|
|
void *data_copy = MEM_mallocN(size_in_bytes_, __func__);
|
|
memcpy(data_copy, data, size_in_bytes_);
|
|
VKContext &context = *VKContext::get();
|
|
buffer_.update_render_graph(context, data_copy);
|
|
}
|
|
data_uploaded_ = true;
|
|
}
|
|
|
|
void VKUniformBuffer::allocate()
|
|
{
|
|
buffer_.create(size_in_bytes_,
|
|
GPU_USAGE_STATIC,
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
|
|
VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
|
false);
|
|
debug::object_label(buffer_.vk_handle(), name_);
|
|
}
|
|
|
|
void VKUniformBuffer::clear_to_zero()
|
|
{
|
|
if (!buffer_.is_allocated()) {
|
|
allocate();
|
|
}
|
|
VKContext &context = *VKContext::get();
|
|
buffer_.clear(context, 0);
|
|
data_uploaded_ = true;
|
|
}
|
|
|
|
void VKUniformBuffer::ensure_updated()
|
|
{
|
|
if (!buffer_.is_allocated()) {
|
|
allocate();
|
|
}
|
|
|
|
/* Upload attached data, during bind time. */
|
|
if (data_) {
|
|
if (!data_uploaded_ && buffer_.is_mapped()) {
|
|
buffer_.update_immediately(data_);
|
|
MEM_freeN(data_);
|
|
data_ = nullptr;
|
|
}
|
|
else {
|
|
VKContext &context = *VKContext::get();
|
|
buffer_.update_render_graph(context, std::move(data_));
|
|
data_ = nullptr;
|
|
}
|
|
data_uploaded_ = true;
|
|
}
|
|
}
|
|
|
|
void VKUniformBuffer::bind(int slot)
|
|
{
|
|
VKContext &context = *VKContext::get();
|
|
context.state_manager_get().uniform_buffer_bind(this, slot);
|
|
}
|
|
|
|
void VKUniformBuffer::bind_as_ssbo(int slot)
|
|
{
|
|
VKContext &context = *VKContext::get();
|
|
context.state_manager_get().storage_buffer_bind(
|
|
BindSpaceStorageBuffers::Type::UniformBuffer, this, slot);
|
|
}
|
|
|
|
void VKUniformBuffer::unbind()
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
|
|
} // namespace blender::gpu
|