summaryrefslogtreecommitdiffstats
path: root/src/render/materialsystem/qshaderprogram.cpp
blob: d14fc5e6432aabb48345699bd4d0a994f8af664b (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
// Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qshaderprogram.h"
#include "qshaderprogram_p.h"
#include <Qt3DCore/private/qurlhelper_p.h>
#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QUrl>
#include <QRegularExpression>

/*!
    \class Qt3DRender::QShaderProgram
    \inmodule Qt3DRender
    \brief Encapsulates a Shader Program.
    \inherits Qt3DCore::QNode
    \since 5.5

    A shader program consists of several different shaders, such as vertex and fragment shaders.

    Qt3D will automatically populate a set of default uniforms if they are
    encountered during the shader instrospection phase.

    \table
    \header
        \li Default Uniform
        \li Associated Qt3D Parameter name
        \li GLSL declaration

    \row
        \li ModelMatrix
        \li modelMatrix
        \li uniform mat4 modelMatrix;

    \row
        \li ViewMatrix
        \li viewMatrix
        \li uniform mat4 viewMatrix;

    \row
        \li ProjectionMatrix
        \li projectionMatrix
        \li uniform mat4 projectionMatrix;

    \row
        \li ModelViewMatrix
        \li modelView
        \li uniform mat4 modelView;

    \row
        \li ViewProjectionMatrix
        \li viewProjectionMatrix
        \li uniform mat4 viewProjectionMatrix;

    \row
        \li ModelViewProjectionMatrix
        \li modelViewProjection \br mvp
        \li uniform mat4 modelViewProjection; \br uniform mat4 mvp;

    \row
        \li InverseModelMatrix
        \li inverseModelMatrix
        \li uniform mat4 inverseModelMatrix;

    \row
        \li InverseViewMatrix
        \li inverseViewMatrix
        \li uniform mat4 inverseViewMatrix;

    \row
        \li InverseProjectionMatrix
        \li inverseProjectionMatrix
        \li uniform mat4 inverseProjectionMatrix;

    \row
        \li InverseModelViewMatrix
        \li inverseModelView
        \li uniform mat4 inverseModelView;

    \row
        \li InverseViewProjectionMatrix
        \li inverseViewProjectionMatrix
        \li uniform mat4 inverseViewProjectionMatrix;

    \row
        \li InverseModelViewProjectionMatrix
        \li inverseModelViewProjection
        \li uniform mat4 inverseModelViewProjection;

    \row
        \li ModelNormalMatrix
        \li modelNormalMatrix
        \li uniform mat3 modelNormalMatrix;

    \row
        \li ModelViewNormalMatrix
        \li modelViewNormal
        \li uniform mat3 modelViewNormal;

    \row
        \li ViewportMatrix
        \li viewportMatrix
        \li uniform mat4 viewportMatrix;

    \row
        \li InverseViewportMatrix
        \li inverseViewportMatrix
        \li uniform mat4 inverseViewportMatrix;

    \row
        \li AspectRatio \br (surface width / surface height)
        \li aspectRatio
        \li uniform float aspectRatio;

    \row
        \li Exposure
        \li exposure
        \li uniform float exposure;

    \row
        \li Gamma
        \li gamma
        \li uniform float gamma;

    \row
        \li Time \br (in nano seconds)
        \li time
        \li uniform float time;

    \row
        \li EyePosition
        \li eyePosition
        \li uniform vec3 eyePosition;

    \row
        \li SkinningPalette
        \li skinningPalette[0]
        \li const int maxJoints = 100; \br uniform mat4 skinningPalette[maxJoints];

    \endtable

    \section1 RHI Support

    When writing GLSL 450 shader code to use with Qt 3D's RHI backend,
    the default uniforms will be provided as 2 uniform buffer objects.

    The binding locations for these is set to bindings 0 for RenderView
    uniforms and 1 for Command uniforms.

    \badcode
    #version 450 core

    layout(location = 0) in vec3 vertexPosition;

    layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
      mat4 viewMatrix;
      mat4 projectionMatrix;
      mat4 uncorrectedProjectionMatrix;
      mat4 clipCorrectionMatrix;
      mat4 viewProjectionMatrix;
      mat4 inverseViewMatrix;
      mat4 inverseProjectionMatrix;
      mat4 inverseViewProjectionMatrix;
      mat4 viewportMatrix;
      mat4 inverseViewportMatrix;
      vec4 textureTransformMatrix;
      vec3 eyePosition;
      float aspectRatio;
      float gamma;
      float exposure;
      float time;
      float yUpInNDC;
      float yUpInFBO;
    };

    layout(std140, binding = 1) uniform qt3d_command_uniforms {
      mat4 modelMatrix;
      mat4 inverseModelMatrix;
      mat4 modelViewMatrix;
      mat3 modelNormalMatrix;
      mat4 inverseModelViewMatrix;
      mat4 modelViewProjection;
      mat4 inverseModelViewProjectionMatrix;
    };

    void main()
    {
        gl_Position = (projectionMatrix * viewMatrix * modelMatrix * vertexPosition);
    }
    \endcode

    For user defined uniform buffer object, use binding starting at 2 or auto
    to let Qt 3D work out the binding automatically. Make sure to remain
    consistent between the different shader stages.


    \badcode
    #version 450 core

    layout(std140, binding = auto) uniform my_uniforms {
      vec4 myColor;
    };

    layout(location=0) out vec4 fragColor;

    void main()
    {
        fragColor = myColor;
    }
    \endcode

    There is no change involved when it comes to feeding values to uniforms.

    For the above example, setting myColor could be done with:

    \badcode
    QParameter *parameter = new QParameter();
    parameter->setName("myColor");
    parameter->setValue(QVariant::fromValue(QColor(Qt::blue)));
    \endcode

    Textures still have to be defined as standalone uniforms.

    \badcode
    #version 450 core

    layout(binding=0) uniform sampler2D source;

    layout(location=0) out vec4 fragColor;

    void main()
    {
        fragColor = texture(source, vec2(0.5, 0.5));
    }
    \endcode
*/

/*!
    \qmltype ShaderProgram
    \instantiates Qt3DRender::QShaderProgram
    \inqmlmodule Qt3D.Render
    \brief Encapsulates a Shader Program.
    \since 5.5

    ShaderProgram class encapsulates a shader program. A shader program consists of several
    different shaders, such as vertex and fragment shaders.

    Qt3D will automatically populate a set of default uniforms if they are
    encountered during the shader instrospection phase.

    \table
    \header
        \li {1, 1} Default Uniform
        \li {2, 1} Associated Qt3D Parameter name
        \li {3, 1} GLSL declaration

    \row
        \li {1, 1} ModelMatrix
        \li {2, 1} modelMatrix
        \li {3, 1} uniform mat4 modelMatrix;

    \row
        \li {1, 1} ViewMatrix
        \li {2, 1} viewMatrix
        \li {3, 1} uniform mat4 viewMatrix;

    \row
        \li {1, 1} ProjectionMatrix
        \li {2, 1} projectionMatrix
        \li {3, 1} uniform mat4 projectionMatrix;

    \row
        \li {1, 1} ModelViewMatrix
        \li {2, 1} modelView
        \li {3, 1} uniform mat4 modelView;

    \row
        \li {1, 1} ViewProjectionMatrix
        \li {2, 1} viewProjectionMatrix
        \li {3, 1} uniform mat4 viewProjectionMatrix;

    \row
        \li {1, 1} ModelViewProjectionMatrix
        \li {2, 1} modelViewProjection \br mvp
        \li {3, 1} uniform mat4 modelViewProjection; \br uniform mat4 mvp;

    \row
        \li {1, 1} InverseModelMatrix
        \li {2, 1} inverseModelMatrix
        \li {3, 1} uniform mat4 inverseModelMatrix;

    \row
        \li {1, 1} InverseViewMatrix
        \li {2, 1} inverseViewMatrix
        \li {3, 1} uniform mat4 inverseViewMatrix;

    \row
        \li {1, 1} InverseProjectionMatrix
        \li {2, 1} inverseProjectionMatrix
        \li {3, 1} uniform mat4 inverseProjectionMatrix;

    \row
        \li {1, 1} InverseModelViewMatrix
        \li {2, 1} inverseModelView
        \li {3, 1} uniform mat4 inverseModelView;

    \row
        \li {1, 1} InverseViewProjectionMatrix
        \li {2, 1} inverseViewProjectionMatrix
        \li {3, 1} uniform mat4 inverseViewProjectionMatrix;

    \row
        \li {1, 1} InverseModelViewProjectionMatrix
        \li {2, 1} inverseModelViewProjection
        \li {3, 1} uniform mat4 inverseModelViewProjection;

    \row
        \li {1, 1} ModelNormalMatrix
        \li {2, 1} modelNormalMatrix
        \li {3, 1} uniform mat3 modelNormalMatrix;

    \row
        \li {1, 1} ModelViewNormalMatrix
        \li {2, 1} modelViewNormal
        \li {3, 1} uniform mat3 modelViewNormal;

    \row
        \li {1, 1} ViewportMatrix
        \li {2, 1} viewportMatrix
        \li {3, 1} uniform mat4 viewportMatrix;

    \row
        \li {1, 1} InverseViewportMatrix
        \li {2, 1} inverseViewportMatrix
        \li {3, 1} uniform mat4 inverseViewportMatrix;

    \row
        \li {1, 1} AspectRatio \br (surface width / surface height)
        \li {2, 1} aspectRatio
        \li {3, 1} uniform float aspectRatio;

    \row
        \li {1, 1} Exposure
        \li {2, 1} exposure
        \li {3, 1} uniform float exposure;

    \row
        \li {1, 1} Gamma
        \li {2, 1} gamma
        \li {3, 1} uniform float gamma;

    \row
        \li {1, 1} Time \br (in nano seconds)
        \li {2, 1} time
        \li {3, 1} uniform float time;

    \row
        \li {1, 1} EyePosition
        \li {2, 1} eyePosition
        \li {3, 1} uniform vec3 eyePosition;

    \row
        \li {1, 1} SkinningPalette
        \li {2, 1} skinningPalette[0]
        \li {3, 1} const int maxJoints = 100; \br uniform mat4 skinningPalette[maxJoints];

    \endtable

    \section1 RHI Support

    When writing GLSL 450 shader code to use with Qt 3D's RHI backend,
    the default uniforms will be provided as 2 uniform buffer objects.

    The binding locations for these is set to bindings 0 for RenderView
    uniforms and 1 for Command uniforms.

    \badcode
    #version 450 core

    layout(location = 0) in vec3 vertexPosition;

    layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
      mat4 viewMatrix;
      mat4 projectionMatrix;
      mat4 uncorrectedProjectionMatrix;
      mat4 clipCorrectionMatrix;
      mat4 viewProjectionMatrix;
      mat4 inverseViewMatrix;
      mat4 inverseProjectionMatrix;
      mat4 inverseViewProjectionMatrix;
      mat4 viewportMatrix;
      mat4 inverseViewportMatrix;
      vec4 textureTransformMatrix;
      vec3 eyePosition;
      float aspectRatio;
      float gamma;
      float exposure;
      float time;
      float yUpInNDC;
      float yUpInFBO;
    };

    layout(std140, binding = 1) uniform qt3d_command_uniforms {
      mat4 modelMatrix;
      mat4 inverseModelMatrix;
      mat4 modelViewMatrix;
      mat3 modelNormalMatrix;
      mat4 inverseModelViewMatrix;
      mat4 modelViewProjection;
      mat4 inverseModelViewProjectionMatrix;
    };

    void main()
    {
        gl_Position = (projectionMatrix * viewMatrix * modelMatrix * vertexPosition);
    }
    \endcode

    For user defined uniform buffer object, use binding starting at 2 or auto
    to let Qt 3D work out the binding automatically. Make sure to remain
    consistent between the different shader stages.


    \badcode
    #version 450 core

    layout(std140, binding = auto) uniform my_uniforms {
      vec4 myColor;
    };

    layout(location=0) out vec4 fragColor;

    void main()
    {
        fragColor = myColor;
    }
    \endcode

    There is no change involved when it comes to feeding values to uniforms.

    For the above example, setting myColor could be done with:

    \badcode
    Parameter { name: "myColor"; value: "blue" }
    \endcode

    Textures still have to be defined as standalone uniforms.

    \badcode
    #version 450 core

    layout(binding=0) uniform sampler2D source;

    layout(location=0) out vec4 fragColor;

    void main()
    {
        fragColor = texture(source, vec2(0.5, 0.5));
    }
    \endcode
*/

/*!
    \enum QShaderProgram::ShaderType

    This enum identifies the type of shader used.

    \value Vertex                  Vertex shader
    \value Fragment                Fragment shader
    \value TessellationControl     Tesselation control shader
    \value TessellationEvaluation  Tesselation evaluation shader
    \value Geometry                Geometry shader
    \value Compute                 Compute shader
*/

/*!
    \enum QShaderProgram::Status

    This enum identifies the status of shader used.

    \value NotReady              The shader hasn't been compiled and linked yet
    \value Ready                 The shader was successfully compiled
    \value Error                 An error occurred while compiling the shader
*/

/*!
    \enum QShaderProgram::Format

    This enum identifies the format of the shader code used.

    \value GLSL              OpenGL
    \value SPIRV             Vulkan, OpenGL 5

    \since 5.15
*/

QT_BEGIN_NAMESPACE

namespace Qt3DRender {

QShaderProgramPrivate::QShaderProgramPrivate()
    : QNodePrivate()
    , m_status(QShaderProgram::NotReady)
    , m_format(QShaderProgram::GLSL)
{
}

void QShaderProgramPrivate::setLog(const QString &log)
{
    Q_Q(QShaderProgram);
    if (log != m_log) {
        m_log = log;
        const bool blocked = q->blockNotifications(true);
        emit q->logChanged(m_log);
        q->blockNotifications(blocked);
    }
}

void QShaderProgramPrivate::setStatus(QShaderProgram::Status status)
{
    Q_Q(QShaderProgram);
    if (status != m_status) {
        m_status = status;
        const bool blocked = q->blockNotifications(true);
        emit q->statusChanged(m_status);
        q->blockNotifications(blocked);
    }
}

QShaderProgram::QShaderProgram(QNode *parent)
    : QNode(*new QShaderProgramPrivate, parent)
{
}

QShaderProgram::~QShaderProgram()
{
}

/*! \internal */
QShaderProgram::QShaderProgram(QShaderProgramPrivate &dd, QNode *parent)
    : QNode(dd, parent)
{
}

/*!
    \qmlproperty string ShaderProgram::vertexShaderCode

    Holds the vertex shader code used by this shader program.
*/
/*!
    \property QShaderProgram::vertexShaderCode

    Holds the vertex shader code used by this shader program.
*/
void QShaderProgram::setVertexShaderCode(const QByteArray &vertexShaderCode)
{
    Q_D(QShaderProgram);
    if (vertexShaderCode != d->m_vertexShaderCode) {
        d->m_vertexShaderCode = vertexShaderCode;
        emit vertexShaderCodeChanged(vertexShaderCode);
    }
}

QByteArray QShaderProgram::vertexShaderCode() const
{
    Q_D(const QShaderProgram);
    return d->m_vertexShaderCode;
}

/*!
    \qmlproperty string ShaderProgram::tessellationControlShaderCode

    Holds the tesselation control shader code used by this shader program.
*/
/*!
    \property QShaderProgram::tessellationControlShaderCode

    Holds the tesselation control shader code used by this shader program.
*/
void QShaderProgram::setTessellationControlShaderCode(const QByteArray &tessellationControlShaderCode)
{
    Q_D(QShaderProgram);
    if (tessellationControlShaderCode != d->m_tessControlShaderCode) {
        d->m_tessControlShaderCode = tessellationControlShaderCode;
        emit tessellationControlShaderCodeChanged(tessellationControlShaderCode);
    }
}

QByteArray QShaderProgram::tessellationControlShaderCode() const
{
    Q_D(const QShaderProgram);
    return d->m_tessControlShaderCode;
}

/*!
    \qmlproperty string ShaderProgram::tessellationEvaluationShaderCode

    Holds the tesselation evaluation shader code used by this shader program.
*/
/*!
    \property QShaderProgram::tessellationEvaluationShaderCode

    Holds the tesselation evaluation shader code used by this shader program.
*/
void QShaderProgram::setTessellationEvaluationShaderCode(const QByteArray &tessellationEvaluationShaderCode)
{
    Q_D(QShaderProgram);
    if (tessellationEvaluationShaderCode != d->m_tessEvalShaderCode) {
        d->m_tessEvalShaderCode = tessellationEvaluationShaderCode;
        emit tessellationEvaluationShaderCodeChanged(tessellationEvaluationShaderCode);
    }
}

QByteArray QShaderProgram::tessellationEvaluationShaderCode() const
{
    Q_D(const QShaderProgram);
    return d->m_tessEvalShaderCode;
}

/*!
    \qmlproperty string ShaderProgram::geometryShaderCode

    Holds the geometry shader code used by this shader program.
*/
/*!
    \property QShaderProgram::geometryShaderCode

    Holds the geometry shader code used by this shader program.
*/
void QShaderProgram::setGeometryShaderCode(const QByteArray &geometryShaderCode)
{
    Q_D(QShaderProgram);
    if (geometryShaderCode != d->m_geometryShaderCode) {
        d->m_geometryShaderCode = geometryShaderCode;
        emit geometryShaderCodeChanged(geometryShaderCode);
    }
}

QByteArray QShaderProgram::geometryShaderCode() const
{
    Q_D(const QShaderProgram);
    return d->m_geometryShaderCode;
}

/*!
    \qmlproperty string ShaderProgram::fragmentShaderCode

    Holds the fragment shader code used by this shader program.
*/
/*!
    \property QShaderProgram::fragmentShaderCode

    Holds the fragment shader code used by this shader program.
*/
void QShaderProgram::setFragmentShaderCode(const QByteArray &fragmentShaderCode)
{
    Q_D(QShaderProgram);
    if (fragmentShaderCode != d->m_fragmentShaderCode) {
        d->m_fragmentShaderCode = fragmentShaderCode;
        emit fragmentShaderCodeChanged(fragmentShaderCode);
    }
}

QByteArray QShaderProgram::fragmentShaderCode() const
{
    Q_D(const QShaderProgram);
    return d->m_fragmentShaderCode;
}

/*!
    \qmlproperty string ShaderProgram::computeShaderCode

    Holds the compute shader code used by this shader program.
*/
/*!
    \property QShaderProgram::computeShaderCode

    Holds the compute shader code used by this shader program.
*/
void QShaderProgram::setComputeShaderCode(const QByteArray &computeShaderCode)
{
    Q_D(QShaderProgram);
    if (computeShaderCode != d->m_computeShaderCode) {
        d->m_computeShaderCode = computeShaderCode;
        emit computeShaderCodeChanged(computeShaderCode);
    }
}

QByteArray QShaderProgram::computeShaderCode() const
{
    Q_D(const QShaderProgram);
    return d->m_computeShaderCode;
}


/*!
    Sets the shader code for \a type of shader to the \a shaderCode.
*/
void QShaderProgram::setShaderCode(ShaderType type, const QByteArray &shaderCode)
{
    switch (type) {
    case Vertex:
        setVertexShaderCode(shaderCode);
        break;
    case TessellationControl:
        setTessellationControlShaderCode(shaderCode);
        break;
    case TessellationEvaluation:
        setTessellationEvaluationShaderCode(shaderCode);
        break;
    case Geometry:
        setGeometryShaderCode(shaderCode);
        break;
    case Fragment:
        setFragmentShaderCode(shaderCode);
        break;
    case Compute:
        setComputeShaderCode(shaderCode);
        break;
    default:
        Q_UNREACHABLE();
    }
}

/*!
    Returns the shader code for \a type.
*/
QByteArray QShaderProgram::shaderCode(ShaderType type) const
{
    Q_D(const QShaderProgram);
    switch (type) {
    case Vertex:
        return d->m_vertexShaderCode;
    case TessellationControl:
        return d->m_tessControlShaderCode;
    case TessellationEvaluation:
        return d->m_tessEvalShaderCode;
    case Geometry:
        return d->m_geometryShaderCode;
    case Fragment:
        return d->m_fragmentShaderCode;
    case Compute:
        return d->m_computeShaderCode;
    default:
        Q_UNREACHABLE();
    }
}

/*!
    \qmlproperty string ShaderProgram::log

    Holds the log of the current shader program. This is useful to diagnose a
    compilation failure of the shader program.
*/
/*!
    \property QShaderProgram::log

    Holds the log of the current shader program. This is useful to diagnose a
    compilation failure of the shader program.
*/
QString QShaderProgram::log() const
{
    Q_D(const QShaderProgram);
    return d->m_log;
}

/*!
    \qmlproperty enumeration ShaderProgram::status

    Holds the status of the current shader program.
*/
/*!
    \property QShaderProgram::status

    Holds the status of the current shader program.
*/
/*!
    Returns the status of the current shader program.
*/
QShaderProgram::Status QShaderProgram::status() const
{
    Q_D(const QShaderProgram);
    return d->m_status;
}

void QShaderProgram::setFormat(QShaderProgram::Format format)
{
    Q_D(QShaderProgram);
    if (format != d->m_format) {
        d->m_format = format;
        emit formatChanged(format);
    }
}

/*!
    \qmlproperty enumeration ShaderProgram::format
    \since 5.15

    Holds the format of the code provided on the ShaderProgram.
    The default is ShaderProgram.GLSL
*/
/*!
    \property QShaderProgram::format
    \since 5.15

    Holds the format of the code provided on the ShaderProgram.
    The default is ShaderProgram.GLSL
*/
QShaderProgram::Format QShaderProgram::format() const
{
    Q_D(const QShaderProgram);
    return d->m_format;
}

QByteArray QShaderProgramPrivate::deincludify(const QString &filePath)
{
    QFile f(filePath);
    if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "Could not read shader source file:" << f.fileName();
        return QByteArray();
    }

    QByteArray contents = f.readAll();
    return deincludify(contents, filePath);
}

QByteArray QShaderProgramPrivate::resolveAutoBindingIndices(const QByteArray &content,
                                                            int &currentBinding,
                                                            int &currentInputLocation,
                                                            int &currentOutputLocation)
{
    QString shaderCode = QString::fromUtf8(content);

    // This lambda will replace all occurrences of a string (e.g. "binding = auto") by another,
    // with the incremented int passed as argument (e.g. "binding = 1", "binding = 2" ...)
    const auto replaceAndIncrement = [&](const QRegularExpression &regexp,
            int &variable,
            const QString &replacement) noexcept {
        int matchStart = 0;
        do {
            matchStart = shaderCode.indexOf(regexp, matchStart);
            if (matchStart != -1) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
                const auto match = regexp.match(QStringView{shaderCode}.mid(matchStart));
#else
                const auto match = regexp.match(shaderCode.mid(matchStart));
#endif
                const auto length = match.capturedLength(0);
                shaderCode.replace(matchStart, length, replacement.arg(variable++));
            }
        } while (matchStart != -1);
    };

    // 1. Handle uniforms
    {
        thread_local const QRegularExpression bindings(
                    QStringLiteral("binding\\s*=\\s*auto"));

        replaceAndIncrement(bindings, currentBinding, QStringLiteral("binding = %1"));
    }

    // 2. Handle inputs
    {
        thread_local const QRegularExpression inLocations(
                    QStringLiteral("location\\s*=\\s*auto\\s*\\)\\s*in\\s+"));

        replaceAndIncrement(inLocations, currentInputLocation,
                            QStringLiteral("location = %1) in "));
    }

    // 3. Handle outputs
    {
        thread_local const QRegularExpression outLocations(
                    QStringLiteral("location\\s*=\\s*auto\\s*\\)\\s*out\\s+"));

        replaceAndIncrement(outLocations, currentOutputLocation,
                            QStringLiteral("location = %1) out "));
    }

    return shaderCode.toUtf8();
}

