Jump to content
  • Advertisement
Sign in to follow this  
GL1zdA

Rendering in background thread (Win32)

This topic is 3622 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a problem with doing my rendering in a different thread. This is the basic, where everything is done in one thread, but it works:
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){
     ...
    HGLRC hRC;
     ...
    hRC = InitDC(hwnd);
     ...
    // Display the window
    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    // Message loop
    while(!done){
        while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
            if(msg.message == WM_QUIT){
                done = TRUE;
            } else {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
            }
        }
        // rendering
        {
            RenderScene();
            SwapBuffers(hDC);
        }
    }
}

I tried to add a second thread like this (hDC is global):
...
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);

wglMakeCurrent(GetDC(hwnd), NULL);
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)RenderingThread,NULL,0,NULL);

// Message loop
while(!done){
...

void RenderingThread(PVOID pvoid){
    HGLRC hRC = wglCreateContext(hDC);
    wglMakeCurrent(hDC,hRC);
    while(TRUE){
        RenderScene();
        SwapBuffers(hDC);
    }
}

But it doesn't work - I only get a black screen (But ATI Tray Tools show a FPS counter with framerate as if it would work). I tried passing the HGLRC:
...
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);

wglMakeCurrent(GetDC(hwnd), NULL);
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)RenderingThread,hRC,0,NULL);

// Message loop
while(!done){
...

void RenderingThread(PVOID pvoid){
    wglMakeCurrent(hDC, (HGLRC) pvoid);
    while(TRUE){
        RenderScene();
        SwapBuffers(hDC);
    }
}

and it works without problems. But I don't know why. Could someone help me understand, where is the problem in the first approach?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by V-man
Are you checking so see if HGLRC hRC = wglCreateContext(hDC); returns a valid value?


Yes. I tried:


if(!wglMakeCurrent(hDC,hRC)) MessageBox(NULL, TEXT("makeCurrent failure"), szAppName, MB_ICONERROR);




But it passes the test. I tried debugging it and if I use wglCreateContext(hDC) in the rendering thread instead of passing the hRC I already have, I get a different HGLRC handle than the one I get in the main thread, where I setup it, so I guess it's a different RC. Is this how it should work?

Share this post


Link to post
Share on other sites
In principle it should work because Microsoft doesn't mention threads
http://msdn.microsoft.com/en-us/library/ms537549(VS.85).aspx

but anyway, you have already found a solution so keep it.

Share this post


Link to post
Share on other sites
You only need to create a context if you are creating and rendering into multiple windows. Before rendering on a different thread you need to make the context current on the thread otherwise all OpenGl commands will be ignored.

Says so on the MSDN: http://msdn.microsoft.com/en-us/library/ms537558(VS.85).aspx

This helps to: http://msdn.microsoft.com/en-us/library/ms537547(VS.85).aspx

OpenGl's website had something which I can't seem to find anymore.

Share this post


Link to post
Share on other sites
Thanks for all answers. It seems it works. I just hope it won't crash in the most unexpected moment.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!