【发布时间】:2017-03-24 22:01:06
【问题描述】:
在 iOS 上使用 OpengGL ES 3.0,我想使用一个片段着色器使用一个帧缓冲区对象(不是同时)绘制到 2 个不同颜色的附件( FBO)。但是,我在调用时收到 GL_INVALID_OPERATION 错误:
const GLenum attachments[] = {GL_COLOR_ATTACHMENT1};
glDrawBuffers(1, attachments);
我检查了 GL 状态并确保当前绑定了正确的 FBO,并且在此调用之前没有 GL_ERROR 并且 GL_MAX_COLOR_ATTACHMENTS 为 4。在 Xcode 中拍摄 GPU 快照会给出以下错误描述:
指定的操作对当前OpenGL状态无效
我创建了一个带有 2 个颜色附件的帧缓冲区对象,如下所示:
// Assuming the FBO and the 2 requried textures were correctly generated
glBindFramebuffer(GL_FRAMEBUFFER, _fbo);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _fbo_tex[0], 0);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, _fbo_tex[1], 0);
// glCheckFramebufferStatus(GL_FRAMEBUFFER) returns GL_FRAMEBUFFER_COMPLETE
顶点着色器:
#version 300 es
uniform mat4 modelviewProjectionMatrix;
in vec4 position;
void main() {
gl_Position = modelviewProjectionMatrix * position;
}
片段着色器:
#version 300 es
uniform lowp vec4 colorIn;
layout(location = 0) out lowp vec4 colorOut;
void main() {
colorOut = colorIn;
}
这是渲染代码:
// Assuming the correct program and vertex array is bound
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _fbo);
{
const GLenum attachments[] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, attachments); // OK
glClear(GL_COLOR_BUFFER_BIT);
// Draw red quad to GL_COLOR_ATTACHMENT0
}
{
const GLenum attachments[] = {GL_COLOR_ATTACHMENT1};
glDrawBuffers(1, attachments); // GL_INVALID_OPERATION Error
glClear(GL_COLOR_BUFFER_BIT);
// Draw green quad to GL_COLOR_ATTACHMENT1
}
奇怪的是,如果我将第二个 glDrawBuffers 调用替换为:
const GLenum attachments[] = {GL_NONE, GL_COLOR_ATTACHMENT1};
glDrawBuffers(2, attachments); // OK
glClear(GL_COLOR_BUFFER_BIT);
但这不是所需的行为,因为片段着色器输出到绘制缓冲区位置 0。根据这篇 wiki 文章:https://www.khronos.org/opengl/wiki/Fragment_Shader#Output_buffers {GL_COLOR_ATTACHMENT1} 是 glDrawBuffers 的有效输入,所以这是 iOS 错误吗?任何帮助将不胜感激。
【问题讨论】:
标签: ios opengl-es fragment-shader