| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <array> |
| 7 |
#include <functional> |
| 8 |
#include <memory> |
| 9 |
#include <vector> |
| 10 |
|
| 11 |
#include <Common/WorkQueueThread.h> |
| 12 |
#include "Common/Flag.h" |
| 13 |
|
| 14 |
#include "VideoBackends/Vulkan/Constants.h" |
| 15 |
|
| 16 |
namespace Vulkan |
| 17 |
{ |
| 18 |
class CommandBufferManager |
| 19 |
{ |
| 20 |
public: |
| 21 |
explicit CommandBufferManager(bool use_threaded_submission); |
| 22 |
~CommandBufferManager(); |
| 23 |
|
| 24 |
bool Initialize(size_t swapchain_image_count); |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
VkCommandBuffer GetCurrentInitCommandBuffer() |
| 29 |
{ |
| 30 |
CmdBufferResources& cmd_buffer_resources = GetCurrentCmdBufferResources(); |
| 31 |
cmd_buffer_resources.init_command_buffer_used = true; |
| 32 |
return cmd_buffer_resources.command_buffers[0]; |
| 33 |
} |
| 34 |
VkCommandBuffer GetCurrentCommandBuffer() const |
| 35 |
{ |
| 36 |
const CmdBufferResources& cmd_buffer_resources = m_command_buffers[m_current_cmd_buffer]; |
| 37 |
return cmd_buffer_resources.command_buffers[1]; |
| 38 |
} |
| 39 |
|
| 40 |
VkDescriptorSet AllocateDescriptorSet(VkDescriptorSetLayout set_layout); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
u64 GetCompletedFenceCounter() const { return m_completed_fence_counter; } |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
u64 GetCurrentFenceCounter() const |
| 51 |
{ |
| 52 |
auto& resources = m_command_buffers[m_current_cmd_buffer]; |
| 53 |
return resources.fence_counter; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
VkSemaphore GetCurrentCommandBufferSemaphore() |
| 61 |
{ |
| 62 |
return m_command_buffers[m_current_cmd_buffer].semaphore; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
void MarkCurrentCommandBufferSemaphoreUsed() |
| 68 |
{ |
| 69 |
m_command_buffers[m_current_cmd_buffer].semaphore_used = true; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
void WaitForWorkerThreadIdle(); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
void WaitForFenceCounter(u64 fence_counter); |
| 78 |
|
| 79 |
void SubmitCommandBuffer(bool submit_on_worker_thread, bool wait_for_completion, |
| 80 |
bool advance_to_next_frame = false, |
| 81 |
VkSwapchainKHR present_swap_chain = VK_NULL_HANDLE, |
| 82 |
uint32_t present_image_index = 0xFFFFFFFF); |
| 83 |
|
| 84 |
|
| 85 |
bool CheckLastPresentFail() { return m_last_present_failed.TestAndClear(); } |
| 86 |
VkResult GetLastPresentResult() const { return m_last_present_result; } |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
void DeferBufferViewDestruction(VkBufferView object); |
| 91 |
void DeferBufferDestruction(VkBuffer buffer, VmaAllocation alloc); |
| 92 |
void DeferFramebufferDestruction(VkFramebuffer object); |
| 93 |
void DeferImageDestruction(VkImage object, VmaAllocation alloc); |
| 94 |
void DeferImageViewDestruction(VkImageView object); |
| 95 |
|
| 96 |
private: |
| 97 |
bool CreateCommandBuffers(size_t swapchain_image_count); |
| 98 |
void DestroyCommandBuffers(); |
| 99 |
|
| 100 |
bool CreateSubmitThread(); |
| 101 |
|
| 102 |
void WaitForCommandBufferCompletion(u32 command_buffer_index); |
| 103 |
void SubmitCommandBuffer(u32 command_buffer_index, VkSwapchainKHR present_swap_chain, |
| 104 |
u32 present_image_index); |
| 105 |
void BeginCommandBuffer(); |
| 106 |
|
| 107 |
VkDescriptorPool CreateDescriptorPool(u32 descriptor_sizes); |
| 108 |
|
| 109 |
const u32 DESCRIPTOR_SETS_PER_POOL = 1024; |
| 110 |
|
| 111 |
struct CmdBufferResources |
| 112 |
{ |
| 113 |
|
| 114 |
VkCommandPool command_pool = VK_NULL_HANDLE; |
| 115 |
std::array<VkCommandBuffer, 2> command_buffers = {}; |
| 116 |
VkFence fence = VK_NULL_HANDLE; |
| 117 |
VkSemaphore semaphore = VK_NULL_HANDLE; |
| 118 |
u64 fence_counter = 0; |
| 119 |
bool init_command_buffer_used = false; |
| 120 |
bool semaphore_used = false; |
| 121 |
std::atomic<bool> waiting_for_submit{false}; |
| 122 |
u32 frame_index = 0; |
| 123 |
|
| 124 |
std::vector<std::function<void()>> cleanup_resources; |
| 125 |
}; |
| 126 |
|
| 127 |
struct FrameResources |
| 128 |
{ |
| 129 |
std::vector<VkDescriptorPool> descriptor_pools; |
| 130 |
u32 current_descriptor_pool_index = 0; |
| 131 |
}; |
| 132 |
|
| 133 |
FrameResources& GetCurrentFrameResources() { return m_frame_resources[m_current_frame]; } |
| 134 |
|
| 135 |
CmdBufferResources& GetCurrentCmdBufferResources() |
| 136 |
{ |
| 137 |
return m_command_buffers[m_current_cmd_buffer]; |
| 138 |
} |
| 139 |
|
| 140 |
u64 m_next_fence_counter = 1; |
| 141 |
u64 m_completed_fence_counter = 0; |
| 142 |
|
| 143 |
std::array<FrameResources, NUM_FRAMES_IN_FLIGHT> m_frame_resources; |
| 144 |
std::array<CmdBufferResources, NUM_COMMAND_BUFFERS> m_command_buffers; |
| 145 |
u32 m_current_frame = 0; |
| 146 |
u32 m_current_cmd_buffer = 0; |
| 147 |
|
| 148 |
|
| 149 |
struct PendingCommandBufferSubmit |
| 150 |
{ |
| 151 |
VkSwapchainKHR present_swap_chain; |
| 152 |
u32 present_image_index; |
| 153 |
u32 command_buffer_index; |
| 154 |
}; |
| 155 |
Common::WorkQueueThreadSP<PendingCommandBufferSubmit> m_submit_thread; |
| 156 |
std::vector<VkSemaphore> m_present_semaphores = {}; |
| 157 |
Common::Flag m_last_present_failed; |
| 158 |
VkResult m_last_present_result = VK_SUCCESS; |
| 159 |
bool m_use_threaded_submission = false; |
| 160 |
u32 m_descriptor_set_count = DESCRIPTOR_SETS_PER_POOL; |
| 161 |
}; |
| 162 |
|
| 163 |
extern std::unique_ptr<CommandBufferManager> g_command_buffer_mgr; |
| 164 |
|
| 165 |
} |