gpu_thread.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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(std::stop_token stop_token, Core::System& system,
  18. VideoCore::RendererBase& renderer, Core::Frontend::GraphicsContext& context,
  19. Tegra::DmaPusher& dma_pusher, 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. auto current_context = context.Acquire();
  27. VideoCore::RasterizerInterface* const rasterizer = renderer.ReadRasterizer();
  28. while (!stop_token.stop_requested()) {
  29. CommandDataContainer next = state.queue.PopWait(stop_token);
  30. if (stop_token.stop_requested()) {
  31. break;
  32. }
  33. if (auto* submit_list = std::get_if<SubmitListCommand>(&next.data)) {
  34. dma_pusher.Push(std::move(submit_list->entries));
  35. dma_pusher.DispatchCalls();
  36. } else if (const auto* data = std::get_if<SwapBuffersCommand>(&next.data)) {
  37. renderer.SwapBuffers(data->framebuffer ? &*data->framebuffer : nullptr);
  38. } else if (std::holds_alternative<OnCommandListEndCommand>(next.data)) {
  39. rasterizer->ReleaseFences();
  40. } else if (std::holds_alternative<GPUTickCommand>(next.data)) {
  41. system.GPU().TickWork();
  42. } else if (const auto* flush = std::get_if<FlushRegionCommand>(&next.data)) {
  43. rasterizer->FlushRegion(flush->addr, flush->size);
  44. } else if (const auto* invalidate = std::get_if<InvalidateRegionCommand>(&next.data)) {
  45. rasterizer->OnCPUWrite(invalidate->addr, invalidate->size);
  46. } else {
  47. UNREACHABLE();
  48. }
  49. state.signaled_fence.store(next.fence);
  50. if (next.block) {
  51. // We have to lock the write_lock to ensure that the condition_variable wait not get a
  52. // race between the check and the lock itself.
  53. std::scoped_lock lk{state.write_lock};
  54. state.cv.notify_all();
  55. }
  56. }
  57. }
  58. ThreadManager::ThreadManager(Core::System& system_, bool is_async_)
  59. : system{system_}, is_async{is_async_} {}
  60. ThreadManager::~ThreadManager() = default;
  61. void ThreadManager::StartThread(VideoCore::RendererBase& renderer,
  62. Core::Frontend::GraphicsContext& context,
  63. Tegra::DmaPusher& dma_pusher) {
  64. rasterizer = renderer.ReadRasterizer();
  65. thread = std::jthread(RunThread, std::ref(system), std::ref(renderer), std::ref(context),
  66. std::ref(dma_pusher), std::ref(state));
  67. }
  68. void ThreadManager::SubmitList(Tegra::CommandList&& entries) {
  69. PushCommand(SubmitListCommand(std::move(entries)));
  70. }
  71. void ThreadManager::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) {
  72. PushCommand(SwapBuffersCommand(framebuffer ? std::make_optional(*framebuffer) : std::nullopt));
  73. }
  74. void ThreadManager::FlushRegion(VAddr addr, u64 size) {
  75. if (!is_async) {
  76. // Always flush with synchronous GPU mode
  77. PushCommand(FlushRegionCommand(addr, size));
  78. return;
  79. }
  80. if (!Settings::IsGPULevelExtreme()) {
  81. return;
  82. }
  83. auto& gpu = system.GPU();
  84. u64 fence = gpu.RequestFlush(addr, size);
  85. PushCommand(GPUTickCommand(), true);
  86. ASSERT(fence <= gpu.CurrentFlushRequestFence());
  87. }
  88. void ThreadManager::InvalidateRegion(VAddr addr, u64 size) {
  89. rasterizer->OnCPUWrite(addr, size);
  90. }
  91. void ThreadManager::FlushAndInvalidateRegion(VAddr addr, u64 size) {
  92. // Skip flush on asynch mode, as FlushAndInvalidateRegion is not used for anything too important
  93. rasterizer->OnCPUWrite(addr, size);
  94. }
  95. void ThreadManager::OnCommandListEnd() {
  96. PushCommand(OnCommandListEndCommand());
  97. }
  98. u64 ThreadManager::PushCommand(CommandData&& command_data, bool block) {
  99. if (!is_async) {
  100. // In synchronous GPU mode, block the caller until the command has executed
  101. block = true;
  102. }
  103. std::unique_lock lk(state.write_lock);
  104. const u64 fence{++state.last_fence};
  105. state.queue.Push(CommandDataContainer(std::move(command_data), fence, block));
  106. if (block) {
  107. state.cv.wait(lk, thread.get_stop_token(), [this, fence] {
  108. return fence <= state.signaled_fence.load(std::memory_order_relaxed);
  109. });
  110. }
  111. return fence;
  112. }
  113. } // namespace VideoCommon::GPUThread