I'm currently developing a 3D game with its own engine. I have some performance issues in the function that orders all the draw events and sends them to the GPU. I've heard about threads, and I thought that it could be nice if I could pass that code to another thread. The structure is something like:
Global function that receives all events and calls OpenGL to draw
void DrawAll(float dt, vector<draw_events>)
Function inside the EventSystem, that calls the previous function
Events passed by copy, that have pointers to objects in the scene (scene is in main thread)
void ModuleEventSystem::PostUpdate(float dt)
{
DrawAll(dt, draw_events);
}
This has been tested, and has worked well.
Now I have inside:
std::thread draw_thread;
void ModuleEventSystem::PostUpdate(float dt)
{
if(draw_thread.IsJoinable())
draw_thread.Join();
draw_thread = std::thread(DrawAll, dt, events_to_draw);
}
The problem: not only the performance as become worse, objects in scene doesn't draw at all. I have never worked on threads before, so I don't know if the issue is having a thread with OpenGl calls and pointers pointing to objects in the main thread, or is the bad structure of code... Or even if this kind of optimization is not possible at all. Thank all of you.