Sign in to follow this  
PhillipClark

Program crash when ppSwapChain->GetBuffer

Recommended Posts

[code]#include <windows.h>
#include <d3d11.h>

HWND hwnd;
HINSTANCE hInstance;
D3D_FEATURE_LEVEL pFeatureLevel;
IDXGISwapChain *ppSwapChain;
ID3D11Device *ppDevice;
ID3D11DeviceContext *ppImmediateContext;
ID3D11RenderTargetView *pRenderTargetView;

void InitWindow( HINSTANCE hInstance, int nCmdShow );
void InitDevice();
void Render();

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
InitWindow( hInstance, nCmdShow );
InitDevice();



MSG msg = {0};
while( WM_QUIT != msg.message )
{
if( PeekMessage( &msg, hwnd, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
Render();
}
}

void InitWindow( HINSTANCE hInstance, int nCmdShow )
{
WNDCLASS wnd;

wnd.cbClsExtra = 0;
wnd.cbWndExtra = 0;
wnd.hbrBackground = (HBRUSH)COLOR_WINDOW;
wnd.hCursor = LoadCursor( 0, IDC_ARROW );
wnd.hIcon = LoadIcon( 0, IDI_APPLICATION );
wnd.hInstance = hInstance;
wnd.lpfnWndProc = WndProc;
wnd.lpszClassName = "Default";
wnd.lpszMenuName = NULL;
wnd.style = CS_VREDRAW | CS_HREDRAW;

RegisterClass( &wnd );

hwnd = CreateWindow( "Default", "Test", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 400, 0, 0, hInstance, 0 );
ShowWindow( hwnd, nCmdShow );
}

void InitDevice()
{
// first fill out swap chain desc
DXGI_SWAP_CHAIN_DESC sd;

sd.BufferCount = 1;

sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.Width = 400;
sd.BufferDesc.Height = 400;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;

sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.Flags = NULL;
sd.OutputWindow = hwnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
sd.Windowed = true;


// now create device and swap chain
D3D_DRIVER_TYPE DriverType = D3D_DRIVER_TYPE_HARDWARE;

D3D_FEATURE_LEVEL pFeatureLevels = D3D_FEATURE_LEVEL_11_0;
if( !D3D11CreateDeviceAndSwapChain( NULL, DriverType, NULL, NULL, &pFeatureLevels, 1, D3D11_SDK_VERSION, &sd, &ppSwapChain, &ppDevice, &pFeatureLevel, &ppImmediateContext ))
MessageBox( 0, "OOps", "Oops!", 0 );

// now handle the render target views for buffers
ID3D11Texture2D *pBackBuffer;

ppSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), reinterpret_cast<void**>(&pBackBuffer) );
MessageBox( 0, "GetBuffer success!", "Success!", 0 );
ppDevice->CreateRenderTargetView( pBackBuffer, NULL, &pRenderTargetView );


D3D11_VIEWPORT vp;
vp.Width = 400;
vp.Height = 400;
vp.MaxDepth = 1.0f;
vp.MinDepth = 0.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
ppImmediateContext->RSSetViewports( 1, &vp );
}

void Render()
{
// Just clear the backbuffer
float ClearColor[4] = { 0.0f, 0.125f, 0.3f, 1.0f }; //red,green,blue,alpha
ppImmediateContext->ClearRenderTargetView( pRenderTargetView, ClearColor );
ppSwapChain->Present( 0, 0 );
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
PAINTSTRUCT ps;
HDC hdc;

switch( msg )
{
case WM_CREATE:
return 0;
case WM_KEYDOWN:
switch( wParam )
{
case VK_LEFT:
SetWindowText( hwnd, "pressed left arrow key!" );
return 0;
case VK_RIGHT:
SetWindowText( hwnd, "pressed right arrow key!" );
return 0;
case VK_DOWN:
SetWindowText( hwnd, "pressed down arrow key!" );
return 0;
case VK_UP:
SetWindowText( hwnd, "pressed up arrow key!" );
return 0;
case VK_ESCAPE:
PostQuitMessage( 0 );
return 0;
}
case WM_KEYUP:
{
SetWindowText( hwnd, "Test" );
return 0;
}
case WM_DESTROY:
{
PostQuitMessage( 0 );
return 0;
}
}
return DefWindowProc( hwnd, msg, wParam, lParam );
}[/code]

Says I have an access violation when I execute ppSwapChain->GetBuffer.

Any ideas?

Share this post


Link to post
Share on other sites
i guess D3D11CreateDeviceAndSwapChain() failed to initialize.

so ppSwapChain is 0x000000 of course.(in this case ppSwapChain is 0xcdcdcdcd or something anyway point address causes violation)

and you better write WM_CLOSE handler code in your WinProc. i closed this program by pushing close button on the title bar but still program was running.


Share this post


Link to post
Share on other sites
Thanks for the reply. I figured that was the case.

Can you spot any errors in my D3D11CreateDeviceAndSwapChain() call? I was unsure how to handle pFeatureLevels, so that may be the incorrect part.

I chose to only include one feature level (D3D_FEATURE_LEVEL_11_0), so my featurelevel count should be 1, right?

EDIT: I fixed it by setting the &pFeatureLevels parameter to NULL and the FeatureLevels paramteter to NULL also. Any tips for how I can get it to work by specifying a feature level like I had tried in my source code?

Share this post


Link to post
Share on other sites
[quote]I chose to only include one feature level (D3D_FEATURE_LEVEL_11_0), so my featurelevel count should be 1, right?[/quote]
not sure,but it obvious i think.


[quote]was unsure how to handle pFeatureLevels,[/quote]
on my PC this code works fine.
but you may need try other lower feature level.
this thread may help
[url="http://www.gamedev.net/topic/565797-cant-initialize-dx11-swapchain-and-device/"]here[/url]

Share this post


Link to post
Share on other sites
[quote name='katsh' timestamp='1306271813' post='4815312']
[quote]I chose to only include one feature level (D3D_FEATURE_LEVEL_11_0), so my featurelevel count should be 1, right?[/quote]
not sure,but it obvious i think.


[quote]was unsure how to handle pFeatureLevels,[/quote]
on my PC this code works fine.
but you may need try other lower feature level.
this thread may help
[url="http://www.gamedev.net/topic/565797-cant-initialize-dx11-swapchain-and-device/"]here[/url]
[/quote]

Feature levels reflect hardware capabilities, so if you set a D3D_FEATURE_LEVLE_11_0 then the GPU in your machine needs to be a DX11 capable GPU otherwise it will fail. So pick the correct one for the hardware you are running on.

The feature levels exist so that you can support DX9.0c, 10, 10.1 and 11 cards through the DX11 API so the rendering code for all levels would look the same instead of having to write a render version for each DX version.

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