gpu_thread.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "core/core.h"
  7. #include "core/core_timing.h"
  8. #include "core/core_timing_util.h"
  9. #include "core/frontend/scope_acquire_window_context.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(VideoCore::RendererBase& renderer, Tegra::DmaPusher& dma_pusher,
  17. SynchState& state) {
  18. MicroProfileOnThreadCreate("GpuThread");
  19. // Wait for first GPU command before acquiring the window context
  20. state.WaitForCommands();
  21. // If emulation was stopped during disk shader loading, abort before trying to acquire context
  22. if (!state.is_running) {
  23. return;
  24. }
  25. Core::Frontend::ScopeAcquireWindowContext acquire_context{renderer.GetRenderWindow()};
  26. CommandDataContainer next;
  27. while (state.is_running) {
  28. state.WaitForCommands();
  29. while (!state.queue.Empty()) {
  30. state.queue.Pop(next);
  31. if (const auto submit_list = std::get_if<SubmitListCommand>(&next.data)) {
  32. dma_pusher.Push(std::move(submit_list->entries));
  33. dma_pusher.DispatchCalls();
  34. } else if (const auto data = std::get_if<SwapBuffersCommand>(&next.data)) {
  35. renderer.SwapBuffers(std::move(data->framebuffer));
  36. } else if (const auto data = std::get_if<FlushRegionCommand>(&next.data)) {
  37. renderer.Rasterizer().FlushRegion(data->addr, data->size);
  38. } else if (const auto data = std::get_if<InvalidateRegionCommand>(&next.data)) {
  39. renderer.Rasterizer().InvalidateRegion(data->addr, data->size);
  40. } else if (std::holds_alternative<EndProcessingCommand>(next.data)) {
  41. return;
  42. } else {
  43. UNREACHABLE();
  44. }
  45. state.signaled_fence = next.fence;
  46. state.TrySynchronize();
  47. }
  48. }
  49. }
  50. ThreadManager::ThreadManager(Core::System& system) : system{system} {}
  51. ThreadManager::~ThreadManager() {
  52. if (!thread.joinable()) {
  53. return;
  54. }
  55. // Notify GPU thread that a shutdown is pending
  56. PushCommand(EndProcessingCommand());
  57. thread.join();
  58. }
  59. void ThreadManager::StartThread(VideoCore::RendererBase& renderer, Tegra::DmaPusher& dma_pusher) {
  60. thread = std::thread{RunThread, std::ref(renderer), std::ref(dma_pusher), std::ref(state)};
  61. synchronization_event = system.CoreTiming().RegisterEvent(
  62. "GPUThreadSynch", [this](u64 fence, s64) { state.WaitForSynchronization(fence); });
  63. }
  64. void ThreadManager::SubmitList(Tegra::CommandList&& entries) {
  65. const u64 fence{PushCommand(SubmitListCommand(std::move(entries)))};
  66. const s64 synchronization_ticks{Core::Timing::usToCycles(std::chrono::microseconds{9000})};
  67. system.CoreTiming().ScheduleEvent(synchronization_ticks, synchronization_event, fence);
  68. }
  69. void ThreadManager::SwapBuffers(
  70. std::optional<std::reference_wrapper<const Tegra::FramebufferConfig>> framebuffer) {
  71. PushCommand(SwapBuffersCommand(std::move(framebuffer)));
  72. }
  73. void ThreadManager::FlushRegion(CacheAddr addr, u64 size) {
  74. PushCommand(FlushRegionCommand(addr, size));
  75. }
  76. void ThreadManager::InvalidateRegion(CacheAddr addr, u64 size) {
  77. if (state.queue.Empty()) {
  78. // It's quicker to invalidate a single region on the CPU if the queue is already empty
  79. system.Renderer().Rasterizer().InvalidateRegion(addr, size);
  80. } else {
  81. PushCommand(InvalidateRegionCommand(addr, size));
  82. }
  83. }
  84. void ThreadManager::FlushAndInvalidateRegion(CacheAddr addr, u64 size) {
  85. // Skip flush on asynch mode, as FlushAndInvalidateRegion is not used for anything too important
  86. InvalidateRegion(addr, size);
  87. }
  88. u64 ThreadManager::PushCommand(CommandData&& command_data) {
  89. const u64 fence{++state.last_fence};
  90. state.queue.Push(CommandDataContainer(std::move(command_data), fence));
  91. state.SignalCommands();
  92. return fence;
  93. }
  94. MICROPROFILE_DEFINE(GPU_wait, "GPU", "Wait for the GPU", MP_RGB(128, 128, 192));
  95. void SynchState::WaitForSynchronization(u64 fence) {
  96. if (signaled_fence >= fence) {
  97. return;
  98. }
  99. // Wait for the GPU to be idle (all commands to be executed)
  100. {
  101. MICROPROFILE_SCOPE(GPU_wait);
  102. std::unique_lock lock{synchronization_mutex};
  103. synchronization_condition.wait(lock, [this, fence] { return signaled_fence >= fence; });
  104. }
  105. }
  106. } // namespace VideoCommon::GPUThread