Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit fd5f4e8

Browse files
committed
Add project for detecting adapters
1 parent 4f1b503 commit fd5f4e8

File tree

10 files changed

+317
-68
lines changed

10 files changed

+317
-68
lines changed

OnnxStack.Adapter/AdapterInfo.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include <dxgi1_6.h>
2+
#include <wrl/client.h>
3+
#include <vector>
4+
#include "AdapterInfo.h"
5+
6+
#pragma comment(lib, "dxguid.lib")
7+
8+
#define RETURN_IF_FAILED(hr) if (FAILED((hr))) { return 0; }
9+
10+
using Microsoft::WRL::ComPtr;
11+
12+
class DxgiModule
13+
{
14+
using CreateFactoryFunc = decltype(CreateDXGIFactory);
15+
HMODULE m_module = nullptr;
16+
CreateFactoryFunc* m_createFactoryFunc = nullptr;
17+
18+
public:
19+
DxgiModule()
20+
{
21+
m_module = LoadLibraryA("dxgi.dll");
22+
if (m_module)
23+
{
24+
auto funcAddr = GetProcAddress(m_module, "CreateDXGIFactory");
25+
if (funcAddr)
26+
{
27+
m_createFactoryFunc = reinterpret_cast<CreateFactoryFunc*>(funcAddr);
28+
}
29+
}
30+
}
31+
~DxgiModule() { if (m_module) { FreeModule(m_module); } }
32+
33+
ComPtr<IDXGIFactory6> CreateFactory()
34+
{
35+
ComPtr<IDXGIFactory6> adapterFactory;
36+
m_createFactoryFunc(IID_PPV_ARGS(&adapterFactory));
37+
return adapterFactory;
38+
}
39+
};
40+
41+
extern "C" __declspec(dllexport) int __cdecl GetAdapter(bool preferHighPerformance)
42+
{
43+
DxgiModule dxgi;
44+
45+
ComPtr<IDXGIFactory6> factory = dxgi.CreateFactory();;
46+
if (!factory)
47+
{
48+
return 0;
49+
}
50+
51+
ComPtr<IDXGIAdapter1> adapter;
52+
53+
// Store LUIDs for hardware adapters in original unsorted order.
54+
std::vector<std::pair<int, LUID>> adaptersUnsortedIndexAndLuid;
55+
for (int i = 0; factory->EnumAdapters1(i, adapter.ReleaseAndGetAddressOf()) != DXGI_ERROR_NOT_FOUND; i++)
56+
{
57+
DXGI_ADAPTER_DESC desc = {};
58+
RETURN_IF_FAILED(adapter->GetDesc(&desc));
59+
adaptersUnsortedIndexAndLuid.emplace_back(i, desc.AdapterLuid);
60+
}
61+
62+
// Find the first adapter meeting GPU preference.
63+
DXGI_ADAPTER_DESC preferredAdapterDesc = {};
64+
{
65+
DXGI_GPU_PREFERENCE gpuPreference = preferHighPerformance ? DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE : DXGI_GPU_PREFERENCE_MINIMUM_POWER;
66+
RETURN_IF_FAILED(factory->EnumAdapterByGpuPreference(0, gpuPreference, IID_PPV_ARGS(adapter.ReleaseAndGetAddressOf())));
67+
RETURN_IF_FAILED(adapter->GetDesc(&preferredAdapterDesc));
68+
}
69+
70+
// Look up the index of the preferred adapter in the unsorted list order.
71+
for (auto& hardwareAdapterEntry : adaptersUnsortedIndexAndLuid)
72+
{
73+
if (hardwareAdapterEntry.second.HighPart == preferredAdapterDesc.AdapterLuid.HighPart &&
74+
hardwareAdapterEntry.second.LowPart == preferredAdapterDesc.AdapterLuid.LowPart)
75+
{
76+
return hardwareAdapterEntry.first;
77+
}
78+
}
79+
80+
return 0;
81+
}
82+
83+
84+
extern "C" __declspec(dllexport) void __cdecl GetAdapters(AdapterInfo * adapterArray)
85+
{
86+
DxgiModule dxgi;
87+
88+
ComPtr<IDXGIFactory6> factory = dxgi.CreateFactory();;
89+
if (!factory)
90+
return;
91+
92+
int adapterCount = 0;
93+
ComPtr<IDXGIAdapter1> adapter;
94+
for (int i = 0; factory->EnumAdapters1(i, adapter.ReleaseAndGetAddressOf()) != DXGI_ERROR_NOT_FOUND; i++)
95+
{
96+
DXGI_ADAPTER_DESC desc = {};
97+
HRESULT hr = adapter->GetDesc(&desc);
98+
if (SUCCEEDED(hr))
99+
{
100+
AdapterInfo info{};
101+
info.Id = adapterCount;
102+
info.AdapterLuid = desc.AdapterLuid;
103+
info.DedicatedSystemMemory = desc.DedicatedSystemMemory;
104+
info.DedicatedVideoMemory = desc.DedicatedVideoMemory;
105+
info.DeviceId = desc.DeviceId;
106+
info.Revision = desc.Revision;
107+
info.SharedSystemMemory = desc.SharedSystemMemory;
108+
info.SubSysId = desc.SubSysId;
109+
info.VendorId = desc.VendorId;
110+
wcsncpy_s(info.Description, desc.Description, _TRUNCATE);
111+
adapterArray[adapterCount] = info;
112+
++adapterCount;
113+
}
114+
}
115+
}

