Jump to content
  • Advertisement
Sign in to follow this  

DX11 Cant Initialize DX11 SwapChain and device

This topic is 3017 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I programmed in DX10 for quite awhile (self taught) and recently switched over to DX11 and my Device and SwapChain wont Initialize. here the relevant code bool InitDirect3D(HWND hWnd) { //driver Types const D3D_DRIVER_TYPE driverTypes[] = { D3D_DRIVER_TYPE_HARDWARE,D3D_DRIVER_TYPE_WARP,D3D_DRIVER_TYPE_REFERENCE}; const UINT numDriverTypes = _countof( driverTypes ); D3D_DRIVER_TYPE driverType = D3D_DRIVER_TYPE_REFERENCE; //Supperted feature levels D3D_FEATURE_LEVEL featureLevelOut; DXGI_SWAP_CHAIN_DESC swapChainDesc; ZeroMemory(&swapChainDesc, sizeof(swapChainDesc)); swapChainDesc.BufferCount = 1; swapChainDesc.BufferDesc.Width = 640; swapChainDesc.BufferDesc.Height = 480; swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; swapChainDesc.BufferDesc.RefreshRate.Numerator = 60; swapChainDesc.BufferDesc.RefreshRate.Denominator = 1; swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; swapChainDesc.OutputWindow = hWnd; swapChainDesc.SampleDesc.Count = 1; swapChainDesc.SampleDesc.Quality = 0; swapChainDesc.Windowed = TRUE; HRESULT hResult = S_OK; DeviceSettings deviceSettings; ZeroMemory(&deviceSettings, sizeof(DeviceSettings)); deviceSettings.createFlags = NULL; deviceSettings.driverType = D3D_DRIVER_TYPE_REFERENCE; deviceSettings.featureLevel = D3D_FEATURE_LEVEL_11_0; deviceSettings.swapChainDesc = swapChainDesc; D3D_FEATURE_LEVEL featureLevels = D3D_FEATURE_LEVEL_11_0; hResult = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_REFERENCE, NULL, 0, &featureLevels, 1, D3D11_SDK_VERSION, &swapChainDesc, &pSwapChain, &pDevice, &featureLevelOut, &pContext); //Check 4 errors if(hResult == D3D11_ERROR_FILE_NOT_FOUND){MessageBox(hWnd,L"D3D11_ERROR_FILE_NOT_FOUND",L"Error",MB_OK);return false;} if(hResult == D3D11_ERROR_TOO_MANY_UNIQUE_STATE_OBJECTS){MessageBox(hWnd,L"D3D11_ERROR_TOO_MANY_UNIQUE_STATE_OBJECTS",L"Error",MB_OK);return false;} if(hResult == D3D11_ERROR_TOO_MANY_UNIQUE_VIEW_OBJECTS){MessageBox(hWnd,L"D3D11_ERROR_TOO_MANY_UNIQUE_VIEW_OBJECTS",L"Error",MB_OK);return false;} if(hResult == D3D11_ERROR_DEFERRED_CONTEXT_MAP_WITHOUT_INITIAL_DISCARD){MessageBox(hWnd,L"D3D11_ERROR_DEFERRED_CONTEXT_MAP_WITHOUT_INITIAL_DISCARD",L"Error",MB_OK);return false;} if(hResult == D3DERR_INVALIDCALL){MessageBox(hWnd,L"D3DERR_INVALIDCALL",L"Error",MB_OK);return false;} if(hResult == D3DERR_WASSTILLDRAWING){MessageBox(hWnd,L"D3DERR_WASSTILLDRAWING",L"Error",MB_OK);return false;} if(hResult == E_FAIL){MessageBox(hWnd,L"E_FAIL",L"Error",MB_OK);return false;} if(hResult == E_INVALIDARG){MessageBox(hWnd,L"E_INVALIDARG",L"Error",MB_OK);return false;} if(hResult == E_OUTOFMEMORY){MessageBox(hWnd,L"E_OUTOFMEMORY",L"Error",MB_OK);return false;} if(hResult == S_FALSE){MessageBox(hWnd,L"S_FALSE",L"Error",MB_OK);return false;} if(hResult == S_OK){MessageBox(hWnd,L"S_OK",L"Works",MB_OK);return false;} if(FAILED(hResult)){MessageBox(hWnd,L"Couldn't create Device and SwapChain",L"Error",MB_OK);return false;} ID3D11Texture2D* pBackBuffer = NULL; hResult = pSwapChain->GetBuffer( 0, __uuidof( *pBackBuffer ), ( LPVOID* )&pBackBuffer ); if(FAILED(hResult)) {MessageBox(hWnd,L"Couldn't create Back buffer",L"Error",MB_OK);return false;}; hResult = pDevice->CreateRenderTargetView(pBackBuffer,NULL,&pCamera); pBackBuffer->Release(); if(FAILED(hResult)) return false; pContext->OMSetRenderTargets(1,&pCamera,NULL); D3D11_VIEWPORT viewPort; viewPort.Width = (float)Width; viewPort.Height = (float)Height; viewPort.MinDepth = 0.0f; viewPort.MaxDepth = 1.0f; viewPort.TopLeftX = 0; viewPort.TopLeftY = 0; pContext->RSSetViewports(1,&viewPort); D3DXMatrixIdentity(&WorldMatrix); D3DXMatrixLookAtLH(&ViewMatrix,new D3DXVECTOR3(0.0f,96,-300.0f),new D3DXVECTOR3(0.0f,0.0f,1.0f),new D3DXVECTOR3(0.0f,1.0f,0.0f)); D3DXMatrixPerspectiveFovLH(&ProjectionMatrix,(float)D3DX_PI*0.5f,(float)Width/(float)Height,0.0f,500.0f); D3D11_RASTERIZER_DESC rasterDesc; rasterDesc.FillMode = D3D11_FILL_SOLID; rasterDesc.CullMode = D3D11_CULL_NONE; rasterDesc.DepthBias = false; rasterDesc.DepthBiasClamp = 0; rasterDesc.SlopeScaledDepthBias = 0; rasterDesc.DepthClipEnable = false; rasterDesc.ScissorEnable = false; rasterDesc.MultisampleEnable = false; rasterDesc.AntialiasedLineEnable = false; ID3D11RasterizerState *pRasterState; pDevice->CreateRasterizerState(&rasterDesc,&pRasterState); pContext->RSSetState(pRasterState); return true; } Here is the relevant Variables of the program HWND hWnd; ID3D11Device* pDevice = NULL; static IDXGISwapChain* pSwapChain = NULL; static ID3D11RenderTargetView* pCamera = NULL; static ID3D11DeviceContext* pContext = NULL; static IDXGIAdapter* pAdapter = NULL; D3D_FEATURE_LEVEL featureLevels[] = {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}; UINT numFeatureLevels = _countof(featureLevels); and ofcourse the relevant header info #ifndef WINDOWS_H #define WINDOWS_H #include <windows.h> #endif #ifndef DIRECTX_INCLUDE #define DIRECTX_INCLUDE #include <d3dx11.h> #include <d3d11.h> #include <d3dx9.h> #endif extern HWND hWnd; extern ID3D11Device* pDevice; extern IDXGISwapChain* pSwapChain; extern ID3D11RenderTargetView* pCamera; extern ID3D11DeviceContext* pContext; extern ID3D11Texture2D* pBackBuffer; extern D3DXMATRIX WorldMatrix; extern D3DXMATRIX ViewMatrix; extern D3DXMATRIX ProjectionMatrix; bool InitDirect3D(HWND hWnd,int Width,int Height); typedef struct DeviceSettings { UINT adapter; D3D_DRIVER_TYPE driverType; DXGI_SWAP_CHAIN_DESC swapChainDesc; UINT32 createFlags; D3D_FEATURE_LEVEL featureLevel; } DeviceSettings; Any feedback will be much apreciated I can get it to work if I use DXUT so it shouldn't be Incompatibility issue but I may be wrong.

Share this post

Link to post
Share on other sites
The FeatureLevels parameter of the CreateDevice call expects a pointer to an array of requested feature levels. As of now, you use a pointer to a single feature level flag, which will probably result in E_INVALIDARG. Note that you have two different variables called "featureLevels", but they are in different scope so the one in the same scope as the call will be used.

-The array can have only one element, if you don't need to fall back to other levels.

-Create your device with the debug flag when doing the initial development of your program. Switch the debug flag off only when either performance profiling your app or releasing it.

-You have a redundant structure, DeviceSettings, that you don't seem to actually use to initialize the device. It is always a bad idea to hold several manual copies of the same data, because it is easy to forget to modify one of them if you need to modify an another one for some reason.

Share this post

Link to post
Share on other sites
Thanks for the help nik02

But it still doesn't work

I removed both featureLevels and added this to the Code

D3D_FEATURE_LEVEL featureLevels[] = {D3D_FEATURE_LEVEL_11_0};
UINT numFeatureLevels = _countof( featureLevels );

I also added Debug flags like so

#ifdef DEBUG
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;

and in my header

#define DEBUG

I originally planned to use the deviceSettings to initialize a device and ultimatly send it to a method that initializes it so that I can make Multiple devices a bit quicker but I commented It out for now

You also said that it should result in E_INVALIDARG but it immediately outputs the message box that it can't create swapChain and Device shouldn't

if(hResult == E_INVALIDARG){MessageBox(hWnd,L"E_INVALIDARG",L"Error",MB_OK);return false;}

pick it up.

Share this post

Link to post
Share on other sites
I don't spot any more obvious errors in the code. (That doesn't mean that there isn't any).

-Are you sure that the HWND you use with the CreateDevice function is valid?
Try to call ShowWindow before calling the code you display here.
-What is the type of the window you bind the swapchain to?
-Does the plain CreateDevice call succeed (the one without "AndSwapChain")?

Share this post

Link to post
Share on other sites
Again thanks for the effort

I commented out my CreateDevice and SwapChain and replaced it with this.

hResult = D3D11CreateDevice( NULL,

It works and ends the method with

if(hResult == S_OK){MessageBox(hWnd,L"S_OK",L"Works",MB_OK);return false;}

otherwise it will complain about not having a swapchain.

also I also picked up a typo where a reversed height and width when creating the window.

I am not sure how to check for a invalid hWnd but I tried.So while trying I found out that the I initialize HWND in a local scope and my Global scope one which I pass it remanes unchanged.

I will quickly fix it then come post another reply

[Edited by - bushmanpaul on March 22, 2010 2:54:59 AM]

Share this post

Link to post
Share on other sites
I fixed my problem and removed createdevice and uncommented my original CreateDeviceAndSwapChain and everything works.

Thanks alot would never have done it without your help Nik02.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!