Sign in to follow this  

DX11 Cant Initialize DX11 SwapChain and device

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
    • Total Posts
  • Similar Content

    • By joeblack
      im reading about specular aliasing because of mip maps, as far as i understood it, you need to compute fetched normal lenght and detect now its changed from unit length. I’m currently using BC5 normal maps, so i reconstruct z in shader and therefore my normals are normalized. Can i still somehow use antialiasing or its not needed? Thanks.
    • By 51mon
      I want to change the sampling behaviour to SampleLevel(coord, ddx(coord.y).xx, ddy(coord.y).xx). I was just wondering if it's possible without explicit shader code, e.g. with some flags or so?
    • By GalacticCrew
      I want to improve the performance of my game (engine) and some of your helped me to make a GPU Profiler. After creating the GPU Profiler, I started to measure the time my GPU needs per frame. I refined my GPU time measurements to find my bottleneck.
      Searching the bottleneck
      Rendering a small scene in an Idle state takes around 15.38 ms per frame. 13.54 ms (88.04%) are spent while rendering the scene, 1.57 ms (10.22%) are spent during the SwapChain.Present call (no VSync!) and the rest is spent on other tasks like rendering the UI. I further investigated the scene rendering, since it takes über 88% of my GPU frame rendering time.
      When rendering my scene, most of the time (80.97%) is spent rendering my models. The rest is spent to render the background/skybox, updating animation data, updating pixel shader constant buffer, etc. It wasn't really suprising that most of the time is spent for my models, so I further refined my measurements to find the actual bottleneck.
      In my example scene, I have five animated NPCs. When rendering these NPCs, most actions are almost for free. Setting the proper shaders in the input layout (0.11%), updating vertex shader constant buffers (0.32%), setting textures (0.24%) and setting vertex and index buffers (0.28%). However, the rest of the GPU time (99.05% !!) is spent in two function calls: DrawIndexed and DrawIndexedInstance.
      I searched this forum and the web for other articles and threads about these functions, but I haven't found a lot of useful information. I use SharpDX and .NET Framework 4.5 to develop my game (engine). The developer of SharpDX said, that "The method DrawIndexed in SharpDX is a direct call to DirectX" (Source). DirectX 11 is widely used and SharpDX is "only" a wrapper for DirectX functions, I assume the problem is in my code.
      How I render my scene
      When rendering my scene, I render one model after another. Each model has one or more parts and one or more positions. For example, a human model has parts like head, hands, legs, torso, etc. and may be placed in different locations (on the couch, on a street, ...). For static elements like furniture, houses, etc. I use instancing, because the positions never change at run-time. Dynamic models like humans and monster don't use instancing, because positions change over time.
      When rendering a model, I use this work-flow:
      Set vertex and pixel shaders, if they need to be updated (e.g. PBR shaders, simple shader, depth info shaders, ...) Set animation data as constant buffer in the vertex shader, if the model is animated Set generic vertex shader constant buffer (world matrix, etc.) Render all parts of the model. For each part: Set diffuse, normal, specular and emissive texture shader views Set vertex buffer Set index buffer Call DrawIndexedInstanced for instanced models and DrawIndexed models What's the problem
      After my GPU profiling, I know that over 99% of the rendering time for a single model is spent in the DrawIndexedInstanced and DrawIndexed function calls. But why do they take so long? Do I have to try to optimize my vertex or pixel shaders? I do not use other types of shaders at the moment. "Le Comte du Merde-fou" suggested in this post to merge regions of vertices to larger vertex buffers to reduce the number of Draw calls. While this makes sense to me, it does not explain why rendering my five (!) animated models takes that much GPU time. To make sure I don't analyse something I wrong, I made sure to not use the D3D11_CREATE_DEVICE_DEBUG flag and to run as Release version in Visual Studio as suggested by Hodgman in this forum thread.
      My engine does its job. Multi-texturing, animation, soft shadowing, instancing, etc. are all implemented, but I need to reduce the GPU load for performance reasons. Each frame takes less than 3ms CPU time by the way. So the problem is on the GPU side, I believe.
    • By noodleBowl
      I was wondering if someone could explain this to me
      I'm working on using the windows WIC apis to load in textures for DirectX 11. I see that sometimes the WIC Pixel Formats do not directly match a DXGI Format that is used in DirectX. I see that in cases like this the original WIC Pixel Format is converted into a WIC Pixel Format that does directly match a DXGI Format. And doing this conversion is easy, but I do not understand the reason behind 2 of the WIC Pixel Formats that are converted based on Microsoft's guide
      I was wondering if someone could tell me why Microsoft's guide on this topic says that GUID_WICPixelFormat40bppCMYKAlpha should be converted into GUID_WICPixelFormat64bppRGBA and why GUID_WICPixelFormat80bppCMYKAlpha should be converted into GUID_WICPixelFormat64bppRGBA
      In one case I would think that: 
      GUID_WICPixelFormat40bppCMYKAlpha would convert to GUID_WICPixelFormat32bppRGBA and that GUID_WICPixelFormat80bppCMYKAlpha would convert to GUID_WICPixelFormat64bppRGBA, because the black channel (k) values would get readded / "swallowed" into into the CMY channels
      In the second case I would think that:
      GUID_WICPixelFormat40bppCMYKAlpha would convert to GUID_WICPixelFormat64bppRGBA and that GUID_WICPixelFormat80bppCMYKAlpha would convert to GUID_WICPixelFormat128bppRGBA, because the black channel (k) bits would get redistributed amongst the remaining 4 channels (CYMA) and those "new bits" added to those channels would fit in the GUID_WICPixelFormat64bppRGBA and GUID_WICPixelFormat128bppRGBA formats. But also seeing as there is no GUID_WICPixelFormat128bppRGBA format this case is kind of null and void
      I basically do not understand why Microsoft says GUID_WICPixelFormat40bppCMYKAlpha and GUID_WICPixelFormat80bppCMYKAlpha should convert to GUID_WICPixelFormat64bppRGBA in the end
    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Dejay Hextrix 
  • Popular Now