• Advertisement
Sign in to follow this  

DX11 Cant Initialize DX11 SwapChain and device

This topic is 2891 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
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Fleshbits
      Back around 2006 I spent a good year or two reading books, articles on this site, and gobbling up everything game dev related I could. I started an engine in DX10 and got through basics. I eventually gave up, because I couldn't do the harder things.
      Now, my C++ is 12 years stronger, my mind is trained better, and I am thinking of giving it another go.
      Alot has changed. There is no more SDK, there is evidently a DX Toolkit, XNA died, all the sweet sites I used to go to are 404, and google searches all point to Unity and Unreal.
      I plainly don't like Unity or Unreal, but might learn them for reference.
      So, what is the current path? Does everyone pretty much use the DX Toolkit? Should I start there? I also read that DX12 is just expert level DX11, so I guess I am going DX 11.
      Is there a current and up to date list of learning resources anywhere?  I am about tired of 404s..
    • By Stewie.G
      I've been trying to implement a basic gaussian blur using the gaussian formula, and here is what it looks like so far:
      float gaussian(float x, float sigma)
          float pi = 3.14159;
          float sigma_square = sigma * sigma;
          float a = 1 / sqrt(2 * pi*sigma_square);
          float b = exp(-((x*x) / (2 * sigma_square)));
          return a * b;
      My problem is that I don't quite know what sigma should be.
      It seems that if I provide a random value for sigma, weights in my kernel won't add up to 1.
      So I ended up calling my gaussian function with sigma == 1, which gives me weights adding up to 1, but also a very subtle blur.
      Here is what my kernel looks like with sigma == 1
              [0]    0.0033238872995488885    
              [1]    0.023804742479357766    
              [2]    0.09713820127276819    
              [3]    0.22585307043511713    
              [4]    0.29920669915475656    
              [5]    0.22585307043511713    
              [6]    0.09713820127276819    
              [7]    0.023804742479357766    
              [8]    0.0033238872995488885    
      I would have liked it to be more "rounded" at the top, or a better spread instead of wasting [0], [1], [2] with values bellow 0.1.
      Based on my experiments, the key to this is to provide a different sigma, but if I do, my kernel values no longer adds up to 1, which results to a darker blur.
      I've found this post 
      ... which helped me a bit, but I am really confused with this the part where he divide sigma by 3.
      Can someone please explain how sigma works? How is it related to my kernel size, how can I balance my weights with different sigmas, ect...
      Thanks :-)
    • By mc_wiggly_fingers
      Is it possible to asynchronously create a Texture2D using DirectX11?
      I have a native Unity plugin that downloads 8K textures from a server and displays them to the user for a VR application. This works well, but there's a large frame drop when calling CreateTexture2D. To remedy this, I've tried creating a separate thread that creates the texture, but the frame drop is still present.
      Is there anything else that I could do to prevent that frame drop from occuring?
    • By cambalinho
      i'm trying draw a circule using math:
      class coordenates { public: coordenates(float x=0, float y=0) { X = x; Y = y; } float X; float Y; }; coordenates RotationPoints(coordenates ActualPosition, double angle) { coordenates NewPosition; NewPosition.X = ActualPosition.X*sin(angle) - ActualPosition.Y*sin(angle); NewPosition.Y = ActualPosition.Y*cos(angle) + ActualPosition.X*cos(angle); return NewPosition; } but now i know that these have 1 problem, because i don't use the orign.
      even so i'm getting problems on how i can rotate the point.
      these coordinates works between -1 and 1 floating points.
      can anyone advice more for i create the circule?
    • By isu diss
      I managed convert opengl code on http://john-chapman-graphics.blogspot.co.uk/2013/02/pseudo-lens-flare.html to hlsl, but unfortunately I don't know how to add it to my atmospheric scattering code (Sky - first image). Can anyone help me?
      I tried to bind the sky texture as SRV and implement lens flare code in pixel shader, I don't know how to separate them (second image)

  • Advertisement