VERY experimental Nintendo Switch 2 emulator
C++
hle: service: nvdrv: Implement SyncpointManager, to manage syncpoints.
bunnei317c2ac88442
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txtindex e0f207f3ee..9a983e81d0 100644--- a/src/core/CMakeLists.txt+++ b/src/core/CMakeLists.txt@@ -454,6 +454,8 @@ add_library(core STATIChle/service/nvdrv/nvdrv.hhle/service/nvdrv/nvmemp.cpphle/service/nvdrv/nvmemp.h+ hle/service/nvdrv/syncpoint_manager.cpp+ hle/service/nvdrv/syncpoint_manager.hhle/service/nvflinger/buffer_queue.cpphle/service/nvflinger/buffer_queue.hhle/service/nvflinger/nvflinger.cppdiff --git a/src/core/hle/service/nvdrv/nvdrv.cpp b/src/core/hle/service/nvdrv/nvdrv.cppindex 803c1a9848..e6a205c8e5 100644--- a/src/core/hle/service/nvdrv/nvdrv.cpp+++ b/src/core/hle/service/nvdrv/nvdrv.cpp@@ -36,7 +36,7 @@ void InstallInterfaces(SM::ServiceManager& service_manager, NVFlinger::NVFlingernvflinger.SetNVDrvInstance(module_);}-Module::Module(Core::System& system) {+Module::Module(Core::System& system) : syncpoint_manager{system.GPU()} {auto& kernel = system.Kernel();for (u32 i = 0; i < MaxNvEvents; i++) {std::string event_label = fmt::format("NVDRV::NvEvent_{}", i);diff --git a/src/core/hle/service/nvdrv/syncpoint_manager.cpp b/src/core/hle/service/nvdrv/syncpoint_manager.cppnew file mode 100644index 0000000000..0151a03b78--- /dev/null+++ b/src/core/hle/service/nvdrv/syncpoint_manager.cpp@@ -0,0 +1,39 @@+// Copyright 2020 yuzu emulator team+// Licensed under GPLv2 or any later version+// Refer to the license.txt file included.++#include "common/assert.h"+#include "core/hle/service/nvdrv/syncpoint_manager.h"+#include "video_core/gpu.h"++namespace Service::Nvidia {++SyncpointManager::SyncpointManager(Tegra::GPU& gpu) : gpu{gpu} {}++SyncpointManager::~SyncpointManager() = default;++u32 SyncpointManager::RefreshSyncpoint(u32 syncpoint_id) {+ syncpoints[syncpoint_id].min = gpu.GetSyncpointValue(syncpoint_id);+ return GetSyncpointMin(syncpoint_id);+}++u32 SyncpointManager::AllocateSyncpoint() {+ for (u32 syncpoint_id = 1; syncpoint_id < MaxSyncPoints; syncpoint_id++) {+ if (!syncpoints[syncpoint_id].is_allocated) {+ syncpoints[syncpoint_id].is_allocated = true;+ return syncpoint_id;+ }+ }+ UNREACHABLE_MSG("No more available syncpoints!");+ return {};+}++u32 SyncpointManager::IncreaseSyncpoint(u32 syncpoint_id, u32 value) {+ for (u32 index = 0; index < value; ++index) {+ syncpoints[syncpoint_id].max.fetch_add(1, std::memory_order_relaxed);+ }++ return GetSyncpointMax(syncpoint_id);+}++} // namespace Service::Nvidiadiff --git a/src/core/hle/service/nvdrv/syncpoint_manager.h b/src/core/hle/service/nvdrv/syncpoint_manager.hnew file mode 100644index 0000000000..4168b6c7ed--- /dev/null+++ b/src/core/hle/service/nvdrv/syncpoint_manager.h@@ -0,0 +1,85 @@+// Copyright 2020 yuzu emulator team+// Licensed under GPLv2 or any later version+// Refer to the license.txt file included.++#pragma once++#include <array>+#include <atomic>++#include "common/common_types.h"+#include "core/hle/service/nvdrv/nvdata.h"++namespace Tegra {+class GPU;+}++namespace Service::Nvidia {++class SyncpointManager final {+public:+ explicit SyncpointManager(Tegra::GPU& gpu);+ ~SyncpointManager();++ /**+ * Returns true if the specified syncpoint is expired for the given value.+ * @param syncpoint_id Syncpoint ID to check.+ * @param value Value to check against the specified syncpoint.+ * @returns True if the specified syncpoint is expired for the given value, otherwise False.+ */+ bool IsSyncpointExpired(u32 syncpoint_id, u32 value) const {+ return (GetSyncpointMax(syncpoint_id) - value) >= (GetSyncpointMin(syncpoint_id) - value);+ }++ /**+ * Gets the lower bound for the specified syncpoint.+ * @param syncpoint_id Syncpoint ID to get the lower bound for.+ * @returns The lower bound for the specified syncpoint.+ */+ u32 GetSyncpointMin(u32 syncpoint_id) const {+ return syncpoints[syncpoint_id].min.load(std::memory_order_relaxed);+ }++ /**+ * Gets the uper bound for the specified syncpoint.+ * @param syncpoint_id Syncpoint ID to get the upper bound for.+ * @returns The upper bound for the specified syncpoint.+ */+ u32 GetSyncpointMax(u32 syncpoint_id) const {+ return syncpoints[syncpoint_id].max.load(std::memory_order_relaxed);+ }++ /**+ * Refreshes the minimum value for the specified syncpoint.+ * @param syncpoint_id Syncpoint ID to be refreshed.+ * @returns The new syncpoint minimum value.+ */+ u32 RefreshSyncpoint(u32 syncpoint_id);++ /**+ * Allocates a new syncoint.+ * @returns The syncpoint ID for the newly allocated syncpoint.+ */+ u32 AllocateSyncpoint();++ /**+ * Increases the maximum value for the specified syncpoint.+ * @param syncpoint_id Syncpoint ID to be increased.+ * @param value Value to increase the specified syncpoint by.+ * @returns The new syncpoint maximum value.+ */+ u32 IncreaseSyncpoint(u32 syncpoint_id, u32 value);++private:+ struct Syncpoint {+ std::atomic<u32> min;+ std::atomic<u32> max;+ std::atomic<bool> is_allocated;+ };++ std::array<Syncpoint, MaxSyncPoints> syncpoints{};++ Tegra::GPU& gpu;+};++} // namespace Service::Nvidia