PySide6.QtQuick.QSGMaterial¶
- class QSGMaterial¶
- The - QSGMaterialclass encapsulates rendering state for a shader program. More…- Inherited by: - QSGVertexColorMaterial,- QSGFlatColorMaterial,- QSGOpaqueTextureMaterial,- QSGTextureMaterial- Synopsis¶- Methods¶- def - __init__()
- def - flags()
- def - setFlag()
- def - viewCount()
 - 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¶- QSGMaterialand- QSGMaterialShadersubclasses form a tight relationship. For one scene graph (including nested graphs), there is one unique- QSGMaterialShaderinstance which encapsulates the shaders the scene graph uses to render that material, such as a shader to flat coloring of geometry. Each- QSGGeometryNodecan have a unique- QSGMaterialcontaining the how the shader should be configured when drawing that node, such as the actual color to used to render the geometry.- QSGMaterialhas 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, specific to that subclass of- QSGMaterial.- A minimal - QSGMaterialimplementation 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 - QQuickItemsubclass backed by a- QSGGeometryNodeand a custom material.- Note - createShader()is called only once per- QSGMaterialType, to reduce redundant work with shader preparation. If a- QSGMaterialis backed by multiple sets of vertex and fragment shader combinations, the implementation of- type()must return a different, unique- QSGMaterialTypepointer 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. - See also - QSGMaterialShaderScene Graph - Custom Material Scene Graph - Two Texture Providers Scene Graph - Graph- 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.zin 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 - otherand returns 0 if they are equal; -1 if this material should sort before- otherand 1 if- othershould 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 - otheris guaranteed to have the same- type().- abstract createShader(renderMode)¶
- Parameters:
- renderMode – - RenderMode
- Return type:
 
 - This function returns a new instance of a the - QSGMaterialShaderimplementation used to render geometry for a specific implementation of- QSGMaterial.- The function will be called only once for each combination of material type and - renderModeand will be cached internally.- For most materials, the - renderModecan 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 - flagson this material if- onis 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 - QSGMaterialShaderinstantiated by- createShader().- For many materials, the typical approach will be to return a pointer to a static, and so globally available, - QSGMaterialTypeinstance. The- QSGMaterialTypeis 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; } - viewCount()¶
- Return type:
- int 
 
 - Returns The number of views in case of the material is used in multiview rendering. - Note - The return value is valid only when called from - createShader(), and afterwards. The value is not necessarily up-to-date before- createShader()is invokved by the scene graph.- Normally the return value is - 1. A view count greater than 2 implies a multiview render pass. Materials that support multiview are expected to query viewCount() in- createShader(), or in their- QSGMaterialShaderconstructor, and ensure the appropriate shaders are picked. The vertex shader is then expected to use- gl_ViewIndexto index the modelview-projection matrix array as there are multiple matrices in multiview mode. (one for each view)- As an example, take the following simple vertex shader: - #version 440 layout(location = 0) in vec4 vertexCoord; layout(location = 1) in vec4 vertexColor; layout(location = 0) out vec4 color; layout(std140, binding = 0) uniform buf { mat4 matrix[2]; float opacity; }; void main() { gl_Position = matrix[gl_ViewIndex] * vertexCoord; color = vertexColor * opacity; } - This shader is prepared to handle 2 views, and 2 views only. It is not compatible with other view counts. When conditioning the shader, the - qsbtool has to be invoked with- --view-count 2or, if using the CMake integration,- VIEW_COUNT 2must be specified in the- qt_add_shaders()command.- Note - A line with - #extension GL_EXT_multiview : requireis injected automatically by- qsbwhenever a view count of 2 or greater is set.- Developers are encouraged to use the automatically injected preprocessor variable - QSHADER_VIEW_COUNTto simplify the handling of the different number of views. For example, if there is a need to support both non-multiview and multiview with a view count of 2 in the same source file, the following could be done:- #version 440 layout(location = 0) in vec4 vertexCoord; layout(location = 1) in vec4 vertexColor; layout(location = 0) out vec4 color; layout(std140, binding = 0) uniform buf { #if QSHADER_VIEW_COUNT >= 2 mat4 matrix[QSHADER_VIEW_COUNT]; #else mat4 matrix; #endif float opacity; }; void main() { #if QSHADER_VIEW_COUNT >= 2 gl_Position = matrix[gl_ViewIndex] * vertexCoord; #else gl_Position = matrix * vertexCoord; #endif color = vertexColor * opacity; } - The same source file can now be run through - qsbor- qt_add_shaders()twice, once without specify the view count, and once with the view count set to 2. The material can then pick the appropriate .qsb file based on viewCount() at run time.- With CMake, this could looks similar to the following. With this example the corresponding - QSGMaterialShaderis expected to choose between- :/shaders/example.vert.qsband- :/shaders/multiview/example.vert.qsbbased on the value of viewCount(). (same goes for the fragment shader)- qt_add_shaders(application "application_shaders" PREFIX / FILES shaders/example.vert shaders/example.frag ) qt_add_shaders(application "application_multiview_shaders" GLSL 330,300es HLSL 61 MSL 12 VIEW_COUNT 2 PREFIX / FILES shaders/example.vert shaders/example.frag OUTPUTS shaders/multiview/example.vert shaders/multiview/example.frag ) - Note - The fragment shader should be treated the same way the vertex shader is, even though the fragment shader code cannot have any dependency on the view count ( - gl_ViewIndex), for maximum portability. There are two reasons for including fragment shaders too in the multiview set. One is that mixing different shader versions within the same graphics pipeline can be problematic, depending on the underlying graphics API: with D3D12 for example, mixing HLSL shaders for shader model 5.0 and 6.1 would generate an error. The other is that having- QSHADER_VIEW_COUNTdefined in fragment shaders can be very useful, for example when sharing a uniform buffer layout between the vertex and fragment stages.- Note - For OpenGL the minimum GLSL version for vertex shaders relying on - gl_ViewIndexis- 330. Lower versions may be accepted at build time, but may lead to an error at run time, depending on the OpenGL implementation.- As a convenience, there is also a - MULTIVIEWoption for qt_add_shaders(). This first runs the- qsbtool normally, then overrides- VIEW_COUNTto- 2, sets- GLSL,- HLSL,- MSLto some suitable defaults, and runs- qsbagain, this time outputting .qsb files with a suffix added. The material implementation can then use the- setShaderFileName()overload taking a- viewCountargument, that automatically picks the correct .qsb file.- The following is therefore mostly equivalent to the example call shown above, except that no manually managed output files need to be specified. Note that there can be cases when the automatically chosen shading language versions are not sufficient, in which case applications should continue specify everything explicitly. - qt_add_shaders(application "application_multiview_shaders" MULTIVIEW PREFIX / FILES shaders/example.vert shaders/example.frag ) - See QRhi::MultiView, QRhiColorAttachment::setMultiViewCount(), and QRhiGraphicsPipeline::setMultiViewCount() for further, lower-level details on multiview support in Qt. The Qt Quick scene graph renderer is prepared to recognize multiview render targets, when specified via - fromRhiRenderTarget()or the 3D API specific functions, such as- fromVulkanImage()with an- arraySizeargument greater than 1. The renderer will then propagate the view count to graphics pipelines and the materials.