| 1 | // Copyright 2016 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/Vulkan/CommandBufferManager.h" |
| 5 | |
| 6 | #include <array> |
| 7 | #include <cstdint> |
| 8 | |
| 9 | #include "Common/Assert.h" |
| 10 | #include "Common/MsgHandler.h" |
| 11 | |
| 12 | #include "VideoBackends/Vulkan/VulkanContext.h" |
| 13 | #include "VideoCommon/Constants.h" |
| 14 | #include "vulkan/vulkan_core.h" |
| 15 | |
| 16 | namespace Vulkan |
| 17 | { |
| 18 | CommandBufferManager::CommandBufferManager(bool use_threaded_submission) |
| 19 | : m_use_threaded_submission(use_threaded_submission) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | CommandBufferManager::~CommandBufferManager() |
| 24 | { |
| 25 | // If the worker thread is enabled, stop and block until it exits. |
| 26 | if (m_use_threaded_submission) |
| 27 | { |
| 28 | WaitForWorkerThreadIdle(); |
| 29 | m_submit_thread.Shutdown(); |
| 30 | } |
| 31 | |
| 32 | DestroyCommandBuffers(); |
| 33 | } |
| 34 | |
| 35 | bool CommandBufferManager::Initialize(size_t swapchain_image_count) |
| 36 | { |
| 37 | if (!CreateCommandBuffers(swapchain_image_count)) |
| 38 | return false; |
| 39 | |
| 40 | if (m_use_threaded_submission && !CreateSubmitThread()) |
| 41 | return false; |
| 42 | |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | bool CommandBufferManager::CreateCommandBuffers(size_t swapchain_image_count) |
| 47 | { |
| 48 | static constexpr VkSemaphoreCreateInfo semaphore_create_info = { |
| 49 | VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, nullptr, 0}; |
| 50 | |
| 51 | VkDevice device = g_vulkan_context->GetDevice(); |
| 52 | VkResult res; |
| 53 | |
| 54 | for (CmdBufferResources& resources : m_command_buffers) |
| 55 | { |
| 56 | resources.init_command_buffer_used = false; |
| 57 | resources.semaphore_used = false; |
| 58 | |
| 59 | VkCommandPoolCreateInfo pool_info = {VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, nullptr, 0, |
| 60 | g_vulkan_context->GetGraphicsQueueFamilyIndex()}; |
| 61 | res = vkCreateCommandPool(g_vulkan_context->GetDevice(), &pool_info, nullptr, |
| 62 | &resources.command_pool); |
| 63 | if (res != VK_SUCCESS) |
| 64 | { |
| 65 | LOG_VULKAN_ERROR(res, "vkCreateCommandPool failed: "); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | VkCommandBufferAllocateInfo buffer_info = { |
| 70 | VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, nullptr, resources.command_pool, |
| 71 | VK_COMMAND_BUFFER_LEVEL_PRIMARY, static_cast<uint32_t>(resources.command_buffers.size())}; |
| 72 | |
| 73 | res = vkAllocateCommandBuffers(device, &buffer_info, resources.command_buffers.data()); |
| 74 | if (res != VK_SUCCESS) |
| 75 | { |
| 76 | LOG_VULKAN_ERROR(res, "vkAllocateCommandBuffers failed: "); |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | VkFenceCreateInfo fence_info = {VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, nullptr, |
| 81 | VK_FENCE_CREATE_SIGNALED_BIT}; |
| 82 | |
| 83 | res = vkCreateFence(device, &fence_info, nullptr, &resources.fence); |
| 84 | if (res != VK_SUCCESS) |
| 85 | { |
| 86 | LOG_VULKAN_ERROR(res, "vkCreateFence failed: "); |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | res = vkCreateSemaphore(device, &semaphore_create_info, nullptr, &resources.semaphore); |
| 91 | if (res != VK_SUCCESS) |
| 92 | { |
| 93 | LOG_VULKAN_ERROR(res, "vkCreateSemaphore failed: "); |
| 94 | return false; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | m_present_semaphores.reserve(swapchain_image_count); |
| 99 | for (uint32_t i = 0; i < swapchain_image_count; i++) |
| 100 | { |
| 101 | VkSemaphore present_semaphore; |
| 102 | res = vkCreateSemaphore(device, &semaphore_create_info, nullptr, &present_semaphore); |
| 103 | if (res != VK_SUCCESS) |
| 104 | { |
| 105 | LOG_VULKAN_ERROR(res, "vkCreateSemaphore failed: "); |
| 106 | return false; |
| 107 | } |
| 108 | m_present_semaphores.push_back(present_semaphore); |
| 109 | } |
| 110 | |
| 111 | // Activate the first command buffer. BeginCommandBuffer moves forward, so start with the last |
| 112 | m_current_cmd_buffer = static_cast<u32>(m_command_buffers.size()) - 1; |
| 113 | BeginCommandBuffer(); |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | void CommandBufferManager::DestroyCommandBuffers() |
| 118 | { |
| 119 | VkDevice device = g_vulkan_context->GetDevice(); |
| 120 | |
| 121 | for (CmdBufferResources& resources : m_command_buffers) |
| 122 | { |
| 123 | // The Vulkan spec section 5.2 says: "When a pool is destroyed, all command buffers allocated |
| 124 | // from the pool are freed.". So we don't need to free the command buffers, just the pools. |
| 125 | // We destroy the command pool first, to avoid any warnings from the validation layers about |
| 126 | // objects which are pending destruction being in-use. |
| 127 | if (resources.command_pool != VK_NULL_HANDLE) |
| 128 | vkDestroyCommandPool(device, resources.command_pool, nullptr); |
| 129 | |
| 130 | // Destroy any pending objects. |
| 131 | for (auto& it : resources.cleanup_resources) |
| 132 | it(); |
| 133 | |
| 134 | if (resources.semaphore != VK_NULL_HANDLE) |
| 135 | vkDestroySemaphore(device, resources.semaphore, nullptr); |
| 136 | |
| 137 | if (resources.fence != VK_NULL_HANDLE) |
| 138 | vkDestroyFence(device, resources.fence, nullptr); |
| 139 | } |
| 140 | |
| 141 | for (FrameResources& resources : m_frame_resources) |
| 142 | { |
| 143 | for (VkDescriptorPool descriptor_pool : resources.descriptor_pools) |
| 144 | { |
| 145 | vkDestroyDescriptorPool(device, descriptor_pool, nullptr); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | for (VkSemaphore present_semaphore : m_present_semaphores) |
| 150 | { |
| 151 | vkDestroySemaphore(device, present_semaphore, nullptr); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | VkDescriptorPool CommandBufferManager::CreateDescriptorPool(u32 max_descriptor_sets) |
| 156 | { |
| 157 | /* |
| 158 | * Worst case descriptor counts according to the descriptor layout created in ObjectCache.cpp: |
| 159 | * UNIFORM_BUFFER_DYNAMIC: 3 |
| 160 | * COMBINED_IMAGE_SAMPLER: NUM_UTILITY_PIXEL_SAMPLERS + NUM_COMPUTE_SHADER_SAMPLERS + |
| 161 | * VideoCommon::MAX_PIXEL_SHADER_SAMPLERS |
| 162 | * STORAGE_BUFFER: 2 |
| 163 | * UNIFORM_TEXEL_BUFFER: 3 |
| 164 | * STORAGE_IMAGE: 1 |
| 165 | */ |
| 166 | const std::array<VkDescriptorPoolSize, 5> pool_sizes{{ |
| 167 | {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, max_descriptor_sets * 3}, |
| 168 | {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
| 169 | max_descriptor_sets * |
| 170 | (VideoCommon::MAX_PIXEL_SHADER_SAMPLERS + VideoCommon::MAX_COMPUTE_SHADER_SAMPLERS + |
| 171 | NUM_UTILITY_PIXEL_SAMPLERS)}, |
| 172 | {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, max_descriptor_sets * 2}, |
| 173 | {VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, max_descriptor_sets * 3}, |
| 174 | {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, max_descriptor_sets * 1}, |
| 175 | }}; |
| 176 | |
| 177 | const VkDescriptorPoolCreateInfo pool_create_info = { |
| 178 | VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, nullptr, 0, max_descriptor_sets, |
| 179 | static_cast<u32>(pool_sizes.size()), pool_sizes.data(), |
| 180 | }; |
| 181 | |
| 182 | VkDevice device = g_vulkan_context->GetDevice(); |
| 183 | VkDescriptorPool descriptor_pool = VK_NULL_HANDLE; |
| 184 | VkResult res = vkCreateDescriptorPool(device, &pool_create_info, nullptr, &descriptor_pool); |
| 185 | if (res != VK_SUCCESS) |
| 186 | { |
| 187 | LOG_VULKAN_ERROR(res, "vkCreateDescriptorPool failed: "); |
| 188 | return VK_NULL_HANDLE; |
| 189 | } |
| 190 | return descriptor_pool; |
| 191 | } |
| 192 | |
| 193 | VkDescriptorSet CommandBufferManager::AllocateDescriptorSet(VkDescriptorSetLayout set_layout) |
| 194 | { |
| 195 | VkDescriptorSet descriptor_set = VK_NULL_HANDLE; |
| 196 | FrameResources& resources = GetCurrentFrameResources(); |
| 197 | |
| 198 | if (!resources.descriptor_pools.empty()) [[likely]] |
| 199 | { |
| 200 | VkDescriptorSetAllocateInfo allocate_info = { |
| 201 | VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, nullptr, |
| 202 | resources.descriptor_pools[resources.current_descriptor_pool_index], 1, &set_layout}; |
| 203 | |
| 204 | VkResult res = |
| 205 | vkAllocateDescriptorSets(g_vulkan_context->GetDevice(), &allocate_info, &descriptor_set); |
| 206 | if (res != VK_SUCCESS && |
| 207 | resources.descriptor_pools.size() > resources.current_descriptor_pool_index + 1) |
| 208 | { |
| 209 | // Mark the next descriptor set as active and try again. |
| 210 | resources.current_descriptor_pool_index++; |
| 211 | descriptor_set = AllocateDescriptorSet(set_layout); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | if (descriptor_set == VK_NULL_HANDLE) [[unlikely]] |
| 216 | { |
| 217 | VkDescriptorPool descriptor_pool = CreateDescriptorPool(DESCRIPTOR_SETS_PER_POOL); |
| 218 | m_descriptor_set_count += DESCRIPTOR_SETS_PER_POOL; |
| 219 | if (descriptor_pool == VK_NULL_HANDLE) [[unlikely]] |
| 220 | return VK_NULL_HANDLE; |
| 221 | |
| 222 | resources.descriptor_pools.push_back(descriptor_pool); |
| 223 | resources.current_descriptor_pool_index = |
| 224 | static_cast<u32>(resources.descriptor_pools.size()) - 1; |
| 225 | descriptor_set = AllocateDescriptorSet(set_layout); |
| 226 | } |
| 227 | |
| 228 | return descriptor_set; |
| 229 | } |
| 230 | |
| 231 | bool CommandBufferManager::CreateSubmitThread() |
| 232 | { |
| 233 | m_submit_thread.Reset("VK submission thread", [this](PendingCommandBufferSubmit submit) { |
| 234 | SubmitCommandBuffer(submit.command_buffer_index, submit.present_swap_chain, |
| 235 | submit.present_image_index); |
| 236 | CmdBufferResources& resources = m_command_buffers[submit.command_buffer_index]; |
| 237 | resources.waiting_for_submit.store(false, std::memory_order_release); |
| 238 | }); |
| 239 | |
| 240 | return true; |
| 241 | } |
| 242 | |
| 243 | void CommandBufferManager::WaitForWorkerThreadIdle() |
| 244 | { |
| 245 | if (!m_use_threaded_submission) |
| 246 | return; |
| 247 | |
| 248 | m_submit_thread.WaitForCompletion(); |
| 249 | } |
| 250 | |
| 251 | void CommandBufferManager::WaitForFenceCounter(u64 fence_counter) |
| 252 | { |
| 253 | if (m_completed_fence_counter >= fence_counter) |
| 254 | return; |
| 255 | |
| 256 | // Find the first command buffer which covers this counter value. |
| 257 | u32 index = (m_current_cmd_buffer + 1) % NUM_COMMAND_BUFFERS; |
| 258 | while (index != m_current_cmd_buffer) |
| 259 | { |
| 260 | if (m_command_buffers[index].fence_counter >= fence_counter) |
| 261 | break; |
| 262 | |
| 263 | index = (index + 1) % NUM_COMMAND_BUFFERS; |
| 264 | } |
| 265 | |
| 266 | ASSERT(index != m_current_cmd_buffer); |
| 267 | WaitForCommandBufferCompletion(index); |
| 268 | } |
| 269 | |
| 270 | void CommandBufferManager::WaitForCommandBufferCompletion(u32 index) |
| 271 | { |
| 272 | CmdBufferResources& resources = m_command_buffers[index]; |
| 273 | |
| 274 | // Ensure this command buffer has been submitted. |
| 275 | if (resources.waiting_for_submit.load(std::memory_order_acquire)) |
| 276 | { |
| 277 | WaitForWorkerThreadIdle(); |
| 278 | ASSERT_MSG(VIDEO, !resources.waiting_for_submit.load(std::memory_order_relaxed), |
| 279 | "Submit thread is idle but command buffer is still waiting for submission!"); |
| 280 | } |
| 281 | |
| 282 | // Wait for this command buffer to be completed. |
| 283 | VkResult res = |
| 284 | vkWaitForFences(g_vulkan_context->GetDevice(), 1, &resources.fence, VK_TRUE, UINT64_MAX); |
| 285 | if (res != VK_SUCCESS) |
| 286 | LOG_VULKAN_ERROR(res, "vkWaitForFences failed: "); |
| 287 | |
| 288 | // Clean up any resources for command buffers between the last known completed buffer and this |
| 289 | // now-completed command buffer. If we use >2 buffers, this may be more than one buffer. |
| 290 | const u64 now_completed_counter = resources.fence_counter; |
| 291 | u32 cleanup_index = (m_current_cmd_buffer + 1) % NUM_COMMAND_BUFFERS; |
| 292 | while (cleanup_index != m_current_cmd_buffer) |
| 293 | { |
| 294 | CmdBufferResources& cleanup_resources = m_command_buffers[cleanup_index]; |
| 295 | if (cleanup_resources.fence_counter > now_completed_counter) |
| 296 | break; |
| 297 | |
| 298 | if (cleanup_resources.fence_counter > m_completed_fence_counter) |
| 299 | { |
| 300 | for (auto& it : cleanup_resources.cleanup_resources) |
| 301 | it(); |
| 302 | cleanup_resources.cleanup_resources.clear(); |
| 303 | } |
| 304 | |
| 305 | cleanup_index = (cleanup_index + 1) % NUM_COMMAND_BUFFERS; |
| 306 | } |
| 307 | |
| 308 | m_completed_fence_counter = now_completed_counter; |
| 309 | } |
| 310 | |
| 311 | void CommandBufferManager::SubmitCommandBuffer(bool submit_on_worker_thread, |
| 312 | bool wait_for_completion, bool advance_to_next_frame, |
| 313 | VkSwapchainKHR present_swap_chain, |
| 314 | uint32_t present_image_index) |
| 315 | { |
| 316 | // End the current command buffer. |
| 317 | CmdBufferResources& resources = GetCurrentCmdBufferResources(); |
| 318 | for (VkCommandBuffer command_buffer : resources.command_buffers) |
| 319 | { |
| 320 | VkResult res = vkEndCommandBuffer(command_buffer); |
| 321 | if (res != VK_SUCCESS) |
| 322 | { |
| 323 | LOG_VULKAN_ERROR(res, "vkEndCommandBuffer failed: "); |
| 324 | PanicAlertFmt("Failed to end command buffer: {} ({})", VkResultToString(res), |
| 325 | static_cast<int>(res)); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // Submitting off-thread? |
| 330 | if (m_use_threaded_submission && submit_on_worker_thread && !wait_for_completion) |
| 331 | { |
| 332 | resources.waiting_for_submit.store(true, std::memory_order_relaxed); |
| 333 | // Push to the pending submit queue. |
| 334 | m_submit_thread.Push({present_swap_chain, present_image_index, m_current_cmd_buffer}); |
| 335 | } |
| 336 | else |
| 337 | { |
| 338 | WaitForWorkerThreadIdle(); |
| 339 | |
| 340 | // Pass through to normal submission path. |
| 341 | SubmitCommandBuffer(m_current_cmd_buffer, present_swap_chain, present_image_index); |
| 342 | if (wait_for_completion) |
| 343 | WaitForCommandBufferCompletion(m_current_cmd_buffer); |
| 344 | } |
| 345 | |
| 346 | if (advance_to_next_frame) |
| 347 | { |
| 348 | m_current_frame = (m_current_frame + 1) % NUM_FRAMES_IN_FLIGHT; |
| 349 | |
| 350 | // Wait for all command buffers that used the descriptor pool to finish |
| 351 | u32 cmd_buffer_index = (m_current_cmd_buffer + 1) % NUM_COMMAND_BUFFERS; |
| 352 | while (cmd_buffer_index != m_current_cmd_buffer) |
| 353 | { |
| 354 | CmdBufferResources& cmd_buffer = m_command_buffers[cmd_buffer_index]; |
| 355 | if (cmd_buffer.frame_index == m_current_frame && cmd_buffer.fence_counter != 0 && |
| 356 | cmd_buffer.fence_counter > m_completed_fence_counter) |
| 357 | { |
| 358 | WaitForCommandBufferCompletion(cmd_buffer_index); |
| 359 | } |
| 360 | cmd_buffer_index = (cmd_buffer_index + 1) % NUM_COMMAND_BUFFERS; |
| 361 | } |
| 362 | |
| 363 | // Reset the descriptor pools |
| 364 | FrameResources& frame_resources = GetCurrentFrameResources(); |
| 365 | |
| 366 | if (frame_resources.descriptor_pools.size() == 1) [[likely]] |
| 367 | { |
| 368 | VkResult res = vkResetDescriptorPool(g_vulkan_context->GetDevice(), |
| 369 | frame_resources.descriptor_pools[0], 0); |
| 370 | if (res != VK_SUCCESS) |
| 371 | LOG_VULKAN_ERROR(res, "vkResetDescriptorPool failed: "); |
| 372 | } |
| 373 | else [[unlikely]] |
| 374 | { |
| 375 | for (VkDescriptorPool descriptor_pool : frame_resources.descriptor_pools) |
| 376 | { |
| 377 | vkDestroyDescriptorPool(g_vulkan_context->GetDevice(), descriptor_pool, nullptr); |
| 378 | } |
| 379 | frame_resources.descriptor_pools.clear(); |
| 380 | VkDescriptorPool descriptor_pool = CreateDescriptorPool(m_descriptor_set_count); |
| 381 | if (descriptor_pool != VK_NULL_HANDLE) [[likely]] |
| 382 | frame_resources.descriptor_pools.push_back(descriptor_pool); |
| 383 | } |
| 384 | |
| 385 | frame_resources.current_descriptor_pool_index = 0; |
| 386 | } |
| 387 | |
| 388 | // Switch to next cmdbuffer. |
| 389 | BeginCommandBuffer(); |
| 390 | } |
| 391 | |
| 392 | void CommandBufferManager::SubmitCommandBuffer(u32 command_buffer_index, |
| 393 | VkSwapchainKHR present_swap_chain, |
| 394 | u32 present_image_index) |
| 395 | { |
| 396 | CmdBufferResources& resources = m_command_buffers[command_buffer_index]; |
| 397 | |
| 398 | // This may be executed on the worker thread, so don't modify any state of the manager class. |
| 399 | uint32_t wait_bits = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 400 | VkSubmitInfo submit_info = {VK_STRUCTURE_TYPE_SUBMIT_INFO, |
| 401 | nullptr, |
| 402 | 0, |
| 403 | nullptr, |
| 404 | &wait_bits, |
| 405 | static_cast<u32>(resources.command_buffers.size()), |
| 406 | resources.command_buffers.data(), |
| 407 | 0, |
| 408 | nullptr}; |
| 409 | |
| 410 | // If the init command buffer did not have any commands recorded, don't submit it. |
| 411 | if (!resources.init_command_buffer_used) |
| 412 | { |
| 413 | submit_info.commandBufferCount = 1; |
| 414 | submit_info.pCommandBuffers = &resources.command_buffers[1]; |
| 415 | } |
| 416 | |
| 417 | if (resources.semaphore_used) |
| 418 | { |
| 419 | submit_info.pWaitSemaphores = &resources.semaphore; |
| 420 | submit_info.waitSemaphoreCount = 1; |
| 421 | } |
| 422 | |
| 423 | if (present_swap_chain != VK_NULL_HANDLE) |
| 424 | { |
| 425 | submit_info.signalSemaphoreCount = 1; |
| 426 | submit_info.pSignalSemaphores = &m_present_semaphores[present_image_index]; |
| 427 | } |
| 428 | |
| 429 | VkResult res = |
| 430 | vkQueueSubmit(g_vulkan_context->GetGraphicsQueue(), 1, &submit_info, resources.fence); |
| 431 | if (res != VK_SUCCESS) |
| 432 | { |
| 433 | LOG_VULKAN_ERROR(res, "vkQueueSubmit failed: "); |
| 434 | PanicAlertFmt("Failed to submit command buffer: {} ({})", VkResultToString(res), |
| 435 | static_cast<int>(res)); |
| 436 | } |
| 437 | |
| 438 | // Do we have a swap chain to present? |
| 439 | if (present_swap_chain != VK_NULL_HANDLE) |
| 440 | { |
| 441 | // Should have a signal semaphore. |
| 442 | VkPresentInfoKHR present_info = {VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, |
| 443 | nullptr, |
| 444 | 1, |
| 445 | &m_present_semaphores[present_image_index], |
| 446 | 1, |
| 447 | &present_swap_chain, |
| 448 | &present_image_index, |
| 449 | nullptr}; |
| 450 | |
| 451 | m_last_present_result = vkQueuePresentKHR(g_vulkan_context->GetPresentQueue(), &present_info); |
| 452 | if (m_last_present_result != VK_SUCCESS) |
| 453 | { |
| 454 | // VK_ERROR_OUT_OF_DATE_KHR is not fatal, just means we need to recreate our swap chain. |
| 455 | if (m_last_present_result != VK_ERROR_OUT_OF_DATE_KHR && |
| 456 | m_last_present_result != VK_SUBOPTIMAL_KHR && |
| 457 | m_last_present_result != VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT) |
| 458 | { |
| 459 | LOG_VULKAN_ERROR(m_last_present_result, "vkQueuePresentKHR failed: "); |
| 460 | } |
| 461 | |
| 462 | // Don't treat VK_SUBOPTIMAL_KHR as fatal on Android. Android 10+ requires prerotation. |
| 463 | // See https://twitter.com/Themaister/status/1207062674011574273 |
| 464 | #ifdef VK_USE_PLATFORM_ANDROID_KHR |
| 465 | if (m_last_present_result != VK_SUBOPTIMAL_KHR) |
| 466 | m_last_present_failed.Set(); |
| 467 | #else |
| 468 | m_last_present_failed.Set(); |
| 469 | #endif |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | void CommandBufferManager::BeginCommandBuffer() |
| 475 | { |
| 476 | // Move to the next command buffer. |
| 477 | const u32 next_buffer_index = (m_current_cmd_buffer + 1) % NUM_COMMAND_BUFFERS; |
| 478 | CmdBufferResources& resources = m_command_buffers[next_buffer_index]; |
| 479 | |
| 480 | // Wait for the GPU to finish with all resources for this command buffer. |
| 481 | if (resources.fence_counter > m_completed_fence_counter) |
| 482 | WaitForCommandBufferCompletion(next_buffer_index); |
| 483 | |
| 484 | // Reset fence to unsignaled before starting. |
| 485 | VkResult res = vkResetFences(g_vulkan_context->GetDevice(), 1, &resources.fence); |
| 486 | if (res != VK_SUCCESS) |
| 487 | LOG_VULKAN_ERROR(res, "vkResetFences failed: "); |
| 488 | |
| 489 | // Reset command pools to beginning since we can re-use the memory now |
| 490 | res = vkResetCommandPool(g_vulkan_context->GetDevice(), resources.command_pool, 0); |
| 491 | if (res != VK_SUCCESS) |
| 492 | LOG_VULKAN_ERROR(res, "vkResetCommandPool failed: "); |
| 493 | |
| 494 | // Enable commands to be recorded to the two buffers again. |
| 495 | VkCommandBufferBeginInfo begin_info = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, nullptr, |
| 496 | VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, nullptr}; |
| 497 | for (VkCommandBuffer command_buffer : resources.command_buffers) |
| 498 | { |
| 499 | res = vkBeginCommandBuffer(command_buffer, &begin_info); |
| 500 | if (res != VK_SUCCESS) |
| 501 | LOG_VULKAN_ERROR(res, "vkBeginCommandBuffer failed: "); |
| 502 | } |
| 503 | |
| 504 | // Reset upload command buffer state |
| 505 | resources.init_command_buffer_used = false; |
| 506 | resources.semaphore_used = false; |
| 507 | resources.fence_counter = m_next_fence_counter++; |
| 508 | resources.frame_index = m_current_frame; |
| 509 | m_current_cmd_buffer = next_buffer_index; |
| 510 | } |
| 511 | |
| 512 | void CommandBufferManager::DeferBufferViewDestruction(VkBufferView object) |
| 513 | { |
| 514 | CmdBufferResources& cmd_buffer_resources = GetCurrentCmdBufferResources(); |
| 515 | cmd_buffer_resources.cleanup_resources.push_back( |
| 516 | [object] { vkDestroyBufferView(g_vulkan_context->GetDevice(), object, nullptr); }); |
| 517 | } |
| 518 | |
| 519 | void CommandBufferManager::DeferBufferDestruction(VkBuffer buffer, VmaAllocation alloc) |
| 520 | { |
| 521 | CmdBufferResources& cmd_buffer_resources = GetCurrentCmdBufferResources(); |
| 522 | cmd_buffer_resources.cleanup_resources.push_back( |
| 523 | [buffer, alloc] { vmaDestroyBuffer(g_vulkan_context->GetMemoryAllocator(), buffer, alloc); }); |
| 524 | } |
| 525 | |
| 526 | void CommandBufferManager::DeferFramebufferDestruction(VkFramebuffer object) |
| 527 | { |
| 528 | CmdBufferResources& cmd_buffer_resources = GetCurrentCmdBufferResources(); |
| 529 | cmd_buffer_resources.cleanup_resources.push_back( |
| 530 | [object] { vkDestroyFramebuffer(g_vulkan_context->GetDevice(), object, nullptr); }); |
| 531 | } |
| 532 | |
| 533 | void CommandBufferManager::DeferImageDestruction(VkImage image, VmaAllocation alloc) |
| 534 | { |
| 535 | CmdBufferResources& cmd_buffer_resources = GetCurrentCmdBufferResources(); |
| 536 | cmd_buffer_resources.cleanup_resources.push_back( |
| 537 | [image, alloc] { vmaDestroyImage(g_vulkan_context->GetMemoryAllocator(), image, alloc); }); |
| 538 | } |
| 539 | |
| 540 | void CommandBufferManager::DeferImageViewDestruction(VkImageView object) |
| 541 | { |
| 542 | CmdBufferResources& cmd_buffer_resources = GetCurrentCmdBufferResources(); |
| 543 | cmd_buffer_resources.cleanup_resources.push_back( |
| 544 | [object] { vkDestroyImageView(g_vulkan_context->GetDevice(), object, nullptr); }); |
| 545 | } |
| 546 | |
| 547 | std::unique_ptr<CommandBufferManager> g_command_buffer_mgr; |
| 548 | } // namespace Vulkan |