| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
- // SPDX-License-Identifier: GPL-2.0-or-later
- #include <array>
- #include <atomic>
- #include <chrono>
- #include <condition_variable>
- #include <list>
- #include <memory>
- #include "common/assert.h"
- #include "common/microprofile.h"
- #include "common/settings.h"
- #include "core/core.h"
- #include "core/core_timing.h"
- #include "core/frontend/emu_window.h"
- #include "core/frontend/graphics_context.h"
- #include "core/hle/service/nvdrv/nvdata.h"
- #include "core/perf_stats.h"
- #include "video_core/cdma_pusher.h"
- #include "video_core/control/channel_state.h"
- #include "video_core/control/scheduler.h"
- #include "video_core/dma_pusher.h"
- #include "video_core/engines/fermi_2d.h"
- #include "video_core/engines/kepler_compute.h"
- #include "video_core/engines/kepler_memory.h"
- #include "video_core/engines/maxwell_3d.h"
- #include "video_core/engines/maxwell_dma.h"
- #include "video_core/gpu.h"
- #include "video_core/gpu_thread.h"
- #include "video_core/host1x/host1x.h"
- #include "video_core/host1x/syncpoint_manager.h"
- #include "video_core/memory_manager.h"
- #include "video_core/renderer_base.h"
- #include "video_core/shader_notify.h"
- namespace Tegra {
- struct GPU::Impl {
- explicit Impl(GPU& gpu_, Core::System& system_, bool is_async_, bool use_nvdec_)
- : gpu{gpu_}, system{system_}, host1x{system.Host1x()}, use_nvdec{use_nvdec_},
- shader_notify{std::make_unique<VideoCore::ShaderNotify>()}, is_async{is_async_},
- gpu_thread{system_, is_async_}, scheduler{std::make_unique<Control::Scheduler>(gpu)} {
- Initialize();
- }
- ~Impl() = default;
- void Initialize() {
- // Initialize the GPU memory manager
- memory_manager = std::make_unique<Tegra::MemoryManager>(system);
-
- // Initialize the command buffer
- command_buffer.reserve(COMMAND_BUFFER_SIZE);
- // Initialize the fence manager
- fence_manager = std::make_unique<FenceManager>();
- }
- std::shared_ptr<Control::ChannelState> CreateChannel(s32 channel_id) {
- auto channel_state = std::make_shared<Tegra::Control::ChannelState>(channel_id);
- channels.emplace(channel_id, channel_state);
- scheduler->DeclareChannel(channel_state);
- return channel_state;
- }
- void BindChannel(s32 channel_id) {
- if (bound_channel == channel_id) {
- return;
- }
- auto it = channels.find(channel_id);
- ASSERT(it != channels.end());
- bound_channel = channel_id;
- current_channel = it->second.get();
- rasterizer->BindChannel(*current_channel);
- }
- std::shared_ptr<Control::ChannelState> AllocateChannel() {
- return CreateChannel(new_channel_id++);
- }
- void InitChannel(Control::ChannelState& to_init, u64 program_id) {
- to_init.Init(system, gpu, program_id);
- to_init.BindRasterizer(rasterizer);
- rasterizer->InitializeChannel(to_init);
- }
- void InitAddressSpace(Tegra::MemoryManager& memory_manager) {
- memory_manager.BindRasterizer(rasterizer);
- }
- void ReleaseChannel(Control::ChannelState& to_release) {
- UNIMPLEMENTED();
- }
- /// Binds a renderer to the GPU.
- void BindRenderer(std::unique_ptr<VideoCore::RendererBase> renderer_) {
- renderer = std::move(renderer_);
- rasterizer = renderer->ReadRasterizer();
- host1x.MemoryManager().BindInterface(rasterizer);
- host1x.GMMU().BindRasterizer(rasterizer);
- }
- /// Flush all current written commands into the host GPU for execution.
- void FlushCommands() {
- if (!command_buffer.empty()) {
- rasterizer->ExecuteCommands(command_buffer);
- command_buffer.clear();
- }
- }
- /// Synchronizes CPU writes with Host GPU memory.
- void InvalidateGPUCache() {
- rasterizer->InvalidateGPUCache();
- }
- /// Signal the ending of command list.
- void OnCommandListEnd() {
- rasterizer->ReleaseFences(false);
- Settings::UpdateGPUAccuracy();
- }
- /// Request a host GPU memory flush from the CPU.
- u64 RequestSyncOperation(std::function<void()>&& action) {
- std::unique_lock lck{sync_request_mutex};
- const u64 fence = ++last_sync_fence;
- sync_requests.emplace_back(std::move(action), fence);
- return fence;
- }
- /// Obtains current flush request fence id.
- [[nodiscard]] u64 CurrentSyncRequestFence() const {
- return current_sync_fence.load(std::memory_order_relaxed);
- }
- void WaitForSyncOperation(const u64 fence) {
- std::unique_lock lck{sync_request_mutex};
- sync_request_cv.wait(lck, [this, fence] { return CurrentSyncRequestFence() >= fence; });
- }
- /// Tick pending requests within the GPU.
- void TickWork() {
- std::unique_lock lck{sync_request_mutex};
- while (!sync_requests.empty()) {
- auto& request = sync_requests.front();
- sync_request_mutex.unlock();
- request.first();
- current_sync_fence.fetch_add(1, std::memory_order_release);
- sync_request_mutex.lock();
- sync_requests.pop_front();
- sync_request_cv.notify_all();
- }
- }
- /// Returns a reference to the Maxwell3D GPU engine.
- [[nodiscard]] Engines::Maxwell3D& Maxwell3D() {
- ASSERT(current_channel);
- return *current_channel->maxwell_3d;
- }
- /// Returns a const reference to the Maxwell3D GPU engine.
- [[nodiscard]] const Engines::Maxwell3D& Maxwell3D() const {
- ASSERT(current_channel);
- return *current_channel->maxwell_3d;
- }
- /// Returns a reference to the KeplerCompute GPU engine.
- [[nodiscard]] Engines::KeplerCompute& KeplerCompute() {
- ASSERT(current_channel);
- return *current_channel->kepler_compute;
- }
- /// Returns a reference to the KeplerCompute GPU engine.
- [[nodiscard]] const Engines::KeplerCompute& KeplerCompute() const {
- ASSERT(current_channel);
- return *current_channel->kepler_compute;
- }
- /// Returns a reference to the GPU DMA pusher.
- [[nodiscard]] Tegra::DmaPusher& DmaPusher() {
- ASSERT(current_channel);
- return *current_channel->dma_pusher;
- }
- /// Returns a const reference to the GPU DMA pusher.
- [[nodiscard]] const Tegra::DmaPusher& DmaPusher() const {
- ASSERT(current_channel);
- return *current_channel->dma_pusher;
- }
- /// Returns a reference to the underlying renderer.
- [[nodiscard]] VideoCore::RendererBase& Renderer() {
- return *renderer;
- }
- /// Returns a const reference to the underlying renderer.
- [[nodiscard]] const VideoCore::RendererBase& Renderer() const {
- return *renderer;
- }
- /// Returns a reference to the shader notifier.
- [[nodiscard]] VideoCore::ShaderNotify& ShaderNotify() {
- return *shader_notify;
- }
- /// Returns a const reference to the shader notifier.
- [[nodiscard]] const VideoCore::ShaderNotify& ShaderNotify() const {
- return *shader_notify;
- }
- [[nodiscard]] u64 GetTicks() const {
- u64 gpu_tick = system.CoreTiming().GetGPUTicks();
- if (Settings::values.use_fast_gpu_time.GetValue()) {
- gpu_tick /= 256;
- }
- return gpu_tick;
- }
- [[nodiscard]] bool IsAsync() const {
- return is_async;
- }
- [[nodiscard]] bool UseNvdec() const {
- return use_nvdec;
- }
- void RendererFrameEndNotify() {
- system.GetPerfStats().EndGameFrame();
- }
- /// Performs any additional setup necessary in order to begin GPU emulation.
- /// This can be used to launch any necessary threads and register any necessary
- /// core timing events.
- void Start() {
- gpu_thread.StartThread(*renderer, renderer->Context(), *scheduler);
- }
- void NotifyShutdown() {
- std::unique_lock lk{sync_mutex};
- shutting_down.store(true, std::memory_order::relaxed);
- sync_cv.notify_all();
- }
- /// Obtain the CPU Context
- void ObtainContext() {
- if (!cpu_context) {
- cpu_context = renderer->GetRenderWindow().CreateSharedContext();
- }
- cpu_context->MakeCurrent();
- }
- /// Release the CPU Context
- void ReleaseContext() {
- cpu_context->DoneCurrent();
- }
- /// Push GPU command entries to be processed
- void PushGPUEntries(s32 channel, Tegra::CommandList&& entries) {
- gpu_thread.SubmitList(channel, std::move(entries));
- }
- /// Notify rasterizer that any caches of the specified region should be flushed to Switch memory
- void FlushRegion(DAddr addr, u64 size) {
- rasterizer->FlushRegion(addr, size);
- }
- VideoCore::RasterizerDownloadArea OnCPURead(DAddr addr, u64 size) {
- auto raster_area = rasterizer->GetFlushArea(addr, size);
- if (raster_area.preemtive) {
- return raster_area;
- }
- raster_area.preemtive = true;
- const u64 fence = RequestSyncOperation([this, &raster_area]() {
- rasterizer->FlushRegion(raster_area.start_address,
- raster_area.end_address - raster_area.start_address);
- });
- gpu_thread.TickGPU();
- WaitForSyncOperation(fence);
- return raster_area;
- }
- /// Notify rasterizer that any caches of the specified region should be invalidated
- void InvalidateRegion(DAddr addr, u64 size) {
- rasterizer->InvalidateRegion(addr, size);
- }
- bool OnCPUWrite(DAddr addr, u64 size) {
- return rasterizer->OnCPUWrite(addr, size);
- }
- /// Notify rasterizer that any caches of the specified region should be flushed and invalidated
- void FlushAndInvalidateRegion(DAddr addr, u64 size) {
- rasterizer->FlushAndInvalidateRegion(addr, size);
- }
- GPU& gpu;
- Core::System& system;
- Host1x::Host1x& host1x;
- std::unique_ptr<VideoCore::RendererBase> renderer;
- VideoCore::RasterizerInterface* rasterizer = nullptr;
- const bool use_nvdec;
- s32 new_channel_id{1};
- /// Shader build notifier
- std::unique_ptr<VideoCore::ShaderNotify> shader_notify;
- /// When true, we are about to shut down emulation session, so terminate outstanding tasks
- std::atomic_bool shutting_down{};
- std::mutex sync_mutex;
- std::mutex device_mutex;
- std::condition_variable sync_cv;
- std::list<std::pair<std::function<void()>, u64>> sync_requests;
- std::atomic<u64> current_sync_fence{};
- u64 last_sync_fence{};
- std::mutex sync_request_mutex;
- std::condition_variable sync_request_cv;
- const bool is_async;
- VideoCommon::GPUThread::ThreadManager gpu_thread;
- std::unique_ptr<Core::Frontend::GraphicsContext> cpu_context;
- std::unique_ptr<Tegra::Control::Scheduler> scheduler;
- std::unordered_map<s32, std::shared_ptr<Tegra::Control::ChannelState>> channels;
- Tegra::Control::ChannelState* current_channel;
- s32 bound_channel{-1};
- std::unique_ptr<Tegra::MemoryManager> memory_manager;
- std::vector<u32> command_buffer;
- std::unique_ptr<FenceManager> fence_manager;
- static constexpr size_t COMMAND_BUFFER_SIZE = 4 * 1024 * 1024;
- };
- // ... (rest of the implementation remains the same)
- } // namespace Tegra
|