FPS Limit for Ally

This commit is contained in:
Serge
2024-01-13 21:37:47 +01:00
parent a29cd7e1be
commit e125afde1a
5 changed files with 136 additions and 30 deletions

View File

@@ -15,13 +15,15 @@ namespace GHelper.Ally
public class AllyControl public class AllyControl
{ {
System.Timers.Timer timer = default!; System.Timers.Timer timer = default!;
AmdGpuControl amdControl; static AmdGpuControl amdControl = new AmdGpuControl();
SettingsForm settings; SettingsForm settings;
ControllerMode mode = ControllerMode.Auto; static ControllerMode mode = ControllerMode.Auto;
ControllerMode _autoMode = ControllerMode.Gamepad; static ControllerMode _autoMode = ControllerMode.Gamepad;
int _autoCount = 0; static int _autoCount = 0;
static int fpsLimit = -1;
public AllyControl(SettingsForm settingsForm) public AllyControl(SettingsForm settingsForm)
{ {
@@ -29,8 +31,6 @@ namespace GHelper.Ally
settings = settingsForm; settings = settingsForm;
amdControl = new AmdGpuControl();
timer = new System.Timers.Timer(500); timer = new System.Timers.Timer(500);
timer.Elapsed += Timer_Elapsed; timer.Elapsed += Timer_Elapsed;
@@ -63,8 +63,39 @@ namespace GHelper.Ally
SetMode((ControllerMode)AppConfig.Get("controller_mode", (int)ControllerMode.Auto)); SetMode((ControllerMode)AppConfig.Get("controller_mode", (int)ControllerMode.Auto));
settings.VisualiseBacklight(InputDispatcher.GetBacklight()); settings.VisualiseBacklight(InputDispatcher.GetBacklight());
fpsLimit = amdControl.GetFPSLimit();
Logger.WriteLine($"FPS Limit: {fpsLimit}");
settings.VisualiseFPSLimit(fpsLimit);
} }
public void ToggleFPSLimit()
{
switch (fpsLimit)
{
case 30:
fpsLimit = 40;
break;
case 40:
fpsLimit = 60;
break;
case 60:
fpsLimit = 120;
break;
default:
fpsLimit = 30;
break;
}
int result = amdControl.SetFPSLimit(fpsLimit);
Logger.WriteLine($"FPS Limit {fpsLimit}: {result}");
settings.VisualiseFPSLimit(fpsLimit);
}
public void ToggleBacklight() public void ToggleBacklight()
{ {
InputDispatcher.SetBacklight(4, true); InputDispatcher.SetBacklight(4, true);
@@ -77,7 +108,8 @@ namespace GHelper.Ally
{ {
amdControl.StartFPS(); amdControl.StartFPS();
timer.Start(); timer.Start();
} else }
else
{ {
timer.Stop(); timer.Stop();
amdControl.StopFPS(); amdControl.StopFPS();

View File

@@ -578,6 +578,12 @@ public class Adl2
[DllImport(Atiadlxx_FileName)] [DllImport(Atiadlxx_FileName)]
public static extern int ADL2_FPS_Settings_Get(IntPtr context, int iAdapterIndex, out ADLFPSSettingsOutput lpFPSSettings); public static extern int ADL2_FPS_Settings_Get(IntPtr context, int iAdapterIndex, out ADLFPSSettingsOutput lpFPSSettings);
[DllImport(Atiadlxx_FileName)]
public static extern int ADL2_FPS_Settings_Set(IntPtr context, int iAdapterIndex, ADLFPSSettingsInput lpFPSSettings);
[DllImport(Atiadlxx_FileName)]
public static extern int ADL2_FPS_Settings_Reset(IntPtr context, int iAdapterIndex);
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct ADLFPSSettingsOutput public struct ADLFPSSettingsOutput
{ {
@@ -592,6 +598,18 @@ public class Adl2
public int ulDCFPSMinimum; public int ulDCFPSMinimum;
} }
[StructLayout(LayoutKind.Sequential)]
public struct ADLFPSSettingsInput
{
public int ulSize;
public int bGlobalSettings;
public int ulACFPSCurrent;
public int ulDCFPSCurrent;
// Assuming ulReserved is an array of 6 integers
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public int[] ulReserved;
}
// Clocks // Clocks
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]

View File

@@ -159,11 +159,31 @@ public class AmdGpuControl : IGpuControl
{ {
if (_adlContextHandle == nint.Zero || _iGPU == null) return 0; if (_adlContextHandle == nint.Zero || _iGPU == null) return 0;
float fps; float fps;
if (ADL2_Adapter_FrameMetrics_Get(_adlContextHandle, ((ADLAdapterInfo)_iGPU).AdapterIndex, 0, out fps) != Adl2.ADL_SUCCESS) return 0; if (ADL2_Adapter_FrameMetrics_Get(_adlContextHandle, ((ADLAdapterInfo)_iGPU).AdapterIndex, 0, out fps) != Adl2.ADL_SUCCESS) return 0;
return fps; return fps;
}
public int GetFPSLimit()
{
if (_adlContextHandle == nint.Zero || _iGPU == null) return -1;
ADLFPSSettingsOutput settings;
if (ADL2_FPS_Settings_Get(_adlContextHandle, ((ADLAdapterInfo)_iGPU).AdapterIndex, out settings) != Adl2.ADL_SUCCESS) return -1;
return settings.ulACFPSCurrent;
}
public int SetFPSLimit(int limit)
{
if (_adlContextHandle == nint.Zero || _iGPU == null) return -1;
ADLFPSSettingsInput settings = new ADLFPSSettingsInput();
settings.ulACFPSCurrent = limit;
settings.ulDCFPSCurrent = limit;
settings.bGlobalSettings = 1;
if (ADL2_FPS_Settings_Set(_adlContextHandle, ((ADLAdapterInfo)_iGPU).AdapterIndex, settings) != Adl2.ADL_SUCCESS) return 0;
return 1;
} }
public ADLODNPerformanceLevels? GetGPUClocks() public ADLODNPerformanceLevels? GetGPUClocks()

View File

@@ -110,11 +110,12 @@ namespace GHelper
labelPeripherals = new Label(); labelPeripherals = new Label();
panelAlly = new Panel(); panelAlly = new Panel();
tableLayoutAlly = new TableLayoutPanel(); tableLayoutAlly = new TableLayoutPanel();
buttonBacklight = new RButton();
buttonController = new RButton(); buttonController = new RButton();
panelAllyTitle = new Panel(); panelAllyTitle = new Panel();
pictureAlly = new PictureBox(); pictureAlly = new PictureBox();
labelAlly = new Label(); labelAlly = new Label();
buttonBacklight = new RButton(); buttonFPS = new RButton();
panelMatrix.SuspendLayout(); panelMatrix.SuspendLayout();
tableLayoutMatrix.SuspendLayout(); tableLayoutMatrix.SuspendLayout();
panelMatrixTitle.SuspendLayout(); panelMatrixTitle.SuspendLayout();
@@ -1427,6 +1428,7 @@ namespace GHelper
tableLayoutAlly.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F)); tableLayoutAlly.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
tableLayoutAlly.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F)); tableLayoutAlly.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
tableLayoutAlly.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F)); tableLayoutAlly.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
tableLayoutAlly.Controls.Add(buttonFPS, 0, 0);
tableLayoutAlly.Controls.Add(buttonBacklight, 0, 0); tableLayoutAlly.Controls.Add(buttonBacklight, 0, 0);
tableLayoutAlly.Controls.Add(buttonController, 0, 0); tableLayoutAlly.Controls.Add(buttonController, 0, 0);
tableLayoutAlly.Dock = DockStyle.Top; tableLayoutAlly.Dock = DockStyle.Top;
@@ -1438,6 +1440,28 @@ namespace GHelper
tableLayoutAlly.Size = new Size(787, 80); tableLayoutAlly.Size = new Size(787, 80);
tableLayoutAlly.TabIndex = 23; tableLayoutAlly.TabIndex = 23;
// //
// buttonBacklight
//
buttonBacklight.Activated = false;
buttonBacklight.BackColor = SystemColors.ControlLightLight;
buttonBacklight.BorderColor = Color.Transparent;
buttonBacklight.BorderRadius = 5;
buttonBacklight.Dock = DockStyle.Fill;
buttonBacklight.FlatAppearance.BorderSize = 0;
buttonBacklight.FlatStyle = FlatStyle.Flat;
buttonBacklight.ForeColor = SystemColors.ControlText;
buttonBacklight.Image = Properties.Resources.backlight;
buttonBacklight.ImageAlign = ContentAlignment.MiddleRight;
buttonBacklight.Location = new Point(266, 4);
buttonBacklight.Margin = new Padding(4);
buttonBacklight.Name = "buttonBacklight";
buttonBacklight.Secondary = false;
buttonBacklight.Size = new Size(254, 72);
buttonBacklight.TabIndex = 10;
buttonBacklight.Text = "100%";
buttonBacklight.TextImageRelation = TextImageRelation.ImageBeforeText;
buttonBacklight.UseVisualStyleBackColor = false;
//
// buttonController // buttonController
// //
buttonController.Activated = false; buttonController.Activated = false;
@@ -1493,27 +1517,27 @@ namespace GHelper
labelAlly.TabIndex = 26; labelAlly.TabIndex = 26;
labelAlly.Text = "Ally Controller"; labelAlly.Text = "Ally Controller";
// //
// buttonBacklight // buttonFPS
// //
buttonBacklight.Activated = false; buttonFPS.Activated = false;
buttonBacklight.BackColor = SystemColors.ControlLightLight; buttonFPS.BackColor = SystemColors.ControlLightLight;
buttonBacklight.BorderColor = Color.Transparent; buttonFPS.BorderColor = Color.Transparent;
buttonBacklight.BorderRadius = 5; buttonFPS.BorderRadius = 5;
buttonBacklight.Dock = DockStyle.Fill; buttonFPS.Dock = DockStyle.Fill;
buttonBacklight.FlatAppearance.BorderSize = 0; buttonFPS.FlatAppearance.BorderSize = 0;
buttonBacklight.FlatStyle = FlatStyle.Flat; buttonFPS.FlatStyle = FlatStyle.Flat;
buttonBacklight.ForeColor = SystemColors.ControlText; buttonFPS.ForeColor = SystemColors.ControlText;
buttonBacklight.Image = Properties.Resources.backlight; buttonFPS.Image = Properties.Resources.icons8_video_48;
buttonBacklight.ImageAlign = ContentAlignment.MiddleRight; buttonFPS.ImageAlign = ContentAlignment.MiddleRight;
buttonBacklight.Location = new Point(266, 4); buttonFPS.Location = new Point(528, 4);
buttonBacklight.Margin = new Padding(4); buttonFPS.Margin = new Padding(4);
buttonBacklight.Name = "buttonBacklight"; buttonFPS.Name = "buttonFPS";
buttonBacklight.Secondary = false; buttonFPS.Secondary = false;
buttonBacklight.Size = new Size(254, 72); buttonFPS.Size = new Size(255, 72);
buttonBacklight.TabIndex = 10; buttonFPS.TabIndex = 11;
buttonBacklight.Text = "100%"; buttonFPS.Text = "FPS Limit OFF";
buttonBacklight.TextImageRelation = TextImageRelation.ImageBeforeText; buttonFPS.TextImageRelation = TextImageRelation.ImageBeforeText;
buttonBacklight.UseVisualStyleBackColor = false; buttonFPS.UseVisualStyleBackColor = false;
// //
// SettingsForm // SettingsForm
// //
@@ -1686,5 +1710,6 @@ namespace GHelper
private Label labelAlly; private Label labelAlly;
private PictureBox pictureAlly; private PictureBox pictureAlly;
private RButton buttonBacklight; private RButton buttonBacklight;
private RButton buttonFPS;
} }
} }

