OSX Framebuffer Depth Attachments

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:







Am I missing something or? What explains the reddish in both 16bits and 24bits mode?



TIA!

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?

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

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.

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.

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.

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:


[ 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 />