为什么 glDrawElements 在使用相同的缓冲区进行计算着色和渲染时会干扰 glGetBufferSubData

Why glDrawElements is interfering with glGetBufferSubData when using the same buffer for Compute Shading and rendering

本文关键字:glGetBufferSubData 干扰 计算 glDrawElements 缓冲区 为什么      更新时间:2023-10-16

我有一个渲染点流的程序。用于获取给定帧的点的方法具有很强的时间一致性,因此,在渲染循环中,首先,我使用计算着色器来压缩流,删除不需要的点。其次,我向集合添加新点。最后,我使用 glDrawElements 渲染它。

这是执行该过程的代码(它使用Qt来操作OpenGL):

template< typename Vec3 >
unsigned int CompactionRenderingState< Vec3 >::render()
{
    // Compact stream.
    m_nElements = compact();
    // Sends new points to GPU.
    QOpenGLBuffer* buffer = m_outputBuffers[ POS ];
    buffer->bind();
    buffer->write( m_nElements * BYTES_PER_VERTEX, ( void * ) &RenderingState::m_positions[ 0 ],
                   RenderingState::m_positions.size() * BYTES_PER_VERTEX );
    buffer = m_outputBuffers[ ATTRIB0 ];
    buffer->bind();
    buffer->write( m_nElements * BYTES_PER_VERTEX, ( void * ) &RenderingState::m_colors[ 0 ],
                   RenderingState::m_colors.size() * BYTES_PER_VERTEX );
    m_nElements += RenderingState::m_positions.size();
    // Draws the resulting points.
    m_arrayObj->bind();
    unsigned int bufferOffset = 0;
    switch( RenderingState::m_attribs )
    {
        case Attributes::NORMALS:
        {
            RenderingState::m_painter->setStandardEffect( QGL::LitMaterial );
            m_outputBuffers[ POS ]->bind();
            m_openGL->glVertexAttribPointer( QGL::Position, 3, GL_FLOAT, GL_FALSE, 0, &bufferOffset );
            m_openGL->glEnableVertexAttribArray( QGL::Position );
            m_outputBuffers[ ATTRIB0 ]->bind();
            m_openGL->glVertexAttribPointer( QGL::Normal, 3, GL_FLOAT, GL_FALSE, 0, &bufferOffset );
            m_openGL->glEnableVertexAttribArray( QGL::Normal );
            break;
        }
        case Attributes::COLORS:
        {
            m_renderingProgram->bind();
            m_outputBuffers[ POS ]->bind();
            m_openGL->glVertexAttribPointer( QGL::Position, 3, GL_FLOAT, GL_FALSE, 0, &bufferOffset );
            m_openGL->glEnableVertexAttribArray( QGL::Position );
            m_outputBuffers[ ATTRIB0 ]->bind();
            m_openGL->glVertexAttribPointer( QGL::Color, 3, GL_FLOAT, GL_FALSE, 0, &bufferOffset );
            m_openGL->glEnableVertexAttribArray( QGL::Color );
            break;
        }
        case Attributes::COLORS_AND_NORMALS:
        {
            throw logic_error( "Colors and normals not supported yet." );
            break;
        }
    }
    m_openGL->glMemoryBarrier( GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT );
    m_openGL->glDrawArrays( GL_POINTS, 0, m_nElements );
    m_openGL->glDisableVertexAttribArray( QGL::Position );
    m_openGL->glDisableVertexAttribArray( QGL::Normal );
    m_openGL->glDisableVertexAttribArray( QGL::Color );
    m_openGL->glBindBuffer( GL_ARRAY_BUFFER, 0 );
    m_renderingProgram->release();
    m_arrayObj->release();
    // Swaps buffers for the next frame.
    for( int i = 0; i < N_BUFFER_TYPES; ++i )
    {
        std::swap( m_inputBuffers[ i ], m_outputBuffers[ i ] );
    }
}
template< typename Vec3 >
unsigned int CompactionRenderingState< Vec3 >::compact()
{
    // Makes the compaction of the unused points.
    unsigned int nElements = m_compactionFlags.size();
    unsigned int nBlocks = ( unsigned int ) ceil( ( float ) nElements / BLOCK_SIZE );
    nElements = m_scan.doScan( m_compactionFlags );
    m_openGL->glBindBufferBase( GL_SHADER_STORAGE_BUFFER, Scan::N_BUFFER_TYPES + POS, m_inputBuffers[ POS ]->bufferId() );
    m_openGL->glBindBufferBase( GL_SHADER_STORAGE_BUFFER, Scan::N_BUFFER_TYPES + ATTRIB0,
                                m_inputBuffers[ ATTRIB0 ]->bufferId() );
    m_openGL->glBindBufferBase( GL_SHADER_STORAGE_BUFFER, Scan::N_BUFFER_TYPES + N_BUFFER_TYPES + POS,
                                m_outputBuffers[ POS ]->bufferId() );
    m_openGL->glBindBufferBase( GL_SHADER_STORAGE_BUFFER, Scan::N_BUFFER_TYPES + N_BUFFER_TYPES + ATTRIB0,
                                m_outputBuffers[ ATTRIB0 ]->bufferId() );
    m_compactionProgram->bind();
    m_compactionProgram->enableAttributeArray( "flags" );
    m_compactionProgram->enableAttributeArray( "prefixes" );
    m_compactionProgram->enableAttributeArray( "inputVertices" );
    m_compactionProgram->enableAttributeArray( "inputAttrib0" );
    m_compactionProgram->enableAttributeArray( "outputVertices" );
    m_compactionProgram->enableAttributeArray( "outputAttrib0" );
    m_openGL->glDispatchCompute( nBlocks, 1, 1 );
    m_openGL->glMemoryBarrier( GL_SHADER_STORAGE_BARRIER_BIT );
    m_compactionProgram->disableAttributeArray( "flags" );
    m_compactionProgram->disableAttributeArray( "prefixes" );
    m_compactionProgram->disableAttributeArray( "inputVertices" );
    m_compactionProgram->disableAttributeArray( "inputAttrib0" );
    m_compactionProgram->disableAttributeArray( "outputVertices" );
    m_compactionProgram->disableAttributeArray( "outputAttrib0" );
    m_openGL->glBindBuffer( GL_SHADER_STORAGE_BUFFER, 0 );
    return nElements;
}

====== 已编辑 ======

我已经编写了一个自动测试来检查整个过程。此测试压缩一个点位置数组和另一个属性,以便删除奇数索引中的数据。当我在禁用地址空间随机化的情况下在 gdb 下运行代码时,它会完美地通过。但是,当在没有 gdb 或在 gdb 中启用地址空间随机化的情况下运行时,返回的数组全为零,除非我在方法 render()glDrawArrays注释。

====== 编辑结束 ======

TEST_F( CompactionTest, Compaction )
    {
        QGuiApplication app( g_argc, g_argv );
        QSurfaceFormat format;
        format.setVersion( 4, 3 );
        format.setRenderableType( QSurfaceFormat::OpenGL );
        format.setSwapBehavior( QSurfaceFormat::DoubleBuffer );
        format.setSamples( 16 );
        unsigned int nElements = 3000;
        vector< unsigned int  > flags( nElements );
        vector< vec3 > pos( nElements );
        vector< vec3 > attrib0( nElements );
        for( int i = 0; i < nElements; ++i )
        {
            flags[ i ] = i % 2;
            pos[ i ] = vec3( i, i, i );
            attrib0[ i ] = vec3( i + nElements, i + nElements, i + nElements );
        }
        CompactionQGLView window( flags, pos, attrib0, format );
        window.resize(640, 480);
        window.show();
        app.exec();
        pos = window.m_compactedPos;
        attrib0 = window.m_compactedAttrib0;
        ASSERT_EQ( pos.size(), nElements * 0.5 );
        ASSERT_EQ( attrib0.size(), nElements * 0.5 );
        float expected = 1.;
        for( int i = 0; i < pos.size(); ++i, expected += 2 )
        {
            vec3 expectedVec( expected, expected, expected );
            cout << "Pos: " << pos[ i ] << ". Expected: " << expectedVec << endl;
            ASSERT_EQ( pos[ i ], expectedVec );
            expectedVec = vec3( expected + nElements, expected + nElements, expected + nElements );
            cout << "Attrib0: " << attrib0[ i ] << ". Expected: " << expectedVec << endl << endl;
            ASSERT_EQ( attrib0[ i ], expectedVec );
        }
    }

