Skip to content

Commit

Permalink
imp - prt - Used DirectX for video probing on Wind...
Browse files Browse the repository at this point in the history
...ows

---

We've used the DirectX native methods. Currently, it doesn't work due to access violations, but we'll fix that soon.

---

Type: imp
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/2
  • Loading branch information
AptiviCEO committed Jul 30, 2024
1 parent 93ba31d commit cee97dc
Show file tree
Hide file tree
Showing 16 changed files with 511 additions and 9 deletions.
7 changes: 4 additions & 3 deletions SpecProbe.Loader/LibraryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,14 @@ internal IntPtr LoadSymbol(string symbolName)
// Try to find a symbol
if (PlatformHelper.IsOnWindows())
{
result = Windows.GetProcAddress(file.handle, symbolName);
string resultingSymbol = symbolName;
result = Windows.GetProcAddress(file.handle, resultingSymbol);
if (result == IntPtr.Zero)
{
symbolName = "_" + symbolName + "@";
resultingSymbol = "_" + resultingSymbol + "@";
for (int stackSize = 0; stackSize < 128; stackSize += 4)
{
IntPtr candidate = Windows.GetProcAddress(file.handle, symbolName + stackSize);
IntPtr candidate = Windows.GetProcAddress(file.handle, resultingSymbol + stackSize);
if (candidate != IntPtr.Zero)
{
result = candidate;
Expand Down
31 changes: 31 additions & 0 deletions SpecProbe.Native/Helpers/VideoHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// SpecProbe Copyright (C) 2023-2024 Aptivi
//
// This file is part of SpecProbe
//
// SpecProbe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SpecProbe is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using System;

namespace SpecProbe.Native.Helpers
{
internal static class VideoHelper
{
internal delegate int spdx_get_gpus(out IntPtr gpus);

internal static spdx_get_gpus GetGpus() =>
Initializer.libManager.GetNativeMethodDelegate<spdx_get_gpus>(nameof(spdx_get_gpus));
}
}
14 changes: 9 additions & 5 deletions SpecProbe.Native/Initializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,23 @@ internal static class Initializer
internal static LibraryManager libManager;
private static bool _initialized;
private const string LibraryName = "libspecprober";
private const string LibraryName2 = "libdxhelper";

internal static void InitializeNative()
{
if (_initialized)
return;
string libPath = GetLibraryPath();
string libPath = GetLibraryPath(LibraryName);
string libDxPath = GetLibraryPath(LibraryName2);
if (!File.Exists(libPath))
throw new Exception("Can't load specprober library because it isn't found.");
if (!File.Exists(libDxPath) && PlatformHelper.IsOnWindows())
throw new Exception("Can't load dxhelper library because it isn't found.");
libManager = new LibraryManager(
new LibraryItem(Platform.Windows, Architecture.X86,
new LibraryFile(libPath)),
new LibraryFile(libPath), new LibraryFile(libDxPath)),
new LibraryItem(Platform.Windows, Architecture.X64,
new LibraryFile(libPath)),
new LibraryFile(libPath), new LibraryFile(libDxPath)),
new LibraryItem(Platform.MacOS, Architecture.X64,
new LibraryFile(libPath)),
new LibraryItem(Platform.Linux, Architecture.X64,
Expand All @@ -54,7 +58,7 @@ internal static void InitializeNative()
_initialized = true;
}

private static string GetLibraryPath()
private static string GetLibraryPath(string libraryName)
{
string execPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "/";
string nonSpecificRid =
Expand All @@ -63,7 +67,7 @@ private static string GetLibraryPath()
PlatformHelper.IsOnUnix() ? "linux-" :
"freebsd-") + RuntimeInformation.OSArchitecture.ToString().ToLower();
string directory = $"runtimes/{nonSpecificRid}/native/";
string libName = $"{LibraryName}{(PlatformHelper.IsOnWindows() ? ".dll" : PlatformHelper.IsOnMacOS() ? ".dylib" : ".so")}";
string libName = $"{libraryName}{(PlatformHelper.IsOnWindows() ? ".dll" : PlatformHelper.IsOnMacOS() ? ".dylib" : ".so")}";
string path = $"{execPath}{directory}{libName}";
return path;
}
Expand Down
1 change: 0 additions & 1 deletion SpecProbe.Native/SpecProbe.Native.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<Folder Include="runtimes\linux-x64\native\" />
<Folder Include="runtimes\linux-x86\native\" />
<Folder Include="runtimes\win-x64\native\" />
<Folder Include="runtimes\win-x86\native\" />
</ItemGroup>

<ItemGroup>
Expand Down
35 changes: 35 additions & 0 deletions SpecProbe.Native/Structs/GpuInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// SpecProbe Copyright (C) 2023-2024 Aptivi
//
// This file is part of SpecProbe
//
// SpecProbe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SpecProbe is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using System;
using System.Runtime.InteropServices;

namespace SpecProbe.Native.Structs
{
[StructLayout(LayoutKind.Sequential)]
internal struct GpuInfo
{
// ushort, was UINT
ushort vendorId;
ushort deviceId;

// IntPtr, was const char*
IntPtr name;
}
}
Binary file not shown.
Binary file not shown.
5 changes: 5 additions & 0 deletions SpecProbe/Probers/VideoProber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using SpecProbe.Native.Helpers;
using SpecProbe.Native.Structs;
using SpecProbe.Parts;
using SpecProbe.Parts.Types;
using SpecProbe.Probers.Platform;
Expand Down Expand Up @@ -229,6 +231,9 @@ public static BaseHardwarePartInfo[] GetBaseHardwarePartsWindows()
devNum++;
devInfo.cb = Marshal.SizeOf(devInfo);
}

// Employ libdxhelper to get info about GPUs
int result = VideoHelper.GetGpus().Invoke(out IntPtr gpus);
}
catch (Exception ex)
{
Expand Down
37 changes: 37 additions & 0 deletions interop/libdxhelper/libdxhelper/libdxhelper.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.35027.167
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libdxhelper", "libdxhelper.vcxproj", "{AB33BBB3-0B11-4044-81E5-F0CFA6560E24}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM64 = Debug|ARM64
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|ARM64 = Release|ARM64
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AB33BBB3-0B11-4044-81E5-F0CFA6560E24}.Debug|ARM64.ActiveCfg = Debug|ARM
{AB33BBB3-0B11-4044-81E5-F0CFA6560E24}.Debug|ARM64.Build.0 = Debug|ARM
{AB33BBB3-0B11-4044-81E5-F0CFA6560E24}.Debug|x64.ActiveCfg = Debug|x64
{AB33BBB3-0B11-4044-81E5-F0CFA6560E24}.Debug|x64.Build.0 = Debug|x64
{AB33BBB3-0B11-4044-81E5-F0CFA6560E24}.Debug|x86.ActiveCfg = Debug|Win32
{AB33BBB3-0B11-4044-81E5-F0CFA6560E24}.Debug|x86.Build.0 = Debug|Win32
{AB33BBB3-0B11-4044-81E5-F0CFA6560E24}.Release|ARM64.ActiveCfg = Release|ARM
{AB33BBB3-0B11-4044-81E5-F0CFA6560E24}.Release|ARM64.Build.0 = Release|ARM
{AB33BBB3-0B11-4044-81E5-F0CFA6560E24}.Release|x64.ActiveCfg = Release|x64
{AB33BBB3-0B11-4044-81E5-F0CFA6560E24}.Release|x64.Build.0 = Release|x64
{AB33BBB3-0B11-4044-81E5-F0CFA6560E24}.Release|x86.ActiveCfg = Release|Win32
{AB33BBB3-0B11-4044-81E5-F0CFA6560E24}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5E16E05E-E1E7-4620-8256-F09B4F6C640F}
EndGlobalSection
EndGlobal
Loading

0 comments on commit cee97dc

Please sign in to comment.