summaryrefslogtreecommitdiffstats
path: root/src/quick/items/qquickshadereffect.cpp
blob: caabd541b66f3c8fe053bd493227ecde01e53a93 (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
/****************************************************************************
**
** Copyright (C) 2016 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:LGPL$
** 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.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <private/qquickshadereffect_p.h>
#include <private/qsgcontextplugin_p.h>
#include <private/qquickitem_p.h>
#include <private/qsgrhisupport_p.h>

#include <private/qquickwindow_p.h>
#include <private/qquickitem_p.h>
#include "qquickshadereffectmesh_p.h"

QT_BEGIN_NAMESPACE

/*!
    \qmltype ShaderEffect
    \instantiates QQuickShaderEffect
    \inqmlmodule QtQuick
    \inherits Item
    \ingroup qtquick-effects
    \brief Applies custom shaders to a rectangle.

    The ShaderEffect type applies a custom \l{vertexShader}{vertex} and
    \l{fragmentShader}{fragment (pixel)} shader to a rectangle. It allows
    adding effects such as drop shadow, blur, colorize and page curl into the
    QML scene.

    \note Depending on the Qt Quick scenegraph backend in use, the ShaderEffect
    type may not be supported. For example, with the \c software backend
    effects will not be rendered at all.

    \section1 Shaders

    In Qt 5, effects were provided in form of GLSL (OpenGL Shading Language)
    source code, often embedded as strings into QML. Starting with Qt 5.8,
    referring to files, either local ones or in the Qt resource system, became
    possible as well.

    In Qt 6, Qt Quick has support for graphics APIs, such as Vulkan, Metal, and
    Direct3D 11 as well. Therefore, working with GLSL source strings is no
    longer feasible. Rather, the new shader pipeline is based on compiling
    Vulkan-compatible GLSL code into \l{https://www.khronos.org/spir/}{SPIR-V},
    followed by gathering reflection information and translating into other
    shading languages, such as HLSL, the Metal Shading Language, and various
    GLSL versions. The resulting assets are packed together into a single
    package, typically stored in files with an extension of \c{.qsb}. This
    process is done offline or at application build time at latest. At run
    time, the scene graph and the underlying graphics abstraction consumes
    these \c{.qsb} files. Therefore, ShaderEffect expects file (local or qrc)
    references in Qt 6 in place of inline shader code.

    The \l vertexShader and \l fragmentShader properties are URLs in Qt 6, and
    work very similarly to \l{Image::source}{Image.source}, for example. Only
    the \c file and \c qrc schemes are supported with ShaderEffect, however. It
    is also possible to omit the \c file scheme, allowing to specify a relative
    path in a convenient way. Such a path is resolved relative to the
    component's (the \c{.qml} file's) location.

    \section1 Shader Inputs and Resources

    There are two types of input to the \l vertexShader: uniforms and vertex
    inputs.

    The following inputs are predefined:

    \list
    \li vec4 qt_Vertex with location 0 - vertex position, the top-left vertex has
       position (0, 0), the bottom-right (\l{Item::width}{width},
       \l{Item::height}{height}).
    \li vec2 qt_MultiTexCoord0 with location 1 - texture coordinate, the top-left
       coordinate is (0, 0), the bottom-right (1, 1). If \l supportsAtlasTextures
       is true, coordinates will be based on position in the atlas instead.
    \endlist

    \note It is only the vertex input location that matters in practice. The
    names are freely changeable, while the location must always be \c 0 for
    vertex position, \c 1 for texture coordinates. However, be aware that this
    applies to vertex inputs only, and is not necessarily true for output
    variables from the vertex shader that are then used as inputs in the
    fragment shader (typically, the interpolated texture coordinates).

    The following uniforms are predefined:

    \list
    \li mat4 qt_Matrix - combined transformation
       matrix, the product of the matrices from the root item to this
       ShaderEffect, and an orthogonal projection.
    \li float qt_Opacity - combined opacity, the product of the
       opacities from the root item to this ShaderEffect.
    \endlist

    \note Vulkan-style GLSL has no separate uniform variables. Instead, shaders
    must always use a uniform block with a binding point of \c 0.

    \note The uniform block layout qualifier must always be \c std140.

    \note Unlike vertex inputs, the predefined names (qt_Matrix, qt_Opacity)
    must not be changed.

    In addition, any property that can be mapped to a GLSL type can be made
    available to the shaders. The following list shows how properties are
    mapped:

    \list
    \li bool, int, qreal -> bool, int, float - If the type in the shader is not
       the same as in QML, the value is converted automatically.
    \li QColor -> vec4 - When colors are passed to the shader, they are first
       premultiplied. Thus Qt.rgba(0.2, 0.6, 1.0, 0.5) becomes
       vec4(0.1, 0.3, 0.5, 0.5) in the shader, for example.
    \li QRect, QRectF -> vec4 - Qt.rect(x, y, w, h) becomes vec4(x, y, w, h) in
       the shader.
    \li QPoint, QPointF, QSize, QSizeF -> vec2
    \li QVector3D -> vec3
    \li QVector4D -> vec4
    \li QTransform -> mat3
    \li QMatrix4x4 -> mat4
    \li QQuaternion -> vec4, scalar value is \c w.
    \li \l Image -> sampler2D - Origin is in the top-left corner, and the
        color values are premultiplied. The texture is provided as is,
        excluding the Image item's fillMode. To include fillMode, use a
        ShaderEffectSource or Image::layer::enabled.
    \li \l ShaderEffectSource -> sampler2D - Origin is in the top-left
        corner, and the color values are premultiplied.
    \endlist

    Samplers are still declared as separate uniform variables in the shader
    code. The shaders are free to choose any binding point for these, except
    for \c 0 because that is reserved for the uniform block.

    Some shading languages and APIs have a concept of separate image and
    sampler objects. Qt Quick always works with combined image sampler objects
    in shaders, as supported by SPIR-V. Therefore shaders supplied for
    ShaderEffect should always use \c{layout(binding = 1) uniform sampler2D
    tex;} style sampler declarations. The underlying abstraction layer and the
    shader pipeline takes care of making this work for all the supported APIs
    and shading languages, transparently to the applications.

    The QML scene graph back-end may choose to allocate textures in texture
    atlases. If a texture allocated in an atlas is passed to a ShaderEffect,
    it is by default copied from the texture atlas into a stand-alone texture
    so that the texture coordinates span from 0 to 1, and you get the expected
    wrap modes. However, this will increase the memory usage. To avoid the
    texture copy, set \l supportsAtlasTextures for simple shaders using
    qt_MultiTexCoord0, or for each "uniform sampler2D <name>" declare a
    "uniform vec4 qt_SubRect_<name>" which will be assigned the texture's
    normalized source rectangle. For stand-alone textures, the source rectangle
    is [0, 1]x[0, 1]. For textures in an atlas, the source rectangle corresponds
    to the part of the texture atlas where the texture is stored.
    The correct way to calculate the texture coordinate for a texture called
    "source" within a texture atlas is
    "qt_SubRect_source.xy + qt_SubRect_source.zw * qt_MultiTexCoord0".

    The output from the \l fragmentShader should be premultiplied. If
    \l blending is enabled, source-over blending is used. However, additive
    blending can be achieved by outputting zero in the alpha channel.

    \table 70%
    \row
    \li \image declarative-shadereffectitem.png
    \li \qml
        import QtQuick 2.0

        Rectangle {
            width: 200; height: 100
            Row {
                Image { id: img;
                        sourceSize { width: 100; height: 100 } source: "qt-logo.png" }
                ShaderEffect {
                    width: 100; height: 100
                    property variant src: img
                    vertexShader: "myeffect.vert.qsb"
                    fragmentShader: "myeffect.frag.qsb"
                }
            }
        }
        \endqml
    \endtable

    The example assumes \c{myeffect.vert} and \c{myeffect.frag} contain
    Vulkan-style GLSL code, processed by the \c qsb tool in order to generate
    the \c{.qsb} files.

    \badcode
        #version 440
        layout(location = 0) in vec4 qt_Vertex;
        layout(location = 1) in vec2 qt_MultiTexCoord0;
        layout(location = 0) out vec2 coord;
        layout(std140, binding = 0) uniform buf {
            mat4 qt_Matrix;
            float qt_Opacity;
        };
        void main() {
            coord = qt_MultiTexCoord0;
            gl_Position = qt_Matrix * qt_Vertex;
        }
    \endcode

    \badcode
        #version 440
        layout(location = 0) in vec2 coord;
        layout(location = 0) out vec4 fragColor;
        layout(std140, binding = 0) uniform buf {
            mat4 qt_Matrix;
            float qt_Opacity;
        };
        layout(binding = 1) uniform sampler2D src;
        void main() {
            vec4 tex = texture(src, coord);
            fragColor = vec4(vec3(dot(tex.rgb, vec3(0.344, 0.5, 0.156))), tex.a) * qt_Opacity;
        }
    \endcode

    \note Scene Graph textures have origin in the top-left corner rather than
    bottom-left which is common in OpenGL.

    \section1 Having One Shader Only

    Specifying both \l vertexShader and \l fragmentShader is not mandatory.
    Many ShaderEffect implementations will want to provide a fragment shader
    only in practice, while relying on the default, built-in vertex shader.

    The default vertex shader passes the texture coordinate along to the
    fragment shader as \c{vec2 qt_TexCoord0} at location \c 0.

    The default fragment shader expects the texture coordinate to be passed
    from the vertex shader as \c{vec2 qt_TexCoord0} at location \c 0, and it
    samples from a sampler2D named \c source at binding point \c 1.

    \warning When only one of the shaders is specified, the writer of the
    shader must be aware of the uniform block layout expected by the default
    shaders: qt_Matrix must always be at offset 0, followed by qt_Opacity at
    offset 64. Any custom uniforms must be placed after these two. This is
    mandatory even when the application-provided shader does not use the matrix
    or the opacity, because at run time there is one single uniform buffer that
    is exposed to both the vertex and fragment shader.

    \warning Unlike with vertex inputs, passing data between the vertex and
    fragment shader may, depending on the underlying graphics API, require the
    same names to be used, a matching location is not always sufficient. Most
    prominently, when specifying a fragment shader while relying on the default,
    built-in vertex shader, the texture coordinates are passed on as \c
    qt_TexCoord0 at location \c 0, and therefore it is strongly advised that the
    fragment shader declares the input with the same name
    (qt_TexCoord0). Failing to do so may lead to issues on some platforms, for
    example when running with a non-core profile OpenGL context where the
    underlying GLSL shader source code has no location qualifiers and matching
    is based on the variable names during to shader linking process.

    \section1 ShaderEffect and Item Layers

    The ShaderEffect type can be combined with \l {Item Layers} {layered items}.

    \table
    \row
      \li \b {Layer with effect disabled} \inlineimage qml-shadereffect-nolayereffect.png
      \li \b {Layer with effect enabled} \inlineimage qml-shadereffect-layereffect.png
    \row
      \li \qml
          Item {
              id: layerRoot
              layer.enabled: true
              layer.effect: ShaderEffect {
                 fragmentShader: "effect.frag.qsb"
              }
          }
          \endqml

          \badcode
          #version 440
          layout(location = 0) in vec2 qt_TexCoord0;
          layout(location = 0) out vec4 fragColor;
          layout(std140, binding = 0) uniform buf {
              mat4 qt_Matrix;
              float qt_Opacity;
          };
          layout(binding = 1) uniform sampler2D source;
          void main() {
              vec4 p = texture(source, qt_TexCoord0);
              float g = dot(p.xyz, vec3(0.344, 0.5, 0.156));
              fragColor = vec4(g, g, g, p.a) * qt_Opacity;
          }
          \endcode
    \endtable

    It is also possible to combine multiple layered items:

    \table
    \row
      \li \inlineimage qml-shadereffect-opacitymask.png
    \row
      \li \qml
          Rectangle {
              id: gradientRect;
              width: 10
              height: 10
              gradient: Gradient {
                  GradientStop { position: 0; color: "white" }
                  GradientStop { position: 1; color: "steelblue" }
              }
              visible: false; // should not be visible on screen.
              layer.enabled: true;
              layer.smooth: true
           }
           Text {
              id: textItem
              font.pixelSize: 48
              text: "Gradient Text"
              anchors.centerIn: parent
              layer.enabled: true
              // This item should be used as the 'mask'
              layer.samplerName: "maskSource"
              layer.effect: ShaderEffect {
                  property var colorSource: gradientRect;
                  fragmentShader: "mask.frag.qsb"
              }
          }
          \endqml

          \badcode
          #version 440
          layout(location = 0) in vec2 qt_TexCoord0;
          layout(location = 0) out vec4 fragColor;
          layout(std140, binding = 0) uniform buf {
              mat4 qt_Matrix;
              float qt_Opacity;
          };
          layout(binding = 1) uniform sampler2D colorSource;
          layout(binding = 2) uniform sampler2D maskSource;
          void main() {
              fragColor = texture(colorSource, qt_TexCoord0)
                              * texture(maskSource, qt_TexCoord0).a
                              * qt_Opacity;
          }
          \endcode
    \endtable

    \section1 Other Notes

    By default, the ShaderEffect consists of four vertices, one for each
    corner. For non-linear vertex transformations, like page curl, you can
    specify a fine grid of vertices by specifying a \l mesh resolution.

    \section1 Migrating From Qt 5

    For Qt 5 applications with ShaderEffect items the migration to Qt 6 involves:
    \list
    \li Moving the shader code to separate \c{.vert} and \c{.frag} files,
    \li updating the shaders to Vulkan-compatible GLSL,
    \li running the \c qsb tool on them,
    \li including the resulting \c{.qsb} files in the executable with the Qt resource system,
    \li and referencing the file in the \l vertexShader and \l fragmentShader properties.
    \endlist

    As described in the \l{Qt Shader Tools} module some of these steps can be
    automated by letting CMake invoke the \c qsb tool at build time. See \l{Qt
    Shader Tools Build System Integration} for more information and examples.

    When it comes to updating the shader code, below is an overview of the
    commonly required changes.

    \table
    \header
    \li Vertex shader in Qt 5
    \li Vertex shader in Qt 6
    \row
    \li \badcode
        attribute highp vec4 qt_Vertex;
        attribute highp vec2 qt_MultiTexCoord0;
        varying highp vec2 coord;
        uniform highp mat4 qt_Matrix;
        void main() {
            coord = qt_MultiTexCoord0;
            gl_Position = qt_Matrix * qt_Vertex;
        }
    \endcode
    \li \badcode
        #version 440
        layout(location = 0) in vec4 qt_Vertex;
        layout(location = 1) in vec2 qt_MultiTexCoord0;
        layout(location = 0) out vec2 coord;
        layout(std140, binding = 0) uniform buf {
            mat4 qt_Matrix;
            float qt_Opacity;
        };
        void main() {
            coord = qt_MultiTexCoord0;
            gl_Position = qt_Matrix * qt_Vertex;
        }
    \endcode
    \endtable

    The conversion process mostly involves updating the code to be compatible
    with
    \l{https://github.com/KhronosGroup/GLSL/blob/master/extensions/khr/GL_KHR_vulkan_glsl.txt}{GL_KHR_vulkan_glsl}.
    It is worth noting that Qt Quick uses a subset of the features provided by
    GLSL and Vulkan, and therefore the conversion process for typical
    ShaderEffect shaders is usually straightforward.

    \list

    \li The \c version directive should state \c 440 or \c 450, although
    specifying other GLSL version may work too, because the
    \l{https://github.com/KhronosGroup/GLSL/blob/master/extensions/khr/GL_KHR_vulkan_glsl.txt}{GL_KHR_vulkan_glsl}
    extension is written for GLSL 140 and higher.

    \li Inputs and outputs must use the modern GLSL \c in and \c out keywords.
    In addition, specifying a location is required. The input and output
    location namespaces are separate, and therefore assigning locations
    starting from 0 for both is safe.

    \li When it comes to vertex shader inputs, the only possibilities with
    ShaderEffect are location \c 0 for vertex position (traditionally named \c
    qt_Vertex) and location \c 1 for texture coordinates (traditionally named
    \c qt_MultiTexCoord0).

    \li The vertex shader outputs and fragment shader inputs are up to the
    shader code to define. The fragment shader must have a \c vec4 output at
    location 0 (typically called \c fragColor). For maximum portability, vertex
    outputs and fragment inputs should use both the same location number and the
    same name. When specifying only a fragment shader, the texture coordinates
    are passed in from the built-in vertex shader as \c{vec2 qt_TexCoord0} at
    location \c 0, as shown in the example snippets above.

    \li Uniform variables outside a uniform block are not legal. Rather,
    uniform data must be declared in a uniform block with binding point \c 0.

    \li The uniform block is expected to use the std140 qualifier.

    \li At run time, the vertex and fragment shader will get the same uniform
    buffer bound to binding point 0. Therefore, as a general rule, the uniform
    block declarations must be identical between the shaders. This also
    includes members that are not used in one of the shaders. The member names
    must match, because with some graphics APIs the uniform block is converted
    to a traditional struct uniform, transparently to the application.

    \li When providing one of the shaders only, watch out for the fact that the
    built-in shaders expect \c qt_Matrix and \c qt_Opacity at the top of the
    uniform block. (more precisely, at offset 0 and 64, respectively) As a
    general rule, always include these as the first and second members in the
    block.

    \li In the example the uniform block specifies the block name \c buf. This
    name can be changed freely, but must match between the shaders. Using an
    instance name, such as \c{layout(...) uniform buf { ... } instance_name;}
    is optional. When specified, all accesses to the members must be qualified
    with instance_name.

    \endlist

    \table
    \header
    \li Fragment shader in Qt 5
    \li Fragment shader in Qt 6
    \row
    \li \badcode
        varying highp vec2 coord;
        uniform lowp float qt_Opacity;
        uniform sampler2D src;
        void main() {
            lowp vec4 tex = texture2D(src, coord);
            gl_FragColor = vec4(vec3(dot(tex.rgb,
                                vec3(0.344, 0.5, 0.156))),
                                     tex.a) * qt_Opacity;
        }
    \endcode
    \li \badcode
        #version 440
        layout(location = 0) in vec2 coord;
        layout(location = 0) out vec4 fragColor;
        layout(std140, binding = 0) uniform buf {
            mat4 qt_Matrix;
            float qt_Opacity;
        };
        layout(binding = 1) uniform sampler2D src;
        void main() {
            vec4 tex = texture(src, coord);
            fragColor = vec4(vec3(dot(tex.rgb,
                             vec3(0.344, 0.5, 0.156))),
                                  tex.a) * qt_Opacity;
        }
    \endcode
    \endtable

    \list

    \li Precision qualifiers (\c lowp, \c mediump, \c highp) are not currently used.

    \li Calling built-in GLSL functions must follow the modern GLSL names, most
    prominently, \c{texture()} instead of \c{texture2D()}.

    \li Samplers must use binding points starting from 1.

    \endlist

    \sa {Item Layers}, {QSB Manual}, {Qt Shader Tools Build System Integration}
*/


namespace QtPrivate {
class EffectSlotMapper: public QtPrivate::QSlotObjectBase
{
public:
    typedef std::function<void()> PropChangedFunc;

    explicit EffectSlotMapper(PropChangedFunc func)
    : QSlotObjectBase(&impl), _signalIndex(-1), func(func)
    { ref(); }

    void setSignalIndex(int idx) { _signalIndex = idx; }
    int signalIndex() const { return _signalIndex; }

private:
    int _signalIndex;
    PropChangedFunc func;

    static void impl(int which, QSlotObjectBase *this_, QObject *, void **a, bool *ret)
    {
    auto thiz = static_cast<EffectSlotMapper*>(this_);
    switch (which) {
    case Destroy:
        delete thiz;
        break;
    case Call:
        thiz->func();
        break;
    case Compare:
        *ret = thiz == reinterpret_cast<EffectSlotMapper *>(a[0]);
        break;
    case NumOperations: ;
    }
    }
};
} // namespace QtPrivate

class QQuickShaderEffectImpl : public QObject
{
    Q_OBJECT

public:
    QQuickShaderEffectImpl(QQuickShaderEffect *item);
    ~QQuickShaderEffectImpl();

    QUrl fragmentShader() const { return m_fragShader; }
    void setFragmentShader(const QUrl &fileUrl);

    QUrl vertexShader() const { return m_vertShader; }
    void setVertexShader(const QUrl &fileUrl);

    bool blending() const { return m_blending; }
    void setBlending(bool enable);

    QVariant mesh() const;
    void setMesh(const QVariant &mesh);

    QQuickShaderEffect::CullMode cullMode() const { return m_cullMode; }
    void setCullMode(QQuickShaderEffect::CullMode face);

    QString log() const;
    QQuickShaderEffect::Status status() const;

    bool supportsAtlasTextures() const { return m_supportsAtlasTextures; }
    void setSupportsAtlasTextures(bool supports);

    QString parseLog();

    void handleEvent(QEvent *);
    void handleGeometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
    QSGNode *handleUpdatePaintNode(QSGNode *, QQuickItem::UpdatePaintNodeData *);
    void handleComponentComplete();
    void handleItemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value);
    void maybeUpdateShaders();
    bool updateUniformValue(const QByteArray &name, const QVariant &value,
                            QSGShaderEffectNode *node);

private slots:
    void propertyChanged(int mappedId);
    void sourceDestroyed(QObject *object);
    void markGeometryDirtyAndUpdate();
    void markGeometryDirtyAndUpdateIfSupportsAtlas();
    void shaderCodePrepared(bool ok, QSGGuiThreadShaderEffectManager::ShaderInfo::Type typeHint,
                            const QUrl &loadUrl, QSGGuiThreadShaderEffectManager::ShaderInfo *result);

private:
    QSGGuiThreadShaderEffectManager *shaderEffectManager() const;

    enum Shader {
        Vertex,
        Fragment,

        NShader
    };
    bool updateShader(Shader shaderType, const QUrl &fileUrl);
    void updateShaderVars(Shader shaderType);
    void disconnectSignals(Shader shaderType);
    void clearMappers(Shader shaderType);
    bool sourceIsUnique(QQuickItem *source, Shader typeToSkip, int indexToSkip) const;
    std::optional<int> findMappedShaderVariableId(const QByteArray &name) const;

    QQuickShaderEffect *m_item;
    const QMetaObject *m_itemMetaObject = nullptr;
    QSize m_meshResolution;
    QQuickShaderEffectMesh *m_mesh;
    QQuickGridMesh m_defaultMesh;
    QQuickShaderEffect::CullMode m_cullMode;
    bool m_blending;
    bool m_supportsAtlasTextures;
    mutable QSGGuiThreadShaderEffectManager *m_mgr;
    QUrl m_fragShader;
    bool m_fragNeedsUpdate;
    QUrl m_vertShader;
    bool m_vertNeedsUpdate;

    QSGShaderEffectNode::ShaderData m_shaders[NShader];
    QSGShaderEffectNode::DirtyShaderFlags m_dirty;
    QSet<int> m_dirtyConstants[NShader];
    QSet<int> m_dirtyTextures[NShader];
    QSGGuiThreadShaderEffectManager::ShaderInfo *m_inProgress[NShader];

    QVector<QtPrivate::EffectSlotMapper*> m_mappers[NShader];
};


class QQuickShaderEffectPrivate : public QQuickItemPrivate
{
    Q_DECLARE_PUBLIC(QQuickShaderEffect)

public:
    void updatePolish() override;
};

QQuickShaderEffect::QQuickShaderEffect(QQuickItem *parent)
    : QQuickItem(*new QQuickShaderEffectPrivate, parent),
      m_impl(nullptr)
{
    setFlag(QQuickItem::ItemHasContents);

    m_impl = new QQuickShaderEffectImpl(this);
}

QQuickShaderEffect::~QQuickShaderEffect()
{
    // Delete the implementations now, while they still have have
    // valid references back to us.
    auto *impl = m_impl;
    m_impl = nullptr;
    delete impl;
}

/*!
    \qmlproperty url QtQuick::ShaderEffect::fragmentShader

    This property contains a reference to a file with the preprocessed fragment
    shader package, typically with an extension of \c{.qsb}. The value is
    treated as a \l{QUrl}{URL}, similarly to other QML types, such as Image. It
    must either be a local file or use the qrc scheme to access files embedded
    via the Qt resource system. The URL may be absolute, or relative to the URL
    of the component.

    \sa vertexShader
*/

QUrl QQuickShaderEffect::fragmentShader() const
{
    return m_impl->fragmentShader();
}

