1

I've seen many docs about the concurrency programming for opengles(iOS), still can't solve my problem, so I ask here for your help.

I followed the instruction, create two threads, each thread owns a context, and make them the same sharegroup, render objects in main thread, and create object in second thread.

What I can't understand is I just can't render the objects that created in the second threads. (if I move the object creation code back to main thread then it works.)

I did glFlush() after the object's setup sequence. I just don't get it.

I use the default opengl demo App that generated by the XCode4. and add codes like this for testing:

-(void)setupGL
{

    [EAGLContext setCurrentContext:self.context];
    self.context2 = [ [ EAGLContext alloc ] initWithAPI: kEAGLRenderingAPIOpenGLES2 sharegroup: self.context.sharegroup ];

    if( !self.context2 )
    {
        printf( " error !!!" );
    }
    if( self.context.sharegroup != self.context2.sharegroup )
    {
        printf( " error2 !!!" );
    }    

    ... self.effect = ....
    ... glEnable....
    ...

    [ self performSelectorInBackground: @selector(indicator) withObject: nil ];
}

-(void)indicator // run this in another thread
{
    [EAGLContext setCurrentContext:self.context2];

    glGenVertexArraysOES(1, &_vertexArray);
    glBindVertexArrayOES(_vertexArray);

    glGenBuffers(1, &_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer2);
    glBufferData(GL_ARRAY_BUFFER, sizeof(gCubeVertexData2), gCubeVertexData2, GL_STATIC_DRAW);

    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));
    glEnableVertexAttribArray(GLKVertexAttribNormal);
    glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(12));

    glBindVertexArrayOES(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glFlush();
    [ EAGLContext setCurrentContext: nil ];
}

- (void)update
{
    .... generated by XCode4 ....
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    .... generated by XCode4 ....
}

What did I miss ??

I found that if I generate and setup objects in main thread, I can still bind and modify objects' data in second thread, and render in main thread correctly.

1 Answers1

1

I think this might answer your question:

Can Vertex Array Objects (VAOs) be shared across EAGLContexts in OpenGL ES?

Most object types will be shared across the sharegroup, but VAOs are explicitly not supported.

Community
  • 1
  • 1
Nik
  • 1,364
  • 8
  • 7