Added stencil buffer support on iOS.
authorRyan C. Gordon <icculus@icculus.org>
Sat, 22 Oct 2011 01:23:21 -0400
changeset 6021 a4bf2a9b0fd0
parent 6020 5374ddd99862
child 6022 e0327dd7d0c0
Added stencil buffer support on iOS. Thanks to Brian Barnes for the initial work on this patch!
src/video/uikit/SDL_uikitopengles.m
src/video/uikit/SDL_uikitopenglview.h
src/video/uikit/SDL_uikitopenglview.m
--- a/src/video/uikit/SDL_uikitopengles.m	Mon Oct 24 11:25:03 2011 -0400
+++ b/src/video/uikit/SDL_uikitopengles.m	Sat Oct 22 01:23:21 2011 -0400
@@ -112,6 +112,7 @@
                                     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];
 
     data->view = view;
--- a/src/video/uikit/SDL_uikitopenglview.h	Mon Oct 24 11:25:03 2011 -0400
+++ b/src/video/uikit/SDL_uikitopenglview.h	Sat Oct 22 01:23:21 2011 -0400
@@ -60,6 +60,7 @@
     bBits:(int)bBits
     aBits:(int)aBits
     depthBits:(int)depthBits
+    stencilBits:(int)stencilBits
     majorVersion:(int)majorVersion;
 
 - (void)updateFrame;
--- a/src/video/uikit/SDL_uikitopenglview.m	Mon Oct 24 11:25:03 2011 -0400
+++ b/src/video/uikit/SDL_uikitopenglview.m	Sat Oct 22 01:23:21 2011 -0400
@@ -40,10 +40,12 @@
       bBits:(int)bBits
       aBits:(int)aBits
       depthBits:(int)depthBits
+      stencilBits:(int)stencilBits
       majorVersion:(int)majorVersion
 {
+    const BOOL useStencilBuffer = (stencilBits != 0);
+    const BOOL useDepthBuffer = (depthBits != 0);
     NSString *colorFormat = nil;
-    BOOL useDepthBuffer;
 
     if (rBits == 8 && gBits == 8 && bBits == 8) {
         /* if user specifically requests rbg888 or some color format higher than 16bpp */
@@ -56,21 +58,18 @@
 
     depthBufferFormat = 0;
 
-    if (depthBits == 24) {
-        useDepthBuffer = YES;
-        depthBufferFormat = GL_DEPTH_COMPONENT24_OES;
-    }
-    else if (depthBits == 0) {
-        useDepthBuffer = NO;
-    }
-    else {
-        /* default case when depth buffer is not disabled */
-        /*
-           strange, even when we use this, we seem to get a 24 bit depth buffer on iPhone.
-           perhaps that's the only depth format iPhone actually supports
-        */
-        useDepthBuffer = YES;
-        depthBufferFormat = GL_DEPTH_COMPONENT16_OES;
+    if (useDepthBuffer) {
+        if (depthBits == 24) {
+            depthBufferFormat = GL_DEPTH_COMPONENT24_OES;
+        }
+        else {
+            /* default case when depth buffer is not disabled */
+            /*
+               strange, even when we use this, we seem to get a 24 bit depth buffer on iPhone.
+               perhaps that's the only depth format iPhone actually supports
+            */
+            depthBufferFormat = GL_DEPTH_COMPONENT16_OES;
+        }
     }
 
     if ((self = [super initWithFrame:frame])) {
@@ -103,11 +102,21 @@
         glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
         glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
 
-        if (useDepthBuffer) {
+        if ((useDepthBuffer) || (useStencilBuffer)) {
+            if (useStencilBuffer) {
+                // Apparently you need to pack stencil and depth into one buffer.
+                // !!! FIXME: this is the only thing (currently) supported. May not always be true.
+                depthBufferFormat = GL_DEPTH24_STENCIL8_OES;
+            }
             glGenRenderbuffersOES(1, &depthRenderbuffer);
             glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
             glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthBufferFormat, backingWidth, backingHeight);
-            glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
+            if (useDepthBuffer) {
+                glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
+            }
+            if (useStencilBuffer) {
+                glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_STENCIL_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
+            }
         }
 
         if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {