fence_manager.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <algorithm>
  6. #include <queue>
  7. #include "common/common_types.h"
  8. #include "core/core.h"
  9. #include "video_core/delayed_destruction_ring.h"
  10. #include "video_core/gpu.h"
  11. #include "video_core/memory_manager.h"
  12. #include "video_core/rasterizer_interface.h"
  13. namespace VideoCommon {
  14. class FenceBase {
  15. public:
  16. explicit FenceBase(u32 payload_, bool is_stubbed_)
  17. : address{}, payload{payload_}, is_semaphore{false}, is_stubbed{is_stubbed_} {}
  18. explicit FenceBase(GPUVAddr address_, u32 payload_, bool is_stubbed_)
  19. : address{address_}, payload{payload_}, is_semaphore{true}, is_stubbed{is_stubbed_} {}
  20. GPUVAddr GetAddress() const {
  21. return address;
  22. }
  23. u32 GetPayload() const {
  24. return payload;
  25. }
  26. bool IsSemaphore() const {
  27. return is_semaphore;
  28. }
  29. private:
  30. GPUVAddr address;
  31. u32 payload;
  32. bool is_semaphore;
  33. protected:
  34. bool is_stubbed;
  35. };
  36. template <typename TFence, typename TTextureCache, typename TTBufferCache, typename TQueryCache>
  37. class FenceManager {
  38. public:
  39. /// Notify the fence manager about a new frame
  40. void TickFrame() {
  41. delayed_destruction_ring.Tick();
  42. }
  43. void SignalSemaphore(GPUVAddr addr, u32 value) {
  44. TryReleasePendingFences();
  45. const bool should_flush = ShouldFlush();
  46. CommitAsyncFlushes();
  47. TFence new_fence = CreateFence(addr, value, !should_flush);
  48. fences.push(new_fence);
  49. QueueFence(new_fence);
  50. if (should_flush) {
  51. rasterizer.FlushCommands();
  52. }
  53. rasterizer.SyncGuestHost();
  54. }
  55. void SignalSyncPoint(u32 value) {
  56. TryReleasePendingFences();
  57. const bool should_flush = ShouldFlush();
  58. CommitAsyncFlushes();
  59. TFence new_fence = CreateFence(value, !should_flush);
  60. fences.push(new_fence);
  61. QueueFence(new_fence);
  62. if (should_flush) {
  63. rasterizer.FlushCommands();
  64. }
  65. rasterizer.SyncGuestHost();
  66. }
  67. void WaitPendingFences() {
  68. while (!fences.empty()) {
  69. TFence& current_fence = fences.front();
  70. if (ShouldWait()) {
  71. WaitFence(current_fence);
  72. }
  73. PopAsyncFlushes();
  74. if (current_fence->IsSemaphore()) {
  75. gpu_memory.template Write<u32>(current_fence->GetAddress(),
  76. current_fence->GetPayload());
  77. } else {
  78. gpu.IncrementSyncPoint(current_fence->GetPayload());
  79. }
  80. PopFence();
  81. }
  82. }
  83. protected:
  84. explicit FenceManager(VideoCore::RasterizerInterface& rasterizer_, Tegra::GPU& gpu_,
  85. TTextureCache& texture_cache_, TTBufferCache& buffer_cache_,
  86. TQueryCache& query_cache_)
  87. : rasterizer{rasterizer_}, gpu{gpu_}, gpu_memory{gpu.MemoryManager()},
  88. texture_cache{texture_cache_}, buffer_cache{buffer_cache_}, query_cache{query_cache_} {}
  89. virtual ~FenceManager() = default;
  90. /// Creates a Sync Point Fence Interface, does not create a backend fence if 'is_stubbed' is
  91. /// true
  92. virtual TFence CreateFence(u32 value, bool is_stubbed) = 0;
  93. /// Creates a Semaphore Fence Interface, does not create a backend fence if 'is_stubbed' is true
  94. virtual TFence CreateFence(GPUVAddr addr, u32 value, bool is_stubbed) = 0;
  95. /// Queues a fence into the backend if the fence isn't stubbed.
  96. virtual void QueueFence(TFence& fence) = 0;
  97. /// Notifies that the backend fence has been signaled/reached in host GPU.
  98. virtual bool IsFenceSignaled(TFence& fence) const = 0;
  99. /// Waits until a fence has been signalled by the host GPU.
  100. virtual void WaitFence(TFence& fence) = 0;
  101. VideoCore::RasterizerInterface& rasterizer;
  102. Tegra::GPU& gpu;
  103. Tegra::MemoryManager& gpu_memory;
  104. TTextureCache& texture_cache;
  105. TTBufferCache& buffer_cache;
  106. TQueryCache& query_cache;
  107. private:
  108. void TryReleasePendingFences() {
  109. while (!fences.empty()) {
  110. TFence& current_fence = fences.front();
  111. if (ShouldWait() && !IsFenceSignaled(current_fence)) {
  112. return;
  113. }
  114. PopAsyncFlushes();
  115. if (current_fence->IsSemaphore()) {
  116. gpu_memory.template Write<u32>(current_fence->GetAddress(),
  117. current_fence->GetPayload());
  118. } else {
  119. gpu.IncrementSyncPoint(current_fence->GetPayload());
  120. }
  121. PopFence();
  122. }
  123. }
  124. bool ShouldWait() const {
  125. return texture_cache.ShouldWaitAsyncFlushes() || buffer_cache.ShouldWaitAsyncFlushes() ||
  126. query_cache.ShouldWaitAsyncFlushes();
  127. }
  128. bool ShouldFlush() const {
  129. return texture_cache.HasUncommittedFlushes() || buffer_cache.HasUncommittedFlushes() ||
  130. query_cache.HasUncommittedFlushes();
  131. }
  132. void PopAsyncFlushes() {
  133. texture_cache.PopAsyncFlushes();
  134. buffer_cache.PopAsyncFlushes();
  135. query_cache.PopAsyncFlushes();
  136. }
  137. void CommitAsyncFlushes() {
  138. texture_cache.CommitAsyncFlushes();
  139. buffer_cache.CommitAsyncFlushes();
  140. query_cache.CommitAsyncFlushes();
  141. }
  142. void PopFence() {
  143. delayed_destruction_ring.Push(std::move(fences.front()));
  144. fences.pop();
  145. }
  146. std::queue<TFence> fences;
  147. DelayedDestructionRing<TFence, 6> delayed_destruction_ring;
  148. };
  149. } // namespace VideoCommon