Skip to content

Commit

Permalink
Cleaned up iOS OpenGL ES context creation code and added sRGB context…
Browse files Browse the repository at this point in the history
… support on iOS 7+
  • Loading branch information
slime73 committed Jul 22, 2014
1 parent cf2958a commit 078ca9f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 52 deletions.
24 changes: 13 additions & 11 deletions src/video/uikit/SDL_uikitopengles.m
Expand Up @@ -132,24 +132,26 @@ void UIKit_GL_SwapWindow(_THIS, SDL_Window * window)
share_group = [view.context sharegroup];
}

/* construct our view, passing in SDL's OpenGL configuration data */
CGRect frame;
if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
frame = [[uiwindow screen] bounds];
} else {
frame = [[uiwindow screen] applicationFrame];
}

/* construct our view, passing in SDL's OpenGL configuration data */
view = [[SDL_uikitopenglview alloc] initWithFrame: frame
scale: scale
retainBacking: _this->gl_config.retained_backing
rBits: _this->gl_config.red_size
gBits: _this->gl_config.green_size
bBits: _this->gl_config.blue_size
aBits: _this->gl_config.alpha_size
depthBits: _this->gl_config.depth_size
stencilBits: _this->gl_config.stencil_size
majorVersion: _this->gl_config.major_version
shareGroup: share_group];
scale: scale
retainBacking: _this->gl_config.retained_backing
rBits: _this->gl_config.red_size
gBits: _this->gl_config.green_size
bBits: _this->gl_config.blue_size
aBits: _this->gl_config.alpha_size
depthBits: _this->gl_config.depth_size
stencilBits: _this->gl_config.stencil_size
sRGB: _this->gl_config.framebuffer_srgb_capable
majorVersion: _this->gl_config.major_version
shareGroup: share_group];
if (!view) {
return NULL;
}
Expand Down
21 changes: 11 additions & 10 deletions src/video/uikit/SDL_uikitopenglview.h
Expand Up @@ -63,16 +63,17 @@
- (void)setCurrentContext;

- (id)initWithFrame:(CGRect)frame
scale:(CGFloat)scale
retainBacking:(BOOL)retained
rBits:(int)rBits
gBits:(int)gBits
bBits:(int)bBits
aBits:(int)aBits
depthBits:(int)depthBits
stencilBits:(int)stencilBits
majorVersion:(int)majorVersion
shareGroup:(EAGLSharegroup*)shareGroup;
scale:(CGFloat)scale
retainBacking:(BOOL)retained
rBits:(int)rBits
gBits:(int)gBits
bBits:(int)bBits
aBits:(int)aBits
depthBits:(int)depthBits
stencilBits:(int)stencilBits
sRGB:(BOOL)sRGB
majorVersion:(int)majorVersion
shareGroup:(EAGLSharegroup*)shareGroup;

- (void)updateFrame;

Expand Down
85 changes: 54 additions & 31 deletions src/video/uikit/SDL_uikitopenglview.m
Expand Up @@ -41,16 +41,17 @@ + (Class)layerClass
}

- (id)initWithFrame:(CGRect)frame
scale:(CGFloat)scale
scale:(CGFloat)scale
retainBacking:(BOOL)retained
rBits:(int)rBits
gBits:(int)gBits
bBits:(int)bBits
aBits:(int)aBits
depthBits:(int)depthBits
stencilBits:(int)stencilBits
majorVersion:(int)majorVersion
shareGroup:(EAGLSharegroup*)shareGroup
rBits:(int)rBits
gBits:(int)gBits
bBits:(int)bBits
aBits:(int)aBits
depthBits:(int)depthBits
stencilBits:(int)stencilBits
sRGB:(BOOL)sRGB
majorVersion:(int)majorVersion
shareGroup:(EAGLSharegroup*)shareGroup
{
depthBufferFormat = 0;

Expand All @@ -59,12 +60,29 @@ - (id)initWithFrame:(CGRect)frame
const BOOL useDepthBuffer = (depthBits != 0);
NSString *colorFormat = nil;

self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.autoresizesSubviews = YES;

/* The EAGLRenderingAPI enum values currently map 1:1 to major GLES
versions, and this allows us to handle future OpenGL ES versions.
*/
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 */
colorFormat = kEAGLColorFormatRGBA8;
} else {
Expand All @@ -81,23 +99,24 @@ - (id)initWithFrame:(CGRect)frame
colorFormat, kEAGLDrawablePropertyColorFormat,
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) */
self.contentScaleFactor = scale;

/* create the buffers */
glGenFramebuffersOES(1, &viewFramebuffer);
/* Create the color Renderbuffer Object */
glGenRenderbuffersOES(1, &viewRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);

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);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];

/* attach the color renderbuffer to the FBO */
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);

glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
Expand All @@ -124,15 +143,14 @@ - (id)initWithFrame:(CGRect)frame
}

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);
/* end create buffers */

self.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.autoresizesSubviews = YES;
}

return self;
}

Expand Down Expand Up @@ -220,12 +238,17 @@ - (void)layoutSubviews

- (void)destroyFramebuffer
{
glDeleteFramebuffersOES(1, &viewFramebuffer);
viewFramebuffer = 0;
glDeleteRenderbuffersOES(1, &viewRenderbuffer);
viewRenderbuffer = 0;
if (viewFramebuffer != 0) {
glDeleteFramebuffersOES(1, &viewFramebuffer);
viewFramebuffer = 0;
}

if (viewRenderbuffer != 0) {
glDeleteRenderbuffersOES(1, &viewRenderbuffer);
viewRenderbuffer = 0;
}

if (depthRenderbuffer) {
if (depthRenderbuffer != 0) {
glDeleteRenderbuffersOES(1, &depthRenderbuffer);
depthRenderbuffer = 0;
}
Expand Down

0 comments on commit 078ca9f

Please sign in to comment.