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
|
|
|
|
2019-06-01 11:55:09 +00:00
|
|
|
#include "VideoBackends/Vulkan/CommandBufferManager.h"
|
|
|
|
|
|
|
|
#include <array>
|
2016-08-13 12:57:50 +00:00
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
#include "Common/Assert.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
2020-08-22 09:55:31 +00:00
|
|
|
#include "Common/Thread.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
#include "VideoBackends/Vulkan/VulkanContext.h"
|
|
|
|
|
|
|
|
namespace Vulkan
|
|
|
|
{
|
|
|
|
CommandBufferManager::CommandBufferManager(bool use_threaded_submission)
|
2022-09-30 23:05:41 +00:00
|
|
|
: m_use_threaded_submission(use_threaded_submission)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandBufferManager::~CommandBufferManager()
|
|
|
|
{
|
2019-01-27 02:41:09 +00:00
|
|
|
// If the worker thread is enabled, stop and block until it exits.
|
2016-08-13 12:57:50 +00:00
|
|
|
if (m_use_threaded_submission)
|
|
|
|
{
|
2022-09-30 23:05:41 +00:00
|
|
|
WaitForWorkerThreadIdle();
|
2016-08-13 12:57:50 +00:00
|
|
|
m_submit_loop->Stop();
|
|
|
|
m_submit_thread.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
DestroyCommandBuffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CommandBufferManager::Initialize()
|
|
|
|
{
|
|
|
|
if (!CreateCommandBuffers())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (m_use_threaded_submission && !CreateSubmitThread())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CommandBufferManager::CreateCommandBuffers()
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
static constexpr VkSemaphoreCreateInfo semaphore_create_info = {
|
|
|
|
VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, nullptr, 0};
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
VkDevice device = g_vulkan_context->GetDevice();
|
2016-11-03 12:42:41 +00:00
|
|
|
VkResult res;
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2022-09-30 23:17:38 +00:00
|
|
|
for (CmdBufferResources& resources : m_command_buffers)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2016-09-28 13:04:02 +00:00
|
|
|
resources.init_command_buffer_used = false;
|
2019-02-15 01:59:50 +00:00
|
|
|
resources.semaphore_used = false;
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2016-11-03 12:42:41 +00:00
|
|
|
VkCommandPoolCreateInfo pool_info = {VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, nullptr, 0,
|
|
|
|
g_vulkan_context->GetGraphicsQueueFamilyIndex()};
|
|
|
|
res = vkCreateCommandPool(g_vulkan_context->GetDevice(), &pool_info, nullptr,
|
|
|
|
&resources.command_pool);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_VULKAN_ERROR(res, "vkCreateCommandPool failed: ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
VkCommandBufferAllocateInfo buffer_info = {
|
|
|
|
VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, nullptr, resources.command_pool,
|
2016-08-13 12:57:50 +00:00
|
|
|
VK_COMMAND_BUFFER_LEVEL_PRIMARY, static_cast<uint32_t>(resources.command_buffers.size())};
|
|
|
|
|
2016-11-03 12:42:41 +00:00
|
|
|
res = vkAllocateCommandBuffers(device, &buffer_info, resources.command_buffers.data());
|
2016-08-13 12:57:50 +00:00
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_VULKAN_ERROR(res, "vkAllocateCommandBuffers failed: ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
VkFenceCreateInfo fence_info = {VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, nullptr,
|
|
|
|
VK_FENCE_CREATE_SIGNALED_BIT};
|
|
|
|
|
|
|
|
res = vkCreateFence(device, &fence_info, nullptr, &resources.fence);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_VULKAN_ERROR(res, "vkCreateFence failed: ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
res = vkCreateSemaphore(device, &semaphore_create_info, nullptr, &resources.semaphore);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_VULKAN_ERROR(res, "vkCreateSemaphore failed: ");
|
|
|
|
return false;
|
|
|
|
}
|
2022-09-30 23:23:10 +00:00
|
|
|
}
|
2019-02-15 01:59:50 +00:00
|
|
|
|
2022-09-30 23:23:10 +00:00
|
|
|
for (VkDescriptorPool& descriptor_pool : m_descriptor_pools)
|
|
|
|
{
|
2016-08-13 12:57:50 +00:00
|
|
|
// TODO: A better way to choose the number of descriptors.
|
2019-06-01 11:55:09 +00:00
|
|
|
const std::array<VkDescriptorPoolSize, 5> pool_sizes{{
|
|
|
|
{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 500000},
|
|
|
|
{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 500000},
|
|
|
|
{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 16},
|
|
|
|
{VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 16384},
|
|
|
|
{VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 16384},
|
|
|
|
}};
|
|
|
|
|
|
|
|
const VkDescriptorPoolCreateInfo pool_create_info = {
|
|
|
|
VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
|
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
100000, // tweak this
|
|
|
|
static_cast<u32>(pool_sizes.size()),
|
|
|
|
pool_sizes.data(),
|
|
|
|
};
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2022-09-30 23:23:10 +00:00
|
|
|
res = vkCreateDescriptorPool(device, &pool_create_info, nullptr, &descriptor_pool);
|
2016-08-13 12:57:50 +00:00
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_VULKAN_ERROR(res, "vkCreateDescriptorPool failed: ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
res = vkCreateSemaphore(device, &semaphore_create_info, nullptr, &m_present_semaphore);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_VULKAN_ERROR(res, "vkCreateSemaphore failed: ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-30 23:17:38 +00:00
|
|
|
// Activate the first command buffer. BeginCommandBuffer moves forward, so start with the last
|
|
|
|
m_current_cmd_buffer = static_cast<u32>(m_command_buffers.size()) - 1;
|
2019-02-15 01:59:50 +00:00
|
|
|
BeginCommandBuffer();
|
2016-08-13 12:57:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandBufferManager::DestroyCommandBuffers()
|
|
|
|
{
|
|
|
|
VkDevice device = g_vulkan_context->GetDevice();
|
|
|
|
|
2022-09-30 23:17:38 +00:00
|
|
|
for (CmdBufferResources& resources : m_command_buffers)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2019-01-27 02:41:09 +00:00
|
|
|
// The Vulkan spec section 5.2 says: "When a pool is destroyed, all command buffers allocated
|
|
|
|
// from the pool are freed.". So we don't need to free the command buffers, just the pools.
|
|
|
|
// We destroy the command pool first, to avoid any warnings from the validation layers about
|
|
|
|
// objects which are pending destruction being in-use.
|
|
|
|
if (resources.command_pool != VK_NULL_HANDLE)
|
|
|
|
vkDestroyCommandPool(device, resources.command_pool, nullptr);
|
|
|
|
|
|
|
|
// Destroy any pending objects.
|
2016-10-01 00:40:44 +00:00
|
|
|
for (auto& it : resources.cleanup_resources)
|
|
|
|
it();
|
2019-02-15 01:59:50 +00:00
|
|
|
|
|
|
|
if (resources.semaphore != VK_NULL_HANDLE)
|
|
|
|
vkDestroySemaphore(device, resources.semaphore, nullptr);
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
if (resources.fence != VK_NULL_HANDLE)
|
|
|
|
vkDestroyFence(device, resources.fence, nullptr);
|
2022-09-30 23:23:10 +00:00
|
|
|
}
|
2019-01-27 02:41:09 +00:00
|
|
|
|
2022-09-30 23:23:10 +00:00
|
|
|
for (VkDescriptorPool descriptor_pool : m_descriptor_pools)
|
|
|
|
{
|
|
|
|
if (descriptor_pool != VK_NULL_HANDLE)
|
|
|
|
vkDestroyDescriptorPool(device, descriptor_pool, nullptr);
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
2019-02-15 01:59:50 +00:00
|
|
|
|
|
|
|
vkDestroySemaphore(device, m_present_semaphore, nullptr);
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VkDescriptorSet CommandBufferManager::AllocateDescriptorSet(VkDescriptorSetLayout set_layout)
|
|
|
|
{
|
2022-09-30 23:17:38 +00:00
|
|
|
VkDescriptorSetAllocateInfo allocate_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
|
|
|
|
nullptr, GetCurrentDescriptorPool(), 1, &set_layout};
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
VkDescriptorSet descriptor_set;
|
|
|
|
VkResult res =
|
|
|
|
vkAllocateDescriptorSets(g_vulkan_context->GetDevice(), &allocate_info, &descriptor_set);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
{
|
|
|
|
// Failing to allocate a descriptor set is not a fatal error, we can
|
|
|
|
// recover by moving to the next command buffer.
|
|
|
|
return VK_NULL_HANDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return descriptor_set;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CommandBufferManager::CreateSubmitThread()
|
|
|
|
{
|
|
|
|
m_submit_loop = std::make_unique<Common::BlockingLoop>();
|
|
|
|
m_submit_thread = std::thread([this]() {
|
2020-08-22 09:55:31 +00:00
|
|
|
Common::SetCurrentThreadName("Vulkan CommandBufferManager SubmitThread");
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
m_submit_loop->Run([this]() {
|
|
|
|
PendingCommandBufferSubmit submit;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard(m_pending_submit_lock);
|
|
|
|
if (m_pending_submits.empty())
|
|
|
|
{
|
|
|
|
m_submit_loop->AllowSleep();
|
2022-09-30 23:05:41 +00:00
|
|
|
m_submit_worker_idle = true;
|
|
|
|
m_submit_worker_condvar.notify_all();
|
2016-08-13 12:57:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
submit = m_pending_submits.front();
|
|
|
|
m_pending_submits.pop_front();
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
SubmitCommandBuffer(submit.command_buffer_index, submit.present_swap_chain,
|
|
|
|
submit.present_image_index);
|
2022-09-30 23:05:41 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard(m_pending_submit_lock);
|
|
|
|
if (m_pending_submits.empty())
|
|
|
|
{
|
|
|
|
m_submit_worker_idle = true;
|
|
|
|
m_submit_worker_condvar.notify_all();
|
|
|
|
}
|
|
|
|
}
|
2016-08-13 12:57:50 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandBufferManager::WaitForWorkerThreadIdle()
|
|
|
|
{
|
2022-09-30 23:05:41 +00:00
|
|
|
if (!m_use_threaded_submission)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::unique_lock lock{m_pending_submit_lock};
|
|
|
|
m_submit_worker_condvar.wait(lock, [&] { return m_submit_worker_idle; });
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-17 05:59:22 +00:00
|
|
|
void CommandBufferManager::WaitForFenceCounter(u64 fence_counter)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2019-03-17 05:59:22 +00:00
|
|
|
if (m_completed_fence_counter >= fence_counter)
|
|
|
|
return;
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2019-03-17 05:59:22 +00:00
|
|
|
// Find the first command buffer which covers this counter value.
|
2022-09-30 23:17:38 +00:00
|
|
|
u32 index = (m_current_cmd_buffer + 1) % NUM_COMMAND_BUFFERS;
|
|
|
|
while (index != m_current_cmd_buffer)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2022-09-30 23:17:38 +00:00
|
|
|
if (m_command_buffers[index].fence_counter >= fence_counter)
|
2016-08-13 12:57:50 +00:00
|
|
|
break;
|
2019-03-17 05:59:22 +00:00
|
|
|
|
|
|
|
index = (index + 1) % NUM_COMMAND_BUFFERS;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 23:17:38 +00:00
|
|
|
ASSERT(index != m_current_cmd_buffer);
|
2019-03-17 05:59:22 +00:00
|
|
|
WaitForCommandBufferCompletion(index);
|
|
|
|
}
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2019-03-17 05:59:22 +00:00
|
|
|
void CommandBufferManager::WaitForCommandBufferCompletion(u32 index)
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
// Ensure this command buffer has been submitted.
|
|
|
|
WaitForWorkerThreadIdle();
|
|
|
|
|
2022-09-30 23:17:38 +00:00
|
|
|
CmdBufferResources& resources = m_command_buffers[index];
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
// Wait for this command buffer to be completed.
|
2022-09-30 23:17:38 +00:00
|
|
|
VkResult res =
|
|
|
|
vkWaitForFences(g_vulkan_context->GetDevice(), 1, &resources.fence, VK_TRUE, UINT64_MAX);
|
2016-08-13 12:57:50 +00:00
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
LOG_VULKAN_ERROR(res, "vkWaitForFences failed: ");
|
|
|
|
|
2019-03-17 05:59:22 +00:00
|
|
|
// Clean up any resources for command buffers between the last known completed buffer and this
|
|
|
|
// now-completed command buffer. If we use >2 buffers, this may be more than one buffer.
|
2022-09-30 23:17:38 +00:00
|
|
|
const u64 now_completed_counter = resources.fence_counter;
|
|
|
|
u32 cleanup_index = (index + 1) % NUM_COMMAND_BUFFERS;
|
|
|
|
while (cleanup_index != index)
|
2019-03-17 05:59:22 +00:00
|
|
|
{
|
2022-09-30 23:17:38 +00:00
|
|
|
CmdBufferResources& cleanup_resources = m_command_buffers[cleanup_index];
|
|
|
|
if (cleanup_resources.fence_counter > now_completed_counter)
|
2019-03-17 05:59:22 +00:00
|
|
|
break;
|
|
|
|
|
2022-09-30 23:17:38 +00:00
|
|
|
if (cleanup_resources.fence_counter > m_completed_fence_counter)
|
2019-03-17 05:59:22 +00:00
|
|
|
{
|
2022-09-30 23:17:38 +00:00
|
|
|
for (auto& it : cleanup_resources.cleanup_resources)
|
2019-03-17 05:59:22 +00:00
|
|
|
it();
|
2022-09-30 23:17:38 +00:00
|
|
|
cleanup_resources.cleanup_resources.clear();
|
2019-03-17 05:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup_index = (cleanup_index + 1) % NUM_COMMAND_BUFFERS;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_completed_fence_counter = now_completed_counter;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CommandBufferManager::SubmitCommandBuffer(bool submit_on_worker_thread,
|
2019-03-17 05:59:22 +00:00
|
|
|
bool wait_for_completion,
|
2016-08-13 12:57:50 +00:00
|
|
|
VkSwapchainKHR present_swap_chain,
|
|
|
|
uint32_t present_image_index)
|
|
|
|
{
|
|
|
|
// End the current command buffer.
|
2022-09-30 23:17:38 +00:00
|
|
|
CmdBufferResources& resources = GetCurrentCmdBufferResources();
|
2016-08-13 12:57:50 +00:00
|
|
|
for (VkCommandBuffer command_buffer : resources.command_buffers)
|
|
|
|
{
|
|
|
|
VkResult res = vkEndCommandBuffer(command_buffer);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_VULKAN_ERROR(res, "vkEndCommandBuffer failed: ");
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Failed to end command buffer");
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Submitting off-thread?
|
2019-03-17 05:59:22 +00:00
|
|
|
if (m_use_threaded_submission && submit_on_worker_thread && !wait_for_completion)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
// Push to the pending submit queue.
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard(m_pending_submit_lock);
|
2022-09-30 23:05:41 +00:00
|
|
|
m_submit_worker_idle = false;
|
2022-09-30 23:17:38 +00:00
|
|
|
m_pending_submits.push_back({present_swap_chain, present_image_index, m_current_cmd_buffer});
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wake up the worker thread for a single iteration.
|
|
|
|
m_submit_loop->Wakeup();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-09-30 23:05:41 +00:00
|
|
|
WaitForWorkerThreadIdle();
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
// Pass through to normal submission path.
|
2022-09-30 23:17:38 +00:00
|
|
|
SubmitCommandBuffer(m_current_cmd_buffer, present_swap_chain, present_image_index);
|
2019-03-17 05:59:22 +00:00
|
|
|
if (wait_for_completion)
|
2022-09-30 23:17:38 +00:00
|
|
|
WaitForCommandBufferCompletion(m_current_cmd_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (present_swap_chain != VK_NULL_HANDLE)
|
|
|
|
{
|
|
|
|
m_current_frame = (m_current_frame + 1) % NUM_FRAMES_IN_FLIGHT;
|
|
|
|
|
|
|
|
// Wait for all command buffers that used the descriptor pool to finish
|
|
|
|
u32 cmd_buffer_index = (m_current_cmd_buffer + 1) % NUM_COMMAND_BUFFERS;
|
|
|
|
while (cmd_buffer_index != m_current_cmd_buffer)
|
|
|
|
{
|
|
|
|
CmdBufferResources& cmd_buffer = m_command_buffers[cmd_buffer_index];
|
|
|
|
if (cmd_buffer.frame_index == m_current_frame && cmd_buffer.fence_counter != 0 &&
|
|
|
|
cmd_buffer.fence_counter > m_completed_fence_counter)
|
|
|
|
{
|
|
|
|
WaitForCommandBufferCompletion(cmd_buffer_index);
|
|
|
|
}
|
|
|
|
cmd_buffer_index = (cmd_buffer_index + 1) % NUM_COMMAND_BUFFERS;
|
|
|
|
}
|
2022-09-30 23:23:10 +00:00
|
|
|
|
|
|
|
// Reset the descriptor pool
|
|
|
|
VkResult res =
|
|
|
|
vkResetDescriptorPool(g_vulkan_context->GetDevice(), GetCurrentDescriptorPool(), 0);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
LOG_VULKAN_ERROR(res, "vkResetDescriptorPool failed: ");
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
2019-02-15 01:59:50 +00:00
|
|
|
|
|
|
|
// Switch to next cmdbuffer.
|
|
|
|
BeginCommandBuffer();
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
void CommandBufferManager::SubmitCommandBuffer(u32 command_buffer_index,
|
2016-08-13 12:57:50 +00:00
|
|
|
VkSwapchainKHR present_swap_chain,
|
2019-02-15 01:59:50 +00:00
|
|
|
u32 present_image_index)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2022-09-30 23:17:38 +00:00
|
|
|
CmdBufferResources& resources = m_command_buffers[command_buffer_index];
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
// This may be executed on the worker thread, so don't modify any state of the manager class.
|
|
|
|
uint32_t wait_bits = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
|
|
|
VkSubmitInfo submit_info = {VK_STRUCTURE_TYPE_SUBMIT_INFO,
|
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
&wait_bits,
|
|
|
|
static_cast<u32>(resources.command_buffers.size()),
|
|
|
|
resources.command_buffers.data(),
|
|
|
|
0,
|
|
|
|
nullptr};
|
|
|
|
|
2016-09-28 13:04:02 +00:00
|
|
|
// If the init command buffer did not have any commands recorded, don't submit it.
|
2019-02-15 01:59:50 +00:00
|
|
|
if (!resources.init_command_buffer_used)
|
2016-09-28 13:04:02 +00:00
|
|
|
{
|
|
|
|
submit_info.commandBufferCount = 1;
|
2019-02-15 01:59:50 +00:00
|
|
|
submit_info.pCommandBuffers = &resources.command_buffers[1];
|
2016-09-28 13:04:02 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 09:30:17 +00:00
|
|
|
if (resources.semaphore_used)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
submit_info.pWaitSemaphores = &resources.semaphore;
|
2016-08-13 12:57:50 +00:00
|
|
|
submit_info.waitSemaphoreCount = 1;
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
if (present_swap_chain != VK_NULL_HANDLE)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
submit_info.signalSemaphoreCount = 1;
|
2019-02-15 01:59:50 +00:00
|
|
|
submit_info.pSignalSemaphores = &m_present_semaphore;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VkResult res =
|
|
|
|
vkQueueSubmit(g_vulkan_context->GetGraphicsQueue(), 1, &submit_info, resources.fence);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_VULKAN_ERROR(res, "vkQueueSubmit failed: ");
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Failed to submit command buffer.");
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do we have a swap chain to present?
|
|
|
|
if (present_swap_chain != VK_NULL_HANDLE)
|
|
|
|
{
|
|
|
|
// Should have a signal semaphore.
|
|
|
|
VkPresentInfoKHR present_info = {VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
|
|
|
|
nullptr,
|
|
|
|
1,
|
2019-02-15 01:59:50 +00:00
|
|
|
&m_present_semaphore,
|
2016-08-13 12:57:50 +00:00
|
|
|
1,
|
|
|
|
&present_swap_chain,
|
|
|
|
&present_image_index,
|
|
|
|
nullptr};
|
|
|
|
|
2019-09-30 15:10:08 +00:00
|
|
|
m_last_present_result = vkQueuePresentKHR(g_vulkan_context->GetPresentQueue(), &present_info);
|
|
|
|
if (m_last_present_result != VK_SUCCESS)
|
2017-09-16 05:53:08 +00:00
|
|
|
{
|
|
|
|
// VK_ERROR_OUT_OF_DATE_KHR is not fatal, just means we need to recreate our swap chain.
|
2020-01-31 09:12:10 +00:00
|
|
|
if (m_last_present_result != VK_ERROR_OUT_OF_DATE_KHR &&
|
|
|
|
m_last_present_result != VK_SUBOPTIMAL_KHR &&
|
2019-09-30 15:10:08 +00:00
|
|
|
m_last_present_result != VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT)
|
|
|
|
{
|
|
|
|
LOG_VULKAN_ERROR(m_last_present_result, "vkQueuePresentKHR failed: ");
|
|
|
|
}
|
2020-01-31 09:12:10 +00:00
|
|
|
|
|
|
|
// Don't treat VK_SUBOPTIMAL_KHR as fatal on Android. Android 10+ requires prerotation.
|
|
|
|
// See https://twitter.com/Themaister/status/1207062674011574273
|
|
|
|
#ifdef VK_USE_PLATFORM_ANDROID_KHR
|
|
|
|
if (m_last_present_result != VK_SUBOPTIMAL_KHR)
|
|
|
|
m_last_present_failed.Set();
|
|
|
|
#else
|
2019-09-30 15:10:08 +00:00
|
|
|
m_last_present_failed.Set();
|
2020-01-31 09:12:10 +00:00
|
|
|
#endif
|
2017-09-16 05:53:08 +00:00
|
|
|
}
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
void CommandBufferManager::BeginCommandBuffer()
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
// Move to the next command buffer.
|
2022-09-30 23:17:38 +00:00
|
|
|
const u32 next_buffer_index = (m_current_cmd_buffer + 1) % NUM_COMMAND_BUFFERS;
|
|
|
|
CmdBufferResources& resources = m_command_buffers[next_buffer_index];
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
// Wait for the GPU to finish with all resources for this command buffer.
|
2019-03-17 05:59:22 +00:00
|
|
|
if (resources.fence_counter > m_completed_fence_counter)
|
|
|
|
WaitForCommandBufferCompletion(next_buffer_index);
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
// Reset fence to unsignaled before starting.
|
|
|
|
VkResult res = vkResetFences(g_vulkan_context->GetDevice(), 1, &resources.fence);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
LOG_VULKAN_ERROR(res, "vkResetFences failed: ");
|
|
|
|
|
2016-11-03 12:42:41 +00:00
|
|
|
// Reset command pools to beginning since we can re-use the memory now
|
|
|
|
res = vkResetCommandPool(g_vulkan_context->GetDevice(), resources.command_pool, 0);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
LOG_VULKAN_ERROR(res, "vkResetCommandPool failed: ");
|
|
|
|
|
|
|
|
// Enable commands to be recorded to the two buffers again.
|
2016-08-13 12:57:50 +00:00
|
|
|
VkCommandBufferBeginInfo begin_info = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, nullptr,
|
|
|
|
VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, nullptr};
|
|
|
|
for (VkCommandBuffer command_buffer : resources.command_buffers)
|
|
|
|
{
|
|
|
|
res = vkBeginCommandBuffer(command_buffer, &begin_info);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
LOG_VULKAN_ERROR(res, "vkBeginCommandBuffer failed: ");
|
|
|
|
}
|
|
|
|
|
2016-11-03 12:42:41 +00:00
|
|
|
// Reset upload command buffer state
|
|
|
|
resources.init_command_buffer_used = false;
|
2019-02-15 01:59:50 +00:00
|
|
|
resources.semaphore_used = false;
|
2019-03-17 05:59:22 +00:00
|
|
|
resources.fence_counter = m_next_fence_counter++;
|
2022-09-30 23:17:38 +00:00
|
|
|
resources.frame_index = m_current_frame;
|
|
|
|
m_current_cmd_buffer = next_buffer_index;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2016-10-01 00:40:44 +00:00
|
|
|
void CommandBufferManager::DeferBufferDestruction(VkBuffer object)
|
|
|
|
{
|
2022-09-30 23:17:38 +00:00
|
|
|
CmdBufferResources& cmd_buffer_resources = GetCurrentCmdBufferResources();
|
|
|
|
cmd_buffer_resources.cleanup_resources.push_back(
|
2016-10-01 00:40:44 +00:00
|
|
|
[object]() { vkDestroyBuffer(g_vulkan_context->GetDevice(), object, nullptr); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandBufferManager::DeferBufferViewDestruction(VkBufferView object)
|
|
|
|
{
|
2022-09-30 23:17:38 +00:00
|
|
|
CmdBufferResources& cmd_buffer_resources = GetCurrentCmdBufferResources();
|
|
|
|
cmd_buffer_resources.cleanup_resources.push_back(
|
2016-10-01 00:40:44 +00:00
|
|
|
[object]() { vkDestroyBufferView(g_vulkan_context->GetDevice(), object, nullptr); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandBufferManager::DeferDeviceMemoryDestruction(VkDeviceMemory object)
|
|
|
|
{
|
2022-09-30 23:17:38 +00:00
|
|
|
CmdBufferResources& cmd_buffer_resources = GetCurrentCmdBufferResources();
|
|
|
|
cmd_buffer_resources.cleanup_resources.push_back(
|
2016-10-01 00:40:44 +00:00
|
|
|
[object]() { vkFreeMemory(g_vulkan_context->GetDevice(), object, nullptr); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandBufferManager::DeferFramebufferDestruction(VkFramebuffer object)
|
|
|
|
{
|
2022-09-30 23:17:38 +00:00
|
|
|
CmdBufferResources& cmd_buffer_resources = GetCurrentCmdBufferResources();
|
|
|
|
cmd_buffer_resources.cleanup_resources.push_back(
|
2016-10-01 00:40:44 +00:00
|
|
|
[object]() { vkDestroyFramebuffer(g_vulkan_context->GetDevice(), object, nullptr); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandBufferManager::DeferImageDestruction(VkImage object)
|
|
|
|
{
|
2022-09-30 23:17:38 +00:00
|
|
|
CmdBufferResources& cmd_buffer_resources = GetCurrentCmdBufferResources();
|
|
|
|
cmd_buffer_resources.cleanup_resources.push_back(
|
2016-10-01 00:40:44 +00:00
|
|
|
[object]() { vkDestroyImage(g_vulkan_context->GetDevice(), object, nullptr); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandBufferManager::DeferImageViewDestruction(VkImageView object)
|
|
|
|
{
|
2022-09-30 23:17:38 +00:00
|
|
|
CmdBufferResources& cmd_buffer_resources = GetCurrentCmdBufferResources();
|
|
|
|
cmd_buffer_resources.cleanup_resources.push_back(
|
2016-10-01 00:40:44 +00:00
|
|
|
[object]() { vkDestroyImageView(g_vulkan_context->GetDevice(), object, nullptr); });
|
|
|
|
}
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
std::unique_ptr<CommandBufferManager> g_command_buffer_mgr;
|
2019-01-27 02:41:09 +00:00
|
|
|
} // namespace Vulkan
|