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

Started by
4 comments, last by ProgrammerDX 10 years, 5 months ago

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

Advertisement

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

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.

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

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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

This topic is closed to new replies.

Advertisement