First Time using DirectX 10

Started by
31 comments, last by Nik02 14 years, 9 months ago
Hiya guys been a while, EDIT - Please scroll down... the nature of the problem has infact changed. Thank you. I just recently began to learn DirectX 10 after a long while without programming... and well it feels great. The problem is that I am... "stuck". Here's my current code:

#include <windows.h>
#include <d3d10.h>
#include <d3dx10.h>

#pragma comment (lib, "d3d10.lib")
#pragma comment (lib, "d3dx10.lib")

#define Screen_Width 640
#define Screen_Height 480

HINSTANCE hInstance = NULL;
HWND hWnd = NULL;

ID3D10Device* D3D = NULL;
IDXGISwapChain* SwapChain = NULL;

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
VOID InitD3D();
VOID RenderD3D();
VOID CleanDX();

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
{

 MSG Msg;
 WNDCLASSEX WndClass;
 
 ZeroMemory(&WndClass, sizeof(WNDCLASSEX));

 WndClass.cbSize = sizeof(WNDCLASSEX);
 WndClass.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
 WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
 WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
 WndClass.hInstance = hInstance;
 WndClass.lpfnWndProc = MainWndProc;
 WndClass.lpszClassName = "MainWND-Class";
 WndClass.style = CS_HREDRAW | CS_VREDRAW;

 RegisterClassEx(&WndClass);

 hWnd = CreateWindowEx(NULL, WndClass.lpszClassName, "Test", WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, Screen_Width, Screen_Height, NULL, NULL, hInstance, NULL);

 UpdateWindow(hWnd);
 InitD3D();

 while(GetMessage(&Msg, NULL, NULL, NULL) > 0)
 {

  TranslateMessage(&Msg);
  DispatchMessage(&Msg);

  RenderD3D();

 }

 CleanDX();

 return 0;

}

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{

 switch(Msg)
 {

  case WM_DESTROY:

   PostQuitMessage(WM_QUIT);

  break;

  default: DefWindowProc(hWnd, Msg, wParam, lParam);

 }

 return TRUE;

}

VOID InitD3D()
{

 DXGI_SWAP_CHAIN_DESC SC_Desc;
 ID3D10RenderTargetView* RenderTargetView;
 ID3D10Texture2D* BackBufferT;

 ZeroMemory(&SC_Desc, sizeof(SC_Desc));

 SC_Desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
 SC_Desc.BufferDesc.Height = Screen_Height;
 SC_Desc.BufferDesc.RefreshRate.Denominator = 1;
 SC_Desc.BufferDesc.RefreshRate.Numerator = 60;
 SC_Desc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
 SC_Desc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
 SC_Desc.BufferDesc.Width = Screen_Width;
 SC_Desc.BufferCount = 1;
 SC_Desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
 SC_Desc.Flags = 0;
 SC_Desc.OutputWindow = hWnd;
 SC_Desc.SampleDesc.Count = 1;
 SC_Desc.SampleDesc.Quality = 0;
 SC_Desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
 SC_Desc.Windowed = TRUE;

 D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, NULL, D3D10_SDK_VERSION, &SC_Desc, &SwapChain, &D3D); 

 SwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&BackBufferT);
 D3D->CreateRenderTargetView(BackBufferT, NULL, &RenderTargetView);
 BackBufferT->Release();

}

VOID RenderD3D()
{



}

VOID CleanDX()
{



}



As you can see its not complete. The problem is that when I compile the program I get an error, but when I set the device type to D3D10_DRIVER_TYPE_REFERENCE my program compiles without a problem. What I don't understand is why that little change fixes everything... I mean you need to use the reference device type only if the hardware has some sort of bug or if your graphics card simply doesn't support DirectX 10. I'm sure that my graphics card does indeed support DirectX 10 because when I open up the DirectX Diagnostic Tool ("dxdiag" in "run") it reports "DirectX 10" and the DirectX version. Do any of you guys know what's causing this problem? Thank you. [Edited by - pcbrainbuster on July 22, 2009 6:03:34 PM]
Advertisement
what error are you getting?
Well when the program starts I get an error message that just states that my program has stopped working. That's all.
Check the HRESULT returned from your call to D3D10CreateDeviceAndSwapChain. The buffer description may be invalid for your graphics card.
sounds like the hardware doesn't like a setting you are using that the reference device doesn't check. here is my startup code from a test project I did see if it helps.

