Skip to content

Instantly share code, notes, and snippets.

@koldev
Created October 14, 2014 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koldev/220ef7dac4ee8842a5f2 to your computer and use it in GitHub Desktop.
Save koldev/220ef7dac4ee8842a5f2 to your computer and use it in GitHub Desktop.
DirectX adapter list (with supported feature levels)
#include <iostream>
#include <locale>
#include <memory>
#include <string>
#include <comdef.h>
#include <dxgi.h>
#include <d3d11.h>
#pragma comment(lib, "comsuppw.lib")
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "d3d11.lib")
class Error {
public:
Error(const wchar_t* message, HRESULT hr) : message1_(message), message2_(_com_error(hr).ErrorMessage()) {}
std::wstring message1() const { return message1_; }
std::wstring message2() const { return message2_; }
private:
std::wstring message1_;
std::wstring message2_;
};
int wmain() {
try {
//std::locale::global(std::locale("Hungarian_Hungary.1250"));
IDXGIFactory1* factory = nullptr;
HRESULT hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), reinterpret_cast<void**>(&factory));
if (FAILED(hr)) {
throw Error(L"CreateDXGIFactory1 failed", hr);
}
auto i = 0U;
IDXGIAdapter1* adapter = nullptr;
while (factory->EnumAdapters1(i, &adapter) != DXGI_ERROR_NOT_FOUND) {
DXGI_ADAPTER_DESC1 adapterDesc;
hr = adapter->GetDesc1(&adapterDesc);
if (FAILED(hr)) {
throw Error(L"IDXGIAdapter1::GetDesc1 failed", hr);
}
std::wcout << L"=== " << adapterDesc.Description << L" ===" << std::endl;
std::wcout << L"- Dedicated video memory: " << adapterDesc.DedicatedVideoMemory << L" B" << std::endl;
std::wcout << L"- Dedicated system memory: " << adapterDesc.DedicatedSystemMemory << L" B" << std::endl;
std::wcout << L"- Shared system memory: " << adapterDesc.SharedSystemMemory << L" B" << std::endl;
std::wcout << L"- Outputs:" << std::endl;
auto j = 0U;
IDXGIOutput* output = nullptr;
while (adapter->EnumOutputs(j, &output) != DXGI_ERROR_NOT_FOUND) {
DXGI_OUTPUT_DESC outputDesc;
hr = output->GetDesc(&outputDesc);
if (FAILED(hr)) {
throw Error(L"IDXGIOutput::GetDesc failed", hr);
}
std::wcout << L" - Name: " << outputDesc.DeviceName << std::endl;
std::wcout
<< L" - Position: "
<< outputDesc.DesktopCoordinates.left
<< ", "
<< outputDesc.DesktopCoordinates.top
<< std::endl;
std::wcout
<< L" - Size: "
<< (outputDesc.DesktopCoordinates.right - outputDesc.DesktopCoordinates.left)
<< " x "
<< (outputDesc.DesktopCoordinates.bottom - outputDesc.DesktopCoordinates.top)
<< std::endl;
std::wcout << L" - Attached to desktop? " << (outputDesc.AttachedToDesktop == 0 ? L"no" : L"yes") << std::endl;
switch (outputDesc.Rotation) {
case DXGI_MODE_ROTATION_UNSPECIFIED: std::wcout << L" - Rotation? unknown" << std::endl; break;
case DXGI_MODE_ROTATION_IDENTITY: std::wcout << L" - Rotation? none" << std::endl; break;
case DXGI_MODE_ROTATION_ROTATE90: std::wcout << L" - Rotation? 90 degrees" << std::endl; break;
case DXGI_MODE_ROTATION_ROTATE180: std::wcout << L" - Rotation? 180 degrees" << std::endl; break;
case DXGI_MODE_ROTATION_ROTATE270: std::wcout << L" - Rotation? 270 degrees" << std::endl; break;
}
auto format = DXGI_FORMAT_R8G8B8A8_UNORM;
auto flags = 0;
auto displayModeCount = 0U;
hr = output->GetDisplayModeList(format, flags, &displayModeCount, nullptr);
if (FAILED(hr) || displayModeCount == 0) {
j += 1;
continue;
}
auto displayModeList = std::make_unique<DXGI_MODE_DESC[]>(displayModeCount);
hr = output->GetDisplayModeList(format, flags, &displayModeCount, displayModeList.get());
if (FAILED(hr)) {
j += 1;
continue;
}
std::wcout << L" - Display modes:" << std::endl;
for (auto k = 0U; k < displayModeCount; k += 1) {
auto displayMode = displayModeList[k];
std::wcout
<< " - "
<< displayMode.Width << " x " << displayMode.Height << " @ "
<< (1.0F * displayMode.RefreshRate.Numerator / displayMode.RefreshRate.Denominator) << " Hz"
<< std::endl;
}
j += 1;
}
D3D_FEATURE_LEVEL maxSupportedFeatureLevel = D3D_FEATURE_LEVEL_9_1;
D3D_FEATURE_LEVEL featureLevels[] = {
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_2,
D3D_FEATURE_LEVEL_9_1
};
hr = D3D11CreateDevice(
adapter,
D3D_DRIVER_TYPE_UNKNOWN,
nullptr,
0,
featureLevels,
7,
D3D11_SDK_VERSION,
nullptr,
&maxSupportedFeatureLevel,
nullptr
);
if (hr == E_INVALIDARG) { // MSDN: "If you provide a D3D_FEATURE_LEVEL array that contains D3D_FEATURE_LEVEL_11_1 on a computer that doesn't have the Direct3D 11.1 runtime installed, this function immediately fails with E_INVALIDARG."
hr = D3D11CreateDevice(
adapter,
D3D_DRIVER_TYPE_UNKNOWN,
nullptr,
0,
&featureLevels[1],
6,
D3D11_SDK_VERSION,
nullptr,
&maxSupportedFeatureLevel,
nullptr
);
}
if (FAILED(hr)) {
Error error(L"D3D11CreateDevice failed", hr);
std::wcout << L"Warning: " << error.message1() << L". " << error.message2() << std::endl;
std::wcout << std::endl;
i += 1;
continue;
}
std::wcout << "- Max. supported feature level: ";
switch (maxSupportedFeatureLevel) {
case D3D_FEATURE_LEVEL_11_1: std::wcout << "11.1"; break;
case D3D_FEATURE_LEVEL_11_0: std::wcout << "11.0"; break;
case D3D_FEATURE_LEVEL_10_1: std::wcout << "10.1"; break;
case D3D_FEATURE_LEVEL_10_0: std::wcout << "10.0"; break;
case D3D_FEATURE_LEVEL_9_3: std::wcout << "9.3"; break;
case D3D_FEATURE_LEVEL_9_2: std::wcout << "9.2"; break;
case D3D_FEATURE_LEVEL_9_1: std::wcout << "9.1"; break;
default: std::wcout << "unknown (" << maxSupportedFeatureLevel << ")"; break;
}
std::wcout << std::endl;
std::wcout << std::endl;
i += 1;
}
factory->Release();
factory = nullptr;
}
catch (const Error& error) {
std::wcout << L"Error: " << error.message1() << ". " << error.message2() << std::endl;
}
std::wcout << L"Ready" << std::endl;
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment