diff --git a/app/Ally/AllyControl.cs b/app/Ally/AllyControl.cs index be651c89..207a2602 100644 --- a/app/Ally/AllyControl.cs +++ b/app/Ally/AllyControl.cs @@ -15,13 +15,15 @@ namespace GHelper.Ally public class AllyControl { System.Timers.Timer timer = default!; - AmdGpuControl amdControl; + static AmdGpuControl amdControl = new AmdGpuControl(); SettingsForm settings; - ControllerMode mode = ControllerMode.Auto; - ControllerMode _autoMode = ControllerMode.Gamepad; - int _autoCount = 0; + static ControllerMode mode = ControllerMode.Auto; + static ControllerMode _autoMode = ControllerMode.Gamepad; + static int _autoCount = 0; + + static int fpsLimit = -1; public AllyControl(SettingsForm settingsForm) { @@ -29,8 +31,6 @@ namespace GHelper.Ally settings = settingsForm; - amdControl = new AmdGpuControl(); - timer = new System.Timers.Timer(500); timer.Elapsed += Timer_Elapsed; @@ -63,8 +63,39 @@ namespace GHelper.Ally SetMode((ControllerMode)AppConfig.Get("controller_mode", (int)ControllerMode.Auto)); 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() { InputDispatcher.SetBacklight(4, true); @@ -77,7 +108,8 @@ namespace GHelper.Ally { amdControl.StartFPS(); timer.Start(); - } else + } + else { timer.Stop(); amdControl.StopFPS(); diff --git a/app/Gpu/AMD/AmdAdl2.cs b/app/Gpu/AMD/AmdAdl2.cs index c9fc2918..d42dfa53 100644 --- a/app/Gpu/AMD/AmdAdl2.cs +++ b/app/Gpu/AMD/AmdAdl2.cs @@ -578,6 +578,12 @@ public class Adl2 [DllImport(Atiadlxx_FileName)] 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)] public struct ADLFPSSettingsOutput { @@ -592,6 +598,18 @@ public class Adl2 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 [StructLayout(LayoutKind.Sequential)] diff --git a/app/Gpu/AMD/AmdGpuControl.cs b/app/Gpu/AMD/AmdGpuControl.cs index 011a0282..45c2c6e2 100644 --- a/app/Gpu/AMD/AmdGpuControl.cs +++ b/app/Gpu/AMD/AmdGpuControl.cs @@ -159,11 +159,31 @@ public class AmdGpuControl : IGpuControl { if (_adlContextHandle == nint.Zero || _iGPU == null) return 0; float fps; - if (ADL2_Adapter_FrameMetrics_Get(_adlContextHandle, ((ADLAdapterInfo)_iGPU).AdapterIndex, 0, out fps) != Adl2.ADL_SUCCESS) return 0; - 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() diff --git a/app/Settings.Designer.cs b/app/Settings.Designer.cs index 7f5902b1..10cfaf66 100644 --- a/app/Settings.Designer.cs +++ b/app/Settings.Designer.cs @@ -110,11 +110,12 @@ namespace GHelper labelPeripherals = new Label(); panelAlly = new Panel(); tableLayoutAlly = new TableLayoutPanel(); + buttonBacklight = new RButton(); buttonController = new RButton(); panelAllyTitle = new Panel(); pictureAlly = new PictureBox(); labelAlly = new Label(); - buttonBacklight = new RButton(); + buttonFPS = new RButton(); panelMatrix.SuspendLayout(); tableLayoutMatrix.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.Controls.Add(buttonFPS, 0, 0); tableLayoutAlly.Controls.Add(buttonBacklight, 0, 0); tableLayoutAlly.Controls.Add(buttonController, 0, 0); tableLayoutAlly.Dock = DockStyle.Top; @@ -1438,6 +1440,28 @@ namespace GHelper tableLayoutAlly.Size = new Size(787, 80); 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.Activated = false; @@ -1493,27 +1517,27 @@ namespace GHelper labelAlly.TabIndex = 26; labelAlly.Text = "Ally Controller"; // - // buttonBacklight + // buttonFPS // - 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; + buttonFPS.Activated = false; + buttonFPS.BackColor = SystemColors.ControlLightLight; + buttonFPS.BorderColor = Color.Transparent; + buttonFPS.BorderRadius = 5; + buttonFPS.Dock = DockStyle.Fill; + buttonFPS.FlatAppearance.BorderSize = 0; + buttonFPS.FlatStyle = FlatStyle.Flat; + buttonFPS.ForeColor = SystemColors.ControlText; + buttonFPS.Image = Properties.Resources.icons8_video_48; + buttonFPS.ImageAlign = ContentAlignment.MiddleRight; + buttonFPS.Location = new Point(528, 4); + buttonFPS.Margin = new Padding(4); + buttonFPS.Name = "buttonFPS"; + buttonFPS.Secondary = false; + buttonFPS.Size = new Size(255, 72); + buttonFPS.TabIndex = 11; + buttonFPS.Text = "FPS Limit OFF"; + buttonFPS.TextImageRelation = TextImageRelation.ImageBeforeText; + buttonFPS.UseVisualStyleBackColor = false; // // SettingsForm // @@ -1686,5 +1710,6 @@ namespace GHelper private Label labelAlly; private PictureBox pictureAlly; private RButton buttonBacklight; + private RButton buttonFPS; } } diff --git a/app/Settings.cs b/app/Settings.cs index 3149d5d1..1b98e955 100644 --- a/app/Settings.cs +++ b/app/Settings.cs @@ -232,6 +232,7 @@ namespace GHelper buttonController.Click += ButtonController_Click; buttonBacklight.Click += ButtonBacklight_Click; + buttonFPS.Click += ButtonFPS_Click; Text = "G-Helper " + (ProcessHelper.IsUserAdministrator() ? "—" : "-") + " " + AppConfig.GetModelShort(); TopMost = AppConfig.Is("topmost"); @@ -246,6 +247,11 @@ namespace GHelper panelPerformance.Focus(); } + private void ButtonFPS_Click(object? sender, EventArgs e) + { + allyControl.ToggleFPSLimit(); + } + private void ButtonBacklight_Click(object? sender, EventArgs e) { allyControl.ToggleBacklight(); @@ -286,6 +292,11 @@ namespace GHelper 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) { lastLostFocus = DateTimeOffset.Now.ToUnixTimeMilliseconds();