void QQuickShaderEffect::setFragmentShader(const QUrl &fileUrl)
{
    m_impl->setFragmentShader(fileUrl);
}

/*!
    \qmlproperty url QtQuick::ShaderEffect::vertexShader

    This property contains a reference to a file with the preprocessed vertex
    shader package, typically with an extension of \c{.qsb}. The value is
    treated as a \l{QUrl}{URL}, similarly to other QML types, such as Image. It
    must either be a local file or use the qrc scheme to access files embedded
    via the Qt resource system. The URL may be absolute, or relative to the URL
    of the component.

    \sa fragmentShader
*/

QUrl QQuickShaderEffect::vertexShader() const
{
    return m_impl->vertexShader();
}

void QQuickShaderEffect::setVertexShader(const QUrl &fileUrl)
{
    m_impl->setVertexShader(fileUrl);
}

/*!
    \qmlproperty bool QtQuick::ShaderEffect::blending

    If this property is true, the output from the \l fragmentShader is blended
    with the background using source-over blend mode. If false, the background
    is disregarded. Blending decreases the performance, so you should set this
    property to false when blending is not needed. The default value is true.
*/

bool QQuickShaderEffect::blending() const
{
    return m_impl->blending();
}

void QQuickShaderEffect::setBlending(bool enable)
{
    m_impl->setBlending(enable);
}

/*!
    \qmlproperty variant QtQuick::ShaderEffect::mesh

    This property defines the mesh used to draw the ShaderEffect. It can hold
    any \l GridMesh object.
    If a size value is assigned to this property, the ShaderEffect implicitly
    uses a \l GridMesh with the value as
    \l{GridMesh::resolution}{mesh resolution}. By default, this property is
    the size 1x1.

    \sa GridMesh
*/

QVariant QQuickShaderEffect::mesh() const
{
    return m_impl->mesh();
}

void QQuickShaderEffect::setMesh(const QVariant &mesh)
{
    m_impl->setMesh(mesh);
}

/*!
    \qmlproperty enumeration QtQuick::ShaderEffect::cullMode

    This property defines which sides of the item should be visible.

    \list
    \li ShaderEffect.NoCulling - Both sides are visible
    \li ShaderEffect.BackFaceCulling - only front side is visible
    \li ShaderEffect.FrontFaceCulling - only back side is visible
    \endlist

    The default is NoCulling.
*/

QQuickShaderEffect::CullMode QQuickShaderEffect::cullMode() const
{
    return m_impl->cullMode();
}

void QQuickShaderEffect::setCullMode(CullMode face)
{
    return m_impl->setCullMode(face);
}

/*!
    \qmlproperty bool QtQuick::ShaderEffect::supportsAtlasTextures

    Set this property true to confirm that your shader code doesn't rely on
    qt_MultiTexCoord0 ranging from (0,0) to (1,1) relative to the mesh.
    In this case the range of qt_MultiTexCoord0 will rather be based on the position
    of the texture within the atlas. This property currently has no effect if there
    is less, or more, than one sampler uniform used as input to your shader.

    This differs from providing qt_SubRect_<name> uniforms in that the latter allows
    drawing one or more textures from the atlas in a single ShaderEffect item, while
    supportsAtlasTextures allows multiple instances of a ShaderEffect component using
    a different source image from the atlas to be batched in a single draw.
    Both prevent a texture from being copied out of the atlas when referenced by a ShaderEffect.

    The default value is false.

    \since 5.4
    \since QtQuick 2.4
*/

bool QQuickShaderEffect::supportsAtlasTextures() const
{
    return m_impl->supportsAtlasTextures();
}

void QQuickShaderEffect::setSupportsAtlasTextures(bool supports)
{
    m_impl->setSupportsAtlasTextures(supports);
}

/*!
    \qmlproperty enumeration QtQuick::ShaderEffect::status

    This property tells the current status of the shaders.

    \list
    \li ShaderEffect.Compiled - the shader program was successfully compiled and linked.
    \li ShaderEffect.Uncompiled - the shader program has not yet been compiled.
    \li ShaderEffect.Error - the shader program failed to compile or link.
    \endlist

    When setting the fragment or vertex shader source code, the status will
    become Uncompiled. The first time the ShaderEffect is rendered with new
    shader source code, the shaders are compiled and linked, and the status is
    updated to Compiled or Error.

    When runtime compilation is not in use and the shader properties refer to
    files with bytecode, the status is always Compiled. The contents of the
    shader is not examined (apart from basic reflection to discover vertex
    input elements and constant buffer data) until later in the rendering
    pipeline so potential errors (like layout or root signature mismatches)
    will only be detected at a later point.

    \sa log
*/

