I am trying to get some shading effects to work in OpenGL ES 2.0 on iOS by putting some code from standard GL. Part of the pattern involves copying the depth buffer to the texture:
glBindTexture(GL_TEXTURE_2D, g_uiDepthBuffer); glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, 800, 600, 0);
However, it seems that glCopyTexImage2D is not supported in ES. Reading the linked stream, it seems that I can use the frame buffer and fragment shaders to retrieve depth data. So I am trying to write the depth component to the color buffer, and then copy it:
// clear everything glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // turn on depth rendering glUseProgram(m_BaseShader.uiId); // this is a switch to cause the fragment shader to just dump out the depth component glUniform1i(uiBaseShaderRenderDepth, true); // and for this, the color buffer needs to be on glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE); // and clear it to 1.0, like how the depth buffer starts glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // draw the scene DrawScene(); // bind our texture glBindTexture(GL_TEXTURE_2D, g_uiDepthBuffer); glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, width, height, 0);
Here is a fragment of a shader:
uniform sampler2D sTexture; uniform bool bRenderDepth; varying lowp float LightIntensity; varying mediump vec2 TexCoord; void main() { if(bRenderDepth) { gl_FragColor = vec4(vec3(gl_FragCoord.z), 1.0); } else { gl_FragColor = vec4(texture2D(sTexture, TexCoord).rgb * LightIntensity, 1.0); } }
I experimented with the absence of the bRenderDepth branch and did not speed it up significantly.
Right now, itβs quite simple to take this step at a speed of 14 frames per second, which is obviously unacceptable. If I pull out a copy, then its speed is above 30 frames per second. I get two suggestions from the Xcode OpenGLES parser in the copy command:
file: //localhost/Users/xxxx/Documents/Development/xxxx.mm: error: Verification error: glCopyTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 960, 640, 0): Height <640> is not a power of two
file: //localhost/Users/xxxx/Documents/Development/xxxx.mm: warning: GPU Wait on Texture: your application has updated the texture that is currently used for rendering. This caused the CPU to wait for the GPU to finish rendering.
I will work on a solution to the above two problems (perhaps they are the essence of this). At the same time, can anyone suggest a more efficient way to pull depth data into a texture?
Thanks in advance!