November 18, 2011

matti- matti-
Ant Farmer
107 posts

OpenGL ES 2.0 & the Depth Buffer/Texture

 

I’m currently in the process of implementing shadow mapping for my OpenGL app and ran across this depth buffer issue – according to the OpenGL ES 2.0 specs (both PowerVR and http://developer.meego.com/api/1.2/opengles-2.0/glTexImage2D.html), a texture format GL_DEPTH_COMPONENT is not supported. However I tried and it does compile for Harmattan. Didn’t yet run it to see whether there is an error. Any experiences on this? I really dislike the idea of having to pack the depth values into a RGBA texture.

- Matti

 Signature 

Author of MMark13

6 replies

November 22, 2011

minimoog77 minimoog77
Lab Rat
128 posts

Does PowerVR support this extension? http://www.khronos.org/registry/gles/extensions/OES/OES_depth_texture.txt

November 22, 2011

matti- matti-
Ant Farmer
107 posts

It would appear that the SGX 530, at least, does not.

http://www.rojtberg.net/348/powervr-sgx-530-does-not-support-depth-textures/

Which would be sad since the performance I’m getting out of a RGBA packed depth texture is not very good. I’ll check with glGetString() later. The most modern phones seem to support the extension (ref google) so I guess it’s worth implementing even if mine doesnt .)

 Signature 

Author of MMark13

November 22, 2011

minimoog77 minimoog77
Lab Rat
128 posts

Well, welcome to OpenGL world. :)

I hope you will survive extension hell. :)

December 6, 2011

matti- matti-
Ant Farmer
107 posts

Well, the device seems to have extensions GL_OES_depth_texture, GL_OES_depth24, GL_OES_texture_float etc. which would hint that this should work. However if I try to pass GL_DEPTH_COMPONENT as the format, glTexImage2D() fails – well, this is what the OpenGL ES 2.0 spec does say, after all, GL_DEPTH_COMPONENT is not among the supported choices.

I tried creating a RGBA texture and was able to attach it to a FBO as GL_DEPTH_ATTACHMENT using glFramebufferTexture2D() and everything goes fine on paper. Rendering / reading that texture doesnt really work like with a depth texture (which I have working on the desktop build very nicely).

Such a shame.

 Signature 

Author of MMark13

December 6, 2011

matti- matti-
Ant Farmer
107 posts

See kids, this is what happens when you don’t read the documentation :) Changing the <type> parameter to GL_UNSIGNED_INT did the trick. I’ll share the code so maybe the next guy is spared the hassle:

Creating the FBO:

  1. bool GLController::CreateDepthTextureAndFBO(GLuint* fboId,
  2.                                             GLuint* depthTextureId,
  3.                                             GLuint* renderBuffer,
  4.                                             int width, int height)
  5. {
  6.     // Create a framebuffer object
  7.     glGenFramebuffers(1, fboId);
  8.     glBindFramebuffer(GL_FRAMEBUFFER, *fboId);
  9.  
  10.     // Create a render buffer
  11.     glGenRenderbuffers(1, renderBuffer);
  12.     glBindRenderbuffer(GL_RENDERBUFFER, *renderBuffer);
  13.  
  14.     // Create a texture for storing the depth
  15.     glGenTextures(1, depthTextureId);
  16.     glBindTexture(GL_TEXTURE_2D, *depthTextureId);
  17.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  18.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  19.  
  20.     // Remove artifact on the edges of the shadowmap
  21.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  22.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  23.  
  24.     Debug("glGetError() 0 = 0x%x", glGetError());
  25.     if ( m_hasDepthTextureExtension )
  26.     {
  27.         // We'll use a depth texture to store the depths in the shadow map
  28.         glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0,
  29.                      GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
  30.  
  31.         // Attach the depth texture to FBO depth attachment point
  32.         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
  33.                                GL_TEXTURE_2D, *depthTextureId, 0);
  34.     }
  35.     else
  36.     {
  37.         // We'll use a RGBA texture into which we pack the depth info
  38.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
  39.                      GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  40.  
  41.         // Attach the RGBA texture to FBO color attachment point
  42.         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
  43.                                GL_TEXTURE_2D, *depthTextureId, 0);
  44.  
  45.         // Allocate 16-bit depth buffer
  46.         glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
  47.  
  48.         // Attach the render buffer as depth buffer - will be ignored
  49.         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
  50.                                   GL_RENDERBUFFER, *renderBuffer);
  51.     }
  52.  
  53.     // check FBO status
  54.     GLenum fboStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  55.     if ( fboStatus != GL_FRAMEBUFFER_COMPLETE )
  56.     {
  57.         Debug("FBO not complete: 0x%x", fboStatus);
  58.         return false;
  59.     }
  60.  
  61.     // Go back to using the default frame buffer
  62.     glBindFramebuffer(GL_FRAMEBUFFER, DefaultFramebufferId);
  63.  
  64.     return true;
  65. }

Shaders for rendering the depth:

  1. const char* DepthShadowMapVertexShader =
  2.         GLSL_VERSION_STRING \
  3.         "precision highp float;\n" \
  4.         "\n" \
  5.         "attribute vec3 in_coord;\n" \
  6.         "uniform mat4 mvp_matrix;\n" \
  7.         "\n" \
  8.         "void main()\n" \
  9.         "{\n" \
  10.         "   gl_Position = mvp_matrix * vec4(in_coord, 1.0);\n" \
  11.         "}\n";
  12.  
  13. const char* DepthShadowMapFragmentShader =
  14.         GLSL_VERSION_STRING \
  15.         "precision highp float;\n" \
  16.         "\n" \
  17.         "void main()\n" \
  18.         "{\n" \
  19.         "}\n";

Shaders using the shadow map: see http://fabiensanglard.net/shadowmapping/index.php#c5t_form

Cheers to Fabian Sanglard +others for help and ideas on this issue.

- Matti

 Signature 

Author of MMark13

December 7, 2011

minimoog77 minimoog77
Lab Rat
128 posts

Happy to see it works! :)

 
  ‹‹ For Beginners Qt      x ››

You must log in to post a reply. Not a member yet? Register here!