Jump to content

  • Log In with Google      Sign In   
  • Create Account

how to make d3d9 work with a window which is created and run in other thread


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 db123   Members   -  Reputation: 216

Like
0Likes
Like

Posted 13 November 2013 - 10:03 PM

in the normal d3d application, when we resize the window or drag the window, it will block the main thread.

if it is a online-game, it will block the logic.

so i want to find a method to resolve the problem.

in my option, multi thread is a proper resolution,

so i create the window( CreateWindowEx ) in the special thread, and call PeekMessage in that thread, like this:

UINT GameWindow::Exec()
        {

            HINSTANCE hInst = 0;

            hInst = (HINSTANCE)::GetModuleHandle(NULL);

            // register window
            WNDCLASSEX wc = {0};

            wc.cbSize        = sizeof(WNDCLASSEX);
            wc.style         = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
            wc.lpfnWndProc   = StaticClientWndProc;
            wc.cbClsExtra    = 0;
            wc.cbWndExtra    = 0;
            wc.hInstance     = hInst;
            wc.hIcon         = LoadIcon(0, IDI_WINLOGO);
            wc.hCursor       = LoadCursor(0, IDC_ARROW);
            //wc.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
            wc.hbrBackground = NULL;
            wc.lpszMenuName  = 0;
            wc.lpszClassName = L"xxxxxMainWindow";

            if( !RegisterClassExW(&wc) )
            {
                LogError( _T("Can't Register Window Class:") _T("HikariMainWindow") );
                LogError( _T("Last Error:"), System::Error::GetErrorDesc() );

                return false;
            }

            DWORD WindowStyle;

            WindowStyle = WS_OVERLAPPEDWINDOW;

            // Create Window
            m_WndID = CreateWindowExW(
                WS_EX_APPWINDOW,
                L"MainWindow",
                Detail::UTF8ToWCHAR(m_strTitle).CStr(),
                WindowStyle,
                CW_USEDEFAULT, CW_USEDEFAULT, m_SizeX, m_SizeY,
                NULL,
                NULL,
                hInst,
                NULL );
               
            // ...... 	

            m_CreateWindowEvent.Trigger();

            MSG msg = {0};

            do
            {
                while( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) )
                {
                    TranslateMessage( &msg );
                    DispatchMessage( &msg );
                }
            }while( !IsRequestExit() );            

            return 0;
        }

so the WndProc will called in this thread, in this function, i send all the parameters to main thread.

and in main thread, i open the message queue, process all the messages.

        LRESULT GameWindow::StaticClientWndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
        {
            X_ASSERT( g_WindowsMessageDisptacher );

            SendWindowsEventImpl(
                true,
                g_WindowsMessageDisptacher,
                WindowsMessageCommandDispatcher::ALIGN_SIZE,
                HWND, hWnd, hwnd,
                UINT, message, msg,
                WPARAM, w, wParam,
                LPARAM, h, lParam
            );	

            return ::DefWindowProc( hwnd, msg, wParam, lParam );
        }

  in the main thread.

        void GameWindow::ProcessGameWindowEvent( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
        {
            switch( msg )
            {
            case WM_DESTROY:
                ::PostQuitMessage(0);
                break;
            case WM_CLOSE:
                m_bRequestExit = true;
                g_GameEngine->RequestQuit();
                break;
            case WM_SIZE:
                if( g_GameEngine && wParam == SIZE_RESTORED )
                {
                   // g_GameEngine->OnGameWindowResize( LOWORD(lParam), HIWORD(lParam) );
                }

                break;
            }
        }

     it works well if i just drag the window, the main thread and its main loop is not blocked.

  

    the question is when i resize the window, there is a render error when i call 

IDirect3DSwapChain9::Present

 

   Direct3D9: (ERROR) :BitBlt or StretchBlt failed in Present

 

   if i ignore this error, it will work well in next frame.   

 

   so i want to know how to avoid this error when i create window in individual thread.

 

   is there any articles or topics?laugh.png

 

   my render work is also doing in the individual thread.rolleyes.gif



Sponsor:

#2 Hodgman   Moderators   -  Reputation: 31799

Like
0Likes
Like

Posted 13 November 2013 - 10:15 PM

According to the documentation, all D3D9 API calls should be made on the same thread where your window is created/updated unsure.png



#3 db123   Members   -  Reputation: 216

Like
0Likes
Like

Posted 13 November 2013 - 10:24 PM

According to the documentation, all D3D9 API calls should be made on the same thread where your window is created/updated unsure.png

if i create the window and process windows message in the render thread, it will block  rendering too, and then ,render thread will block the main thread, because main thread will wait render thread every frame.



#4 Nik02   Crossbones+   -  Reputation: 2917

Like
0Likes
Like

Posted 13 November 2013 - 11:26 PM

Since the problem is that the game logic is blocked, why don't you run that in a separate thread and let d3d happily work on the primary thread?

Niko Suni


#5 L. Spiro   Crossbones+   -  Reputation: 14244

Like
2Likes
Like

Posted 13 November 2013 - 11:55 PM

As just mentioned, you have no choice but to keep DirectX and the window on the same thread.

You are tackling the problem the wrong way.

If the logic is being blocked, put the logic on a second thread.

And it’s not really a problem anyway.  If you use fixed-step updates the worst that can happen is you lag for a bit in the online game.  This is a classic situation.  Hell, I used to resize my window on purpose in Starsiege: Tribes to make people think I was lagging out.

 

So I really don’t see what the problem is.

 

 

L. Spiro


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#6 ProgrammerDX   Members   -  Reputation: 175

Like
2Likes
Like

Posted 14 November 2013 - 04:37 AM

Catch the window events WM_ENTERSIZEMOVE and WM_ENTERMENULOOP to detect when user is dragging the window

In that window event, create a new Timer (SetTimer) for 100ms

then, catch WM_TIMER message, and in there, call your game loop

Delete the timer (KillTimer) on WM_EXITSIZEMOVE or WM_EXITMENULOOP

This will solve your problem




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.



PARTNERS