// D3D10Test.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "D3D10Test.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name

// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_D3D10TEST, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_D3D10TEST));

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}



//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_D3D10TEST));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_D3D10TEST);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//

char czShader[] ="struct PSIN{float4 Pos : SV_POSITION;float2 Pixel : VPOS;};\n"
"float4x4 g_Matrix;PSIN VS(float3 Pos: POSITION) {PSIN Out = (PSIN)0;Out.Pos = float4(Pos,1);return Out;}\n"
" float4 PS(PSIN In): SV_TARGET {float4 OutColor=(float4)0;OutColor = lerp(float4(0.5,0,0,1),float4(1,0,0,1),(In.Pos.y/480));return OutColor;}\n"
" technique10 t\n"
"{\n"
"pass p{SetVertexShader( CompileShader( vs_4_0, VS() ) );\n"
"SetGeometryShader( NULL );SetPixelShader( CompileShader( ps_4_0, PS() ) );}}\n";

int nWidth = 1280;
int nHeight = 800;

ID3D10Device * g_pd3dDevice=NULL;
IDXGISwapChain * g_pSwapChain=NULL;
ID3D10RenderTargetView * g_pRenderTargetView=NULL;

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
0, 0, nWidth, nHeight, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory( &sd, sizeof(sd) );
sd.BufferCount = 1;
sd.BufferDesc.Width = nWidth;
sd.BufferDesc.Height = nHeight;
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;
HRESULT hr;
if( FAILED(hr = D3D10CreateDeviceAndSwapChain( NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL,
0, D3D10_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice ) ) )
{
return FALSE;
}

// Create a render target view
ID3D10Texture2D *pBackBuffer;
if( FAILED( g_pSwapChain->GetBuffer( 0, __uuidof( ID3D10Texture2D ), (LPVOID*)&pBackBuffer ) ) )
return FALSE;
hr = g_pd3dDevice->CreateRenderTargetView( pBackBuffer, NULL, &g_pRenderTargetView );
pBackBuffer->Release();
if( FAILED( hr ) )
return FALSE;
g_pd3dDevice->OMSetRenderTargets( 1, &g_pRenderTargetView, NULL );

D3D10_VIEWPORT vp;
vp.Width = nWidth;
vp.Height = nHeight;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
g_pd3dDevice->RSSetViewports( 1, &vp );


ID3D10Effect * pEffect = NULL;
ID3D10Blob * pBlob = NULL;
ID3D10Blob * pError = NULL;
if(SUCCEEDED(D3D10CompileEffectFromMemory(czShader,sizeof(czShader),"basic.fx",NULL,NULL,0,0,&pBlob,&pError)))
{
if(FAILED(D3D10CreateEffectFromMemory(pBlob->GetBufferPointer(),pBlob->GetBufferSize(),0,g_pd3dDevice,NULL,&pEffect)))
{
int n = 0;
return FALSE;
}
}
else
{
char * pChar = (char*)pError->GetBufferPointer();
int n = 0;
return FALSE;
}

