aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/scenegraph/coreapi/qsgmaterial.cpp
blob: 7b335927e67566f082467321f8b23c6969a0ea4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtQuick module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "qsgmaterial.h"
#include "qsgrenderer_p.h"

QT_BEGIN_NAMESPACE

#ifndef QT_NO_DEBUG
bool qsg_material_failure = false;
bool qsg_test_and_clear_material_failure()
{
    bool fail = qsg_material_failure;
    qsg_material_failure = false;
    return fail;
}

void qsg_set_material_failure()
{
    qsg_material_failure = true;
}
#endif

#ifndef QT_NO_DEBUG
static const bool qsg_leak_check = !qEnvironmentVariableIsEmpty("QML_LEAK_CHECK");
#endif

/*!
    \group qtquick-scenegraph-materials
    \title Qt Quick Scene Graph Material Classes
    \brief classes used to define materials in the Qt Quick Scene Graph.

    This page lists the material classes in \l {Qt Quick}'s
    \l {scene graph}{Qt Quick Scene Graph}.
 */

#ifndef QT_NO_DEBUG
static int qt_material_count = 0;

static void qt_print_material_count()
{
    qDebug("Number of leaked materials: %i", qt_material_count);
    qt_material_count = -1;
}
#endif

/*!
    \class QSGMaterialType
    \brief The QSGMaterialType class is used as a unique type token in combination with QSGMaterial.
    \inmodule QtQuick
    \ingroup qtquick-scenegraph-materials

    It serves no purpose outside the QSGMaterial::type() function.

    \note All classes with QSG prefix should be used solely on the scene graph's
    rendering thread. See \l {Scene Graph and Rendering} for more information.
 */

/*!
    \class QSGMaterial
    \brief The QSGMaterial class encapsulates rendering state for a shader program.
    \inmodule QtQuick
    \ingroup qtquick-scenegraph-materials

    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:
    \code
        class Material : public QSGMaterial
        {
        public:
            QSGMaterialType *type() const { static QSGMaterialType type; return &type; }
            QSGMaterialShader *createShader() const { return new Shader; }
        };
    \endcode

    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:
    \code
        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;
                }
            }
        };
    \endcode

    \note All classes with QSG prefix should be used solely on the scene graph's
    rendering thread. See \l {Scene Graph and Rendering} for more information.
 */

/*!
    \internal
 */

QSGMaterial::QSGMaterial()
{
    Q_UNUSED(m_reserved);
#ifndef QT_NO_DEBUG
    if (qsg_leak_check) {
        ++qt_material_count;
        static bool atexit_registered = false;
        if (!atexit_registered) {
            atexit(qt_print_material_count);
            atexit_registered = true;
        }
    }
#endif
}


/*!
    \internal
 */

QSGMaterial::~QSGMaterial()
{
#ifndef QT_NO_DEBUG
    if (qsg_leak_check) {
        --qt_material_count;
        if (qt_material_count < 0)
            qDebug("Material destroyed after qt_print_material_count() was called.");
    }
#endif
}



/*!
    \enum QSGMaterial::Flag

    \value Blending Set this flag to true if the material requires GL_BLEND to be
    enabled during rendering.

    \value RequiresDeterminant Set this flag to true if the material relies on
    the determinant of the matrix of the geometry nodes for rendering.

    \value RequiresFullMatrixExceptTranslate Set this flag to true if the material
    relies on the full matrix of the geometry nodes for rendering, except the translation part.

    \value RequiresFullMatrix Set this flag to true if the material relies on
    the full matrix of the geometry nodes for rendering.

    \value CustomCompileStep 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.

    \value SupportsRhiShader 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.

    \value RhiShaderWanted 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.
 */

/*!
    \fn QSGMaterial::Flags QSGMaterial::flags() const

    Returns the material's flags.
 */



/*!
    Sets the flags \a flags on this material if \a on is true;
    otherwise clears the attribute.
*/

void QSGMaterial::setFlag(Flags flags, bool on)
{
    if (on)
        m_flags |= flags;
    else
        m_flags &= ~flags;
}



/*!
    Compares this material to \a other and returns 0 if they are equal; -1 if
    this material should sort before \a other and 1 if \a 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 \a other is guaranteed to have the same type().
 */

int QSGMaterial::compare(const QSGMaterial *other) const
{
    Q_ASSERT(other && type() == other->type());
    return qint64(this) - qint64(other);
}



/*!
    \fn QSGMaterialType QSGMaterial::type() const

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



/*!
    \fn 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.

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

QT_END_NAMESPACE