From 3e08494146d60c0c484d704bc141046e57df4e8b Mon Sep 17 00:00:00 2001 From: PixelyIon Date: Sun, 5 Jun 2022 12:26:58 +0530 Subject: [PATCH] Minor `CommandScheduler` refactor There was a lot of redundant code in the `CommandScheduler` when the same functionality could be achieved with much shorter and cleaner code which this commit fixes. This includes no changes to the user-facing API and does not require any changes on the user side as a result. --- .../cpp/skyline/gpu/command_scheduler.cpp | 30 ++++++++----------- .../main/cpp/skyline/gpu/command_scheduler.h | 6 ---- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/app/src/main/cpp/skyline/gpu/command_scheduler.cpp b/app/src/main/cpp/skyline/gpu/command_scheduler.cpp index 91e7ae96..f41e9187 100644 --- a/app/src/main/cpp/skyline/gpu/command_scheduler.cpp +++ b/app/src/main/cpp/skyline/gpu/command_scheduler.cpp @@ -11,29 +11,23 @@ namespace skyline::gpu { fence(device, vk::FenceCreateInfo{}), cycle(std::make_shared(device, *fence)) {} - bool CommandScheduler::CommandBufferSlot::AllocateIfFree(CommandScheduler::CommandBufferSlot &slot) { - if (!slot.active.test_and_set(std::memory_order_acq_rel)) { - if (slot.cycle->Poll()) { - slot.commandBuffer.reset(); - slot.cycle = std::make_shared(slot.device, *slot.fence); - return true; - } else { - slot.active.clear(std::memory_order_release); - } - } - return false; - } - CommandScheduler::CommandScheduler(GPU &pGpu) : gpu(pGpu), pool(std::ref(pGpu.vkDevice), vk::CommandPoolCreateInfo{ .flags = vk::CommandPoolCreateFlagBits::eTransient | vk::CommandPoolCreateFlagBits::eResetCommandBuffer, .queueFamilyIndex = pGpu.vkQueueFamilyIndex, }) {} CommandScheduler::ActiveCommandBuffer CommandScheduler::AllocateCommandBuffer() { - auto slot{std::find_if(pool->buffers.begin(), pool->buffers.end(), CommandBufferSlot::AllocateIfFree)}; - auto slotId{std::distance(pool->buffers.begin(), slot)}; - if (slot != pool->buffers.end()) - return ActiveCommandBuffer(*slot); + for (auto &slot : pool->buffers) { + if (!slot.active.test_and_set(std::memory_order_acq_rel)) { + if (slot.cycle->Poll()) { + slot.commandBuffer.reset(); + slot.cycle = std::make_shared(slot.device, *slot.fence); + return {slot}; + } else { + slot.active.clear(std::memory_order_release); + } + } + } vk::CommandBuffer commandBuffer; vk::CommandBufferAllocateInfo commandBufferAllocateInfo{ @@ -45,7 +39,7 @@ namespace skyline::gpu { auto result{(*gpu.vkDevice).allocateCommandBuffers(&commandBufferAllocateInfo, &commandBuffer, *gpu.vkDevice.getDispatcher())}; if (result != vk::Result::eSuccess) vk::throwResultException(result, __builtin_FUNCTION()); - return ActiveCommandBuffer(pool->buffers.emplace_back(gpu.vkDevice, commandBuffer, pool->vkCommandPool)); + return {pool->buffers.emplace_back(gpu.vkDevice, commandBuffer, pool->vkCommandPool)}; } void CommandScheduler::SubmitCommandBuffer(const vk::raii::CommandBuffer &commandBuffer, vk::Fence fence) { diff --git a/app/src/main/cpp/skyline/gpu/command_scheduler.h b/app/src/main/cpp/skyline/gpu/command_scheduler.h index 73a30bee..7ac4c6cf 100644 --- a/app/src/main/cpp/skyline/gpu/command_scheduler.h +++ b/app/src/main/cpp/skyline/gpu/command_scheduler.h @@ -23,12 +23,6 @@ namespace skyline::gpu { std::shared_ptr cycle; //!< The latest cycle on the fence, all waits must be performed through this CommandBufferSlot(vk::raii::Device &device, vk::CommandBuffer commandBuffer, vk::raii::CommandPool &pool); - - /** - * @brief Attempts to allocate the buffer if it is free (Not being recorded/executing) - * @return If the allocation was successful or not - */ - static bool AllocateIfFree(CommandBufferSlot &slot); }; GPU &gpu;