gpu_thread: Improve synchronization by using CoreTiming.
This commit is contained in:
parent
62860dc0b0
commit
4555b63750
@ -9,7 +9,7 @@
|
|||||||
namespace VideoCommon {
|
namespace VideoCommon {
|
||||||
|
|
||||||
GPUAsynch::GPUAsynch(Core::System& system, VideoCore::RendererBase& renderer)
|
GPUAsynch::GPUAsynch(Core::System& system, VideoCore::RendererBase& renderer)
|
||||||
: Tegra::GPU(system, renderer), gpu_thread{renderer, *dma_pusher} {}
|
: Tegra::GPU(system, renderer), gpu_thread{system, renderer, *dma_pusher} {}
|
||||||
|
|
||||||
GPUAsynch::~GPUAsynch() = default;
|
GPUAsynch::~GPUAsynch() = default;
|
||||||
|
|
||||||
|
@ -4,6 +4,9 @@
|
|||||||
|
|
||||||
#include "common/assert.h"
|
#include "common/assert.h"
|
||||||
#include "common/microprofile.h"
|
#include "common/microprofile.h"
|
||||||
|
#include "core/core.h"
|
||||||
|
#include "core/core_timing.h"
|
||||||
|
#include "core/core_timing_util.h"
|
||||||
#include "core/frontend/scope_acquire_window_context.h"
|
#include "core/frontend/scope_acquire_window_context.h"
|
||||||
#include "video_core/dma_pusher.h"
|
#include "video_core/dma_pusher.h"
|
||||||
#include "video_core/gpu.h"
|
#include "video_core/gpu.h"
|
||||||
@ -36,7 +39,6 @@ static void RunThread(VideoCore::RendererBase& renderer, Tegra::DmaPusher& dma_p
|
|||||||
dma_pusher.Push(std::move(submit_list->entries));
|
dma_pusher.Push(std::move(submit_list->entries));
|
||||||
dma_pusher.DispatchCalls();
|
dma_pusher.DispatchCalls();
|
||||||
} else if (const auto data = std::get_if<SwapBuffersCommand>(&next.data)) {
|
} else if (const auto data = std::get_if<SwapBuffersCommand>(&next.data)) {
|
||||||
state.DecrementFramesCounter();
|
|
||||||
renderer.SwapBuffers(std::move(data->framebuffer));
|
renderer.SwapBuffers(std::move(data->framebuffer));
|
||||||
} else if (const auto data = std::get_if<FlushRegionCommand>(&next.data)) {
|
} else if (const auto data = std::get_if<FlushRegionCommand>(&next.data)) {
|
||||||
renderer.Rasterizer().FlushRegion(data->addr, data->size);
|
renderer.Rasterizer().FlushRegion(data->addr, data->size);
|
||||||
@ -47,13 +49,18 @@ static void RunThread(VideoCore::RendererBase& renderer, Tegra::DmaPusher& dma_p
|
|||||||
} else {
|
} else {
|
||||||
UNREACHABLE();
|
UNREACHABLE();
|
||||||
}
|
}
|
||||||
|
state.signaled_fence = next.fence;
|
||||||
|
state.TrySynchronize();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ThreadManager::ThreadManager(VideoCore::RendererBase& renderer, Tegra::DmaPusher& dma_pusher)
|
ThreadManager::ThreadManager(Core::System& system, VideoCore::RendererBase& renderer,
|
||||||
: renderer{renderer}, thread{RunThread, std::ref(renderer), std::ref(dma_pusher),
|
Tegra::DmaPusher& dma_pusher)
|
||||||
std::ref(state)} {}
|
: system{system}, thread{RunThread, std::ref(renderer), std::ref(dma_pusher), std::ref(state)} {
|
||||||
|
synchronization_event = system.CoreTiming().RegisterEvent(
|
||||||
|
"GPUThreadSynch", [this](u64 fence, int) { state.WaitForSynchronization(fence); });
|
||||||
|
}
|
||||||
|
|
||||||
ThreadManager::~ThreadManager() {
|
ThreadManager::~ThreadManager() {
|
||||||
// Notify GPU thread that a shutdown is pending
|
// Notify GPU thread that a shutdown is pending
|
||||||
@ -62,14 +69,14 @@ ThreadManager::~ThreadManager() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ThreadManager::SubmitList(Tegra::CommandList&& entries) {
|
void ThreadManager::SubmitList(Tegra::CommandList&& entries) {
|
||||||
PushCommand(SubmitListCommand(std::move(entries)));
|
const u64 fence{PushCommand(SubmitListCommand(std::move(entries)))};
|
||||||
|
const s64 synchronization_ticks{Core::Timing::usToCycles(9000)};
|
||||||
|
system.CoreTiming().ScheduleEvent(synchronization_ticks, synchronization_event, fence);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ThreadManager::SwapBuffers(
|
void ThreadManager::SwapBuffers(
|
||||||
std::optional<std::reference_wrapper<const Tegra::FramebufferConfig>> framebuffer) {
|
std::optional<std::reference_wrapper<const Tegra::FramebufferConfig>> framebuffer) {
|
||||||
state.IncrementFramesCounter();
|
|
||||||
PushCommand(SwapBuffersCommand(std::move(framebuffer)));
|
PushCommand(SwapBuffersCommand(std::move(framebuffer)));
|
||||||
state.WaitForFrames();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ThreadManager::FlushRegion(CacheAddr addr, u64 size) {
|
void ThreadManager::FlushRegion(CacheAddr addr, u64 size) {
|
||||||
@ -79,7 +86,7 @@ void ThreadManager::FlushRegion(CacheAddr addr, u64 size) {
|
|||||||
void ThreadManager::InvalidateRegion(CacheAddr addr, u64 size) {
|
void ThreadManager::InvalidateRegion(CacheAddr addr, u64 size) {
|
||||||
if (state.queue.Empty()) {
|
if (state.queue.Empty()) {
|
||||||
// It's quicker to invalidate a single region on the CPU if the queue is already empty
|
// It's quicker to invalidate a single region on the CPU if the queue is already empty
|
||||||
renderer.Rasterizer().InvalidateRegion(addr, size);
|
system.Renderer().Rasterizer().InvalidateRegion(addr, size);
|
||||||
} else {
|
} else {
|
||||||
PushCommand(InvalidateRegionCommand(addr, size));
|
PushCommand(InvalidateRegionCommand(addr, size));
|
||||||
}
|
}
|
||||||
@ -90,9 +97,25 @@ void ThreadManager::FlushAndInvalidateRegion(CacheAddr addr, u64 size) {
|
|||||||
InvalidateRegion(addr, size);
|
InvalidateRegion(addr, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ThreadManager::PushCommand(CommandData&& command_data) {
|
u64 ThreadManager::PushCommand(CommandData&& command_data) {
|
||||||
state.queue.Push(CommandDataContainer(std::move(command_data)));
|
const u64 fence{++state.last_fence};
|
||||||
|
state.queue.Push(CommandDataContainer(std::move(command_data), fence));
|
||||||
state.SignalCommands();
|
state.SignalCommands();
|
||||||
|
return fence;
|
||||||
|
}
|
||||||
|
|
||||||
|
MICROPROFILE_DEFINE(GPU_wait, "GPU", "Wait for the GPU", MP_RGB(128, 128, 192));
|
||||||
|
void SynchState::WaitForSynchronization(u64 fence) {
|
||||||
|
if (signaled_fence >= fence) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for the GPU to be idle (all commands to be executed)
|
||||||
|
{
|
||||||
|
MICROPROFILE_SCOPE(GPU_wait);
|
||||||
|
std::unique_lock<std::mutex> lock{synchronization_mutex};
|
||||||
|
synchronization_condition.wait(lock, [this, fence] { return signaled_fence >= fence; });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace VideoCommon::GPUThread
|
} // namespace VideoCommon::GPUThread
|
||||||
|
@ -19,9 +19,12 @@ struct FramebufferConfig;
|
|||||||
class DmaPusher;
|
class DmaPusher;
|
||||||
} // namespace Tegra
|
} // namespace Tegra
|
||||||
|
|
||||||
namespace VideoCore {
|
namespace Core {
|
||||||
class RendererBase;
|
class System;
|
||||||
} // namespace VideoCore
|
namespace Timing {
|
||||||
|
struct EventType;
|
||||||
|
} // namespace Timing
|
||||||
|
} // namespace Core
|
||||||
|
|
||||||
namespace VideoCommon::GPUThread {
|
namespace VideoCommon::GPUThread {
|
||||||
|
|
||||||
@ -75,63 +78,47 @@ using CommandData =
|
|||||||
struct CommandDataContainer {
|
struct CommandDataContainer {
|
||||||
CommandDataContainer() = default;
|
CommandDataContainer() = default;
|
||||||
|
|
||||||
CommandDataContainer(CommandData&& data) : data{std::move(data)} {}
|
CommandDataContainer(CommandData&& data, u64 next_fence)
|
||||||
|
: data{std::move(data)}, fence{next_fence} {}
|
||||||
|
|
||||||
CommandDataContainer& operator=(const CommandDataContainer& t) {
|
CommandDataContainer& operator=(const CommandDataContainer& t) {
|
||||||
data = std::move(t.data);
|
data = std::move(t.data);
|
||||||
|
fence = t.fence;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
CommandData data;
|
CommandData data;
|
||||||
|
u64 fence{};
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Struct used to synchronize the GPU thread
|
/// Struct used to synchronize the GPU thread
|
||||||
struct SynchState final {
|
struct SynchState final {
|
||||||
std::atomic_bool is_running{true};
|
std::atomic_bool is_running{true};
|
||||||
std::atomic_int queued_frame_count{};
|
std::atomic_int queued_frame_count{};
|
||||||
std::mutex frames_mutex;
|
std::mutex synchronization_mutex;
|
||||||
std::mutex commands_mutex;
|
std::mutex commands_mutex;
|
||||||
std::condition_variable commands_condition;
|
std::condition_variable commands_condition;
|
||||||
std::condition_variable frames_condition;
|
std::condition_variable synchronization_condition;
|
||||||
|
|
||||||
void IncrementFramesCounter() {
|
/// Returns true if the gap in GPU commands is small enough that we can consider the CPU and GPU
|
||||||
std::lock_guard lock{frames_mutex};
|
/// synchronized. This is entirely empirical.
|
||||||
++queued_frame_count;
|
bool IsSynchronized() const {
|
||||||
|
constexpr std::size_t max_queue_gap{5};
|
||||||
|
return queue.Size() <= max_queue_gap;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DecrementFramesCounter() {
|
void TrySynchronize() {
|
||||||
{
|
if (IsSynchronized()) {
|
||||||
std::lock_guard lock{frames_mutex};
|
std::lock_guard<std::mutex> lock{synchronization_mutex};
|
||||||
--queued_frame_count;
|
synchronization_condition.notify_one();
|
||||||
|
|
||||||
if (queued_frame_count) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
frames_condition.notify_one();
|
|
||||||
}
|
|
||||||
|
|
||||||
void WaitForFrames() {
|
|
||||||
{
|
|
||||||
std::lock_guard lock{frames_mutex};
|
|
||||||
if (!queued_frame_count) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for the GPU to be idle (all commands to be executed)
|
|
||||||
{
|
|
||||||
std::unique_lock lock{frames_mutex};
|
|
||||||
frames_condition.wait(lock, [this] { return !queued_frame_count; });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WaitForSynchronization(u64 fence);
|
||||||
|
|
||||||
void SignalCommands() {
|
void SignalCommands() {
|
||||||
{
|
if (queue.Empty()) {
|
||||||
std::unique_lock lock{commands_mutex};
|
return;
|
||||||
if (queue.Empty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
commands_condition.notify_one();
|
commands_condition.notify_one();
|
||||||
@ -144,12 +131,15 @@ struct SynchState final {
|
|||||||
|
|
||||||
using CommandQueue = Common::SPSCQueue<CommandDataContainer>;
|
using CommandQueue = Common::SPSCQueue<CommandDataContainer>;
|
||||||
CommandQueue queue;
|
CommandQueue queue;
|
||||||
|
u64 last_fence{};
|
||||||
|
std::atomic<u64> signaled_fence{};
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Class used to manage the GPU thread
|
/// Class used to manage the GPU thread
|
||||||
class ThreadManager final {
|
class ThreadManager final {
|
||||||
public:
|
public:
|
||||||
explicit ThreadManager(VideoCore::RendererBase& renderer, Tegra::DmaPusher& dma_pusher);
|
explicit ThreadManager(Core::System& system, VideoCore::RendererBase& renderer,
|
||||||
|
Tegra::DmaPusher& dma_pusher);
|
||||||
~ThreadManager();
|
~ThreadManager();
|
||||||
|
|
||||||
/// Push GPU command entries to be processed
|
/// Push GPU command entries to be processed
|
||||||
@ -170,11 +160,12 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
/// Pushes a command to be executed by the GPU thread
|
/// Pushes a command to be executed by the GPU thread
|
||||||
void PushCommand(CommandData&& command_data);
|
u64 PushCommand(CommandData&& command_data);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SynchState state;
|
SynchState state;
|
||||||
VideoCore::RendererBase& renderer;
|
Core::System& system;
|
||||||
|
Core::Timing::EventType* synchronization_event{};
|
||||||
std::thread thread;
|
std::thread thread;
|
||||||
std::thread::id thread_id;
|
std::thread::id thread_id;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user