diff options
Diffstat (limited to 'src/video_core/fence_manager.h')
| -rw-r--r-- | src/video_core/fence_manager.h | 143 |
1 files changed, 111 insertions, 32 deletions
diff --git a/src/video_core/fence_manager.h b/src/video_core/fence_manager.h index c390ac91b..3b2f6aab6 100644 --- a/src/video_core/fence_manager.h +++ b/src/video_core/fence_manager.h | |||
| @@ -4,13 +4,20 @@ | |||
| 4 | #pragma once | 4 | #pragma once |
| 5 | 5 | ||
| 6 | #include <algorithm> | 6 | #include <algorithm> |
| 7 | #include <condition_variable> | ||
| 7 | #include <cstring> | 8 | #include <cstring> |
| 8 | #include <deque> | 9 | #include <deque> |
| 9 | #include <functional> | 10 | #include <functional> |
| 10 | #include <memory> | 11 | #include <memory> |
| 12 | #include <mutex> | ||
| 13 | #include <thread> | ||
| 11 | #include <queue> | 14 | #include <queue> |
| 12 | 15 | ||
| 13 | #include "common/common_types.h" | 16 | #include "common/common_types.h" |
| 17 | #include "common/microprofile.h" | ||
| 18 | #include "common/scope_exit.h" | ||
| 19 | #include "common/settings.h" | ||
| 20 | #include "common/thread.h" | ||
| 14 | #include "video_core/delayed_destruction_ring.h" | 21 | #include "video_core/delayed_destruction_ring.h" |
| 15 | #include "video_core/gpu.h" | 22 | #include "video_core/gpu.h" |
| 16 | #include "video_core/host1x/host1x.h" | 23 | #include "video_core/host1x/host1x.h" |
| @@ -23,15 +30,26 @@ class FenceBase { | |||
| 23 | public: | 30 | public: |
| 24 | explicit FenceBase(bool is_stubbed_) : is_stubbed{is_stubbed_} {} | 31 | explicit FenceBase(bool is_stubbed_) : is_stubbed{is_stubbed_} {} |
| 25 | 32 | ||
| 33 | bool IsStubbed() const { | ||
| 34 | return is_stubbed; | ||
| 35 | } | ||
| 36 | |||
| 26 | protected: | 37 | protected: |
| 27 | bool is_stubbed; | 38 | bool is_stubbed; |
| 28 | }; | 39 | }; |
| 29 | 40 | ||
| 30 | template <typename TFence, typename TTextureCache, typename TTBufferCache, typename TQueryCache> | 41 | template <typename Traits> |
| 31 | class FenceManager { | 42 | class FenceManager { |
| 43 | using TFence = typename Traits::FenceType; | ||
| 44 | using TTextureCache = typename Traits::TextureCacheType; | ||
| 45 | using TBufferCache = typename Traits::BufferCacheType; | ||
| 46 | using TQueryCache = typename Traits::QueryCacheType; | ||
| 47 | static constexpr bool can_async_check = Traits::HAS_ASYNC_CHECK; | ||
| 48 | |||
| 32 | public: | 49 | public: |
| 33 | /// Notify the fence manager about a new frame | 50 | /// Notify the fence manager about a new frame |
| 34 | void TickFrame() { | 51 | void TickFrame() { |
| 52 | std::unique_lock lock(ring_guard); | ||
| 35 | delayed_destruction_ring.Tick(); | 53 | delayed_destruction_ring.Tick(); |
| 36 | } | 54 | } |
| 37 | 55 | ||
| @@ -46,17 +64,33 @@ public: | |||
| 46 | } | 64 | } |
| 47 | 65 | ||
| 48 | void SignalFence(std::function<void()>&& func) { | 66 | void SignalFence(std::function<void()>&& func) { |
| 49 | TryReleasePendingFences(); | 67 | rasterizer.InvalidateGPUCache(); |
| 68 | bool delay_fence = Settings::IsGPULevelHigh(); | ||
| 69 | if constexpr (!can_async_check) { | ||
| 70 | TryReleasePendingFences<false>(); | ||
| 71 | } | ||
| 50 | const bool should_flush = ShouldFlush(); | 72 | const bool should_flush = ShouldFlush(); |
| 51 | CommitAsyncFlushes(); | 73 | CommitAsyncFlushes(); |
| 52 | uncommitted_operations.emplace_back(std::move(func)); | ||
| 53 | CommitOperations(); | ||
| 54 | TFence new_fence = CreateFence(!should_flush); | 74 | TFence new_fence = CreateFence(!should_flush); |
| 55 | fences.push(new_fence); | 75 | if constexpr (can_async_check) { |
| 76 | guard.lock(); | ||
| 77 | } | ||
| 78 | if (delay_fence) { | ||
| 79 | uncommitted_operations.emplace_back(std::move(func)); | ||
| 80 | } | ||
| 81 | pending_operations.emplace_back(std::move(uncommitted_operations)); | ||
| 56 | QueueFence(new_fence); | 82 | QueueFence(new_fence); |
| 83 | if (!delay_fence) { | ||
| 84 | func(); | ||
| 85 | } | ||
| 86 | fences.push(std::move(new_fence)); | ||
| 57 | if (should_flush) { | 87 | if (should_flush) { |
| 58 | rasterizer.FlushCommands(); | 88 | rasterizer.FlushCommands(); |
| 59 | } | 89 | } |
| 90 | if constexpr (can_async_check) { | ||
| 91 | guard.unlock(); | ||
| 92 | cv.notify_all(); | ||
| 93 | } | ||
| 60 | } | 94 | } |
| 61 | 95 | ||
| 62 | void SignalSyncPoint(u32 value) { | 96 | void SignalSyncPoint(u32 value) { |
| @@ -66,29 +100,30 @@ public: | |||
| 66 | } | 100 | } |
| 67 | 101 | ||
| 68 | void WaitPendingFences() { | 102 | void WaitPendingFences() { |
| 69 | while (!fences.empty()) { | 103 | if constexpr (!can_async_check) { |
| 70 | TFence& current_fence = fences.front(); | 104 | TryReleasePendingFences<true>(); |
| 71 | if (ShouldWait()) { | ||
| 72 | WaitFence(current_fence); | ||
| 73 | } | ||
| 74 | PopAsyncFlushes(); | ||
| 75 | auto operations = std::move(pending_operations.front()); | ||
| 76 | pending_operations.pop_front(); | ||
| 77 | for (auto& operation : operations) { | ||
| 78 | operation(); | ||
| 79 | } | ||
| 80 | PopFence(); | ||
| 81 | } | 105 | } |
| 82 | } | 106 | } |
| 83 | 107 | ||
| 84 | protected: | 108 | protected: |
| 85 | explicit FenceManager(VideoCore::RasterizerInterface& rasterizer_, Tegra::GPU& gpu_, | 109 | explicit FenceManager(VideoCore::RasterizerInterface& rasterizer_, Tegra::GPU& gpu_, |
| 86 | TTextureCache& texture_cache_, TTBufferCache& buffer_cache_, | 110 | TTextureCache& texture_cache_, TBufferCache& buffer_cache_, |
| 87 | TQueryCache& query_cache_) | 111 | TQueryCache& query_cache_) |
| 88 | : rasterizer{rasterizer_}, gpu{gpu_}, syncpoint_manager{gpu.Host1x().GetSyncpointManager()}, | 112 | : rasterizer{rasterizer_}, gpu{gpu_}, syncpoint_manager{gpu.Host1x().GetSyncpointManager()}, |
| 89 | texture_cache{texture_cache_}, buffer_cache{buffer_cache_}, query_cache{query_cache_} {} | 113 | texture_cache{texture_cache_}, buffer_cache{buffer_cache_}, query_cache{query_cache_} { |
| 114 | if constexpr (can_async_check) { | ||
| 115 | fence_thread = | ||
| 116 | std::jthread([this](std::stop_token token) { ReleaseThreadFunc(token); }); | ||
| 117 | } | ||
| 118 | } | ||
| 90 | 119 | ||
| 91 | virtual ~FenceManager() = default; | 120 | virtual ~FenceManager() { |
| 121 | if constexpr (can_async_check) { | ||
| 122 | fence_thread.request_stop(); | ||
| 123 | cv.notify_all(); | ||
| 124 | fence_thread.join(); | ||
| 125 | } | ||
| 126 | } | ||
| 92 | 127 | ||
| 93 | /// Creates a Fence Interface, does not create a backend fence if 'is_stubbed' is | 128 | /// Creates a Fence Interface, does not create a backend fence if 'is_stubbed' is |
| 94 | /// true | 129 | /// true |
| @@ -104,15 +139,20 @@ protected: | |||
| 104 | Tegra::GPU& gpu; | 139 | Tegra::GPU& gpu; |
| 105 | Tegra::Host1x::SyncpointManager& syncpoint_manager; | 140 | Tegra::Host1x::SyncpointManager& syncpoint_manager; |
| 106 | TTextureCache& texture_cache; | 141 | TTextureCache& texture_cache; |
| 107 | TTBufferCache& buffer_cache; | 142 | TBufferCache& buffer_cache; |
| 108 | TQueryCache& query_cache; | 143 | TQueryCache& query_cache; |
| 109 | 144 | ||
| 110 | private: | 145 | private: |
| 146 | template <bool force_wait> | ||
| 111 | void TryReleasePendingFences() { | 147 | void TryReleasePendingFences() { |
| 112 | while (!fences.empty()) { | 148 | while (!fences.empty()) { |
| 113 | TFence& current_fence = fences.front(); | 149 | TFence& current_fence = fences.front(); |
| 114 | if (ShouldWait() && !IsFenceSignaled(current_fence)) { | 150 | if (ShouldWait() && !IsFenceSignaled(current_fence)) { |
| 115 | return; | 151 | if constexpr (force_wait) { |
| 152 | WaitFence(current_fence); | ||
| 153 | } else { | ||
| 154 | return; | ||
| 155 | } | ||
| 116 | } | 156 | } |
| 117 | PopAsyncFlushes(); | 157 | PopAsyncFlushes(); |
| 118 | auto operations = std::move(pending_operations.front()); | 158 | auto operations = std::move(pending_operations.front()); |
| @@ -120,7 +160,49 @@ private: | |||
| 120 | for (auto& operation : operations) { | 160 | for (auto& operation : operations) { |
| 121 | operation(); | 161 | operation(); |
| 122 | } | 162 | } |
| 123 | PopFence(); | 163 | { |
| 164 | std::unique_lock lock(ring_guard); | ||
| 165 | delayed_destruction_ring.Push(std::move(current_fence)); | ||
| 166 | } | ||
| 167 | fences.pop(); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | void ReleaseThreadFunc(std::stop_token stop_token) { | ||
| 172 | std::string name = "GPUFencingThread"; | ||
| 173 | MicroProfileOnThreadCreate(name.c_str()); | ||
| 174 | |||
| 175 | // Cleanup | ||
| 176 | SCOPE_EXIT({ MicroProfileOnThreadExit(); }); | ||
| 177 | |||
| 178 | Common::SetCurrentThreadName(name.c_str()); | ||
| 179 | Common::SetCurrentThreadPriority(Common::ThreadPriority::High); | ||
| 180 | |||
| 181 | TFence current_fence; | ||
| 182 | std::deque<std::function<void()>> current_operations; | ||
| 183 | while (!stop_token.stop_requested()) { | ||
| 184 | { | ||
| 185 | std::unique_lock lock(guard); | ||
| 186 | cv.wait(lock, [&] { return stop_token.stop_requested() || !fences.empty(); }); | ||
| 187 | if (stop_token.stop_requested()) [[unlikely]] { | ||
| 188 | return; | ||
| 189 | } | ||
| 190 | current_fence = std::move(fences.front()); | ||
| 191 | current_operations = std::move(pending_operations.front()); | ||
| 192 | fences.pop(); | ||
| 193 | pending_operations.pop_front(); | ||
| 194 | } | ||
| 195 | if (!current_fence->IsStubbed()) { | ||
| 196 | WaitFence(current_fence); | ||
| 197 | } | ||
| 198 | PopAsyncFlushes(); | ||
| 199 | for (auto& operation : current_operations) { | ||
| 200 | operation(); | ||
| 201 | } | ||
| 202 | { | ||
| 203 | std::unique_lock lock(ring_guard); | ||
| 204 | delayed_destruction_ring.Push(std::move(current_fence)); | ||
| 205 | } | ||
| 124 | } | 206 | } |
| 125 | } | 207 | } |
| 126 | 208 | ||
| @@ -154,19 +236,16 @@ private: | |||
| 154 | query_cache.CommitAsyncFlushes(); | 236 | query_cache.CommitAsyncFlushes(); |
| 155 | } | 237 | } |
| 156 | 238 | ||
| 157 | void PopFence() { | ||
| 158 | delayed_destruction_ring.Push(std::move(fences.front())); | ||
| 159 | fences.pop(); | ||
| 160 | } | ||
| 161 | |||
| 162 | void CommitOperations() { | ||
| 163 | pending_operations.emplace_back(std::move(uncommitted_operations)); | ||
| 164 | } | ||
| 165 | |||
| 166 | std::queue<TFence> fences; | 239 | std::queue<TFence> fences; |
| 167 | std::deque<std::function<void()>> uncommitted_operations; | 240 | std::deque<std::function<void()>> uncommitted_operations; |
| 168 | std::deque<std::deque<std::function<void()>>> pending_operations; | 241 | std::deque<std::deque<std::function<void()>>> pending_operations; |
| 169 | 242 | ||
| 243 | std::mutex guard; | ||
| 244 | std::mutex ring_guard; | ||
| 245 | std::condition_variable cv; | ||
| 246 | |||
| 247 | std::jthread fence_thread; | ||
| 248 | |||
| 170 | DelayedDestructionRing<TFence, 6> delayed_destruction_ring; | 249 | DelayedDestructionRing<TFence, 6> delayed_destruction_ring; |
| 171 | }; | 250 | }; |
| 172 | 251 | ||