Jump to content

  • Log In with Google      Sign In   
  • Create Account


OpenGL + Threaded load + Flickering


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Raptisoft   Members   -  Reputation: 171

Like
0Likes
Like

Posted 16 June 2013 - 10:25 AM

Hi all...

 

I'm porting to OSX+OpenGL... coming from Windows+DirectX.

 

In the DirectX program, images are loaded in a seperate thread, while the main program displays a progress bar.

 

Using CGLGetCurrentContext, CGLEnable([context],kCGLCEMPEngine), and CGLLockContext/CGLUnlockContext, I'm duplicating this on Mac with XCode 4.  It works.

 

It works, except for one thing: During threaded load, I am getting flickering while drawing the loading bar/title screen.  The loading bar/title screen graphics are loaded BEFORE the thread load.  The flickering appears to be related to graphics that are being loaded at the time.  However, I have surrounded all my OpenGL calls (or groups of calls) with CGLLockContext/UnlockContext calls.  I assume there's some sort of gotcha that is slipping through.  Yes, I have confirmed that *all* my GL calls or clusters of calls have the Lock/Unlock around them.

 

Anyone have any idea what could cause this?  Near as I can tell nothing is unblocked.

 

Some example code:

#define THREADLOCK CGLLockContext(gOpenGLContext);
#define THREADUNLOCK  CGLUnlockContext(gOpenGLContext);

void SetTexture(int theNumber)
{
    THREADLOCK;
    if (theNumber==-1)
    {
        glDisable(GL_TEXTURE_2D);
    }
    else
    {
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D,gTextureList[theNumber].mGLTexture);    
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,gFilter);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,gFilter);
    }
    THREADUNLOCK;
}

void RenderAdditive()
{
    THREADLOCK;
    glBlendFunc(GL_SRC_ALPHA,GL_ONE);
    THREADUNLOCK;
}

void DrawIndexedShape(void *theVertexBuffer, void* theIndexBuffer, int theVertexCount, int theTriangleCount, int theVertexStride)
{
    THREADLOCK;
    Vertex2DRef *aRef=(Vertex2DRef*)theVertexBuffer;
    glVertexPointer(3,GL_FLOAT,theVertexStride,&aRef->mX);
    glColorPointer(4,GL_UNSIGNED_BYTE,theVertexStride,&aRef->mDiffuse);
    glTexCoordPointer(2,GL_FLOAT,theVertexStride,&aRef->mTextureU);
    glDrawElements(GL_TRIANGLES,theTriangleCount*3,GL_UNSIGNED_SHORT,theIndexBuffer);
    THREADUNLOCK;
}



So as you can see, very basic stuff.  I would normally assume that the flickering was caused by two threads trying to access the context at the same time, but I do have everything bounded by those THREADLOCK/THREADUNLOCK statements.  Can anyone offer assistance?

 



Sponsor:

#2 C0lumbo   Crossbones+   -  Reputation: 1839

Like
0Likes
Like

Posted 18 June 2013 - 01:08 AM

I'm not sure what might be going wrong with your CGLLockContext/CGLUnlockContext approach to the problem. I don't see any reason why it shouldn't work.

 

However if you're stuck and looking for a workable workaround for your loading screens you might find that a better solution is to have a separate OpenGL context for your loading thread, which shares data with your main OpenGL context. I think this is a more robust solution overall because it doesn't require you to remember to lock/unlock around every single OpenGL call in your entire codebase. To see how OpenGL share groups work on OSX you can look at the documentation for CGLCreateContext (the share parameter) and there's some higher level documentation here: https://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/opengl-macprogguide/opengl_threading/opengl_threading.html






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS