I'm in a long battle with DirectX 11. Originally my aim was to do deferred shading (the original thread is here), but the code got a little hacky and a long story short, I started again trying to keep things clean and tidy.
Now my problem is with the creation of a device and swap chain using, D3D11CreateDeviceAndSwapChain(). It won't build me a context at feature level 11.
I'll try and explain everything I've tried and give you a code listing below.
I read somewhere that sometimes, especially on laptops the primary graphcis adapter is a low powered DX10.1 chip and the more powerful DX11 card has to be selected. I'm developing on a laptop so on these lines, I enumerated through all of the available graphics adapters using an IDXGIFactory and try to set up my D3D objects at feature level 11 on all of them until I succeed. It only returns 1 adapter before returning DXGI_ERROR_NOT_FOUND.
I extracted the adapters description in the debugger and it says it's an intel graphics family chip. On inspection of my device manager under Display Adapters I can see 2 adapters listed - this Intel® HD Graphics Family (that DX 11 seems to be able to find) and my NVIDIA GeForce GT 555M (which DX11 can't seem to find).
I've checked the NVidia website and my card is definitly fully DX 11 compatible. Infact I can run all of the DX11 samples in the SDK Sample browser!!! I've also made sure all of my drivers are up to date and still I can't get it build my D3D objects at feature level 11....
I've also tried letting DX try and pick the adapter for me by leaving the corresponding argument blank and simply specifying a feature level but it only manages feature level 10.1
So....any ideas on why it won't let me build my device and swap chain a feature leve 11??
Thanks very much in advance!
P.s. Here is all of my set up code so far. It just fills out the swap chain description, and creates a DXGI Factory. The creation code enumerates through the adapters trying to create everything at feature level 11, if it fails it moves to the next. As a last ditch if it really can't do it - it'll go for software emulation but let you opt out if you want.
HRESULT result = S_OK;
D3D_DRIVER_TYPE driverType;
// a hardware driver is preferred so this is default.
driverType = D3D_DRIVER_TYPE_HARDWARE;
//driverType = D3D_DRIVER_TYPE_REFERENCE;
IDXGIFactory* factory = NULL;
IDXGIAdapter* adapter = NULL;
// this will be used to step through the devices to find a DX11 compatible one
result = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&factory);
if(FAILED(result))
{
m_crashed = true;
return result;
}
DXGI_SWAP_CHAIN_DESC swapChainDesc;
D3D_FEATURE_LEVEL featureLevel;
// Initialize the swap chain description.
ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
// Set to a single back buffer.
swapChainDesc.BufferCount = 1;
// Set the width and height of the back buffer.
swapChainDesc.BufferDesc.Width = screenWidth;
swapChainDesc.BufferDesc.Height = screenHeight;
// Set regular 32-bit surface for the back buffer.
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
// TODO:FLAG - These can be altered to make the rendering match the refresh rate of the hardware
swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
// Set the usage of the back buffer.
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
// Set the handle for the window to render to.
swapChainDesc.OutputWindow = m_hWnd;
// Turn multisampling off.
swapChainDesc.SampleDesc.Count = D3D_SAMPLE_DESC_COUNT;
swapChainDesc.SampleDesc.Quality = D3D_SAMPLE_DESC_QUALITY;
swapChainDesc.Windowed = !fullscreen;
// Set the scan line ordering and scaling to unspecified.
swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
// Discard the back buffer contents after presenting.
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
// Don't set the advanced flags.
swapChainDesc.Flags = 0;
D3D_FEATURE_LEVEL targetLevels[] =
{
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,
};
int levelCount = 1;
// initialise to true, as if there aren't any adapters - then we've failed to create the device and swap chain
bool failedToCreateDeviceAndSwapChain = true;
char videoCardDescription[128];
DXGI_ADAPTER_DESC adapterDesc;
unsigned int stringLength;
// step through the adapters until we find a compatible adapter and successfully create a device and swap chain
for(int i = 0; factory->EnumAdapters(i, &adapter) != DXGI_ERROR_NOT_FOUND; i++)
{
failedToCreateDeviceAndSwapChain = false;
adapter->GetDesc(&adapterDesc);
int error = wcstombs_s(&stringLength, videoCardDescription, 128, adapterDesc.Description, 128);
driverType = D3D_DRIVER_TYPE_UNKNOWN;// as we're specifying an adapter to use, we must specify that the driver type is unknown!!!
result = D3D11CreateDeviceAndSwapChain(
adapter,
driverType,
NULL,
0,
targetLevels,
levelCount,
D3D11_SDK_VERSION,
&swapChainDesc,
&m_pSwapChain,
&m_pDevice,
&featureLevel,
&m_pDeviceContext);
// this device failed to create the devie and swap chain - ensure no handles are left by safely releasing what we were trying to create,
// set the flag to fail and then try again with the next device
if(FAILED(result))
{
SafeRelease(m_pSwapChain);
SafeRelease(m_pDevice);
SafeRelease(m_pDeviceContext);
failedToCreateDeviceAndSwapChain = true;
// try again with the next device
continue;
}
// if we've reached this point then a compatible graphics device must've been found so break from the loop
break;
}
// after looping through the devices we still couldn't find what we needed, therefore see if the user will use software emulation as a last attempt
if(failedToCreateDeviceAndSwapChain)
{
int messageAnswer = MessageBox(
m_hWnd,
"No compatible graphics hardware was detected. Do you want to use software emulation instead? Note: This will be very slow!",
"No compatible graphics hardware",
MB_YESNO | MB_ICONWARNING);
// the user doesn't want to use software emulation - therefore initialisation has failed. Quit here
if(messageAnswer == IDNO)
{
m_crashed = true;
return E_FAIL;
}
// the user is happy to use software emulation so make a final attempt at creating the device and swap chain at the desired level using the
// reference device
driverType = D3D_DRIVER_TYPE_REFERENCE;
result = D3D11CreateDeviceAndSwapChain(
NULL,
driverType,
NULL,
0,
targetLevels,
levelCount,
D3D11_SDK_VERSION,
&swapChainDesc,
&m_pSwapChain,
&m_pDevice,
&featureLevel,
&m_pDeviceContext);
}
// Ensure all temporary COM objects are released
SafeRelease(factory);
return S_OK;






