# iOS Stencil Patch

**URL:** <https://discourse.libsdl.org/t/ios-stencil-patch/18530>\
**Category:** SDL Development\
**Created:** [October 20, 2011, 10:32pm UTC](https://discourse.libsdl.org/t/ios-stencil-patch/18530 "2011-10-20T22:32:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Barnes](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/b/c67d28/32.png) [@Brian\_Barnes](https://discourse.libsdl.org/u/Brian_Barnes)\
**Post date:** [October 20, 2011, 10:32pm UTC](https://discourse.libsdl.org/t/ios-stencil-patch/18530/1 "2011-10-20T22:32:53Z")

</div>

Here’s the patch: [http://www.klinksoftware.com/misc/sdl\_1\_3\_ios\_stencil\_patch.txt](http://www.klinksoftware.com/misc/sdl_1_3_ios_stencil_patch.txt)

Note that iOS only allows packed depth/stencil buffers, so if you don’t have a depth buffer, it won’t make a stencil buffer. It only allows 24 bit depth and 8 bit stencil. Basically, anything in stencil bits other than 0 makes a stencil buffer (the same goes with the depth buffer in the original code.)

Here’s it in text form, which I’m sure won’t work because of the email formatting. Here’s to hoping I did this right!====================================================================================  
diff -au SDL2/src/video/uikit/SDL\_uikitopengles.m SDL/src/video/uikit/SDL\_uikitopengles.m  
— SDL2/src/video/uikit/SDL\_uikitopengles.m 2011-10-19 19:36:56.000000000 -0400  
+++ SDL/src/video/uikit/SDL\_uikitopengles.m 2011-10-19 23:36:27.000000000 -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;  
diff -au SDL2/src/video/uikit/SDL\_uikitopenglview.h SDL/src/video/uikit/SDL\_uikitopenglview.h  
— SDL2/src/video/uikit/SDL\_uikitopenglview.h 2011-10-19 19:36:56.000000000 -0400  
+++ SDL/src/video/uikit/SDL\_uikitopenglview.h 2011-10-19 23:35:37.000000000 -0400  
@@ -60,6 +60,7 @@  
bBits:(int)bBits  
aBits:(int)aBits  
depthBits:(int)depthBits

- stencilBits:(int)stencilBits  
majorVersion:(int)majorVersion;

- (void)updateFrame;  
diff -au SDL2/src/video/uikit/SDL\_uikitopenglview.m SDL/src/video/uikit/SDL\_uikitopenglview.m  
— SDL2/src/video/uikit/SDL\_uikitopenglview.m 2011-10-19 19:36:56.000000000 -0400  
+++ SDL/src/video/uikit/SDL\_uikitopenglview.m 2011-10-19 23:37:36.000000000 -0400  
@@ -40,6 +40,7 @@  
bBits:(int)bBits  
aBits:(int)aBits  
depthBits:(int)depthBits

- 

```
stencilBits:(int)stencilBits
 majorVersion:(int)majorVersion

```

{  
NSString \*colorFormat = nil;  
@@ -104,10 +105,19 @@  
glGetRenderbufferParameterivOES(GL\_RENDERBUFFER\_OES, GL\_RENDERBUFFER\_HEIGHT\_OES, &backingHeight);

```
     if (useDepthBuffer) {

```

- 

```
       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 (stencilBits==0) {

```

- 

```
  		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);

```

- 

```
  	}

```

- 

```
  	else {

```

- 

```
  		glGenRenderbuffersOES(1, &depthRenderbuffer);

```

- 

```
  		glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);

```

- 

```
  		glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH24_STENCIL8_OES, backingWidth, backingHeight);

```

- 

```
  		glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);

```

- 

```
  		glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_STENCIL_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);

```

- 

```
  	}
   }

   if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {

```

====================================================================================

[\>] Brian

---

<div class="post-metadata">

**Author:** ![icculus](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/icculus/32/81_2.png) [@icculus](https://discourse.libsdl.org/u/icculus)\
**Post date:** [October 22, 2011, 5:24am UTC](https://discourse.libsdl.org/t/ios-stencil-patch/18530/2 "2011-10-22T05:24:52Z")

</div>

I did some reworking on this patch; can you check and see if this is  
still okay? (Attached.)

–ryan.

-------------- next part --------------  
A non-text attachment was scrubbed…  
Name: SDL-ios-stencil-RYAN1.diff  
Type: text/x-diff  
Size: 4525 bytes  
Desc: not available  
URL: [http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20111022/22fdd2c3/attachment.diff](http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20111022/22fdd2c3/attachment.diff)On 10/20/11 6:32 PM, Brian Barnes wrote:

> Here’s the patch: [http://www.klinksoftware.com/misc/sdl\_1\_3\_ios\_stencil\_patch.txt](http://www.klinksoftware.com/misc/sdl_1_3_ios_stencil_patch.txt)
> 
> Note that iOS only allows packed depth/stencil buffers, so if you don’t have a depth buffer, it won’t make a stencil buffer. It only allows 24 bit depth and 8 bit stencil. Basically, anything in stencil bits other than 0 makes a stencil buffer (the same goes with the depth buffer in the original code.)

---

<div class="post-metadata">

**Author:** ![Brian\_Barnes](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/b/c67d28/32.png) [@Brian\_Barnes](https://discourse.libsdl.org/u/Brian_Barnes)\
**Post date:** [October 22, 2011, 7:32pm UTC](https://discourse.libsdl.org/t/ios-stencil-patch/18530/3 "2011-10-22T19:32:30Z")

</div>

Ryan wrote:

> > Here’s the patch: [http://www.klinksoftware.com/misc/sdl\_1\_3\_ios\_stencil\_patch.txt](http://www.klinksoftware.com/misc/sdl_1_3_ios_stencil_patch.txt)
> > 
> > Note that iOS only allows packed depth/stencil buffers, so if you don’t have a depth buffer, it won’t make a stencil buffer. It only allows 24 bit depth and 8 bit stencil. Basically, anything in stencil bits other than 0 makes a stencil buffer (the same goes with the depth buffer in the original code.)
> 
> I did some reworking on this patch; can you check and see if this is  
> still okay? (Attached.)

The code looks fine to me (I just looked over the patch). I will tell you this – Apple docs say that in \>=4.0 and above:

[http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES\_ProgrammingGuide/OpenGLESPlatforms/OpenGLESPlatforms.html](http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/OpenGLESPlatforms/OpenGLESPlatforms.html)

“In iOS 4.0 and later, separate stencil buffers are not supported. Use a combined depth/stencil buffer.”

But in your code it looks like it’s possible to attach a stencil buffer WITHOUT attaching a depth buffer. I have NO idea if this will work or not. When you have no stencil buffer, you use GL\_DEPTH\_COMPONENT24\_OES, but when you have a stencil buffer, you use GL\_DEPTH24\_STENCIL8\_OES; a depth buffer MIGHT be required. I have no idea if this is true or not. It might be safe just to attach one anyway (the memory is already being used), which is what I did in the original patch.

Also, (by the same docs as above), the only support on iOS \>=4 is 24 bit depth buffers, so the 16 bit stuff can probably just be removed.

Regardless, this version of the patch should work for me!

[\>] Brian\> On 10/20/11 6:32 PM, Brian Barnes wrote:

---

<div class="post-metadata">

**Author:** ![icculus](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/icculus/32/81_2.png) [@icculus](https://discourse.libsdl.org/u/icculus)\
**Post date:** [October 24, 2011, 3:43pm UTC](https://discourse.libsdl.org/t/ios-stencil-patch/18530/4 "2011-10-24T15:43:02Z")

</div>

> But in your code it looks like it’s possible to attach a stencil buffer  
> WITHOUT attaching a depth buffer. I have NO idea if this will work or  
> not. When you have no stencil buffer, you use GL\_DEPTH\_COMPONENT24\_OES,  
> but when you have a stencil buffer, you use GL\_DEPTH24\_STENCIL8\_OES; a  
> depth buffer MIGHT be required. I have no idea if this is true or not.  
> It might be safe just to attach one anyway (the memory is already being  
> used), which is what I did in the original patch.

I think it’s legal to not attach both…the memory might be used in  
either case, but it’s possible the GL can optimize for the case of no  
depth attachment, by not writing to the depth buffer at all.

I could be wrong (and I suspect it’s rare that people would not want a  
depth buffer anyhow), but let’s leave it like this in revision control  
and see if anyone complains. If we need to attach both, the fix is trivial.

> Also, (by the same docs as above), the only support on iOS \>=4 is 24 bit  
> depth buffers, so the 16 bit stuff can probably just be removed.

Thanks for the feedback, I did some more cleanup and put this in  
revision control!

–ryan.

---

<div class="post-metadata">

**Author:** ![Brian\_Barnes](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/b/c67d28/32.png) [@Brian\_Barnes](https://discourse.libsdl.org/u/Brian_Barnes)\
**Post date:** [October 28, 2011, 10:46pm UTC](https://discourse.libsdl.org/t/ios-stencil-patch/18530/5 "2011-10-28T22:46:49Z")

</div>

I finally got a chance to get back to this, had another version to get out. It works, and it seems to work fine for me, so check that one off!

[\>] Brian