D3D10_INPUT_ELEMENT_DESC layout[] =
{
{"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D10_INPUT_PER_VERTEX_DATA,0},
};

UINT numElements = sizeof(layout)/sizeof(layout[0]);

struct Vertex
{
float m_FPosition[3];
} Vertices[] =
{
//first tri
{-1.0f, 1.0f, 1.0f},
{1.0f, -1.0f, 1.0f},
{-1.0f, -1.0f, 1.0f},
{-1.0f, 1.0f, 1.0f},
{1.0f, 1.0f, 1.0f},
{1.0f, -1.0f, 1.0f},
};
D3D10_SUBRESOURCE_DATA Data;
Data.pSysMem = Vertices;
ID3D10Buffer * pBuffer = NULL;
D3D10_BUFFER_DESC Desc;
Desc.ByteWidth = sizeof(Vertices);
Desc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
Desc.Usage = D3D10_USAGE_DEFAULT;
Desc.CPUAccessFlags = 0;
Desc.MiscFlags = 0;
D3DXMATRIX Matrix;
D3DXMATRIX World;
D3DXMATRIX Project;
D3DXMATRIX View;
D3DXVECTOR3 Translate;
Translate.x = 0;
Translate.y = 0;
Translate.z = 0;
D3DXQUATERNION Rotate;
Rotate.w = 1;
Rotate.x = 0;
Rotate.y = 0;
Rotate.z = 0;
D3DXVECTOR3 Scale;
Scale.x = 0;
Scale.y = 0;
Scale.z = 0;
D3DXMatrixTransformation(&World,NULL,NULL,&Scale,NULL,&Rotate,&Translate);
D3DXMatrixTransformation(&View,NULL,NULL,NULL,NULL,&Rotate,&Translate);
D3DXMatrixPerspectiveFovLH(&Project,40/100*3.14,640/480,0.0,10.0);
Matrix = World * View * Project;
/*ID3D10EffectVariable * pVariable = pEffect->GetVariableByName("g_Matrix");
if(pVariable)
{
pVariable->AsMatrix()->SetMatrix((float*)&Matrix);
}*/

if(SUCCEEDED(g_pd3dDevice->CreateBuffer(&Desc,&Data,&pBuffer)))
{
//render
ID3D10EffectTechnique* pTechnique = pEffect->GetTechniqueByIndex(0);
if(pTechnique && pTechnique->IsValid())
{
ID3D10EffectPass * pPass = pTechnique->GetPassByIndex(0);
if(pPass && pPass->IsValid())
{
ID3D10InputLayout* pVertexLayout = NULL;
D3D10_PASS_DESC Desc;
pPass->GetDesc(&Desc);
pVertexLayout;
if(SUCCEEDED(g_pd3dDevice->CreateInputLayout( layout, numElements,
Desc.pIAInputSignature, Desc.IAInputSignatureSize, &pVertexLayout )))
{
g_pd3dDevice->IASetInputLayout(pVertexLayout);
UINT nStride = sizeof(Vertex);
UINT nOffset = 0;
float fColor[4] = {0,0,1,1};
g_pd3dDevice->ClearRenderTargetView(g_pRenderTargetView,fColor);
g_pd3dDevice->IASetVertexBuffers(0,1,&pBuffer,&nStride,&nOffset);
g_pd3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
if(SUCCEEDED(pPass->Apply(0)))
{
g_pd3dDevice->Draw(6,0);
}
}
}
}
}

g_pSwapChain->Present(0,0);


return TRUE;
}

//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}


What graphics card do you actually have? Even though the D3D10 runtime is installed, it doesn't mean that any hardware is available that supports it.

Niko Suni

Thanks for the posts.

I checked to see if D3D10CreateDeviceAndSwapChain was the cause and it does seem to be. I used this line of code:

if(FAILED(D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, NULL, D3D10_SDK_VERSION, &SC_Desc, &SwapChain, &D3D)) == TRUE) MessageBox(NULL, NULL, NULL, NULL);

... the message box did actually come up which tells me that something there went wrong, but what? How can I tell?

Thanks.
My graphics card is "Mobile Intel(R) 965 Express Chipset Family"... not to high-tech is it.
check the return code. Otherwise try different display desc settings till it works. As a side note you are using vista right?
Yeah, I'm using Vista. How do I check the return code? I only know about the "FAILED" and "SUCCEEDED" macros...

This topic is closed to new replies.

Advertisement