CPU & GPU Multithread

Hi all.

I want to use two threads for my test_game: one is a Draw_Thread which renders various objects using GLSL, and another is a Calculate_Thread which does some calculation like AI, collision detections, etc.

I know OpenGL’s rendering can’t work when initializing and drawing are executed different thread,
(it is based on context problem)
so I make Draw_Thread doing without using thread.

Then, construction of my program is as follows.

main()
{
 initialize_gl();
 /*mainloop*/
 while( some_condition ){
  createthread(Calculate_Thread);
  drawstate();
  jointhread(Calculate_Thread);
 }
}

(Some problems

  • you shouldn’t use create/join thread at any loop
  • where are your program do asynchrous?
    are out of scope in this topic.)

Now I execute it on a Linux PC, so createthread is pthread_create and jointhread is pthread_join.
For simplicity, drawState includes only one pair of glBegin/glEnd and some instruction(viewport setup, texture binding, shader activate, etc).
In addition, I make glsl program too heavy and measure the time of executing rendering and the time of executing AI(and etc).

In my prediction, because CPU works little while executing shader, CPU and GPU can execute computation by parallel.
(Even if CPU require 10sec for calculation and GPU require 10sec for calculation, the real execution time does not require 20sec.)
But, the time of drawstate growth when Calculate_Thread do much calculation.

I can’t understand why the time of drawstate growth.
Please teach me why. (and how do I create parallel program?)

Thanks.

glutSwapBuffer (as glFinish) is waiting GPU. As is well known “glFinish does not return until all
effects from previously issued commands on GL client and server state and the
framebuffer are fully realized.” (- ogl 2.0 spec)

You can make it:
glFlush
Calculate
glutSwapBuffer

As long as you don’t do anything to wait for the GPU to finish (such as glFinish or reading back data), rendering will be done in parallel anyway.

That means, as long as your app is not too complex, and you don’t need to program for multiple core CPUs, it is wasted time to try to make rendering and other computations parallel through threads, since it is done in parallel even without you doing anything special!

In fact the overhead of thread-manangement might slow you down (although certainly not much).

Jan.

Thanks for advices.

For more analysis, I executed more experiments.

-experiment 1
For confirming a overhead of handling thread,
I make Calculate_Thread a function executing only return 0;
As a result, execution time become short as there is no thread.
Thus, it is thought that a overhead of handling thread is very small.

-experiment 2
I dare use glFinish() before glBegin/glEnd and after glBegin/glEnd and execute program.
Then, execution time become long when CPU is busy compared with the case of CPU is not busy.
(In addition, I know glFinish does not return until finish issues.
Now, does glFinish make CPU busy ? Depend on implementation of driver ?)

As a result, it thought that OpenGL’s rendering needs some degree of high CPU’s power.
Are there any method to render (and shading) without CPU power (or very low power) ?

You might want to take a look into “Vertex Arrays”.

There you put all your data together in one big array, tell the GPU, “hey you, render this!” and from that point on the CPU is not involved in the rendering anymore, at all.

Jan.

Originally posted by tgbt:

Now, does glFinish make CPU busy ? Depend on implementation of driver ?

Indeed.

Originally posted by tgbt:

As a result, it thought that OpenGL’s rendering needs some degree of high CPU’s power.
Are there any method to render (and shading) without CPU power (or very low power) ?

glFinish is great (main) consumer of CPU power (or any gl waitings of GPU). glFinish is simply cyclic check (without of CPU sleep) of GPU idle state. So, before and instead of glFinish you might to use CPU for your calculations.