/*!
    \qmlproperty string QtQuick::ShaderEffect::log

    This property holds a log of warnings and errors from the latest attempt at
    compiling the shaders. It is updated at the same time \l status is set to
    Compiled or Error.

    \note In Qt 6, the shader pipeline promotes compiling and translating the
    Vulkan-style GLSL shaders offline, or at build time at latest. This does
    not necessarily mean there is no shader compilation happening at run time,
    but even if there is, ShaderEffect is not involved in that, and syntax and
    similar errors should not occur anymore at that stage. Therefore the value
    of this property is typically empty.

    \sa status
*/

QString QQuickShaderEffect::log() const
{
    return m_impl->log();
}

QQuickShaderEffect::Status QQuickShaderEffect::status() const
{
    return m_impl->status();
}

bool QQuickShaderEffect::event(QEvent *e)
{
    if (m_impl)
        m_impl->handleEvent(e);
    return QQuickItem::event(e);
}

void QQuickShaderEffect::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
{
    m_impl->handleGeometryChanged(newGeometry, oldGeometry);
    QQuickItem::geometryChange(newGeometry, oldGeometry);
}

QSGNode *QQuickShaderEffect::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData)
{
    return m_impl->handleUpdatePaintNode(oldNode, updatePaintNodeData);
}

void QQuickShaderEffect::componentComplete()
{
    m_impl->maybeUpdateShaders();
    QQuickItem::componentComplete();
}

void QQuickShaderEffect::itemChange(ItemChange change, const ItemChangeData &value)
{
    // It's possible for itemChange to be called during destruction when deleting
    // the QQuickShaderEffectImpl. We nullify m_impl before deleting it via another pointer
    // to it, so we must check that it's not null before trying to use it here.
    if (m_impl)
        m_impl->handleItemChange(change, value);
    QQuickItem::itemChange(change, value);
}

bool QQuickShaderEffect::isComponentComplete() const
{
    return QQuickItem::isComponentComplete();
}

QString QQuickShaderEffect::parseLog() // for OpenGL-based autotests
{
    return m_impl->parseLog();
}

bool QQuickShaderEffect::updateUniformValue(const QByteArray &name, const QVariant &value)
{
    auto node = static_cast<QSGShaderEffectNode *>(QQuickItemPrivate::get(this)->paintNode);
    if (!node)
        return false;

    return m_impl->updateUniformValue(name, value, node);
}

void QQuickShaderEffectPrivate::updatePolish()
{
    Q_Q(QQuickShaderEffect);
    if (!qmlEngine(q))
        return;
    q->m_impl->maybeUpdateShaders();
}

constexpr int indexToMappedId(const int shaderType, const int idx)
{
    return idx | (shaderType << 16);
}

constexpr int mappedIdToIndex(const int mappedId)
{
    return mappedId & 0xFFFF;
}

constexpr int mappedIdToShaderType(const int mappedId)
{
    return mappedId >> 16;
}

QQuickShaderEffectImpl::QQuickShaderEffectImpl(QQuickShaderEffect *item)
    : QObject(item)
    , m_item(item)
    , m_meshResolution(1, 1)
    , m_mesh(nullptr)
    , m_cullMode(QQuickShaderEffect::NoCulling)
    , m_blending(true)
    , m_supportsAtlasTextures(false)
    , m_mgr(nullptr)
    , m_fragNeedsUpdate(true)
    , m_vertNeedsUpdate(true)
{
    qRegisterMetaType<QSGGuiThreadShaderEffectManager::ShaderInfo::Type>("ShaderInfo::Type");
    for (int i = 0; i < NShader; ++i)
        m_inProgress[i] = nullptr;
}

QQuickShaderEffectImpl::~QQuickShaderEffectImpl()
{
    for (int i = 0; i < NShader; ++i) {
        disconnectSignals(Shader(i));
        clearMappers(Shader(i));
    }

    delete m_mgr;
}

void QQuickShaderEffectImpl::setFragmentShader(const QUrl &fileUrl)
{
    if (m_fragShader == fileUrl)
        return;

    m_fragShader = fileUrl;

    m_fragNeedsUpdate = true;
    if (m_item->isComponentComplete())
        maybeUpdateShaders();

    emit m_item->fragmentShaderChanged();
}

void QQuickShaderEffectImpl::setVertexShader(const QUrl &fileUrl)
{
    if (m_vertShader == fileUrl)
        return;

    m_vertShader = fileUrl;

    m_vertNeedsUpdate = true;
    if (m_item->isComponentComplete())
        maybeUpdateShaders();

    emit m_item->vertexShaderChanged();
}

void QQuickShaderEffectImpl::setBlending(bool enable)
{
    if (m_blending == enable)
        return;

    m_blending = enable;
    m_item->update();
    emit m_item->blendingChanged();
}

QVariant QQuickShaderEffectImpl::mesh() const
{
    return m_mesh ? QVariant::fromValue(static_cast<QObject *>(m_mesh))
                  : QVariant::fromValue(m_meshResolution);
}

void QQuickShaderEffectImpl::setMesh(const QVariant &mesh)
{
    QQuickShaderEffectMesh *newMesh = qobject_cast<QQuickShaderEffectMesh *>(qvariant_cast<QObject *>(mesh));
    if (newMesh && newMesh == m_mesh)
        return;

    if (m_mesh)
        disconnect(m_mesh, SIGNAL(geometryChanged()), this, nullptr);

    m_mesh = newMesh;

    if (m_mesh) {
        connect(m_mesh, SIGNAL(geometryChanged()), this, SLOT(markGeometryDirtyAndUpdate()));
    } else {
        if (mesh.canConvert<QSize>()) {
            m_meshResolution = mesh.toSize();
        } else {
            QList<QByteArray> res = mesh.toByteArray().split('x');
            bool ok = res.size() == 2;
            if (ok) {
                int w = res.at(0).toInt(&ok);
                if (ok) {
                    int h = res.at(1).toInt(&ok);
                    if (ok)
                        m_meshResolution = QSize(w, h);
                }
            }
            if (!ok)
                qWarning("ShaderEffect: mesh property must be a size or an object deriving from QQuickShaderEffectMesh");
        }
        m_defaultMesh.setResolution(m_meshResolution);
    }

    m_dirty |= QSGShaderEffectNode::DirtyShaderMesh;
    m_item->update();

    emit m_item->meshChanged();
}

void QQuickShaderEffectImpl::setCullMode(QQuickShaderEffect::CullMode face)
{
    if (m_cullMode == face)
        return;

    m_cullMode = face;
    m_item->update();
    emit m_item->cullModeChanged();
}

void QQuickShaderEffectImpl::setSupportsAtlasTextures(bool supports)
{
    if (m_supportsAtlasTextures == supports)
        return;

    m_supportsAtlasTextures = supports;
    markGeometryDirtyAndUpdate();
    emit m_item->supportsAtlasTexturesChanged();
}

QString QQuickShaderEffectImpl::parseLog()
{
    maybeUpdateShaders();
    return log();
}

QString QQuickShaderEffectImpl::log() const
{
    QSGGuiThreadShaderEffectManager *mgr = shaderEffectManager();
    if (!mgr)
        return QString();

    return mgr->log();
}

QQuickShaderEffect::Status QQuickShaderEffectImpl::status() const
{
    QSGGuiThreadShaderEffectManager *mgr = shaderEffectManager();
    if (!mgr)
        return QQuickShaderEffect::Uncompiled;

    return QQuickShaderEffect::Status(mgr->status());
}

