Sign in to follow this  
bushmanpaul

DX11 Cant Initialize DX11 SwapChain and device

Recommended Posts

bushmanpaul    100
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
Nik02    4348
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
bushmanpaul    100
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;
#endif

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
Nik02    4348
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
bushmanpaul    100
Again thanks for the effort

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

hResult = D3D11CreateDevice( NULL,
D3D_DRIVER_TYPE_REFERENCE,
NULL,
createDeviceFlags,
&featureLevels[0],
numFeatureLevels,
D3D11_SDK_VERSION,
&pDevice,
&featureLevelOut,
&pContext);

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
bushmanpaul    100
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

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  

  • Similar Content

    • By Modymek
      hi all
      I want to enable and disable shader in MPCH Media player Classic
      the MPCH have shader option using HLSL shaders
      I want the shader to read each file extension before it plays the file
      so if the video file name is video.GR.Mp4 it will play it in Grayscale shader 
      if it is not and standard file name Video.Mp4 without GR. unique extension so it plays standard without shader or end the shader
      here is the shader I have for grayscale
      // $MinimumShaderProfile: ps_2_0
      sampler s0 : register(s0);
      float4 main(float2 tex : TEXCOORD0) : COLOR {
          float c0 = dot(tex2D(s0, tex), float4(0.299, 0.587, 0.114, 0));
          return c0;
      }
       
      I want to add if or block stantement or bloean to detect file name before it call the shader in order to go to the procedure or disable it or goto end direct without it
       
      any thoughts or help
    • By noodleBowl
      I've gotten to part in my DirectX 11 project where I need to pass the MVP matrices to my vertex shader. And I'm a little lost when it comes to the use of the constant buffer with the vertex shader
      I understand I need to set up the constant buffer just like any other buffer:
      1. Create a buffer description with the D3D11_BIND_CONSTANT_BUFFER flag 2. Map my matrix data into the constant buffer 3. Use VSSetConstantBuffers to actually use the buffer But I get lost at the VertexShader part, how does my vertex shader know to use this constant buffer when we get to the shader side of things
      In the example I'm following I see they have this as their vertex shader, but I don't understand how the shader knows to use the MatrixBuffer cbuffer. They just use the members directly. What if there was multiple cbuffer declarations like the Microsoft documentation says you could have?
      //Inside vertex shader cbuffer MatrixBuffer { matrix worldMatrix; matrix viewMatrix; matrix projectionMatrix; }; struct VertexInputType { float4 position : POSITION; float4 color : COLOR; }; struct PixelInputType { float4 position : SV_POSITION; float4 color : COLOR; }; PixelInputType ColorVertexShader(VertexInputType input) { PixelInputType output; // Change the position vector to be 4 units for proper matrix calculations. input.position.w = 1.0f; // Calculate the position of the vertex against the world, view, and projection matrices. output.position = mul(input.position, worldMatrix); output.position = mul(output.position, viewMatrix); output.position = mul(output.position, projectionMatrix); // Store the input color for the pixel shader to use. output.color = input.color; return output; }  
    • By gomidas
      I am trying to add normal map to my project I have an example of a cube: 
      I have normal in my shader I think. Then I set shader resource view for texture (NOT BUMP)
                  device.ImmediateContext.PixelShader.SetShaderResource(0, textureView);             device.ImmediateContext.Draw(VerticesCount,0); What should I do to set my normal map or how it is done in dx11 generally example c++?
    • By fighting_falcon93
      Imagine that we have a vertex structure that looks like this:
      struct Vertex { XMFLOAT3 position; XMFLOAT4 color; }; The vertex shader looks like this:
      cbuffer MatrixBuffer { matrix world; matrix view; matrix projection; }; struct VertexInput { float4 position : POSITION; float4 color : COLOR; }; struct PixelInput { float4 position : SV_POSITION; float4 color : COLOR; }; PixelInput main(VertexInput input) { PixelInput output; input.position.w = 1.0f; output.position = mul(input.position, world); output.position = mul(output.position, view); output.position = mul(output.position, projection); output.color = input.color; return output; } And the pixel shader looks like this:
      struct PixelInput { float4 position : SV_POSITION; float4 color : COLOR; }; float4 main(PixelInput input) : SV_TARGET { return input.color; } Now let's create a quad consisting of 2 triangles and the vertices A, B, C and D:
      // Vertex A. vertices[0].position = XMFLOAT3(-1.0f, 1.0f, 0.0f); vertices[0].color = XMFLOAT4( 0.5f, 0.5f, 0.5f, 1.0f); // Vertex B. vertices[1].position = XMFLOAT3( 1.0f, 1.0f, 0.0f); vertices[1].color = XMFLOAT4( 0.5f, 0.5f, 0.5f, 1.0f); // Vertex C. vertices[2].position = XMFLOAT3(-1.0f, -1.0f, 0.0f); vertices[2].color = XMFLOAT4( 0.5f, 0.5f, 0.5f, 1.0f); // Vertex D. vertices[3].position = XMFLOAT3( 1.0f, -1.0f, 0.0f); vertices[3].color = XMFLOAT4( 0.5f, 0.5f, 0.5f, 1.0f); // 1st triangle. indices[0] = 0; // Vertex A. indices[1] = 3; // Vertex D. indices[2] = 2; // Vertex C. // 2nd triangle. indices[3] = 0; // Vertex A. indices[4] = 1; // Vertex B. indices[5] = 3; // Vertex D. This will result in a grey quad as shown in the image below. I've outlined the edges in red color to better illustrate the triangles:

      Now imagine that we’d want our quad to have a different color in vertex A:
      // Vertex A. vertices[0].position = XMFLOAT3(-1.0f, 1.0f, 0.0f); vertices[0].color = XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f);
      That works as expected since there’s now an interpolation between the black color in vertex A and the grey color in vertices B, C and D. Let’s revert the previus changes and instead change the color of vertex C:
      // Vertex C. vertices[2].position = XMFLOAT3(-1.0f, -1.0f, 0.0f); vertices[2].color = XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f);
      As you can see, the interpolation is only done half of the way across the first triangle and not across the entire quad. This is because there's no edge between vertex C and vertex B.
      Which brings us to my question:
      I want the interpolation to go across the entire quad and not only across the triangle. So regardless of which vertex we decide to change the color of, the color interpolation should always go across the entire quad. Is there any efficient way of achieving this without adding more vertices and triangles?
      An illustration of what I'm trying to achieve is shown in the image below:

       
      Background
      This is just a very brief explanation of the problems background in case that would make it easier for you to understand the problems roots and maybe help you with finding a better solution to the problem.
      I'm trying to texture a terrain mesh in DirectX11. It's working, but I'm a bit unsatisfied with the result. When changing the terrain texture of a single vertex, the interpolation with the other vertices results in a hexagon shape instead of a squared shape:

      As the red arrows illustrate, I'd like the texture to be interpolated all the way into the corners of the quads.
    • By -Tau-
      Hello, I'm close to releasing my first game to Steam however, my game keeps failing the review process because it keeps crashing. The problem is that the game doesn't crash on my computer, on my laptop, on our family computer, on fathers laptop and i also gave 3 beta keys to people i know and they said the game hasn't crashed.
      Steam reports that the game doesn't crash on startup but few frames after a level has been started.
      What could cause something like this? I have no way of debugging this as the game works fine on every computer i have.
       
      Game is written in C++, using DirectX 11 and DXUT framework.
  • Popular Now