# OpenGL multisampling not working (and fix)

**URL:** <https://forum.juce.com/t/opengl-multisampling-not-working-and-fix/13564>\
**Category:** MacOSX and iOS\
**Created:** [September 14, 2014, 4:05pm UTC](https://forum.juce.com/t/opengl-multisampling-not-working-and-fix/13564 "2014-09-14T16:05:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unlinked](https://avatars.discourse-cdn.com/v4/letter/u/dc4da7/32.png) [@unlinked](https://forum.juce.com/u/unlinked)\
**Post date:** [September 14, 2014, 4:05pm UTC](https://forum.juce.com/t/opengl-multisampling-not-working-and-fix/13564/1 "2014-09-14T16:05:42Z")

</div>

Hey Jules,

As far as I can tell, OpenGL multisampling isn't working on iOS (current Git checkout as of today) - adding the following lines into the teapot OpenGLDemo constructor makes the OpenGL component render as an empty black box:

```
openGLContext.setMultisamplingEnabled(true);
OpenGLPixelFormat format;
format.multisamplingLevel = 4;
openGLContext.setPixelFormat(format);

... openGLContext.setRenderer (this); ... etc.
```

I think additional code is needed in the iOS OpenGLContext::NativeContext::swapBuffers():

```
void swapBuffers()
   {
       if (useMSAA)
       {
           glBindFramebuffer(GL_DRAW_FRAMEBUFFER_APPLE, frameBufferHandle);
           glBindFramebuffer(GL_READ_FRAMEBUFFER_APPLE, msaaBufferHandle);
           glResolveMultisampleFramebufferAPPLE();
       }
       ...existing code
```

Certainly seems to work here?

Steve.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [September 14, 2014, 7:10pm UTC](https://forum.juce.com/t/opengl-multisampling-not-working-and-fix/13564/2 "2014-09-14T19:10:12Z")

</div>

Ah, excellent stuff, ta very much!

---

<div class="post-metadata">

**Author:** ![unlinked](https://avatars.discourse-cdn.com/v4/letter/u/dc4da7/32.png) [@unlinked](https://forum.juce.com/u/unlinked)\
**Post date:** [September 14, 2014, 8:11pm UTC](https://forum.juce.com/t/opengl-multisampling-not-working-and-fix/13564/3 "2014-09-14T20:11:49Z")

</div>

Another possible fix for MSAA on iOS. When using FBOs within a multisampled context, the iOS OpenGLFrameBuffer::Pimpl::unbind call is failing.&nbsp; I think it's because the call to iOS OpenGLContext::NativeContext::getFrameBufferID() is returning the downsampled buffer ID, rather than the multisampled buffer ID and hence subsequent calls are writing to the wrong buffer.

The obvious fix is:

```
GLuint getFrameBufferID() const noexcept { return useMSAA ? msaaBufferHandle : frameBufferHandle; }
```

It seems to work, but there are a couple of other calls to that function, and I'm unsure as to whether either of those calls are expecting the downsampled buffer, so a different fix may be appropriate?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [September 16, 2014, 10:58am UTC](https://forum.juce.com/t/opengl-multisampling-not-working-and-fix/13564/4 "2014-09-16T10:58:29Z")

</div>

Seems legit!&nbsp;Can't see any problems, and if someone has chosen to use&nbsp;it in multisampled mode then it seems like this is probably what they'd intend to happen. I've checked in these changes now so give me a shout if I've broken anything!

---

<div class="post-metadata">

**Author:** ![unlinked](https://avatars.discourse-cdn.com/v4/letter/u/dc4da7/32.png) [@unlinked](https://forum.juce.com/u/unlinked)\
**Post date:** [December 9, 2014, 10:44pm UTC](https://forum.juce.com/t/opengl-multisampling-not-working-and-fix/13564/5 "2014-12-09T22:44:51Z")

</div>

I see some fixes were made to fix some deprecated APPLE-suffixed OpenGL calls for the iOS8 SDK, but seems in the latest version multisampling might be broken? I hit the problem in my own code but reproed using the OpenGLAppExample project on the Github head by adding a couple of lines:

OpenGLAppComponent::OpenGLAppComponent()&nbsp;&nbsp; : frameCounter (0)  
{  
&nbsp;&nbsp;&nbsp; setOpaque (true);  
&nbsp;&nbsp; &nbsp;

// -- lines added here  
&nbsp;&nbsp; &nbsp;juce::OpenGLPixelFormat pixelFormat;  
&nbsp;&nbsp; &nbsp;openGLContext.setMultisamplingEnabled(true);  
&nbsp;&nbsp; &nbsp;pixelFormat.multisamplingLevel = 2;  
&nbsp;&nbsp; &nbsp;openGLContext.setPixelFormat(pixelFormat);

// -- to here

&nbsp;&nbsp;&nbsp; openGLContext.setRenderer (this);  
&nbsp;&nbsp;&nbsp; openGLContext.attachTo (\*this);  
&nbsp;&nbsp;&nbsp; openGLContext.setContinuousRepainting (true);  
}

and they result in the following JUCE assert:

JUCE v3.1.0  
\*\*\*\*\* GL\_INVALID\_OPERATION&nbsp; at /Users/steve/personaldev/external/juce\_master/modules/juce\_opengl/opengl/juce\_OpenGLContext.cpp : 168  
JUCE Assertion failure in juce\_opengl.cpp:141

(there's a minor error in the shader in that example with a vec4 missing a precision specifier in the fragment shader, but apart from that this is straight from the example code.)

Problem seems to be consistent between iOS7.1 & 8.1. I'll dig around but if there's anything obvious I'm missing it would be good to know.

Cheers,

Steve.

&nbsp;
