Added MSAA support for OpenGL ES contexts on iOS.
authorAlex Szpakowski <slime73@gmail.com>
Sun, 19 Jul 2015 19:44:40 -0300
changeset 98101e079120100e
parent 9809 7f019df79cfc
child 9811 9d44e8794a9b
Added MSAA support for OpenGL ES contexts on iOS.

Note that extra steps must be taken when using glReadPixels to read the contents of the main OpenGL ES framebuffer on iOS, if multisampling is used. See the OpenGL ES section of README-ios.md for details.
docs/README-ios.md
include/SDL_syswm.h
src/video/uikit/SDL_uikitopengles.m
src/video/uikit/SDL_uikitopenglview.h
src/video/uikit/SDL_uikitopenglview.m
src/video/uikit/SDL_uikitwindow.m
     1.1 --- a/docs/README-ios.md	Sat Jul 18 00:04:49 2015 -0400
     1.2 +++ b/docs/README-ios.md	Sun Jul 19 19:44:40 2015 -0300
     1.3 @@ -1,14 +1,14 @@
     1.4 -iOS
     1.5 -======
     1.6 -
     1.7 +iOS
     1.8 +======
     1.9 +
    1.10  ==============================================================================
    1.11 -Building the Simple DirectMedia Layer for iPhone OS 5.1
    1.12 +Building the Simple DirectMedia Layer for iOS 5.1+
    1.13  ==============================================================================
    1.14  
    1.15 -Requirements: Mac OS X v10.5 or later and the iPhone SDK.
    1.16 +Requirements: Mac OS X 10.8 or later and the iOS 7+ SDK.
    1.17  
    1.18  Instructions:
    1.19 -1.  Open SDL.xcodeproj (located in Xcode-iOS/SDL) in XCode.
    1.20 +1.  Open SDL.xcodeproj (located in Xcode-iOS/SDL) in Xcode.
    1.21  2.  Select your desired target, and hit build.
    1.22  
    1.23  There are three build targets:
    1.24 @@ -134,13 +134,21 @@
    1.25  Notes -- OpenGL ES
    1.26  ==============================================================================
    1.27  
    1.28 -Your SDL application for iPhone uses OpenGL ES for video by default.
    1.29 +Your SDL application for iOS uses OpenGL ES for video by default.
    1.30  
    1.31 -OpenGL ES for iPhone supports several display pixel formats, such as RGBA8 and RGB565, which provide a 32 bit and 16 bit color buffer respectively.  By default, the implementation uses RGB565, but you may use RGBA8 by setting each color component to 8 bits in SDL_GL_SetAttribute.
    1.32 +OpenGL ES for iOS supports several display pixel formats, such as RGBA8 and RGB565, which provide a 32 bit and 16 bit color buffer respectively. By default, the implementation uses RGB565, but you may use RGBA8 by setting each color component to 8 bits in SDL_GL_SetAttribute.
    1.33  
    1.34  If your application doesn't use OpenGL's depth buffer, you may find significant performance improvement by setting SDL_GL_DEPTH_SIZE to 0.
    1.35  
    1.36 -Finally, if your application completely redraws the screen each frame, you may find significant performance improvement by setting the attribute SDL_GL_RETAINED_BACKING to 1.
    1.37 +Finally, if your application completely redraws the screen each frame, you may find significant performance improvement by setting the attribute SDL_GL_RETAINED_BACKING to 0.
    1.38 +
    1.39 +OpenGL ES on iOS doesn't use the traditional system-framebuffer setup provided in other operating systems. Special care must be taken because of this:
    1.40 +
    1.41 +- The drawable Renderbuffer must be bound to the GL_RENDERBUFFER binding point when SDL_GL_SwapWindow is called.
    1.42 +- The drawable Framebuffer Object must be bound while rendering to the screen and when SDL_GL_SwapWindow is called.
    1.43 +- If multisample antialiasing (MSAA) is used and glReadPixels is used on the screen, the drawable framebuffer must be resolved to the MSAA resolve framebuffer (via glBlitFramebuffer or glResolveMultisampleFramebufferAPPLE), and the MSAA resolve framebuffer must be bound to the GL_READ_FRAMEBUFFER binding point, before glReadPixels is called.
    1.44 +
    1.45 +The above objects can be obtained via SDL_GetWindowWMInfo (in SDL_syswm.h).
    1.46  
    1.47  ==============================================================================
    1.48  Notes -- Keyboard
    1.49 @@ -189,7 +197,7 @@
    1.50  	The optimal texture formats on iOS are SDL_PIXELFORMAT_ABGR8888, SDL_PIXELFORMAT_ABGR8888, SDL_PIXELFORMAT_BGR888, and SDL_PIXELFORMAT_RGB24 pixel formats.
    1.51  
    1.52  Loading Shared Objects:
    1.53 -	This is disabled by default since it seems to break the terms of the iPhone SDK agreement.  It can be re-enabled in SDL_config_iphoneos.h.
    1.54 +	This is disabled by default since it seems to break the terms of the iOS SDK agreement for iOS versions prior to iOS 8. It can be re-enabled in SDL_config_iphoneos.h.
    1.55  
    1.56  ==============================================================================
    1.57  Game Center 
     2.1 --- a/include/SDL_syswm.h	Sat Jul 18 00:04:49 2015 -0400
     2.2 +++ b/include/SDL_syswm.h	Sun Jul 19 19:44:40 2015 -0300
     2.3 @@ -232,6 +232,7 @@
     2.4  #endif
     2.5              GLuint framebuffer; /* The GL view's Framebuffer Object. It must be bound when rendering to the screen using GL. */
     2.6              GLuint colorbuffer; /* The GL view's color Renderbuffer Object. It must be bound when SDL_GL_SwapWindow is called. */
     2.7 +            GLuint resolveFramebuffer; /* The Framebuffer Object which holds the resolve color Renderbuffer, when MSAA is used. */
     2.8          } uikit;
     2.9  #endif
    2.10  #if defined(SDL_VIDEO_DRIVER_WAYLAND)
     3.1 --- a/src/video/uikit/SDL_uikitopengles.m	Sat Jul 18 00:04:49 2015 -0400
     3.2 +++ b/src/video/uikit/SDL_uikitopengles.m	Sun Jul 19 19:44:40 2015 -0300
     3.3 @@ -141,11 +141,16 @@
     3.4          CGRect frame = UIKit_ComputeViewFrame(window, data.uiwindow.screen);
     3.5          EAGLSharegroup *sharegroup = nil;
     3.6          CGFloat scale = 1.0;
     3.7 +        int samples = 0;
     3.8  
     3.9          /* The EAGLRenderingAPI enum values currently map 1:1 to major GLES
    3.10           * versions. */
    3.11          EAGLRenderingAPI api = _this->gl_config.major_version;
    3.12  
    3.13 +        if (_this->gl_config.multisamplebuffers > 0) {
    3.14 +            samples = _this->gl_config.multisamplesamples;
    3.15 +        }
    3.16 +
    3.17          if (_this->gl_config.share_with_current_context) {
    3.18              EAGLContext *context = (__bridge EAGLContext *) SDL_GL_GetCurrentContext();
    3.19              sharegroup = context.sharegroup;
    3.20 @@ -182,6 +187,7 @@
    3.21                                                  depthBits:_this->gl_config.depth_size
    3.22                                                stencilBits:_this->gl_config.stencil_size
    3.23                                                       sRGB:_this->gl_config.framebuffer_srgb_capable
    3.24 +                                             multisamples:samples
    3.25                                                    context:context];
    3.26  
    3.27          if (!view) {
     4.1 --- a/src/video/uikit/SDL_uikitopenglview.h	Sat Jul 18 00:04:49 2015 -0400
     4.2 +++ b/src/video/uikit/SDL_uikitopenglview.h	Sun Jul 19 19:44:40 2015 -0300
     4.3 @@ -21,7 +21,7 @@
     4.4  
     4.5  #import <UIKit/UIKit.h>
     4.6  #import <OpenGLES/EAGL.h>
     4.7 -#import <OpenGLES/ES2/gl.h>
     4.8 +#import <OpenGLES/ES3/gl.h>
     4.9  
    4.10  #import "SDL_uikitview.h"
    4.11  #include "SDL_uikitvideo.h"
    4.12 @@ -38,6 +38,7 @@
    4.13                      depthBits:(int)depthBits
    4.14                    stencilBits:(int)stencilBits
    4.15                           sRGB:(BOOL)sRGB
    4.16 +                 multisamples:(int)multisamples
    4.17                        context:(EAGLContext *)glcontext;
    4.18  
    4.19  @property (nonatomic, readonly, weak) EAGLContext *context;
    4.20 @@ -48,6 +49,7 @@
    4.21  
    4.22  @property (nonatomic, readonly) GLuint drawableRenderbuffer;
    4.23  @property (nonatomic, readonly) GLuint drawableFramebuffer;
    4.24 +@property (nonatomic, readonly) GLuint msaaResolveFramebuffer;
    4.25  
    4.26  - (void)swapBuffers;
    4.27  
     5.1 --- a/src/video/uikit/SDL_uikitopenglview.m	Sat Jul 18 00:04:49 2015 -0400
     5.2 +++ b/src/video/uikit/SDL_uikitopenglview.m	Sun Jul 19 19:44:40 2015 -0300
     5.3 @@ -34,8 +34,18 @@
     5.4      /* The depth buffer that is attached to viewFramebuffer, if it exists. */
     5.5      GLuint depthRenderbuffer;
     5.6  
     5.7 +    GLenum colorBufferFormat;
     5.8 +
     5.9      /* format of depthRenderbuffer */
    5.10      GLenum depthBufferFormat;
    5.11 +
    5.12 +    /* The framebuffer and renderbuffer used for rendering with MSAA. */
    5.13 +    GLuint msaaFramebuffer, msaaRenderbuffer;
    5.14 +
    5.15 +    /* The number of MSAA samples. */
    5.16 +    int samples;
    5.17 +
    5.18 +    BOOL retainedBacking;
    5.19  }
    5.20  
    5.21  @synthesize context;
    5.22 @@ -57,6 +67,7 @@
    5.23                      depthBits:(int)depthBits
    5.24                    stencilBits:(int)stencilBits
    5.25                           sRGB:(BOOL)sRGB
    5.26 +                 multisamples:(int)multisamples
    5.27                        context:(EAGLContext *)glcontext
    5.28  {
    5.29      if ((self = [super initWithFrame:frame])) {
    5.30 @@ -65,6 +76,8 @@
    5.31          NSString *colorFormat = nil;
    5.32  
    5.33          context = glcontext;
    5.34 +        samples = multisamples;
    5.35 +        retainedBacking = retained;
    5.36  
    5.37          if (!context || ![EAGLContext setCurrentContext:context]) {
    5.38              SDL_SetError("Could not create OpenGL ES drawable (could not make context current)");
    5.39 @@ -75,6 +88,7 @@
    5.40              /* sRGB EAGL drawable support was added in iOS 7. */
    5.41              if (UIKit_IsSystemVersionAtLeast(7.0)) {
    5.42                  colorFormat = kEAGLColorFormatSRGBA8;
    5.43 +                colorBufferFormat = GL_SRGB8_ALPHA8;
    5.44              } else {
    5.45                  SDL_SetError("sRGB drawables are not supported.");
    5.46                  return nil;
    5.47 @@ -82,9 +96,11 @@
    5.48          } else if (rBits >= 8 || gBits >= 8 || bBits >= 8) {
    5.49              /* if user specifically requests rbg888 or some color format higher than 16bpp */
    5.50              colorFormat = kEAGLColorFormatRGBA8;
    5.51 +            colorBufferFormat = GL_RGBA8;
    5.52          } else {
    5.53              /* default case (potentially faster) */
    5.54              colorFormat = kEAGLColorFormatRGB565;
    5.55 +            colorBufferFormat = GL_RGB565;
    5.56          }
    5.57  
    5.58          CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
    5.59 @@ -117,7 +133,31 @@
    5.60          glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
    5.61          glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);
    5.62  
    5.63 -        if ((useDepthBuffer) || (useStencilBuffer)) {
    5.64 +        if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
    5.65 +            SDL_SetError("Failed creating OpenGL ES framebuffer");
    5.66 +            return nil;
    5.67 +        }
    5.68 +
    5.69 +        /* When MSAA is used we'll use a separate framebuffer for rendering to,
    5.70 +         * since we'll need to do an explicit MSAA resolve before presenting. */
    5.71 +        if (samples > 0) {
    5.72 +            glGenFramebuffers(1, &msaaFramebuffer);
    5.73 +            glBindFramebuffer(GL_FRAMEBUFFER, msaaFramebuffer);
    5.74 +
    5.75 +            glGenRenderbuffers(1, &msaaRenderbuffer);
    5.76 +            glBindRenderbuffer(GL_RENDERBUFFER, msaaRenderbuffer);
    5.77 +
    5.78 +            glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, colorBufferFormat, backingWidth, backingHeight);
    5.79 +
    5.80 +            glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, msaaRenderbuffer);
    5.81 +
    5.82 +            if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
    5.83 +                SDL_SetError("Failed creating OpenGL ES framebuffer: Unsupported MSAA sample count");
    5.84 +                return nil;
    5.85 +            }
    5.86 +        }
    5.87 +
    5.88 +        if (useDepthBuffer || useStencilBuffer) {
    5.89              if (useStencilBuffer) {
    5.90                  /* Apparently you need to pack stencil and depth into one buffer. */
    5.91                  depthBufferFormat = GL_DEPTH24_STENCIL8_OES;
    5.92 @@ -129,7 +169,13 @@
    5.93  
    5.94              glGenRenderbuffers(1, &depthRenderbuffer);
    5.95              glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
    5.96 -            glRenderbufferStorage(GL_RENDERBUFFER, depthBufferFormat, backingWidth, backingHeight);
    5.97 +
    5.98 +            if (samples > 0) {
    5.99 +                glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, depthBufferFormat, backingWidth, backingHeight);
   5.100 +            } else {
   5.101 +                glRenderbufferStorage(GL_RENDERBUFFER, depthBufferFormat, backingWidth, backingHeight);
   5.102 +            }
   5.103 +
   5.104              if (useDepthBuffer) {
   5.105                  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);
   5.106              }
   5.107 @@ -158,7 +204,23 @@
   5.108  
   5.109  - (GLuint)drawableFramebuffer
   5.110  {
   5.111 -    return viewFramebuffer;
   5.112 +    /* When MSAA is used, the MSAA draw framebuffer is used for drawing. */
   5.113 +    if (msaaFramebuffer) {
   5.114 +        return msaaFramebuffer;
   5.115 +    } else {
   5.116 +        return viewFramebuffer;
   5.117 +    }
   5.118 +}
   5.119 +
   5.120 +- (GLuint)msaaResolveFramebuffer
   5.121 +{
   5.122 +    /* When MSAA is used, the MSAA draw framebuffer is used for drawing and the
   5.123 +     * view framebuffer is used as a MSAA resolve framebuffer. */
   5.124 +    if (msaaFramebuffer) {
   5.125 +        return viewFramebuffer;
   5.126 +    } else {
   5.127 +        return 0;
   5.128 +    }
   5.129  }
   5.130  
   5.131  - (void)updateFrame
   5.132 @@ -172,9 +234,19 @@
   5.133      glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
   5.134      glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);
   5.135  
   5.136 +    if (msaaRenderbuffer != 0) {
   5.137 +        glBindRenderbuffer(GL_RENDERBUFFER, msaaRenderbuffer);
   5.138 +        glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, GL_RGBA8, backingWidth, backingHeight);
   5.139 +    }
   5.140 +
   5.141      if (depthRenderbuffer != 0) {
   5.142          glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
   5.143 -        glRenderbufferStorage(GL_RENDERBUFFER, depthBufferFormat, backingWidth, backingHeight);
   5.144 +
   5.145 +        if (samples > 0) {
   5.146 +            glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, depthBufferFormat, backingWidth, backingHeight);
   5.147 +        } else {
   5.148 +            glRenderbufferStorage(GL_RENDERBUFFER, depthBufferFormat, backingWidth, backingHeight);
   5.149 +        }
   5.150      }
   5.151  
   5.152      glBindRenderbuffer(GL_RENDERBUFFER, prevRenderbuffer);
   5.153 @@ -197,10 +269,47 @@
   5.154              glLabelObjectEXT(GL_RENDERBUFFER, depthRenderbuffer, 0, "context depth buffer");
   5.155          }
   5.156      }
   5.157 +
   5.158 +    if (msaaFramebuffer != 0) {
   5.159 +        glLabelObjectEXT(GL_FRAMEBUFFER, msaaFramebuffer, 0, "context MSAA FBO");
   5.160 +    }
   5.161 +
   5.162 +    if (msaaRenderbuffer != 0) {
   5.163 +        glLabelObjectEXT(GL_RENDERBUFFER, msaaRenderbuffer, 0, "context MSAA renderbuffer");
   5.164 +    }
   5.165  }
   5.166  
   5.167  - (void)swapBuffers
   5.168  {
   5.169 +    if (msaaFramebuffer) {
   5.170 +        const GLenum attachments[] = {GL_COLOR_ATTACHMENT0};
   5.171 +
   5.172 +        glBindFramebuffer(GL_DRAW_FRAMEBUFFER, viewFramebuffer);
   5.173 +
   5.174 +        /* OpenGL ES 3+ provides explicit MSAA resolves via glBlitFramebuffer.
   5.175 +         * In OpenGL ES 1 and 2, MSAA resolves must be done via an extension. */
   5.176 +        if (context.API >= kEAGLRenderingAPIOpenGLES3) {
   5.177 +            int w = backingWidth;
   5.178 +            int h = backingHeight;
   5.179 +            glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST);
   5.180 +
   5.181 +            if (!retainedBacking) {
   5.182 +                /* Discard the contents of the MSAA drawable color buffer. */
   5.183 +                glInvalidateFramebuffer(GL_READ_FRAMEBUFFER, 1, attachments);
   5.184 +            }
   5.185 +        } else {
   5.186 +            glResolveMultisampleFramebufferAPPLE();
   5.187 +
   5.188 +            if (!retainedBacking) {
   5.189 +                glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER, 1, attachments);
   5.190 +            }
   5.191 +        }
   5.192 +
   5.193 +        /* We assume the "drawable framebuffer" (MSAA draw framebuffer) was
   5.194 +         * previously bound... */
   5.195 +        glBindFramebuffer(GL_DRAW_FRAMEBUFFER, msaaFramebuffer);
   5.196 +    }
   5.197 +
   5.198      /* viewRenderbuffer should always be bound here. Code that binds something
   5.199       * else is responsible for rebinding viewRenderbuffer, to reduce duplicate
   5.200       * state changes. */
   5.201 @@ -245,6 +354,16 @@
   5.202          glDeleteRenderbuffers(1, &depthRenderbuffer);
   5.203          depthRenderbuffer = 0;
   5.204      }
   5.205 +
   5.206 +    if (msaaFramebuffer != 0) {
   5.207 +        glDeleteFramebuffers(1, &msaaFramebuffer);
   5.208 +        msaaFramebuffer = 0;
   5.209 +    }
   5.210 +
   5.211 +    if (msaaRenderbuffer != 0) {
   5.212 +        glDeleteRenderbuffers(1, &msaaRenderbuffer);
   5.213 +        msaaRenderbuffer = 0;
   5.214 +    }
   5.215  }
   5.216  
   5.217  - (void)dealloc
     6.1 --- a/src/video/uikit/SDL_uikitwindow.m	Sat Jul 18 00:04:49 2015 -0400
     6.2 +++ b/src/video/uikit/SDL_uikitwindow.m	Sun Jul 19 19:44:40 2015 -0300
     6.3 @@ -339,9 +339,11 @@
     6.4                      SDL_uikitopenglview *glview = (SDL_uikitopenglview *)data.viewcontroller.view;
     6.5                      info->info.uikit.framebuffer = glview.drawableFramebuffer;
     6.6                      info->info.uikit.colorbuffer = glview.drawableRenderbuffer;
     6.7 +                    info->info.uikit.resolveFramebuffer = glview.msaaResolveFramebuffer;
     6.8                  } else {
     6.9                      info->info.uikit.framebuffer = 0;
    6.10                      info->info.uikit.colorbuffer = 0;
    6.11 +                    info->info.uikit.resolveFramebuffer = 0;
    6.12                  }
    6.13              }
    6.14