Created
July 1, 2024 02:15
-
-
Save ahmed605/8772102a6670baff96373b33537965db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private unsafe void WalkDxgiAdapters() | |
{ | |
using ComPtr<IDXGIFactory2> dxgiFactory = default; | |
using ComPtr<IDXGIAdapter> dxgiAdapter = default; | |
HRESULT hr = DirectX.CreateDXGIFactory2(0x0, __uuidof<IDXGIFactory2>(), (void**)dxgiFactory.GetAddressOf()); | |
if (hr >= S.S_OK) | |
{ | |
D3D_FEATURE_LEVEL[] d3dFeatureLevels = new D3D_FEATURE_LEVEL[] { D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_9_3 }; | |
fixed (D3D_FEATURE_LEVEL* levels = d3dFeatureLevels) | |
{ | |
uint i = 0; | |
while (dxgiFactory.Get()->EnumAdapters(i, dxgiAdapter.GetAddressOf()) != DXGI.DXGI_ERROR_NOT_FOUND) | |
{ | |
DXGI_ADAPTER_DESC desc; | |
dxgiAdapter.Get()->GetDesc(&desc); | |
D3D_FEATURE_LEVEL d3dFeatureLevel = default; | |
if (localSettings.Containers.TryGetValue("CachedDXGIAdapters", out ApplicationDataContainer container) | |
&& container.Values.TryGetValue($"{desc.VendorId};{desc.DeviceId}", out uint cachedValue) | |
&& cachedValue > (uint)D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_1_0_CORE) | |
{ | |
d3dFeatureLevel = (D3D_FEATURE_LEVEL)cachedValue; | |
} | |
else | |
{ | |
hr = DirectX.D3D11CreateDevice(dxgiAdapter.Get(), D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_UNKNOWN, HMODULE.NULL, 0, levels, (uint)d3dFeatureLevels.Length, D3D11.D3D11_SDK_VERSION, null, &d3dFeatureLevel, null); | |
if (hr >= S.S_OK) | |
{ | |
var cacheContainer = localSettings.CreateContainer("CachedDXGIAdapters", ApplicationDataCreateDisposition.Always); | |
cacheContainer.Values.AddOrUpdate($"{desc.VendorId};{desc.DeviceId}", (uint)d3dFeatureLevel); | |
} | |
} | |
if (d3dFeatureLevel > D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_1_0_CORE && d3dFeatureLevel >= availableDeviceFeatureLevel) | |
{ | |
availableDeviceFeatureLevel = d3dFeatureLevel; | |
if (desc.DedicatedVideoMemory >= AvailableVideoMemory) | |
AvailableVideoMemory = (uint)desc.DedicatedVideoMemory; | |
} | |
++i; | |
} | |
} | |
if (AvailableVideoMemory > 0) | |
return; | |
} | |
IDirect3DDxgiInterfaceAccess dxgiInterfaceAccess = (IDirect3DDxgiInterfaceAccess)(object)CanvasDevice.GetSharedDevice(); | |
using ComPtr<ID3D11Device> d3d11Device = default; | |
dxgiInterfaceAccess.GetInterface(__uuidof<ID3D11Device>(), (void**)d3d11Device.GetAddressOf()); | |
availableDeviceFeatureLevel = d3d11Device.Get()->GetFeatureLevel(); | |
using ComPtr<IDXGIDevice> dxgiDevice = default; | |
d3d11Device.CopyTo(dxgiDevice.GetAddressOf()); | |
dxgiDevice.Get()->GetAdapter(dxgiAdapter.GetAddressOf()); | |
DXGI_ADAPTER_DESC dxgiDesc = default; | |
dxgiAdapter.Get()->GetDesc(&dxgiDesc); | |
AvailableVideoMemory = (uint)dxgiDesc.DedicatedVideoMemory; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment