# OSX Framebuffer Depth Attachments

**URL:** <https://forums.imgtec.com/t/osx-framebuffer-depth-attachments/2193>\
**Category:** PowerVR Insider\
**Created:** [October 25, 2014, 2:41am UTC](https://forums.imgtec.com/t/osx-framebuffer-depth-attachments/2193 "2014-10-25T02:41:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rom](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@rom](https://forums.imgtec.com/u/rom)\
**Post date:** [October 25, 2014, 2:41am UTC](https://forums.imgtec.com/t/osx-framebuffer-depth-attachments/2193/1 "2014-10-25T02:41:19Z")

</div>

In earlier version of the simulator (on OSX ES2) the FBO depth attachments were as expected black and white, in the latest version they look like this:  
  
  
  
 ![](http://sio2interactive.com/dump/fbo_attachments.png)  
  
  
  
Am I missing something or? What explains the reddish in both 16bits and 24bits mode?  
  
  
  
TIA!

---

<div class="post-metadata">

**Author:** ![ChrisPenrose](https://avatars.discourse-cdn.com/v4/letter/c/e36b37/32.png) [@ChrisPenrose](https://forums.imgtec.com/u/ChrisPenrose)\
**Post date:** [October 27, 2014, 10:26am UTC](https://forums.imgtec.com/t/osx-framebuffer-depth-attachments/2193/2 "2014-10-27T10:26:46Z")

</div>

Hi ROm  
  
  
  
That’s interesting! I will file a bug against this issue and try to reproduce it here. Can I ask what method you are using to render the depth buffers?

---

<div class="post-metadata">

**Author:** ![rom](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@rom](https://forums.imgtec.com/u/rom)\
**Post date:** [October 27, 2014, 11:27pm UTC](https://forums.imgtec.com/t/osx-framebuffer-depth-attachments/2193/3 "2014-10-27T23:27:26Z")

</div>

```auto
	GLuint texture_id = 0;<br />
<br />
glGenTexture( &texture_id );<br />
<br />
glBindTexture( GL_TEXTURE_2D, texture_id );<br />
<br />
/* Same problem with internal format=GL_DEPTH_COMPONENT16 or GL_DEPTH_COMPONENT24<br />
and also when format and internal format is set to GL_DEPTH_STENCIL and type=GL_UNSIGNED_INT_24_8.<br />
The bug seems to affect all depth texture regardless of the type that is initialized POT and NPOT. */<br />
GLint internal_format = GL_DEPTH_COMPONENT;<br />
<br />
GLenum format = GL_DEPTH_COMPONENT,<br />
type;<br />
<br />
if( GL.extension.version == 20 && // ES 2.0 with depth texture extension<br />
GL.extension.depth_texture )<br />
{<br />
internal_format =<br />
format = GL_DEPTH_COMPONENT;<br />
}<br />
else if( Gfx.extension.version > 20 ) // ES 3.x<br />
{<br />
internal_format = ( flags & is16bits ) ?<br />
GL_DEPTH_COMPONENT16:<br />
GL_DEPTH_COMPONENT24;<br />
<br />
format	= GL_DEPTH_COMPONENT;<br />
}<br />
<br />
type = !( flags & is16bits ) ?<br />
GL_UNSIGNED_INT :<br />
GL_UNSIGNED_SHORT;<br />
<br />
glTexImage2D( GL_TEXTURE_2D,<br />
0,<br />
internal_format,<br />
256,<br />
128,<br />
0,<br />
format,<br />
type,<br />
NULL );<br />
<br />
GLuint framebuffer_id = 0;<br />
<br />
glGenFramebuffer( &framebuffer_id );<br />
<br />
glBindFramebuffer( framebuffer_id );<br />
<br />
glFramebufferTexture2D( GL_FRAMEBUFFER,<br />
GL_DEPTH_ATTACHMENT,<br />
GL_TEXTURE_2D,<br />
texture_id,<br />
0 );

```

---

<div class="post-metadata">

**Author:** ![ChrisPenrose](https://avatars.discourse-cdn.com/v4/letter/c/e36b37/32.png) [@ChrisPenrose](https://forums.imgtec.com/u/ChrisPenrose)\
**Post date:** [November 3, 2014, 6:00pm UTC](https://forums.imgtec.com/t/osx-framebuffer-depth-attachments/2193/4 "2014-11-03T18:00:20Z")

</div>

Hi ROm  
  
  
  
How are you rendering the resulting depth texture to the screen? In a fragment shader I believe the values are placed only in the red channel, so for a black and white image you need to do something like:  
  
  
  
`
uniform sampler2D tex;
in highp vec2 texture_coords;
out highp vec4 color;

void main()
{
color = texture(tex, texture_coords);
color = vec4(color.r, color.r, color.r, 1.0);
}
`  
  
  
  
At this stage I am not sure what has changed in the emulator as I can’t reproduce the black-and-white behaviour in the previous version without explicitly reading from only the red channel as above, but perhaps my test is too simple.

---

<div class="post-metadata">

**Author:** ![rom](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@rom](https://forums.imgtec.com/u/rom)\
**Post date:** [November 4, 2014, 1:27am UTC](https://forums.imgtec.com/t/osx-framebuffer-depth-attachments/2193/5 "2014-11-04T01:27:02Z")

</div>

I believe that is because in OpenGL 3.x GL\_LUMINANCE is removed, and they are now storing the depth value as GL\_RED. I thought the emulator would still support the format/internal format in order to be compliant with GLES3.

---

<div class="post-metadata">

**Author:** ![ChrisPenrose](https://avatars.discourse-cdn.com/v4/letter/c/e36b37/32.png) [@ChrisPenrose](https://forums.imgtec.com/u/ChrisPenrose)\
**Post date:** [November 4, 2014, 10:07am UTC](https://forums.imgtec.com/t/osx-framebuffer-depth-attachments/2193/6 "2014-11-04T10:07:47Z")

</div>

Ah, I didn’t realise that this behaviour is dependent on the version of ES. In ES 2.0 the depth value should be stored in only GL\_RED, whereas in ES 3.x it’s stored in RGB.  
  
  
  
At the moment the emulator always uses the ES 2.0 behaviour, which should be easy to fix. In the meantime as a workaround you could apply your own texture swizzle to get the value into all three channels.

---

<div class="post-metadata">

**Author:** ![rom](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@rom](https://forums.imgtec.com/u/rom)\
**Post date:** [November 4, 2014, 11:36pm UTC](https://forums.imgtec.com/t/osx-framebuffer-depth-attachments/2193/7 "2014-11-04T23:36:28Z")

</div>

If this the case GL\_ALPHA, GL\_LUMINANCE and GL\_LUMINANCE\_ALPHA will also cause problem in the simulator… In order to be more compatible with a real GLES3 context maybe add in the emulator code theses swizzle:

````auto
[GL_ALPHA -> GL_R]<br />
<br />
GfxTexParameteri( target, GL_TEXTURE_SWIZZLE_R, GL_ZERO );<br />
GfxTexParameteri( target, GL_TEXTURE_SWIZZLE_G, GL_ZERO );<br />
GfxTexParameteri( target, GL_TEXTURE_SWIZZLE_B, GL_ZERO );<br />
GfxTexParameteri( target, GL_TEXTURE_SWIZZLE_A, GL_RED );<br />
<br />
[GL_LUMINANCE -> GL_R]<br />
<br />
GfxTexParameteri( target, GL_TEXTURE_SWIZZLE_R, GL_RED );<br />
GfxTexParameteri( target, GL_TEXTURE_SWIZZLE_G, GL_RED );<br />
GfxTexParameteri( target, GL_TEXTURE_SWIZZLE_B, GL_RED );<br />
GfxTexParameteri( target, GL_TEXTURE_SWIZZLE_A, GL_ONE );<br />
<br />
[GL_LUMINANCE_ALPHA -> GL_RG]<br />
<br />
GfxTexParameteri( target, GL_TEXTURE_SWIZZLE_R, GL_RED );<br />
GfxTexParameteri( target, GL_TEXTURE_SWIZZLE_G, GL_RED );<br />
GfxTexParameteri( target, GL_TEXTURE_SWIZZLE_B, GL_RED );<br />
GfxTexParameteri( target, GL_TEXTURE_SWIZZLE_A, GL_GREEN );<br />
<br />
[GL_DEPTH_COMPONENT / GL_DEPTH_COMPONENT16 / GL_DEPTH_COMPONENT24 / GL_DEPTH_STENCIL]<br />
<br />
GfxTexParameteri( target, GL_TEXTURE_SWIZZLE_R, GL_RED );<br />
GfxTexParameteri( target, GL_TEXTURE_SWIZZLE_G, GL_RED );<br />
GfxTexParameteri( target, GL_TEXTURE_SWIZZLE_B, GL_RED );<br />
GfxTexParameteri( target, GL_TEXTURE_SWIZZLE_A, GL_ONE );
```<br />
````