void QQuickShaderEffectImpl::handleEvent(QEvent *event)
{
    if (event->type() == QEvent::DynamicPropertyChange) {
        const auto propertyName = static_cast<QDynamicPropertyChangeEvent *>(event)->propertyName();
        const auto mappedId = findMappedShaderVariableId(propertyName);
        if (mappedId)
            propertyChanged(*mappedId);
    }
}

void QQuickShaderEffectImpl::handleGeometryChanged(const QRectF &, const QRectF &)
{
    m_dirty |= QSGShaderEffectNode::DirtyShaderGeometry;
}

QSGNode *QQuickShaderEffectImpl::handleUpdatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *)
{
    QSGShaderEffectNode *node = static_cast<QSGShaderEffectNode *>(oldNode);

    if (m_item->width() <= 0 || m_item->height() <= 0) {
        delete node;
        return nullptr;
    }

    // Do not change anything while a new shader is being reflected or compiled.
    if (m_inProgress[Vertex] || m_inProgress[Fragment])
        return node;

    // The manager should be already created on the gui thread. Just take that instance.
    QSGGuiThreadShaderEffectManager *mgr = shaderEffectManager();
    if (!mgr) {
        delete node;
        return nullptr;
    }

    if (!node) {
        QSGRenderContext *rc = QQuickWindowPrivate::get(m_item->window())->context;
        node = rc->sceneGraphContext()->createShaderEffectNode(rc);
        if (!node) {
            qWarning("No shader effect node");
            return nullptr;
        }
        m_dirty = QSGShaderEffectNode::DirtyShaderAll;
        connect(node, &QSGShaderEffectNode::textureChanged,
                this, &QQuickShaderEffectImpl::markGeometryDirtyAndUpdateIfSupportsAtlas);
    }

    QSGShaderEffectNode::SyncData sd;
    sd.dirty = m_dirty;
    sd.cullMode = QSGShaderEffectNode::CullMode(m_cullMode);
    sd.blending = m_blending;
    sd.vertex.shader = &m_shaders[Vertex];
    sd.vertex.dirtyConstants = &m_dirtyConstants[Vertex];
    sd.vertex.dirtyTextures = &m_dirtyTextures[Vertex];
    sd.fragment.shader = &m_shaders[Fragment];
    sd.fragment.dirtyConstants = &m_dirtyConstants[Fragment];
    sd.fragment.dirtyTextures = &m_dirtyTextures[Fragment];
    node->syncMaterial(&sd);

    if (m_dirty & QSGShaderEffectNode::DirtyShaderMesh) {
        node->setGeometry(nullptr);
        m_dirty &= ~QSGShaderEffectNode::DirtyShaderMesh;
        m_dirty |= QSGShaderEffectNode::DirtyShaderGeometry;
    }

    if (m_dirty & QSGShaderEffectNode::DirtyShaderGeometry) {
        const QRectF rect(0, 0, m_item->width(), m_item->height());
        QQuickShaderEffectMesh *mesh = m_mesh ? m_mesh : &m_defaultMesh;
        QSGGeometry *geometry = node->geometry();

        const QRectF srcRect = node->updateNormalizedTextureSubRect(m_supportsAtlasTextures);
        geometry = mesh->updateGeometry(geometry, 2, 0, srcRect, rect);

        node->setFlag(QSGNode::OwnsGeometry, false);
        node->setGeometry(geometry);
        node->setFlag(QSGNode::OwnsGeometry, true);

        m_dirty &= ~QSGShaderEffectNode::DirtyShaderGeometry;
    }

    m_dirty = {};
    for (int i = 0; i < NShader; ++i) {
        m_dirtyConstants[i].clear();
        m_dirtyTextures[i].clear();
    }

    return node;
}

void QQuickShaderEffectImpl::maybeUpdateShaders()
{
    if (m_vertNeedsUpdate)
        m_vertNeedsUpdate = !updateShader(Vertex, m_vertShader);
    if (m_fragNeedsUpdate)
        m_fragNeedsUpdate = !updateShader(Fragment, m_fragShader);
    if (m_vertNeedsUpdate || m_fragNeedsUpdate) {
        // This function is invoked either from componentComplete or in a
        // response to a previous invocation's polish() request. If this is
        // case #1 then updateShader can fail due to not having a window or
        // scenegraph ready. Schedule the polish to try again later. In case #2
        // the backend probably does not have shadereffect support so there is
        // nothing to do for us here.
        if (!m_item->window() || !m_item->window()->isSceneGraphInitialized())
            m_item->polish();
    }
}

bool QQuickShaderEffectImpl::updateUniformValue(const QByteArray &name, const QVariant &value,
                                                QSGShaderEffectNode *node)
{
    const auto mappedId = findMappedShaderVariableId(name);
    if (!mappedId)
        return false;

    const Shader type = Shader(mappedIdToShaderType(*mappedId));
    const int idx = mappedIdToIndex(*mappedId);

    // Update value
    m_shaders[type].varData[idx].value = value;

    // Insert dirty uniform
    QSet<int> dirtyConstants[NShader];
    dirtyConstants[type].insert(idx);

    // Sync material change
    QSGShaderEffectNode::SyncData sd;
    sd.dirty = QSGShaderEffectNode::DirtyShaderConstant;
    sd.cullMode = QSGShaderEffectNode::CullMode(m_cullMode);
    sd.blending = m_blending;
    sd.vertex.shader = &m_shaders[Vertex];
    sd.vertex.dirtyConstants = &dirtyConstants[Vertex];
    sd.vertex.dirtyTextures = {};
    sd.fragment.shader = &m_shaders[Fragment];
    sd.fragment.dirtyConstants = &dirtyConstants[Fragment];
    sd.fragment.dirtyTextures = {};

    node->syncMaterial(&sd);

    return true;
}

void QQuickShaderEffectImpl::handleItemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value)
{
    // Move the window ref.
    if (change == QQuickItem::ItemSceneChange) {
        for (int shaderType = 0; shaderType < NShader; ++shaderType) {
            for (const auto &vd : qAsConst(m_shaders[shaderType].varData)) {
                if (vd.specialType == QSGShaderEffectNode::VariableData::Source) {
                    QQuickItem *source = qobject_cast<QQuickItem *>(qvariant_cast<QObject *>(vd.value));
                    if (source) {
                        if (value.window)
                            QQuickItemPrivate::get(source)->refWindow(value.window);
                        else
                            QQuickItemPrivate::get(source)->derefWindow();
                    }
                }
            }
        }
    }
}

QSGGuiThreadShaderEffectManager *QQuickShaderEffectImpl::shaderEffectManager() const
{
    if (!m_mgr) {
        // return null if this is not the gui thread and not already created
        if (QThread::currentThread() != m_item->thread())
            return m_mgr;
        QQuickWindow *w = m_item->window();
        if (w) { // note: just the window, don't care about isSceneGraphInitialized() here
            m_mgr = QQuickWindowPrivate::get(w)->context->sceneGraphContext()->createGuiThreadShaderEffectManager();
            if (m_mgr) {
                connect(m_mgr, SIGNAL(logAndStatusChanged()), m_item, SIGNAL(logChanged()));
                connect(m_mgr, SIGNAL(logAndStatusChanged()), m_item, SIGNAL(statusChanged()));
                connect(m_mgr, &QSGGuiThreadShaderEffectManager::shaderCodePrepared, this, &QQuickShaderEffectImpl::shaderCodePrepared);
            }
        }
    }

    return m_mgr;
}

