Cleaned up iOS OpenGL ES context creation code and added sRGB context support on iOS 7+

This commit is contained in:
Alex Szpakowski 2014-07-22 20:06:13 -03:00
parent cf2958a8de
commit 078ca9f078
3 changed files with 79 additions and 53 deletions

View File

@ -132,24 +132,26 @@ UIKit_GL_CreateContext(_THIS, SDL_Window * window)
share_group = [view.context sharegroup]; share_group = [view.context sharegroup];
} }
/* construct our view, passing in SDL's OpenGL configuration data */
CGRect frame; CGRect frame;
if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) { if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
frame = [[uiwindow screen] bounds]; frame = [[uiwindow screen] bounds];
} else { } else {
frame = [[uiwindow screen] applicationFrame]; frame = [[uiwindow screen] applicationFrame];
} }
/* construct our view, passing in SDL's OpenGL configuration data */
view = [[SDL_uikitopenglview alloc] initWithFrame: frame view = [[SDL_uikitopenglview alloc] initWithFrame: frame
scale: scale scale: scale
retainBacking: _this->gl_config.retained_backing retainBacking: _this->gl_config.retained_backing
rBits: _this->gl_config.red_size rBits: _this->gl_config.red_size
gBits: _this->gl_config.green_size gBits: _this->gl_config.green_size
bBits: _this->gl_config.blue_size bBits: _this->gl_config.blue_size
aBits: _this->gl_config.alpha_size aBits: _this->gl_config.alpha_size
depthBits: _this->gl_config.depth_size depthBits: _this->gl_config.depth_size
stencilBits: _this->gl_config.stencil_size stencilBits: _this->gl_config.stencil_size
majorVersion: _this->gl_config.major_version sRGB: _this->gl_config.framebuffer_srgb_capable
shareGroup: share_group]; majorVersion: _this->gl_config.major_version
shareGroup: share_group];
if (!view) { if (!view) {
return NULL; return NULL;
} }

View File

@ -63,16 +63,17 @@
- (void)setCurrentContext; - (void)setCurrentContext;
- (id)initWithFrame:(CGRect)frame - (id)initWithFrame:(CGRect)frame
scale:(CGFloat)scale scale:(CGFloat)scale
retainBacking:(BOOL)retained retainBacking:(BOOL)retained
rBits:(int)rBits rBits:(int)rBits
gBits:(int)gBits gBits:(int)gBits
bBits:(int)bBits bBits:(int)bBits
aBits:(int)aBits aBits:(int)aBits
depthBits:(int)depthBits depthBits:(int)depthBits
stencilBits:(int)stencilBits stencilBits:(int)stencilBits
majorVersion:(int)majorVersion sRGB:(BOOL)sRGB
shareGroup:(EAGLSharegroup*)shareGroup; majorVersion:(int)majorVersion
shareGroup:(EAGLSharegroup*)shareGroup;
- (void)updateFrame; - (void)updateFrame;

View File

