QSGSimpleMaterialShader ClassThe QSGSimpleMaterialShader provides a convenient way of building custom materials for the scene graph. More... #include <QSGSimpleMaterialShader> Inherits: QSGMaterialShader. Public Types
Public Functions
Reimplemented Public Functions
Protected Functions
Detailed DescriptionThe QSGSimpleMaterialShader provides a convenient way of building custom materials for the scene graph. 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 implemenation must be instantiated with a unique C++ type. Member Function Documentation
|