W3cubDocs

/Qt 5.15

QSGSimpleMaterialShader Class

template <typename State> class QSGSimpleMaterialShader

The QSGSimpleMaterialShader class provides a convenient way of building custom OpenGL-based materials for the scene graph. More...

Header: #include <QSGSimpleMaterialShader>
qmake: QT += quick
Inherits: QSGMaterialShader

This class is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.

Public Functions

virtual QList<QByteArray> attributes() const = 0
virtual void resolveUniforms()
const char * uniformMatrixName() const
const char * uniformOpacityName() const
virtual void updateState(const State *newState, const State *oldState) = 0

Macros

QSG_DECLARE_SIMPLE_COMPARABLE_SHADER(Shader, State)
QSG_DECLARE_SIMPLE_SHADER(Shader, State)

Detailed Description

Warning: This utility class is only functional when running with the legacy OpenGL renderer of the Qt Quick scenegraph. Its usage is not recommended in new application code.

Where the QSGMaterial and QSGMaterialShader API requires a bit of boilerplate code to create a functioning material, the QSGSimpleMaterialShader tries to hide some of this through the use of templates.

QSGSimpleMaterialShader::vertexShader() and QSGSimpleMaterialShader::fragmentShader() are used to specify the actual shader source code. The names of the vertex attributes should be listed in the QSGSimpleMaterialShader::attributes()

QSGSimpleMaterialShader::updateState() is used to push the material state to the OpenGL shader program.

The actual OpenGL shader program is accessible through the QSGSimpleMaterialShader::program() function.

Each QSGSimpleMaterialShader implementation operates on a unique state struct. The state struct must be declared using the QSG_DECLARE_SIMPLE_SHADER macro.

Here is a simple example of a custom solid-color:

struct Color
{
    float r, g, b, a;
};

class MinimalShader : public QSGSimpleMaterialShader<Color>
{
    QSG_DECLARE_SIMPLE_SHADER(MinimalShader, Color)
public:

    const char *vertexShader() const {
        return
        "attribute highp vec4 vertex;               \n"
        "uniform highp mat4 qt_Matrix;              \n"
        "void main() {                              \n"
        "    gl_Position = qt_Matrix * vertex;      \n"
        "}";
    }

    const char *fragmentShader() const {
        return
        "uniform lowp float qt_Opacity;             \n"
        "uniform lowp vec4 color;                   \n"
        "void main() {                              \n"
        "    gl_FragColor = color * qt_Opacity;     \n"
        "}";
    }

    QList<QByteArray> attributes() const {
        return QList<QByteArray>() << "vertex";
    }

    void updateState(const Color *color, const Color *) {
        program()->setUniformValue("color", color->r, color->g, color->b, color->a);
    }

};

Instances of materials using this shader can be created using the createMaterial() function which will be defined by the QSG_DECLARE_SIMPLE_SHADER macro.

QSGSimpleMaterial<Color> *material = MinimalShader::createMaterial();
material->state()->r = 1;
material->state()->g = 0;
material->state()->b = 0;
material->state()->a = 1;

node->setMaterial(material);

The scene graph will often try to find materials that have the same or at least similar state so that these can be batched together inside the renderer, which gives better performance. To specify sortable material states, use QSG_DECLARE_SIMPLE_COMPARABLE_SHADER instead of QSG_DECLARE_SIMPLE_SHADER. The state struct must then also define the function:

int compare(const Type *other) const;

Warning: The QSGSimpleMaterialShader relies on template instantiation to create a QSGMaterialType which the scene graph renderer internally uses to identify this shader. For this reason, the unique QSGSimpleMaterialShader implementation must be instantiated with a unique C++ type.

Note: All classes with QSG prefix should be used solely on the scene graph's rendering thread. See Scene Graph and Rendering for more information.

See also Scene Graph - Simple Material.

Member Function Documentation

[pure virtual] QList<QByteArray> QSGSimpleMaterialShader::attributes() const

Returns a list of names, declaring the vertex attributes in the vertex shader.

[virtual] void QSGSimpleMaterialShader::resolveUniforms()

Reimplement this function to resolve the location of named uniforms in the shader program.

This function is called when the material shader is initialized.

const char *QSGSimpleMaterialShader::uniformMatrixName() const

Returns the name for the transform matrix uniform of this item. The default value is qt_Matrix.

const char *QSGSimpleMaterialShader::uniformOpacityName() const

Returns the name for the opacity uniform of this item. The default value is qt_Opacity.

[pure virtual] void QSGSimpleMaterialShader::updateState(const State *newState, const State *oldState)

Called whenever the state of this shader should be updated from oldState to newState, typical for each new set of geometries being drawn.

Both the old and the new state are passed in so that the implementation can compare and minimize the state changes when applicable.

Macro Documentation

QSG_DECLARE_SIMPLE_COMPARABLE_SHADER(Shader, State)

This macro is used to declare a QSGMaterialType and a createMaterial() function for Shader with the given State, where the State class must define a compare function on the form:

int compare(const State *other) const;

QSG_DECLARE_SIMPLE_SHADER(Shader, State)

This macro is used to declare a QSGMaterialType and a createMaterial() function for Shader with the given State.

© The Qt Company Ltd
Licensed under the GNU Free Documentation License, Version 1.3.
https://doc.qt.io/qt-5.15/qsgsimplematerialshader.html