gpu_thread.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/assert.h"
  5. #include "common/microprofile.h"
  6. #include "common/scope_exit.h"
  7. #include "common/settings.h"
  8. #include "common/thread.h"
  9. #include "core/core.h"
  10. #include "core/frontend/emu_window.h"
  11. #include "video_core/dma_pusher.h"
  12. #include "video_core/gpu.h"
  13. #include "video_core/gpu_thread.h"
  14. #include "video_core/renderer_base.h"
  15. namespace VideoCommon::GPUThread {
  16. /// Runs the GPU thread
  17. static void RunThread(Core::System& system, VideoCore::RendererBase& renderer,
  18. Core::Frontend::GraphicsContext& context, Tegra::DmaPusher& dma_pusher,
  19. SynchState& state) {
  20. std::string name = "yuzu:GPU";
  21. MicroProfileOnThreadCreate(name.c_str());
  22. SCOPE_EXIT({ MicroProfileOnThreadExit(); });
  23. Common::SetCurrentThreadName(name.c_str());
  24. Common::SetCurrentThreadPriority(Common::ThreadPriority::High);
  25. system.RegisterHostThread();
  26. // Wait for first GPU command before acquiring the window context
  27. state.queue.Wait();
  28. // If emulation was stopped during disk shader loading, abort before trying to acquire context
  29. if (!state.is_running) {
  30. return;
  31. }
  32. auto current_context = context.Acquire();
  33. VideoCore::RasterizerInterface* const rasterizer = renderer.ReadRasterizer();
  34. CommandDataContainer next;
  35. while (state.is_running) {
  36. next = state.queue.PopWait();
  37. if (auto* submit_list = std::get_if<SubmitListCommand>(&next.data)) {
  38. dma_pusher.Push(std::move(submit_list->entries));
  39. dma_pusher.DispatchCalls();
  40. } else if (const auto* data = std::get_if<SwapBuffersCommand>(&next.data)) {
  41. renderer.SwapBuffers(data->framebuffer ? &*data->framebuffer : nullptr);
  42. } else if (std::holds_alternative<OnCommandListEndCommand>(next.data)) {
  43. rasterizer->ReleaseFences();
  44. } else if (std::holds_alternative<GPUTickCommand>(next.data)) {
  45. system.GPU().TickWork();
  46. } else if (const auto* flush = std::get_if<FlushRegionCommand>(&next.data)) {
  47. rasterizer->FlushRegion(flush->addr, flush->size);
  48. } else if (const auto* invalidate = std::get_if<InvalidateRegionCommand>(&next.data)) {
  49. rasterizer->OnCPUWrite(invalidate->addr, invalidate->size);
  50. } else if (std::holds_alternative<EndProcessingCommand>(next.data)) {
  51. ASSERT(state.is_running == false);
  52. } else {
  53. UNREACHABLE();
  54. }
  55. state.signaled_fence.store(next.fence);
  56. if (next.block) {
  57. // We have to lock the write_lock to ensure that the condition_variable wait not get a
  58. // race between the check and the lock itself.
  59. std::lock_guard lk(state.write_lock);
  60. state.cv.notify_all();
  61. }
  62. }
  63. }
  64. ThreadManager::ThreadManager(Core::System& system_, bool is_async_)
  65. : system{system_}, is_async{is_async_} {}
  66. ThreadManager::~ThreadManager() {
  67. ShutDown();
  68. }
  69. void ThreadManager::StartThread(VideoCore::RendererBase& renderer,
  70. Core::Frontend::GraphicsContext& context,
  71. Tegra::DmaPusher& dma_pusher) {
  72. rasterizer = renderer.ReadRasterizer();
  73. thread = std::thread(RunThread, std::ref(system), std::ref(renderer), std::ref(context),
  74. std::ref(dma_pusher), std::ref(state));
  75. }
  76. void ThreadManager::SubmitList(Tegra::CommandList&& entries) {
  77. PushCommand(SubmitListCommand(std::move(entries)));
  78. }
  79. void ThreadManager::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) {
  80. PushCommand(SwapBuffersCommand(framebuffer ? std::make_optional(*framebuffer) : std::nullopt));
  81. }
  82. void ThreadManager::FlushRegion(VAddr addr, u64 size) {
  83. if (!is_async) {
  84. // Always flush with synchronous GPU mode
  85. PushCommand(FlushRegionCommand(addr, size));
  86. return;
  87. }
  88. if (!Settings::IsGPULevelExtreme()) {
  89. return;
  90. }
  91. auto& gpu = system.GPU();
  92. u64 fence = gpu.RequestFlush(addr, size);
  93. PushCommand(GPUTickCommand(), true);
  94. ASSERT(fence <= gpu.CurrentFlushRequestFence());
  95. }
  96. void ThreadManager::InvalidateRegion(VAddr addr, u64 size) {
  97. rasterizer->OnCPUWrite(addr, size);
  98. }
  99. void ThreadManager::FlushAndInvalidateRegion(VAddr addr, u64 size) {
  100. // Skip flush on asynch mode, as FlushAndInvalidateRegion is not used for anything too important
  101. rasterizer->OnCPUWrite(addr, size);
  102. }
  103. void ThreadManager::ShutDown() {
  104. if (!state.is_running) {
  105. return;
  106. }
  107. {
  108. std::lock_guard lk(state.write_lock);
  109. state.is_running = false;
  110. state.cv.notify_all();
  111. }
  112. if (!thread.joinable()) {
  113. return;
  114. }
  115. // Notify GPU thread that a shutdown is pending
  116. PushCommand(EndProcessingCommand());
  117. thread.join();
  118. }
  119. void ThreadManager::OnCommandListEnd() {
  120. PushCommand(OnCommandListEndCommand());
  121. }
  122. u64 ThreadManager::PushCommand(CommandData&& command_data, bool block) {
  123. if (!is_async) {
  124. // In synchronous GPU mode, block the caller until the command has executed
  125. block = true;
  126. }
  127. std::unique_lock lk(state.write_lock);
  128. const u64 fence{++state.last_fence};
  129. state.queue.Push(CommandDataContainer(std::move(command_data), fence, block));
  130. if (block) {
  131. state.cv.wait(lk, [this, fence] {
  132. return fence <= state.signaled_fence.load(std::memory_order_relaxed) ||
  133. !state.is_running;
  134. });
  135. }
  136. return fence;
  137. }
  138. } // namespace VideoCommon::GPUThread