gpu.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <array>
  4. #include <atomic>
  5. #include <chrono>
  6. #include <condition_variable>
  7. #include <list>
  8. #include <memory>
  9. #include "common/assert.h"
  10. #include "common/microprofile.h"
  11. #include "common/settings.h"
  12. #include "core/core.h"
  13. #include "core/core_timing.h"
  14. #include "core/frontend/emu_window.h"
  15. #include "core/frontend/graphics_context.h"
  16. #include "core/hle/service/nvdrv/nvdata.h"
  17. #include "core/perf_stats.h"
  18. #include "video_core/cdma_pusher.h"
  19. #include "video_core/control/channel_state.h"
  20. #include "video_core/control/scheduler.h"
  21. #include "video_core/dma_pusher.h"
  22. #include "video_core/engines/fermi_2d.h"
  23. #include "video_core/engines/kepler_compute.h"
  24. #include "video_core/engines/kepler_memory.h"
  25. #include "video_core/engines/maxwell_3d.h"
  26. #include "video_core/engines/maxwell_dma.h"
  27. #include "video_core/gpu.h"
  28. #include "video_core/gpu_thread.h"
  29. #include "video_core/host1x/host1x.h"
  30. #include "video_core/host1x/syncpoint_manager.h"
  31. #include "video_core/memory_manager.h"
  32. #include "video_core/renderer_base.h"
  33. #include "video_core/shader_notify.h"
  34. namespace Tegra {
  35. struct GPU::Impl {
  36. explicit Impl(GPU& gpu_, Core::System& system_, bool is_async_, bool use_nvdec_)
  37. : gpu{gpu_}, system{system_}, host1x{system.Host1x()}, use_nvdec{use_nvdec_},
  38. shader_notify{std::make_unique<VideoCore::ShaderNotify>()}, is_async{is_async_},
  39. gpu_thread{system_, is_async_}, scheduler{std::make_unique<Control::Scheduler>(gpu)} {
  40. Initialize();
  41. }
  42. ~Impl() = default;
  43. void Initialize() {
  44. // Initialize the GPU memory manager
  45. memory_manager = std::make_unique<Tegra::MemoryManager>(system);
  46. // Initialize the command buffer
  47. command_buffer.reserve(COMMAND_BUFFER_SIZE);
  48. // Initialize the fence manager
  49. fence_manager = std::make_unique<FenceManager>();
  50. }
  51. std::shared_ptr<Control::ChannelState> CreateChannel(s32 channel_id) {
  52. auto channel_state = std::make_shared<Tegra::Control::ChannelState>(channel_id);
  53. channels.emplace(channel_id, channel_state);
  54. scheduler->DeclareChannel(channel_state);
  55. return channel_state;
  56. }
  57. void BindChannel(s32 channel_id) {
  58. if (bound_channel == channel_id) {
  59. return;
  60. }
  61. auto it = channels.find(channel_id);
  62. ASSERT(it != channels.end());
  63. bound_channel = channel_id;
  64. current_channel = it->second.get();
  65. rasterizer->BindChannel(*current_channel);
  66. }
  67. std::shared_ptr<Control::ChannelState> AllocateChannel() {
  68. return CreateChannel(new_channel_id++);
  69. }
  70. void InitChannel(Control::ChannelState& to_init, u64 program_id) {
  71. to_init.Init(system, gpu, program_id);
  72. to_init.BindRasterizer(rasterizer);
  73. rasterizer->InitializeChannel(to_init);
  74. }
  75. void InitAddressSpace(Tegra::MemoryManager& memory_manager) {
  76. memory_manager.BindRasterizer(rasterizer);
  77. }
  78. void ReleaseChannel(Control::ChannelState& to_release) {
  79. UNIMPLEMENTED();
  80. }
  81. /// Binds a renderer to the GPU.
  82. void BindRenderer(std::unique_ptr<VideoCore::RendererBase> renderer_) {
  83. renderer = std::move(renderer_);
  84. rasterizer = renderer->ReadRasterizer();
  85. host1x.MemoryManager().BindInterface(rasterizer);
  86. host1x.GMMU().BindRasterizer(rasterizer);
  87. }
  88. /// Flush all current written commands into the host GPU for execution.
  89. void FlushCommands() {
  90. if (!command_buffer.empty()) {
  91. rasterizer->ExecuteCommands(command_buffer);
  92. command_buffer.clear();
  93. }
  94. }
  95. /// Synchronizes CPU writes with Host GPU memory.
  96. void InvalidateGPUCache() {
  97. rasterizer->InvalidateGPUCache();
  98. }
  99. /// Signal the ending of command list.
  100. void OnCommandListEnd() {
  101. rasterizer->ReleaseFences(false);
  102. Settings::UpdateGPUAccuracy();
  103. }
  104. /// Request a host GPU memory flush from the CPU.
  105. u64 RequestSyncOperation(std::function<void()>&& action) {
  106. std::unique_lock lck{sync_request_mutex};
  107. const u64 fence = ++last_sync_fence;
  108. sync_requests.emplace_back(std::move(action), fence);
  109. return fence;
  110. }
  111. /// Obtains current flush request fence id.
  112. [[nodiscard]] u64 CurrentSyncRequestFence() const {
  113. return current_sync_fence.load(std::memory_order_relaxed);
  114. }
  115. void WaitForSyncOperation(const u64 fence) {
  116. std::unique_lock lck{sync_request_mutex};
  117. sync_request_cv.wait(lck, [this, fence] { return CurrentSyncRequestFence() >= fence; });
  118. }
  119. /// Tick pending requests within the GPU.
  120. void TickWork() {
  121. std::unique_lock lck{sync_request_mutex};
  122. while (!sync_requests.empty()) {
  123. auto& request = sync_requests.front();
  124. sync_request_mutex.unlock();
  125. request.first();
  126. current_sync_fence.fetch_add(1, std::memory_order_release);
  127. sync_request_mutex.lock();
  128. sync_requests.pop_front();
  129. sync_request_cv.notify_all();
  130. }
  131. }
  132. /// Returns a reference to the Maxwell3D GPU engine.
  133. [[nodiscard]] Engines::Maxwell3D& Maxwell3D() {
  134. ASSERT(current_channel);
  135. return *current_channel->maxwell_3d;
  136. }
  137. /// Returns a const reference to the Maxwell3D GPU engine.
  138. [[nodiscard]] const Engines::Maxwell3D& Maxwell3D() const {
  139. ASSERT(current_channel);
  140. return *current_channel->maxwell_3d;
  141. }
  142. /// Returns a reference to the KeplerCompute GPU engine.
  143. [[nodiscard]] Engines::KeplerCompute& KeplerCompute() {
  144. ASSERT(current_channel);
  145. return *current_channel->kepler_compute;
  146. }
  147. /// Returns a reference to the KeplerCompute GPU engine.
  148. [[nodiscard]] const Engines::KeplerCompute& KeplerCompute() const {
  149. ASSERT(current_channel);
  150. return *current_channel->kepler_compute;
  151. }
  152. /// Returns a reference to the GPU DMA pusher.
  153. [[nodiscard]] Tegra::DmaPusher& DmaPusher() {
  154. ASSERT(current_channel);
  155. return *current_channel->dma_pusher;
  156. }
  157. /// Returns a const reference to the GPU DMA pusher.
  158. [[nodiscard]] const Tegra::DmaPusher& DmaPusher() const {
  159. ASSERT(current_channel);
  160. return *current_channel->dma_pusher;
  161. }
  162. /// Returns a reference to the underlying renderer.
  163. [[nodiscard]] VideoCore::RendererBase& Renderer() {
  164. return *renderer;
  165. }
  166. /// Returns a const reference to the underlying renderer.
  167. [[nodiscard]] const VideoCore::RendererBase& Renderer() const {
  168. return *renderer;
  169. }
  170. /// Returns a reference to the shader notifier.
  171. [[nodiscard]] VideoCore::ShaderNotify& ShaderNotify() {
  172. return *shader_notify;
  173. }
  174. /// Returns a const reference to the shader notifier.
  175. [[nodiscard]] const VideoCore::ShaderNotify& ShaderNotify() const {
  176. return *shader_notify;
  177. }
  178. [[nodiscard]] u64 GetTicks() const {
  179. u64 gpu_tick = system.CoreTiming().GetGPUTicks();
  180. if (Settings::values.use_fast_gpu_time.GetValue()) {
  181. gpu_tick /= 256;
  182. }
  183. return gpu_tick;
  184. }
  185. [[nodiscard]] bool IsAsync() const {
  186. return is_async;
  187. }
  188. [[nodiscard]] bool UseNvdec() const {
  189. return use_nvdec;
  190. }
  191. void RendererFrameEndNotify() {
  192. system.GetPerfStats().EndGameFrame();
  193. }
  194. /// Performs any additional setup necessary in order to begin GPU emulation.
  195. /// This can be used to launch any necessary threads and register any necessary
  196. /// core timing events.
  197. void Start() {
  198. gpu_thread.StartThread(*renderer, renderer->Context(), *scheduler);
  199. }
  200. void NotifyShutdown() {
  201. std::unique_lock lk{sync_mutex};
  202. shutting_down.store(true, std::memory_order::relaxed);
  203. sync_cv.notify_all();
  204. }
  205. /// Obtain the CPU Context
  206. void ObtainContext() {
  207. if (!cpu_context) {
  208. cpu_context = renderer->GetRenderWindow().CreateSharedContext();
  209. }
  210. cpu_context->MakeCurrent();
  211. }
  212. /// Release the CPU Context
  213. void ReleaseContext() {
  214. cpu_context->DoneCurrent();
  215. }
  216. /// Push GPU command entries to be processed
  217. void PushGPUEntries(s32 channel, Tegra::CommandList&& entries) {
  218. gpu_thread.SubmitList(channel, std::move(entries));
  219. }
  220. /// Notify rasterizer that any caches of the specified region should be flushed to Switch memory
  221. void FlushRegion(DAddr addr, u64 size) {
  222. rasterizer->FlushRegion(addr, size);
  223. }
  224. VideoCore::RasterizerDownloadArea OnCPURead(DAddr addr, u64 size) {
  225. auto raster_area = rasterizer->GetFlushArea(addr, size);
  226. if (raster_area.preemtive) {
  227. return raster_area;
  228. }
  229. raster_area.preemtive = true;
  230. const u64 fence = RequestSyncOperation([this, &raster_area]() {
  231. rasterizer->FlushRegion(raster_area.start_address,
  232. raster_area.end_address - raster_area.start_address);
  233. });
  234. gpu_thread.TickGPU();
  235. WaitForSyncOperation(fence);
  236. return raster_area;
  237. }
  238. /// Notify rasterizer that any caches of the specified region should be invalidated
  239. void InvalidateRegion(DAddr addr, u64 size) {
  240. rasterizer->InvalidateRegion(addr, size);
  241. }
  242. bool OnCPUWrite(DAddr addr, u64 size) {
  243. return rasterizer->OnCPUWrite(addr, size);
  244. }
  245. /// Notify rasterizer that any caches of the specified region should be flushed and invalidated
  246. void FlushAndInvalidateRegion(DAddr addr, u64 size) {
  247. rasterizer->FlushAndInvalidateRegion(addr, size);
  248. }
  249. GPU& gpu;
  250. Core::System& system;
  251. Host1x::Host1x& host1x;
  252. std::unique_ptr<VideoCore::RendererBase> renderer;
  253. VideoCore::RasterizerInterface* rasterizer = nullptr;
  254. const bool use_nvdec;
  255. s32 new_channel_id{1};
  256. /// Shader build notifier
  257. std::unique_ptr<VideoCore::ShaderNotify> shader_notify;
  258. /// When true, we are about to shut down emulation session, so terminate outstanding tasks
  259. std::atomic_bool shutting_down{};
  260. std::mutex sync_mutex;
  261. std::mutex device_mutex;
  262. std::condition_variable sync_cv;
  263. std::list<std::pair<std::function<void()>, u64>> sync_requests;
  264. std::atomic<u64> current_sync_fence{};
  265. u64 last_sync_fence{};
  266. std::mutex sync_request_mutex;
  267. std::condition_variable sync_request_cv;
  268. const bool is_async;
  269. VideoCommon::GPUThread::ThreadManager gpu_thread;
  270. std::unique_ptr<Core::Frontend::GraphicsContext> cpu_context;
  271. std::unique_ptr<Tegra::Control::Scheduler> scheduler;
  272. std::unordered_map<s32, std::shared_ptr<Tegra::Control::ChannelState>> channels;
  273. Tegra::Control::ChannelState* current_channel;
  274. s32 bound_channel{-1};
  275. std::unique_ptr<Tegra::MemoryManager> memory_manager;
  276. std::vector<u32> command_buffer;
  277. std::unique_ptr<FenceManager> fence_manager;
  278. static constexpr size_t COMMAND_BUFFER_SIZE = 4 * 1024 * 1024;
  279. };
  280. // ... (rest of the implementation remains the same)
  281. } // namespace Tegra