From 71550ba9a7065fe34e289a289a0cc17e299bfa56 Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Fri, 18 Jun 2021 18:02:14 +1200 Subject: [PATCH] [WIP] Partial move to ADL2 Need to do this to gain access to extra useful fields that just don't exist in ADL functions. Example being HDR capabilties. --- DisplayMagicianShared/AMD/ADL.cs | 293 +++++++++++++++++++++--- DisplayMagicianShared/AMD/ADLWrapper.cs | 22 +- 2 files changed, 271 insertions(+), 44 deletions(-) diff --git a/DisplayMagicianShared/AMD/ADL.cs b/DisplayMagicianShared/AMD/ADL.cs index fa5f50c..c68e3eb 100644 --- a/DisplayMagicianShared/AMD/ADL.cs +++ b/DisplayMagicianShared/AMD/ADL.cs @@ -54,7 +54,45 @@ namespace ATI.ADL /// retrun ADL Error Code internal delegate int ADL2_Main_Control_Destroy(IntPtr contextHandle); - // ///// ADL2 Create Function to create ADL Data + ///// ADL2 Function to get the number of adapters + /// Handle to ADL client context. + /// return number of adapters + internal delegate int ADL2_Adapter_NumberOfAdapters_Get(IntPtr ADLContextHandle, ref int numAdapters); + + /// Retrieves all OS-known adapter information. + /// This function retrieves the adapter information of all OS-known adapters in the system. OS-known adapters can include adapters that are physically present in the system (logical adapters) as well as ones that are no longer present in the system but are still recognized by the OS. + /// Handle to ADL client context. + /// return GPU adapter information + /// the size of the GPU adapter struct + /// retrun ADL Error Code + internal delegate int ADL2_Adapter_AdapterInfo_Get(IntPtr ADLContextHandle, int inputSize, out IntPtr adapterInfoArray); + + /// Retrieves all OS-known adapter information. + /// This function retrieves the adapter information of all OS-known adapters in the system. OS-known adapters can include adapters that are physically present in the system (logical adapters) as well as ones that are no longer present in the system but are still recognized by the OS. + /// Handle to ADL client context. + /// return GPU adapter information. Is a pointer to the pointer of AdapterInfo array. Initialize to NULL before calling this API. ADL will allocate the necessary memory, using the user provided callback function. + /// retrun ADL Error Code + internal delegate int ADL2_Adapter_AdapterInfoX2_Get(IntPtr ADLContextHandle, out IntPtr adapterInfoArray); + + /// Retrieves all OS-known adapter information. + /// This function retrieves the adapter information of all OS-known adapters in the system. OS-known adapters can include adapters that are physically present in the system (logical adapters) as well as ones that are no longer present in the system but are still recognized by the OS. + /// Handle to ADL client context. + /// The ADL index handle of the desired adapter or -1 if all adapters are desired + /// Number of items in the AdapterInfo Array. Can pass NULL pointer if passign an adapter index (in which case only one AdapterInfo is returned) + /// return GPU adapter information + /// retrun ADL Error Code + internal delegate int ADL2_Adapter_AdapterInfoX3_Get(IntPtr ADLContextHandle, int adapterIndex, out int numAdapters, out IntPtr adapterInfoArray); + + /// Retrieves all OS-known adapter information. + /// This function retrieves the adapter information of all OS-known adapters in the system. OS-known adapters can include adapters that are physically present in the system (logical adapters) as well as ones that are no longer present in the system but are still recognized by the OS. + /// Handle to ADL client context. + /// The ADL index handle of the desired adapter or -1 if all adapters are desired + /// Number of items in the AdapterInfo Array. Can pass NULL pointer if passign an adapter index (in which case only one AdapterInfo is returned) + /// return GPU adapter information in adapterInfoX2 array + /// retrun ADL Error Code + internal delegate int ADL2_Adapter_AdapterInfoX4_Get(IntPtr ADLContextHandle, int adapterIndex, out int numAdapters, out IntPtr adapterInfoX2Array); + + ///// ADL2 Create Function to create ADL Data /// Handle to ADL client context. /// Adapter Index /// Display Index @@ -78,12 +116,6 @@ namespace ATI.ADL /// retrun ADL Error Code internal delegate int ADL_Adapter_NumberOfAdapters_Get (ref int numAdapters); - /// ADL Function to get the GPU adapter information - /// return GPU adapter information - /// the size of the GPU adapter struct - /// retrun ADL Error Code - internal delegate int ADL_Adapter_AdapterInfo_Get (IntPtr info, int inputSize); - /// Function to determine if the adapter is active or not. /// The function is used to check if the adapter associated with iAdapterIndex is active /// Adapter Index. @@ -105,6 +137,14 @@ namespace ATI.ADL /// return ADL Error Code internal delegate int ADL_AdapterX2_Caps(int adapterIndex, out ADLAdapterCapsX2 adapterCapabilities); + /// Retrieves all OS-known adapter information. + /// This function retrieves the adapter information of all OS-known adapters in the system. OS-known adapters can include adapters that are physically present in the system (logical adapters) as well as ones that are no longer present in the system but are still recognized by the OS. + /// return GPU adapter information + /// the size of the GPU adapter struct + /// retrun ADL Error Code + internal delegate int ADL_Adapter_AdapterInfo_Get(out IntPtr adapterInfoArray, int inputSize); + + /// Function to get the EDID data. /// This function retrieves the EDID data for a specififed display. /// /// Adapter Index @@ -285,6 +325,62 @@ namespace ATI.ADL [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)ADL.ADL_MAX_ADAPTERS)] internal ADLAdapterInfo[] ADLAdapterInfo; } + + /// ADLAdapterInfoX2 Structure + [StructLayout(LayoutKind.Sequential)] + internal struct ADLAdapterInfoX2 + { + /// The size of the structure + int Size; + /// Adapter Index + internal int AdapterIndex; + /// Adapter UDID + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)ADL.ADL_MAX_PATH)] + internal string UDID; + /// Adapter Bus Number + internal int BusNumber; + /// Adapter Device Number + internal int DeviceNumber; + /// Adapter Function Number + internal int FunctionNumber; + /// Adapter Vendor ID + internal int VendorID; + /// Adapter Adapter name + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)ADL.ADL_MAX_PATH)] + internal string AdapterName; + /// Adapter Display name + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)ADL.ADL_MAX_PATH)] + internal string DisplayName; + /// Adapter Present status + internal int Present; + /// Adapter Exist status + internal int Exist; + /// Adapter Driver Path + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)ADL.ADL_MAX_PATH)] + internal string DriverPath; + /// Adapter Driver Ext Path + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)ADL.ADL_MAX_PATH)] + internal string DriverPathExt; + /// Adapter PNP String + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)ADL.ADL_MAX_PATH)] + internal string PNPString; + /// OS Display Index + internal int OSDisplayIndex; + /// Display Info Mask + internal int InfoMask; + /// Display Info Value + internal int InfoValue; + } + + /// ADLAdapterInfoX2 Array + [StructLayout(LayoutKind.Sequential)] + internal struct ADLAdapterInfoX2Array + { + /// ADLAdapterInfo Array + [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)ADL.ADL_MAX_ADAPTERS)] + internal ADLAdapterInfoX2[] ADLAdapterInfoX2; + } + #endregion ADLAdapterInfo @@ -926,6 +1022,21 @@ namespace ATI.ADL [DllImport(Atiadlxx_FileName)] internal static extern int ADL2_Main_Control_Destroy(IntPtr contextHandle); + [DllImport(Atiadlxx_FileName)] + internal static extern int ADL2_Adapter_NumberOfAdapters_Get(IntPtr contextHandle, ref int numAdapters); + + [DllImport(Atiadlxx_FileName)] + internal static extern int ADL2_Adapter_AdapterInfo_Get(IntPtr ADLContextHandle, int inputSize, out IntPtr AdapterInfoArray); + + [DllImport(Atiadlxx_FileName)] + internal static extern int ADL2_Adapter_AdapterInfoX2_Get(IntPtr ADLContextHandle, out IntPtr AdapterInfoArray); + + [DllImport(Atiadlxx_FileName)] + internal static extern int ADL2_Adapter_AdapterInfoX3_Get(IntPtr ADLContextHandle, int adapterIndex, out int numAdapters, out IntPtr AdapterInfoArray); + + [DllImport(Atiadlxx_FileName)] + internal static extern int ADL2_Adapter_AdapterInfoX4_Get(IntPtr ADLContextHandle, int adapterIndex, out int numAdapters, out IntPtr AdapterInfoX2Array); + [DllImport(Atiadlxx_FileName)] internal static extern int ADL2_Display_DDCInfo2_Get(IntPtr contextHandle, int adapterIndex, int displayIndex, out ADLDDCInfo2 DDCInfo); @@ -945,7 +1056,7 @@ namespace ATI.ADL internal static extern int ADL_Adapter_NumberOfAdapters_Get (ref int numAdapters); [DllImport(Atiadlxx_FileName)] - internal static extern int ADL_Adapter_AdapterInfo_Get (IntPtr info, int inputSize); + internal static extern int ADL_Adapter_AdapterInfo_Get (out IntPtr info, int inputSize); [DllImport(Atiadlxx_FileName)] internal static extern int ADL_Adapter_Active_Get(int adapterIndex, ref int status); @@ -1145,7 +1256,121 @@ namespace ATI.ADL /// check flag to indicate the delegate has been checked private static bool ADL2_Main_Control_Destroy_Check = false; #endregion ADL2_Main_Control_Destroy - + + #region ADL2_Adapter_NumberOfAdapters_Get + /// ADL2_Adapter_NumberOfAdapters_Get Delegates + internal static ADL2_Adapter_NumberOfAdapters_Get ADL2_Adapter_NumberOfAdapters_Get + { + get + { + if (!ADL2_Adapter_NumberOfAdapters_Get_Check && null == ADL2_Adapter_NumberOfAdapters_Get_) + { + ADL2_Adapter_NumberOfAdapters_Get_Check = true; + if (ADLCheckLibrary.IsFunctionValid("ADL2_Adapter_NumberOfAdapters_Get")) + { + ADL2_Adapter_NumberOfAdapters_Get_ = ADLImport.ADL2_Adapter_NumberOfAdapters_Get; + } + } + return ADL2_Adapter_NumberOfAdapters_Get_; + } + } + /// Private Delegate + private static ADL2_Adapter_NumberOfAdapters_Get ADL2_Adapter_NumberOfAdapters_Get_ = null; + /// check flag to indicate the delegate has been checked + private static bool ADL2_Adapter_NumberOfAdapters_Get_Check = false; + #endregion ADL2_Adapter_NumberOfAdapters_Get + + #region ADL2_Adapter_AdapterInfo_Get + /// ADL2_Adapter_AdapterInfo_Get Delegates + internal static ADL2_Adapter_AdapterInfo_Get ADL2_Adapter_AdapterInfo_Get + { + get + { + if (!ADL2_Adapter_AdapterInfo_Get_Check && null == ADL2_Adapter_AdapterInfo_Get_) + { + ADL2_Adapter_AdapterInfo_Get_Check = true; + if (ADLCheckLibrary.IsFunctionValid("ADL2_Adapter_AdapterInfo_Get")) + { + ADL2_Adapter_AdapterInfo_Get_ = ADLImport.ADL2_Adapter_AdapterInfo_Get; + } + } + return ADL2_Adapter_AdapterInfo_Get_; + } + } + /// Private Delegate + private static ADL2_Adapter_AdapterInfo_Get ADL2_Adapter_AdapterInfo_Get_ = null; + /// check flag to indicate the delegate has been checked + private static bool ADL2_Adapter_AdapterInfo_Get_Check = false; + #endregion ADL2_Adapter_AdapterInfo_Get + + #region ADL2_Adapter_AdapterInfoX2_Get + /// ADL2_Adapter_AdapterInfoX2_Get Delegates + internal static ADL2_Adapter_AdapterInfoX2_Get ADL2_Adapter_AdapterInfoX2_Get + { + get + { + if (!ADL2_Adapter_AdapterInfoX2_Get_Check && null == ADL2_Adapter_AdapterInfoX2_Get_) + { + ADL2_Adapter_AdapterInfoX2_Get_Check = true; + if (ADLCheckLibrary.IsFunctionValid("ADL2_Adapter_AdapterInfoX2_Get")) + { + ADL2_Adapter_AdapterInfoX2_Get_ = ADLImport.ADL2_Adapter_AdapterInfoX2_Get; + } + } + return ADL2_Adapter_AdapterInfoX2_Get_; + } + } + /// Private Delegate + private static ADL2_Adapter_AdapterInfoX2_Get ADL2_Adapter_AdapterInfoX2_Get_ = null; + /// check flag to indicate the delegate has been checked + private static bool ADL2_Adapter_AdapterInfoX2_Get_Check = false; + #endregion ADL2_Adapter_AdapterInfoX2_Get + + #region ADL2_Adapter_AdapterInfoX3_Get + /// ADL2_Adapter_AdapterInfoX3_Get Delegates + internal static ADL2_Adapter_AdapterInfoX3_Get ADL2_Adapter_AdapterInfoX3_Get + { + get + { + if (!ADL2_Adapter_AdapterInfoX3_Get_Check && null == ADL2_Adapter_AdapterInfoX3_Get_) + { + ADL2_Adapter_AdapterInfoX3_Get_Check = true; + if (ADLCheckLibrary.IsFunctionValid("ADL2_Adapter_AdapterInfoX3_Get")) + { + ADL2_Adapter_AdapterInfoX3_Get_ = ADLImport.ADL2_Adapter_AdapterInfoX3_Get; + } + } + return ADL2_Adapter_AdapterInfoX3_Get_; + } + } + /// Private Delegate + private static ADL2_Adapter_AdapterInfoX3_Get ADL2_Adapter_AdapterInfoX3_Get_ = null; + /// check flag to indicate the delegate has been checked + private static bool ADL2_Adapter_AdapterInfoX3_Get_Check = false; + #endregion ADL2_Adapter_AdapterInfoX3_Get + + #region ADL2_Adapter_AdapterInfoX4_Get + /// ADL2_Adapter_AdapterInfoX4_Get Delegates + internal static ADL2_Adapter_AdapterInfoX4_Get ADL2_Adapter_AdapterInfoX4_Get + { + get + { + if (!ADL2_Adapter_AdapterInfoX4_Get_Check && null == ADL2_Adapter_AdapterInfoX4_Get_) + { + ADL2_Adapter_AdapterInfoX4_Get_Check = true; + if (ADLCheckLibrary.IsFunctionValid("ADL2_Adapter_AdapterInfoX4_Get")) + { + ADL2_Adapter_AdapterInfoX4_Get_ = ADLImport.ADL2_Adapter_AdapterInfoX4_Get; + } + } + return ADL2_Adapter_AdapterInfoX4_Get_; + } + } + /// Private Delegate + private static ADL2_Adapter_AdapterInfoX4_Get ADL2_Adapter_AdapterInfoX4_Get_ = null; + /// check flag to indicate the delegate has been checked + private static bool ADL2_Adapter_AdapterInfoX4_Get_Check = false; + #endregion ADL2_Adapter_AdapterInfoX4_Get #region ADL2_Display_DDCInfo2_Get /// ADL2_Display_DDCInfo2_Get Delegates @@ -1240,32 +1465,8 @@ namespace ATI.ADL private static bool ADL_Adapter_NumberOfAdapters_Get_Check = false; #endregion ADL_Adapter_NumberOfAdapters_Get - #region ADL_Adapter_AdapterInfo_Get - /// ADL_Adapter_AdapterInfo_Get Delegates - internal static ADL_Adapter_AdapterInfo_Get ADL_Adapter_AdapterInfo_Get - { - get - { - if (!ADL_Adapter_AdapterInfo_Get_Check && null == ADL_Adapter_AdapterInfo_Get_) - { - ADL_Adapter_AdapterInfo_Get_Check = true; - if (ADLCheckLibrary.IsFunctionValid("ADL_Adapter_AdapterInfo_Get")) - { - ADL_Adapter_AdapterInfo_Get_ = ADLImport.ADL_Adapter_AdapterInfo_Get; - } - } - return ADL_Adapter_AdapterInfo_Get_; - } - } - /// Private Delegate - private static ADL_Adapter_AdapterInfo_Get ADL_Adapter_AdapterInfo_Get_ = null; - /// check flag to indicate the delegate has been checked - private static bool ADL_Adapter_AdapterInfo_Get_Check = false; - #endregion ADL_Adapter_AdapterInfo_Get - #region ADL_Adapter_ID_Get - #endregion ADL_Adapter_ID_Get - + /// ADL_Adapter_Active_Get Delegates internal static ADL_Adapter_ID_Get ADL_Adapter_ID_Get { @@ -1286,6 +1487,7 @@ namespace ATI.ADL private static ADL_Adapter_ID_Get ADL_Adapter_ID_Get_ = null; /// check flag to indicate the delegate has been checked private static bool ADL_Adapter_ID_Get_Check = false; + #endregion ADL_Adapter_ID_Get #region ADL_AdapterX2_Caps /// ADL_AdapterX2_Caps Delegates @@ -1310,6 +1512,29 @@ namespace ATI.ADL private static bool ADL_AdapterX2_Caps_Check = false; #endregion ADL_AdapterX2_Caps + #region ADL_Adapter_AdapterInfo_Get + /// ADL_Adapter_AdapterInfo_Get Delegates + internal static ADL_Adapter_AdapterInfo_Get ADL_Adapter_AdapterInfo_Get + { + get + { + if (!ADL_Adapter_AdapterInfo_Get_Check && null == ADL_Adapter_AdapterInfo_Get_) + { + ADL_Adapter_AdapterInfo_Get_Check = true; + if (ADLCheckLibrary.IsFunctionValid("ADL_Adapter_AdapterInfo_Get")) + { + ADL_Adapter_AdapterInfo_Get_ = ADLImport.ADL_Adapter_AdapterInfo_Get; + } + } + return ADL_Adapter_AdapterInfo_Get_; + } + } + /// Private Delegate + private static ADL_Adapter_AdapterInfo_Get ADL_Adapter_AdapterInfo_Get_ = null; + /// check flag to indicate the delegate has been checked + private static bool ADL_Adapter_AdapterInfo_Get_Check = false; + #endregion ADL_Adapter_AdapterInfo_Get + #region ADL_Adapter_Active_Get /// ADL_Adapter_Active_Get Delegates internal static ADL_Adapter_Active_Get ADL_Adapter_Active_Get diff --git a/DisplayMagicianShared/AMD/ADLWrapper.cs b/DisplayMagicianShared/AMD/ADLWrapper.cs index 752ebe0..d4946e4 100644 --- a/DisplayMagicianShared/AMD/ADLWrapper.cs +++ b/DisplayMagicianShared/AMD/ADLWrapper.cs @@ -221,20 +221,20 @@ namespace DisplayMagicianShared.AMD }*/ - if (null != ADL.ADL_Adapter_NumberOfAdapters_Get) + if (null != ADL.ADL2_Adapter_NumberOfAdapters_Get) { - ADL.ADL_Adapter_NumberOfAdapters_Get(ref NumberOfAdapters); + ADL.ADL2_Adapter_NumberOfAdapters_Get(_adlContextHandle, ref NumberOfAdapters); SharedLogger.logger.Trace($"ADLWrapper/GenerateProfileDisplayIdentifiers: Number Of Adapters: {NumberOfAdapters.ToString()} "); } if (NumberOfAdapters > 0) { // Get OS adpater info from ADL - ADLAdapterInfoArray OSAdapterInfoData; - OSAdapterInfoData = new ADLAdapterInfoArray(); + ADLAdapterInfoX2Array OSAdapterInfoData; + OSAdapterInfoData = new ADLAdapterInfoX2Array(); IntPtr AdapterBuffer = IntPtr.Zero; - if (ADL.ADL_Adapter_AdapterInfo_Get != null) + if (ADL.ADL2_Adapter_AdapterInfoX4_Get != null) { SharedLogger.logger.Trace($"ADLWrapper/GenerateProfileDisplayIdentifiers: ADL_Adapter_AdapterInfo_Get DLL function exists."); // Figure out the size of the AdapterBuffer we need to build @@ -244,11 +244,13 @@ namespace DisplayMagicianShared.AMD // Get the Adapter info and put it in the AdapterBuffer SharedLogger.logger.Trace($"ADLWrapper/GenerateProfileDisplayIdentifiers: Running ADL_Adapter_AdapterInfo_Get to find all known AMD adapters."); - ADLRet = ADL.ADL_Adapter_AdapterInfo_Get(AdapterBuffer, size); + //ADLRet = ADL.ADL2_Adapter_AdapterInfoX4_Get(_adlContextHandle, AdapterBuffer, size); + int numAdapters = 0; + ADLRet = ADL.ADL2_Adapter_AdapterInfoX4_Get(_adlContextHandle, ADL.ADL_ADAPTER_INDEX_ALL, out numAdapters, out AdapterBuffer); if (ADLRet == ADL.ADL_OK) { // Use the AdapterBuffer pointer to marshal the OS Adapter Info into a structure - OSAdapterInfoData = (ADLAdapterInfoArray)Marshal.PtrToStructure(AdapterBuffer, OSAdapterInfoData.GetType()); + OSAdapterInfoData = (ADLAdapterInfoX2Array)Marshal.PtrToStructure(AdapterBuffer, OSAdapterInfoData.GetType()); int IsActive = ADL.ADL_TRUE; // We only want to search for active adapters SharedLogger.logger.Trace($"ADLWrapper/GenerateProfileDisplayIdentifiers: Successfully run ADL_Adapter_AdapterInfo_Get to find information about all known AMD adapters."); @@ -256,7 +258,7 @@ namespace DisplayMagicianShared.AMD // Go through each adapter for (int i = 0; i < NumberOfAdapters; i++) { - ADLAdapterInfo oneAdapter = OSAdapterInfoData.ADLAdapterInfo[i]; + ADLAdapterInfoX2 oneAdapter = OSAdapterInfoData.ADLAdapterInfoX2[i]; // Check if the adapter is active @@ -562,7 +564,7 @@ namespace DisplayMagicianShared.AMD OSAdapterInfoData = new ADLAdapterInfoArray(); IntPtr AdapterBuffer = IntPtr.Zero; - if (ADL.ADL_Adapter_AdapterInfo_Get != null) + if (ADL.ADL2_Adapter_AdapterInfoX4_Get != null) { SharedLogger.logger.Trace($"ADLWrapper/GenerateProfileDisplayIdentifiers: ADL_Adapter_AdapterInfo_Get DLL function exists."); // Figure out the size of the AdapterBuffer we need to build @@ -572,7 +574,7 @@ namespace DisplayMagicianShared.AMD // Get the Adapter info and put it in the AdapterBuffer SharedLogger.logger.Trace($"ADLWrapper/GenerateProfileDisplayIdentifiers: Running ADL_Adapter_AdapterInfo_Get to find all known AMD adapters."); - ADLRet = ADL.ADL_Adapter_AdapterInfo_Get(AdapterBuffer, size); + ADLRet = ADL.ADL_Adapter_AdapterInfo_Get(out AdapterBuffer, size); if (ADLRet == ADL.ADL_OK) { // Use the AdapterBuffer pointer to marshal the OS Adapter Info into a structure