下一个函数读取压缩结果,并由测试中的window用于设置m_compactedPosm_compactedAttrib0

template< typename Vec3 >
vector< vector< Vec3 > > CompactionRenderingState< Vec3 >::getResultCPU()
{
    m_openGL->glMemoryBarrier( GL_SHADER_STORAGE_BARRIER_BIT );
    unsigned int resultSize = sizeof( Vec3 ) * m_nElements;
    Vec3* result = ( Vec3* ) malloc( resultSize );
    vector< vector< Vec3 > > results;
    for( int i = 0; i < N_BUFFER_TYPES; ++i )
    {
        if( m_inputBuffers[ i ] != NULL )
        {
            m_openGL->glBindBuffer( GL_SHADER_STORAGE_BUFFER, m_inputBuffers[ i ]->bufferId() );
            m_openGL->glGetBufferSubData( GL_SHADER_STORAGE_BUFFER, 0, resultSize, ( void * ) result );
            vector< Vec3 > tempVec( m_nElements );
            std::copy( result, result + m_nElements, tempVec.begin() );
            results.push_back( tempVec );
        }
    }
    free( result );
    return results;
}

====== 已编辑 ======

那么,仅在启用地址空间随机化时发生此错误的可能原因是什么?我被困住了,已经有几天试图弄清楚这一点。有什么想法吗?

====== 编辑结束 ======

最后,问题在于调用glVertexAttribPointer时出现的一个微妙的错误隐式类型转换。

void glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid * pointer)接收最后一个参数 const GLvoid * pointer 。在我的代码中,我将变量unsigned int bufferOffset的地址作为最后一个参数传递。这应该会导致OpenGL上的一些内部混乱。

要修复代码,我们只需要更改 render() 函数中 switch 子句处的glVertexAttribPointer调用:

switch( RenderingState::m_attribs )
    {
        case Attributes::NORMALS:
        {
            RenderingState::m_painter->setStandardEffect( QGL::LitMaterial );
            m_outputBuffers[ POS ]->bind();
            m_openGL->glVertexAttribPointer( QGL::Position, 3, GL_FLOAT, GL_FALSE, 0, ( void * ) 0 );
            m_openGL->glEnableVertexAttribArray( QGL::Position );
            m_outputBuffers[ ATTRIB0 ]->bind();
            m_openGL->glVertexAttribPointer( QGL::Normal, 3, GL_FLOAT, GL_FALSE, 0, ( void * ) 0 );
            m_openGL->glEnableVertexAttribArray( QGL::Normal );
            break;
        }
        case Attributes::COLORS:
        {
            m_renderingProgram->bind();
            m_outputBuffers[ POS ]->bind();
            m_openGL->glVertexAttribPointer( QGL::Position, 3, GL_FLOAT, GL_FALSE, 0, ( void * ) 0 );
            m_openGL->glEnableVertexAttribArray( QGL::Position );
            m_outputBuffers[ ATTRIB0 ]->bind();
            m_openGL->glVertexAttribPointer( QGL::Color, 3, GL_FLOAT, GL_FALSE, 0, ( void * ) 0 );
            m_openGL->glEnableVertexAttribArray( QGL::Color );
            break;
        }
        case Attributes::COLORS_AND_NORMALS:
        {
            throw logic_error( "Colors and normals not supported yet." );
            break;
        }
    }
相关文章:
  • 没有找到相关文章