Hi,
I want to do some optimization by using the real potential of VBO : multithreading.
The game life cycle is composed of 3 steps :
- logic
- CPU -> GPU data transmission
- rendering
Those steps are organized in 2 threads :
[tr][td]OpenGL Thread[/td][td][/td][td]Transmission[/td][td]Rendering[/td][td]Transmission[/td][td]Rendering[/td][td]Transmission[/td][td]Rendering[/td][/tr]
[tr][td]Logic Thread[/td][td]Logic[/td][td]—[/td][td]Logic[/td][td]—[/td][td]Logic[/td][td]—[/td][td]Logic[/td][/tr]
So the two thread cycles look like :
Logic thread :
while (!finished)
{
waitForFPS();
logic(time);
mutex.endOfLogic();
}
Rendering thread :
while (!finished)
{
mutex.waitForLogic();
transmit();
mutex.endOfTransmition();
draw();
mutex.endOfDrawing();
}
I have managed to create the “Mutex” class to synchronized the two thread and it works. But I’m not 100% sure of it and I’m not really happy about it :
/**
*
* @author Bonbon-Chan
*/
public class Mutex
{
private boolean lockLogic = true;
private Object logic = new Object();
private boolean lockRendering = false;
private Object rendering = new Object();
public void waitForLogic()
{
while(lockLogic)
{
synchronized(rendering)
{
try { rendering.wait(18); } catch(Exception ex) {}
}
}
}
public void endOfTransmition()
{
synchronized(logic)
{
lockLogic = true;
logic.notify();
}
}
public void endOfDrawing()
{
synchronized(logic)
{
lockRendering = false;
logic.notify();
}
}
public void endOfLogic()
{
while(lockRendering) // Wait for end of drawing;
{
synchronized(logic)
{
try { logic.wait(18); } catch(Exception ex) { ex.printStackTrace(); }
}
}
synchronized(rendering) // Start Transmission
{
lockLogic = false;
lockRendering = true;
rendering.notify();
}
while(!lockLogic) // Wait for end of transmission
{
synchronized(logic)
{
try { logic.wait(18); } catch(Exception ex) { ex.printStackTrace(); }
}
}
}
}
What do you think about it ?
