Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS and multiple OpenGL views

I'm currently developping an iPad app which is using OpenGL to draw some very simple (no more than 1000 or 2000 vertices) rotating models in multiple OpenGL views. There are currently 6 view in a grid, each one running its own display link to update the drawing. Due to the simplicity of the models, it's by far the simplest method to do it, I don't have the time to code a full OpenGL interface.

Currently, it's doing well performance-wise, but there are some annoying glitches. The first 3 OpenGL views display without problems, and the last 3 only display a few triangles (while still retaining the ability to rotate the model). Also there are some cases where the glDrawArrays call is going straight into EXC_BAD_ACCESS (especially on the simulator), which tell me there is something wrong with the buffers.

What I checked (as well as double- and triple-checked) is :

  • Buffer allocation seems OK
  • All resources are freed on dealloc
  • The instruments show some warnings, but nothing that seems related

I'm thinking it's probably related to my having multiple views drawing at the same time, so is there any known thing I should have done there? Each view has its own context, but perhaps I'm doing something wrong with that...

Also, I just noticed that in the simulator, the afflicted views are flickering between the right drawing with all the vertices and the wrong drawing with only a few.

Anyway, if you have any ideas, thanks for sharing!

like image 315
F.X. Avatar asked Jan 19 '23 07:01

F.X.


1 Answers

Okay, I'm going to answer my own question since I finally found what was going on. It was a small missing line that was causing all those problems.

Basically, to have multiple OpenGL views displayed at the same time, you need :

  • Either, the same context for every view. Here, you have to take care not to draw with multiple threads at the same time (i.e. lock the context somehow, as explained on this answer. And you have to re-bind the frame- and render-buffers every time on each frame.
  • Or, you can use different contexts for each view. Then, you have to re-set the context on each frame, because other display links, could (and would, as in my case) cause your OpenGL calls to use the wrong data. Also, there is no need for re-binding frame- and render-buffers since your context is preserved.

Also, call glFlush() after each frame, to tell the GPU to finish rendering each frame fully.

In my case (the second one), the code for rendering each frame (in iOS) looks like :

- (void) drawFrame:(CADisplayLink*)displayLink {
  // Set current context, assuming _context
  // is the class ivar for the OpenGL Context
  [EAGLContext setCurrentContext:_context]

  // Clear whatever you want
  glClear (GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

  // Do matrix stuff
  ...
  glUniformMatrix4fv (...);

  // Set your viewport
  glViewport (0, 0, self.frame.size.width, self.frame.size.height);

  // Bind object buffers
  glBindBuffer (GL_ARRAY_BUFFER, _vertexBuffer);
  glVertexAttribPointer (_glVertexPositionSlot, 3, ...);

  // Draw elements
  glDrawArrays (GL_TRIANGLES, 0, _currentVertexCount);

  // Discard unneeded depth buffer
  const GLenum discard[] = {GL_DEPTH_ATTACHMENT};
  glDiscardFramebufferEXT (GL_FRAMEBUFFER, 1, discard);

  // Present render buffer
  [_context presentRenderbuffer:GL_RENDERBUFFER];

  // Unbind and flush
  glBindBuffer (GL_ARRAY_BUFFER, 0);
  glFlush();
}

EDIT

I'm going to edit this answer, since I found out that running multiple CADisplayLinks could cause some issues. You have to make sure to set the frameInterval property of your CADisplayLink instance to something other than 0 or 1. Else, the run loop will only have time to call the first render method, and then it'll call it again, and again. In my case, that was why only one object was moving. Now, it's set to 3 or 4 frames, and the run loop has time to call all the render methods.

This applies only to the application running on the device. The simulator, being very fast, doesn't care about such things.

like image 121
F.X. Avatar answered Jan 26 '23 00:01

F.X.