Further de-duplicate NVAPI interface code + GTK

This commit is contained in:
MutantAura 2024-04-11 18:09:27 +01:00
parent 1c4f8b8175
commit 965ee0d016
7 changed files with 30 additions and 52 deletions

View file

@ -1,3 +1,4 @@
using Ryujinx.Common.GraphicsDriver.NVAPI;
using Ryujinx.Common.Logging; using Ryujinx.Common.Logging;
using System; using System;
@ -5,30 +6,21 @@ namespace Ryujinx.Common.GraphicsDriver
{ {
public static class DriverUtilities public static class DriverUtilities
{ {
public static void ToggleOGLThreading(bool enabled) public static void ToggleNvDriverSetting(NvapiSettingId id, bool enabled)
{
try
{
if (id == NvapiSettingId.OglThreadControlId)
{ {
Environment.SetEnvironmentVariable("mesa_glthread", enabled.ToString().ToLower()); Environment.SetEnvironmentVariable("mesa_glthread", enabled.ToString().ToLower());
Environment.SetEnvironmentVariable("__GL_THREADED_OPTIMIZATIONS", enabled ? "1" : "0"); Environment.SetEnvironmentVariable("__GL_THREADED_OPTIMIZATIONS", enabled ? "1" : "0");
}
try NVDriverHelper.SetControlOption(id, enabled);
{
NVDriverHelper.SetThreadedOptimization(enabled);
} }
catch catch
{ {
Logger.Warning?.Print(LogClass.Application, "Failed to set threaded optimizations. NVAPI may be unavailable."); Logger.Warning?.Print(LogClass.Application, "Failed to set NVIDIA driver settings. 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.");
} }
} }
} }

View file

@ -1,6 +1,6 @@
namespace Ryujinx.Common.GraphicsDriver.NVAPI namespace Ryujinx.Common.GraphicsDriver.NVAPI
{ {
enum NvapiSettingId : uint public enum NvapiSettingId : uint
{ {
OglThreadControlId = 0x20C1221E, OglThreadControlId = 0x20C1221E,
OglCplPreferDxPresentId = 0x20D690F8, OglCplPreferDxPresentId = 0x20D690F8,

View file

@ -128,44 +128,23 @@ namespace Ryujinx.Common.GraphicsDriver
return (uint)Unsafe.SizeOf<T>() | version << 16; return (uint)Unsafe.SizeOf<T>() | version << 16;
} }
public static void SetThreadedOptimization(bool enabled) public static void SetControlOption(NvapiSettingId id, bool enabled)
{ {
Initialize(); Initialize();
SetupNvProfile(); SetupNvProfile();
uint targetValue = (uint)(enabled ? OglThreadControl.OglThreadControlEnable : OglThreadControl.OglThreadControlDisable); uint targetValue = id switch
NvdrsSetting setting = new()
{ {
Version = MakeVersion<NvdrsSetting>(1), NvapiSettingId.OglThreadControlId => (uint)(enabled ? OglThreadControl.OglThreadControlEnable : OglThreadControl.OglThreadControlDisable),
SettingId = NvapiSettingId.OglThreadControlId, NvapiSettingId.OglCplPreferDxPresentId => (uint)(enabled ? OglCplDxPresent.OglCplPreferDxPresentEnable : OglCplDxPresent.OglCplPreferDxPresentDisable),
SettingType = NvdrsSettingType.NvdrsDwordType, _ => throw new ArgumentException(),
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);
}
public static void SetDxgiSwapchain(bool enabled)
{
Initialize();
SetupNvProfile();
uint targetValue = (uint)(enabled ? OglCplDxPresent.OglCplPreferDxPresentEnable : OglCplDxPresent.OglCplPreferDxPresentDisable);
NvdrsSetting setting = new() NvdrsSetting setting = new()
{ {
Version = MakeVersion<NvdrsSetting>(1), Version = MakeVersion<NvdrsSetting>(1),
SettingId = NvapiSettingId.OglCplPreferDxPresentId, SettingId = id,
SettingType = NvdrsSettingType.NvdrsDwordType, SettingType = NvdrsSettingType.NvdrsDwordType,
SettingLocation = NvdrsSettingLocation.NvdrsCurrentProfileLocation, SettingLocation = NvdrsSettingLocation.NvdrsCurrentProfileLocation,
IsCurrentPredefined = 0, IsCurrentPredefined = 0,

View file

@ -2,6 +2,7 @@ using Gtk;
using Ryujinx.Common; using Ryujinx.Common;
using Ryujinx.Common.Configuration; using Ryujinx.Common.Configuration;
using Ryujinx.Common.GraphicsDriver; using Ryujinx.Common.GraphicsDriver;
using Ryujinx.Common.GraphicsDriver.NVAPI;
using Ryujinx.Common.Logging; using Ryujinx.Common.Logging;
using Ryujinx.Common.SystemInterop; using Ryujinx.Common.SystemInterop;
using Ryujinx.Modules; using Ryujinx.Modules;
@ -235,7 +236,10 @@ namespace Ryujinx
// Enable OGL multithreading on the driver, when available. // Enable OGL multithreading on the driver, when available.
BackendThreading threadingMode = ConfigurationState.Instance.Graphics.BackendThreading; 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. // Initialize Gtk.
Application.Init(); Application.Init();

View file

@ -7,6 +7,7 @@ using Ryujinx.Common.Configuration;
using Ryujinx.Common.Configuration.Hid; using Ryujinx.Common.Configuration.Hid;
using Ryujinx.Common.Configuration.Multiplayer; using Ryujinx.Common.Configuration.Multiplayer;
using Ryujinx.Common.GraphicsDriver; using Ryujinx.Common.GraphicsDriver;
using Ryujinx.Common.GraphicsDriver.NVAPI;
using Ryujinx.HLE.FileSystem; using Ryujinx.HLE.FileSystem;
using Ryujinx.HLE.HOS.Services.Time.TimeZone; using Ryujinx.HLE.HOS.Services.Time.TimeZone;
using Ryujinx.UI.Common.Configuration; using Ryujinx.UI.Common.Configuration;
@ -610,7 +611,7 @@ namespace Ryujinx.UI.Windows
BackendThreading backendThreading = Enum.Parse<BackendThreading>(_galThreading.ActiveId); BackendThreading backendThreading = Enum.Parse<BackendThreading>(_galThreading.ActiveId);
if (ConfigurationState.Instance.Graphics.BackendThreading != backendThreading) 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; ConfigurationState.Instance.Logger.EnableError.Value = _errorLogToggle.Active;

View file

@ -5,6 +5,7 @@ using Ryujinx.Ava.UI.Windows;
using Ryujinx.Common; using Ryujinx.Common;
using Ryujinx.Common.Configuration; using Ryujinx.Common.Configuration;
using Ryujinx.Common.GraphicsDriver; using Ryujinx.Common.GraphicsDriver;
using Ryujinx.Common.GraphicsDriver.NVAPI;
using Ryujinx.Common.Logging; using Ryujinx.Common.Logging;
using Ryujinx.Common.SystemInterop; using Ryujinx.Common.SystemInterop;
using Ryujinx.Modules; using Ryujinx.Modules;
@ -112,10 +113,10 @@ namespace Ryujinx.Ava
PrintSystemInfo(); PrintSystemInfo();
// Enable OGL multithreading on the driver, when available. // 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. // Enable Dxgi present on the driver, when available.
DriverUtilities.ToggleDxgiSwapchain(true); DriverUtilities.ToggleNvDriverSetting(NvapiSettingId.OglCplPreferDxPresentId, true);
// Check if keys exists. // Check if keys exists.
if (!File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys"))) if (!File.Exists(Path.Combine(AppDataManager.KeysDirPath, "prod.keys")))

View file

@ -12,6 +12,7 @@ using Ryujinx.Ava.UI.Windows;
using Ryujinx.Common.Configuration; using Ryujinx.Common.Configuration;
using Ryujinx.Common.Configuration.Multiplayer; using Ryujinx.Common.Configuration.Multiplayer;
using Ryujinx.Common.GraphicsDriver; using Ryujinx.Common.GraphicsDriver;
using Ryujinx.Common.GraphicsDriver.NVAPI;
using Ryujinx.Common.Logging; using Ryujinx.Common.Logging;
using Ryujinx.Graphics.Vulkan; using Ryujinx.Graphics.Vulkan;
using Ryujinx.HLE.FileSystem; using Ryujinx.HLE.FileSystem;
@ -529,7 +530,7 @@ namespace Ryujinx.Ava.UI.ViewModels
if (ConfigurationState.Instance.Graphics.BackendThreading != (BackendThreading)GraphicsBackendMultithreadingIndex) 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; config.Graphics.BackendThreading.Value = (BackendThreading)GraphicsBackendMultithreadingIndex;