Qt OpenGL 渲染到纹理性能问题

Qt OpenGL Render to texture Performance Issue

本文关键字:纹理 性能 问题 OpenGL Qt      更新时间:2023-10-16

为了给出有关该问题的背景,我想在openGL渲染上实现一些图像处理。所以我决定使用渲染到纹理的概念。这种实现是在Qt/OpenGL和targt Embedded平台中实现的。窗口大小为1280*720。

问题是如果没有渲染到纹理,我能够达到大约 47fps,而渲染到纹理,fps 下降到 20。

我现在正在做的是使用 QOpenGLFramebufferObject 在 Qt 中创建一个帧缓冲区,这就是我在 InitializeGL(( 中初始化它的方式。尺寸1280*720。

QOpenGLFramebufferObjectFormat format;
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
m_fbo = new QOpenGLFramebufferObject(SCR_WIDTH, SCR_HEIGHT, format);

在 PaintGL(( 函数中,我绑定帧缓冲区首先执行所有渲染,然后从应用图像处理的帧缓冲区生成纹理。在下面的代码中,OnTrip(( 函数是 OpenGL 渲染。在 drawOpticalDistortedTexture(( 里面,我正在绑定缓冲区并使用 Qt 中的 glDrawElements(( 函数绘制纹理。

void MainWidget::paintGL()
{
if (!tripEnded)
{
m_fbo->bind();
onTrip();
}
else
{
m_fbo->bind();
onEndTrip(); 
}
QImage imagefbo = m_fbo->toImage();
m_fbo->release();
QOpenGLTexture* textureProcessed = initTexturesHelper(imagefbo);
glViewport(SCR_WIDTH_OFFSET, SCR_HEIGHT_OFFSET, (GLsizei)SCR_WIDTH, (GLsizei)SCR_HEIGHT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/*************************** Post processed Drawing *********************************************/
programOpticalDistortion.bind();
modelMatrix.setToIdentity();
programOpticalDistortion.setUniformValue("mvp_matrix", getOrthoProjectionMatrix() * modelMatrix);
programOpticalDistortion.setUniformValue("tex", 0);
programOpticalDistortion.setUniformValue("k1", distortionFactor);
programOpticalDistortion.setUniformValue("screenDimen", screenDimension);
textureProcessed->bind();
// Draw Texture geometry
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
geometries->drawOpticalDistortedTexture(&programOpticalDistortion);
glDisable(GL_BLEND);
textureProcessed->release();
programOpticalDistortion.release();
/*************************** Post processed Drawing - End *********************************************/
++frameCount;
if (frameTime.elapsed() >= 1000)
{
float fps = frameCount / ((double)frameTime.elapsed() / 1000.0f);
qDebug() << "FPS" << "  " << fps ;
}
}

VS 着色器如下,

#version 300 es
uniform mat4 mvp_matrix;
in vec4 a_position;
in vec2 a_texcoord;
out vec2 v_texcoord;
void main()
{
// Calculate vertex position in screen space
gl_Position = mvp_matrix * a_position;
v_texcoord = a_texcoord;
}

片段着色器如下,

#version 300 es
#undef lowp
#undef mediump
#undef highp
precision mediump float;
uniform sampler2D tex;
uniform float k1;
uniform vec2 screenDimen;
in vec2 v_texcoord;
out vec4 out_color;
//! [0]
void main()
{
vec2 uv = (gl_FragCoord.xy / screenDimen.xy) - vec2(0.5);
float uva = atan(uv.x, uv.y);
float uvd = sqrt(dot(uv, uv));
uvd = uvd*(1.0 + k1*uvd*uvd);
out_color = texture(tex, vec2(0.5) + vec2(sin(uva), cos(uva))*uvd);
}

我对最终输出没有任何问题,但 fps 现在是主要问题,如何提高性能?

有 2 个问题

  1. 我在PaintGL()中创建的动态纹理纹理在循环结束时未被删除。新增删除纹理处理;这提高了性能,但没有达到 60 FPS。

  2. main()函数中,我有这些线条,用于设置渲染的表面格式。

QSurfaceFormat format;
format.setDepthBufferSize(24);
QSurfaceFormat::setDefaultFormat(format);
format.setRenderableType(QSurfaceFormat::OpenGLES);
format.setVersion(2, 0);

当这些行被注释时,FPS 提高到 60。不知何故,这是Qt内部Surface格式设置的问题。设置表面格式不是强制性的,OpenGL仍然可以工作。