@ -41,16 +41,17 @@
} }
- (id)initWithFrame:(CGRect)frame - (id)initWithFrame:(CGRect)frame
scale:(CGFloat)scale scale:(CGFloat)scale
retainBacking:(BOOL)retained retainBacking:(BOOL)retained
rBits:(int)rBits rBits:(int)rBits
gBits:(int)gBits gBits:(int)gBits
bBits:(int)bBits bBits:(int)bBits
aBits:(int)aBits aBits:(int)aBits
depthBits:(int)depthBits depthBits:(int)depthBits
stencilBits:(int)stencilBits stencilBits:(int)stencilBits
majorVersion:(int)majorVersion sRGB:(BOOL)sRGB
shareGroup:(EAGLSharegroup*)shareGroup majorVersion:(int)majorVersion
shareGroup:(EAGLSharegroup*)shareGroup
{ {
depthBufferFormat = 0; depthBufferFormat = 0;
@ -59,12 +60,29 @@
const BOOL useDepthBuffer = (depthBits != 0); const BOOL useDepthBuffer = (depthBits != 0);
NSString *colorFormat = nil; NSString *colorFormat = nil;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.autoresizesSubviews = YES;
/* The EAGLRenderingAPI enum values currently map 1:1 to major GLES /* The EAGLRenderingAPI enum values currently map 1:1 to major GLES
versions, and this allows us to handle future OpenGL ES versions. versions, and this allows us to handle future OpenGL ES versions.
*/ */
EAGLRenderingAPI api = majorVersion; EAGLRenderingAPI api = majorVersion;
if (rBits == 8 && gBits == 8 && bBits == 8) { context = [[EAGLContext alloc] initWithAPI:api sharegroup:shareGroup];
if (!context || ![EAGLContext setCurrentContext:context]) {
[self release];
SDL_SetError("OpenGL ES %d not supported", majorVersion);
return nil;
}
#ifdef __IPHONE_7_0
/* sRGB context support was added in iOS 7 */
BOOL hasiOS7 = [[UIDevice currentDevice].systemVersion compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending;
if (sRGB && hasiOS7) {
colorFormat = kEAGLColorFormatSRGBA8;
} else
#endif
if (rBits >= 8 && gBits >= 8 && bBits >= 8) {
/* if user specifically requests rbg888 or some color format higher than 16bpp */ /* if user specifically requests rbg888 or some color format higher than 16bpp */
colorFormat = kEAGLColorFormatRGBA8; colorFormat = kEAGLColorFormatRGBA8;
} else { } else {
@ -81,23 +99,24 @@
colorFormat, kEAGLDrawablePropertyColorFormat, colorFormat, kEAGLDrawablePropertyColorFormat,
nil]; nil];
context = [[EAGLContext alloc] initWithAPI:api sharegroup:shareGroup];
if (!context || ![EAGLContext setCurrentContext:context]) {
[self release];
SDL_SetError("OpenGL ES %d not supported", majorVersion);
return nil;
}
/* Set the appropriate scale (for retina display support) */ /* Set the appropriate scale (for retina display support) */
self.contentScaleFactor = scale; self.contentScaleFactor = scale;
/* create the buffers */ /* Create the color Renderbuffer Object */
glGenFramebuffersOES(1, &viewFramebuffer);
glGenRenderbuffersOES(1, &viewRenderbuffer); glGenRenderbuffersOES(1, &viewRenderbuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
if (![context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:eaglLayer]) {
[self release];
SDL_SetError("Failed creating OpenGL ES drawable");
return nil;
}
/* Create the Framebuffer Object */
glGenFramebuffersOES(1, &viewFramebuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
/* attach the color renderbuffer to the FBO */
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer); glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth); glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
@ -124,15 +143,14 @@
} }
if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) { if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
return NO; [self release];
SDL_SetError("Failed creating OpenGL ES framebuffer");
return nil;
} }
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
/* end create buffers */
self.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.autoresizesSubviews = YES;
} }
return self; return self;
} }
@ -220,12 +238,17 @@
- (void)destroyFramebuffer - (void)destroyFramebuffer
{ {
glDeleteFramebuffersOES(1, &viewFramebuffer); if (viewFramebuffer != 0) {
viewFramebuffer = 0; glDeleteFramebuffersOES(1, &viewFramebuffer);
glDeleteRenderbuffersOES(1, &viewRenderbuffer); viewFramebuffer = 0;
viewRenderbuffer = 0; }
if (depthRenderbuffer) { if (viewRenderbuffer != 0) {
glDeleteRenderbuffersOES(1, &viewRenderbuffer);
viewRenderbuffer = 0;
}
if (depthRenderbuffer != 0) {
glDeleteRenderbuffersOES(1, &depthRenderbuffer); glDeleteRenderbuffersOES(1, &depthRenderbuffer);
depthRenderbuffer = 0; depthRenderbuffer = 0;
} }