DirectX 11

Started by
5 comments, last by skilz8099 12 years, 10 months ago
Hello, I am at the stage of Initializing Direct X 11 and I get an error when trying to call this function and assign it to the buffers pointer.

// Get The Address Of The Back Buffer
ID3D11Texture2D *pBackBuffer;
swapchain->GetBuffer( 0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer );



the source compiles fine, it builds fine, but when I run without debugging, I get windows message that the program has stopped working. When I run it with debugging I get this message from Visual Studio 2010 c++ express :
Unhandled exception at 0x008917fc in DX11 Tutorial.exe: 0xC0000005: Access violation reading location 0x00000000.

Here is the complete code, since this program is in its early development it is all in one .cpp file.

// -----------------------------------------------------------------------
// DX11 Tutorial
// WinMain.cpp
// Coded by Francis R Cugler (c) 2011
// -----------------------------------------------------------------------

// Include Files
#include
#include

// DirectX Headers
#include
#include
#include

// Include The Direct3D Library File
#pragma comment (lib, "d3d11.lib" )
#pragma comment (lib, "d3dx11.lib" )
#pragma comment (lib, "d3dx10.lib" )

// Global Declarations
IDXGISwapChain *swapchain; // The Pointer To The Swap Chain Interface
ID3D11Device *dev; // The Pointer To Our Direct3D Device Interface
ID3D11DeviceContext *devcon; // The Pointer To Our Direct3D Device Context
ID3D11RenderTargetView *backbuffer; // The Pointer To Our Back Buffer

// Function Prototypes
void InitD3D( HWND hWnd ); // Sets Up And Initializes Direct3D
void RenderFrame(); // Renders A Single Frame
void CleanD3D(); // Closes Direct3D And Releases Memory

// The WindowProc Function Prototype
LRESULT CALLBACK WindowProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );

// The Entry Point For Any Windows Program
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
// The Handle For The Window, Filled By A Function
HWND hWnd;
// This Struct Holds Information For The Window Class
WNDCLASSEX wc;

// Clear Out The Window Class For Use
ZeroMemory( &wc, sizeof(WNDCLASSEX) );

// Fill In The Struct With The Needed Information
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = "WindwoClass";

// Register The Window Class
RegisterClassEx( &wc );

// Calculate The Size Of The Client Area
RECT wr = { 0, 0, 800, 600 }; // Set The Size But Not The Position
AdjustWindowRect( &wr, WS_OVERLAPPEDWINDOW, FALSE ); // Adjust The Size

// Create The Window And Use The Result As The Handle
hWnd = CreateWindowEx( NULL, "WindowClass", "Our First Direct3D Program",
WS_OVERLAPPEDWINDOW, 300, 300,
wr.right - wr.left, // Width Of The Window
wr.bottom - wr.top, // Height Of The Window
NULL, NULL, hInstance, NULL );

// Display The Window On The Screen
ShowWindow( hWnd, nCmdShow );

// Setup And Init Direct3D
InitD3D( hWnd );

// Enter The Main Loop:

MSG msg;

// Wait For The Next Message In The Queue, Store The Result In 'msg'
while ( TRUE ) {
// Check To See If Any Messages Are Waiting In The Queue
if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) {

// Translate Keystroke Messages Into The Right Format
TranslateMessage( &msg );
// Send Messages To The WindowProc Function
DispatchMessage( &msg );

// Check To See If It's Time To Quit
if ( msg.message == WM_QUIT )
break;
}

// Render Frame Each Time Through Our Loop
RenderFrame();
}

// Clean up DirectX And Com
CleanD3D();

// Return This Part Of The WM_QUIT Message To Windows
return msg.wParam;

} // WinMain

