首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用多个像素缓冲区对象更新视频纹理时出现卡顿

使用多个像素缓冲区对象更新视频纹理时出现卡顿
EN

Stack Overflow用户
提问于 2013-12-10 19:32:49
回答 1查看 1.3K关注 0票数 7

我有一个应用程序,它使用FFMPEG解码视频文件(在一个单独的线程中),并在另一个线程中使用PBO渲染此纹理。所有的PBO do-hickey都发生在以下函数中:

代码语言:javascript
复制
void DynamicTexture::update()
{
    if(!_isDirty)
    {
        return;
    }

    /// \todo   Check to make sure that PBOs are supported
    if(_usePbo)
    {
        // In multi PBO mode, we keep swapping between the PBOs
        // We use one PBO to actually set the texture data that we will upload
        // and the other we use to update/modify. Once modification is complete,
        // we simply swap buffers

        // Unmap the PBO that was updated last so that it can be released for rendering

        glBindBuffer(GL_PIXEL_UNPACK_BUFFER, _pboIds[_currentPboIndex]);
        glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
        Util::GLErrorAssert();

        // bind the texture
        glBindTexture(GL_TEXTURE_2D, _textureId);
        Util::GLErrorAssert();

        // copy pixels from PBO to texture object
        // Use offset instead of pointer.
        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _width, _height, 
                        (_channelCount==4)?GL_RGBA:GL_RGB,
                        GL_UNSIGNED_BYTE, 0);
        Util::GLErrorAssert();

        // Now swap the pbo index
        _currentPboIndex = (++_currentPboIndex) % _numPbos;

        // bind PBO to update pixel values
        glBindBuffer(GL_PIXEL_UNPACK_BUFFER, _pboIds[_currentPboIndex]);
        Util::GLErrorAssert();

        // map the next buffer object into client's memory
        // Note that glMapBuffer() causes sync issue.
        // If GPU is working with this buffer, glMapBuffer() will wait(stall)
        // for GPU to finish its job
        GLubyte* ptr = (GLubyte*)glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
        Util::GLErrorAssert();
        if(ptr)
        {
            // update data directly on the mapped buffer
            _currentBuffer = ptr;
            Util::GLErrorAssert();
        }
        else
        {
            printf("Unable to map PBO!");
            assert(false);
        }

        // It is good idea to release PBOs with ID 0 after use.
        // Once bound with 0, all pixel operations behave normal ways.
        glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
        Util::GLErrorAssert();

        // If a callback was registered, call it
        if(_renderCallback)
        {
            (*_renderCallback)(this);
        }
    }
    else
    {
        glBindTexture(GL_TEXTURE_2D, _textureId);
        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
                        _width, _height, (_channelCount==4)?GL_RGBA:GL_RGB,
                        GL_UNSIGNED_BYTE,
                        &(_buffer[0]) 
                        );
        Util::GLErrorAssert();
    }

    // Reset the dirty flag after updating
    _isDirty = false;
}

在解码线程中,我只需更新_currentBuffer并将_isDirty标志设置为true。此函数在渲染线程中调用。

当我使用单个PBO时,也就是在上面的代码中使用_numPbos=1时,渲染工作得很好,没有任何卡顿。但是,当我使用多个PBO时,视频中出现了明显的卡顿。你可以找到我用_numPbos=2 here渲染5个视频的示例。我使用PBO的次数越多,卡顿的情况就越严重。

从理论上讲,我正在更新的缓冲区和我用于渲染的缓冲区是不同的,所以应该不会出现这种问题。我想使用双/三重缓冲来提高渲染性能。

我正在寻找一些指示/提示,以了解可能出现的问题。

EN

回答 1

Stack Overflow用户

发布于 2013-12-18 00:15:48

我不知道,如果这是你的问题,但在你调用这个之后:

代码语言:javascript
复制
 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, _pboIds[_currentPboIndex]);
 glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
 Util::GLErrorAssert();

你在打电话给我

代码语言:javascript
复制
glBindTexture

但您仍在使用索引_currentPboIndex处的缓冲区进行操作。

在我的代码中,我有两个索引-索引和nextIndex

在init i set中

代码语言:javascript
复制
index = 0;
nextIndex = 1;

那么我的更新管道是这样的:

代码语言:javascript
复制
index = (index + 1) % 2;
nextIndex = (nextIndex + 1) % 2;

uint32 textureSize = sizeof(RGB) * width * height;


GL_CHECK( glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo[nextIndex]) );
GL_CHECK( glBufferData(GL_PIXEL_UNPACK_BUFFER, textureSize, 0, GL_STREAM_DRAW_ARB) );
GL_CHECK( gpuDataPtr = glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, textureSize, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT) );

//update data gpuDataPtr

GL_CHECK( glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER_ARB) );

//bind texture

GL_CHECK( glBindBufferARB(GL_PIXEL_UNPACK_BUFFER, pbo[index]) ); 

GL_CHECK( glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 
            width, height, glFormat, GL_UNSIGNED_BYTE, 0) );

GL_CHECK( glBindBufferARB(type, 0) );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20493372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档