From 965ee0d01614b0514e3bf68021c8f64e7f0def84 Mon Sep 17 00:00:00 2001 From: MutantAura Date: Thu, 11 Apr 2024 18:09:27 +0100 Subject: [PATCH] Further de-duplicate NVAPI interface code + GTK --- .../GraphicsDriver/DriverUtilities.cs | 30 +++++++---------- .../GraphicsDriver/NVAPI/Nvapi.cs | 2 +- .../GraphicsDriver/NVDriverHelper.cs | 33 ++++--------------- src/Ryujinx.Gtk3/Program.cs | 6 +++- src/Ryujinx.Gtk3/UI/Windows/SettingsWindow.cs | 3 +- src/Ryujinx/Program.cs | 5 +-- .../UI/ViewModels/SettingsViewModel.cs | 3 +- 7 files changed, 30 insertions(+), 52 deletions(-) diff --git a/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs b/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs index 2d8ee9f99c..fde7801eb8 100644 --- a/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs +++ b/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs @@ -1,3 +1,4 @@ +using Ryujinx.Common.GraphicsDriver.NVAPI; using Ryujinx.Common.Logging; using System; @@ -5,30 +6,21 @@ namespace Ryujinx.Common.GraphicsDriver { public static class DriverUtilities { - public static void ToggleOGLThreading(bool enabled) + public static void ToggleNvDriverSetting(NvapiSettingId id, bool enabled) { - Environment.SetEnvironmentVariable("mesa_glthread", enabled.ToString().ToLower()); - Environment.SetEnvironmentVariable("__GL_THREADED_OPTIMIZATIONS", enabled ? "1" : "0"); - - try + try { - NVDriverHelper.SetThreadedOptimization(enabled); + if (id == NvapiSettingId.OglThreadControlId) + { + Environment.SetEnvironmentVariable("mesa_glthread", enabled.ToString().ToLower()); + Environment.SetEnvironmentVariable("__GL_THREADED_OPTIMIZATIONS", enabled ? "1" : "0"); + } + + NVDriverHelper.SetControlOption(id, enabled); } catch { - Logger.Warning?.Print(LogClass.Application, "Failed to set threaded optimizations. NVAPI may be unavailable."); - } - } - - public static void ToggleDxgiSwapchain(bool enabled) - { - try - { - NVDriverHelper.SetDxgiSwapchain(enabled); - } - catch - { - Logger.Warning?.Print(LogClass.Application, "Failed to set Vulkan/OpenGL present method. NVAPI may be unavailable."); + Logger.Warning?.Print(LogClass.Application, "Failed to set NVIDIA driver settings. NVAPI may be unavailable."); } } } diff --git a/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs b/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs index 3450b230b2..8030bec5b7 100644 --- a/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs +++ b/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs @@ -1,6 +1,6 @@ namespace Ryujinx.Common.GraphicsDriver.NVAPI { - enum NvapiSettingId : uint + public enum NvapiSettingId : uint { OglThreadControlId = 0x20C1221E, OglCplPreferDxPresentId = 0x20D690F8, diff --git a/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs b/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs index a21770bafc..3a69b5631c 100644 --- a/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs +++ b/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs @@ -128,44 +128,23 @@ namespace Ryujinx.Common.GraphicsDriver return (uint)Unsafe.SizeOf() | version << 16; } - public static void SetThreadedOptimization(bool enabled) + public static void SetControlOption(NvapiSettingId id, bool enabled) { Initialize(); SetupNvProfile(); - uint targetValue = (uint)(enabled ? OglThreadControl.OglThreadControlEnable : OglThreadControl.OglThreadControlDisable); - - NvdrsSetting setting = new() + uint targetValue = id switch { - Version = MakeVersion(1), - SettingId = NvapiSettingId.OglThreadControlId, - SettingType = NvdrsSettingType.NvdrsDwordType, - SettingLocation = NvdrsSettingLocation.NvdrsCurrentProfileLocation, - IsCurrentPredefined = 0, - IsPredefinedValid = 0, - CurrentValue = targetValue, - PredefinedValue = targetValue, + NvapiSettingId.OglThreadControlId => (uint)(enabled ? OglThreadControl.OglThreadControlEnable : OglThreadControl.OglThreadControlDisable), + NvapiSettingId.OglCplPreferDxPresentId => (uint)(enabled ? OglCplDxPresent.OglCplPreferDxPresentEnable : OglCplDxPresent.OglCplPreferDxPresentDisable), + _ => throw new ArgumentException(), }; - Check(NvAPI_DRS_SetSetting(_handle, _profileHandle, ref setting)); - Check(NvAPI_DRS_SaveSettings(_handle)); - - NvAPI_DRS_DestroySession(_handle); - } - - public static void SetDxgiSwapchain(bool enabled) - { - Initialize(); - - SetupNvProfile(); - - uint targetValue = (uint)(enabled ? OglCplDxPresent.OglCplPreferDxPresentEnable : OglCplDxPresent.OglCplPreferDxPresentDisable); - NvdrsSetting setting = new() { Version = MakeVersion(1), - SettingId = NvapiSettingId.OglCplPreferDxPresentId, + SettingId = id, SettingType = NvdrsSettingType.NvdrsDwordType, SettingLocation = NvdrsSettingLocation.NvdrsCurrentProfileLocation, IsCurrentPredefined = 0, diff --git a/src/Ryujinx.Gtk3/Program.cs b/src/Ryujinx.Gtk3/Program.cs index 1845c512e4..4dd170a3da 100644 --- a/src/Ryujinx.Gtk3/Program.cs +++ b/src/Ryujinx.Gtk3/Program.cs @@ -2,6 +2,7 @@ using Gtk; using Ryujinx.Common; using Ryujinx.Common.Configuration; using Ryujinx.Common.GraphicsDriver; +using Ryujinx.Common.GraphicsDriver.NVAPI; using Ryujinx.Common.Logging; using Ryujinx.Common.SystemInterop; using Ryujinx.Modules; @@ -235,7 +236,10 @@ namespace Ryujinx // Enable OGL multithreading on the driver, when available. BackendThreading threadingMode = ConfigurationState.Instance.Graphics.BackendThreading; - DriverUtilities.ToggleOGLThreading(threadingMode == BackendThreading.Off); + DriverUtilities.ToggleNvDriverSetting(NvapiSettingId.OglThreadControlId, threadingMode == BackendThreading.Off); + + // Enable DXGI present mode on the driver, when available. + DriverUtilities.ToggleNvDriverSetting(NvapiSettingId.OglCplPreferDxPresentId, true); // Initialize Gtk. Application.Init(); diff --git a/src/Ryujinx.Gtk3/UI/Windows/SettingsWindow.cs b/src/Ryujinx.Gtk3/UI/Windows/SettingsWindow.cs index dc467c0f21..876c6b9048 100644 --- a/src/Ryujinx.Gtk3/UI/Windows/SettingsWindow.cs +++ b/src/Ryujinx.Gtk3/UI/Windows/SettingsWindow.cs @@ -7,6 +7,7 @@ using Ryujinx.Common.Configuration; using Ryujinx.Common.Configuration.Hid; using Ryujinx.Common.Configuration.Multiplayer; using Ryujinx.Common.GraphicsDriver; +using Ryujinx.Common.GraphicsDriver.NVAPI; using Ryujinx.HLE.FileSystem; using Ryujinx.HLE.HOS.Services.Time.TimeZone; using Ryujinx.UI.Common.Configuration; @@ -610,7 +611,7 @@ namespace Ryujinx.UI.Windows BackendThreading backendThreading = Enum.Parse(_galThreading.ActiveId); if (ConfigurationState.Instance.Graphics.BackendThreading != backendThreading) { - DriverUtilities.ToggleOGLThreading(backendThreading == BackendThreading.Off); + DriverUtilities.ToggleNvDriverSetting(NvapiSettingId.OglThreadControlId, backendThreading == BackendThreading.Off); } ConfigurationState.Instance.Logger.EnableError.Value = _errorLogToggle.Active; diff --git a/src/Ryujinx/Program.cs b/src/Ryujinx/Program.cs index 45e2a0c40d..3ba2e87d4e 100644 --- a/src/Ryujinx/Program.cs +++ b/src/Ryujinx/Program.cs @@ -5,6 +5,7 @@ using Ryujinx.Ava.UI.Windows; using Ryujinx.Common; using Ryujinx.Common.Configuration; using Ryujinx.Common.GraphicsDriver; +using Ryujinx.Common.GraphicsDriver.NVAPI; using Ryujinx.Common.Logging; using Ryujinx.Common.SystemInterop; using Ryujinx.Modules; @@ -112,10 +113,10 @@ namespace Ryujinx.Ava PrintSystemInfo(); // Enable OGL multithreading on the driver, when available. - DriverUtilities.ToggleOGLThreading(ConfigurationState.Instance.Graphics.BackendThreading == BackendThreading.Off); + DriverUtilities.ToggleNvDriverSetting(NvapiSettingId.OglThreadControlId, ConfigurationState.Instance.Graphics.BackendThreading == BackendThreading.Off); // Enable Dxgi present on the driver, when available. - DriverUtilities.ToggleDxgiSwapchain(true); + DriverUtilities.ToggleNvDriverSetting(NvapiSettingId.OglCplPreferDxPresentId, true); // Check if keys exists. if (!File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys"))) diff --git a/src/Ryujinx/UI/ViewModels/SettingsViewModel.cs b/src/Ryujinx/UI/ViewModels/SettingsViewModel.cs index 6074a5fdb3..8574837723 100644 --- a/src/Ryujinx/UI/ViewModels/SettingsViewModel.cs +++ b/src/Ryujinx/UI/ViewModels/SettingsViewModel.cs @@ -12,6 +12,7 @@ using Ryujinx.Ava.UI.Windows; using Ryujinx.Common.Configuration; using Ryujinx.Common.Configuration.Multiplayer; using Ryujinx.Common.GraphicsDriver; +using Ryujinx.Common.GraphicsDriver.NVAPI; using Ryujinx.Common.Logging; using Ryujinx.Graphics.Vulkan; using Ryujinx.HLE.FileSystem; @@ -529,7 +530,7 @@ namespace Ryujinx.Ava.UI.ViewModels if (ConfigurationState.Instance.Graphics.BackendThreading != (BackendThreading)GraphicsBackendMultithreadingIndex) { - DriverUtilities.ToggleOGLThreading(GraphicsBackendMultithreadingIndex == (int)BackendThreading.Off); + DriverUtilities.ToggleNvDriverSetting(NvapiSettingId.OglThreadControlId, GraphicsBackendMultithreadingIndex == (int)BackendThreading.Off); } config.Graphics.BackendThreading.Value = (BackendThreading)GraphicsBackendMultithreadingIndex;