iOS Stencil Patch

Here’s the patch: 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

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.diffOn 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

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.)

Ryan wrote:

Here’s the patch: 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

“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:

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.

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