QSGMaterial Class

The QSGMaterial class encapsulates rendering state for a shader program. 更多...

头: #include <QSGMaterial>
qmake: QT += quick
继承者:

QSGFlatColorMaterial , QSGOpaqueTextureMaterial , QSGSimpleMaterial ,和 QSGVertexColorMaterial

公共类型

enum Flag { Blending, RequiresDeterminant, RequiresFullMatrixExceptTranslate, RequiresFullMatrix, CustomCompileStep, …, RhiShaderWanted }
flags Flags

公共函数

virtual int compare (const QSGMaterial * other ) const
virtual QSGMaterialShader * createShader () const = 0
QSGMaterial::Flags flags () const
void setFlag (QSGMaterial::Flags flags , bool on = true)
virtual QSGMaterialType * type () const = 0

详细描述

The QSGMaterial, QSGMaterialShader and QSGMaterialRhiShader subclasses form a tight relationship. For one scene graph (including nested graphs), there is one unique QSGMaterialShader or QSGMaterialRhiShader instance which encapsulates the shaders the scene graph uses to render that material, such as a shader to flat coloring of geometry. Each QSGGeometryNode can have a unique QSGMaterial containing the how the shader should be configured when drawing that node, such as the actual color to used to render the geometry.

QSGMaterial has two virtual functions that both need to be implemented. The function type () should return a unique instance for all instances of a specific subclass. The createShader () function should return a new instance of QSGMaterialShader or QSGMaterialRhiShader , specific to that subclass of QSGMaterial.

A minimal QSGMaterial implementation could look like this:

class Material : public QSGMaterial
{
public:
    QSGMaterialType *type() const { static QSGMaterialType type; return &type; }
    QSGMaterialShader *createShader() const { return new Shader; }
};
					

This is suitable only for the OpenGL-based, traditional renderer of the scene graph. When using the new, graphics API abstracted renderer, materials must create QSGMaterialRhiShader instances instead, or in addition:

class Material : public QSGMaterial
{
public:
    Material() { setFlag(SupportsRhiShader, true); }
    QSGMaterialType *type() const { static QSGMaterialType type; return &type; }
    QSGMaterialShader *createShader() {
        if (flags().testFlag(RhiShaderWanted)) {
            return new RhiShader;
        } else {
            // this is optional, relevant for materials that intend to be usable with the legacy OpenGL renderer as well
            return new Shader;
        }
    }
};
					

注意: 所有带有 QSG 前缀的类只应用于场景图形渲染线程。见 场景图形和渲染 了解更多信息。

成员类型文档编制

enum QSGMaterial:: Flag
flags QSGMaterial:: Flags

常量 描述
QSGMaterial::Blending 0x0001 Set this flag to true if the material requires GL_BLEND to be enabled during rendering.
QSGMaterial::RequiresDeterminant 0x0002 Set this flag to true if the material relies on the determinant of the matrix of the geometry nodes for rendering.
QSGMaterial::RequiresFullMatrixExceptTranslate 0x0004 | RequiresDeterminant Set this flag to true if the material relies on the full matrix of the geometry nodes for rendering, except the translation part.
QSGMaterial::RequiresFullMatrix 0x0008 | RequiresFullMatrixExceptTranslate Set this flag to true if the material relies on the full matrix of the geometry nodes for rendering.
QSGMaterial::CustomCompileStep 0x0010 Starting with Qt 5.2, the scene graph will not always call QSGMaterialShader::compile () when its shader program is compiled and linked. Set this flag to enforce that the function is called.
QSGMaterial::SupportsRhiShader 0x0020 Starting with Qt 5.14, the scene graph supports QSGMaterialRhiShader as an alternative to the OpenGL-specific QSGMaterialShader . Set this flag to indicate createShader () is capable of returning QSGMaterialRhiShader instances when the RhiShaderWanted flag is set.
QSGMaterial::RhiShaderWanted 0x1000 This flag is set by the scene graph, not by the QSGMaterial . When set, and that can only happen when SupportsRhiShader was set by the material, it indicates that createShader () must return a QSGMaterialRhiShader instance instead of QSGMaterialShader .

Flags 类型是 typedef 对于 QFlags <Flag>。它存储 Flag 值的 OR 组合。

成员函数文档编制

[虚拟] int QSGMaterial:: compare (const QSGMaterial * other ) const

Compares this material to other and returns 0 if they are equal; -1 if this material should sort before other and 1 if other should sort before.

The scene graph can reorder geometry nodes to minimize state changes. The compare function is called during the sorting process so that the materials can be sorted to minimize state changes in each call to QSGMaterialShader::updateState ().

The this pointer and other is guaranteed to have the same type ().

[pure virtual] QSGMaterialShader *QSGMaterial:: createShader () const

This function returns a new instance of a the QSGMaterialShader implementatation used to render geometry for a specific implementation of QSGMaterial .

The function will be called only once for each material type that exists in the scene graph and will be cached internally.

QSGMaterial reports SupportsRhiShader in flags (), the scene graph may request a QSGMaterialRhiShader 而不是 QSGMaterialShader . This is indicated by having the RhiShaderWanted flag set. In this case the return value must be a QSGRhiMaterialShader subclass.

QSGMaterial::Flags QSGMaterial:: flags () const

Returns the material's flags.

void QSGMaterial:: setFlag ( QSGMaterial::Flags flags , bool on = true)

Sets the flags flags on this material if on 为 true;否则清零属性。

[pure virtual] QSGMaterialType *QSGMaterial:: type () const

This function is called by the scene graph to return a unique instance per subclass.