void QQuickShaderEffectImpl::disconnectSignals(Shader shaderType)
{
    for (auto *mapper : m_mappers[shaderType]) {
        void *a = mapper;
        if (mapper)
            QObjectPrivate::disconnect(m_item, mapper->signalIndex(), &a);
    }
    for (const auto &vd : qAsConst(m_shaders[shaderType].varData)) {
        if (vd.specialType == QSGShaderEffectNode::VariableData::Source) {
            QQuickItem *source = qobject_cast<QQuickItem *>(qvariant_cast<QObject *>(vd.value));
            if (source) {
                if (m_item->window())
                    QQuickItemPrivate::get(source)->derefWindow();
                QObject::disconnect(source, SIGNAL(destroyed(QObject*)), this, SLOT(sourceDestroyed(QObject*)));
            }
        }
    }
}

void QQuickShaderEffectImpl::clearMappers(QQuickShaderEffectImpl::Shader shaderType)
{
    for (auto *mapper : qAsConst(m_mappers[shaderType])) {
        if (mapper)
            mapper->destroyIfLastRef();
    }
    m_mappers[shaderType].clear();
}

static inline QVariant getValueFromProperty(QObject *item, const QMetaObject *itemMetaObject,
                                            const QByteArray &name, int propertyIndex)
{
    QVariant value;
    if (propertyIndex == -1) {
        value = item->property(name);
    } else {
        value = itemMetaObject->property(propertyIndex).read(item);
    }
    return value;
}

using QQuickShaderInfoCache = QHash<QUrl, QSGGuiThreadShaderEffectManager::ShaderInfo>;
Q_GLOBAL_STATIC(QQuickShaderInfoCache, shaderInfoCache)

void qtquick_shadereffect_purge_gui_thread_shader_cache()
{
    shaderInfoCache()->clear();
}

bool QQuickShaderEffectImpl::updateShader(Shader shaderType, const QUrl &fileUrl)
{
    QSGGuiThreadShaderEffectManager *mgr = shaderEffectManager();
    if (!mgr)
        return false;

    const bool texturesSeparate = mgr->hasSeparateSamplerAndTextureObjects();

    disconnectSignals(shaderType);

    m_shaders[shaderType].shaderInfo = QSGGuiThreadShaderEffectManager::ShaderInfo();
    m_shaders[shaderType].varData.clear();

    if (!fileUrl.isEmpty()) {
        const QQmlContext *context = qmlContext(m_item);
        const QUrl loadUrl = context ? context->resolvedUrl(fileUrl) : fileUrl;
        auto it = shaderInfoCache()->constFind(loadUrl);
        if (it != shaderInfoCache()->cend()) {
            m_shaders[shaderType].shaderInfo = *it;
            m_shaders[shaderType].hasShaderCode = true;
        } else {
            // Each prepareShaderCode call needs its own work area, hence the
            // dynamic alloc. If there are calls in progress, let those run to
            // finish, their results can then simply be ignored because
            // m_inProgress indicates what we care about.
            m_inProgress[shaderType] = new QSGGuiThreadShaderEffectManager::ShaderInfo;
            const QSGGuiThreadShaderEffectManager::ShaderInfo::Type typeHint =
                    shaderType == Vertex ? QSGGuiThreadShaderEffectManager::ShaderInfo::TypeVertex
                                         : QSGGuiThreadShaderEffectManager::ShaderInfo::TypeFragment;
            // Figure out what input parameters and variables are used in the
            // shader. This is where the data is pulled in from the file.
            // (however, if there is compilation involved, that happens at a
            // later stage, up to the QRhi backend)
            mgr->prepareShaderCode(typeHint, loadUrl, m_inProgress[shaderType]);
            // the rest is handled in shaderCodePrepared()
            return true;
        }
    } else {
        m_shaders[shaderType].hasShaderCode = false;
        if (shaderType == Fragment) {
            // With built-in shaders hasShaderCode is set to false and all
            // metadata is empty, as it is left up to the node to provide a
            // built-in default shader and its metadata. However, in case of
            // the built-in fragment shader the value for 'source' has to be
            // provided and monitored like with an application-provided shader.
            QSGGuiThreadShaderEffectManager::ShaderInfo::Variable v;
            v.name = QByteArrayLiteral("source");
            v.bindPoint = 0; // fake
            v.type = texturesSeparate ? QSGGuiThreadShaderEffectManager::ShaderInfo::Texture
                                      : QSGGuiThreadShaderEffectManager::ShaderInfo::Sampler;
            m_shaders[shaderType].shaderInfo.variables.append(v);
        }
    }

    updateShaderVars(shaderType);
    m_dirty |= QSGShaderEffectNode::DirtyShaders;
    m_item->update();
    return true;
}

void QQuickShaderEffectImpl::shaderCodePrepared(bool ok, QSGGuiThreadShaderEffectManager::ShaderInfo::Type typeHint,
                                                const QUrl &loadUrl, QSGGuiThreadShaderEffectManager::ShaderInfo *result)
{
    const Shader shaderType = typeHint == QSGGuiThreadShaderEffectManager::ShaderInfo::TypeVertex ? Vertex : Fragment;

    // If another call was made to updateShader() for the same shader type in
    // the meantime then our results are useless, just drop them.
    if (result != m_inProgress[shaderType]) {
        delete result;
        return;
    }

    m_shaders[shaderType].shaderInfo = *result;
    delete result;
    m_inProgress[shaderType] = nullptr;

    if (!ok) {
        qWarning("ShaderEffect: shader preparation failed for %s\n%s\n",
                 qPrintable(loadUrl.toString()), qPrintable(log()));
        m_shaders[shaderType].hasShaderCode = false;
        return;
    }

    m_shaders[shaderType].hasShaderCode = true;
    shaderInfoCache()->insert(loadUrl, m_shaders[shaderType].shaderInfo);
    updateShaderVars(shaderType);
    m_dirty |= QSGShaderEffectNode::DirtyShaders;
    m_item->update();
}