QByteArray QShaderProgramPrivate::resolveAutoBindingIndices(const QByteArray &content)
{
    int currentBinding = 2; // Qt3D default uniforms are 0 and 1
    int currentInputLocation = 0;
    int currentOutputLocation = 0;

    return QShaderProgramPrivate::resolveAutoBindingIndices(content,
                                                            currentBinding,
                                                            currentInputLocation,
                                                            currentOutputLocation);
}

QByteArray QShaderProgramPrivate::deincludify(const QByteArray &contents, const QString &filePath)
{
    QByteArrayList lines = contents.split('\n');
    const QByteArray includeDirective = QByteArrayLiteral("#pragma include");
    for (int i = 0; i < lines.count(); ++i) {
        const auto line = lines[i].simplified();
        if (line.startsWith(includeDirective)) {
            const QString includePartialPath = QString::fromUtf8(line.mid(includeDirective.size() + 1));

            QString includePath = QFileInfo(includePartialPath).isAbsolute() ? includePartialPath
                                : QFileInfo(filePath).absolutePath() + QLatin1Char('/') + includePartialPath;
            if (qEnvironmentVariableIsSet("QT3D_GLSL100_WORKAROUND")) {
                QString candidate = includePath + QLatin1String("100");
                if (QFile::exists(candidate))
                    includePath = candidate;
            }
            lines.removeAt(i);
            QByteArray includedContents = deincludify(includePath);
            lines.insert(i, includedContents);
            QString lineDirective = QString(QStringLiteral("#line %1")).arg(i + 2);
            lines.insert(i + 1, lineDirective.toUtf8());
        }
    }

    return lines.join('\n');
}

/*!
    \qmlmethod string ShaderProgram::loadSource(url sourceUrl)

    Returns the shader code loaded from \a sourceUrl.
*/
/*!
    Returns the shader code loaded from \a sourceUrl.
*/
QByteArray QShaderProgram::loadSource(const QUrl &sourceUrl)
{
    // TO DO: Handle remote path
    const QByteArray deincluded = QShaderProgramPrivate::deincludify(Qt3DCore::QUrlHelper::urlToLocalFileOrQrc(sourceUrl));
    return QShaderProgramPrivate::resolveAutoBindingIndices(deincluded);
}

} // of namespace Qt3DRender

QT_END_NAMESPACE

#include "moc_qshaderprogram.cpp"