Skip to content

PythonQt.QtGui.QOpenGLShaderProgram

QOpenGLShaderProgram

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

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.

Source: https://doc.qt.io/archives/qt-5.15/qopenglshaderprogram.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
addShader function (self, shader: Any) -> bool X.addShader(shader) -> bool
addShaderFromSourceCode function (self, type: Any, source: Any) -> bool X.addShaderFromSourceCode(type, source) -> bool
addShaderFromSourceFile function (self, type: Any, fileName: str) -> bool X.addShaderFromSourceFile(type, fileName) -> bool
attributeLocation function (self, name: Any) -> int X.attributeLocation(name) -> int
bind function (self) -> bool X.bind() -> bool
bindAttributeLocation function (self, name: Any, location: int) -> None X.bindAttributeLocation(name, location)
blockSignals function (self, b: Any) -> bool X.blockSignals(b) -> bool (shared captured text)
childEvent function (self, event: Any) -> Any (shared captured text)
children function (self) -> tuple X.children() -> tuple (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
connect function (self, signal: Any, receiver: Any, slot: Any, type: Any) -> bool X.connect(signal, receiver, slot, type) -> bool (shared captured text)
create function (self) -> bool X.create() -> bool
customEvent function (self, event: Any) -> Any (shared captured text)
defaultInnerTessellationLevels function (self) -> tuple X.defaultInnerTessellationLevels() -> tuple
defaultOuterTessellationLevels function (self) -> tuple X.defaultOuterTessellationLevels() -> tuple
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
deleteLater function (self) -> Any (shared captured text)
destroyed function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtGui.QOpenGLShaderProgram.destroyed; argument details are not documented.
disableAttributeArray function (self, name: Any) -> None Disables the vertex array at location in this shader program that was enabled by a previous call to…
disconnect function (self, signal: Any, receiver: Any, slot: Any) -> bool X.disconnect(signal, receiver, slot) -> bool (shared captured text)
dumpObjectInfo function (self) -> Any (shared captured text)
dumpObjectTree function (self) -> Any (shared captured text)
dynamicPropertyNames function (self) -> Any (shared captured text)
enableAttributeArray function (self, name: Any) -> None Enables the vertex array at location in this shader program so that the value set by [setAttributeArray](https://doc.qt.io/archives/qt-5.15/qopenglshaderprogram.html#setAttributeArray)() on…
event function (self, event: Any) -> bool X.event(event) -> bool (shared captured text)
eventFilter function (self, watched: Any, event: Any) -> bool X.eventFilter(watched, event) -> bool (shared captured text)
findChild function (self, type: Any, name: Any) -> Any X.findChild(type, name) -> PythonQt.private.QObject (shared captured text)
findChildren function (self, type: Any, name: Any) -> tuple X.findChildren(type, name) -> tuple (shared captured text)
hasOpenGLShaderPrograms function (self, context: Any) -> bool X.hasOpenGLShaderPrograms(context) -> bool
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
installEventFilter function (self, filterObj: Any) -> Any (shared captured text)
isLinked function (self) -> bool X.isLinked() -> bool
isSignalConnected function (self, signal: Any) -> bool X.isSignalConnected(signal) -> bool (shared captured text)
isWidgetType function (self) -> bool X.isWidgetType() -> bool (shared captured text)
isWindowType function (self) -> bool X.isWindowType() -> bool (shared captured text)
killTimer function (self, id: Any) -> Any (shared captured text)
link function (self) -> bool X.link() -> bool
log function (self) -> str X.log() -> str
maxGeometryOutputVertices function (self) -> int X.maxGeometryOutputVertices() -> int
metaObject function (self) -> Any X.metaObject() -> PythonQt.QtCore.QMetaObject (shared captured text)
moveToThread function (self, thread: Any) -> Any (shared captured text)
objectName attribute Any (shared captured text)
objectNameChanged function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtGui.QOpenGLShaderProgram.objectNameChanged; argument details are not documented.
parent function (self) -> Any X.parent() -> PythonQt.private.QObject (shared captured text)
patchVertexCount function (self) -> int X.patchVertexCount() -> int
programId function (self) -> int X.programId() -> int
property function (self, name: Any) -> Any X.property(name) -> object (shared captured text)
release function (self) -> None Releases the active shader program from the current [QOpenGLContext](https://doc.qt.io/archives/qt-5.15/qopenglcontext.html).
removeAllShaders function (self) -> None Removes all of the shaders that were added to this program previously.
removeEventFilter function (self, obj: Any) -> Any (shared captured text)
removeShader function (self, shader: Any) -> None Removes shader from this shader program.
sender function (self) -> Any X.sender() -> PythonQt.private.QObject (shared captured text)
senderSignalIndex function (self) -> int X.senderSignalIndex() -> int (shared captured text)
setAttributeArray function (self, name: Any, type: Any, values: Any, tupleSize: int, stride: int) -> None X.setAttributeArray(name, type, values, tupleSize, stride)
setAttributeBuffer function (self, name: Any, type: int, offset: int, tupleSize: int, stride: int) -> None X.setAttributeBuffer(name, type, offset, tupleSize, stride)
setAttributeValue function (self, name: Any, x: Any, y: Any, z: Any, w: Any) -> None X.setAttributeValue(name, x, y, z, w)
setDefaultInnerTessellationLevels function (self, levels: Any) -> None Sets the default outer tessellation levels to be used by the tessellation primitive generator in the event that the tessellation control shader does not output them to levels.
setDefaultOuterTessellationLevels function (self, levels: Any) -> None Sets the default outer tessellation levels to be used by the tessellation primitive generator in the event that the tessellation control shader does not output them to levels.
setObjectName function (self, name: Any) -> Any (shared captured text)
setParent function (self, parent: Any) -> Any (shared captured text)
setPatchVertexCount function (self, count: int) -> None Use this function to specify to OpenGL the number of vertices in a patch to count.
setProperty function (self, name: Any, value: Any) -> bool X.setProperty(name, value) -> bool (shared captured text)
setUniformValue function (self, name: Any, x: Any, y: Any, z: Any, w: Any) -> None X.setUniformValue(name, x, y, z, w)
setUniformValueArray function (self, name: Any, values: Any, count: int, tupleSize: Any) -> None X.setUniformValueArray(name, values, count, tupleSize)
shaders function (self) -> tuple X.shaders() -> tuple
signalsBlocked function (self) -> bool X.signalsBlocked() -> bool (shared captured text)
startTimer function (self, interval: Any, timerType: Any) -> int X.startTimer(interval, timerType) -> int (shared captured text)
thread function (self) -> Any (shared captured text)
timerEvent function (self, event: Any) -> Any (shared captured text)
tr function (self, text: Any, ambig: Any, n: Any) -> str X.tr(text, ambig, n) -> str (shared captured text)
uniformLocation function (self, name: Any) -> int X.uniformLocation(name) -> int