2016-08-13 12:57:50 +00:00
|
|
|
// Copyright 2016 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2020-09-15 12:43:41 +00:00
|
|
|
#include "VideoBackends/Vulkan/VKStreamBuffer.h"
|
2016-10-01 03:07:50 +00:00
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdint>
|
2016-10-01 03:07:50 +00:00
|
|
|
#include <functional>
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
#include "Common/Align.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
#include "Common/Assert.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
|
|
|
|
|
|
|
#include "VideoBackends/Vulkan/CommandBufferManager.h"
|
|
|
|
#include "VideoBackends/Vulkan/VulkanContext.h"
|
|
|
|
|
|
|
|
namespace Vulkan
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
StreamBuffer::StreamBuffer(VkBufferUsageFlags usage, u32 size) : m_usage(usage), m_size(size)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
StreamBuffer::~StreamBuffer()
|
|
|
|
{
|
2022-10-07 21:48:11 +00:00
|
|
|
// VMA_ALLOCATION_CREATE_MAPPED_BIT automatically handles unmapping for us
|
2016-08-13 12:57:50 +00:00
|
|
|
if (m_buffer != VK_NULL_HANDLE)
|
2022-10-07 21:48:11 +00:00
|
|
|
g_command_buffer_mgr->DeferBufferDestruction(m_buffer, m_alloc);
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
std::unique_ptr<StreamBuffer> StreamBuffer::Create(VkBufferUsageFlags usage, u32 size)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
std::unique_ptr<StreamBuffer> buffer = std::make_unique<StreamBuffer>(usage, size);
|
|
|
|
if (!buffer->AllocateBuffer())
|
2016-08-13 12:57:50 +00:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
bool StreamBuffer::AllocateBuffer()
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
// Create the buffer descriptor
|
|
|
|
VkBufferCreateInfo buffer_create_info = {
|
|
|
|
VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType
|
|
|
|
nullptr, // const void* pNext
|
|
|
|
0, // VkBufferCreateFlags flags
|
2019-02-15 01:59:50 +00:00
|
|
|
static_cast<VkDeviceSize>(m_size), // VkDeviceSize size
|
2016-08-13 12:57:50 +00:00
|
|
|
m_usage, // VkBufferUsageFlags usage
|
|
|
|
VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode
|
|
|
|
0, // uint32_t queueFamilyIndexCount
|
|
|
|
nullptr // const uint32_t* pQueueFamilyIndices
|
|
|
|
};
|
|
|
|
|
2022-10-07 21:48:11 +00:00
|
|
|
VmaAllocationCreateInfo alloc_create_info = {};
|
|
|
|
alloc_create_info.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
|
|
|
|
VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT |
|
|
|
|
VMA_ALLOCATION_CREATE_MAPPED_BIT;
|
|
|
|
alloc_create_info.usage =
|
|
|
|
VMA_MEMORY_USAGE_AUTO_PREFER_HOST; // Host visible VRAM is slower in practice
|
|
|
|
alloc_create_info.pool = VK_NULL_HANDLE;
|
|
|
|
alloc_create_info.pUserData = nullptr;
|
|
|
|
alloc_create_info.priority = 0.0;
|
|
|
|
alloc_create_info.requiredFlags = 0;
|
|
|
|
alloc_create_info.preferredFlags = 0;
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2022-10-07 21:48:11 +00:00
|
|
|
VkBuffer buffer = VK_NULL_HANDLE;
|
|
|
|
VmaAllocation alloc = VK_NULL_HANDLE;
|
|
|
|
VmaAllocationInfo alloc_info;
|
|
|
|
VkResult res = vmaCreateBuffer(g_vulkan_context->GetMemoryAllocator(), &buffer_create_info,
|
|
|
|
&alloc_create_info, &buffer, &alloc, &alloc_info);
|
2016-08-13 12:57:50 +00:00
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
{
|
2022-10-07 21:48:11 +00:00
|
|
|
LOG_VULKAN_ERROR(res, "vmaCreateBuffer failed: ");
|
2016-08-13 12:57:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy the backings for the buffer after the command buffer executes
|
2022-10-07 21:48:11 +00:00
|
|
|
// VMA_ALLOCATION_CREATE_MAPPED_BIT automatically handles unmapping for us
|
2016-08-13 12:57:50 +00:00
|
|
|
if (m_buffer != VK_NULL_HANDLE)
|
2022-10-07 21:48:11 +00:00
|
|
|
g_command_buffer_mgr->DeferBufferDestruction(m_buffer, m_alloc);
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
// Replace with the new buffer
|
|
|
|
m_buffer = buffer;
|
2022-10-07 21:48:11 +00:00
|
|
|
m_alloc = alloc;
|
|
|
|
m_host_pointer = reinterpret_cast<u8*>(alloc_info.pMappedData);
|
2016-08-13 12:57:50 +00:00
|
|
|
m_current_offset = 0;
|
|
|
|
m_current_gpu_position = 0;
|
|
|
|
m_tracked_fences.clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
bool StreamBuffer::ReserveMemory(u32 num_bytes, u32 alignment)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
const u32 required_bytes = num_bytes + alignment;
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
// Check for sane allocations
|
2019-02-15 01:59:50 +00:00
|
|
|
if (required_bytes > m_size)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Attempting to allocate {} bytes from a {} byte stream buffer", num_bytes,
|
|
|
|
m_size);
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is the GPU behind or up to date with our current offset?
|
2019-02-15 01:59:50 +00:00
|
|
|
UpdateCurrentFencePosition();
|
2016-08-13 12:57:50 +00:00
|
|
|
if (m_current_offset >= m_current_gpu_position)
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
const u32 remaining_bytes = m_size - m_current_offset;
|
2016-08-13 12:57:50 +00:00
|
|
|
if (required_bytes <= remaining_bytes)
|
|
|
|
{
|
|
|
|
// Place at the current position, after the GPU position.
|
2019-02-15 01:59:50 +00:00
|
|
|
m_current_offset = Common::AlignUp(m_current_offset, alignment);
|
2016-08-13 12:57:50 +00:00
|
|
|
m_last_allocation_size = num_bytes;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for space at the start of the buffer
|
|
|
|
// We use < here because we don't want to have the case of m_current_offset ==
|
|
|
|
// m_current_gpu_position. That would mean the code above would assume the
|
|
|
|
// GPU has caught up to us, which it hasn't.
|
2019-02-15 01:59:50 +00:00
|
|
|
if (required_bytes < m_current_gpu_position)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
// Reset offset to zero, since we're allocating behind the gpu now
|
|
|
|
m_current_offset = 0;
|
|
|
|
m_last_allocation_size = num_bytes;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is the GPU ahead of our current offset?
|
|
|
|
if (m_current_offset < m_current_gpu_position)
|
|
|
|
{
|
|
|
|
// We have from m_current_offset..m_current_gpu_position space to use.
|
2019-02-15 01:59:50 +00:00
|
|
|
const u32 remaining_bytes = m_current_gpu_position - m_current_offset;
|
2016-08-13 12:57:50 +00:00
|
|
|
if (required_bytes < remaining_bytes)
|
|
|
|
{
|
|
|
|
// Place at the current position, since this is still behind the GPU.
|
2019-02-15 01:59:50 +00:00
|
|
|
m_current_offset = Common::AlignUp(m_current_offset, alignment);
|
2016-08-13 12:57:50 +00:00
|
|
|
m_last_allocation_size = num_bytes;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can we find a fence to wait on that will give us enough memory?
|
2019-02-15 01:59:50 +00:00
|
|
|
if (WaitForClearSpace(required_bytes))
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
m_current_offset = Common::AlignUp(m_current_offset, alignment);
|
2016-08-13 12:57:50 +00:00
|
|
|
m_last_allocation_size = num_bytes;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
// We tried everything we could, and still couldn't get anything. This means that too much space
|
|
|
|
// in the buffer is being used by the command buffer currently being recorded. Therefore, the
|
|
|
|
// only option is to execute it, and wait until it's done.
|
2016-08-13 12:57:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
void StreamBuffer::CommitMemory(u32 final_num_bytes)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
ASSERT((m_current_offset + final_num_bytes) <= m_size);
|
2018-03-15 00:34:35 +00:00
|
|
|
ASSERT(final_num_bytes <= m_last_allocation_size);
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
// For non-coherent mappings, flush the memory range
|
2022-10-07 21:48:11 +00:00
|
|
|
// vmaFlushAllocation checks whether the allocation uses a coherent memory type internally
|
|
|
|
vmaFlushAllocation(g_vulkan_context->GetMemoryAllocator(), m_alloc, m_current_offset,
|
|
|
|
final_num_bytes);
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
m_current_offset += final_num_bytes;
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
void StreamBuffer::UpdateCurrentFencePosition()
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
// Don't create a tracking entry if the GPU is caught up with the buffer.
|
|
|
|
if (m_current_offset == m_current_gpu_position)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Has the offset changed since the last fence?
|
2019-03-17 05:59:22 +00:00
|
|
|
const u64 counter = g_command_buffer_mgr->GetCurrentFenceCounter();
|
|
|
|
if (!m_tracked_fences.empty() && m_tracked_fences.back().first == counter)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
// Still haven't executed a command buffer, so just update the offset.
|
|
|
|
m_tracked_fences.back().second = m_current_offset;
|
2016-08-13 12:57:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-17 05:59:22 +00:00
|
|
|
// New buffer, so update the GPU position while we're at it.
|
|
|
|
UpdateGPUPosition();
|
|
|
|
m_tracked_fences.emplace_back(counter, m_current_offset);
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-17 05:59:22 +00:00
|
|
|
void StreamBuffer::UpdateGPUPosition()
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2019-03-17 05:59:22 +00:00
|
|
|
auto start = m_tracked_fences.begin();
|
|
|
|
auto end = start;
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2019-03-17 05:59:22 +00:00
|
|
|
const u64 completed_counter = g_command_buffer_mgr->GetCompletedFenceCounter();
|
|
|
|
while (end != m_tracked_fences.end() && completed_counter >= end->first)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2019-03-17 05:59:22 +00:00
|
|
|
m_current_gpu_position = end->second;
|
|
|
|
++end;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
2019-03-17 05:59:22 +00:00
|
|
|
|
|
|
|
if (start != end)
|
|
|
|
m_tracked_fences.erase(start, end);
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
bool StreamBuffer::WaitForClearSpace(u32 num_bytes)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
u32 new_offset = 0;
|
2019-03-17 05:59:22 +00:00
|
|
|
u32 new_gpu_position = 0;
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
auto iter = m_tracked_fences.begin();
|
2019-09-14 20:48:48 +00:00
|
|
|
for (; iter != m_tracked_fences.end(); ++iter)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
// Would this fence bring us in line with the GPU?
|
2016-11-30 12:13:55 +00:00
|
|
|
// This is the "last resort" case, where a command buffer execution has been forced
|
|
|
|
// after no additional data has been written to it, so we can assume that after the
|
|
|
|
// fence has been signaled the entire buffer is now consumed.
|
2019-02-15 01:59:50 +00:00
|
|
|
u32 gpu_position = iter->second;
|
2016-11-30 12:13:55 +00:00
|
|
|
if (m_current_offset == gpu_position)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
new_offset = 0;
|
2019-03-17 05:59:22 +00:00
|
|
|
new_gpu_position = 0;
|
2016-08-13 12:57:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-11-30 12:13:55 +00:00
|
|
|
// Assuming that we wait for this fence, are we allocating in front of the GPU?
|
|
|
|
if (m_current_offset > gpu_position)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2019-03-17 05:59:22 +00:00
|
|
|
// This would suggest the GPU has now followed us and wrapped around, so we have from
|
|
|
|
// m_current_position..m_size free, as well as and 0..gpu_position.
|
|
|
|
const u32 remaining_space_after_offset = m_size - m_current_offset;
|
|
|
|
if (remaining_space_after_offset >= num_bytes)
|
|
|
|
{
|
|
|
|
// Switch to allocating in front of the GPU, using the remainder of the buffer.
|
|
|
|
new_offset = m_current_offset;
|
|
|
|
new_gpu_position = gpu_position;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-11-30 12:13:55 +00:00
|
|
|
// We can wrap around to the start, behind the GPU, if there is enough space.
|
|
|
|
// We use > here because otherwise we'd end up lining up with the GPU, and then the
|
|
|
|
// allocator would assume that the GPU has consumed what we just wrote.
|
2016-08-13 12:57:50 +00:00
|
|
|
if (gpu_position > num_bytes)
|
|
|
|
{
|
|
|
|
new_offset = 0;
|
2019-03-17 05:59:22 +00:00
|
|
|
new_gpu_position = gpu_position;
|
2016-08-13 12:57:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-30 12:13:55 +00:00
|
|
|
// We're currently allocating behind the GPU. This would give us between the current
|
|
|
|
// offset and the GPU position worth of space to work with. Again, > because we can't
|
|
|
|
// align the GPU position with the buffer offset.
|
2019-02-15 01:59:50 +00:00
|
|
|
u32 available_space_inbetween = gpu_position - m_current_offset;
|
2016-11-30 12:13:55 +00:00
|
|
|
if (available_space_inbetween > num_bytes)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2016-11-30 12:13:55 +00:00
|
|
|
// Leave the offset as-is, but update the GPU position.
|
|
|
|
new_offset = m_current_offset;
|
2019-03-17 05:59:22 +00:00
|
|
|
new_gpu_position = gpu_position;
|
2016-11-30 12:13:55 +00:00
|
|
|
break;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Did any fences satisfy this condition?
|
2019-02-15 01:59:50 +00:00
|
|
|
// Has the command buffer been executed yet? If not, the caller should execute it.
|
|
|
|
if (iter == m_tracked_fences.end() ||
|
2019-03-17 05:59:22 +00:00
|
|
|
iter->first == g_command_buffer_mgr->GetCurrentFenceCounter())
|
2019-02-15 01:59:50 +00:00
|
|
|
{
|
2016-08-13 12:57:50 +00:00
|
|
|
return false;
|
2019-02-15 01:59:50 +00:00
|
|
|
}
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
// Wait until this fence is signaled. This will fire the callback, updating the GPU position.
|
2019-03-17 05:59:22 +00:00
|
|
|
g_command_buffer_mgr->WaitForFenceCounter(iter->first);
|
|
|
|
m_tracked_fences.erase(m_tracked_fences.begin(),
|
|
|
|
m_current_offset == iter->second ? m_tracked_fences.end() : ++iter);
|
2016-08-13 12:57:50 +00:00
|
|
|
m_current_offset = new_offset;
|
2019-03-17 05:59:22 +00:00
|
|
|
m_current_gpu_position = new_gpu_position;
|
2016-08-13 12:57:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Vulkan
|