Debian Codegraph @

Source/Core/VideoBackends/Vulkan/CommandBufferManager.h

raw symbols SCIP
1 // Copyright 2016 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
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 // These command buffers are allocated per-frame. They are valid until the command buffer
27 // is submitted, after that you should call these functions again.
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 // Allocates a descriptors set from the pool reserved for the current frame.
40 VkDescriptorSet AllocateDescriptorSet(VkDescriptorSetLayout set_layout);
41
42 // Fence "counters" are used to track which commands have been completed by the GPU.
43 // If the last completed fence counter is greater or equal to N, it means that the work
44 // associated counter N has been completed by the GPU. The value of N to associate with
45 // commands can be retrieved by calling GetCurrentFenceCounter().
46 u64 GetCompletedFenceCounter() const { return m_completed_fence_counter; }
47
48 // Gets the fence that will be signaled when the currently executing command buffer is
49 // queued and executed. Do not wait for this fence before the buffer is executed.
50 u64 GetCurrentFenceCounter() const
51 {
52 auto& resources = m_command_buffers[m_current_cmd_buffer];
53 return resources.fence_counter;
54 }
55
56 // Returns the semaphore for the current command buffer, which can be used to ensure the
57 // swap chain image is ready before the command buffer executes.
58 // Once you've confirmed that the semaphore will be signalled this frame, call
59 // `MarkCurrentCommandBufferSemaphoreUsed`.
60 VkSemaphore GetCurrentCommandBufferSemaphore()
61 {
62 return m_command_buffers[m_current_cmd_buffer].semaphore;
63 }
64
65 // Marks the current command buffer's semaphore as used, so we'll wait on it in the next
66 // command buffer submission.
67 void MarkCurrentCommandBufferSemaphoreUsed()
68 {
69 m_command_buffers[m_current_cmd_buffer].semaphore_used = true;
70 }
71
72 // Ensure that the worker thread has submitted any previous command buffers and is idle.
73 void WaitForWorkerThreadIdle();
74
75 // Wait for a fence to be completed.
76 // Also invokes callbacks for completion.
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 // Was the last present submitted to the queue a failure? If so, we must recreate our swapchain.
85 bool CheckLastPresentFail() { return m_last_present_failed.TestAndClear(); }
86 VkResult GetLastPresentResult() const { return m_last_present_result; }
87
88 // Schedule a vulkan resource for destruction later on. This will occur when the command buffer
89 // is next re-used, and the GPU has finished working with the specified resource.
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 // [0] - Init (upload) command buffer, [1] - draw command buffer
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 // Threaded command buffer execution
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 } // namespace Vulkan