// -----------------------------------------------------------------------
// This Is The Main Message Handler For The Program
LRESULT CALLBACK WindowProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ) {
// Sort Through And Find What Code To Run For The Message Given
switch ( message ) {
// This Message Is Read When The Window Is Closed
case WM_DESTROY: {
// Close The Application Entirely
PostQuitMessage( 0 );
return 0;
}
break;
}

// Handle Any Messages The Switch Statement Didn't
return DefWindowProc( hWnd, message, wParam, lParam );

} // WindowProc

// -----------------------------------------------------------------------
// This Function Initializes And Prepares Direct3D For Use
void InitD3D( HWND hWnd ) {
// Creates A Struct To Hold Information About The Swap Chain
DXGI_SWAP_CHAIN_DESC scd;

// Clear Out The Struct For Use
ZeroMemory( &scd, sizeof( DXGI_SWAP_CHAIN_DESC ) );

// Fill The Swap Chain Description Struct
scd.BufferCount = 1; // One Back Buffer
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // Use 32-bit Color
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // How Swap Chain Is To Be Used
scd.OutputWindow = hWnd; // The Window To Be Used
scd.SampleDesc.Count = 4; // How Many Multisamples
scd.Windowed = TRUE; // Windowsd/Full-Screen Mode

// Create A Device, Device Context And Swap Chain Using The Information In The scd Struct
D3D11CreateDeviceAndSwapChain ( NULL, D3D_DRIVER_TYPE_HARDWARE,
NULL, NULL, NULL, NULL, D3D11_SDK_VERSION,
&scd, &swapchain, &dev, NULL, &devcon );

// Get The Address Of The Back Buffer
ID3D11Texture2D *pBackBuffer;
swapchain->GetBuffer( 0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer );

// Use The Back Buffer Address To Create The Render Target
dev->CreateRenderTargetView( pBackBuffer, NULL, &backbuffer );
pBackBuffer->Release();

// Set The Render Target As The Back Buffer
devcon->OMSetRenderTargets(1, &backbuffer, NULL );

// Set The Viewport
D3D11_VIEWPORT viewport;
ZeroMemory( &viewport, sizeof( D3D11_VIEWPORT ) );

viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = 800;
viewport.Height = 600;

devcon->RSSetViewports( 1, &viewport );

} // InitD3D

// ----------------------------------------------------------------------- //
// This Is The Function Used To Render A Single Frame
void RenderFrame() {
// Clear The Back Buffer To A Deep Blue
devcon->ClearRenderTargetView( backbuffer, D3DXCOLOR( 0.0f, 0.2f, 0.4f, 1.0f ) );

// Do 3D Rendering On The Back Buffer Here

// Switch The Back Buffer And The Front Buffer
swapchain->Present( 0, 0 );

} // RenderFrame

// ----------------------------------------------------------------------- //
// This Is The Function That Cleans Up Direct3D And Com
void CleanD3D() {
// Close And Release All Existing COM Objects
swapchain->Release();
backbuffer->Release();
dev->Release();
devcon->Release();
}




If someone can point me in the right direction, could you please help me. If you need to know more about my pc.
here is a copy of my system information:


OS Name Microsoft® Windows Vista™ Ultimate
Version 6.0.6002 Service Pack 2 Build 6002
Other OS Description Not Available
OS Manufacturer Microsoft Corporation
System Name SKILZ80-PC
System Manufacturer INTEL
System Model DP45SG
System Type X86-based PC
Processor Intel® Core™2 Quad CPU Q9650 @ 3.00GHz, 2997 Mhz, 4 Core(s), 4 Logical Processor(s)
BIOS Version/Date Intel Corp. SGP4510H.86A.0125.2010.0121.1927, 1/21/2010
SMBIOS Version 2.4
Windows Directory C:\Windows
System Directory C:\Windows\system32
Boot Device \Device\HarddiskVolume1
Locale United States
Hardware Abstraction Layer Version = "6.0.6002.18005"
User Name skilz80-PC\skilz80
Time Zone Central Daylight Time
Installed Physical Memory (RAM) 4.00 GB
Total Physical Memory 3.24 GB
Available Physical Memory 1.20 GB
Total Virtual Memory 7.66 GB
Available Virtual Memory 5.41 GB
Page File Space 4.49 GB
Page File C:\pagefile.sys

