gpu_thread.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/thread.h"
  8. #include "core/core.h"
  9. #include "core/frontend/emu_window.h"
  10. #include "core/settings.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, Tegra::CDmaPusher& cdma_pusher) {
  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. while (state.queue.Empty())
  28. ;
  29. // If emulation was stopped during disk shader loading, abort before trying to acquire context
  30. if (!state.is_running) {
  31. return;
  32. }
  33. auto current_context = context.Acquire();
  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 (auto* command_list = std::get_if<SubmitChCommandEntries>(&next.data)) {
  41. // NVDEC
  42. cdma_pusher.Push(std::move(command_list->entries));
  43. cdma_pusher.DispatchCalls();
  44. } else if (const auto* data = std::get_if<SwapBuffersCommand>(&next.data)) {
  45. renderer.SwapBuffers(data->framebuffer ? &*data->framebuffer : nullptr);
  46. } else if (std::holds_alternative<OnCommandListEndCommand>(next.data)) {
  47. renderer.Rasterizer().ReleaseFences();
  48. } else if (std::holds_alternative<GPUTickCommand>(next.data)) {
  49. system.GPU().TickWork();
  50. } else if (const auto* flush = std::get_if<FlushRegionCommand>(&next.data)) {
  51. renderer.Rasterizer().FlushRegion(flush->addr, flush->size);
  52. } else if (const auto* invalidate = std::get_if<InvalidateRegionCommand>(&next.data)) {
  53. renderer.Rasterizer().OnCPUWrite(invalidate->addr, invalidate->size);
  54. } else if (std::holds_alternative<EndProcessingCommand>(next.data)) {
  55. return;
  56. } else {
  57. UNREACHABLE();
  58. }
  59. state.signaled_fence.store(next.fence);
  60. }
  61. }
  62. ThreadManager::ThreadManager(Core::System& system_, bool is_async_)
  63. : system{system_}, is_async{is_async_} {}
  64. ThreadManager::~ThreadManager() {
  65. if (!thread.joinable()) {
  66. return;
  67. }
  68. // Notify GPU thread that a shutdown is pending
  69. PushCommand(EndProcessingCommand());
  70. thread.join();
  71. }
  72. void ThreadManager::StartThread(VideoCore::RendererBase& renderer,
  73. Core::Frontend::GraphicsContext& context,
  74. Tegra::DmaPusher& dma_pusher, Tegra::CDmaPusher& cdma_pusher) {
  75. thread = std::thread(RunThread, std::ref(system), std::ref(renderer), std::ref(context),
  76. std::ref(dma_pusher), std::ref(state), std::ref(cdma_pusher));
  77. }
  78. void ThreadManager::SubmitList(Tegra::CommandList&& entries) {
  79. PushCommand(SubmitListCommand(std::move(entries)));
  80. }
  81. void ThreadManager::SubmitCommandBuffer(Tegra::ChCommandHeaderList&& entries) {
  82. PushCommand(SubmitChCommandEntries(std::move(entries)));
  83. }
  84. void ThreadManager::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) {
  85. PushCommand(SwapBuffersCommand(framebuffer ? std::make_optional(*framebuffer) : std::nullopt));
  86. }
  87. void ThreadManager::FlushRegion(VAddr addr, u64 size) {
  88. if (!is_async) {
  89. // Always flush with synchronous GPU mode
  90. PushCommand(FlushRegionCommand(addr, size));
  91. return;
  92. }
  93. // Asynchronous GPU mode
  94. switch (Settings::values.gpu_accuracy.GetValue()) {
  95. case Settings::GPUAccuracy::Normal:
  96. PushCommand(FlushRegionCommand(addr, size));
  97. break;
  98. case Settings::GPUAccuracy::High:
  99. // TODO(bunnei): Is this right? Preserving existing behavior for now
  100. break;
  101. case Settings::GPUAccuracy::Extreme: {
  102. auto& gpu = system.GPU();
  103. u64 fence = gpu.RequestFlush(addr, size);
  104. PushCommand(GPUTickCommand());
  105. while (fence > gpu.CurrentFlushRequestFence()) {
  106. }
  107. break;
  108. }
  109. default:
  110. UNIMPLEMENTED_MSG("Unsupported gpu_accuracy {}", Settings::values.gpu_accuracy.GetValue());
  111. }
  112. }
  113. void ThreadManager::InvalidateRegion(VAddr addr, u64 size) {
  114. system.Renderer().Rasterizer().OnCPUWrite(addr, size);
  115. }
  116. void ThreadManager::FlushAndInvalidateRegion(VAddr addr, u64 size) {
  117. // Skip flush on asynch mode, as FlushAndInvalidateRegion is not used for anything too important
  118. system.Renderer().Rasterizer().OnCPUWrite(addr, size);
  119. }
  120. void ThreadManager::WaitIdle() const {
  121. while (state.last_fence > state.signaled_fence.load(std::memory_order_relaxed) &&
  122. system.IsPoweredOn()) {
  123. }
  124. }
  125. void ThreadManager::OnCommandListEnd() {
  126. PushCommand(OnCommandListEndCommand());
  127. }
  128. u64 ThreadManager::PushCommand(CommandData&& command_data) {
  129. const u64 fence{++state.last_fence};
  130. state.queue.Push(CommandDataContainer(std::move(command_data), fence));
  131. if (!is_async) {
  132. // In synchronous GPU mode, block the caller until the command has executed
  133. WaitIdle();
  134. }
  135. return fence;
  136. }
  137. } // namespace VideoCommon::GPUThread