Jump to content
  • Advertisement
Sign in to follow this  
raptor_xxl

OpenGL Rendering in another thread

This topic is 3845 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'm trying to make the rendering of the scene in a diffrent thread than the one that maintaines game's logic. I'm using OpenGL. I downloaded NeHe's lesson 5, compiled and it works. I moved the SwapBuffers(hDC); to the end of DrawGlScene function - it works. Then I created just before the main loop a new thread this way: HANDLE hThread = (HANDLE)_beginthread( &gpuThreadFunction, 0, NULL); and gpuThreadFunction: void __cdecl gpuThreadFunction( void* arguments ) { while(1) DrawGLScene(); } And now it doesn't render anything. The thread is working - checked with debuger. Does anyone know how to make it work?

Share this post


Link to post
Share on other sites
Advertisement
trying to multi thread a render is usually pointless becouse its already multi threaded by the gpu, adding more cpu threads won't accomplish anything

EDIT:

if you just want to create a new thread exclusivity for the rendering just pass the hdc to it and have all the initialization done in it

Share this post


Link to post
Share on other sites
Thanks, now it works. And about the point of doing it multithreaded - there is some, but it will be usefull if you have 2 or more processors. When you call GPU functions to render something, than the CPU waits for the GPU for finish and from my practise I know that CPU can't do anything else when waiting for the GPU. I think the reason why are these functions blocking is that afterall the GPU renders quite fast so the CPU waits not so long and changing threads take some time, so when the CPU would change thread after giving GPU data, than we would waiste lot of time for comming back to the rendering thread. And my second argument is that from what I know modern engines like Unreal 3 engine have threads for rendering, AI, physics, network, and probably something else. So there must be a point to make the rendering in a diffrent thread.

Share this post


Link to post
Share on other sites
Quote:
Original post by raptor_xxl
Thanks, now it works. And about the point of doing it multithreaded - there is some, but it will be usefull if you have 2 or more processors. When you call GPU functions to render something, than the CPU waits for the GPU for finish and from my practise I know that CPU can't do anything else when waiting for the GPU. I think the reason why are these functions blocking is that afterall the GPU renders quite fast so the CPU waits not so long and changing threads take some time, so when the CPU would change thread after giving GPU data, than we would waiste lot of time for comming back to the rendering thread. And my second argument is that from what I know modern engines like Unreal 3 engine have threads for rendering, AI, physics, network, and probably something else. So there must be a point to make the rendering in a diffrent thread.


You are incorrect in most cases about the CPU waiting for the GPU to render something. Practically all rendering functions place commands on a "display list" or "command buffer" which the GPU reads independently of the CPU. The CPU only takes long enough to place the correct commands on the display list in response to your function calls. The few places that the CPU waits for the GPU are when the CPU is expecting to access results of rendering like saving a screenshot (and remember, simply displaying results to the screen is not a CPU task :) ), or vsync which has to slow the CPU down otherwise you rack up commands too many frames ahead of the displayed frame and you get what seems like input lag!

DirectX has grave warnings about using it from multiple threads, it has a creation flag that allows it but contains CPU performance penalties, as access to the display list or hardware registers now has to be in critical sections.

In general throwing non-rendering tasks like AI or file reads into other threads is a fine idea. Calling rendering functions from multiple threads/not the first thread in any API can be very tricky.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!