OnnxStack.Adapter/AdapterInfo.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
struct AdapterInfo
4+
{
5+
UINT Id;
6+
UINT VendorId;
7+
UINT DeviceId;
8+
UINT SubSysId;
9+
UINT Revision;
10+
SIZE_T DedicatedVideoMemory;
11+
SIZE_T DedicatedSystemMemory;
12+
SIZE_T SharedSystemMemory;
13+
LUID AdapterLuid;
14+
WCHAR Description[128];
15+
};
16+
17+
18+
extern "C" __declspec(dllexport) int __cdecl GetAdapter(bool preferHighPerformance);
19+
20+
extern "C" __declspec(dllexport) void __cdecl GetAdapters(AdapterInfo * adapterArray);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClCompile Include="AdapterInfo.cpp">
19+
<Filter>Source Files</Filter>
20+
</ClCompile>
21+
</ItemGroup>
22+
<ItemGroup>
23+
<ClInclude Include="AdapterInfo.h">
24+
<Filter>Header Files</Filter>
25+
</ClInclude>
26+
</ItemGroup>
27+
</Project>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|x64">
5+
<Configuration>Debug</Configuration>
6+
<Platform>x64</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|x64">
9+
<Configuration>Release</Configuration>
10+
<Platform>x64</Platform>
11+
</ProjectConfiguration>
12+
</ItemGroup>
13+
<PropertyGroup Label="Globals">
14+
<VCProjectVersion>16.0</VCProjectVersion>
15+
<Keyword>Win32Proj</Keyword>
16+
<ProjectGuid>{5a64fc26-6926-4a45-acce-1fe173166990}</ProjectGuid>
17+
<RootNamespace>OnnxStack.Adapter</RootNamespace>
18+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
19+
<ProjectName>OnnxStack.Adapter</ProjectName>
20+
</PropertyGroup>
21+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
22+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
23+
<ConfigurationType>DynamicLibrary</ConfigurationType>
24+
<UseDebugLibraries>true</UseDebugLibraries>
25+
<PlatformToolset>v143</PlatformToolset>
26+
<CharacterSet>Unicode</CharacterSet>
27+
</PropertyGroup>
28+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
29+
<ConfigurationType>DynamicLibrary</ConfigurationType>
30+
<UseDebugLibraries>false</UseDebugLibraries>
31+
<PlatformToolset>v143</PlatformToolset>
32+
<WholeProgramOptimization>true</WholeProgramOptimization>
33+
<CharacterSet>Unicode</CharacterSet>
34+
</PropertyGroup>
35+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
36+
<ImportGroup Label="ExtensionSettings">
37+
</ImportGroup>
38+
<ImportGroup Label="Shared">
39+
</ImportGroup>
40+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
41+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
42+
</ImportGroup>
43+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
44+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
45+
</ImportGroup>
46+
<PropertyGroup Label="UserMacros" />
47+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
48+
<OutDir>$(ProjectDir)$(Platform)\$(Configuration)\</OutDir>
49+
</PropertyGroup>
50+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
51+
<ClCompile>
52+
<WarningLevel>Level3</WarningLevel>
53+
<SDLCheck>true</SDLCheck>
54+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
55+
<ConformanceMode>true</ConformanceMode>
56+
</ClCompile>
57+
<Link>
58+
<SubSystem>Console</SubSystem>
59+
<GenerateDebugInformation>true</GenerateDebugInformation>
60+
</Link>
61+
</ItemDefinitionGroup>
62+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
63+
<ClCompile>
64+
<WarningLevel>Level3</WarningLevel>
65+
<FunctionLevelLinking>true</FunctionLevelLinking>
66+
<IntrinsicFunctions>true</IntrinsicFunctions>
67+
<SDLCheck>true</SDLCheck>
68+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
69+
<ConformanceMode>true</ConformanceMode>
70+
</ClCompile>
71+
<Link>
72+
<SubSystem>Console</SubSystem>
73+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
74+
<OptimizeReferences>true</OptimizeReferences>
75+
<GenerateDebugInformation>true</GenerateDebugInformation>
76+
</Link>
77+
</ItemDefinitionGroup>
78+
<ItemGroup>
79+
<ClCompile Include="AdapterInfo.cpp" />
80+
</ItemGroup>
81+
<ItemGroup>
82+
<ClInclude Include="AdapterInfo.h" />
83+
</ItemGroup>
84+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
85+
<ImportGroup Label="ExtensionTargets">
86+
</ImportGroup>
87+
</Project>