I have Windows SDK, the platform kit to simulate DirectX 11, DirectX's June SDK, and using Visual Studio C++ 2010 express edition. Please feel free to post here or you can email me at skilz8099@gmail.com . Thank You.
Advertisement
Access violation reading location 0x00000000[/quote]This means you've used a NULL pointer.
You're not checking the return value of [font="Courier New"]D3D11CreateDeviceAndSwapChain[/font]. If this function returns an error for whatever reason, then [font="Courier New"]swapchain[/font] will never be assigned to (and then you use it on the next line that's causing the NULL pointer crash -- coincidence?).
Thank you for the feedback. It did take me a while to debug it but i was finally able to run the program without any crashes.

I do however have a second question.

Since I am using Windows Vista Ultimate with service pack 2 with the platform update to use DirectX 11. And if i run the directx dialog, it says that my version of directx is 11. If i remember correctly my video card is DirectX 10. compatible. I am using DirectX11 code, now when I add in the shader using hlsl.

the function that calls the shader from a file

// load and compile the two shaders
ID3D10Blob *VS, *PS;
D3DX11CompileFromFile( "shaders.hlsl", 0, 0, "VShader", "vs_5_0", 0, 0, 0, &VS, 0, 0);
D3DX11CompileFromFile( "shaders.hlsl", 0, 0, "PShader", "ps_5_0", 0, 0, 0, &PS, 0, 0);


when i run this the program crashes, but if i change the 5 in both function calls to a 4, it runs fine. Is this because my video card will not support the hlsl version 5 language?
[font="Consolas"][font="Consolas"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][/font][/font][/font]

[font="Consolas"] [/font]

[font="Consolas"][font="Consolas"][/font][/font][/font][/font]
Since you have a DX10 GPU you're limited to FEATURE_LEVEL_10_0, which means the max shader profile you can use is vs_4_0/ps_4_0/gs_4_0/etc. For SM5.0, you need a DX11 GPU.
Thank you for you reply. I just updated the newest available ati catalyst driver suit for my video card. When i read the documentation on my particular video card, it does have DirectX 11 support, but I think it only support hlsl 4.x

I have an ATI Radeon HD 4550 512mb ddr3 pci-express 2.0 video card. Could you verify this for me. thank you. The reason I am asking for this, I am trying to develop using strictly dx11.
Now if my video card supports dx11 but not hlsl 5, then i will have to use 4 in its place. I guess there is now way of getting around this, if i want to test my code on this pc, unless i get a newer video card. Thank You in advance.
There is a difference between "supporting DX11" and the feature level supported by the GPU. Technically even old DX9 GPU's support DX11, because the API includes the lower feature levels for targeting those GPU's. However depending on the feature level supported by a GPU, it may or may not have access to newer features like 5.0-level shader profiles or DrawIndirect.To determine which features you can use at runtime, you should check the value of the pFeatureLevelOut parameter to D3D11CreateDevice. See this for more details: http://msdn.microsoft.com/en-us/library/ff476872%28v=vs.85%29.aspx

Your HD 4550 supports FEATURE_LEVEL_10_1, which means you have access to SM4.1.
Thank You, this will help me to prevent future program crashes, when building and running the executable. So, I can assume it is safe to say, build the application and test it on my machine using hlsl 4.1, then once the program is complete and there are no bugs, I can then replace the 4.1 with 5.0 and just add in the new shaders but i won't be able to test it on my machine. Hopefully I'll be able to get a slightly newer video card that supports hlsl 5.0. Would like to get the new 4gb or 8gb video cards but they run close to $1,000.00 lol, so i can settle with a 1gb or 2gb video card. Yeah my video card is no more then 3 years old and already it is nearly obsolete.

This topic is closed to new replies.

Advertisement