gpu_thread.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. while (state.queue.Empty())
  21. ;
  22. // If emulation was stopped during disk shader loading, abort before trying to acquire context
  23. if (!state.is_running) {
  24. return;
  25. }
  26. Core::Frontend::ScopeAcquireWindowContext acquire_context{renderer.GetRenderWindow()};
  27. CommandDataContainer next;
  28. while (state.is_running) {
  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(data->framebuffer ? &*data->framebuffer : nullptr);
  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.store(next.fence);
  46. }
  47. }
  48. }
  49. ThreadManager::ThreadManager(Core::System& system) : system{system} {}
  50. ThreadManager::~ThreadManager() {
  51. if (!thread.joinable()) {
  52. return;
  53. }
  54. // Notify GPU thread that a shutdown is pending
  55. PushCommand(EndProcessingCommand());
  56. thread.join();
  57. }
  58. void ThreadManager::StartThread(VideoCore::RendererBase& renderer, Tegra::DmaPusher& dma_pusher) {
  59. thread = std::thread{RunThread, std::ref(renderer), std::ref(dma_pusher), std::ref(state)};
  60. synchronization_event = system.CoreTiming().RegisterEvent(
  61. "GPUThreadSynch", [this](u64 fence, s64) { state.WaitForSynchronization(fence); });
  62. }
  63. void ThreadManager::SubmitList(Tegra::CommandList&& entries) {
  64. const u64 fence{PushCommand(SubmitListCommand(std::move(entries)))};
  65. const s64 synchronization_ticks{Core::Timing::usToCycles(std::chrono::microseconds{9000})};
  66. system.CoreTiming().ScheduleEvent(synchronization_ticks, synchronization_event, fence);
  67. }
  68. void ThreadManager::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) {
  69. PushCommand(SwapBuffersCommand(framebuffer ? *framebuffer
  70. : std::optional<const Tegra::FramebufferConfig>{}));
  71. }
  72. void ThreadManager::FlushRegion(CacheAddr addr, u64 size) {
  73. PushCommand(FlushRegionCommand(addr, size));
  74. }
  75. void ThreadManager::InvalidateRegion(CacheAddr addr, u64 size) {
  76. system.Renderer().Rasterizer().InvalidateRegion(addr, size);
  77. }
  78. void ThreadManager::FlushAndInvalidateRegion(CacheAddr addr, u64 size) {
  79. // Skip flush on asynch mode, as FlushAndInvalidateRegion is not used for anything too important
  80. InvalidateRegion(addr, size);
  81. }
  82. u64 ThreadManager::PushCommand(CommandData&& command_data) {
  83. const u64 fence{++state.last_fence};
  84. state.queue.Push(CommandDataContainer(std::move(command_data), fence));
  85. return fence;
  86. }
  87. MICROPROFILE_DEFINE(GPU_wait, "GPU", "Wait for the GPU", MP_RGB(128, 128, 192));
  88. void SynchState::WaitForSynchronization(u64 fence) {
  89. while (signaled_fence.load() < fence)
  90. ;
  91. }
  92. } // namespace VideoCommon::GPUThread