summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar bunnei2019-04-04 22:38:04 -0400
committerGravatar GitHub2019-04-04 22:38:04 -0400
commit66be5150d6d201e3f8ca6e5e09968f052df4beb1 (patch)
treeff6a45a1b6dce02eadd9f432cb1b8bda90f66685
parentMerge pull request #2292 from lioncash/nacp (diff)
parentgpu_thread: Improve synchronization by using CoreTiming. (diff)
downloadyuzu-66be5150d6d201e3f8ca6e5e09968f052df4beb1.tar.gz
yuzu-66be5150d6d201e3f8ca6e5e09968f052df4beb1.tar.xz
yuzu-66be5150d6d201e3f8ca6e5e09968f052df4beb1.zip
Merge pull request #2282 from bunnei/gpu-asynch-v2
gpu_thread: Improve synchronization by using CoreTiming.
-rw-r--r--src/video_core/gpu_asynch.cpp2
-rw-r--r--src/video_core/gpu_thread.cpp43
-rw-r--r--src/video_core/gpu_thread.h71
3 files changed, 65 insertions, 51 deletions
diff --git a/src/video_core/gpu_asynch.cpp b/src/video_core/gpu_asynch.cpp
index 8b355cf7b..db507cf04 100644
--- a/src/video_core/gpu_asynch.cpp
+++ b/src/video_core/gpu_asynch.cpp
@@ -9,7 +9,7 @@
9namespace VideoCommon { 9namespace VideoCommon {
10 10
11GPUAsynch::GPUAsynch(Core::System& system, VideoCore::RendererBase& renderer) 11GPUAsynch::GPUAsynch(Core::System& system, VideoCore::RendererBase& renderer)
12 : Tegra::GPU(system, renderer), gpu_thread{renderer, *dma_pusher} {} 12 : Tegra::GPU(system, renderer), gpu_thread{system, renderer, *dma_pusher} {}
13 13
14GPUAsynch::~GPUAsynch() = default; 14GPUAsynch::~GPUAsynch() = default;
15 15
diff --git a/src/video_core/gpu_thread.cpp b/src/video_core/gpu_thread.cpp
index c5dc199c5..23f9bd422 100644
--- a/src/video_core/gpu_thread.cpp
+++ b/src/video_core/gpu_thread.cpp
@@ -4,6 +4,9 @@
4 4
5#include "common/assert.h" 5#include "common/assert.h"
6#include "common/microprofile.h" 6#include "common/microprofile.h"
7#include "core/core.h"
8#include "core/core_timing.h"
9#include "core/core_timing_util.h"
7#include "core/frontend/scope_acquire_window_context.h" 10#include "core/frontend/scope_acquire_window_context.h"
8#include "video_core/dma_pusher.h" 11#include "video_core/dma_pusher.h"
9#include "video_core/gpu.h" 12#include "video_core/gpu.h"
@@ -36,7 +39,6 @@ static void RunThread(VideoCore::RendererBase& renderer, Tegra::DmaPusher& dma_p
36 dma_pusher.Push(std::move(submit_list->entries)); 39 dma_pusher.Push(std::move(submit_list->entries));
37 dma_pusher.DispatchCalls(); 40 dma_pusher.DispatchCalls();
38 } else if (const auto data = std::get_if<SwapBuffersCommand>(&next.data)) { 41 } else if (const auto data = std::get_if<SwapBuffersCommand>(&next.data)) {
39 state.DecrementFramesCounter();
40 renderer.SwapBuffers(std::move(data->framebuffer)); 42 renderer.SwapBuffers(std::move(data->framebuffer));
41 } else if (const auto data = std::get_if<FlushRegionCommand>(&next.data)) { 43 } else if (const auto data = std::get_if<FlushRegionCommand>(&next.data)) {
42 renderer.Rasterizer().FlushRegion(data->addr, data->size); 44 renderer.Rasterizer().FlushRegion(data->addr, data->size);
@@ -47,13 +49,18 @@ static void RunThread(VideoCore::RendererBase& renderer, Tegra::DmaPusher& dma_p
47 } else { 49 } else {
48 UNREACHABLE(); 50 UNREACHABLE();
49 } 51 }
52 state.signaled_fence = next.fence;
53 state.TrySynchronize();
50 } 54 }
51 } 55 }
52} 56}
53 57
54ThreadManager::ThreadManager(VideoCore::RendererBase& renderer, Tegra::DmaPusher& dma_pusher) 58ThreadManager::ThreadManager(Core::System& system, VideoCore::RendererBase& renderer,
55 : renderer{renderer}, thread{RunThread, std::ref(renderer), std::ref(dma_pusher), 59 Tegra::DmaPusher& dma_pusher)
56 std::ref(state)} {} 60 : system{system}, thread{RunThread, std::ref(renderer), std::ref(dma_pusher), std::ref(state)} {
61 synchronization_event = system.CoreTiming().RegisterEvent(
62 "GPUThreadSynch", [this](u64 fence, int) { state.WaitForSynchronization(fence); });
63}
57 64
58ThreadManager::~ThreadManager() { 65ThreadManager::~ThreadManager() {
59 // Notify GPU thread that a shutdown is pending 66 // Notify GPU thread that a shutdown is pending
@@ -62,14 +69,14 @@ ThreadManager::~ThreadManager() {
62} 69}
63 70
64void ThreadManager::SubmitList(Tegra::CommandList&& entries) { 71void ThreadManager::SubmitList(Tegra::CommandList&& entries) {
65 PushCommand(SubmitListCommand(std::move(entries))); 72 const u64 fence{PushCommand(SubmitListCommand(std::move(entries)))};
73 const s64 synchronization_ticks{Core::Timing::usToCycles(9000)};
74 system.CoreTiming().ScheduleEvent(synchronization_ticks, synchronization_event, fence);
66} 75}
67 76
68void ThreadManager::SwapBuffers( 77void ThreadManager::SwapBuffers(
69 std::optional<std::reference_wrapper<const Tegra::FramebufferConfig>> framebuffer) { 78 std::optional<std::reference_wrapper<const Tegra::FramebufferConfig>> framebuffer) {
70 state.IncrementFramesCounter();
71 PushCommand(SwapBuffersCommand(std::move(framebuffer))); 79 PushCommand(SwapBuffersCommand(std::move(framebuffer)));
72 state.WaitForFrames();
73} 80}
74 81
75void ThreadManager::FlushRegion(CacheAddr addr, u64 size) { 82void ThreadManager::FlushRegion(CacheAddr addr, u64 size) {
@@ -79,7 +86,7 @@ void ThreadManager::FlushRegion(CacheAddr addr, u64 size) {
79void ThreadManager::InvalidateRegion(CacheAddr addr, u64 size) { 86void ThreadManager::InvalidateRegion(CacheAddr addr, u64 size) {
80 if (state.queue.Empty()) { 87 if (state.queue.Empty()) {
81 // It's quicker to invalidate a single region on the CPU if the queue is already empty 88 // It's quicker to invalidate a single region on the CPU if the queue is already empty
82 renderer.Rasterizer().InvalidateRegion(addr, size); 89 system.Renderer().Rasterizer().InvalidateRegion(addr, size);
83 } else { 90 } else {
84 PushCommand(InvalidateRegionCommand(addr, size)); 91 PushCommand(InvalidateRegionCommand(addr, size));
85 } 92 }
@@ -90,9 +97,25 @@ void ThreadManager::FlushAndInvalidateRegion(CacheAddr addr, u64 size) {
90 InvalidateRegion(addr, size); 97 InvalidateRegion(addr, size);
91} 98}
92 99
93void ThreadManager::PushCommand(CommandData&& command_data) { 100u64 ThreadManager::PushCommand(CommandData&& command_data) {
94 state.queue.Push(CommandDataContainer(std::move(command_data))); 101 const u64 fence{++state.last_fence};
102 state.queue.Push(CommandDataContainer(std::move(command_data), fence));
95 state.SignalCommands(); 103 state.SignalCommands();
104 return fence;
105}
106
107MICROPROFILE_DEFINE(GPU_wait, "GPU", "Wait for the GPU", MP_RGB(128, 128, 192));
108void SynchState::WaitForSynchronization(u64 fence) {
109 if (signaled_fence >= fence) {
110 return;
111 }
112
113 // Wait for the GPU to be idle (all commands to be executed)
114 {
115 MICROPROFILE_SCOPE(GPU_wait);
116 std::unique_lock<std::mutex> lock{synchronization_mutex};
117 synchronization_condition.wait(lock, [this, fence] { return signaled_fence >= fence; });
118 }
96} 119}
97 120
98} // namespace VideoCommon::GPUThread 121} // namespace VideoCommon::GPUThread
diff --git a/src/video_core/gpu_thread.h b/src/video_core/gpu_thread.h
index 70acb2e79..62bcea5bb 100644
--- a/src/video_core/gpu_thread.h
+++ b/src/video_core/gpu_thread.h
@@ -19,9 +19,12 @@ struct FramebufferConfig;
19class DmaPusher; 19class DmaPusher;
20} // namespace Tegra 20} // namespace Tegra
21 21
22namespace VideoCore { 22namespace Core {
23class RendererBase; 23class System;
24} // namespace VideoCore 24namespace Timing {
25struct EventType;
26} // namespace Timing
27} // namespace Core
25 28
26namespace VideoCommon::GPUThread { 29namespace VideoCommon::GPUThread {
27 30
@@ -75,63 +78,47 @@ using CommandData =
75struct CommandDataContainer { 78struct CommandDataContainer {
76 CommandDataContainer() = default; 79 CommandDataContainer() = default;
77 80
78 CommandDataContainer(CommandData&& data) : data{std::move(data)} {} 81 CommandDataContainer(CommandData&& data, u64 next_fence)
82 : data{std::move(data)}, fence{next_fence} {}
79 83
80 CommandDataContainer& operator=(const CommandDataContainer& t) { 84 CommandDataContainer& operator=(const CommandDataContainer& t) {
81 data = std::move(t.data); 85 data = std::move(t.data);
86 fence = t.fence;
82 return *this; 87 return *this;
83 } 88 }
84 89
85 CommandData data; 90 CommandData data;
91 u64 fence{};
86}; 92};
87 93
88/// Struct used to synchronize the GPU thread 94/// Struct used to synchronize the GPU thread
89struct SynchState final { 95struct SynchState final {
90 std::atomic_bool is_running{true}; 96 std::atomic_bool is_running{true};
91 std::atomic_int queued_frame_count{}; 97 std::atomic_int queued_frame_count{};
92 std::mutex frames_mutex; 98 std::mutex synchronization_mutex;
93 std::mutex commands_mutex; 99 std::mutex commands_mutex;
94 std::condition_variable commands_condition; 100 std::condition_variable commands_condition;
95 std::condition_variable frames_condition; 101 std::condition_variable synchronization_condition;
96 102
97 void IncrementFramesCounter() { 103 /// Returns true if the gap in GPU commands is small enough that we can consider the CPU and GPU
98 std::lock_guard lock{frames_mutex}; 104 /// synchronized. This is entirely empirical.
99 ++queued_frame_count; 105 bool IsSynchronized() const {
106 constexpr std::size_t max_queue_gap{5};
107 return queue.Size() <= max_queue_gap;
100 } 108 }
101 109
102 void DecrementFramesCounter() { 110 void TrySynchronize() {
103 { 111 if (IsSynchronized()) {
104 std::lock_guard lock{frames_mutex}; 112 std::lock_guard<std::mutex> lock{synchronization_mutex};
105 --queued_frame_count; 113 synchronization_condition.notify_one();
106
107 if (queued_frame_count) {
108 return;
109 }
110 } 114 }
111 frames_condition.notify_one();
112 } 115 }
113 116
114 void WaitForFrames() { 117 void WaitForSynchronization(u64 fence);
115 {
116 std::lock_guard lock{frames_mutex};
117 if (!queued_frame_count) {
118 return;
119 }
120 }
121
122 // Wait for the GPU to be idle (all commands to be executed)
123 {
124 std::unique_lock lock{frames_mutex};
125 frames_condition.wait(lock, [this] { return !queued_frame_count; });
126 }
127 }
128 118
129 void SignalCommands() { 119 void SignalCommands() {
130 { 120 if (queue.Empty()) {
131 std::unique_lock lock{commands_mutex}; 121 return;
132 if (queue.Empty()) {
133 return;
134 }
135 } 122 }
136 123
137 commands_condition.notify_one(); 124 commands_condition.notify_one();
@@ -144,12 +131,15 @@ struct SynchState final {
144 131
145 using CommandQueue = Common::SPSCQueue<CommandDataContainer>; 132 using CommandQueue = Common::SPSCQueue<CommandDataContainer>;
146 CommandQueue queue; 133 CommandQueue queue;
134 u64 last_fence{};
135 std::atomic<u64> signaled_fence{};
147}; 136};
148 137
149/// Class used to manage the GPU thread 138/// Class used to manage the GPU thread
150class ThreadManager final { 139class ThreadManager final {
151public: 140public:
152 explicit ThreadManager(VideoCore::RendererBase& renderer, Tegra::DmaPusher& dma_pusher); 141 explicit ThreadManager(Core::System& system, VideoCore::RendererBase& renderer,
142 Tegra::DmaPusher& dma_pusher);
153 ~ThreadManager(); 143 ~ThreadManager();
154 144
155 /// Push GPU command entries to be processed 145 /// Push GPU command entries to be processed
@@ -170,11 +160,12 @@ public:
170 160
171private: 161private:
172 /// Pushes a command to be executed by the GPU thread 162 /// Pushes a command to be executed by the GPU thread
173 void PushCommand(CommandData&& command_data); 163 u64 PushCommand(CommandData&& command_data);
174 164
175private: 165private:
176 SynchState state; 166 SynchState state;
177 VideoCore::RendererBase& renderer; 167 Core::System& system;
168 Core::Timing::EventType* synchronization_event{};
178 std::thread thread; 169 std::thread thread;
179 std::thread::id thread_id; 170 std::thread::id thread_id;
180}; 171};