OpenGL纹理速度太慢了?

时间:2013-02-25 09:06:00

标签: qt opengl pixel texture-mapping pixel-manipulation

我有一个500万像素的纹理,我无法更新。纹理显示在与视频流不同的矩形上。

OpenGL命令可以快速执行,但真正的纹理更新速率是次优的,也许每秒只有3个实际帧。有一些变化,但使用较小的纹理(500x500)时没有太大的变化。

本机配有NVIDIA gtx 570

我最初的努力是使用glTexSubImage2DglBufferSubData,但这些表现略差于内存映射方案。

有没有办法强制显卡更新纹理?如何编写视频流软件?

渲染循环

void glStream::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    program1.bind();
    program1.setUniformValue("texture", 0);
    program1.enableAttributeArray(vertexAttr1);
    program1.enableAttributeArray(vertexTexr1);
    program1.setAttributeArray(vertexAttr1, vertices.constData());
    program1.setAttributeArray(vertexTexr1, texCoords.constData());
    //
    glECheck();
    glBindTexture(GL_TEXTURE_2D, textures[0]);
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB,pbos[0]);
    void* memory = glMapBuffer(GL_PIXEL_UNPACK_BUFFER_ARB,GL_WRITE_ONLY);
    device->fillBuffer((unsigned char *)memory,heightGL,widthGL); // takes 2ms (not long)
    glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER_ARB);
    glTexSubImage2D(GL_TEXTURE_2D,0,0,0,widthGL,heightGL,GL_LUMINANCE,GL_UNSIGNED_BYTE, NULL);
    glDrawArrays(GL_TRIANGLES, 0, vertices.size());
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB,0);
    glBindTexture(GL_TEXTURE_2D,0);
    //
    program1.disableAttributeArray(vertexTexr1);
    program1.disableAttributeArray(vertexAttr1);
    program1.release();
    glECheck();//no errors
}

纹理预约

void glStream::reserveTextures()
{
    assert(numGLFrames>0);
    assert(glGenBuffers);
    displayBuff = (GLubyte*) calloc(numGLFrames*widthGL*heightGL,sizeof(GLubyte));//GL_RGB8
    memset(displayBuff,100,numGLFrames*widthGL*heightGL*sizeof(GLubyte));
    glGenBuffers(1,&pbos[0]);
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[0]);
    glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB,
                 numGLFrames*widthGL*heightGL*sizeof(GLubyte),
                 &displayBuff[0], GL_STREAM_DRAW);
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
    glGenTextures(1,&textures[0]);
    glBindTexture(GL_TEXTURE_2D,textures[0]);
    glTexImage2D(GL_TEXTURE_2D,0,GL_LUMINANCE,
                 widthGL,heightGL,0,GL_LUMINANCE,GL_UNSIGNED_BYTE,NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
    glBindTexture(GL_TEXTURE_2D,0);
}

初始化

void glStream::initializeGL()
{
    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        const char * emssage = (const char*)glewGetErrorString(err);
        QMessageBox::information(0, "OpenGL 3.x Context Example",
                                 emssage);
        exit(20);
    }
    glDisable(GL_DEPTH_TEST);
    QGLShader *vshader1 = new QGLShader(QGLShader::Vertex, this);
    const char *vsrc1 =
        "attribute vec2 coord2d;   \n"
        "attribute mediump vec4 texCoord;\n"
        "varying mediump vec4 texc;\n"
        "void main()                  \n"
        "{                            \n"
        "   gl_Position = vec4(coord2d, 0.0, 1.0); \n"
        "   texc = texCoord;\n"
        "}                          \n";
    vshader1->compileSourceCode(vsrc1);
    QGLShader *fshader1 = new QGLShader(QGLShader::Fragment, this);
    const char *fsrc1 =
        "uniform sampler2D texture;\n"
        "varying mediump vec4 texc;\n"
        "void main(void)\n"
        "{\n"
        "    gl_FragColor = texture2D(texture, texc.st);\n"
        "}\n";
    fshader1->compileSourceCode(fsrc1);
    program1.addShader(vshader1);
    program1.addShader(fshader1);
    program1.link();
    vertexAttr1 = program1.attributeLocation( "coord2d");
    vertexTexr1 = program1.attributeLocation( "texCoord");
    // Create the vertex buffer.
    vertices.clear();
    float u=1;
#define AVEC -u,u
#define BVEC -u,-u
#define CVEC u,u
#define DVEC u,-u
    vertices << QVector2D(AVEC);vertices << QVector2D(BVEC);
    vertices << QVector2D(CVEC);vertices << QVector2D(BVEC);
    vertices << QVector2D(DVEC);vertices << QVector2D(CVEC);
    // Create the texture vertex buffer
#define TAVEC 0,1
#define TBVEC 0,0
#define TCVEC 1,1
#define TDVEC 1,0
    texCoords << QVector2D(TAVEC);texCoords << QVector2D(TBVEC);
    texCoords << QVector2D(TCVEC);texCoords << QVector2D(TBVEC);
    texCoords << QVector2D(TDVEC);texCoords << QVector2D(TCVEC);
    glECheck();
    reserveTextures();
}

修改1

我确信填充缓冲区会出现一个新的纹理,因为在代码的其他部分我将这个纹理写入文件并且确实不同。我的文件IO比OpenGL纹理更快,这是一个悲伤的日子。

编辑2 我尝试了FRAPS并验证了渲染循环的速度为~18 FPS,但可见更新速度较慢(可能为3 FPS)。有什么可以解释这种差异?

1 个答案:

答案 0 :(得分:1)

在调用glBufferData:

时,PBO比纹理大得多
numGLFrames*widthGL*heightGL*sizeof(GLubyte)

您正在为多个纹理(帧)分配足够大的PBO,但只能读取/写入一帧的数据。

如果你让PBO与纹理大小相同并使用glTexImage2D而不是glTexSubImage2D,它会更快吗?