我正在尝试使用QOpenGLBuffer作为像素缓冲区对象。目标是显示高分辨率视频流(4K,60FPS),因此我需要良好的性能。
由于我只是从OpenGL开始,因此我首先尝试以最佳方式显示简单的2D纹理。我已经包含了VBO和VAO,下一步(如我所读)将是使用PBO以获得更好的性能。
有针对PBO的教程,但使用glGenBufferARB(),而不使用QOpenGLBuffer。
这是我的代码:
glwidget.h
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLBuffer>
#include <QDebug>
#include <QOpenGLTexture>
#include <QOpenGLShader>
#include <QOpenGLShaderProgram>
#include <QOpenGLVertexArrayObject>
class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent = 0);
~GLWidget();
void initializeGL();
void paintGL();
void resizeGL(int w, int h);
void LoadGLTextures();
private :
QOpenGLShaderProgram *program;
QOpenGLBuffer vbo;
QOpenGLVertexArrayObject vao;
GLuint tex;
GLint vertexLocation;
GLint texcoordLocation;
int tailleVerticesBytes;
int tailleCoordTexturesBytes;
float vertices[8];
float coordTexture[8];
public slots:
private slots:
};
#endif // GLWIDGET_H
glwidget.cpp
#ifndef BUFFER_OFFSET
#define BUFFER_OFFSET(offset) ((char*)NULL + (offset))
#include "glwidget.h"
#include <QElapsedTimer>
GLWidget::GLWidget(QWidget *parent) :
QOpenGLWidget(parent)
{
tailleVerticesBytes = 8*sizeof(float);
tailleCoordTexturesBytes = 8*sizeof(float);
}
GLWidget::~GLWidget(){
vao.destroy();
vbo.destroy();
delete program;
glDeleteTextures(1, &tex);
}
void GLWidget::LoadGLTextures(){
QImage img;
if(!img.load("C:\\Users\\Adrien\\Desktop\\open3.bmp")){
qDebug()<<"Image loading failed";
}
QImage t = (img.convertToFormat(QImage::Format_RGBA8888)).mirrored();
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, 3, t.width(), t.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, t.bits());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture( GL_TEXTURE_2D, 0);
}
void GLWidget::initializeGL(){
float verticesTmp[] = {-1.0,-1.0, 1.0,-1.0, 1.0,1.0, -1.0,1.0};
float coordTextureTmp[] = {0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0};
for(int i = 0; i<8; i++){
vertices[i] = verticesTmp[i];
coordTexture[i] = coordTextureTmp[i];
}
initializeOpenGLFunctions();
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
LoadGLTextures();
//Shader setup
QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
const char *vsrc =
"#version 150 core\n"
"in vec2 in_Vertex;\n"
"in vec2 vertTexCoord;\n"
"out vec2 fragTexCoord;\n"
"void main(void)\n"
"{\n"
" gl_Position = vec4(in_Vertex, 0.0, 1.0);\n"
" fragTexCoord = vertTexCoord;\n"
"}\n";
vshader->compileSourceCode(vsrc);
QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
const char *fsrc =
"#version 150 core\n"
"uniform sampler2D tex;\n"
"in vec2 fragTexCoord;\n"
"void main(void)\n"
"{\n"
" gl_FragColor = texture2D(tex,fragTexCoord);\n"
"}\n";
fshader->compileSourceCode(fsrc);
program = new QOpenGLShaderProgram;
program->addShader(vshader);
program->addShader(fshader);
program->link();
program->bind();
glActiveTexture(GL_TEXTURE0);
program->setUniformValue("tex", 0);
vertexLocation = glGetAttribLocation(program->programId(), "in_Vertex");
texcoordLocation = glGetAttribLocation(program->programId(), "vertTexCoord");
//VAO setup
vao.create();
vao.bind();
//VBO setup
vbo.create();
vbo.setUsagePattern(QOpenGLBuffer::StaticDraw);
vbo.bind();
vbo.allocate(tailleVerticesBytes + tailleCoordTexturesBytes);
vbo.write(0, vertices, tailleVerticesBytes);
vbo.write(tailleVerticesBytes, coordTexture, tailleCoordTexturesBytes);
program->enableAttributeArray(vertexLocation);
program->setAttributeBuffer(vertexLocation, GL_FLOAT, 0, 2);
program->enableAttributeArray(texcoordLocation);
program->setAttributeBuffer(texcoordLocation, GL_FLOAT, tailleVerticesBytes, 2);
vbo.release();
vao.release();
program->release();
}
void GLWidget::paintGL(){
glClear(GL_COLOR_BUFFER_BIT);
program->bind();
{
vao.bind();
glBindTexture(GL_TEXTURE_2D, tex);
glDrawArrays(GL_QUADS, 0, 4);
glBindTexture(GL_TEXTURE_2D, 0);
vao.release();
}
program->release();
}
void GLWidget::resizeGL(int w, int h){
glViewport(0, 0, (GLint)w, (GLint)h);
}
#endif
因此,基本上,我将如何在此代码中使用PBO?
首先要做的是在指定类型(QOpenglBuffer :: PixelUnpackBuffer)的同时创建一个QOpenGLBuffer对象,然后我想我需要将像素上载到缓冲区中,最后使用它代替glTexImage2D吗?这只是全球性想法,我不知道如何去做。
谢谢。
目标是显示高分辨率视频流(4K,60FPS),因此我需要良好的性能。
唯一正确的方法是使用某些加速的演示API(与OpenGL无关)。
如果要坚持使用OpenGL,则至少要让GPU进行视频解码并将其上传到纹理中。如何执行此操作取决于您的OS和GPU。例如,在Linux上并使用NVIDIA,您可以使用VDPAU进行加速解码,并使用NV_VDPAU_interop来获取使用解码帧填充的纹理。
如果仍然要为此使用像素解压缩缓冲区对象(PUBO;您要上传到GL =>这是一个解压缩),那就没有什么魔术了。创建一个:
QOpenGLBuffer *pubo = new QOpenGLBuffer(QOpenGLBuffer::PixelUnpackBuffer);
pubo->create();
然后用您框架的数据填充它:
pubo->bind();
pubo->allocate(...); // or, if already allocated, also write/map
现在,PUBO的作用是,如果绑定了一个调用,则某些调用将更改语义,以不从用户内存而是从PUBO读取数据。值得注意的是,上载纹理数据的调用。因此,如果周围有纹理(并且应该使用QOpenGLTexture,它使用不可变的存储,而不是手动调用glTexImage
),则可以执行以下操作:
pubo->bind();
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D,
level,
internalFormat,
width,
heigth,
border,
externalFormat,
type,
data);
由于存在PUBO绑定,最后一个参数(data
)更改了语义:它不再是指向客户端内存的指针,而是到当前绑定的像素解压缩缓冲区对象的字节偏移量。因此,如果您的纹理数据从偏移量0开始到缓冲区,则需要在缓冲区中传递0(或实际上是(const GLvoid *)0
)。否则,您需要相应地进行调整。
现在,您可以发布pubo:
pubo->release();
然后像往常一样在着色器中使用纹理,一切都会好起来。
除非立即使用纹理,否则性能不会得到任何改善!这种复杂设置的全部目的是允许GL异步传输图像数据,同时渲染在先前帧中上传的数据。如果立即使用图像数据,则GL需要同步整个管道,以等待将图像数据上传到GPU。
因此,在这种情况下,典型的设置是以循环方式使用多个PUBO。例如,如果有两个(在ping-poing中),则将数据上传到一个PBO中,然后使用前一个PBO填充并绘制纹理。这应该为GL购买“足够的时间”以实际在总线上传输当前数据,因此在下一帧纹理上载和绘制将立即找到可用的数据。
理想情况下,您还可以使用共享的OpenGL上下文在另一个线程中执行PUBO中的数据上载,并在上载完成后使用篱笆向渲染线程发出信号,以便可以填充纹理。而且,您还可以使用孤立的,固定的映射,不同步的写入等等来进一步构建。
在这一切的深度交代一个伟大的是OpenGL的章节的见解28/29,我不能重现整体可在这里,并附带提供一些代码在这里。
您还可以找到有关缓冲对象流上的OpenGL维基一些更多的信息在这里。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句