First Time using DirectX 10

Started by
31 comments, last by Nik02 14 years, 8 months ago
must be a driver issue there really isn't a reason for D3D10CreateDevice to fail that I can think of.
Advertisement
It only failed under hardware mode if that's of any constellation...
Well I guess I've concluded that my graphics card isn't good enough to provide hardware acceleration. But I still have another problem now... for some reason that following code jst displays a white screen even though it should have some colour now.

#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 480HINSTANCE hInstance = NULL;HWND hWnd = NULL;ID3D10Device* D3D = NULL;IDXGISwapChain* SwapChain = NULL;ID3D10RenderTargetView* RenderTargetView = 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; ID3D10Texture2D* BackBufferT; D3D10_VIEWPORT VP; 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_REFERENCE, NULL, NULL, D3D10_SDK_VERSION, &SC_Desc, &SwapChain, &D3D); SwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&BackBufferT); D3D->CreateRenderTargetView(BackBufferT, NULL, &RenderTargetView); SwapChain->Release(); D3D->OMSetRenderTargets(1, &RenderTargetView, NULL); VP.TopLeftX = 0; VP.TopLeftY = 0; VP.Width = Screen_Width; VP.Height = Screen_Height; VP.MinDepth = 0; VP.MaxDepth = 1; D3D->RSSetViewports(1, &VP);}VOID RenderD3D(){ D3D->ClearRenderTargetView(RenderTargetView, D3DXCOLOR(0, 0.5f, 0, 0));}VOID CleanDX(){}


Sigh... any ideas?
That's probably a sign that your Directx rendering function is not being called. Did you forget to call it? I haven't done much on direct x 10, but I think you also need to call the present function as well. Also why are you calling release() in your init function?

My render function is being called (RenderD3D())...
Why are you calling swapchain->release in your init function?
You don't seem to call swapChain->Present anywhere, which you must do after drawing your frame. Also, it's not a good idea to have a loop like that with GetMessage, since GetMessage only returns when a message is available, resulting in that you will repaint at irregular intervals and only when your application receives a message.
Oh that was supposed to be BackBufferT->Release()... thanks for pointing that out but it didn't seem to change anything :S... Am I doing something that's considering wrong? because nothing is showing up...
Well, you said you are seeing white, which tells me that something is preventing the d3d code from being executed. In your rendering section you definitely need to call swapchain->Present() This actually causes d3d to show up on screen. It's like calling Swap Buffers() in Opengl. And please don't forget to release the resources after you are done. :)
In addition to what others have said, I highly recommend using the debug layer when developing D3D10(+) applications. To enable it, you only need to set a single flag at the device creation parameters.

When the debug layer is enabled, the runtime does extra checks to validate your device states, and will print debug messages to the debugger if some validations fail. The checks will reduce performance slightly, but it is worth it since you eliminate a lot of guesswork. In the later cycles of development, when you've confirmed that your systems work, take the debug flag off.

To see what your app must minimally do when using D3D10 to draw to the screen, check out the D3D10 tutorial 1 on the SDK.

In case the debug layer doesn't report any errors by the time you call swapchain::present, the error is in your host app logic. This can be resolved by putting breakpoints to your code and/or stepping thru the code where you interact with D3D.

Finally, you can use PIX to verify that D3D understood all the stuff you fed into it. You can, for example, view your backbuffer even if it didn't make it into the screen for some reason.

Niko Suni

This topic is closed to new replies.

Advertisement