Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualMilla.M

Posted 02 July 2012 - 08:13 AM

Hi
I can send minimum ( doesn't compile code). I should mention that the code uses wild magic 5 library for rendering, and I am using Qt , to start the second thread. The second thread starts its own message loop , so it can receive the update message . I don't know if you familiar wit qt signal/slots mechanism but this should be irrelevant here.
void Initialize( HWND parentHWND)
{
	// DirectX uses a projection matrix for depth in [0,1].
	Wm5::Camera::SetDefaultDepthType(Wm5::Camera::PM_DEPTH_ZERO_TO_ONE);

	// === Create the window for rendering. ===  
	// Register the window class.
	static wchar_t sWindowClass[] = L"My Window";
	WNDCLASS wc;
	wc.style		 = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wc.lpfnWndProc   = MyEventHandler;
	wc.cbClsExtra	= 0;
	wc.cbWndExtra	= 0;
	wc.hInstance	 = 0;
	wc.hIcon		 = 0;
	wc.hCursor	   =0;
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszClassName = sWindowClass;
	wc.lpszMenuName  = 0;
	RegisterClass(&wc);
  
	DWORD dwStyle;
	dwStyle = WS_CHILDWINDOW;

	// resize it, to fit its parent
	RECT rect;
	::GetWindowRect(parentHWND, &rect);
	int width = rect.right - rect.left;
	int height = rect.bottom - rect.top;
  
  
static wchar_t sWindowName[] = L"MyWindow";
	// Create the application window.
	HWND handle = CreateWindow(sWindowClass,
		sWindowName,
		dwStyle,
		0,//xPos
		0,//yPos
		width,
		height,
		parentHWND,
		0,
		0,
		0);

	 // Create the device for rendering.
	mRenderInput.mWindowHandle = handle;
	mRenderInput.mDriver = Direct3DCreate9(D3D_SDK_VERSION);
	assertion(mRenderInput.mDriver != 0, "Failed to create Direct3D9\n");
	mRenderer = new0 Wm5::Renderer(mRenderInput, width, height,
		mColorFormat, mDepthStencilFormat, mNumMultisamples);
  
	// Display the window.
	ShowWindow(handle, SW_SHOW);
	UpdateWindow(handle);

}
class MyRunner: public QObject
{

public slots :
  void update()
  {
  
	 mSceneGraph->Update();  
	 mCuller.ComputeVisibleSet(mSceneGraph->GetRoot());

		if (mRenderer->PreDraw()) // PreDraw just calls BeginScene
		{
			mRenderer->ClearBuffers();
			mRenderer->Draw(mCuller.GetVisibleSet());		  
			mRenderer->PostDraw(); // PostDraw just calls EndScene
			mRenderer->DisplayColorBuffer(); // DisplayColorBuffer just calls Present
		}

	  emit(SignalUpdate()); // this post message to the second thread message loop so it can basically start the update cycle anew

  }
};
class MyThread : public QThread
{
   void run()
   {
	   this->exec(); // this starts message loop
   }
};
HWND mainAppHwnd = GetMainHwnd();
Initialize(mainAppHwnd);
MyThread* thread = new MyThread;
MyRunner* runner = new MyRunner;
runner->moveToThread(thread);
QObject::connect(thread,SIGNAL(started()),runner,SLOT(update()));
thread->start();


"And if i remember correct, you should not render on a secondthread, you should render on your main thread, but do all the updating and setup all data to
just be scheduald for drawing, so that the main thread do as litle as possible when the drawing´s begins."

I don't uderstand this.
Does this mean that in my case that I should call the updating of the scenegraph, and drawing functions on second thread, but only call Present on main thread ?

By the way what problem did you solve with Sleep(1) ?

Also I can't switch to DirectX11

#1Milla.M

Posted 02 July 2012 - 08:07 AM

Hi
I can send minimum ( doesn't compile code). I should mention that the code uses wild magic 5 library for rendering, and I am using Qt , to start the second thread. The second thread starts its own message loop , so it can receive the update message . I don't know if you familiar wit qt signal/slots mechanism but this should be irrelevant here.
void Initialize( HWND parentHWND)
{
    // DirectX uses a projection matrix for depth in [0,1].
    Wm5::Camera::SetDefaultDepthType(Wm5::Camera::PM_DEPTH_ZERO_TO_ONE); 

    // === Create the window for rendering. ===  
    // Register the window class.
    static wchar_t sWindowClass[] = L"My Window";
    WNDCLASS wc;
    wc.style		 = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wc.lpfnWndProc   = MyEventHandler;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance	 = 0;
    wc.hIcon		 = 0;
    wc.hCursor	   =0;
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszClassName = sWindowClass;
    wc.lpszMenuName  = 0;
    RegisterClass(&wc);
  
    DWORD dwStyle;
    dwStyle = WS_CHILDWINDOW;

    // resize it, to fit its parent
    RECT rect;
    ::GetWindowRect(parentHWND, &rect);
    int width = rect.right - rect.left;
    int height = rect.bottom - rect.top;
   
   
static wchar_t sWindowName[] = L"MyWindow";
    // Create the application window.
    HWND handle = CreateWindow(sWindowClass,
	    sWindowName,
	    dwStyle,
	    0,//xPos
	    0,//yPos
	    width,
	    height,
	    parentHWND,
	    0,
	    0,
	    0); 

	 // Create the device for rendering.
    mRenderInput.mWindowHandle = handle;
    mRenderInput.mDriver = Direct3DCreate9(D3D_SDK_VERSION);
    assertion(mRenderInput.mDriver != 0, "Failed to create Direct3D9\n");
    mRenderer = new0 Wm5::Renderer(mRenderInput, width, height,
	    mColorFormat, mDepthStencilFormat, mNumMultisamples);
  
    // Display the window.
    ShowWindow(handle, SW_SHOW);
    UpdateWindow(handle);

}
MyRunner: public QObject
{
 
public slots :
  void update()
  {
  
	 mSceneGraph->Update();   
	 mCuller.ComputeVisibleSet(mSceneGraph->GetRoot());

	    if (mRenderer->PreDraw()) // PreDraw just calls BeginScene
	    {
		    mRenderer->ClearBuffers();
		    mRenderer->Draw(mCuller.GetVisibleSet());		  
		    mRenderer->PostDraw(); // PostDraw just calls EndScene
		    mRenderer->DisplayColorBuffer(); // DisplayColorBuffer just calls Present
	    } 

	  emit(SignalUpdate()); // this post message to the second thread message loop so it can basically start the update cycle anew
 
  }
};
class MyThread : public QThread
{
   void run()
   {
	   this->exec(); // this starts message loop
   }
};

Initialize();
MyThread* thread = new MyThread;
MyRunner* runner = new MyRunner; 
runner->moveToThread(thread);
QObject::connect(thread,SIGNAL(started()),runner,SLOT(update()));
thread->start();


"And if i remember correct, you should not render on a secondthread, you should render on your main thread, but do all the updating and setup all data to
just be scheduald for drawing, so that the main thread do as litle as possible when the drawing´s begins."

I don't uderstand this.
Does this mean that in my case that I should call the updating of the scenegraph, and drawing functions on second thread, but only call Present on main thread ?

By the way what problem did you solve with Sleep(1) ?

Also I can't switch to DirectX11

PARTNERS