From 2d14384a7dfe8659c4ed387aad2439d4a897355e Mon Sep 17 00:00:00 2001 From: MutantAura Date: Wed, 10 Apr 2024 22:01:59 +0100 Subject: [PATCH 01/11] Adjust driver helper to more generic NVAPI interface and add DXGI present option. --- .../GraphicsDriver/DriverUtilities.cs | 14 +++- .../GraphicsDriver/NVAPI/Nvapi.cs | 15 +++- .../GraphicsDriver/NVAPI/NvdrsSetting.cs | 2 +- ...eadedOptimization.cs => NVDriverHelper.cs} | 71 ++++++++++++++----- src/Ryujinx/Program.cs | 3 + 5 files changed, 85 insertions(+), 20 deletions(-) rename src/Ryujinx.Common/GraphicsDriver/{NVThreadedOptimization.cs => NVDriverHelper.cs} (79%) diff --git a/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs b/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs index 7fe2a4f024..83bc8d555b 100644 --- a/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs +++ b/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs @@ -11,7 +11,19 @@ namespace Ryujinx.Common.GraphicsDriver try { - NVThreadedOptimization.SetThreadedOptimization(enabled); + NVDriverHelper.SetThreadedOptimization(enabled); + } + catch + { + // NVAPI is not available, or couldn't change the application profile. + } + } + + public static void ToggleDxgiSwapchain(bool enabled) + { + try + { + NVDriverHelper.SetDxgiSwapchain(enabled); } catch { diff --git a/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs b/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs index 985e5e6d3a..71ea62f47a 100644 --- a/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs +++ b/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs @@ -1,11 +1,22 @@ namespace Ryujinx.Common.GraphicsDriver.NVAPI { - enum Nvapi : uint + enum NvapiSettingID : uint { OglThreadControlId = 0x20C1221E, - + OglCplPreferDxPresentId = 0x20D690F8, + } + enum OglThreadControl : uint + { OglThreadControlDefault = 0, OglThreadControlEnable = 1, OglThreadControlDisable = 2, } + + // Only present in driver versions >= 526.47 + enum OglCplDxPresent : uint + { + OglCplPreferDxPresentDisable = 0, + OglCplPreferDxPresentEnable = 1, + OglCplPreferDxPresentDefault = 2, + } } diff --git a/src/Ryujinx.Common/GraphicsDriver/NVAPI/NvdrsSetting.cs b/src/Ryujinx.Common/GraphicsDriver/NVAPI/NvdrsSetting.cs index a36781bafc..9e355c33c9 100644 --- a/src/Ryujinx.Common/GraphicsDriver/NVAPI/NvdrsSetting.cs +++ b/src/Ryujinx.Common/GraphicsDriver/NVAPI/NvdrsSetting.cs @@ -26,7 +26,7 @@ namespace Ryujinx.Common.GraphicsDriver.NVAPI [FieldOffset(0x4)] public NvapiUnicodeString SettingName; [FieldOffset(0x1004)] - public Nvapi SettingId; + public NvapiSettingID SettingId; [FieldOffset(0x1008)] public NvdrsSettingType SettingType; [FieldOffset(0x100C)] diff --git a/src/Ryujinx.Common/GraphicsDriver/NVThreadedOptimization.cs b/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs similarity index 79% rename from src/Ryujinx.Common/GraphicsDriver/NVThreadedOptimization.cs rename to src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs index f7b11783d5..bc28f4ec87 100644 --- a/src/Ryujinx.Common/GraphicsDriver/NVThreadedOptimization.cs +++ b/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs @@ -1,11 +1,13 @@ using Ryujinx.Common.GraphicsDriver.NVAPI; using System; +using System.ComponentModel; +using System.Dynamic; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace Ryujinx.Common.GraphicsDriver { - static partial class NVThreadedOptimization + static partial class NVDriverHelper { private const string ProfileName = "Ryujinx Nvidia Profile"; @@ -49,6 +51,9 @@ namespace Ryujinx.Common.GraphicsDriver private delegate int NvAPI_DRS_DestroySessionDelegate(IntPtr handle); private static NvAPI_DRS_DestroySessionDelegate NvAPI_DRS_DestroySession; + private static IntPtr _handle; + private static nint _profileHandle; + private static bool _initialized; private static void Check(int status) @@ -80,17 +85,8 @@ namespace Ryujinx.Common.GraphicsDriver } } - private static uint MakeVersion(uint version) where T : unmanaged + private static void SetupNvProfile() { - return (uint)Unsafe.SizeOf() | version << 16; - } - - public static void SetThreadedOptimization(bool enabled) - { - Initialize(); - - uint targetValue = (uint)(enabled ? Nvapi.OglThreadControlEnable : Nvapi.OglThreadControlDisable); - Check(NvAPI_Initialize()); Check(NvAPI_DRS_CreateSession(out IntPtr handle)); @@ -98,7 +94,6 @@ namespace Ryujinx.Common.GraphicsDriver Check(NvAPI_DRS_LoadSettings(handle)); // Check if the profile already exists. - int status = NvAPI_DRS_FindProfileByName(handle, new NvapiUnicodeString(ProfileName), out nint profileHandle); if (status != 0) @@ -126,10 +121,27 @@ namespace Ryujinx.Common.GraphicsDriver Check(NvAPI_DRS_CreateApplication(handle, profileHandle, ref application)); } + _handle = handle; + _profileHandle = profileHandle; + } + + private static uint MakeVersion(uint version) where T : unmanaged + { + return (uint)Unsafe.SizeOf() | version << 16; + } + + public static void SetThreadedOptimization(bool enabled) + { + Initialize(); + + SetupNvProfile(); + + uint targetValue = (uint)(enabled ? OglThreadControl.OglThreadControlEnable : OglThreadControl.OglThreadControlDisable); + NvdrsSetting setting = new() { Version = MakeVersion(1), - SettingId = Nvapi.OglThreadControlId, + SettingId = NvapiSettingID.OglThreadControlId, SettingType = NvdrsSettingType.NvdrsDwordType, SettingLocation = NvdrsSettingLocation.NvdrsCurrentProfileLocation, IsCurrentPredefined = 0, @@ -138,11 +150,38 @@ namespace Ryujinx.Common.GraphicsDriver PredefinedValue = targetValue, }; - Check(NvAPI_DRS_SetSetting(handle, profileHandle, ref setting)); + Check(NvAPI_DRS_SetSetting(_handle, _profileHandle, ref setting)); - Check(NvAPI_DRS_SaveSettings(handle)); + Check(NvAPI_DRS_SaveSettings(_handle)); - NvAPI_DRS_DestroySession(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, + SettingType = NvdrsSettingType.NvdrsDwordType, + SettingLocation = NvdrsSettingLocation.NvdrsCurrentProfileLocation, + IsCurrentPredefined = 0, + IsPredefinedValid = 0, + CurrentValue = targetValue, + PredefinedValue = targetValue, + }; + + Check(NvAPI_DRS_SetSetting(_handle, _profileHandle, ref setting)); + + Check(NvAPI_DRS_SaveSettings(_handle)); + + NvAPI_DRS_DestroySession(_handle); } private static T NvAPI_Delegate(uint id) where T : class diff --git a/src/Ryujinx/Program.cs b/src/Ryujinx/Program.cs index 89e895e81b..45e2a0c40d 100644 --- a/src/Ryujinx/Program.cs +++ b/src/Ryujinx/Program.cs @@ -114,6 +114,9 @@ namespace Ryujinx.Ava // Enable OGL multithreading on the driver, when available. DriverUtilities.ToggleOGLThreading(ConfigurationState.Instance.Graphics.BackendThreading == BackendThreading.Off); + // Enable Dxgi present on the driver, when available. + DriverUtilities.ToggleDxgiSwapchain(true); + // Check if keys exists. if (!File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys"))) { From e2122a532d6eccc6e0470f7a58f9b6f931ba0aec Mon Sep 17 00:00:00 2001 From: MutantAura Date: Wed, 10 Apr 2024 22:23:12 +0100 Subject: [PATCH 02/11] Remove unused usings... --- src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs b/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs index bc28f4ec87..0b984face5 100644 --- a/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs +++ b/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs @@ -1,7 +1,5 @@ using Ryujinx.Common.GraphicsDriver.NVAPI; using System; -using System.ComponentModel; -using System.Dynamic; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; From 9cf64e612f1aed3ad785893422f6224346944505 Mon Sep 17 00:00:00 2001 From: MutantAura Date: Wed, 10 Apr 2024 22:48:54 +0100 Subject: [PATCH 03/11] Add spacing back. --- src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs b/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs index 71ea62f47a..cd3f3c7ad1 100644 --- a/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs +++ b/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs @@ -5,6 +5,7 @@ namespace Ryujinx.Common.GraphicsDriver.NVAPI OglThreadControlId = 0x20C1221E, OglCplPreferDxPresentId = 0x20D690F8, } + enum OglThreadControl : uint { OglThreadControlDefault = 0, From d1aa76d22d99a20e046902efc1b34e221e4d0a63 Mon Sep 17 00:00:00 2001 From: MutantAura Date: Wed, 10 Apr 2024 22:54:59 +0100 Subject: [PATCH 04/11] Remove whitespace via format. --- src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs b/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs index cd3f3c7ad1..5b28d3b9fb 100644 --- a/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs +++ b/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs @@ -5,7 +5,7 @@ namespace Ryujinx.Common.GraphicsDriver.NVAPI OglThreadControlId = 0x20C1221E, OglCplPreferDxPresentId = 0x20D690F8, } - + enum OglThreadControl : uint { OglThreadControlDefault = 0, From c1c8aaee0ff8d0b194d277476bea999f4c9ce66a Mon Sep 17 00:00:00 2001 From: MutantAura Date: Wed, 10 Apr 2024 23:13:24 +0100 Subject: [PATCH 05/11] Remove some line breaks and fix ID -> Id. --- src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs | 2 +- src/Ryujinx.Common/GraphicsDriver/NVAPI/NvdrsSetting.cs | 2 +- src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs | 6 ++---- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs b/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs index 5b28d3b9fb..3450b230b2 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 + enum NvapiSettingId : uint { OglThreadControlId = 0x20C1221E, OglCplPreferDxPresentId = 0x20D690F8, diff --git a/src/Ryujinx.Common/GraphicsDriver/NVAPI/NvdrsSetting.cs b/src/Ryujinx.Common/GraphicsDriver/NVAPI/NvdrsSetting.cs index 9e355c33c9..5942bf1af8 100644 --- a/src/Ryujinx.Common/GraphicsDriver/NVAPI/NvdrsSetting.cs +++ b/src/Ryujinx.Common/GraphicsDriver/NVAPI/NvdrsSetting.cs @@ -26,7 +26,7 @@ namespace Ryujinx.Common.GraphicsDriver.NVAPI [FieldOffset(0x4)] public NvapiUnicodeString SettingName; [FieldOffset(0x1004)] - public NvapiSettingID SettingId; + public NvapiSettingId SettingId; [FieldOffset(0x1008)] public NvdrsSettingType SettingType; [FieldOffset(0x100C)] diff --git a/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs b/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs index 0b984face5..a21770bafc 100644 --- a/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs +++ b/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs @@ -139,7 +139,7 @@ namespace Ryujinx.Common.GraphicsDriver NvdrsSetting setting = new() { Version = MakeVersion(1), - SettingId = NvapiSettingID.OglThreadControlId, + SettingId = NvapiSettingId.OglThreadControlId, SettingType = NvdrsSettingType.NvdrsDwordType, SettingLocation = NvdrsSettingLocation.NvdrsCurrentProfileLocation, IsCurrentPredefined = 0, @@ -149,7 +149,6 @@ namespace Ryujinx.Common.GraphicsDriver }; Check(NvAPI_DRS_SetSetting(_handle, _profileHandle, ref setting)); - Check(NvAPI_DRS_SaveSettings(_handle)); NvAPI_DRS_DestroySession(_handle); @@ -166,7 +165,7 @@ namespace Ryujinx.Common.GraphicsDriver NvdrsSetting setting = new() { Version = MakeVersion(1), - SettingId = NvapiSettingID.OglCplPreferDxPresentId, + SettingId = NvapiSettingId.OglCplPreferDxPresentId, SettingType = NvdrsSettingType.NvdrsDwordType, SettingLocation = NvdrsSettingLocation.NvdrsCurrentProfileLocation, IsCurrentPredefined = 0, @@ -176,7 +175,6 @@ namespace Ryujinx.Common.GraphicsDriver }; Check(NvAPI_DRS_SetSetting(_handle, _profileHandle, ref setting)); - Check(NvAPI_DRS_SaveSettings(_handle)); NvAPI_DRS_DestroySession(_handle); From 1c4f8b817575c5dbc337bdbbf07a91940c6cc4d2 Mon Sep 17 00:00:00 2001 From: MutantAura Date: Thu, 11 Apr 2024 17:45:37 +0100 Subject: [PATCH 06/11] Add logging to the nvapi try-catch. --- src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs b/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs index 83bc8d555b..2d8ee9f99c 100644 --- a/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs +++ b/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs @@ -1,3 +1,4 @@ +using Ryujinx.Common.Logging; using System; namespace Ryujinx.Common.GraphicsDriver @@ -15,7 +16,7 @@ namespace Ryujinx.Common.GraphicsDriver } catch { - // NVAPI is not available, or couldn't change the application profile. + Logger.Warning?.Print(LogClass.Application, "Failed to set threaded optimizations. NVAPI may be unavailable."); } } @@ -27,7 +28,7 @@ namespace Ryujinx.Common.GraphicsDriver } catch { - // NVAPI is not available, or couldn't change the application profile. + Logger.Warning?.Print(LogClass.Application, "Failed to set Vulkan/OpenGL present method. NVAPI may be unavailable."); } } } From 965ee0d01614b0514e3bf68021c8f64e7f0def84 Mon Sep 17 00:00:00 2001 From: MutantAura Date: Thu, 11 Apr 2024 18:09:27 +0100 Subject: [PATCH 07/11] 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; From 190791a1c9067feb1483b5381f4c1d626d37a96d Mon Sep 17 00:00:00 2001 From: MutantAura Date: Thu, 11 Apr 2024 18:11:41 +0100 Subject: [PATCH 08/11] dotnet format fixes --- src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs b/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs index fde7801eb8..7e2598c258 100644 --- a/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs +++ b/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs @@ -8,7 +8,7 @@ namespace Ryujinx.Common.GraphicsDriver { public static void ToggleNvDriverSetting(NvapiSettingId id, bool enabled) { - try + try { if (id == NvapiSettingId.OglThreadControlId) { From 32628bb8a3e6a5558f2ef88721e6d043e5c5b552 Mon Sep 17 00:00:00 2001 From: MutantAura Date: Thu, 11 Apr 2024 18:19:38 +0100 Subject: [PATCH 09/11] Add message to `ArgumentException` --- src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs b/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs index 3a69b5631c..6fca326085 100644 --- a/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs +++ b/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs @@ -138,7 +138,7 @@ namespace Ryujinx.Common.GraphicsDriver { NvapiSettingId.OglThreadControlId => (uint)(enabled ? OglThreadControl.OglThreadControlEnable : OglThreadControl.OglThreadControlDisable), NvapiSettingId.OglCplPreferDxPresentId => (uint)(enabled ? OglCplDxPresent.OglCplPreferDxPresentEnable : OglCplDxPresent.OglCplPreferDxPresentDisable), - _ => throw new ArgumentException(), + _ => throw new ArgumentException($"Invalid NVAPI setting id: {id}"), }; NvdrsSetting setting = new() From 7e3f6af6c6181082261e3c254796fb138f69e609 Mon Sep 17 00:00:00 2001 From: MutantAura Date: Sun, 21 Apr 2024 16:53:57 +0100 Subject: [PATCH 10/11] De-duplicate and simplify enum naming. --- src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs | 12 ++++++------ src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs b/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs index 8030bec5b7..525510e9f4 100644 --- a/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs +++ b/src/Ryujinx.Common/GraphicsDriver/NVAPI/Nvapi.cs @@ -8,16 +8,16 @@ namespace Ryujinx.Common.GraphicsDriver.NVAPI enum OglThreadControl : uint { - OglThreadControlDefault = 0, - OglThreadControlEnable = 1, - OglThreadControlDisable = 2, + Default = 0, + Enabled = 1, + Disabled = 2, } // Only present in driver versions >= 526.47 enum OglCplDxPresent : uint { - OglCplPreferDxPresentDisable = 0, - OglCplPreferDxPresentEnable = 1, - OglCplPreferDxPresentDefault = 2, + Disabled = 0, + Enabled = 1, + Default = 2, } } diff --git a/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs b/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs index 6fca326085..d0c4b5f902 100644 --- a/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs +++ b/src/Ryujinx.Common/GraphicsDriver/NVDriverHelper.cs @@ -136,9 +136,9 @@ namespace Ryujinx.Common.GraphicsDriver uint targetValue = id switch { - NvapiSettingId.OglThreadControlId => (uint)(enabled ? OglThreadControl.OglThreadControlEnable : OglThreadControl.OglThreadControlDisable), - NvapiSettingId.OglCplPreferDxPresentId => (uint)(enabled ? OglCplDxPresent.OglCplPreferDxPresentEnable : OglCplDxPresent.OglCplPreferDxPresentDisable), - _ => throw new ArgumentException($"Invalid NVAPI setting id: {id}"), + NvapiSettingId.OglThreadControlId => (uint)(enabled ? OglThreadControl.Enabled : OglThreadControl.Disabled), + NvapiSettingId.OglCplPreferDxPresentId => (uint)(enabled ? OglCplDxPresent.Enabled : OglCplDxPresent.Disabled), + _ => throw new ArgumentException($"Invalid NVAPI setting id: 0x{id:X}"), }; NvdrsSetting setting = new() From 811c2651f2a48d100cf94aab5d5999253ad1fe0a Mon Sep 17 00:00:00 2001 From: MutantAura Date: Thu, 25 Apr 2024 18:00:49 +0100 Subject: [PATCH 11/11] Keep OGL threading specific path and adjust warning to info log. --- .../GraphicsDriver/DriverUtilities.cs | 16 +++++++++------- src/Ryujinx.Gtk3/Program.cs | 2 +- src/Ryujinx.Gtk3/UI/Windows/SettingsWindow.cs | 2 +- src/Ryujinx/Program.cs | 2 +- src/Ryujinx/UI/ViewModels/SettingsViewModel.cs | 2 +- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs b/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs index 7e2598c258..3caa823dc2 100644 --- a/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs +++ b/src/Ryujinx.Common/GraphicsDriver/DriverUtilities.cs @@ -6,21 +6,23 @@ namespace Ryujinx.Common.GraphicsDriver { public static class DriverUtilities { + public static void ToggleOglThreading(bool enabled) + { + Environment.SetEnvironmentVariable("mesa_glthread", enabled.ToString().ToLower()); + Environment.SetEnvironmentVariable("__GL_THREADED_OPTIMIZATIONS", enabled ? "1" : "0"); + + ToggleNvDriverSetting(NvapiSettingId.OglThreadControlId, enabled); + } + public static void ToggleNvDriverSetting(NvapiSettingId id, bool enabled) { try { - 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 NVIDIA driver settings. NVAPI may be unavailable."); + Logger.Info?.Print(LogClass.Application, "NVAPI was unreachable, no changes were made."); } } } diff --git a/src/Ryujinx.Gtk3/Program.cs b/src/Ryujinx.Gtk3/Program.cs index 4dd170a3da..bd3fa4366b 100644 --- a/src/Ryujinx.Gtk3/Program.cs +++ b/src/Ryujinx.Gtk3/Program.cs @@ -236,7 +236,7 @@ namespace Ryujinx // Enable OGL multithreading on the driver, when available. BackendThreading threadingMode = ConfigurationState.Instance.Graphics.BackendThreading; - DriverUtilities.ToggleNvDriverSetting(NvapiSettingId.OglThreadControlId, threadingMode == BackendThreading.Off); + DriverUtilities.ToggleOglThreading(threadingMode == BackendThreading.Off); // Enable DXGI present mode on the driver, when available. DriverUtilities.ToggleNvDriverSetting(NvapiSettingId.OglCplPreferDxPresentId, true); diff --git a/src/Ryujinx.Gtk3/UI/Windows/SettingsWindow.cs b/src/Ryujinx.Gtk3/UI/Windows/SettingsWindow.cs index 876c6b9048..aeb33d6fd1 100644 --- a/src/Ryujinx.Gtk3/UI/Windows/SettingsWindow.cs +++ b/src/Ryujinx.Gtk3/UI/Windows/SettingsWindow.cs @@ -611,7 +611,7 @@ namespace Ryujinx.UI.Windows BackendThreading backendThreading = Enum.Parse(_galThreading.ActiveId); if (ConfigurationState.Instance.Graphics.BackendThreading != backendThreading) { - DriverUtilities.ToggleNvDriverSetting(NvapiSettingId.OglThreadControlId, backendThreading == BackendThreading.Off); + DriverUtilities.ToggleOglThreading(backendThreading == BackendThreading.Off); } ConfigurationState.Instance.Logger.EnableError.Value = _errorLogToggle.Active; diff --git a/src/Ryujinx/Program.cs b/src/Ryujinx/Program.cs index 3ba2e87d4e..5e1193426a 100644 --- a/src/Ryujinx/Program.cs +++ b/src/Ryujinx/Program.cs @@ -113,7 +113,7 @@ namespace Ryujinx.Ava PrintSystemInfo(); // Enable OGL multithreading on the driver, when available. - DriverUtilities.ToggleNvDriverSetting(NvapiSettingId.OglThreadControlId, ConfigurationState.Instance.Graphics.BackendThreading == BackendThreading.Off); + DriverUtilities.ToggleOglThreading(ConfigurationState.Instance.Graphics.BackendThreading == BackendThreading.Off); // Enable Dxgi present on the driver, when available. DriverUtilities.ToggleNvDriverSetting(NvapiSettingId.OglCplPreferDxPresentId, true); diff --git a/src/Ryujinx/UI/ViewModels/SettingsViewModel.cs b/src/Ryujinx/UI/ViewModels/SettingsViewModel.cs index 8574837723..667de1940c 100644 --- a/src/Ryujinx/UI/ViewModels/SettingsViewModel.cs +++ b/src/Ryujinx/UI/ViewModels/SettingsViewModel.cs @@ -530,7 +530,7 @@ namespace Ryujinx.Ava.UI.ViewModels if (ConfigurationState.Instance.Graphics.BackendThreading != (BackendThreading)GraphicsBackendMultithreadingIndex) { - DriverUtilities.ToggleNvDriverSetting(NvapiSettingId.OglThreadControlId, GraphicsBackendMultithreadingIndex == (int)BackendThreading.Off); + DriverUtilities.ToggleOglThreading(GraphicsBackendMultithreadingIndex == (int)BackendThreading.Off); } config.Graphics.BackendThreading.Value = (BackendThreading)GraphicsBackendMultithreadingIndex;