Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsgmaterialshader.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qsgmaterial.h"
7#include <QtCore/QFile>
8
10
11/*!
12 \class QSGMaterialShader
13 \brief The QSGMaterialShader class represents a graphics API independent shader program.
14 \inmodule QtQuick
15 \ingroup qtquick-scenegraph-materials
16 \since 5.14
17
18 QSGMaterialShader represents a combination of vertex and fragment shaders,
19 data that define the graphics pipeline state changes, and logic that
20 updates graphics resources, such as uniform buffers and textures.
21
22 \note All classes with QSG prefix should be used solely on the scene graph's
23 rendering thread. See \l {Scene Graph and Rendering} for more information.
24
25 The QSGMaterial and QSGMaterialShader form a tight relationship. For one
26 scene graph (including nested graphs), there is one unique
27 QSGMaterialShader instance that encapsulates the shaders and other data
28 the scene graph uses to render an object with that material. Each
29 QSGGeometryNode can have a unique QSGMaterial that defines how the graphics
30 pipeline must be configured while drawing the node. An instance of
31 QSGMaterialShader is never created explicitly by the user, it will be
32 created on demand by the scene graph through QSGMaterial::createShader().
33 The scene graph creates an instance of QSGMaterialShader by calling the
34 QSGMaterial::createShader() method, ensuring that there is only one
35 instance of each shader implementation.
36
37 In Qt 5, QSGMaterialShader was tied to OpenGL. It was built directly on
38 QOpenGLShaderProgram and had functions like \c updateState() that could
39 issue arbitrary OpenGL commands. This is no longer the case in Qt 6.
40 QSGMaterialShader is not strictly data-oriented, meaning it provides data
41 (shaders and the desired pipeline state changes) together with logic that
42 updates data in a uniform buffer. Graphics API access is not provided. This
43 means that a QSGMaterialShader cannot make OpenGL, Vulkan, Metal, or Direct
44 3D calls on its own. Together with the unified shader management, this
45 allows a QSGMaterialShader to be written once, and be functional with any of
46 the supported graphics APIs at run time.
47
48 The shaders set by calling the protected setShaderFileName() function
49 control what material does with the vertex data from the geometry, and how
50 the fragments are shaded. A QSGMaterialShader will typically set a vertex
51 and a fragment shader during construction. Changing the shaders afterwards
52 may not lead to the desired effect and must be avoided.
53
54 In Qt 6, the default approach is to ship \c{.qsb} files with the application,
55 typically embedded via the resource system, and referenced when calling
56 setShaderFileName(). The \c{.qsb} files are generated offline, or at latest
57 at application build time, from Vulkan-style GLSL source code using the \c
58 qsb tool from the Qt Shader Tools module.
59
60 There are three virtuals that can be overridden. These provide the data, or
61 the logic to generate the data, for uniform buffers, textures, and pipeline
62 state changes.
63
64 updateUniformData() is the function that is most commonly reimplemented in
65 subclasses. This function is expected to update the contents of a
66 QByteArray that will then be exposed to the shaders as a uniform buffer.
67 Any QSGMaterialShader that has a uniform block in its vertex or fragment
68 shader must reimplement updateUniformData().
69
70 updateSampledImage() is relevant when the shader code samples textures. The
71 function will be invoked for each sampler (or combined image sampler, in
72 APIs where relevant), giving it the option to specify which QSGTexture
73 should be exposed to the shader.
74
75 The shader pipeline state changes are less often used. One use case is
76 materials that wish to use a specific blend mode. The relevant function is
77 updateGraphicsPipelineState(). This function is not called unless the
78 QSGMaterialShader has opted in by setting the flag
79 UpdatesGraphicsPipelineState. The task of the function is to update the
80 GraphicsPipelineState struct instance that is passed to it with the
81 desired changes. Currently only blending and culling-related features are
82 available, other states cannot be controlled by materials.
83
84 A minimal example, that also includes texture support, could be the
85 following. Here we assume that Material is the QSGMaterial that creates an
86 instance of Shader in its \l{QSGMaterial::createShader()}{createShader()},
87 and that it holds a QSGTexture we want to sample in the fragment shader. The
88 vertex shader relies only on the modelview-projection matrix.
89
90 \code
91 class Shader : public QSGMaterialShader
92 {
93 public:
94 Shader()
95 {
96 setShaderFileName(VertexStage, QLatin1String(":/materialshader.vert.qsb"));
97 setShaderFileName(FragmentStage, QLatin1String(":/materialshader.frag.qsb"));
98 }
99
100 bool updateUniformData(RenderState &state, QSGMaterial *, QSGMaterial *)
101 {
102 bool changed = false;
103 QByteArray *buf = state.uniformData();
104 if (state.isMatrixDirty()) {
105 const QMatrix4x4 m = state.combinedMatrix();
106 memcpy(buf->data(), m.constData(), 64);
107 changed = true;
108 }
109 return changed;
110 }
111
112 void updateSampledImage(RenderState &, int binding, QSGTexture **texture, QSGMaterial *newMaterial, QSGMaterial *)
113 {
114 Material *mat = static_cast<Material *>(newMaterial);
115 if (binding == 1)
116 *texture = mat->texture();
117 }
118 };
119 \endcode
120
121 The Vulkan-style GLSL source code for the shaders could look like the
122 following. These are expected to be preprocessed offline using the \c qsb
123 tool, which generates the \c{.qsb} files referenced in the Shader()
124 constructor.
125
126 \badcode
127 #version 440
128 layout(location = 0) in vec4 aVertex;
129 layout(location = 1) in vec2 aTexCoord;
130 layout(location = 0) out vec2 vTexCoord;
131 layout(std140, binding = 0) uniform buf {
132 mat4 qt_Matrix;
133 } ubuf;
134 out gl_PerVertex { vec4 gl_Position; };
135 void main() {
136 gl_Position = ubuf.qt_Matrix * aVertex;
137 vTexCoord = aTexCoord;
138 }
139 \endcode
140
141 \badcode
142 #version 440
143 layout(location = 0) in vec2 vTexCoord;
144 layout(location = 0) out vec4 fragColor;
145 layout(binding = 1) uniform sampler2D srcTex;
146 void main() {
147 vec4 c = texture(srcTex, vTexCoord);
148 fragColor = vec4(c.rgb * 0.5, 1.0);
149 }
150 \endcode
151
152 \note All classes with QSG prefix should be used solely on the scene graph's
153 rendering thread. See \l {Scene Graph and Rendering} for more information.
154
155 \sa QSGMaterial, {Scene Graph - Custom Material}, {Scene Graph - Two Texture Providers}, {Scene Graph - Graph}
156 */
157
158/*!
159 \enum QSGMaterialShader::Flag
160 Flag values to indicate special material properties.
161
162 \value UpdatesGraphicsPipelineState Setting this flag enables calling
163 updateGraphicsPipelineState().
164 */
165
166QShader QSGMaterialShaderPrivate::loadShader(const QString &filename)
167{
168 QFile f(filename);
169 if (!f.open(QIODevice::ReadOnly)) {
170 qWarning() << "Failed to find shader" << filename;
171 return QShader();
172 }
173 return QShader::fromSerialized(f.readAll());
174}
175
176void QSGMaterialShaderPrivate::clearCachedRendererData()
177{
178 for (int i = 0; i < MAX_SHADER_RESOURCE_BINDINGS; ++i)
179 textureBindingTable[i].clear();
180 for (int i = 0; i < MAX_SHADER_RESOURCE_BINDINGS; ++i)
181 samplerBindingTable[i].clear();
182}
183
184static inline QRhiShaderResourceBinding::StageFlags toSrbStage(QShader::Stage stage)
185{
186 switch (stage) {
187 case QShader::VertexStage:
188 return QRhiShaderResourceBinding::VertexStage;
189 case QShader::FragmentStage:
190 return QRhiShaderResourceBinding::FragmentStage;
191 default:
192 Q_UNREACHABLE();
193 break;
194 }
195 return { };
196}
197
198void QSGMaterialShaderPrivate::prepare(QShader::Variant vertexShaderVariant)
199{
200 ubufBinding = -1;
201 ubufSize = 0;
202 ubufStages = { };
203 memset(static_cast<void *>(combinedImageSamplerBindings), 0, sizeof(combinedImageSamplerBindings));
204 memset(static_cast<void *>(combinedImageSamplerCount), 0, sizeof(combinedImageSamplerCount));
205 vertexShader = fragmentShader = nullptr;
206 masterUniformData.clear();
207
208 clearCachedRendererData();
209
210 for (QShader::Stage stage : { QShader::VertexStage, QShader::FragmentStage }) {
211 auto it = shaderFileNames.constFind(stage);
212 if (it != shaderFileNames.cend()) {
213 const QShader s = loadShader(*it);
214 if (!s.isValid())
215 continue;
216 shaders[stage] = ShaderStageData(s);
217 // load only once, subsequent prepare() calls will have it all in shaders already
218 shaderFileNames.erase(it);
219 }
220 }
221
222 auto vsIt = shaders.find(QShader::VertexStage);
223 if (vsIt != shaders.end()) {
224 vsIt->shaderVariant = vertexShaderVariant;
225 vsIt->vertexInputLocations.clear();
226 vsIt->qt_order_attrib_location = -1;
227
228 const QShaderDescription desc = vsIt->shader.description();
229 const QVector<QShaderDescription::InOutVariable> vertexInputs = desc.inputVariables();
230 for (const QShaderDescription::InOutVariable &v : vertexInputs) {
231 if (vertexShaderVariant == QShader::BatchableVertexShader
232 && v.name == QByteArrayLiteral("_qt_order")) {
233 vsIt->qt_order_attrib_location = v.location;
234 } else {
235 vsIt->vertexInputLocations.append(v.location);
236 }
237 }
238
239 if (vsIt->vertexInputLocations.contains(vsIt->qt_order_attrib_location)) {
240 qWarning("Vertex input clash in rewritten (batchable) vertex shader at input location %d. "
241 "Vertex shaders must avoid using this location.", vsIt->qt_order_attrib_location);
242 }
243 }
244
245 for (auto it = shaders.begin(); it != shaders.end(); ++it) {
246 const QShaderDescription desc = it->shader.description();
247
248 const QVector<QShaderDescription::UniformBlock> ubufs = desc.uniformBlocks();
249 const int ubufCount = ubufs.size();
250 if (ubufCount > 1) {
251 qWarning("Multiple uniform blocks found in shader. "
252 "This should be avoided as Qt Quick supports only one.");
253 }
254 for (int i = 0; i < ubufCount; ++i) {
255 const QShaderDescription::UniformBlock &ubuf(ubufs[i]);
256 if (ubufBinding == -1 && ubuf.binding >= 0) {
257 ubufBinding = ubuf.binding;
258 ubufSize = ubuf.size;
259 ubufStages |= toSrbStage(it->shader.stage());
260 masterUniformData.fill('\0', ubufSize);
261 } else if (ubufBinding == ubuf.binding && ubuf.binding >= 0) {
262 if (ubuf.size > ubufSize) {
263 ubufSize = ubuf.size;
264 masterUniformData.fill('\0', ubufSize);
265 }
266 ubufStages |= toSrbStage(it->shader.stage());
267 } else {
268 qWarning("Uniform block %s (binding %d) ignored", ubuf.blockName.constData(),
269 ubuf.binding);
270 }
271 }
272
273 const QVector<QShaderDescription::InOutVariable> imageSamplers = desc.combinedImageSamplers();
274 const int imageSamplersCount = imageSamplers.size();
275 for (int i = 0; i < imageSamplersCount; ++i) {
276 const QShaderDescription::InOutVariable &var(imageSamplers[i]);
277
278 if (var.binding < 0)
279 continue;
280
281 if (var.binding < MAX_SHADER_RESOURCE_BINDINGS) {
282 combinedImageSamplerBindings[var.binding] |= toSrbStage(it->shader.stage());
283
284 int count = 1;
285 for (int dim : var.arrayDims)
286 count *= dim;
287
288 combinedImageSamplerCount[var.binding] = count;
289 } else {
290 qWarning("Encountered invalid combined image sampler (%s) binding %d",
291 var.name.constData(), var.binding);
292 }
293 }
294
295 if (it.key() == QShader::VertexStage)
296 vertexShader = &it.value();
297 else if (it.key() == QShader::FragmentStage)
298 fragmentShader = &it.value();
299 }
300
301 if (vertexShader && vertexShaderVariant == QShader::BatchableVertexShader && vertexShader->qt_order_attrib_location == -1)
302 qWarning("No rewriter-inserted attribute found, this should not happen.");
303}
304
305/*!
306 Constructs a new QSGMaterialShader.
307 */
308QSGMaterialShader::QSGMaterialShader()
309 : d_ptr(new QSGMaterialShaderPrivate(this))
310{
311}
312
313/*!
314 \internal
315 */
316QSGMaterialShader::QSGMaterialShader(QSGMaterialShaderPrivate &dd)
317 : d_ptr(&dd)
318{
319}
320
321/*!
322 \internal
323 */
324QSGMaterialShader::~QSGMaterialShader()
325{
326}
327
328// We have our own enum as QShader is not initially public. Internally
329// everything works with QShader::Stage however. So convert.
330static inline QShader::Stage toShaderStage(QSGMaterialShader::Stage stage)
331{
332 switch (stage) {
333 case QSGMaterialShader::VertexStage:
334 return QShader::VertexStage;
335 case QSGMaterialShader::FragmentStage:
336 return QShader::FragmentStage;
337 default:
338 Q_UNREACHABLE_RETURN(QShader::VertexStage);
339 }
340}
341
342/*!
343 Sets the \a shader for the specified \a stage.
344 */
345void QSGMaterialShader::setShader(Stage stage, const QShader &shader)
346{
347 Q_D(QSGMaterialShader);
348 d->shaders[toShaderStage(stage)] = QSGMaterialShaderPrivate::ShaderStageData(shader);
349}
350
351/*!
352 Sets the \a filename for the shader for the specified \a stage.
353
354 The file is expected to contain a serialized QShader.
355
356 \warning Shaders, including \c{.qsb} files, are assumed to be trusted
357 content. Application developers are advised to carefully consider the
358 potential implications before allowing the loading of user-provided content
359 that is not part of the application.
360 */
361void QSGMaterialShader::setShaderFileName(Stage stage, const QString &filename)
362{
363 Q_D(QSGMaterialShader);
364 d->shaderFileNames[toShaderStage(stage)] = filename;
365}
366
367/*!
368 Sets the \a filename for the shader for the specified \a stage.
369
370 The file is expected to contain a serialized QShader.
371
372 This overload is used when enabling \l{QSGMaterial::viewCount()}{multiview}
373 rendering, in particular when the \l{Qt Shader Tools Build System
374 Integration}{build system's MULTIVIEW convenience option} is used.
375
376 \a viewCount should be 2, 3, or 4. The \a filename is adjusted automatically
377 based on this.
378
379 \warning Shaders, including \c{.qsb} files, are assumed to be trusted
380 content. Application developers are advised to carefully consider the
381 potential implications before allowing the loading of user-provided content
382 that is not part of the application.
383
384 \since 6.8
385 */
386void QSGMaterialShader::setShaderFileName(Stage stage, const QString &filename, int viewCount)
387{
388 Q_D(QSGMaterialShader);
389 if (viewCount == 2)
390 d->shaderFileNames[toShaderStage(stage)] = filename + QStringLiteral(".mv2qsb");
391 else if (viewCount == 3)
392 d->shaderFileNames[toShaderStage(stage)] = filename + QStringLiteral(".mv3qsb");
393 else if (viewCount == 4)
394 d->shaderFileNames[toShaderStage(stage)] = filename + QStringLiteral(".mv4qsb");
395 else
396 d->shaderFileNames[toShaderStage(stage)] = filename;
397}
398
399/*!
400 \return the currently set flags for this material shader.
401 */
402QSGMaterialShader::Flags QSGMaterialShader::flags() const
403{
404 Q_D(const QSGMaterialShader);
405 return d->flags;
406}
407
408/*!
409 Sets the \a flags on this material shader if \a on is true;
410 otherwise clears the specified flags.
411*/
412void QSGMaterialShader::setFlag(Flags flags, bool on)
413{
414 Q_D(QSGMaterialShader);
415 if (on)
416 d->flags |= flags;
417 else
418 d->flags &= ~flags;
419}
420
421/*!
422 Sets the \a flags for this material shader.
423 */
424void QSGMaterialShader::setFlags(Flags flags)
425{
426 Q_D(QSGMaterialShader);
427 d->flags = flags;
428}
429
430/*!
431 Returns the number of elements in the combined image sampler variable at \a
432 binding. This value is introspected from the shader code. The variable may
433 be an array, and may have more than one dimension.
434
435 The count reflects the total number of combined image sampler items in the
436 variable. In the following example, the count for \c{srcA} is 1, \c{srcB}
437 is 4, and \c{srcC} is 6.
438
439 \badcode
440 layout (binding = 0) uniform sampler2D srcA;
441 layout (binding = 1) uniform sampler2D srcB[4];
442 layout (binding = 2) uniform sampler2D srcC[2][3];
443 \endcode
444
445 This count is the number of QSGTexture pointers in the texture parameter
446 of \l{QSGMaterialShader::updateSampledImage}.
447
448 \sa QSGMaterialShader::updateSampledImage
449 \since 6.4
450 */
451int QSGMaterialShader::combinedImageSamplerCount(int binding) const
452{
453 Q_D(const QSGMaterialShader);
454
455 if (binding >= 0 && binding < d->MAX_SHADER_RESOURCE_BINDINGS)
456 return d->combinedImageSamplerCount[binding];
457
458 return 0;
459}
460
461/*!
462 This function is called by the scene graph to get the contents of the
463 shader program's uniform buffer updated. The implementation is not expected
464 to perform any real graphics operations, it is merely responsible for
465 copying data to the QByteArray returned from RenderState::uniformData().
466 The scene graph takes care of making that buffer visible in the shaders.
467
468 The current rendering \a state is passed from the scene graph. If the state
469 indicates that any relevant state is dirty, the implementation must update
470 the appropriate region in the buffer data that is accessible via
471 RenderState::uniformData(). When a state, such as, matrix or opacity, is
472 not dirty, there is no need to touch the corresponding region since the
473 data is persistent.
474
475 The return value must be \c true whenever any change was made to the uniform data.
476
477 The subclass specific state, such as the color of a flat color material,
478 should be extracted from \a newMaterial to update the relevant regions in
479 the buffer accordingly.
480
481 \a oldMaterial can be used to minimize buffer changes (which are typically
482 memcpy calls) when updating material states. When \a oldMaterial is null,
483 this shader was just activated.
484 */
485bool QSGMaterialShader::updateUniformData(RenderState &state,
486 QSGMaterial *newMaterial,
487 QSGMaterial *oldMaterial)
488{
489 Q_UNUSED(state);
490 Q_UNUSED(newMaterial);
491 Q_UNUSED(oldMaterial);
492 return false;
493}
494
495/*!
496 This function is called by the scene graph to prepare use of sampled images
497 in the shader, typically in the form of combined image samplers.
498
499 \a binding is the binding number of the sampler. The function is called for
500 each combined image sampler variable in the shader code associated with the
501 QSGMaterialShader.
502
503 \a{texture} is an array of QSGTexture pointers. The number of elements in
504 the array matches the number of elements in the image sampler variable
505 specified in the shader code. This variable may be an array, and may have
506 more than one dimension. The number of elements in the array may be
507 found via \l{QSGMaterialShader::combinedImageSamplerCount}
508
509 When an element in \a{texture} is null, it must be set to a valid
510 QSGTexture pointer before returning. When non-null, it is up to the
511 material to decide if a new \c{QSGTexture *} is stored to it, or if it
512 updates some parameters on the already known QSGTexture. The ownership of
513 the QSGTexture is not transferred.
514
515 The current rendering \a state is passed from the scene graph. Where
516 relevant, it is up to the material to trigger enqueuing texture data
517 uploads via QSGTexture::commitTextureOperations().
518
519 The subclass specific state can be extracted from \a newMaterial.
520
521 \a oldMaterial can be used to minimize changes. When \a oldMaterial is null,
522 this shader was just activated.
523
524 \sa QSGMaterialShader::combinedImageSamplerCount
525 */
526void QSGMaterialShader::updateSampledImage(RenderState &state,
527 int binding,
528 QSGTexture **texture,
529 QSGMaterial *newMaterial,
530 QSGMaterial *oldMaterial)
531{
532 Q_UNUSED(state);
533 Q_UNUSED(binding);
534 Q_UNUSED(texture);
535 Q_UNUSED(newMaterial);
536 Q_UNUSED(oldMaterial);
537}
538
539/*!
540 This function is called by the scene graph to enable the material to
541 provide a custom set of graphics state. The set of states that are
542 customizable by material is limited to blending and related settings.
543
544 \note This function is only called when the UpdatesGraphicsPipelineState
545 flag was enabled via setFlags(). By default it is not set, and so this
546 function is never called.
547
548 The return value must be \c true whenever a change was made to any of the
549 members in \a ps.
550
551 \note The contents of \a ps is not persistent between invocations of this
552 function.
553
554 The current rendering \a state is passed from the scene graph.
555
556 The subclass specific state can be extracted from \a newMaterial. When \a
557 oldMaterial is null, this shader was just activated.
558 */
559bool QSGMaterialShader::updateGraphicsPipelineState(RenderState &state, GraphicsPipelineState *ps,
560 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
561{
562 Q_UNUSED(state);
563 Q_UNUSED(ps);
564 Q_UNUSED(newMaterial);
565 Q_UNUSED(oldMaterial);
566 return false;
567}
568
569/*!
570 \class QSGMaterialShader::RenderState
571
572 \brief Encapsulates the current rendering state during a call to
573 QSGMaterialShader::updateUniformData() and the other \c update type of
574 functions.
575
576 \inmodule QtQuick
577 \since 5.14
578
579 The render state contains a number of accessors that the shader needs to
580 respect in order to conform to the current state of the scene graph.
581 */
582
583/*!
584 \enum QSGMaterialShader::RenderState::DirtyState
585
586 \value DirtyMatrix Used to indicate that the matrix has changed and must be
587 updated.
588
589 \value DirtyOpacity Used to indicate that the opacity has changed and must
590 be updated.
591
592 \value DirtyCachedMaterialData Used to indicate that the cached material
593 state has changed and must be updated.
594
595 \value DirtyAll Used to indicate that everything needs to be updated.
596 */
597
598/*!
599 \fn bool QSGMaterialShader::RenderState::isMatrixDirty() const
600
601 Returns \c true if the dirtyStates() contain the dirty matrix state,
602 otherwise returns \c false.
603 */
604
605/*!
606 \fn bool QSGMaterialShader::RenderState::isOpacityDirty() const
607
608 Returns \c true if the dirtyStates() contains the dirty opacity state,
609 otherwise returns \c false.
610 */
611
612/*!
613 \fn QSGMaterialShader::RenderState::DirtyStates QSGMaterialShader::RenderState::dirtyStates() const
614
615 Returns which rendering states that have changed and needs to be updated
616 for geometry rendered with this material to conform to the current
617 rendering state.
618 */
619
620/*!
621 \class QSGMaterialShader::GraphicsPipelineState
622
623 \brief Describes state changes that the material wants to apply to the
624 currently active graphics pipeline state.
625
626 \inmodule QtQuick
627 \since 5.14
628
629 Unlike QSGMaterialShader, directly issuing state change commands with the
630 underlying graphics API is not possible with QSGMaterialShader. This is
631 mainly because the concept of individually changeable states is considered
632 deprecated and not supported with modern graphics APIs.
633
634 Therefore, it is up to QSGMaterialShader to expose a data structure with
635 the set of supported states, which the material can change in its
636 updatePipelineState() implementation, if there is one. The scenegraph will
637 then internally apply these changes to the active graphics pipeline state,
638 then rolling them back as appropriate.
639
640 When updateGraphicsPipelineState() is called, the struct has all members
641 set to a valid value to reflect the renderer's current state. Not changing
642 any values (or not reimplementing the function) indicates that the material
643 is fine with the defaults (which are dynamic however, depending on
644 QSGMaterial flags, for example).
645 */
646
647/*!
648 \enum QSGMaterialShader::GraphicsPipelineState::BlendFactor
649 \since 5.14
650
651 \value Zero
652 \value One
653 \value SrcColor
654 \value OneMinusSrcColor
655 \value DstColor
656 \value OneMinusDstColor
657 \value SrcAlpha
658 \value OneMinusSrcAlpha
659 \value DstAlpha
660 \value OneMinusDstAlpha
661 \value ConstantColor
662 \value OneMinusConstantColor
663 \value ConstantAlpha
664 \value OneMinusConstantAlpha
665 \value SrcAlphaSaturate
666 \value Src1Color
667 \value OneMinusSrc1Color
668 \value Src1Alpha
669 \value OneMinusSrc1Alpha
670 */
671
672/*!
673 \enum QSGMaterialShader::GraphicsPipelineState::BlendOp
674 \since 6.8
675
676 \value Add
677 \value Subtract
678 \value ReverseSubtract
679 \value Min
680 \value Max
681 */
682
683/*!
684 \enum QSGMaterialShader::GraphicsPipelineState::ColorMaskComponent
685 \since 5.14
686
687 \value R
688 \value G
689 \value B
690 \value A
691 */
692
693/*!
694 \enum QSGMaterialShader::GraphicsPipelineState::CullMode
695 \since 5.14
696
697 \value CullNone
698 \value CullFront
699 \value CullBack
700 */
701
702/*!
703 \enum QSGMaterialShader::GraphicsPipelineState::PolygonMode
704 \since 6.4
705 \brief Specifies the polygon rasterization mode
706
707 Polygon Mode (Triangle Fill Mode in Metal, Fill Mode in D3D) specifies
708 the fill mode used when rasterizing polygons. Polygons may be drawn as
709 solids (Fill), or as a wire mesh (Line).
710
711 \warning OpenGL ES does not support the \c{Line} polygon mode. OpenGL ES
712 will rasterize all polygons as filled no matter what polygon mode is set.
713 Using \c{Line} will make your application non-portable.
714
715 \value Fill The interior of the polygon is filled (default)
716 \value Line Boundary edges of the polygon are drawn as line segments.
717 */
718
719/*!
720 \variable QSGMaterialShader::GraphicsPipelineState::blendEnable
721 \since 5.14
722 \brief Enables blending.
723
724 \note Changing this flag should be done with care, and is best avoided.
725 Rather, materials should always use the QSGMaterial::Blend flag to indicate
726 that they wish to use blending. Changing this value from false to true for
727 a material that did not declare QSGMaterial::Blend can lead to unexpected
728 visual results.
729 */
730
731/*!
732 \variable QSGMaterialShader::GraphicsPipelineState::srcColor
733 \since 5.14
734 \brief Source blending factor, either RGB or RGBA depending on separateBlendFactors.
735 */
736
737/*!
738 \variable QSGMaterialShader::GraphicsPipelineState::dstColor
739 \since 5.14
740 \brief Destination blending factor, either RGB or RGBA depending on separateBlendFactors.
741 */
742
743/*!
744 \variable QSGMaterialShader::GraphicsPipelineState::colorWrite
745 \since 5.14
746 \brief Color write mask.
747 */
748
749/*!
750 \variable QSGMaterialShader::GraphicsPipelineState::blendConstant
751 \since 5.14
752 \brief Blend constant applicable when a blending factor is set to use a constant value.
753 */
754
755/*!
756 \variable QSGMaterialShader::GraphicsPipelineState::cullMode
757 \since 5.14
758 \brief Cull mode.
759 */
760
761/*!
762 \variable QSGMaterialShader::GraphicsPipelineState::polygonMode
763 \since 6.4
764 \brief Polygon rasterization mode.
765 */
766
767/*!
768 \variable QSGMaterialShader::GraphicsPipelineState::separateBlendFactors
769 \since 6.5
770 \brief Indicates that alpha blending factors are specified separately.
771
772 False by default, meaning both RGB and alpha blending factors are defined
773 by srcColor and dstColor. When set to true, the alpha blending factors are
774 taken from srcAlpha and dstAlpha instead, and srcColor and dstColor applies
775 only to RGB.
776 */
777
778/*!
779 \variable QSGMaterialShader::GraphicsPipelineState::srcAlpha
780 \since 6.5
781 \brief Source alpha blending factor.
782
783 Applies only when separateBlendFactors is set to true.
784 */
785
786/*!
787 \variable QSGMaterialShader::GraphicsPipelineState::dstAlpha
788 \since 6.5
789 \brief Destination alpha blending factor.
790
791 Applies only when separateBlendFactors is set to true.
792 */
793
794/*!
795 \variable QSGMaterialShader::GraphicsPipelineState::opColor
796 \since 6.8
797 \brief RGB blending operation.
798 */
799
800/*!
801 \variable QSGMaterialShader::GraphicsPipelineState::opAlpha
802 \since 6.8
803 \brief Alpha blending operation.
804 */
805
806/*!
807 Returns the accumulated opacity to be used for rendering.
808 */
809float QSGMaterialShader::RenderState::opacity() const
810{
811 Q_ASSERT(m_data);
812 return float(static_cast<const QSGRenderer *>(m_data)->currentOpacity());
813}
814
815/*!
816 Returns the modelview determinant to be used for rendering.
817 */
818float QSGMaterialShader::RenderState::determinant() const
819{
820 Q_ASSERT(m_data);
821 return float(static_cast<const QSGRenderer *>(m_data)->determinant());
822}
823
824/*!
825 Returns the matrix combined of modelview matrix and project matrix.
826 */
827QMatrix4x4 QSGMaterialShader::RenderState::combinedMatrix() const
828{
829 Q_ASSERT(m_data);
830 return static_cast<const QSGRenderer *>(m_data)->currentCombinedMatrix(0);
831}
832
833/*!
834 \internal
835 */
836QMatrix4x4 QSGMaterialShader::RenderState::combinedMatrix(qsizetype index) const
837{
838 Q_ASSERT(m_data);
839 return static_cast<const QSGRenderer *>(m_data)->currentCombinedMatrix(index);
840}
841
842/*!
843 Returns the ratio between physical pixels and device-independent pixels
844 to be used for rendering.
845*/
846float QSGMaterialShader::RenderState::devicePixelRatio() const
847{
848 Q_ASSERT(m_data);
849 return float(static_cast<const QSGRenderer *>(m_data)->devicePixelRatio());
850}
851
852/*!
853 Returns the model view matrix.
854
855 If the material has the RequiresFullMatrix flag set, this is guaranteed to
856 be the complete transform matrix calculated from the scenegraph.
857
858 However, if this flag is not set, the renderer may choose to alter this
859 matrix. For example, it may pre-transform vertices on the CPU and set this
860 matrix to identity.
861
862 In a situation such as the above, it is still possible to retrieve the
863 actual matrix determinant by setting the RequiresDeterminant flag in the
864 material and calling the determinant() accessor.
865 */
866QMatrix4x4 QSGMaterialShader::RenderState::modelViewMatrix() const
867{
868 Q_ASSERT(m_data);
869 return static_cast<const QSGRenderer *>(m_data)->currentModelViewMatrix();
870}
871
872/*!
873 Returns the projection matrix.
874 */
875QMatrix4x4 QSGMaterialShader::RenderState::projectionMatrix() const
876{
877 Q_ASSERT(m_data);
878 return static_cast<const QSGRenderer *>(m_data)->currentProjectionMatrix(0);
879}
880
881/*!
882 \internal
883 */
884QMatrix4x4 QSGMaterialShader::RenderState::projectionMatrix(qsizetype index) const
885{
886 Q_ASSERT(m_data);
887 return static_cast<const QSGRenderer *>(m_data)->currentProjectionMatrix(index);
888}
889
890/*!
891 \internal
892 */
893qsizetype QSGMaterialShader::RenderState::projectionMatrixCount() const
894{
895 Q_ASSERT(m_data);
896 return static_cast<const QSGRenderer *>(m_data)->projectionMatrixCount();
897}
898
899/*!
900 Returns the viewport rect of the surface being rendered to.
901 */
902QRect QSGMaterialShader::RenderState::viewportRect() const
903{
904 Q_ASSERT(m_data);
905 return static_cast<const QSGRenderer *>(m_data)->viewportRect();
906}
907
908/*!
909 Returns the device rect of the surface being rendered to
910 */
911QRect QSGMaterialShader::RenderState::deviceRect() const
912{
913 Q_ASSERT(m_data);
914 return static_cast<const QSGRenderer *>(m_data)->deviceRect();
915}
916
917/*!
918 Returns a pointer to the data for the uniform (constant) buffer in the
919 shader. Uniform data must only be updated from
920 QSGMaterialShader::updateUniformData(). The return value is null in the
921 other reimplementable functions, such as,
922 QSGMaterialShader::updateSampledImage().
923
924 \note It is strongly recommended to declare the uniform block with \c
925 std140 in the shader, and to carefully study the standard uniform block
926 layout as described in section 7.6.2.2 of the OpenGL specification. It is
927 up to the QSGMaterialShader implementation to ensure data gets placed
928 at the right location in this QByteArray, taking alignment requirements
929 into account. Shader code translated to other shading languages is expected
930 to use the same offsets for block members, even when the target language
931 uses different packing rules by default.
932
933 \note Avoid copying from C++ POD types, such as, structs, in order to
934 update multiple members at once, unless it has been verified that the
935 layouts of the C++ struct and the GLSL uniform block match.
936 */
937QByteArray *QSGMaterialShader::RenderState::uniformData()
938{
939 Q_ASSERT(m_data);
940 return static_cast<const QSGRenderer *>(m_data)->currentUniformData();
941}
942
943/*!
944 Returns a resource update batch to which upload and copy operatoins can be
945 queued. This is typically used by
946 QSGMaterialShader::updateSampledImage() to enqueue texture image
947 content updates.
948 */
949QRhiResourceUpdateBatch *QSGMaterialShader::RenderState::resourceUpdateBatch()
950{
951 Q_ASSERT(m_data);
952 return static_cast<const QSGRenderer *>(m_data)->currentResourceUpdateBatch();
953}
954
955/*!
956 Returns the current QRhi.
957 */
958QRhi *QSGMaterialShader::RenderState::rhi()
959{
960 Q_ASSERT(m_data);
961 return static_cast<const QSGRenderer *>(m_data)->currentRhi();
962}
963
964QT_END_NAMESPACE
static QShader::Stage toShaderStage(QSGMaterialShader::Stage stage)
static QRhiShaderResourceBinding::StageFlags toSrbStage(QShader::Stage stage)