gpu_thread.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "common/assert.h"
  4. #include "common/microprofile.h"
  5. #include "common/scope_exit.h"
  6. #include "common/settings.h"
  7. #include "common/thread.h"
  8. #include "core/core.h"
  9. #include "core/frontend/graphics_context.h"
  10. #include "video_core/control/scheduler.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::Control::Scheduler& scheduler, SynchState& state) {
  20. std::string name = "GPU";
  21. MicroProfileOnThreadCreate(name.c_str());
  22. SCOPE_EXIT {
  23. MicroProfileOnThreadExit();
  24. };
  25. Common::SetCurrentThreadName(name.c_str());
  26. Common::SetCurrentThreadPriority(Common::ThreadPriority::Critical);
  27. system.RegisterHostThread();
  28. auto current_context = context.Acquire();
  29. VideoCore::RasterizerInterface* const rasterizer = renderer.ReadRasterizer();
  30. CommandDataContainer next;
  31. while (!stop_token.stop_requested()) {
  32. state.queue.PopWait(next, stop_token);
  33. if (stop_token.stop_requested()) {
  34. break;
  35. }
  36. if (auto* submit_list = std::get_if<SubmitListCommand>(&next.data)) {
  37. scheduler.Push(submit_list->channel, std::move(submit_list->entries));
  38. } else if (std::holds_alternative<GPUTickCommand>(next.data)) {
  39. system.GPU().TickWork();
  40. } else if (const auto* flush = std::get_if<FlushRegionCommand>(&next.data)) {
  41. rasterizer->FlushRegion(flush->addr, flush->size);
  42. } else if (const auto* invalidate = std::get_if<InvalidateRegionCommand>(&next.data)) {
  43. rasterizer->OnCacheInvalidation(invalidate->addr, invalidate->size);
  44. } else {
  45. ASSERT(false);
  46. }
  47. state.signaled_fence.store(next.fence);
  48. if (next.block) {
  49. // We have to lock the write_lock to ensure that the condition_variable wait not get a
  50. // race between the check and the lock itself.
  51. std::scoped_lock lk{state.write_lock};
  52. state.cv.notify_all();
  53. }
  54. }
  55. }
  56. ThreadManager::ThreadManager(Core::System& system_, bool is_async_)
  57. : system{system_}, is_async{is_async_} {}
  58. ThreadManager::~ThreadManager() = default;
  59. void ThreadManager::StartThread(VideoCore::RendererBase& renderer,
  60. Core::Frontend::GraphicsContext& context,
  61. Tegra::Control::Scheduler& scheduler) {
  62. rasterizer = renderer.ReadRasterizer();
  63. thread = std::jthread(RunThread, std::ref(system), std::ref(renderer), std::ref(context),
  64. std::ref(scheduler), std::ref(state));
  65. }
  66. void ThreadManager::SubmitList(s32 channel, Tegra::CommandList&& entries) {
  67. PushCommand(SubmitListCommand(channel, std::move(entries)));
  68. }
  69. void ThreadManager::FlushRegion(DAddr addr, u64 size) {
  70. if (!is_async) {
  71. // Always flush with synchronous GPU mode
  72. PushCommand(FlushRegionCommand(addr, size));
  73. return;
  74. }
  75. if (!Settings::IsGPULevelExtreme()) {
  76. return;
  77. }
  78. auto& gpu = system.GPU();
  79. u64 fence = gpu.RequestFlush(addr, size);
  80. TickGPU();
  81. gpu.WaitForSyncOperation(fence);
  82. }
  83. void ThreadManager::TickGPU() {
  84. PushCommand(GPUTickCommand());
  85. }
  86. void ThreadManager::InvalidateRegion(DAddr addr, u64 size) {
  87. rasterizer->OnCacheInvalidation(addr, size);
  88. }
  89. void ThreadManager::FlushAndInvalidateRegion(DAddr addr, u64 size) {
  90. // Skip flush on asynch mode, as FlushAndInvalidateRegion is not used for anything too important
  91. rasterizer->OnCacheInvalidation(addr, size);
  92. }
  93. u64 ThreadManager::PushCommand(CommandData&& command_data, bool block) {
  94. if (!is_async) {
  95. // In synchronous GPU mode, block the caller until the command has executed
  96. block = true;
  97. }
  98. std::unique_lock lk(state.write_lock);
  99. const u64 fence{++state.last_fence};
  100. state.queue.EmplaceWait(std::move(command_data), fence, block);
  101. if (block) {
  102. Common::CondvarWait(state.cv, lk, thread.get_stop_token(), [this, fence] {
  103. return fence <= state.signaled_fence.load(std::memory_order_relaxed);
  104. });
  105. }
  106. return fence;
  107. }
  108. } // namespace VideoCommon::GPUThread