QOpenGLShaderProgram Class
The QOpenGLShaderProgram class allows OpenGL shader programs to be linked and used. More...
| Header: | #include <QOpenGLShaderProgram> |
| qmake: | QT += gui |
| Since: | Qt 5.0 |
| Inherits: | QObject |
This class was introduced in Qt 5.0.
Detailed Description
Introduction
This class supports shader programs written in the OpenGL Shading Language (GLSL) and in the OpenGL/ES Shading Language (GLSL/ES).
QOpenGLShader and QOpenGLShaderProgram shelter the programmer from the details of compiling and linking vertex and fragment shaders.
The following example creates a vertex shader program using the supplied source code. Once compiled and linked, the shader program is activated in the current QOpenGLContext by calling QOpenGLShaderProgram::bind():
QOpenGLShader shader(QOpenGLShader::Vertex); shader.compileSourceCode(code); QOpenGLShaderProgram program(context); program.addShader(&shader); program.link(); program.bind();
Writing Portable Shaders
Shader programs can be difficult to reuse across OpenGL implementations because of varying levels of support for standard vertex attributes and uniform variables. In particular, GLSL/ES lacks all of the standard variables that are present on desktop OpenGL systems: gl_Vertex, gl_Normal, gl_Color, and so on. Desktop OpenGL lacks the variable qualifiers highp, mediump, and lowp.
The QOpenGLShaderProgram class makes the process of writing portable shaders easier by prefixing all shader programs with the following lines on desktop OpenGL:
#define highp #define mediump #define lowp
This makes it possible to run most GLSL/ES shader programs on desktop systems. The programmer should restrict themselves to just features that are present in GLSL/ES, and avoid standard variable names that only work on the desktop.
Simple Shader Example
program.addShaderFromSourceCode(QOpenGLShader::Vertex, "attribute highp vec4 vertex;\n" "uniform highp mat4 matrix;\n" "void main(void)\n" "{\n" " gl_Position = matrix * vertex;\n" "}"); program.addShaderFromSourceCode(QOpenGLShader::Fragment, "uniform mediump vec4 color;\n" "void main(void)\n" "{\n" " gl_FragColor = color;\n" "}"); program.link(); program.bind(); int vertexLocation = program.attributeLocation("vertex"); int matrixLocation = program.uniformLocation("matrix"); int colorLocation = program.uniformLocation("color");
With the above shader program active, we can draw a green triangle as follows:
static GLfloat const triangleVertices[] = { 60.0f, 10.0f, 0.0f, 110.0f, 110.0f, 0.0f, 10.0f, 110.0f, 0.0f }; QColor color(0, 255, 0, 255); QMatrix4x4 pmvMatrix; pmvMatrix.ortho(rect()); program.enableAttributeArray(vertexLocation); program.setAttributeArray(vertexLocation, triangleVertices, 3); program.setUniformValue(matrixLocation, pmvMatrix); program.setUniformValue(colorLocation, color); glDrawArrays(GL_TRIANGLES, 0, 3); program.disableAttributeArray(vertexLocation);
Binary Shaders and Programs
Binary shaders may be specified using glShaderBinary() on the return value from QOpenGLShader::shaderId(). The QOpenGLShader instance containing the binary can then be added to the shader program with addShader() and linked in the usual fashion with link().
Binary programs may be specified using glProgramBinaryOES() on the return value from programId(). Then the application should call link(), which will notice that the program has already been specified and linked, allowing other operations to be performed on the shader program. The shader program's id can be explicitly created using the create() function.
Caching Program Binaries
As of Qt 5.9, support for caching program binaries on disk is built in. To enable this, switch to using addCacheableShaderFromSourceCode() and addCacheableShaderFromSourceFile(). With an OpenGL ES 3.x context or support for GL_ARB_get_program_binary, this will transparently cache program binaries under QStandardPaths::GenericCacheLocation or QStandardPaths::CacheLocation. When support is not available, calling the cacheable function variants is equivalent to the normal ones.
Note: Some drivers do not have any binary formats available, even though they advertise the extension or offer OpenGL ES 3.0. In this case program binary support will be disabled.
See also QOpenGLShader.