fence_manager.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <array>
  7. #include <memory>
  8. #include <queue>
  9. #include "common/assert.h"
  10. #include "common/common_types.h"
  11. #include "core/core.h"
  12. #include "core/memory.h"
  13. #include "core/settings.h"
  14. #include "video_core/gpu.h"
  15. #include "video_core/memory_manager.h"
  16. #include "video_core/rasterizer_interface.h"
  17. namespace VideoCommon {
  18. class FenceBase {
  19. public:
  20. FenceBase(u32 payload, bool is_stubbed)
  21. : address{}, payload{payload}, is_semaphore{false}, is_stubbed{is_stubbed} {}
  22. FenceBase(GPUVAddr address, u32 payload, bool is_stubbed)
  23. : address{address}, payload{payload}, is_semaphore{true}, is_stubbed{is_stubbed} {}
  24. GPUVAddr GetAddress() const {
  25. return address;
  26. }
  27. u32 GetPayload() const {
  28. return payload;
  29. }
  30. bool IsSemaphore() const {
  31. return is_semaphore;
  32. }
  33. private:
  34. GPUVAddr address;
  35. u32 payload;
  36. bool is_semaphore;
  37. protected:
  38. bool is_stubbed;
  39. };
  40. template <typename TFence, typename TTextureCache, typename TTBufferCache, typename TQueryCache>
  41. class FenceManager {
  42. public:
  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. auto& gpu{system.GPU()};
  69. auto& memory_manager{gpu.MemoryManager()};
  70. while (!fences.empty()) {
  71. TFence& current_fence = fences.front();
  72. if (ShouldWait()) {
  73. WaitFence(current_fence);
  74. }
  75. PopAsyncFlushes();
  76. if (current_fence->IsSemaphore()) {
  77. memory_manager.template Write<u32>(current_fence->GetAddress(),
  78. current_fence->GetPayload());
  79. } else {
  80. gpu.IncrementSyncPoint(current_fence->GetPayload());
  81. }
  82. fences.pop();
  83. }
  84. }
  85. protected:
  86. FenceManager(Core::System& system, VideoCore::RasterizerInterface& rasterizer,
  87. TTextureCache& texture_cache, TTBufferCache& buffer_cache,
  88. TQueryCache& query_cache)
  89. : system{system}, rasterizer{rasterizer}, texture_cache{texture_cache},
  90. buffer_cache{buffer_cache}, query_cache{query_cache} {}
  91. virtual ~FenceManager() {}
  92. /// Creates a Sync Point Fence Interface, does not create a backend fence if 'is_stubbed' is
  93. /// true
  94. virtual TFence CreateFence(u32 value, bool is_stubbed) = 0;
  95. /// Creates a Semaphore Fence Interface, does not create a backend fence if 'is_stubbed' is true
  96. virtual TFence CreateFence(GPUVAddr addr, u32 value, bool is_stubbed) = 0;
  97. /// Queues a fence into the backend if the fence isn't stubbed.
  98. virtual void QueueFence(TFence& fence) = 0;
  99. /// Notifies that the backend fence has been signaled/reached in host GPU.
  100. virtual bool IsFenceSignaled(TFence& fence) const = 0;
  101. /// Waits until a fence has been signalled by the host GPU.
  102. virtual void WaitFence(TFence& fence) = 0;
  103. Core::System& system;
  104. VideoCore::RasterizerInterface& rasterizer;
  105. TTextureCache& texture_cache;
  106. TTBufferCache& buffer_cache;
  107. TQueryCache& query_cache;
  108. private:
  109. void TryReleasePendingFences() {
  110. auto& gpu{system.GPU()};
  111. auto& memory_manager{gpu.MemoryManager()};
  112. while (!fences.empty()) {
  113. TFence& current_fence = fences.front();
  114. if (ShouldWait() && !IsFenceSignaled(current_fence)) {
  115. return;
  116. }
  117. PopAsyncFlushes();
  118. if (current_fence->IsSemaphore()) {
  119. memory_manager.template Write<u32>(current_fence->GetAddress(),
  120. current_fence->GetPayload());
  121. } else {
  122. gpu.IncrementSyncPoint(current_fence->GetPayload());
  123. }
  124. fences.pop();
  125. }
  126. }
  127. bool ShouldWait() const {
  128. return texture_cache.ShouldWaitAsyncFlushes() || buffer_cache.ShouldWaitAsyncFlushes() ||
  129. query_cache.ShouldWaitAsyncFlushes();
  130. }
  131. bool ShouldFlush() const {
  132. return texture_cache.HasUncommittedFlushes() || buffer_cache.HasUncommittedFlushes() ||
  133. query_cache.HasUncommittedFlushes();
  134. }
  135. void PopAsyncFlushes() {
  136. texture_cache.PopAsyncFlushes();
  137. buffer_cache.PopAsyncFlushes();
  138. query_cache.PopAsyncFlushes();
  139. }
  140. void CommitAsyncFlushes() {
  141. texture_cache.CommitAsyncFlushes();
  142. buffer_cache.CommitAsyncFlushes();
  143. query_cache.CommitAsyncFlushes();
  144. }
  145. std::queue<TFence> fences;
  146. };
  147. } // namespace VideoCommon