fence_manager.h 5.2 KB

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