Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!

(solved) Main thread and d3d?

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 cozzie   Members   -  Reputation: 2763


Posted 23 December 2012 - 11:32 AM


I'm trying to make sure I'm calling create, testcooperativelevel, reset and release on my d3d device, from the main thread (same one as the window messages are handled). I'm not sure I understand the definition of a thread, I believe I only have one thread. Here's a bit of my code to give you a better view.

I'm doing the following calls to create, reset, release and testcooperative level:

Create => part of private function in d3d class, class includes mHwnd and d3ddev vars
Reset => part of private function 'ResetDevice' in d3d class, triggered from winproc of d3d window/hwnd
TestCooperativelevel => both from the winproc handler of d3d window/hwnd as within several member functions of d3d class
(also from main program loop, through 'CheckDevice' function, member of d3d class)
Release => in the destructor of my d3d class, in which d3ddev is a public member

CD3d		_d3d; // object of own d3d wrapper/class including mHwnd and md3ddev membersint WINAPI WinMain(HINSTANCE hInstance,				   HINSTANCE hPrevInstance,				   LPSTR lpCmdLine,				   int nCmdShow){        _d3d.CreateWindow();        _d3d.InitD3ddevice();        // load scene etc.	PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);	while(msg.message != WM_QUIT)	{		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))			{			TranslateMessage(&msg);			DispatchMessage(&msg);		}		else		{			if(active)			{				if(_d3d.CheckDevice())				{					_timer.CalcFps();					SetWindowTextA(_d3d.mHwnd, _timer.mStrFramerate);										// render 3d scene					if(!_d3d.RenderFrame(&_d3dscene, &_d3dcam)) 					{						MessageBox(_d3d.mHwnd, err_render.c_str(), err_windowtext.c_str(), MB_ICONERROR);							return (INT)msg.wParam;					}				}			}		}	}	return (INT)msg.wParam;}// windowproc, which is linked to the mHwnd in the _d3d objectLRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){	int sleeptime = 20;	switch(message)		{		case WM_ACTIVATE:			if(LOWORD(wParam) == WA_ACTIVE || WA_CLICKACTIVE)			{				active = true;				Sleep(sleeptime);				break;			}			else				active = false;				Sleep(sleeptime);				break;			break; 		case WM_ACTIVATEAPP:			if(!wParam)			{				active = false;				Sleep(sleeptime);			}			if(wParam)			{				if(_d3d.mD3ddev)				{					if(!_d3d.CheckDevice())					{						{							while(1)							{								Sleep(sleeptime);								switch (_d3d.mD3ddev->TestCooperativeLevel())								{								case D3D_OK:									return 0;								case D3DERR_DEVICELOST:									break;								case D3DERR_DEVICENOTRESET:									{										_d3d.OnLostDevice();										_d3dscene.OnLostDevice();										if(!_d3d.ResetDevice()) break;										else										{											_d3d.OnResetDevice(&_d3dscene, &_d3dcam);											_d3dscene.OnResetDevice(&_scene, &_d3d.mD3ddev);										}										active = true;										break;									}								case D3DERR_DRIVERINTERNALERROR:									PostQuitMessage(WM_QUIT);								}							}						}					}				}				else active = true;				Sleep(sleeptime);			}			break; 		case WM_DESTROY:				PostQuitMessage(WM_QUIT);			break;		default:			return DefWindowProc(hWnd, message, wParam, lParam);	}	return 0;}

Does anyone know if I'm using just one thread and if I follow the MSDN statement that the d3d device calls are executed from the main thread?
(or can I only call Create, Reset, Testcooperativelevel, Release from the Wndproc function of the d3d window?)


#2 Steve_Segreto   Crossbones+   -  Reputation: 1706


Posted 23 December 2012 - 01:54 PM

A good rule of thumb for you is if you don't know what a thread is, then you probably aren't using more than one :)


The thread where you have your WinMain and TranslateMessage and DispatchMessage is called the "MainThread".


If you put a breakpoint in Visual Studio inside your WindowProc you will see that ultimately it gets called back from the MainThread via the DispatchMessage routine.


So unless you explicitly go out of your way to make a new thread, you are only using one thread (MainThread), so you are complying with MSDN's usage pattern of D3D.

#3 kubera   Members   -  Reputation: 1104


Posted 23 December 2012 - 04:22 PM

For legacy technology you could have 2 threads.

  • General thread for processing everyting in CPU, the logic of appplication
  • Renderer, the thread, which has access to Direcr3D9 API

Such application would be faster significantly.

Main thread is schedulling operations to renderer, and the comsumer is getting commands from such queue and processing them in D3D.


Please, consider reading a book "GPU Pro" - ISBN 978-1-56881-472-8.

#4 cozzie   Members   -  Reputation: 2763


Posted 23 December 2012 - 04:51 PM

Thanks both, for the quick reaction. Good to know that I can proceed for now.

I'll see if I can check out the book, to prevent that i'll accidentally create a 2nd thread when expanding my engine rolleyes.gif

#5 kubera   Members   -  Reputation: 1104


Posted 23 December 2012 - 05:05 PM

Here you are.


The authors of book are more than professionals, they are experts and MVP.


In my application, I have added a thread pool from the ATL library.

This pool is used for loading resources divided into tasks.

You would even heve more threads.

It would be a boost.


Not easy, but good start would be here:


#6 cozzie   Members   -  Reputation: 2763


Posted 24 December 2012 - 07:53 AM

Thanks, will get into it. Especially if it will give a boost (when I'm further in development of my engine).


Still struggling with something else d3d though, which I thought would be related (but probably not):


(calling dev->present to early it seams, checking cooperative level is not sufficient enough it seems)


Might you have any suggestions...

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.