我正在尝试通过从标准GL移植某些代码,在iOS上使用OpenGL ES 2.0实现一些阴影效果。示例的一部分涉及将深度缓冲区复制到纹理中:
然而,似乎ES不支持glCopyTexImage2D。阅读相关主题时,我发现可以使用帧缓冲区和片段着色器提取深度数据。因此,我正在尝试将深度组件写入颜色缓冲区,然后复制它。
我已经尝试过没有“bRenderDepth”分支,但它并没有明显加快速度。
现在仅仅完成这一步骤它的帧率为14fps,显然是不可接受的。如果我去掉复制,它的帧率就会超过30fps。Xcode OpenGLES分析器在复制命令上给出了两个建议:
提前感谢!
glBindTexture(GL_TEXTURE_2D, g_uiDepthBuffer);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, 800, 600, 0);
然而,似乎ES不支持glCopyTexImage2D。阅读相关主题时,我发现可以使用帧缓冲区和片段着色器提取深度数据。因此,我正在尝试将深度组件写入颜色缓冲区,然后复制它。
// 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);
这是片元着色器:
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);
}
}
我已经尝试过没有“bRenderDepth”分支,但它并没有明显加快速度。
现在仅仅完成这一步骤它的帧率为14fps,显然是不可接受的。如果我去掉复制,它的帧率就会超过30fps。Xcode OpenGLES分析器在复制命令上给出了两个建议:
file://localhost/Users/xxxx/Documents/Development/xxxx.mm: error: Validation Error: glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 960, 640, 0) : Height<640>不是2的幂
file://localhost/Users/xxxx/Documents/Development/xxxx.mm: warning: GPU Wait on Texture: Your app updated a texture that is currently used for rendering. This caused the CPU to wait for the GPU to finish rendering.
提前感谢!