OnnxStack.Console/OnnxStack.Console.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<Nullable>disable</Nullable>
88
<PlatformTarget>x64</PlatformTarget>
99
<Configurations>Debug;Release;Debug-Cuda;Debug-TensorRT;Release-Cuda;Release-TensorRT</Configurations>
10+
<Platforms>x64</Platforms>
1011
</PropertyGroup>
1112

1213
<ItemGroup>

OnnxStack.Core/OnnxStack.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
2626
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
2727
<PackageIcon>OnnxStack - 128x128.png</PackageIcon>
28+
<Platforms>x64</Platforms>
2829
</PropertyGroup>
2930

3031
<ItemGroup>

OnnxStack.ImageUpscaler/OnnxStack.ImageUpscaler.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
2424
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
2525
<PackageIcon>OnnxStack - 128x128.png</PackageIcon>
26+
<Platforms>x64</Platforms>
2627
</PropertyGroup>
2728

2829
<ItemGroup>

OnnxStack.StableDiffusion/OnnxStack.StableDiffusion.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
2626
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
2727
<PackageIcon>OnnxStack - 128x128.png</PackageIcon>
28+
<Platforms>x64</Platforms>
2829
</PropertyGroup>
2930

3031
<ItemGroup>

OnnxStack.UI/OnnxStack.UI.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
<UseWindowsForms>true</UseWindowsForms>
1010
<PlatformTarget>x64</PlatformTarget>
1111
<Configurations>Debug;Release;Debug-Cuda;Debug-TensorRT;Release-Cuda;Release-TensorRT</Configurations>
12+
<Platforms>x64</Platforms>
1213
</PropertyGroup>
1314

14-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release-Cuda|AnyCPU'">
15+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release-Cuda|x64'">
1516
<Optimize>True</Optimize>
1617
</PropertyGroup>
1718

18-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release-TensorRT|AnyCPU'">
19+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release-TensorRT|x64'">
1920
<Optimize>True</Optimize>
2021
</PropertyGroup>
2122

@@ -38,6 +39,7 @@
3839
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
3940
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
4041
</Content>
42+
<None Include="..\OnnxStack.Adapter\x64\Debug\OnnxStack.Adapter.dll" Link="OnnxStack.Adapter.dll" />
4143
<Resource Include="Fonts\fa-brands-400.ttf" />
4244
<Resource Include="Fonts\fa-duotone-900.ttf" />
4345
<Resource Include="Fonts\fa-light-300.ttf" />

0 commit comments

Comments
 (0)