gpu_thread.cpp 3.6 KB

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