Direct x failing

Started by
10 comments, last by thefatshizms 10 years, 9 months ago

I have the following code:


HRESULT result = S_OK;
	ID3D11Device* g_pd3dDevice = NULL;
	IDXGISwapChain* g_pSwapChain = NULL;
	ID3D11DeviceContext* g_pImmediateContext = NULL;
	D3D_FEATURE_LEVEL	FeatureLevelsSupported;

	RECT deminsions;
	GetClientRect(hwnd, &deminsions);
	LONG width = deminsions.right - deminsions.left;
	LONG height = deminsions.top - deminsions.bottom;

	DXGI_SWAP_CHAIN_DESC sd;
	ZeroMemory( &sd, sizeof( sd ) );
	sd.BufferCount = 1;
	sd.BufferDesc.Width = static_cast<unsigned int>(width);
	sd.BufferDesc.Height = static_cast<unsigned int>(height);
	sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	sd.BufferDesc.RefreshRate.Numerator = 60;
	sd.BufferDesc.RefreshRate.Denominator = 1;
	sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	sd.OutputWindow = hwnd;
	sd.SampleDesc.Count = 1;
	sd.SampleDesc.Quality = 0;
	sd.Windowed = TRUE;

	D3D_FEATURE_LEVEL FeatureLevelsRequested[] =
	{
		D3D_FEATURE_LEVEL_9_1,
		D3D_FEATURE_LEVEL_9_2,
		D3D_FEATURE_LEVEL_9_3,
		D3D_FEATURE_LEVEL_10_0,
		D3D_FEATURE_LEVEL_10_1,
		D3D_FEATURE_LEVEL_11_0
	};

	result = D3D11CreateDeviceAndSwapChain( NULL, 
                D3D_DRIVER_TYPE_HARDWARE, 
                NULL, 
                0,
                FeatureLevelsRequested, 
                3, 
                D3D11_SDK_VERSION, 
                &sd, 
                &g_pSwapChain, 
                &g_pd3dDevice, 
                &FeatureLevelsSupported,
                &g_pImmediateContext );

	if(FAILED(result)) return -1;


	ID3D11RenderTargetView* p_BackBufferTarget = NULL;
	ID3D11Texture2D* p_RT;

	result = g_pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&p_RT);
	if(FAILED(result)) return -2;

	result = g_pd3dDevice->CreateRenderTargetView(p_RT, 0, &p_BackBufferTarget);
	if(FAILED(result)) return -3;
	if(p_RT) p_RT->Release(); p_RT = NULL;
	g_pImmediateContext->OMSetRenderTargets(1, &p_BackBufferTarget, 0);

	D3D11_VIEWPORT viewport;
	viewport.Height = static_cast<float>(height);
	viewport.Width = static_cast<float>(width);
	viewport.MaxDepth = 1.0f;
	viewport.MinDepth = 0.0f;
	viewport.TopLeftX = 0.0f;
	viewport.TopLeftY = 0.0f;

	g_pImmediateContext->RSSetViewports(1, &viewport);

    UpdateWindow(hwnd);

the program fails and returns -1 which means there is something wrong with this:


result = D3D11CreateDeviceAndSwapChain( NULL, 
                D3D_DRIVER_TYPE_HARDWARE, 
                NULL, 
                0,
                FeatureLevelsRequested, 
                3, 
                D3D11_SDK_VERSION, 
                &sd, 
                &g_pSwapChain, 
                &g_pd3dDevice, 
                &FeatureLevelsSupported,
                &g_pImmediateContext );

But I'm not sure what?

Thanks, thefatshizms

Advertisement

Try to create the device with the debug flag and check your output window and see what the error says.

This may be a dull question, but do you have a dedicated graphics card that supports DX11? You may want to try D3D_DRIVER_TYPE_SOFTWARE as the second parameter...

Disclaimer: it's been a while since I used DirectX outside of C# though. So I may be way off base.

I believe the problem is that when you are calculating the height, you should be doing (bottom - top), not (top - bottom).

Also, two other things:

  • You are passing in 3 as the feature level count to D3D11CreateDeviceAndSwapChain, meaning it will only look at the first 3 elements of your feature level array.
  • Your feature level array is backwards. It will attempt to use feature level 9.1 first, then 9.2, etc.

Okay changed the height calculation, changed the feature level count to 6 and re arranged the array. Still crashes.

Using the debug flag now.. and this is the output :


