Early exit related thread

pixelshader

Newcomer
Hello,

I am trying to optimise an algorithm using CG. The kernel calculates Mutual information. It has to be executed for a fixed number of iteration. But it can do an early exit in case an acceptable MI value is achieved.

while ( MI > 0.8 )
{
Call Render function to execute kernel.
Retrieve MI value to CPU.

}

The kernel outputs MI value to a texture location. So in order to take decision i need to read that value to CPU which i think can be a performance hit.

So how shud i go forward.. Please suggest.
Am using Opengl + Cg

Thanks..
 
You could try using conditional rendering. It's part of the OpenGL core since version 3.0, or as GL_NV_conditional_render.

Basically you do like this:

Code:
for (int i = 0; i < max_loops; i++)
{
    if (i > 0)
        BeginConditionalRender();
    ExecuteKernel();
    if (i > 0)
        EndConditionalRender();

    BeginQuery();
    RenderCondition();
    EndQuery();
}
The RenderCondition() would be a simply shader that simply tests the condition and discards the fragment if the condition succeeds. If you MI value is just a single float, you only need to render a single point. If you have more values that needs to be tested you need to render more points. If all fragments are discarded the occlusion query will return zero and the code inside the conditional render path will not be executed. This eliminated the CPU readback and can run entirely asynchronously on the GPU.
 
Back
Top