Xcode中的帧捕获失败

时间:2012-10-22 14:47:12

标签: ios xcode opengl-es-2.0

我正在使用Xcode 4.5.1并在带有iOS6的iPhone5上进行测试。

我正在使用帧捕获功能没有问题,但突然它停止工作。 当我按下帧捕获按钮时,似乎帧被捕获,手机切换到空白屏幕,只是突然切换回应用程序屏幕,应用程序继续运行。我仍然可以调试和暂停应用程序,但是没有办法获得帧捕获。我在控制台中也没有看到任何错误。

它停止工作的原因是这段代码。这段代码应该渲染一个rendertexture,但rendertexture似乎是空白的。我想使用帧捕获功能找出错误,但代码本身不会让我捕获...... :(

知道为什么吗?

// ------------- init function -----------------
// Create the framebuffer and bind it
glGenFramebuffers(1, &g_framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, g_framebuffer);
//Create the destination texture, and attach it to the framebuffer’s color attachment point.
glGenTextures(1, &g_texture);
glBindTexture(GL_TEXTURE_2D, g_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,  w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, g_texture, 0);    
//Test the framebuffer for completeness
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER) ;
if(status != GL_FRAMEBUFFER_COMPLETE) {
    NSLog(@"failed to make complete framebuffer object %x", status);
} else {
    NSLog(@"SkyPlugin initialized");
}


// ----------------- on update  ------------------
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &oldFBO);
glGetIntegerv(GL_VIEWPORT, oldViewPort);
// set the framebuffer and clear
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, g_framebuffer);
glViewport(0, 0, 32, 32);
//glClearColor(0.9f, 0.1f, 0.1f, 1.0f);
glDisable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT);
// Set shader   
glUseProgram(m_program);
// do some glEnableVertexAttribArray
// ...
// texture setting
glActiveTexture(GL_TEXTURE0);
glUniform1i(m_uniform, 0);
ResourceManager* resourceManager = ResourceManager::GetInstance();
glBindTexture(GL_TEXTURE_2D, m_texture[0]);
// ----------- Draw -----------
// Draws a full-screen quad to copy textures
static const vertexDataUV quad[] = {
    {/*v:*/{-1.f,-1.f,0}, /*t:*/{0,0}},
    {/*v:*/{-1.f,1,0}, /*t:*/{0,1}},
    {/*v:*/{1,-1.f,0}, /*t:*/{1,0}},
    {/*v:*/{1,1,0}, /*t:*/{1,1}}
};
static const GLubyte indeces[] = {0,2,1,3};
glVertexAttribPointer(m_posAttrib, 3, GL_FLOAT, 0, sizeof(vertexDataUV), &quad[0].vertex);
glVertexAttribPointer(m_texCoordAttrib, 2, GL_FLOAT, 0, sizeof(vertexDataUV), &quad[0].uv);
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, indeces);
// ------------ End
// go back to the main framebuffer!
glBindFramebuffer(GL_FRAMEBUFFER, oldFBO);
glViewport(oldViewPort[0], oldViewPort[1], oldViewPort[2], oldViewPort[3]);
glEnable(GL_DEPTH_TEST);
//glClearColor(0.1f, 0.1f, 0.1f, 1.0f);

编辑:(2012/10月/ 28日)

我发现了上述代码无效的原因。我忘了绑定一个渲染缓冲区!下面的代码有效,但是当此代码处于活动状态时帧捕获仍然失败...

在init上,

//创建renderbuffer并绑定它     glGenRenderbuffers(1,& g_renderbuffer);     glBindRenderbuffer(GL_RENDERBUFFER,g_renderbuffer);     glRenderbufferStorage(GL_RENDERBUFFER,GL_RGBA8_OES,w,h);

// Create the framebuffer and bind it
glGenFramebuffers(1, &g_framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, g_framebuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                          GL_COLOR_ATTACHMENT0,
                          GL_RENDERBUFFER, g_renderbuffer);
//Create the destination texture, and attach it to the framebuffer’s color attachment point.
glGenTextures(1, &g_texture);
glBindTexture(GL_TEXTURE_2D, g_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,  w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, g_texture, 0);

更新时,

glGetIntegerv(GL_RENDERBUFFER_BINDING, &oldRBO);
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &oldFBO);
glGetIntegerv(GL_VIEWPORT, oldViewPort);

// set the framebuffer and clear
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, g_framebuffer);
glBindRenderbuffer(GL_RENDERBUFFER, g_renderbuffer);
glViewport(0, 0, 32, 32);

// ... draw stuff ...

更新结束,

// go back to the main framebuffer!
glBindFramebuffer(GL_FRAMEBUFFER, oldFBO);
glBindRenderbuffer(GL_RENDERBUFFER, oldRBO);

2 个答案:

答案 0 :(得分:2)

这似乎是Xcode的一个错误。 最新版本Xcode 4.5.2让我捕捉到框架:)

捕获帧后,我在代码的这一部分出现错误:

// ... draw stuff ...
glActiveTexture(GL_TEXTURE0);
glUniform1i(MY_TEXTURE, 0);

在glUniform1i上,我收到此错误:“指定的操作对当前的OpenGL状态无效”。

不知道为什么我会收到此错误(渲染它正在工作),但我怀疑这个错误可能是我无法在之前版本的Xcode中捕获帧的原因...

答案 1 :(得分:0)

我看到过非常相似的行为,虽然它有点不稳定,但似乎确实与内存使用有关。通常当它失败时,重放应用程序似乎耗尽了内存(当控制台失败时我会在控制台中看到内存警告。)

切换到固定更多内存的设备(iPad 3中的iPad 4),但我也可以通过减少使用的纹理内存量偶尔解决它 - 跳过为我的所有纹理设置顶部mipmap会通常足够自由。