void QQuickShaderEffectImpl::updateShaderVars(Shader shaderType)
{
    QSGGuiThreadShaderEffectManager *mgr = shaderEffectManager();
    if (!mgr)
        return;

    const bool texturesSeparate = mgr->hasSeparateSamplerAndTextureObjects();

    const int varCount = m_shaders[shaderType].shaderInfo.variables.count();
    m_shaders[shaderType].varData.resize(varCount);

    // Reuse signal mappers as much as possible since the mapping is based on
    // the index and shader type which are both constant.
    if (m_mappers[shaderType].count() < varCount)
        m_mappers[shaderType].resize(varCount);

    auto *engine = qmlEngine(m_item);
    QQmlPropertyCache *propCache = engine ? QQmlData::ensurePropertyCache(engine, m_item) : nullptr;

    if (!m_itemMetaObject)
        m_itemMetaObject = m_item->metaObject();

    // Hook up the signals to get notified about changes for properties that
    // correspond to variables in the shader. Store also the values.
    for (int i = 0; i < varCount; ++i) {
        const auto &v(m_shaders[shaderType].shaderInfo.variables.at(i));
        QSGShaderEffectNode::VariableData &vd(m_shaders[shaderType].varData[i]);
        const bool isSpecial = v.name.startsWith("qt_"); // special names not mapped to properties
        if (isSpecial) {
            if (v.name == "qt_Opacity")
                vd.specialType = QSGShaderEffectNode::VariableData::Opacity;
            else if (v.name == "qt_Matrix")
                vd.specialType = QSGShaderEffectNode::VariableData::Matrix;
            else if (v.name.startsWith("qt_SubRect_"))
                vd.specialType = QSGShaderEffectNode::VariableData::SubRect;
            continue;
        }

        // The value of a property corresponding to a sampler is the source
        // item ref, unless there are separate texture objects in which case
        // the sampler is ignored (here).
        if (v.type == QSGGuiThreadShaderEffectManager::ShaderInfo::Sampler) {
            if (texturesSeparate) {
                vd.specialType = QSGShaderEffectNode::VariableData::Unused;
                continue;
            } else {
                vd.specialType = QSGShaderEffectNode::VariableData::Source;
            }
        } else if (v.type == QSGGuiThreadShaderEffectManager::ShaderInfo::Texture) {
            Q_ASSERT(texturesSeparate);
            vd.specialType = QSGShaderEffectNode::VariableData::Source;
        } else {
            vd.specialType = QSGShaderEffectNode::VariableData::None;
        }

        // Find the property on the ShaderEffect item.
        int propIdx = -1;
        QQmlPropertyData *pd = nullptr;
        if (propCache) {
            pd = propCache->property(QLatin1String(v.name), nullptr, nullptr);
            if (pd) {
                if (!pd->isFunction())
                    propIdx = pd->coreIndex();
            }
        }
        if (propIdx >= 0) {
            if (pd && !pd->isFunction()) {
                if (pd->notifyIndex() == -1) {
                    qWarning("QQuickShaderEffect: property '%s' does not have notification method!", v.name.constData());
                } else {
                    auto *&mapper = m_mappers[shaderType][i];
                    if (!mapper) {
                        const int mappedId = indexToMappedId(shaderType, i);
                        mapper = new QtPrivate::EffectSlotMapper([this, mappedId](){
                            this->propertyChanged(mappedId);
                        });
                    }
                    mapper->setSignalIndex(m_itemMetaObject->property(propIdx).notifySignal().methodIndex());
                    Q_ASSERT(m_item->metaObject() == m_itemMetaObject);
                    bool ok = QObjectPrivate::connectImpl(m_item, pd->notifyIndex(), m_item, nullptr, mapper,
                                                          Qt::AutoConnection, nullptr, m_itemMetaObject);
                    if (!ok)
                        qWarning() << "Failed to connect to property" << m_itemMetaObject->property(propIdx).name()
                                   << "(" << propIdx << ", signal index" << pd->notifyIndex()
                                   << ") of item" << m_item;
                }
            }
        } else {
            // Do not warn for dynamic properties.
            if (!m_item->property(v.name.constData()).isValid())
                qWarning("ShaderEffect: '%s' does not have a matching property", v.name.constData());
        }


        vd.propertyIndex = propIdx;
        vd.value = getValueFromProperty(m_item, m_itemMetaObject, v.name, vd.propertyIndex);
        if (vd.specialType == QSGShaderEffectNode::VariableData::Source) {
            QQuickItem *source = qobject_cast<QQuickItem *>(qvariant_cast<QObject *>(vd.value));
            if (source) {
                if (m_item->window())
                    QQuickItemPrivate::get(source)->refWindow(m_item->window());
                QObject::connect(source, SIGNAL(destroyed(QObject*)), this, SLOT(sourceDestroyed(QObject*)));
            }
        }
    }
}

bool QQuickShaderEffectImpl::sourceIsUnique(QQuickItem *source, Shader typeToSkip, int indexToSkip) const
{
    for (int shaderType = 0; shaderType < NShader; ++shaderType) {
        for (int idx = 0; idx < m_shaders[shaderType].varData.count(); ++idx) {
            if (shaderType != typeToSkip || idx != indexToSkip) {
                const auto &vd(m_shaders[shaderType].varData[idx]);
                if (vd.specialType == QSGShaderEffectNode::VariableData::Source && qvariant_cast<QObject *>(vd.value) == source)
                    return false;
            }
        }
    }
    return true;
}

std::optional<int> QQuickShaderEffectImpl::findMappedShaderVariableId(const QByteArray &name) const
{
    for (int shaderType = 0; shaderType < NShader; ++shaderType) {
        const auto &vars = m_shaders[shaderType].shaderInfo.variables;
        for (int idx = 0; idx < vars.count(); ++idx) {
            if (vars[idx].name == name)
                return indexToMappedId(shaderType, idx);
        }
    }

    return {};
}

void QQuickShaderEffectImpl::propertyChanged(int mappedId)
{
    const Shader type = Shader(mappedIdToShaderType(mappedId));
    const int idx = mappedIdToIndex(mappedId);
    const auto &v(m_shaders[type].shaderInfo.variables[idx]);
    auto &vd(m_shaders[type].varData[idx]);

    vd.value = getValueFromProperty(m_item, m_itemMetaObject, v.name, vd.propertyIndex);

    if (vd.specialType == QSGShaderEffectNode::VariableData::Source) {
        QQuickItem *source = qobject_cast<QQuickItem *>(qvariant_cast<QObject *>(vd.value));
        if (source) {
            if (m_item->window())
                QQuickItemPrivate::get(source)->derefWindow();
            // QObject::disconnect() will disconnect all matching connections.
            // If the same source has been attached to two separate
            // textures/samplers, then changing one of them would trigger both
            // to be disconnected. So check first.
            if (sourceIsUnique(source, type, idx))
                QObject::disconnect(source, SIGNAL(destroyed(QObject*)), this, SLOT(sourceDestroyed(QObject*)));
        }

        source = qobject_cast<QQuickItem *>(qvariant_cast<QObject *>(vd.value));
        if (source) {
            // 'source' needs a window to get a scene graph node. It usually gets one through its
            // parent, but if the source item is "inline" rather than a reference -- i.e.
            // "property variant source: Image { }" instead of "property variant source: foo" -- it
            // will not get a parent. In those cases, 'source' should get the window from 'item'.
            if (m_item->window())
                QQuickItemPrivate::get(source)->refWindow(m_item->window());
            QObject::connect(source, SIGNAL(destroyed(QObject*)), this, SLOT(sourceDestroyed(QObject*)));
        }

        m_dirty |= QSGShaderEffectNode::DirtyShaderTexture;
        m_dirtyTextures[type].insert(idx);

     } else {
        m_dirty |= QSGShaderEffectNode::DirtyShaderConstant;
        m_dirtyConstants[type].insert(idx);
    }

    m_item->update();
}

void QQuickShaderEffectImpl::sourceDestroyed(QObject *object)
{
    for (int shaderType = 0; shaderType < NShader; ++shaderType) {
        for (auto &vd : m_shaders[shaderType].varData) {
            if (vd.specialType == QSGShaderEffectNode::VariableData::Source && vd.value.canConvert<QObject *>()) {
                if (qvariant_cast<QObject *>(vd.value) == object)
                    vd.value = QVariant();
            }
        }
    }
}

void QQuickShaderEffectImpl::markGeometryDirtyAndUpdate()
{
    m_dirty |= QSGShaderEffectNode::DirtyShaderGeometry;
    m_item->update();
}

void QQuickShaderEffectImpl::markGeometryDirtyAndUpdateIfSupportsAtlas()
{
    if (m_supportsAtlasTextures)
        markGeometryDirtyAndUpdate();
}



QT_END_NAMESPACE

#include "moc_qquickshadereffect_p.cpp"
#include "qquickshadereffect.moc"