View File

@@ -232,6 +232,7 @@ namespace GHelper
buttonController.Click += ButtonController_Click; buttonController.Click += ButtonController_Click;
buttonBacklight.Click += ButtonBacklight_Click; buttonBacklight.Click += ButtonBacklight_Click;
buttonFPS.Click += ButtonFPS_Click;
Text = "G-Helper " + (ProcessHelper.IsUserAdministrator() ? "—" : "-") + " " + AppConfig.GetModelShort(); Text = "G-Helper " + (ProcessHelper.IsUserAdministrator() ? "—" : "-") + " " + AppConfig.GetModelShort();
TopMost = AppConfig.Is("topmost"); TopMost = AppConfig.Is("topmost");
@@ -246,6 +247,11 @@ namespace GHelper
panelPerformance.Focus(); panelPerformance.Focus();
} }
private void ButtonFPS_Click(object? sender, EventArgs e)
{
allyControl.ToggleFPSLimit();
}
private void ButtonBacklight_Click(object? sender, EventArgs e) private void ButtonBacklight_Click(object? sender, EventArgs e)
{ {
allyControl.ToggleBacklight(); allyControl.ToggleBacklight();
@@ -286,6 +292,11 @@ namespace GHelper
buttonBacklight.Text = Math.Round((double)backlight*33.33).ToString() + "%"; buttonBacklight.Text = Math.Round((double)backlight*33.33).ToString() + "%";
} }
public void VisualiseFPSLimit(int limit)
{
buttonFPS.Text = "FPS Limit " + ((limit > 0 && limit < 120) ? limit : "OFF");
}
private void SettingsForm_LostFocus(object? sender, EventArgs e) private void SettingsForm_LostFocus(object? sender, EventArgs e)
{ {
lastLostFocus = DateTimeOffset.Now.ToUnixTimeMilliseconds(); lastLostFocus = DateTimeOffset.Now.ToUnixTimeMilliseconds();