Skip to content

Instantly share code, notes, and snippets.

@kaneta1992
Created October 25, 2017 02:53
Show Gist options
  • Save kaneta1992/8bb3d2a6a4eef090bea149964bd8d138 to your computer and use it in GitHub Desktop.
Save kaneta1992/8bb3d2a6a4eef090bea149964bd8d138 to your computer and use it in GitHub Desktop.
#include <d3d11.h>
D3D_DRIVER_TYPE g_driverType;
D3D_FEATURE_LEVEL g_featureLevel;
ID3D11Device* g_pd3dDevice = nullptr;
ID3D11DeviceContext* g_pImmediateContext = nullptr;
typedef HRESULT (__stdcall *FPD3D11CREATEDEVICE)(
IDXGIAdapter *pAdapter,
D3D_DRIVER_TYPE DriverType,
HMODULE Software,
UINT Flags,
CONST D3D_FEATURE_LEVEL *pFeatureLevels,
UINT FeatureLevels,
UINT SDKVersion,
ID3D11Device **ppDevice,
D3D_FEATURE_LEVEL *pFeatureLevel,
ID3D11DeviceContext **ppImmediateContext
);
HRESULT InitDevice()
{
HMODULE hModule = LoadLibrary("d3d11.dll");
if (hModule == NULL)
{
return S_FALSE;
}
FPD3D11CREATEDEVICE lpD3D11CreateDevice = (FPD3D11CREATEDEVICE)GetProcAddress(hModule, "D3D11CreateDevice");
if (lpD3D11CreateDevice == NULL)
{
FreeLibrary(hModule);
return S_FALSE;
}
HRESULT hr = S_OK;
UINT createDeviceFlags = 0;
#ifdef _DEBUG
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
D3D_DRIVER_TYPE driverTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE,
D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE,
};
UINT numDriverTypes = ARRAYSIZE(driverTypes);
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
};
UINT numFeatureLevels = ARRAYSIZE(featureLevels);
for (UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++)
{
g_driverType = driverTypes[driverTypeIndex];
hr = (*lpD3D11CreateDevice)(nullptr, g_driverType, nullptr, createDeviceFlags, featureLevels, numFeatureLevels,
D3D11_SDK_VERSION, &g_pd3dDevice, &g_featureLevel, &g_pImmediateContext);
if (SUCCEEDED(hr))
break;
}
D3D11_CREATE_DEVICE_FLAG flag = (D3D11_CREATE_DEVICE_FLAG)g_pd3dDevice->GetCreationFlags();
return hr;
}
void main()
{
InitDevice();
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment