- class QSGMaterial#
The
QSGMaterial
class encapsulates rendering state for a shader program. More…Inherited by:
QSGVertexColorMaterial
,QSGFlatColorMaterial
,QSGOpaqueTextureMaterial
,QSGTextureMaterial
Synopsis#
Methods#
def
__init__()
def
flags()
def
setFlag()
Virtual methods#
def
compare()
def
createShader()
def
type()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description#
QSGMaterial
andQSGMaterialShader
subclasses form a tight relationship. For one scene graph (including nested graphs), there is one uniqueQSGMaterialShader
instance which encapsulates the shaders the scene graph uses to render that material, such as a shader to flat coloring of geometry. EachQSGGeometryNode
can have a uniqueQSGMaterial
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 functiontype()
should return a unique instance for all instances of a specific subclass. ThecreateShader()
function should return a new instance ofQSGMaterialShader
, specific to that subclass ofQSGMaterial
.A minimal
QSGMaterial
implementation could look like this:class Material : public QSGMaterial { public: QSGMaterialType *type() const override { static QSGMaterialType type; return &type; } QSGMaterialShader *createShader(QSGRendererInterface::RenderMode) const override { return new Shader; } };
See the Custom Material example for an introduction on implementing a
QQuickItem
subclass backed by aQSGGeometryNode
and a custom material.Note
createShader()
is called only once perQSGMaterialType
, to reduce redundant work with shader preparation. If aQSGMaterial
is backed by multiple sets of vertex and fragment shader combinations, the implementation oftype()
must return a different, uniqueQSGMaterialType
pointer for each combination of shaders.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.
- class Flag#
Constant
Description
QSGMaterial.Blending
(inherits
enum.Flag
) Set this flag to true if the material requires blending to be enabled during rendering.QSGMaterial.RequiresDeterminant
Set this flag to true if the material relies on the determinant of the matrix of the geometry nodes for rendering.
QSGMaterial.RequiresFullMatrixExceptTranslate
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
Set this flag to true if the material relies on the full matrix of the geometry nodes for rendering.
QSGMaterial.NoBatching
Set this flag to true if the material uses shaders that are incompatible with the scene graph’s batching mechanism . This is relevant in certain advanced usages, such as, directly manipulating
gl_Position.z
in the vertex shader. Such solutions are often tied to a specific scene structure, and are likely not safe to use with arbitrary contents in a scene. Thus this flag should only be set after appropriate investigation, and will never be needed for the vast majority of materials. Setting this flag can lead to reduced performance due to having to issue more draw calls. This flag was introduced in Qt 6.3.QSGMaterial.CustomCompileStep
In Qt 6 this flag is identical to NoBatching. Prefer using NoBatching instead.
- __init__()#
- compare(other)#
- Parameters:
other –
QSGMaterial
- Return type:
int
Compares this material to
other
and returns 0 if they are equal; -1 if this material should sort beforeother
and 1 ifother
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 sametype()
.- abstract createShader(renderMode)#
- Parameters:
renderMode –
RenderMode
- Return type:
This function returns a new instance of a the
QSGMaterialShader
implementation used to render geometry for a specific implementation ofQSGMaterial
.The function will be called only once for each combination of material type and
renderMode
and will be cached internally.For most materials, the
renderMode
can be ignored. A few materials may need custom handling for specific render modes. For instance if the material implements antialiasing in a way that needs to account for perspective transformations when RenderMode3D is in use.Returns the material’s flags.
Sets the flags
flags
on this material ifon
is true; otherwise clears the attribute.- abstract type()#
- Return type:
This function is called by the scene graph to query an identifier that is unique to the
QSGMaterialShader
instantiated bycreateShader()
.For many materials, the typical approach will be to return a pointer to a static, and so globally available,
QSGMaterialType
instance. TheQSGMaterialType
is an opaque object. Its purpose is only to serve as a type-safe, simple way to generate unique material identifiers.QSGMaterialType *type() const override { static QSGMaterialType type; return &type; }