'ITE.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\user32.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\d3d11.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\dxgi.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\version.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\dwmapi.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
'ITE.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\uxtheme.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\d3d10level9.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\setupapi.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\cfgmgr32.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\oleaut32.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\devobj.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\wintrust.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\crypt32.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\msasn1.dll', Cannot find or open the PDB file
'ITE.exe': Unloaded 'C:\Windows\SysWOW64\setupapi.dll'
'ITE.exe': Unloaded 'C:\Windows\SysWOW64\devobj.dll'
'ITE.exe': Unloaded 'C:\Windows\SysWOW64\oleaut32.dll'
'ITE.exe': Unloaded 'C:\Windows\SysWOW64\cfgmgr32.dll'
'ITE.exe': Loaded 'C:\Windows\SysWOW64\aticfx32.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\atiu9pag.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\atiumdag.dll', Cannot find or open the PDB file
'ITE.exe': Loaded 'C:\Windows\SysWOW64\atiumdva.dll', Cannot find or open the PDB file
First-chance exception at 0x760dc41f in ITE.exe: Microsoft C++ exception: _com_error at memory location 0x0024f520..
'ITE.exe': Unloaded 'C:\Windows\SysWOW64\atiumdva.dll'
'ITE.exe': Unloaded 'C:\Windows\SysWOW64\atiumdag.dll'
'ITE.exe': Unloaded 'C:\Windows\SysWOW64\aticfx32.dll'
'ITE.exe': Unloaded 'C:\Windows\SysWOW64\d3d10level9.dll'
First-chance exception at 0x00c417f1 in ITE.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x774215de in ITE.exe: 0xC0000005: Access violation reading location 0x00000000.

On the note that my graphics card might not be able to run direct x 11 .. I'm not actually sure on that.. I have a AMD Radeon HD something. I can run far cry 3 (which I think is directx 11).

Have you tried stepping through with the debugger to ensure that things are behaving as expected? (ex. hwnd is not null, width and height have the expected values, etc.) Is it still failing at the same spot?

Also, you have this:


result = g_pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&p_RT);

I think you want ID3D11Texture2D there, not ID3D10Texture2D.

Here is the full code:

/*
 
Window.h Creates and instalises direct x
*/
 
// make the window
#include <Windows.h>
 
// incude direct x 11
#pragma comment(lib, "d3d11.lib")
#include <d3d11.h>
 
// we need to use some math functions
#include <cmath>
 
 
// forward functions
bool InitializeDirect3dApp(HINSTANCE hInstance);
 
const char WindowClass[] = "GameWindow";
 
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
 
wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = WindowClass;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
 
if(RegisterClassEx(&wc) == NULL) 
MessageBox(NULL, "ERROR : Failed to register the windows properties.", "ERROR", MB_ICONEXCLAMATION  | MB_OK);
 
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, WindowClass, "Inhabiting The Edge", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 1100, 700, NULL, NULL, hInstance, NULL); 
 
if(hwnd == NULL)
MessageBox(NULL, "ERROR : Failed to create window.", "ERROR", MB_ICONEXCLAMATION | MB_OK);
 
 
HRESULT result = S_OK;
ID3D11Device* g_pd3dDevice = NULL;
IDXGISwapChain* g_pSwapChain = NULL;
ID3D11DeviceContext* g_pImmediateContext = NULL;
D3D_FEATURE_LEVEL FeatureLevelsSupported;
 
RECT deminsions;
GetClientRect(hwnd, &deminsions);
LONG width = deminsions.right - deminsions.left;
LONG height = deminsions.bottom - deminsions.top;
 
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory( &sd, sizeof( sd ) );
sd.BufferCount = 1;
sd.BufferDesc.Width = static_cast<unsigned int>(width);
sd.BufferDesc.Height = static_cast<unsigned int>(height);
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = hwnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
 
D3D_FEATURE_LEVEL FeatureLevelsRequested[] =
{
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
};
 
result = D3D11CreateDeviceAndSwapChain( NULL, 
                D3D_DRIVER_TYPE_HARDWARE, 
                NULL, 
                D3D11_CREATE_DEVICE_DEBUG,
                FeatureLevelsRequested, 
                6, 
                D3D11_SDK_VERSION, 
                &sd, 
                &g_pSwapChain, 
                &g_pd3dDevice, 
                &FeatureLevelsSupported,
                &g_pImmediateContext );
 
 
 
 
ID3D11RenderTargetView* p_BackBufferTarget = NULL;
ID3D11Texture2D* p_RT;
 
result = g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&p_RT);
if(FAILED(result)) return -2;
 
result = g_pd3dDevice->CreateRenderTargetView(p_RT, 0, &p_BackBufferTarget);
if(FAILED(result)) return -3;
if(p_RT) p_RT->Release(); p_RT = NULL;
g_pImmediateContext->OMSetRenderTargets(1, &p_BackBufferTarget, 0);
 
D3D11_VIEWPORT viewport;
viewport.Height = static_cast<float>(height);
viewport.Width = static_cast<float>(width);
viewport.MaxDepth = 1.0f;
viewport.MinDepth = 0.0f;
viewport.TopLeftX = 0.0f;
viewport.TopLeftY = 0.0f;
 
g_pImmediateContext->RSSetViewports(1, &viewport);
ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
 
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
 
 
return msg.wParam;
 
}

Is the issue resolved? Your code runs fine here. If not, which OS are you on?

I'm on windows 7 ultimate(64 bit). If it runs okay on your system, it probably means I can't run direct x 11 or something :/

I'm on windows 7 ultimate(64 bit). If it runs okay on your system, it probably means I can't run direct x 11 or something :/

Did you even try to debug it yourself before comming to this conclusion. Did you try to run any of the DX11 Samples and see whether it is using Hardware or Ref mode.

This topic is closed to new replies.

Advertisement