summaryrefslogtreecommitdiffstats
path: root/src/gui/rhi/qshaderdescription.cpp
blob: f64daf02ef04bb3e7f1059c011f05d84bafdb678 (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
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qshaderdescription_p.h"
#include "qshader_p.h"
#include <QDebug>
#include <QDataStream>
#include <QJsonObject>
#include <QJsonArray>

QT_BEGIN_NAMESPACE

/*!
    \class QShaderDescription
    \ingroup painting-3D
    \inmodule QtGui
    \since 6.6

    \brief Describes the interface of a shader.

    \warning The QRhi family of classes in the Qt Gui module, including QShader
    and QShaderDescription, offer limited compatibility guarantees. There are
    no source or binary compatibility guarantees for these classes, meaning the
    API is only guaranteed to work with the Qt version the application was
    developed against. Source incompatible changes are however aimed to be kept
    at a minimum and will only be made in minor releases (6.7, 6.8, and so on).
    To use these classes in an application, link to
    \c{Qt::GuiPrivate} (if using CMake), and include the headers with the \c
    rhi prefix, for example \c{#include <rhi/qshaderdescription.h>}.

    A shader typically has a set of inputs and outputs. A vertex shader for
    example has a number of input variables and may use one or more uniform
    buffers to access data (e.g. a modelview matrix) provided by the
    application. The shader for the fragment stage receives data from the
    vertex stage (in a simple setup) and may also rely on data from uniform
    buffers, images, and samplers.

    When it comes to vertex inputs and the layout of the uniform buffers (what
    are the names of the members? what is there size, offset, and so on),
    applications and frameworks may need to discover this dynamically at run
    time. This is typical when the shader is not built-in but provided by an
    external entity, like the user.

    Modern and lean graphics APIs may no longer provide a way to query shader
    reflection information at run time. Therefore, such data is now
    automatically generated by QShaderBaker and is provided as a
    QShaderDescription object for each and every QShader.

    \section2 Example

    Take the following vertex shader:

    \badcode
        #version 440

        layout(location = 0) in vec4 position;
        layout(location = 1) in vec3 color;
        layout(location = 0) out vec3 v_color;

        layout(std140, binding = 0) uniform buf {
            mat4 mvp;
            float opacity;
        } ubuf;

        void main()
        {
            v_color = color;
            gl_Position = ubuf.mvp * position;
        }
    \endcode

    This shader has two inputs: \c position at location 0 with a type of \c
    vec4, and \c color at location 1 with a type of \c vec3. It has one output:
    \c v_color, although this is typically not interesting for applications.
    What is more important, there is a uniform block at binding 0 with a size
    of 68 bytes and two members, a 4x4 matrix named \c mvp at offset 0, and a
    float \c opacity at offset 64.

    All this is described by a QShaderDescription object. QShaderDescription can
    be serialized to JSON and to a binary format via QDataStream, and can be
    deserialized from this binary format. In practice this is rarely needed
    since QShader takes care of the associated QShaderDescription automatically,
    but if the QShaderDescription of the above shader would be written out as
    JSON (like it is done by the \c qsb tool's \c{-d} option), it would look
    like the following:

    \badcode
        {
            "inputs": [
                {
                    "location": 1,
                    "name": "color",
                    "type": "vec3"
                },
                {
                    "location": 0,
                    "name": "position",
                    "type": "vec4"
                }
            ],
            "outputs": [
                {
                    "location": 0,
                    "name": "v_color",
                    "type": "vec3"
                }
            ],
            "uniformBlocks": [
                {
                    "binding": 0,
                    "blockName": "buf",
                    "members": [
                        {
                            "matrixStride": 16,
                            "name": "mvp",
                            "offset": 0,
                            "size": 64,
                            "type": "mat4"
                        },
                        {
                            "name": "opacity",
                            "offset": 64,
                            "size": 4,
                            "type": "float"
                        }
                    ],
                    "set": 0,
                    "size": 68,
                    "structName": "ubuf"
                }
            ]
        }
    \endcode

    The C++ API allows accessing a data structure like the above. For
    simplicity the inner structs only contain public data members, also
    considering that their layout is unlikely to change in the future.

    \sa QShaderBaker, QShader
 */

/*!
    \enum QShaderDescription::VariableType
    Represents the type of a variable or block member.

    \value Unknown
    \value Float
    \value Vec2
    \value Vec3
    \value Vec4
    \value Mat2
    \value Mat2x3
    \value Mat2x4
    \value Mat3
    \value Mat3x2
    \value Mat3x4
    \value Mat4
    \value Mat4x2
    \value Mat4x3
    \value Int
    \value Int2
    \value Int3
    \value Int4
    \value Uint
    \value Uint2
    \value Uint3
    \value Uint4
    \value Bool
    \value Bool2
    \value Bool3
    \value Bool4
    \value Double
    \value Double2
    \value Double3
    \value Double4
    \value DMat2
    \value DMat2x3
    \value DMat2x4
    \value DMat3
    \value DMat3x2
    \value DMat3x4
    \value DMat4
    \value DMat4x2
    \value DMat4x3
    \value Sampler1D
    \value Sampler2D
    \value Sampler2DMS
    \value Sampler3D
    \value SamplerCube
    \value Sampler1DArray
    \value Sampler2DArray
    \value Sampler2DMSArray
    \value Sampler3DArray
    \value SamplerCubeArray
    \value SamplerRect
    \value SamplerBuffer
    \value SamplerExternalOES
    \value Sampler For separate samplers.
    \value Image1D
    \value Image2D
    \value Image2DMS
    \value Image3D
    \value ImageCube
    \value Image1DArray
    \value Image2DArray
    \value Image2DMSArray
    \value Image3DArray
    \value ImageCubeArray
    \value ImageRect
    \value ImageBuffer
    \value Struct
    \value Half
    \value Half2
    \value Half3
    \value Half4
 */

/*!
    \enum QShaderDescription::ImageFormat
    Image format.

    \value ImageFormatUnknown
    \value ImageFormatRgba32f
    \value ImageFormatRgba16f
    \value ImageFormatR32f
    \value ImageFormatRgba8
    \value ImageFormatRgba8Snorm
    \value ImageFormatRg32f
    \value ImageFormatRg16f
    \value ImageFormatR11fG11fB10f
    \value ImageFormatR16f
    \value ImageFormatRgba16
    \value ImageFormatRgb10A2
    \value ImageFormatRg16
    \value ImageFormatRg8
    \value ImageFormatR16
    \value ImageFormatR8
    \value ImageFormatRgba16Snorm
    \value ImageFormatRg16Snorm
    \value ImageFormatRg8Snorm
    \value ImageFormatR16Snorm
    \value ImageFormatR8Snorm
    \value ImageFormatRgba32i
    \value ImageFormatRgba16i
    \value ImageFormatRgba8i
    \value ImageFormatR32i
    \value ImageFormatRg32i
    \value ImageFormatRg16i
    \value ImageFormatRg8i
    \value ImageFormatR16i
    \value ImageFormatR8i
    \value ImageFormatRgba32ui
    \value ImageFormatRgba16ui
    \value ImageFormatRgba8ui
    \value ImageFormatR32ui
    \value ImageFormatRgb10a2ui
    \value ImageFormatRg32ui
    \value ImageFormatRg16ui
    \value ImageFormatRg8ui
    \value ImageFormatR16ui
    \value ImageFormatR8ui
 */

/*!
    \enum QShaderDescription::ImageFlag
    Image flags.

    \value ReadOnlyImage
    \value WriteOnlyImage
 */

/*!
    \enum QShaderDescription::QualifierFlag
    Qualifier flags.

    \value QualifierReadOnly
    \value QualifierWriteOnly
    \value QualifierCoherent
    \value QualifierVolatile
    \value QualifierRestrict
 */

/*!
    \struct QShaderDescription::InOutVariable
    \inmodule QtGui
    \since 6.6

    \brief Describes an input or output variable in the shader.

    \note This is a RHI API with limited compatibility guarantees, see \l QShaderDescription
    for details.
 */

/*!
    \variable QShaderDescription::InOutVariable::name
 */

/*!
    \variable QShaderDescription::InOutVariable::type
 */

/*!
    \variable QShaderDescription::InOutVariable::location
 */

/*!
    \variable QShaderDescription::InOutVariable::binding
 */

/*!
    \variable QShaderDescription::InOutVariable::descriptorSet
 */

/*!
    \variable QShaderDescription::InOutVariable::imageFormat
 */

/*!
    \variable QShaderDescription::InOutVariable::imageFlags
 */

/*!
    \variable QShaderDescription::InOutVariable::arrayDims
 */

/*!
    \variable QShaderDescription::InOutVariable::perPatch
 */

/*!
    \variable QShaderDescription::InOutVariable::structMembers
 */

/*!
    \struct QShaderDescription::BlockVariable
    \inmodule QtGui
    \since 6.6

    \brief Describes a member of a uniform or push constant block.

    \note This is a RHI API with limited compatibility guarantees, see \l QShaderDescription
    for details.
 */

/*!
    \variable QShaderDescription::BlockVariable::name
 */

/*!
    \variable QShaderDescription::BlockVariable::type
 */

/*!
    \variable QShaderDescription::BlockVariable::offset
 */

/*!
    \variable QShaderDescription::BlockVariable::size
 */

/*!
    \variable QShaderDescription::BlockVariable::arrayDims
 */

/*!
    \variable QShaderDescription::BlockVariable::arrayStride
 */

/*!
    \variable QShaderDescription::BlockVariable::matrixStride
 */

/*!
    \variable QShaderDescription::BlockVariable::matrixIsRowMajor
 */

/*!
    \variable QShaderDescription::BlockVariable::structMembers
 */

/*!
    \struct QShaderDescription::UniformBlock
    \inmodule QtGui
    \since 6.6

    \brief Describes a uniform block.

    \note When translating to shading languages without uniform block support
    (like GLSL 120 or GLSL/ES 100), uniform blocks are replaced with ordinary
    uniforms in a struct. The name of the struct, and so the prefix for the
    uniforms generated from the block members, is given by structName.

    \note This is a RHI API with limited compatibility guarantees, see \l QShaderDescription
    for details.
 */

/*!
    \variable QShaderDescription::UniformBlock::blockName
 */

/*!
    \variable QShaderDescription::UniformBlock::structName
 */

/*!
    \variable QShaderDescription::UniformBlock::size
 */

/*!
    \variable QShaderDescription::UniformBlock::binding
 */

/*!
    \variable QShaderDescription::UniformBlock::descriptorSet
 */

/*!
    \variable QShaderDescription::UniformBlock::members
 */

/*!
    \struct QShaderDescription::PushConstantBlock
    \inmodule QtGui
    \since 6.6

    \brief Describes a push constant block.

    \note This is a RHI API with limited compatibility guarantees, see \l QShaderDescription
    for details.
 */

/*!
    \variable QShaderDescription::PushConstantBlock::name
 */

/*!
    \variable QShaderDescription::PushConstantBlock::size
 */

/*!
    \variable QShaderDescription::PushConstantBlock::members
 */

/*!
    \struct QShaderDescription::StorageBlock
    \inmodule QtGui
    \since 6.6

    \brief Describes a shader storage block.

    \note This is a RHI API with limited compatibility guarantees, see \l QShaderDescription
    for details.
 */

/*!
    \variable QShaderDescription::StorageBlock::blockName
 */

/*!
    \variable QShaderDescription::StorageBlock::instanceName
 */

/*!
    \variable QShaderDescription::StorageBlock::knownSize
 */

/*!
    \variable QShaderDescription::StorageBlock::binding
 */

/*!
    \variable QShaderDescription::StorageBlock::descriptorSet
 */

/*!
    \variable QShaderDescription::StorageBlock::members
 */

/*!
    \variable QShaderDescription::StorageBlock::runtimeArrayStride
 */

/*!
    \variable QShaderDescription::StorageBlock::qualifierFlags
 */

/*!
    \struct QShaderDescription::BuiltinVariable
    \inmodule QtGui
    \since 6.6

    \brief Describes a built-in variable.

    \note This is a RHI API with limited compatibility guarantees, see \l QShaderDescription
    for details.
 */

/*!
    \variable QShaderDescription::BuiltinVariable::type
 */

/*!
    \variable QShaderDescription::BuiltinVariable::varType
 */

/*!
    \variable QShaderDescription::BuiltinVariable::arrayDims
 */

/*!
    \enum QShaderDescription::BuiltinType
    Built-in variable type.

    \value PositionBuiltin
    \value PointSizeBuiltin
    \value ClipDistanceBuiltin
    \value CullDistanceBuiltin
    \value VertexIdBuiltin
    \value InstanceIdBuiltin
    \value PrimitiveIdBuiltin
    \value InvocationIdBuiltin
    \value LayerBuiltin
    \value ViewportIndexBuiltin
    \value TessLevelOuterBuiltin
    \value TessLevelInnerBuiltin
    \value TessCoordBuiltin
    \value PatchVerticesBuiltin
    \value FragCoordBuiltin
    \value PointCoordBuiltin
    \value FrontFacingBuiltin
    \value SampleIdBuiltin
    \value SamplePositionBuiltin
    \value SampleMaskBuiltin
    \value FragDepthBuiltin
    \value NumWorkGroupsBuiltin
    \value WorkgroupSizeBuiltin
    \value WorkgroupIdBuiltin
    \value LocalInvocationIdBuiltin
    \value GlobalInvocationIdBuiltin
    \value LocalInvocationIndexBuiltin
    \value VertexIndexBuiltin
    \value InstanceIndexBuiltin
 */

/*!
    Constructs a new, empty QShaderDescription.

    \note Being empty implies that isValid() returns \c false for the
    newly constructed instance.
 */
QShaderDescription::QShaderDescription()
    : d(new QShaderDescriptionPrivate)
{
}

/*!
    \internal
 */
void QShaderDescription::detach()
{
    qAtomicDetach(d);
}

/*!
    Constructs a copy of \a other.
 */
QShaderDescription::QShaderDescription(const QShaderDescription &other)
    : d(other.d)
{
    d->ref.ref();
}

/*!
    Assigns \a other to this object.
 */
QShaderDescription &QShaderDescription::operator=(const QShaderDescription &other)
{
    qAtomicAssign(d, other.d);
    return *this;
}

/*!
    Destructor.
 */
QShaderDescription::~QShaderDescription()
{
    if (!d->ref.deref())
        delete d;
}

/*!
   \return true if the QShaderDescription contains at least one entry in one of
   the variable and block lists.
 */
bool QShaderDescription::isValid() const
{
    return !d->inVars.isEmpty() || !d->outVars.isEmpty()
        || !d->uniformBlocks.isEmpty() || !d->pushConstantBlocks.isEmpty() || !d->storageBlocks.isEmpty()
        || !d->combinedImageSamplers.isEmpty() || !d->storageImages.isEmpty()
        || !d->separateImages.isEmpty() || !d->separateSamplers.isEmpty()
        || !d->inBuiltins.isEmpty() || !d->outBuiltins.isEmpty();
}

/*!
    \return a serialized JSON text version of the data.

    \note There is no deserialization method provided for JSON text.

    \sa serialize()
 */
QByteArray QShaderDescription::toJson() const
{
    return d->makeDoc().toJson();
}

/*!
    Serializes this QShaderDescription to \a stream. \a version specifies
    the qsb version.

    \sa deserialize(), toJson()
 */
void QShaderDescription::serialize(QDataStream *stream, int version) const
{
    d->writeToStream(stream, version);
}

/*!
    \return a new QShaderDescription loaded from \a stream. \a version specifies
    the qsb version.

    \sa serialize()
 */
QShaderDescription QShaderDescription::deserialize(QDataStream *stream, int version)
{
    QShaderDescription desc;
    QShaderDescriptionPrivate::get(&desc)->loadFromStream(stream, version);
    return desc;
}

/*!
    \return the list of input variables. This includes vertex inputs (sometimes
    called attributes) for the vertex stage, and inputs for other stages
    (sometimes called varyings).
 */
QList<QShaderDescription::InOutVariable> QShaderDescription::inputVariables() const
{
    return d->inVars;
}

/*!
    \return the list of output variables.
 */
QList<QShaderDescription::InOutVariable> QShaderDescription::outputVariables() const
{
    return d->outVars;
}

/*!
    \return the list of uniform blocks.
 */
QList<QShaderDescription::UniformBlock> QShaderDescription::uniformBlocks() const
{
    return d->uniformBlocks;
}

/*!
    \return the list of push constant blocks.

    \note Avoid relying on push constant blocks for shaders that are to be used
    in combination with the Qt Rendering Hardware Interface since that
    currently has no support for them.
 */
QList<QShaderDescription::PushConstantBlock> QShaderDescription::pushConstantBlocks() const
{
    return d->pushConstantBlocks;
}

/*!
    \return the list of shader storage blocks.

    For example, with GLSL/Vulkan shaders as source, the declaration

    \badcode
        struct Stuff {
            vec2 a;
            vec2 b;
        };
        layout(std140, binding = 0) buffer StuffSsbo {
            vec4 whatever;
            Stuff stuff[];
        } buf;
    \endcode

    generates the following: (shown as textual JSON here)

    \badcode
        "storageBlocks": [ {
            "binding": 0,
            "blockName": "StuffSsbo",
            "instanceName": "buf",
            "knownSize": 16,
            "runtimeArrayStride": 16
            "members": [
                {
                    "name": "whatever",
                    "offset": 0,
                    "size": 16,
                    "type": "vec4"
                },
                {
                    "arrayDims": [
                        0
                    ],
                    "name": "stuff",
                    "offset": 16,
                    "size": 0,
                    "structMembers": [
                        {
                            "name": "a",
                            "offset": 0,
                            "size": 8,
                            "type": "vec2"
                        },
                        {
                            "name": "b",
                            "offset": 8,
                            "size": 8,
                            "type": "vec2"
                        }
                    ],
                    "type": "struct"
                }
            ],
            "set": 0
        } ]
    \endcode

    \note The size of the last member in the storage block is undefined. This shows
    up as \c size 0 and an array dimension of \c{[0]}. The storage block's \c knownSize
    excludes the size of the last member since that will only be known at run time. The
    stride in bytes between array items for a last member with undefined array size is
    \c runtimeArrayStride.  This value is determined according to the specified buffer
    memory layout standard (std140, std430) rules.

    \note SSBOs are not available with some graphics APIs, such as, OpenGL 2.x or
    OpenGL ES older than 3.1.
 */
QList<QShaderDescription::StorageBlock> QShaderDescription::storageBlocks() const
{
    return d->storageBlocks;
}

/*!
    \return the list of combined image samplers

    With GLSL/Vulkan shaders as source a \c{layout(binding = 1) uniform sampler2D tex;}
    uniform generates the following: (shown as textual JSON here)

    \badcode
       "combinedImageSamplers": [
            {
                "binding": 1,
                "name": "tex",
                "set": 0,
                "type": "sampler2D"
            }
        ]
    \endcode

    This does not mean that other language versions of the shader must also use
    a combined image sampler, especially considering that the concept may not
    exist everywhere. For instance, a HLSL version will likely just use a
    Texture2D and SamplerState object with registers t1 and s1, respectively.
  */
QList<QShaderDescription::InOutVariable> QShaderDescription::combinedImageSamplers() const
{
    return d->combinedImageSamplers;
}

QList<QShaderDescription::InOutVariable> QShaderDescription::separateImages() const
{
    return d->separateImages;
}

QList<QShaderDescription::InOutVariable> QShaderDescription::separateSamplers() const
{
    return d->separateSamplers;
}

/*!
    \return the list of image variables.

    These will likely occur in compute shaders. For example,
    \c{layout (binding = 0, rgba8) uniform readonly image2D inputImage;}
    generates the following: (shown as textual JSON here)

    \badcode
       "storageImages": [
            {
                "binding": 0,
                "imageFormat": "rgba8",
                "name": "inputImage",
                "set": 0,
                "type": "image2D"
            }
        ]
    \endcode

    \note Separate image objects are not compatible with some graphics APIs,
    such as, OpenGL 2.x or OpenGL ES older than 3.1.
  */
QList<QShaderDescription::InOutVariable> QShaderDescription::storageImages() const
{
    return d->storageImages;
}

/*!
    \return the list of active builtins used as input. For example, a
    tessellation evaluation shader reading the value of gl_TessCoord and
    gl_Position will have TessCoordBuiltin and PositionBuiltin listed here.
 */
QVector<QShaderDescription::BuiltinVariable> QShaderDescription::inputBuiltinVariables() const
{
    return d->inBuiltins;
}

/*!
    \return the list of active built-in variables used as input. For example, a
    vertex shader will very often have PositionBuiltin as an output built-in.
 */
QVector<QShaderDescription::BuiltinVariable> QShaderDescription::outputBuiltinVariables() const
{
    return d->outBuiltins;
}

/*!
    \return the local size of a compute shader.

    For example, for a compute shader with the following declaration the
    function returns { 256, 16, 1}.

    \badcode
        layout(local_size_x = 256, local_size_y = 16, local_size_z = 1) in;
    \endcode
 */
std::array<uint, 3> QShaderDescription::computeShaderLocalSize() const
{
    return d->localSize;
}

/*!
    \return the number of output vertices.

    For example, for a tessellation control shader with the following
    declaration the function returns 3.

    \badcode
        layout(vertices = 3) out;
    \endcode
 */
uint QShaderDescription::tessellationOutputVertexCount() const
{
    return d->tessOutVertCount;
}

/*!
    \enum QShaderDescription::TessellationMode

    \value UnknownTessellationMode
    \value TrianglesTessellationMode
    \value QuadTessellationMode
    \value IsolineTessellationMode
 */

/*!
    \return the tessellation execution mode for a tessellation control or
    evaluation shader.

    When not set, the returned value is UnknownTessellationMode.

    For example, for a tessellation evaluation shader with the following
    declaration the function returns TrianglesTessellationMode.

    \badcode
        layout(triangles) in;
    \endcode
 */
QShaderDescription::TessellationMode QShaderDescription::tessellationMode() const
{
    return d->tessMode;
}

/*!
    \enum QShaderDescription::TessellationWindingOrder

    \value UnknownTessellationWindingOrder
    \value CwTessellationWindingOrder
    \value CcwTessellationWindingOrder
 */

/*!
    \return the tessellation winding order for a tessellation control or
    evaluation shader.

    When not set, the returned value is UnknownTessellationWindingOrder.

    For example, for a tessellation evaluation shader with the following
    declaration the function returns CcwTessellationWindingOrder.

    \badcode
        layout(triangles, fractional_odd_spacing, ccw) in;
    \endcode
 */
QShaderDescription::TessellationWindingOrder QShaderDescription::tessellationWindingOrder() const
{
    return d->tessWind;
}

/*!
    \enum QShaderDescription::TessellationPartitioning

    \value UnknownTessellationPartitioning
    \value EqualTessellationPartitioning
    \value FractionalEvenTessellationPartitioning
    \value FractionalOddTessellationPartitioning
 */

/*!
    \return the tessellation partitioning mode for a tessellation control or
    evaluation shader.

    When not set, the returned value is UnknownTessellationPartitioning.

    For example, for a tessellation evaluation shader with the following
    declaration the function returns FractionalOddTessellationPartitioning.

    \badcode
        layout(triangles, fractional_odd_spacing, ccw) in;
    \endcode
 */
QShaderDescription::TessellationPartitioning QShaderDescription::tessellationPartitioning() const
{
    return d->tessPart;
}

static const struct TypeTab {
    const char k[20];
    QShaderDescription::VariableType v;
} typeTab[] = {
    { "float", QShaderDescription::Float },
    { "vec2", QShaderDescription::Vec2 },
    { "vec3", QShaderDescription::Vec3 },
    { "vec4", QShaderDescription::Vec4 },
    { "mat2", QShaderDescription::Mat2 },
    { "mat3", QShaderDescription::Mat3 },
    { "mat4", QShaderDescription::Mat4 },

    { "struct", QShaderDescription::Struct },

    { "sampler1D", QShaderDescription::Sampler1D },
    { "sampler2D", QShaderDescription::Sampler2D },
    { "sampler2DMS", QShaderDescription::Sampler2DMS },
    { "sampler3D", QShaderDescription::Sampler3D },
    { "samplerCube", QShaderDescription::SamplerCube },
    { "sampler1DArray", QShaderDescription::Sampler1DArray },
    { "sampler2DArray", QShaderDescription::Sampler2DArray },
    { "sampler2DMSArray", QShaderDescription::Sampler2DMSArray },
    { "sampler3DArray", QShaderDescription::Sampler3DArray },
    { "samplerCubeArray", QShaderDescription::SamplerCubeArray },
    { "samplerRect", QShaderDescription::SamplerRect },
    { "samplerBuffer", QShaderDescription::SamplerBuffer },
    { "samplerExternalOES", QShaderDescription::SamplerExternalOES },
    { "sampler", QShaderDescription::Sampler },

    { "mat2x3", QShaderDescription::Mat2x3 },
    { "mat2x4", QShaderDescription::Mat2x4 },
    { "mat3x2", QShaderDescription::Mat3x2 },
    { "mat3x4", QShaderDescription::Mat3x4 },
    { "mat4x2", QShaderDescription::Mat4x2 },
    { "mat4x3", QShaderDescription::Mat4x3 },

    { "int", QShaderDescription::Int },
    { "ivec2", QShaderDescription::Int2 },
    { "ivec3", QShaderDescription::Int3 },
    { "ivec4", QShaderDescription::Int4 },

    { "uint", QShaderDescription::Uint },
    { "uvec2", QShaderDescription::Uint2 },
    { "uvec3", QShaderDescription::Uint3 },
    { "uvec4", QShaderDescription::Uint4 },

    { "bool", QShaderDescription::Bool },
    { "bvec2", QShaderDescription::Bool2 },
    { "bvec3", QShaderDescription::Bool3 },
    { "bvec4", QShaderDescription::Bool4 },

    { "double", QShaderDescription::Double },
    { "dvec2", QShaderDescription::Double2 },
    { "dvec3", QShaderDescription::Double3 },
    { "dvec4", QShaderDescription::Double4 },
    { "dmat2", QShaderDescription::DMat2 },
    { "dmat3", QShaderDescription::DMat3 },
    { "dmat4", QShaderDescription::DMat4 },
    { "dmat2x3", QShaderDescription::DMat2x3 },
    { "dmat2x4", QShaderDescription::DMat2x4 },
    { "dmat3x2", QShaderDescription::DMat3x2 },
    { "dmat3x4", QShaderDescription::DMat3x4 },
    { "dmat4x2", QShaderDescription::DMat4x2 },
    { "dmat4x3", QShaderDescription::DMat4x3 },

    { "image1D", QShaderDescription::Image1D },
    { "image2D", QShaderDescription::Image2D },
    { "image2DMS", QShaderDescription::Image2DMS },
    { "image3D", QShaderDescription::Image3D },
    { "imageCube", QShaderDescription::ImageCube },
    { "image1DArray", QShaderDescription::Image1DArray },
    { "image2DArray", QShaderDescription::Image2DArray },
    { "image2DMSArray", QShaderDescription::Image2DMSArray },
    { "image3DArray", QShaderDescription::Image3DArray },
    { "imageCubeArray", QShaderDescription::ImageCubeArray },
    { "imageRect", QShaderDescription::ImageRect },
    { "imageBuffer", QShaderDescription::ImageBuffer },

    { "half", QShaderDescription::Half },
    { "half2", QShaderDescription::Half2 },
    { "half3", QShaderDescription::Half3 },
    { "half4", QShaderDescription::Half4 } };

static QLatin1StringView typeStr(QShaderDescription::VariableType t)
{
    for (size_t i = 0; i < sizeof(typeTab) / sizeof(TypeTab); ++i) {
        if (typeTab[i].v == t)
            return QLatin1StringView(typeTab[i].k);
    }
    return {};
}

static const struct ImageFormatTab {
    const char k[15];
    QShaderDescription::ImageFormat v;
} imageFormatTab[] {
    { "unknown", QShaderDescription::ImageFormatUnknown },
    { "rgba32f", QShaderDescription::ImageFormatRgba32f },
    { "rgba16", QShaderDescription::ImageFormatRgba16f },
    { "r32f", QShaderDescription::ImageFormatR32f },
    { "rgba8", QShaderDescription::ImageFormatRgba8 },
    { "rgba8_snorm", QShaderDescription::ImageFormatRgba8Snorm },
    { "rg32f", QShaderDescription::ImageFormatRg32f },
    { "rg16f", QShaderDescription::ImageFormatRg16f },
    { "r11f_g11f_b10f", QShaderDescription::ImageFormatR11fG11fB10f },
    { "r16f", QShaderDescription::ImageFormatR16f },
    { "rgba16", QShaderDescription::ImageFormatRgba16 },
    { "rgb10_a2", QShaderDescription::ImageFormatRgb10A2 },
    { "rg16", QShaderDescription::ImageFormatRg16 },
    { "rg8", QShaderDescription::ImageFormatRg8 },
    { "r16", QShaderDescription::ImageFormatR16 },
    { "r8", QShaderDescription::ImageFormatR8 },
    { "rgba16_snorm", QShaderDescription::ImageFormatRgba16Snorm },
    { "rg16_snorm", QShaderDescription::ImageFormatRg16Snorm },
    { "rg8_snorm", QShaderDescription::ImageFormatRg8Snorm },
    { "r16_snorm", QShaderDescription::ImageFormatR16Snorm },
    { "r8_snorm", QShaderDescription::ImageFormatR8Snorm },
    { "rgba32i", QShaderDescription::ImageFormatRgba32i },
    { "rgba16i", QShaderDescription::ImageFormatRgba16i },
    { "rgba8i", QShaderDescription::ImageFormatRgba8i },
    { "r32i", QShaderDescription::ImageFormatR32i },
    { "rg32i", QShaderDescription::ImageFormatRg32i },
    { "rg16i", QShaderDescription::ImageFormatRg16i },
    { "rg8i", QShaderDescription::ImageFormatRg8i },
    { "r16i", QShaderDescription::ImageFormatR16i },
    { "r8i", QShaderDescription::ImageFormatR8i },
    { "rgba32ui", QShaderDescription::ImageFormatRgba32ui },
    { "rgba16ui", QShaderDescription::ImageFormatRgba16ui },
    { "rgba8ui", QShaderDescription::ImageFormatRgba8ui },
    { "r32ui", QShaderDescription::ImageFormatR32ui },
    { "rgb10_a2ui", QShaderDescription::ImageFormatRgb10a2ui },
    { "rg32ui", QShaderDescription::ImageFormatRg32ui },
    { "rg16ui", QShaderDescription::ImageFormatRg16ui },
    { "rg8ui", QShaderDescription::ImageFormatRg8ui },
    { "r16ui", QShaderDescription::ImageFormatR16ui },
    { "r8ui", QShaderDescription::ImageFormatR8ui }
};

static QLatin1StringView imageFormatStr(QShaderDescription::ImageFormat f)
{
    for (size_t i = 0; i < sizeof(imageFormatTab) / sizeof(ImageFormatTab); ++i) {
        if (imageFormatTab[i].v == f)
            return QLatin1StringView(imageFormatTab[i].k);
    }
    return {};
}

static const struct BuiltinTypeTab {
    const char k[21];
    QShaderDescription::BuiltinType v;
} builtinTypeTab[] = {
    { "Position", QShaderDescription::PositionBuiltin },
    { "PointSize", QShaderDescription::PointSizeBuiltin },
    { "ClipDistance", QShaderDescription::ClipDistanceBuiltin },
    { "CullDistance", QShaderDescription::CullDistanceBuiltin },
    { "VertexId", QShaderDescription::VertexIdBuiltin },
    { "InstanceId", QShaderDescription::InstanceIdBuiltin },
    { "PrimitiveId", QShaderDescription::PrimitiveIdBuiltin },
    { "InvocationId", QShaderDescription::InvocationIdBuiltin },
    { "Layer", QShaderDescription::LayerBuiltin },
    { "ViewportIndex", QShaderDescription::ViewportIndexBuiltin },
    { "TessLevelOuter", QShaderDescription::TessLevelOuterBuiltin },
    { "TessLevelInner", QShaderDescription::TessLevelInnerBuiltin },
    { "TessCoord", QShaderDescription::TessCoordBuiltin },
    { "PatchVertices", QShaderDescription::PatchVerticesBuiltin },
    { "FragCoord", QShaderDescription::FragCoordBuiltin },
    { "PointCoord", QShaderDescription::PointCoordBuiltin },
    { "FrontFacing", QShaderDescription::FrontFacingBuiltin },
    { "SampleId", QShaderDescription::SampleIdBuiltin },
    { "SamplePosition", QShaderDescription::SamplePositionBuiltin },
    { "SampleMask", QShaderDescription::SampleMaskBuiltin },
    { "FragDepth", QShaderDescription::FragDepthBuiltin },
    { "NumWorkGroups", QShaderDescription::NumWorkGroupsBuiltin },
    { "WorkgroupSize", QShaderDescription::WorkgroupSizeBuiltin },
    { "WorkgroupId", QShaderDescription::WorkgroupIdBuiltin },
    { "LocalInvocationId", QShaderDescription::LocalInvocationIdBuiltin },
    { "GlobalInvocationId", QShaderDescription::GlobalInvocationIdBuiltin },
    { "LocalInvocationIndex", QShaderDescription::LocalInvocationIndexBuiltin },
    { "VertexIndex", QShaderDescription::VertexIndexBuiltin },
    { "InstanceIndex", QShaderDescription::InstanceIndexBuiltin }
};

static QLatin1StringView builtinTypeStr(QShaderDescription::BuiltinType t)
{
    for (size_t i = 0; i < sizeof(builtinTypeTab) / sizeof(BuiltinTypeTab); ++i) {
        if (builtinTypeTab[i].v == t)
            return QLatin1StringView(builtinTypeTab[i].k);
    }
    return {};
}

static const struct TessellationModeTab {
    const char k[10];
    QShaderDescription::TessellationMode v;
} tessellationModeTab[] {
    { "unknown", QShaderDescription::UnknownTessellationMode },
    { "triangles", QShaderDescription::TrianglesTessellationMode },
    { "quad", QShaderDescription::QuadTessellationMode },
    { "isoline", QShaderDescription::IsolineTessellationMode }
};

static QLatin1StringView tessModeStr(QShaderDescription::TessellationMode mode)
{
    for (size_t i = 0; i < sizeof(tessellationModeTab) / sizeof(TessellationModeTab); ++i) {
        if (tessellationModeTab[i].v == mode)
            return QLatin1StringView(tessellationModeTab[i].k);
    }
    return {};
}

static const struct TessellationWindingOrderTab {
    const char k[8];
    QShaderDescription::TessellationWindingOrder v;
} tessellationWindingOrderTab[] {
    { "unknown", QShaderDescription::UnknownTessellationWindingOrder },
    { "cw", QShaderDescription::CwTessellationWindingOrder },
    { "ccw", QShaderDescription::CcwTessellationWindingOrder }
};

static QLatin1StringView tessWindStr(QShaderDescription::TessellationWindingOrder w)
{
    for (size_t i = 0; i < sizeof(tessellationWindingOrderTab) / sizeof(TessellationWindingOrderTab); ++i) {
        if (tessellationWindingOrderTab[i].v == w)
            return QLatin1StringView(tessellationWindingOrderTab[i].k);
    }
    return {};
}

static const struct TessellationPartitioningTab {
    const char k[24];
    QShaderDescription::TessellationPartitioning v;
} tessellationPartitioningTab[] {
    { "unknown", QShaderDescription::UnknownTessellationPartitioning },
    { "equal_spacing", QShaderDescription::EqualTessellationPartitioning },
    { "fractional_even_spacing", QShaderDescription::FractionalEvenTessellationPartitioning },
    { "fractional_odd_spacing", QShaderDescription::FractionalOddTessellationPartitioning }
};

static QLatin1StringView tessPartStr(QShaderDescription::TessellationPartitioning p)
{
    for (size_t i = 0; i < sizeof(tessellationPartitioningTab) / sizeof(TessellationPartitioningTab); ++i) {
        if (tessellationPartitioningTab[i].v == p)
            return QLatin1StringView(tessellationPartitioningTab[i].k);
    }
    return {};
}

#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QShaderDescription &sd)
{
    const QShaderDescriptionPrivate *d = sd.d;
    QDebugStateSaver saver(dbg);

    if (sd.isValid()) {
        dbg.nospace() << "QShaderDescription("
                      << "inVars " << d->inVars
                      << " outVars " << d->outVars
                      << " uniformBlocks " << d->uniformBlocks
                      << " pcBlocks " << d->pushConstantBlocks
                      << " storageBlocks " << d->storageBlocks
                      << " combinedSamplers " << d->combinedImageSamplers
                      << " storageImages " << d->storageImages
                      << " separateImages " << d->separateImages
                      << " separateSamplers " << d->separateSamplers
                      << " inBuiltins " << d->inBuiltins
                      << " outBuiltins " << d->outBuiltins
                      << ')';
    } else {
        dbg.nospace() << "QShaderDescription(null)";
    }

    return dbg;
}

QDebug operator<<(QDebug dbg, const QShaderDescription::InOutVariable &var)
{
    QDebugStateSaver saver(dbg);
    dbg.nospace() << "InOutVariable(" << typeStr(var.type) << ' ' << var.name;
    if (var.perPatch)
        dbg.nospace() << " per-patch";
    if (var.location >= 0)
        dbg.nospace() << " location=" << var.location;
    if (var.binding >= 0)
        dbg.nospace() << " binding=" << var.binding;
    if (var.descriptorSet >= 0)
        dbg.nospace() << " set=" << var.descriptorSet;
    if (var.imageFormat != QShaderDescription::ImageFormatUnknown)
        dbg.nospace() << " imageFormat=" << imageFormatStr(var.imageFormat);
    if (var.imageFlags)
        dbg.nospace() << " imageFlags=" << var.imageFlags;
    if (!var.arrayDims.isEmpty())
        dbg.nospace() << " array=" << var.arrayDims;
    if (!var.structMembers.isEmpty())
        dbg.nospace() << " structMembers=" << var.structMembers;
    dbg.nospace() << ')';
    return dbg;
}

QDebug operator<<(QDebug dbg, const QShaderDescription::BlockVariable &var)
{
    QDebugStateSaver saver(dbg);
    dbg.nospace() << "BlockVariable(" << typeStr(var.type) << ' ' << var.name;
    if (var.offset != -1)
        dbg.nospace() << " offset=" << var.offset;
    dbg.nospace() << " size=" << var.size;
    if (!var.arrayDims.isEmpty())
        dbg.nospace() << " array=" << var.arrayDims;
    if (var.arrayStride)
        dbg.nospace() << " arrayStride=" << var.arrayStride;
    if (var.matrixStride)
        dbg.nospace() << " matrixStride=" << var.matrixStride;
    if (var.matrixIsRowMajor)
        dbg.nospace() << " [rowmaj]";
    if (!var.structMembers.isEmpty())
        dbg.nospace() << " structMembers=" << var.structMembers;
    dbg.nospace() << ')';
    return dbg;
}

QDebug operator<<(QDebug dbg, const QShaderDescription::UniformBlock &blk)
{
    QDebugStateSaver saver(dbg);
    dbg.nospace() << "UniformBlock(" << blk.blockName << ' ' << blk.structName
                  << " size=" << blk.size;
    if (blk.binding >= 0)
        dbg.nospace() << " binding=" << blk.binding;
    if (blk.descriptorSet >= 0)
        dbg.nospace() << " set=" << blk.descriptorSet;
    dbg.nospace() << ' ' << blk.members << ')';
    return dbg;
}

QDebug operator<<(QDebug dbg, const QShaderDescription::PushConstantBlock &blk)
{
    QDebugStateSaver saver(dbg);
    dbg.nospace() << "PushConstantBlock(" << blk.name << " size=" << blk.size << ' ' << blk.members
                  << ')';
    return dbg;
}

QDebug operator<<(QDebug dbg, const QShaderDescription::StorageBlock &blk)
{
    QDebugStateSaver saver(dbg);
    dbg.nospace() << "StorageBlock(" << blk.blockName << ' ' << blk.instanceName
                  << " knownSize=" << blk.knownSize;
    if (blk.binding >= 0)
        dbg.nospace() << " binding=" << blk.binding;
    if (blk.descriptorSet >= 0)
        dbg.nospace() << " set=" << blk.descriptorSet;
    if (blk.runtimeArrayStride)
        dbg.nospace() << " runtimeArrayStride=" << blk.runtimeArrayStride;
    if (blk.qualifierFlags)
        dbg.nospace() << " qualifierFlags=" << blk.qualifierFlags;
    dbg.nospace() << ' ' << blk.members << ')';
    return dbg;
}

QDebug operator<<(QDebug dbg, const QShaderDescription::BuiltinVariable &builtin)
{
    QDebugStateSaver saver(dbg);
    dbg.nospace() << "BuiltinVariable(type=" << builtinTypeStr(builtin.type);
    dbg.nospace() << " varType=" << typeStr(builtin.varType);
    if (!builtin.arrayDims.isEmpty())
        dbg.nospace() << " array=" << builtin.arrayDims;
    dbg.nospace() << ")";
    return dbg;
}
#endif

#define JSON_KEY(key) static constexpr QLatin1StringView key ## Key() noexcept { return QLatin1StringView( #key ); }
JSON_KEY(name)
JSON_KEY(type)
JSON_KEY(location)
JSON_KEY(binding)
JSON_KEY(set)
JSON_KEY(perPatch)
JSON_KEY(imageFormat)
JSON_KEY(imageFlags)
JSON_KEY(offset)
JSON_KEY(arrayDims)
JSON_KEY(arrayStride)
JSON_KEY(matrixStride)
JSON_KEY(matrixRowMajor)
JSON_KEY(structMembers)
JSON_KEY(members)
JSON_KEY(inputs)
JSON_KEY(outputs)
JSON_KEY(uniformBlocks)
JSON_KEY(blockName)
JSON_KEY(structName)
JSON_KEY(instanceName)
JSON_KEY(size)
JSON_KEY(knownSize)
JSON_KEY(pushConstantBlocks)
JSON_KEY(storageBlocks)
JSON_KEY(combinedImageSamplers)
JSON_KEY(storageImages)
JSON_KEY(inBuiltins)
JSON_KEY(outBuiltins)
JSON_KEY(computeLocalSize)
JSON_KEY(tessellationOutputVertexCount)
JSON_KEY(tessellationMode)
JSON_KEY(tessellationWindingOrder)
JSON_KEY(tessellationPartitioning)
JSON_KEY(separateImages)
JSON_KEY(separateSamplers)
JSON_KEY(runtimeArrayStride)
JSON_KEY(qualifierFlags)
#undef JSON_KEY

static void addDeco(QJsonObject *obj, const QShaderDescription::InOutVariable &v)
{
    if (v.location >= 0)
        (*obj)[locationKey()] = v.location;
    if (v.binding >= 0)
        (*obj)[bindingKey()] = v.binding;
    if (v.descriptorSet >= 0)
        (*obj)[setKey()] = v.descriptorSet;
    if (v.perPatch)
        (*obj)[perPatchKey()] = v.perPatch;
    if (v.imageFormat != QShaderDescription::ImageFormatUnknown)
        (*obj)[imageFormatKey()] = imageFormatStr(v.imageFormat);
    if (v.imageFlags)
        (*obj)[imageFlagsKey()] = int(v.imageFlags);
    if (!v.arrayDims.isEmpty()) {
        QJsonArray dimArr;
        for (int dim : v.arrayDims)
            dimArr.append(dim);
        (*obj)[arrayDimsKey()] = dimArr;
    }
}

static void serializeDecorations(QDataStream *stream, const QShaderDescription::InOutVariable &v, int version)
{
    (*stream) << v.location;
    (*stream) << v.binding;
    (*stream) << v.descriptorSet;
    (*stream) << int(v.imageFormat);
    (*stream) << int(v.imageFlags);
    (*stream) << int(v.arrayDims.size());
    for (int dim : v.arrayDims)
        (*stream) << dim;
    if (version > QShaderPrivate::QSB_VERSION_WITHOUT_NATIVE_SHADER_INFO)
        (*stream) << quint8(v.perPatch);
}

static void serializeBuiltinVar(QDataStream *stream, const QShaderDescription::BuiltinVariable &v, int version)
{
    (*stream) << int(v.type);
    if (version > QShaderPrivate::QSB_VERSION_WITHOUT_INPUT_OUTPUT_INTERFACE_BLOCKS) {
        (*stream) << int(v.varType);
        (*stream) << int(v.arrayDims.size());
        for (int dim : v.arrayDims)
            (*stream) << dim;
    }
}

static QJsonObject blockMemberObject(const QShaderDescription::BlockVariable &v)
{
    QJsonObject obj;
    obj[nameKey()] = QString::fromUtf8(v.name);
    obj[typeKey()] = typeStr(v.type);
    if (v.offset != -1)
        obj[offsetKey()] = v.offset;
    obj[sizeKey()] = v.size;
    if (!v.arrayDims.isEmpty()) {
        QJsonArray dimArr;
        for (int dim : v.arrayDims)
            dimArr.append(dim);
        obj[arrayDimsKey()] = dimArr;
    }
    if (v.arrayStride)
        obj[arrayStrideKey()] = v.arrayStride;
    if (v.matrixStride)
        obj[matrixStrideKey()] = v.matrixStride;
    if (v.matrixIsRowMajor)
        obj[matrixRowMajorKey()] = true;
    if (!v.structMembers.isEmpty()) {
        QJsonArray arr;
        for (const QShaderDescription::BlockVariable &sv : v.structMembers)
            arr.append(blockMemberObject(sv));
        obj[structMembersKey()] = arr;
    }
    return obj;
}

static QJsonObject inOutObject(const QShaderDescription::InOutVariable &v)
{
    QJsonObject obj;
    obj[nameKey()] = QString::fromUtf8(v.name);
    obj[typeKey()] = typeStr(v.type);
    addDeco(&obj, v);
    if (!v.structMembers.isEmpty()) {
        QJsonArray arr;
        for (const QShaderDescription::BlockVariable &sv : v.structMembers)
            arr.append(blockMemberObject(sv));
        obj[structMembersKey()] = arr;
    }
    return obj;
}

static QJsonObject builtinObject(const QShaderDescription::BuiltinVariable &v)
{
    QJsonObject obj;

    obj[nameKey()] = builtinTypeStr(v.type);
    obj[typeKey()] = typeStr(v.varType);
    if (!v.arrayDims.isEmpty()) {
        QJsonArray dimArr;
        for (int dim : v.arrayDims)
            dimArr.append(dim);
        obj[arrayDimsKey()] = dimArr;
    }
    return obj;
}

static void serializeBlockMemberVar(QDataStream *stream, const QShaderDescription::BlockVariable &v)
{
    (*stream) << QString::fromUtf8(v.name);
    (*stream) << int(v.type);
    (*stream) << v.offset;
    (*stream) << v.size;
    (*stream) << int(v.arrayDims.size());
    for (int dim : v.arrayDims)
        (*stream) << dim;
    (*stream) << v.arrayStride;
    (*stream) << v.matrixStride;
    (*stream) << v.matrixIsRowMajor;
    (*stream) << int(v.structMembers.size());
    for (const QShaderDescription::BlockVariable &sv : v.structMembers)
        serializeBlockMemberVar(stream, sv);
}

static void serializeInOutVar(QDataStream *stream, const QShaderDescription::InOutVariable &v,
                              int version)
{
    (*stream) << QString::fromUtf8(v.name);
    (*stream) << int(v.type);
    serializeDecorations(stream, v, version);
    if (version > QShaderPrivate::QSB_VERSION_WITHOUT_INPUT_OUTPUT_INTERFACE_BLOCKS) {
        (*stream) << int(v.structMembers.size());
        for (const QShaderDescription::BlockVariable &sv : v.structMembers)
            serializeBlockMemberVar(stream, sv);
    }
}

QJsonDocument QShaderDescriptionPrivate::makeDoc()
{
    QJsonObject root;

    QJsonArray jinputs;
    for (const QShaderDescription::InOutVariable &v : std::as_const(inVars))
        jinputs.append(inOutObject(v));
    if (!jinputs.isEmpty())
        root[inputsKey()] = jinputs;

    QJsonArray joutputs;
    for (const QShaderDescription::InOutVariable &v : std::as_const(outVars))
        joutputs.append(inOutObject(v));
    if (!joutputs.isEmpty())
        root[outputsKey()] = joutputs;

    QJsonArray juniformBlocks;
    for (const QShaderDescription::UniformBlock &b : uniformBlocks) {
        QJsonObject juniformBlock;
        juniformBlock[blockNameKey()] = QString::fromUtf8(b.blockName);
        juniformBlock[structNameKey()] = QString::fromUtf8(b.structName);
        juniformBlock[sizeKey()] = b.size;
        if (b.binding >= 0)
            juniformBlock[bindingKey()] = b.binding;
        if (b.descriptorSet >= 0)
            juniformBlock[setKey()] = b.descriptorSet;
        QJsonArray members;
        for (const QShaderDescription::BlockVariable &v : b.members)
            members.append(blockMemberObject(v));
        juniformBlock[membersKey()] = members;
        juniformBlocks.append(juniformBlock);
    }
    if (!juniformBlocks.isEmpty())
        root[uniformBlocksKey()] = juniformBlocks;

    QJsonArray jpushConstantBlocks;
    for (const QShaderDescription::PushConstantBlock &b : pushConstantBlocks) {
        QJsonObject jpushConstantBlock;
        jpushConstantBlock[nameKey()] = QString::fromUtf8(b.name);
        jpushConstantBlock[sizeKey()] = b.size;
        QJsonArray members;
        for (const QShaderDescription::BlockVariable &v : b.members)
            members.append(blockMemberObject(v));
        jpushConstantBlock[membersKey()] = members;
        jpushConstantBlocks.append(jpushConstantBlock);
    }
    if (!jpushConstantBlocks.isEmpty())
        root[pushConstantBlocksKey()] = jpushConstantBlocks;

    QJsonArray jstorageBlocks;
    for (const QShaderDescription::StorageBlock &b : storageBlocks) {
        QJsonObject jstorageBlock;
        jstorageBlock[blockNameKey()] = QString::fromUtf8(b.blockName);
        jstorageBlock[instanceNameKey()] = QString::fromUtf8(b.instanceName);
        jstorageBlock[knownSizeKey()] = b.knownSize;
        if (b.binding >= 0)
            jstorageBlock[bindingKey()] = b.binding;
        if (b.descriptorSet >= 0)
            jstorageBlock[setKey()] = b.descriptorSet;
        if (b.runtimeArrayStride)
            jstorageBlock[runtimeArrayStrideKey()] = b.runtimeArrayStride;
        if (b.qualifierFlags)
            jstorageBlock[qualifierFlagsKey()] = int(b.qualifierFlags);
        QJsonArray members;
        for (const QShaderDescription::BlockVariable &v : b.members)
            members.append(blockMemberObject(v));
        jstorageBlock[membersKey()] = members;
        jstorageBlocks.append(jstorageBlock);
    }
    if (!jstorageBlocks.isEmpty())
        root[storageBlocksKey()] = jstorageBlocks;

    QJsonArray jcombinedSamplers;
    for (const QShaderDescription::InOutVariable &v : std::as_const(combinedImageSamplers)) {
        QJsonObject sampler;
        sampler[nameKey()] = QString::fromUtf8(v.name);
        sampler[typeKey()] = typeStr(v.type);
        addDeco(&sampler, v);
        jcombinedSamplers.append(sampler);
    }
    if (!jcombinedSamplers.isEmpty())
        root[combinedImageSamplersKey()] = jcombinedSamplers;

    QJsonArray jstorageImages;
    for (const QShaderDescription::InOutVariable &v : std::as_const(storageImages)) {
        QJsonObject image;
        image[nameKey()] = QString::fromUtf8(v.name);
        image[typeKey()] = typeStr(v.type);
        addDeco(&image, v);
        jstorageImages.append(image);
    }
    if (!jstorageImages.isEmpty())
        root[storageImagesKey()] = jstorageImages;

    QJsonArray jinBuiltins;
    for (const QShaderDescription::BuiltinVariable &v : std::as_const(inBuiltins))
        jinBuiltins.append(builtinObject(v));
    if (!jinBuiltins.isEmpty())
        root[inBuiltinsKey()] = jinBuiltins;

    QJsonArray joutBuiltins;
    for (const QShaderDescription::BuiltinVariable &v : std::as_const(outBuiltins))
        joutBuiltins.append(builtinObject(v));
    if (!joutBuiltins.isEmpty())
        root[outBuiltinsKey()] = joutBuiltins;

    if (localSize[0] || localSize[1] || localSize[2]) {
        QJsonArray jlocalSize;
        for (size_t i = 0; i < 3; ++i)
            jlocalSize.append(QJsonValue(int(localSize[i])));
        root[computeLocalSizeKey()] = jlocalSize;
    }

    if (tessOutVertCount)
        root[tessellationOutputVertexCountKey()] = int(tessOutVertCount);

    if (tessMode != QShaderDescription::UnknownTessellationMode)
        root[tessellationModeKey()] = tessModeStr(tessMode);

    if (tessWind != QShaderDescription::UnknownTessellationWindingOrder)
        root[tessellationWindingOrderKey()] = tessWindStr(tessWind);

    if (tessPart != QShaderDescription::UnknownTessellationPartitioning)
        root[tessellationPartitioningKey()] = tessPartStr(tessPart);

    QJsonArray jseparateImages;
    for (const QShaderDescription::InOutVariable &v : std::as_const(separateImages)) {
        QJsonObject image;
        image[nameKey()] = QString::fromUtf8(v.name);
        image[typeKey()] = typeStr(v.type);
        addDeco(&image, v);
        jseparateImages.append(image);
    }
    if (!jseparateImages.isEmpty())
        root[separateImagesKey()] = jseparateImages;

    QJsonArray jseparateSamplers;
    for (const QShaderDescription::InOutVariable &v : std::as_const(separateSamplers)) {
        QJsonObject sampler;
        sampler[nameKey()] = QString::fromUtf8(v.name);
        sampler[typeKey()] = typeStr(v.type);
        addDeco(&sampler, v);
        jseparateSamplers.append(sampler);
    }
    if (!jseparateSamplers.isEmpty())
        root[separateSamplersKey()] = jseparateSamplers;

    return QJsonDocument(root);
}

void QShaderDescriptionPrivate::writeToStream(QDataStream *stream, int version)
{
    (*stream) << int(inVars.size());
    for (const QShaderDescription::InOutVariable &v : std::as_const(inVars))
        serializeInOutVar(stream, v, version);

    (*stream) << int(outVars.size());
    for (const QShaderDescription::InOutVariable &v : std::as_const(outVars))
        serializeInOutVar(stream, v, version);

    (*stream) << int(uniformBlocks.size());
    for (const QShaderDescription::UniformBlock &b : uniformBlocks) {
        (*stream) << QString::fromUtf8(b.blockName);
        (*stream) << QString::fromUtf8(b.structName);
        (*stream) << b.size;
        (*stream) << b.binding;
        (*stream) << b.descriptorSet;
        (*stream) << int(b.members.size());
        for (const QShaderDescription::BlockVariable &v : b.members)
            serializeBlockMemberVar(stream, v);
    }

    (*stream) << int(pushConstantBlocks.size());
    for (const QShaderDescription::PushConstantBlock &b : pushConstantBlocks) {
        (*stream) << QString::fromUtf8(b.name);
        (*stream) << b.size;
        (*stream) << int(b.members.size());
        for (const QShaderDescription::BlockVariable &v : b.members)
            serializeBlockMemberVar(stream, v);
    }

    (*stream) << int(storageBlocks.size());
    for (const QShaderDescription::StorageBlock &b : storageBlocks) {
        (*stream) << QString::fromUtf8(b.blockName);
        (*stream) << QString::fromUtf8(b.instanceName);
        (*stream) << b.knownSize;
        (*stream) << b.binding;
        (*stream) << b.descriptorSet;
        (*stream) << int(b.members.size());
        for (const QShaderDescription::BlockVariable &v : b.members)
            serializeBlockMemberVar(stream, v);
        if (version > QShaderPrivate::QSB_VERSION_WITHOUT_EXTENDED_STORAGE_BUFFER_INFO) {
            (*stream) << b.runtimeArrayStride;
            (*stream) << b.qualifierFlags;
        }
    }

    (*stream) << int(combinedImageSamplers.size());
    for (const QShaderDescription::InOutVariable &v : std::as_const(combinedImageSamplers)) {
        (*stream) << QString::fromUtf8(v.name);
        (*stream) << int(v.type);
        serializeDecorations(stream, v, version);
    }

    (*stream) << int(storageImages.size());
    for (const QShaderDescription::InOutVariable &v : std::as_const(storageImages)) {
        (*stream) << QString::fromUtf8(v.name);
        (*stream) << int(v.type);
        serializeDecorations(stream, v, version);
    }

    for (size_t i = 0; i < 3; ++i)
        (*stream) << quint32(localSize[i]);

    (*stream) << int(separateImages.size());
    for (const QShaderDescription::InOutVariable &v : std::as_const(separateImages)) {
        (*stream) << QString::fromUtf8(v.name);
        (*stream) << int(v.type);
        serializeDecorations(stream, v, version);
    }

    (*stream) << int(separateSamplers.size());
    for (const QShaderDescription::InOutVariable &v : std::as_const(separateSamplers)) {
        (*stream) << QString::fromUtf8(v.name);
        (*stream) << int(v.type);
        serializeDecorations(stream, v, version);
    }

    if (version > QShaderPrivate::QSB_VERSION_WITHOUT_NATIVE_SHADER_INFO) {
        (*stream) << quint32(tessOutVertCount);
        (*stream) << quint32(tessMode);
        (*stream) << quint32(tessWind);
        (*stream) << quint32(tessPart);

        (*stream) << int(inBuiltins.size());
        for (const QShaderDescription::BuiltinVariable &v : std::as_const(inBuiltins))
            serializeBuiltinVar(stream, v, version);

        (*stream) << int(outBuiltins.size());
        for (const QShaderDescription::BuiltinVariable &v : std::as_const(outBuiltins))
            serializeBuiltinVar(stream, v, version);
    }
}

static void deserializeDecorations(QDataStream *stream, int version, QShaderDescription::InOutVariable *v)
{
    (*stream) >> v->location;
    (*stream) >> v->binding;
    (*stream) >> v->descriptorSet;
    int f;
    (*stream) >> f;
    v->imageFormat = QShaderDescription::ImageFormat(f);
    (*stream) >> f;
    v->imageFlags = QShaderDescription::ImageFlags(f);

    if (version > QShaderPrivate::QSB_VERSION_WITHOUT_VAR_ARRAYDIMS) {
        (*stream) >> f;
        v->arrayDims.resize(f);
        for (int i = 0; i < f; ++i)
            (*stream) >> v->arrayDims[i];
    }

    if (version > QShaderPrivate::QSB_VERSION_WITHOUT_NATIVE_SHADER_INFO) {
        quint8 b;
        (*stream) >> b;
        v->perPatch = b;
    }
}

static QShaderDescription::BuiltinVariable deserializeBuiltinVar(QDataStream *stream, int version)
{
    QShaderDescription::BuiltinVariable var;
    int t;
    (*stream) >> t;
    var.type = QShaderDescription::BuiltinType(t);
    if (version > QShaderPrivate::QSB_VERSION_WITHOUT_INPUT_OUTPUT_INTERFACE_BLOCKS) {
        (*stream) >> t;
        var.varType = QShaderDescription::VariableType(t);
        int count;
        (*stream) >> count;
        var.arrayDims.resize(count);
        for (int i = 0; i < count; ++i)
            (*stream) >> var.arrayDims[i];
    }
    return var;
}

static QShaderDescription::BlockVariable deserializeBlockMemberVar(QDataStream *stream, int version)
{
    QShaderDescription::BlockVariable var;
    QString tmp;
    (*stream) >> tmp;
    var.name = tmp.toUtf8();
    int t;
    (*stream) >> t;
    var.type = QShaderDescription::VariableType(t);
    (*stream) >> var.offset;
    (*stream) >> var.size;
    int count;
    (*stream) >> count;
    var.arrayDims.resize(count);
    for (int i = 0; i < count; ++i)
        (*stream) >> var.arrayDims[i];
    (*stream) >> var.arrayStride;
    (*stream) >> var.matrixStride;
    (*stream) >> var.matrixIsRowMajor;
    (*stream) >> count;
    var.structMembers.resize(count);
    for (int i = 0; i < count; ++i)
        var.structMembers[i] = deserializeBlockMemberVar(stream, version);
    return var;
}

static QShaderDescription::InOutVariable deserializeInOutVar(QDataStream *stream, int version)
{
    QShaderDescription::InOutVariable var;
    QString tmp;
    (*stream) >> tmp;
    var.name = tmp.toUtf8();
    int t;
    (*stream) >> t;
    var.type = QShaderDescription::VariableType(t);
    deserializeDecorations(stream, version, &var);
    if (version > QShaderPrivate::QSB_VERSION_WITHOUT_INPUT_OUTPUT_INTERFACE_BLOCKS) {
        int count;
        (*stream) >> count;
        var.structMembers.resize(count);
        for (int i = 0; i < count; ++i)
            var.structMembers[i] = deserializeBlockMemberVar(stream, version);
    }
    return var;
}

void QShaderDescriptionPrivate::loadFromStream(QDataStream *stream, int version)
{
    Q_ASSERT(ref.loadRelaxed() == 1); // must be detached

    int count;
    (*stream) >> count;
    inVars.resize(count);
    for (int i = 0; i < count; ++i)
        inVars[i] = deserializeInOutVar(stream, version);

    (*stream) >> count;
    outVars.resize(count);
    for (int i = 0; i < count; ++i)
        outVars[i] = deserializeInOutVar(stream, version);

    (*stream) >> count;
    uniformBlocks.resize(count);
    for (int i = 0; i < count; ++i) {
        QString tmp;
        (*stream) >> tmp;
        uniformBlocks[i].blockName = tmp.toUtf8();
        (*stream) >> tmp;
        uniformBlocks[i].structName = tmp.toUtf8();
        (*stream) >> uniformBlocks[i].size;
        (*stream) >> uniformBlocks[i].binding;
        (*stream) >> uniformBlocks[i].descriptorSet;
        int memberCount;
        (*stream) >> memberCount;
        uniformBlocks[i].members.resize(memberCount);
        for (int memberIdx = 0; memberIdx < memberCount; ++memberIdx)
            uniformBlocks[i].members[memberIdx] = deserializeBlockMemberVar(stream, version);
    }

    (*stream) >> count;
    pushConstantBlocks.resize(count);
    for (int i = 0; i < count; ++i) {
        QString tmp;
        (*stream) >> tmp;
        pushConstantBlocks[i].name = tmp.toUtf8();
        (*stream) >> pushConstantBlocks[i].size;
        int memberCount;
        (*stream) >> memberCount;
        pushConstantBlocks[i].members.resize(memberCount);
        for (int memberIdx = 0; memberIdx < memberCount; ++memberIdx)
            pushConstantBlocks[i].members[memberIdx] = deserializeBlockMemberVar(stream, version);
    }

    (*stream) >> count;
    storageBlocks.resize(count);
    for (int i = 0; i < count; ++i) {
        QString tmp;
        (*stream) >> tmp;
        storageBlocks[i].blockName = tmp.toUtf8();
        (*stream) >> tmp;
        storageBlocks[i].instanceName = tmp.toUtf8();
        (*stream) >> storageBlocks[i].knownSize;
        (*stream) >> storageBlocks[i].binding;
        (*stream) >> storageBlocks[i].descriptorSet;
        int memberCount;
        (*stream) >> memberCount;
        storageBlocks[i].members.resize(memberCount);
        for (int memberIdx = 0; memberIdx < memberCount; ++memberIdx)
            storageBlocks[i].members[memberIdx] = deserializeBlockMemberVar(stream, version);

        if (version > QShaderPrivate::QSB_VERSION_WITHOUT_EXTENDED_STORAGE_BUFFER_INFO) {
            (*stream) >> storageBlocks[i].runtimeArrayStride;
            (*stream) >> storageBlocks[i].qualifierFlags;
        }
    }

    (*stream) >> count;
    combinedImageSamplers.resize(count);
    for (int i = 0; i < count; ++i) {
        QString tmp;
        (*stream) >> tmp;
        combinedImageSamplers[i].name = tmp.toUtf8();
        int t;
        (*stream) >> t;
        combinedImageSamplers[i].type = QShaderDescription::VariableType(t);
        deserializeDecorations(stream, version, &combinedImageSamplers[i]);
    }

    (*stream) >> count;
    storageImages.resize(count);
    for (int i = 0; i < count; ++i) {
        QString tmp;
        (*stream) >> tmp;
        storageImages[i].name = tmp.toUtf8();
        int t;
        (*stream) >> t;
        storageImages[i].type = QShaderDescription::VariableType(t);
        deserializeDecorations(stream, version, &storageImages[i]);
    }

    for (size_t i = 0; i < 3; ++i) {
        quint32 v;
        (*stream) >> v;
        localSize[i] = v;
    }

    if (version > QShaderPrivate::QSB_VERSION_WITHOUT_SEPARATE_IMAGES_AND_SAMPLERS) {
        (*stream) >> count;
        separateImages.resize(count);
        for (int i = 0; i < count; ++i) {
            QString tmp;
            (*stream) >> tmp;
            separateImages[i].name = tmp.toUtf8();
            int t;
            (*stream) >> t;
            separateImages[i].type = QShaderDescription::VariableType(t);
            deserializeDecorations(stream, version, &separateImages[i]);
        }

        (*stream) >> count;
        separateSamplers.resize(count);
        for (int i = 0; i < count; ++i) {
            QString tmp;
            (*stream) >> tmp;
            separateSamplers[i].name = tmp.toUtf8();
            int t;
            (*stream) >> t;
            separateSamplers[i].type = QShaderDescription::VariableType(t);
            deserializeDecorations(stream, version, &separateSamplers[i]);
        }
    }

    if (version > QShaderPrivate::QSB_VERSION_WITHOUT_NATIVE_SHADER_INFO) {
        quint32 v;
        (*stream) >> v;
        tessOutVertCount = v;
        (*stream) >> v;
        tessMode = QShaderDescription::TessellationMode(v);
        (*stream) >> v;
        tessWind = QShaderDescription::TessellationWindingOrder(v);
        (*stream) >> v;
        tessPart = QShaderDescription::TessellationPartitioning(v);

        (*stream) >> count;
        inBuiltins.resize(count);
        for (int i = 0; i < count; ++i)
            inBuiltins[i] = deserializeBuiltinVar(stream, version);

        (*stream) >> count;
        outBuiltins.resize(count);
        for (int i = 0; i < count; ++i)
            outBuiltins[i] = deserializeBuiltinVar(stream, version);
    }
}

/*!
    Returns \c true if the two QShaderDescription objects \a lhs and \a rhs are
    equal.

    \relates QShaderDescription
 */
bool operator==(const QShaderDescription &lhs, const QShaderDescription &rhs) noexcept
{
    if (lhs.d == rhs.d)
        return true;

    return lhs.d->inVars == rhs.d->inVars
            && lhs.d->outVars == rhs.d->outVars
            && lhs.d->uniformBlocks == rhs.d->uniformBlocks
            && lhs.d->pushConstantBlocks == rhs.d->pushConstantBlocks
            && lhs.d->storageBlocks == rhs.d->storageBlocks
            && lhs.d->combinedImageSamplers == rhs.d->combinedImageSamplers
            && lhs.d->separateImages == rhs.d->separateImages
            && lhs.d->separateSamplers == rhs.d->separateSamplers
            && lhs.d->storageImages == rhs.d->storageImages
            && lhs.d->inBuiltins == rhs.d->inBuiltins
            && lhs.d->outBuiltins == rhs.d->outBuiltins
            && lhs.d->localSize == rhs.d->localSize
            && lhs.d->tessOutVertCount == rhs.d->tessOutVertCount
            && lhs.d->tessMode == rhs.d->tessMode
            && lhs.d->tessWind == rhs.d->tessWind
            && lhs.d->tessPart == rhs.d->tessPart;
}

/*!
    Returns \c true if the two InOutVariable objects \a lhs and \a rhs are
    equal.

    \relates QShaderDescription::InOutVariable
 */
bool operator==(const QShaderDescription::InOutVariable &lhs, const QShaderDescription::InOutVariable &rhs) noexcept
{
    return lhs.name == rhs.name
            && lhs.type == rhs.type
            && lhs.location == rhs.location
            && lhs.binding == rhs.binding
            && lhs.descriptorSet == rhs.descriptorSet
            && lhs.imageFormat == rhs.imageFormat
            && lhs.imageFlags == rhs.imageFlags
            && lhs.arrayDims == rhs.arrayDims
            && lhs.perPatch == rhs.perPatch
            && lhs.structMembers == rhs.structMembers;
}

/*!
    Returns \c true if the two BlockVariable objects \a lhs and \a rhs are
    equal.

    \relates QShaderDescription::BlockVariable
 */
bool operator==(const QShaderDescription::BlockVariable &lhs, const QShaderDescription::BlockVariable &rhs) noexcept
{
    return lhs.name == rhs.name
            && lhs.type == rhs.type
            && lhs.offset == rhs.offset
            && lhs.size == rhs.size
            && lhs.arrayDims == rhs.arrayDims
            && lhs.arrayStride == rhs.arrayStride
            && lhs.matrixStride == rhs.matrixStride
            && lhs.matrixIsRowMajor == rhs.matrixIsRowMajor
            && lhs.structMembers == rhs.structMembers;
}

/*!
    Returns \c true if the two UniformBlock objects \a lhs and \a rhs are
    equal.

    \relates QShaderDescription::UniformBlock
 */
bool operator==(const QShaderDescription::UniformBlock &lhs, const QShaderDescription::UniformBlock &rhs) noexcept
{
    return lhs.blockName == rhs.blockName
            && lhs.structName == rhs.structName
            && lhs.size == rhs.size
            && lhs.binding == rhs.binding
            && lhs.descriptorSet == rhs.descriptorSet
            && lhs.members == rhs.members;
}

/*!
    Returns \c true if the two PushConstantBlock objects \a lhs and \a rhs are
    equal.

    \relates QShaderDescription::PushConstantBlock
 */
bool operator==(const QShaderDescription::PushConstantBlock &lhs, const QShaderDescription::PushConstantBlock &rhs) noexcept
{
    return lhs.name == rhs.name
            && lhs.size == rhs.size
            && lhs.members == rhs.members;
}

/*!
    Returns \c true if the two StorageBlock objects \a lhs and \a rhs are
    equal.

    \relates QShaderDescription::StorageBlock
 */
bool operator==(const QShaderDescription::StorageBlock &lhs, const QShaderDescription::StorageBlock &rhs) noexcept
{
    return lhs.blockName == rhs.blockName
            && lhs.instanceName == rhs.instanceName
            && lhs.knownSize == rhs.knownSize
            && lhs.binding == rhs.binding
            && lhs.descriptorSet == rhs.descriptorSet
            && lhs.runtimeArrayStride == rhs.runtimeArrayStride
            && lhs.qualifierFlags == rhs.qualifierFlags
            && lhs.members == rhs.members;
}

/*!
    Returns \c true if the two BuiltinVariable objects \a lhs and \a rhs are
    equal.

    \relates QShaderDescription::BuiltinVariable
 */
bool operator==(const QShaderDescription::BuiltinVariable &lhs, const QShaderDescription::BuiltinVariable &rhs) noexcept
{
    return lhs.type == rhs.type
            && lhs.varType == rhs.varType
            && lhs.arrayDims == rhs.arrayDims;
}

QT_END_NAMESPACE