2019-02-22 05:27:25 +01:00
|
|
|
// Copyright 2019 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2020-02-21 00:35:53 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
#include <optional>
|
|
|
|
#include <thread>
|
|
|
|
#include <utility>
|
|
|
|
|
2019-12-13 06:24:48 +01:00
|
|
|
#include "common/microprofile.h"
|
2020-04-05 15:48:53 +02:00
|
|
|
#include "common/thread.h"
|
2020-09-10 08:43:30 +02:00
|
|
|
#include "video_core/renderer_vulkan/vk_command_pool.h"
|
2019-02-22 05:27:25 +01:00
|
|
|
#include "video_core/renderer_vulkan/vk_device.h"
|
2020-09-10 08:43:30 +02:00
|
|
|
#include "video_core/renderer_vulkan/vk_master_semaphore.h"
|
2020-02-11 22:59:44 +01:00
|
|
|
#include "video_core/renderer_vulkan/vk_query_cache.h"
|
2019-02-22 05:27:25 +01:00
|
|
|
#include "video_core/renderer_vulkan/vk_scheduler.h"
|
2020-02-21 00:35:53 +01:00
|
|
|
#include "video_core/renderer_vulkan/vk_state_tracker.h"
|
2020-03-27 05:33:21 +01:00
|
|
|
#include "video_core/renderer_vulkan/wrapper.h"
|
2019-02-22 05:27:25 +01:00
|
|
|
|
|
|
|
namespace Vulkan {
|
|
|
|
|
2019-12-13 06:24:48 +01:00
|
|
|
MICROPROFILE_DECLARE(Vulkan_WaitForWorker);
|
|
|
|
|
2020-03-27 05:33:21 +01:00
|
|
|
void VKScheduler::CommandChunk::ExecuteAll(vk::CommandBuffer cmdbuf) {
|
2019-12-13 06:24:48 +01:00
|
|
|
auto command = first;
|
|
|
|
while (command != nullptr) {
|
|
|
|
auto next = command->GetNext();
|
2020-03-27 05:33:21 +01:00
|
|
|
command->Execute(cmdbuf);
|
2019-12-13 06:24:48 +01:00
|
|
|
command->~Command();
|
|
|
|
command = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
command_offset = 0;
|
|
|
|
first = nullptr;
|
|
|
|
last = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-09-10 08:43:30 +02:00
|
|
|
VKScheduler::VKScheduler(const VKDevice& device_, StateTracker& state_tracker_)
|
|
|
|
: device{device_}, state_tracker{state_tracker_},
|
|
|
|
master_semaphore{std::make_unique<MasterSemaphore>(device)},
|
|
|
|
command_pool{std::make_unique<CommandPool>(*master_semaphore, device)} {
|
2019-12-13 06:24:48 +01:00
|
|
|
AcquireNewChunk();
|
2019-02-22 05:27:25 +01:00
|
|
|
AllocateNewContext();
|
2019-12-13 06:24:48 +01:00
|
|
|
worker_thread = std::thread(&VKScheduler::WorkerThread, this);
|
2019-02-22 05:27:25 +01:00
|
|
|
}
|
|
|
|
|
2019-12-13 06:24:48 +01:00
|
|
|
VKScheduler::~VKScheduler() {
|
|
|
|
quit = true;
|
|
|
|
cv.notify_all();
|
|
|
|
worker_thread.join();
|
|
|
|
}
|
2019-02-22 05:27:25 +01:00
|
|
|
|
2020-09-10 08:43:30 +02:00
|
|
|
u64 VKScheduler::CurrentTick() const noexcept {
|
|
|
|
return master_semaphore->CurrentTick();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VKScheduler::IsFree(u64 tick) const noexcept {
|
|
|
|
return master_semaphore->IsFree(tick);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VKScheduler::Wait(u64 tick) {
|
|
|
|
master_semaphore->Wait(tick);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VKScheduler::Flush(VkSemaphore semaphore) {
|
2019-02-22 05:27:25 +01:00
|
|
|
SubmitExecution(semaphore);
|
|
|
|
AllocateNewContext();
|
|
|
|
}
|
|
|
|
|
2020-09-10 08:43:30 +02:00
|
|
|
void VKScheduler::Finish(VkSemaphore semaphore) {
|
|
|
|
const u64 presubmit_tick = CurrentTick();
|
2019-02-22 05:27:25 +01:00
|
|
|
SubmitExecution(semaphore);
|
2020-09-10 08:43:30 +02:00
|
|
|
Wait(presubmit_tick);
|
2019-02-22 05:27:25 +01:00
|
|
|
AllocateNewContext();
|
|
|
|
}
|
|
|
|
|
2019-12-13 06:24:48 +01:00
|
|
|
void VKScheduler::WaitWorker() {
|
|
|
|
MICROPROFILE_SCOPE(Vulkan_WaitForWorker);
|
|
|
|
DispatchWork();
|
|
|
|
|
|
|
|
bool finished = false;
|
|
|
|
do {
|
|
|
|
cv.notify_all();
|
|
|
|
std::unique_lock lock{mutex};
|
|
|
|
finished = chunk_queue.Empty();
|
|
|
|
} while (!finished);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VKScheduler::DispatchWork() {
|
|
|
|
if (chunk->Empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chunk_queue.Push(std::move(chunk));
|
|
|
|
cv.notify_all();
|
|
|
|
AcquireNewChunk();
|
|
|
|
}
|
|
|
|
|
2020-03-27 05:33:21 +01:00
|
|
|
void VKScheduler::RequestRenderpass(VkRenderPass renderpass, VkFramebuffer framebuffer,
|
|
|
|
VkExtent2D render_area) {
|
|
|
|
if (renderpass == state.renderpass && framebuffer == state.framebuffer &&
|
|
|
|
render_area.width == state.render_area.width &&
|
|
|
|
render_area.height == state.render_area.height) {
|
2019-12-13 06:24:48 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-03-27 05:33:21 +01:00
|
|
|
const bool end_renderpass = state.renderpass != nullptr;
|
|
|
|
state.renderpass = renderpass;
|
|
|
|
state.framebuffer = framebuffer;
|
|
|
|
state.render_area = render_area;
|
|
|
|
|
2020-07-17 01:10:49 +02:00
|
|
|
const VkRenderPassBeginInfo renderpass_bi{
|
|
|
|
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
|
|
|
|
.pNext = nullptr,
|
|
|
|
.renderPass = renderpass,
|
|
|
|
.framebuffer = framebuffer,
|
|
|
|
.renderArea =
|
|
|
|
{
|
|
|
|
.offset = {.x = 0, .y = 0},
|
|
|
|
.extent = render_area,
|
|
|
|
},
|
|
|
|
.clearValueCount = 0,
|
|
|
|
.pClearValues = nullptr,
|
|
|
|
};
|
2020-03-27 05:33:21 +01:00
|
|
|
|
|
|
|
Record([renderpass_bi, end_renderpass](vk::CommandBuffer cmdbuf) {
|
2019-12-13 06:24:48 +01:00
|
|
|
if (end_renderpass) {
|
2020-03-27 05:33:21 +01:00
|
|
|
cmdbuf.EndRenderPass();
|
2019-12-13 06:24:48 +01:00
|
|
|
}
|
2020-03-27 05:33:21 +01:00
|
|
|
cmdbuf.BeginRenderPass(renderpass_bi, VK_SUBPASS_CONTENTS_INLINE);
|
2019-12-13 06:24:48 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void VKScheduler::RequestOutsideRenderPassOperationContext() {
|
|
|
|
EndRenderPass();
|
|
|
|
}
|
|
|
|
|
2020-03-27 05:33:21 +01:00
|
|
|
void VKScheduler::BindGraphicsPipeline(VkPipeline pipeline) {
|
2019-12-13 06:24:48 +01:00
|
|
|
if (state.graphics_pipeline == pipeline) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
state.graphics_pipeline = pipeline;
|
2020-03-27 05:33:21 +01:00
|
|
|
Record([pipeline](vk::CommandBuffer cmdbuf) {
|
|
|
|
cmdbuf.BindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
2019-12-13 06:24:48 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void VKScheduler::WorkerThread() {
|
2020-04-05 15:48:53 +02:00
|
|
|
Common::SetCurrentThreadPriority(Common::ThreadPriority::High);
|
2019-12-13 06:24:48 +01:00
|
|
|
std::unique_lock lock{mutex};
|
|
|
|
do {
|
|
|
|
cv.wait(lock, [this] { return !chunk_queue.Empty() || quit; });
|
|
|
|
if (quit) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto extracted_chunk = std::move(chunk_queue.Front());
|
|
|
|
chunk_queue.Pop();
|
2020-03-27 05:33:21 +01:00
|
|
|
extracted_chunk->ExecuteAll(current_cmdbuf);
|
2019-12-13 06:24:48 +01:00
|
|
|
chunk_reserve.Push(std::move(extracted_chunk));
|
|
|
|
} while (!quit);
|
|
|
|
}
|
|
|
|
|
2020-03-27 05:33:21 +01:00
|
|
|
void VKScheduler::SubmitExecution(VkSemaphore semaphore) {
|
2019-12-13 06:24:48 +01:00
|
|
|
EndPendingOperations();
|
|
|
|
InvalidateState();
|
|
|
|
WaitWorker();
|
|
|
|
|
|
|
|
std::unique_lock lock{mutex};
|
|
|
|
|
2020-03-27 05:33:21 +01:00
|
|
|
current_cmdbuf.End();
|
2019-02-22 05:27:25 +01:00
|
|
|
|
2020-09-10 08:43:30 +02:00
|
|
|
const VkSemaphore timeline_semaphore = master_semaphore->Handle();
|
|
|
|
const u32 num_signal_semaphores = semaphore ? 2U : 1U;
|
|
|
|
|
|
|
|
const u64 signal_value = master_semaphore->CurrentTick();
|
|
|
|
const u64 wait_value = signal_value - 1;
|
|
|
|
const VkPipelineStageFlags wait_stage_mask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
|
|
|
|
|
|
|
|
master_semaphore->NextTick();
|
|
|
|
|
|
|
|
const std::array signal_values{signal_value, u64(0)};
|
|
|
|
const std::array signal_semaphores{timeline_semaphore, semaphore};
|
|
|
|
|
|
|
|
const VkTimelineSemaphoreSubmitInfoKHR timeline_si{
|
|
|
|
.sType = VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO_KHR,
|
|
|
|
.pNext = nullptr,
|
|
|
|
.waitSemaphoreValueCount = 1,
|
|
|
|
.pWaitSemaphoreValues = &wait_value,
|
|
|
|
.signalSemaphoreValueCount = num_signal_semaphores,
|
|
|
|
.pSignalSemaphoreValues = signal_values.data(),
|
|
|
|
};
|
2020-07-17 01:10:49 +02:00
|
|
|
const VkSubmitInfo submit_info{
|
|
|
|
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
|
2020-09-10 08:43:30 +02:00
|
|
|
.pNext = &timeline_si,
|
|
|
|
.waitSemaphoreCount = 1,
|
|
|
|
.pWaitSemaphores = &timeline_semaphore,
|
|
|
|
.pWaitDstStageMask = &wait_stage_mask,
|
2020-07-17 01:10:49 +02:00
|
|
|
.commandBufferCount = 1,
|
|
|
|
.pCommandBuffers = current_cmdbuf.address(),
|
2020-09-10 08:43:30 +02:00
|
|
|
.signalSemaphoreCount = num_signal_semaphores,
|
|
|
|
.pSignalSemaphores = signal_semaphores.data(),
|
2020-07-17 01:10:49 +02:00
|
|
|
};
|
2020-09-10 08:43:30 +02:00
|
|
|
switch (const VkResult result = device.GetGraphicsQueue().Submit(submit_info)) {
|
2020-03-30 10:14:35 +02:00
|
|
|
case VK_SUCCESS:
|
|
|
|
break;
|
|
|
|
case VK_ERROR_DEVICE_LOST:
|
|
|
|
device.ReportLoss();
|
|
|
|
[[fallthrough]];
|
|
|
|
default:
|
|
|
|
vk::Check(result);
|
|
|
|
}
|
2019-02-22 05:27:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VKScheduler::AllocateNewContext() {
|
2019-12-13 06:24:48 +01:00
|
|
|
std::unique_lock lock{mutex};
|
2019-02-22 05:27:25 +01:00
|
|
|
|
2020-09-10 08:43:30 +02:00
|
|
|
current_cmdbuf = vk::CommandBuffer(command_pool->Commit(), device.GetDispatchLoader());
|
2020-07-17 01:10:49 +02:00
|
|
|
current_cmdbuf.Begin({
|
|
|
|
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
|
|
|
|
.pNext = nullptr,
|
|
|
|
.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
|
|
|
|
.pInheritanceInfo = nullptr,
|
|
|
|
});
|
2020-03-27 05:33:21 +01:00
|
|
|
|
2020-02-11 22:59:44 +01:00
|
|
|
// Enable counters once again. These are disabled when a command buffer is finished.
|
|
|
|
if (query_cache) {
|
|
|
|
query_cache->UpdateCounters();
|
|
|
|
}
|
2019-12-13 06:24:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VKScheduler::InvalidateState() {
|
|
|
|
state.graphics_pipeline = nullptr;
|
2020-02-21 00:35:53 +01:00
|
|
|
state_tracker.InvalidateCommandBufferState();
|
2019-12-13 06:24:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VKScheduler::EndPendingOperations() {
|
2020-02-11 22:59:44 +01:00
|
|
|
query_cache->DisableStreams();
|
2019-12-13 06:24:48 +01:00
|
|
|
EndRenderPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VKScheduler::EndRenderPass() {
|
|
|
|
if (!state.renderpass) {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-27 05:33:21 +01:00
|
|
|
state.renderpass = nullptr;
|
|
|
|
Record([](vk::CommandBuffer cmdbuf) { cmdbuf.EndRenderPass(); });
|
2019-12-13 06:24:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VKScheduler::AcquireNewChunk() {
|
|
|
|
if (chunk_reserve.Empty()) {
|
|
|
|
chunk = std::make_unique<CommandChunk>();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chunk = std::move(chunk_reserve.Front());
|
|
|
|
chunk_reserve.Pop();
|
2019-02-22 05:27:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Vulkan
|