gpu_thread.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/thread.h"
  7. #include "core/core.h"
  8. #include "core/frontend/emu_window.h"
  9. #include "core/settings.h"
  10. #include "video_core/dma_pusher.h"
  11. #include "video_core/gpu.h"
  12. #include "video_core/gpu_thread.h"
  13. #include "video_core/renderer_base.h"
  14. namespace VideoCommon::GPUThread {
  15. /// Runs the GPU thread
  16. static void RunThread(Core::System& system, VideoCore::RendererBase& renderer,
  17. Core::Frontend::GraphicsContext& context, Tegra::DmaPusher& dma_pusher,
  18. SynchState& state) {
  19. std::string name = "yuzu:GPU";
  20. MicroProfileOnThreadCreate(name.c_str());
  21. Common::SetCurrentThreadName(name.c_str());
  22. Common::SetCurrentThreadPriority(Common::ThreadPriority::High);
  23. system.RegisterHostThread();
  24. // Wait for first GPU command before acquiring the window context
  25. while (state.queue.Empty())
  26. ;
  27. // If emulation was stopped during disk shader loading, abort before trying to acquire context
  28. if (!state.is_running) {
  29. return;
  30. }
  31. auto current_context = context.Acquire();
  32. CommandDataContainer next;
  33. while (state.is_running) {
  34. next = state.queue.PopWait();
  35. if (const auto submit_list = std::get_if<SubmitListCommand>(&next.data)) {
  36. dma_pusher.Push(std::move(submit_list->entries));
  37. dma_pusher.DispatchCalls();
  38. } else if (const auto data = std::get_if<SwapBuffersCommand>(&next.data)) {
  39. renderer.SwapBuffers(data->framebuffer ? &*data->framebuffer : nullptr);
  40. } else if (const auto data = std::get_if<OnCommandListEndCommand>(&next.data)) {
  41. renderer.Rasterizer().ReleaseFences();
  42. } else if (const auto data = std::get_if<GPUTickCommand>(&next.data)) {
  43. system.GPU().TickWork();
  44. } else if (const auto data = std::get_if<FlushRegionCommand>(&next.data)) {
  45. renderer.Rasterizer().FlushRegion(data->addr, data->size);
  46. } else if (const auto data = std::get_if<InvalidateRegionCommand>(&next.data)) {
  47. renderer.Rasterizer().OnCPUWrite(data->addr, data->size);
  48. } else if (std::holds_alternative<EndProcessingCommand>(next.data)) {
  49. return;
  50. } else {
  51. UNREACHABLE();
  52. }
  53. state.signaled_fence.store(next.fence);
  54. }
  55. }
  56. ThreadManager::ThreadManager(Core::System& system) : system{system} {}
  57. ThreadManager::~ThreadManager() {
  58. if (!thread.joinable()) {
  59. return;
  60. }
  61. // Notify GPU thread that a shutdown is pending
  62. PushCommand(EndProcessingCommand());
  63. thread.join();
  64. }
  65. void ThreadManager::StartThread(VideoCore::RendererBase& renderer,
  66. Core::Frontend::GraphicsContext& context,
  67. Tegra::DmaPusher& dma_pusher) {
  68. thread = std::thread{RunThread, std::ref(system), std::ref(renderer),
  69. std::ref(context), std::ref(dma_pusher), std::ref(state)};
  70. }
  71. void ThreadManager::SubmitList(Tegra::CommandList&& entries) {
  72. PushCommand(SubmitListCommand(std::move(entries)));
  73. }
  74. void ThreadManager::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) {
  75. PushCommand(SwapBuffersCommand(framebuffer ? std::make_optional(*framebuffer) : std::nullopt));
  76. }
  77. void ThreadManager::FlushRegion(VAddr addr, u64 size) {
  78. if (!Settings::IsGPULevelHigh()) {
  79. PushCommand(FlushRegionCommand(addr, size));
  80. return;
  81. }
  82. if (!Settings::IsGPULevelExtreme()) {
  83. return;
  84. }
  85. if (system.Renderer().Rasterizer().MustFlushRegion(addr, size)) {
  86. auto& gpu = system.GPU();
  87. u64 fence = gpu.RequestFlush(addr, size);
  88. PushCommand(GPUTickCommand());
  89. while (fence > gpu.CurrentFlushRequestFence()) {
  90. }
  91. }
  92. }
  93. void ThreadManager::InvalidateRegion(VAddr addr, u64 size) {
  94. system.Renderer().Rasterizer().OnCPUWrite(addr, size);
  95. }
  96. void ThreadManager::FlushAndInvalidateRegion(VAddr addr, u64 size) {
  97. // Skip flush on asynch mode, as FlushAndInvalidateRegion is not used for anything too important
  98. system.Renderer().Rasterizer().OnCPUWrite(addr, size);
  99. }
  100. void ThreadManager::WaitIdle() const {
  101. while (state.last_fence > state.signaled_fence.load(std::memory_order_relaxed)) {
  102. }
  103. }
  104. void ThreadManager::OnCommandListEnd() {
  105. PushCommand(OnCommandListEndCommand());
  106. }
  107. u64 ThreadManager::PushCommand(CommandData&& command_data) {
  108. const u64 fence{++state.last_fence};
  109. state.queue.Push(CommandDataContainer(std::move(command_data), fence));
  110. return fence;
  111. }
  112. } // namespace VideoCommon::GPUThread