aboutsummaryrefslogtreecommitdiffstats
path: root/src/runtimerender/qssgrenderdynamicobjectsystem.cpp
blob: 985f759cb8b9d69b58e27d4edf48b4b578aa1507 (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
/****************************************************************************
**
** Copyright (C) 2008-2012 NVIDIA Corporation.
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Quick 3D.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "qssgrenderdynamicobjectsystem_p.h"

#include <QtQuick3DRuntimeRender/private/qssgrendercontextcore_p.h>
#include <QtQuick3DRuntimeRender/private/qssgrendershadercache_p.h>
#include <QtQuick3DRuntimeRender/private/qssgrenderinputstreamfactory_p.h>
#include <QtQuick3DRuntimeRender/private/qssgrenderer_p.h>
#include <QtQuick3DRuntimeRender/private/qssgrenderdynamicobjectsystemcommands_p.h>
#include <QtQuick3DRuntimeRender/private/qssgrenderdynamicobjectsystemutil_p.h>
#include <QtQuick3DRuntimeRender/private/qssgrendershadercodegenerator_p.h>
#include <QtQuick3DRuntimeRender/private/qssgruntimerenderlogging_p.h>

#include <QtQuick3DRender/private/qssgrendershaderconstant_p.h>
#include <QtQuick3DRender/private/qssgrendershaderprogram_p.h>

#include <QtQuick3DUtils/private/qssgutils_p.h>

#include <QtCore/QMutex>
#include <QtCore/QMutexLocker>

QT_BEGIN_NAMESPACE

uint qHash(const TStrStrPair &item)
{
    return qHash(item.first) ^ qHash(item.second);
}

namespace dynamic {

uint qHash(const QSSGDynamicShaderMapKey &inKey)
{
    return inKey.m_hashCode;
}

//quint32 QSSGCommand::getSizeofCommand(const QSSGCommand &inCommand)
//{
//    switch (inCommand.m_type) {
//    case CommandType::AllocateBuffer:
//        return sizeof(QSSGAllocateBuffer);
//    case CommandType::BindBuffer:
//        return sizeof(QSSGBindBuffer);
//    case CommandType::BindTarget:
//        return sizeof(QSSGBindTarget);
//    case CommandType::BindShader:
//        return sizeof(QSSGBindShader);
//    case CommandType::Render:
//        return sizeof(QSSGRender);
//    case CommandType::ApplyBufferValue:
//        return sizeof(QSSGApplyBufferValue);
//    case CommandType::ApplyDepthValue:
//        return sizeof(QSSGApplyDepthValue);
//    case CommandType::ApplyInstanceValue:
//        return sizeof(QSSGApplyInstanceValue);
//    case CommandType::ApplyBlending:
//        return sizeof(QSSGApplyBlending);
//    case CommandType::ApplyRenderState:
//        return sizeof(QSSGApplyRenderState);
//    case CommandType::ApplyBlitFramebuffer:
//        return sizeof(QSSGApplyBlitFramebuffer);
//    case CommandType::ApplyValue:
//        return sizeof(QSSGApplyValue) + static_cast<const QSSGApplyValue &>(inCommand).m_value.mSize;
//    case CommandType::DepthStencil:
//        return sizeof(QSSGDepthStencil);
//    case CommandType::AllocateImage:
//        return sizeof(QSSGAllocateImage);
//    case CommandType::ApplyImageValue:
//        return sizeof(QSSGApplyImageValue);
//    case CommandType::AllocateDataBuffer:
//        return sizeof(QSSGAllocateDataBuffer);
//    case CommandType::ApplyDataBufferValue:
//        return sizeof(QSSGApplyDataBufferValue);
//    default:
//        break;
//    }
//    Q_ASSERT(false);
//    return 0;
//}

//template<typename TCommandType>
//inline void CopyConstructCommandT(quint8 *inDataBuffer, const QSSGCommand &inCommand)
//{
//    TCommandType *theCommand = (TCommandType *)inDataBuffer;
//    theCommand = new (theCommand) TCommandType(static_cast<const TCommandType &>(inCommand));
//}

//void QSSGCommand::copyConstructCommand(quint8 *inDataBuffer, const QSSGCommand &inCommand)
//{
//    switch (inCommand.m_type) {
//    case CommandType::AllocateBuffer:
//        CopyConstructCommandT<QSSGAllocateBuffer>(inDataBuffer, inCommand);
//        break;
//    case CommandType::BindBuffer:
//        CopyConstructCommandT<QSSGBindBuffer>(inDataBuffer, inCommand);
//        break;
//    case CommandType::BindTarget:
//        CopyConstructCommandT<QSSGBindTarget>(inDataBuffer, inCommand);
//        break;
//    case CommandType::BindShader:
//        CopyConstructCommandT<QSSGBindShader>(inDataBuffer, inCommand);
//        break;
//    case CommandType::Render:
//        CopyConstructCommandT<QSSGRender>(inDataBuffer, inCommand);
//        break;
//    case CommandType::ApplyBufferValue:
//        CopyConstructCommandT<QSSGApplyBufferValue>(inDataBuffer, inCommand);
//        break;
//    case CommandType::ApplyDepthValue:
//        CopyConstructCommandT<QSSGApplyDepthValue>(inDataBuffer, inCommand);
//        break;
//    case CommandType::ApplyInstanceValue:
//        CopyConstructCommandT<QSSGApplyInstanceValue>(inDataBuffer, inCommand);
//        break;
//    case CommandType::ApplyBlending:
//        CopyConstructCommandT<QSSGApplyBlending>(inDataBuffer, inCommand);
//        break;
//    case CommandType::ApplyRenderState:
//        CopyConstructCommandT<QSSGApplyRenderState>(inDataBuffer, inCommand);
//        break;
//    case CommandType::ApplyBlitFramebuffer:
//        CopyConstructCommandT<QSSGApplyBlitFramebuffer>(inDataBuffer, inCommand);
//        break;
//    case CommandType::ApplyValue: {
//        CopyConstructCommandT<QSSGApplyValue>(inDataBuffer, inCommand);
//        QSSGApplyValue &dest = *reinterpret_cast<QSSGApplyValue *>(inDataBuffer);
//        quint8 *destMem = inDataBuffer + sizeof(QSSGApplyValue);
//        const QSSGApplyValue &src = static_cast<const QSSGApplyValue &>(inCommand);
//        memcpy(destMem, src.m_value.mData, src.m_value.mSize);
//        dest.m_value.mData = destMem;
//        break;
//    }
//    case CommandType::DepthStencil:
//        CopyConstructCommandT<QSSGDepthStencil>(inDataBuffer, inCommand);
//        break;
//    case CommandType::AllocateImage:
//        CopyConstructCommandT<QSSGAllocateImage>(inDataBuffer, inCommand);
//        break;
//    case CommandType::ApplyImageValue:
//        CopyConstructCommandT<QSSGApplyImageValue>(inDataBuffer, inCommand);
//        break;
//    case CommandType::AllocateDataBuffer:
//        CopyConstructCommandT<QSSGAllocateDataBuffer>(inDataBuffer, inCommand);
//        break;
//    case CommandType::ApplyDataBufferValue:
//        CopyConstructCommandT<QSSGApplyDataBufferValue>(inDataBuffer, inCommand);
//        break;
//    default:
//        Q_ASSERT(false);
//        break;
//    }
//}
}

QString QSSGDynamicObjectSystem::getShaderCodeLibraryDirectory()
{
    return QStringLiteral("res/effectlib");
}
static QByteArray includeSearch() { return QByteArrayLiteral("#include \""); };
static QByteArray copyrightHeaderStart() { return QByteArrayLiteral("/****************************************************************************"); }
static QByteArray copyrightHeaderEnd() { return QByteArrayLiteral("****************************************************************************/"); }


QSSGDynamicObjectSystem::QSSGDynamicObjectSystem(QSSGRenderContextInterface *ctx)
    : m_context(ctx), m_propertyLoadMutex()
{
}

QSSGDynamicObjectSystem::~QSSGDynamicObjectSystem() {}

void QSSGDynamicObjectSystem::setShaderData(const QByteArray &inPath,
                                              const QByteArray &inData,
                                              const QByteArray &inShaderType,
                                              const QByteArray &inShaderVersion,
                                              bool inHasGeomShader,
                                              bool inIsComputeShader)
{
    //        inData = inData ? inData : "";
    auto foundIt = m_expandedFiles.find(inPath);
    if (foundIt != m_expandedFiles.end())
        foundIt.value() = inData;
    else
        m_expandedFiles.insert(inPath, inData);

    // set shader type and version if available
    if (!inShaderType.isNull() || !inShaderVersion.isNull() || inHasGeomShader || inIsComputeShader) {
        // UdoL TODO: Add this to the load / save setction
        // In addition we should merge the source code into SDynamicObjectShaderInfo as well
        QSSGDynamicObjectShaderInfo &theShaderInfo = m_shaderInfoMap.insert(inPath, QSSGDynamicObjectShaderInfo()).value();
        theShaderInfo.m_type = inShaderType;
        theShaderInfo.m_version = inShaderVersion;
        theShaderInfo.m_hasGeomShader = inHasGeomShader;
        theShaderInfo.m_isComputeShader = inIsComputeShader;
    }
}

QByteArray QSSGDynamicObjectSystem::getShaderCacheKey(const QByteArray &inId,
                                                        const QByteArray &inProgramMacro,
                                                        const dynamic::QSSGDynamicShaderProgramFlags &inFlags)
{
    QByteArray shaderKey = inId;
    if (!inProgramMacro.isEmpty()) {
        shaderKey.append("#");
        shaderKey.append(inProgramMacro);
    }
    if (inFlags & ShaderCacheProgramFlagValues::TessellationEnabled) {
        shaderKey.append("#");
        shaderKey.append(toString(inFlags.tessMode));
    }
    if (inFlags & ShaderCacheProgramFlagValues::GeometryShaderEnabled && inFlags.wireframeMode) {
        shaderKey.append("#");
        shaderKey.append(inFlags.wireframeToString(inFlags.wireframeMode));
    }
    return shaderKey;
}

void QSSGDynamicObjectSystem::insertShaderHeaderInformation(QByteArray &theReadBuffer, const QByteArray &inPathToEffect)
{
    doInsertShaderHeaderInformation(theReadBuffer, inPathToEffect);
}

void QSSGDynamicObjectSystem::doInsertShaderHeaderInformation(QByteArray &theReadBuffer, const QByteArray &inPathToEffect)
{
    // Now do search and replace for the headers
    for (int thePos = theReadBuffer.indexOf(includeSearch()); thePos != -1;
         thePos = theReadBuffer.indexOf(includeSearch(), thePos + 1)) {
        int theEndQuote = theReadBuffer.indexOf('\"', thePos + includeSearch().length() + 1);
        // Indicates an unterminated include file.
        if (theEndQuote == -1) {
            qCCritical(INVALID_OPERATION, "Unterminated include in file: %s", inPathToEffect.constData());
            theReadBuffer.clear();
            break;
        }
        const int theActualBegin = thePos + includeSearch().length();
        const auto &theInclude = theReadBuffer.mid(theActualBegin, theEndQuote - theActualBegin);
        // If we haven't included the file yet this round
        auto theHeader = doLoadShader(theInclude);
        // Strip copywrite headers from include if present
        if (theHeader.startsWith(copyrightHeaderStart())) {
            int clipPos = theHeader.indexOf(copyrightHeaderEnd()) ;
            if (clipPos >= 0)
                theHeader.remove(0, clipPos + copyrightHeaderEnd().count());
        }
        // Write insert comment for begin source
        theHeader.prepend(QByteArrayLiteral("\n// begin \"") + theInclude + QByteArrayLiteral("\"\n"));
        // Write insert comment for end source
        theHeader.append(QByteArrayLiteral("\n// end \"" ) + theInclude + QByteArrayLiteral("\"\n"));
        theReadBuffer = theReadBuffer.replace(thePos, (theEndQuote + 1) - thePos, theHeader);
    }
}

QByteArray QSSGDynamicObjectSystem::doLoadShader(const QByteArray &inPathToEffect)
{
    auto theInsert = m_expandedFiles.find(inPathToEffect);
    const bool found = (theInsert != m_expandedFiles.end());

    QByteArray theReadBuffer;
    if (!found) {
        const QString defaultDir = getShaderCodeLibraryDirectory();
        const QString platformDir = shaderCodeLibraryPlatformDirectory();
        const auto ver = shaderCodeLibraryVersion();

        QString fullPath;
        QSharedPointer<QIODevice> theStream;
        if (!platformDir.isEmpty()) {
            QTextStream stream(&fullPath);
            stream << platformDir << QLatin1Char('/') << QString::fromLocal8Bit(inPathToEffect);
            theStream = m_context->inputStreamFactory()->getStreamForFile(fullPath, true);
        }

        if (theStream.isNull()) {
            fullPath.clear();
            QTextStream stream(&fullPath);
            stream << defaultDir << QLatin1Char('/') << ver << QLatin1Char('/') << QString::fromLocal8Bit(inPathToEffect);
            theStream = m_context->inputStreamFactory()->getStreamForFile(fullPath, true);
            if (theStream.isNull()) {
                fullPath.clear();
                QTextStream stream(&fullPath);
                stream << defaultDir << QLatin1Char('/') << QString::fromLocal8Bit(inPathToEffect);
                theStream = m_context->inputStreamFactory()->getStreamForFile(fullPath, false);
            }
        }
        if (!theStream.isNull()) {
            char readBuf[1024];
            qint64 amountRead = 0;
            do {
                amountRead = theStream->read(readBuf, 1024);
                if (amountRead)
                    theReadBuffer.append(readBuf, int(amountRead));
            } while (amountRead);
        } else {
            qCCritical(INVALID_OPERATION, "Failed to find include file %s", qPrintable(QString::fromLocal8Bit(inPathToEffect)));
            Q_ASSERT(false);
        }
        theInsert = m_expandedFiles.insert(inPathToEffect, theReadBuffer);
    } else {
        theReadBuffer = theInsert.value();
    }
    doInsertShaderHeaderInformation(theReadBuffer, inPathToEffect);
    return theReadBuffer;
}

QStringList QSSGDynamicObjectSystem::getParameters(const QString &str, int begin, int end)
{
    const QString s = str.mid(begin, end - begin + 1);
    return s.split(',');
}

void QSSGDynamicObjectSystem::insertSnapperDirectives(QString &str)
{
    int beginIndex = 0;
    // Snapper macros:
    //  #define SNAPPER_SAMPLER2D(propName, propNiceName, texFilter, texWrap, showUI )
    //      uniform sampler2D propName;
    //      uniform int flag##propName;
    //      uniform vec4 propName##Info;
    //      vec4 texture2D_##propName(vec2 uv)
    //      {
    //          return GetTextureValue( propName, uv, propName##Info.z );
    //      }
    //
    //  #define SNAPPER_SAMPLER2DWITHDEFAULT(propName, propNiceName, texFilter, texWrap, defaultPath, showUI )
    //      SNAPPER_SAMPLER2D( propName, propNiceName, texFilter, texWrap, showUI )
    //
    //  #define SNAPPER_SAMPLERCUBE(propName, propNiceName, texFilter, texWrap )
    //      uniform samplerCube propName;
    //      uniform vec2 propName##UVRange;
    //      uniform int flag##propName;
    //      uniform vec2 propName##Size;
    QString snapperSampler = QStringLiteral("SNAPPER_SAMPLER2D(");
    QString snapperSamplerDefault = QStringLiteral("SNAPPER_SAMPLER2DWITHDEFAULT(");
    QString snapperSamplerCube = QStringLiteral("SNAPPER_SAMPLERCUBE(");
    QString endingBracket = QStringLiteral(")");

    while ((beginIndex = str.indexOf(snapperSampler, beginIndex)) >= 0) {
        int endIndex = str.indexOf(endingBracket, beginIndex);
        const QStringList list = getParameters(str, beginIndex + snapperSampler.length(), endIndex);
        str.remove(beginIndex, endIndex - beginIndex + 1);
        if (list.size() == 5) {
            QString insertStr;
            QTextStream stream(&insertStr);
            stream << "uniform sampler2D " << list[0] << ";\n";
            stream << "uniform int flag" << list[0] << ";\n";
            stream << "uniform vec4 " << list[0] << "Info;\n";
            stream << "vec4 texture2D_" << list[0] << "(vec2 uv) "
                   << "{ return GetTextureValue( " << list[0] << ", uv, " << list[0] << "Info.z ); }\n";
            str.insert(beginIndex, insertStr);
        }
    }
    beginIndex = 0;
    while ((beginIndex = str.indexOf(snapperSamplerDefault, beginIndex)) >= 0) {
        int endIndex = str.indexOf(endingBracket, beginIndex);
        const QStringList list = getParameters(str, beginIndex + snapperSamplerDefault.length(), endIndex);
        str.remove(beginIndex, endIndex - beginIndex + 1);
        if (list.size() == 5) {
            QString insertStr;
            QTextStream stream(&insertStr);
            stream << "uniform sampler2D " << list[0] << ";\n";
            stream << "uniform int flag" << list[0] << ";\n";
            stream << "uniform vec4 " << list[0] << "Info;\n";
            stream << "vec4 texture2D_" << list[0] << "(vec2 uv) "
                   << "{ return GetTextureValue( " << list[0] << ", uv, " << list[0] << "Info.z ); }\n";
            str.insert(beginIndex, insertStr);
        }
    }
    beginIndex = 0;
    while ((beginIndex = str.indexOf(snapperSamplerCube, beginIndex)) >= 0) {
        int endIndex = str.indexOf(endingBracket, beginIndex);
        const QStringList list = getParameters(str, beginIndex + snapperSamplerCube.length(), endIndex);
        str.remove(beginIndex, endIndex - beginIndex + 1);
        if (list.size() == 4) {
            QString insertStr;
            QTextStream stream(&insertStr);
            stream << "uniform samplerCube " << list[0] << ";\n";
            stream << "uniform vec2 " << list[0] << "UVRange;\n";
            stream << "uniform int flag" << list[0] << ";\n";
            stream << "uniform vec2 " << list[0] << "Size;\n";
            str.insert(beginIndex, insertStr);
        }
    }
}

QSSGRef<QSSGRenderShaderProgram> QSSGDynamicObjectSystem::compileShader(const QByteArray &inId,
                                                                              const QByteArray &inProgramSource,
                                                                              const QByteArray &inGeomSource,
                                                                              const QByteArray &inProgramMacroName,
                                                                              const ShaderFeatureSetList &inFeatureSet,
                                                                              const dynamic::QSSGDynamicShaderProgramFlags &inFlags,
                                                                              bool inForceCompilation)
{
    m_vertShader.clear();
    m_fragShader.clear();
    m_geometryShader.clear();
    QSSGShaderCacheProgramFlags theFlags;

    m_vertShader.append("#define VERTEX_SHADER\n");
    m_fragShader.append("#define FRAGMENT_SHADER\n");

    if (!inProgramMacroName.isEmpty()) {
        m_vertShader.append("#define ");
        m_vertShader.append(inProgramMacroName);
        m_vertShader.append("\n");

        m_fragShader.append("#define ");
        m_fragShader.append(inProgramMacroName);
        m_fragShader.append("\n");
    }

    if (!inGeomSource.isEmpty() && inFlags & ShaderCacheProgramFlagValues::GeometryShaderEnabled) {
        theFlags |= ShaderCacheProgramFlagValues::GeometryShaderEnabled;

        m_geometryShader.append("#define GEOMETRY_SHADER 1\n");
        m_geometryShader.append(inGeomSource);

        m_vertShader.append("#define GEOMETRY_SHADER 1\n");
    } else if (inFlags & ShaderCacheProgramFlagValues::GeometryShaderEnabled) {
        theFlags |= ShaderCacheProgramFlagValues::GeometryShaderEnabled;
        m_geometryShader.append("#define USER_GEOMETRY_SHADER 1\n");
        m_geometryShader.append(inProgramSource);
        m_vertShader.append("#define GEOMETRY_SHADER 0\n");
        m_fragShader.append("#define GEOMETRY_WIREFRAME_SHADER 0\n");
    } else {
        m_vertShader.append("#define GEOMETRY_SHADER 0\n");
        m_fragShader.append("#define GEOMETRY_WIREFRAME_SHADER 0\n");
    }

    if (strstr(inProgramSource, "SNAPPER_SAMPLER")) {
        QString programSource = QString::fromLatin1(inProgramSource);
        insertSnapperDirectives(programSource);
        QByteArray data = programSource.toLatin1();
        const char *source = data.constData();

        m_vertShader.append(source);
        m_fragShader.append(source);
    } else {
        m_vertShader.append(inProgramSource);
        m_fragShader.append(inProgramSource);
    }

    QSSGRef<QSSGShaderCache> theShaderCache = m_context->shaderCache();

    QByteArray theKey = getShaderCacheKey(inId, inProgramMacroName, inFlags);
    if (inForceCompilation) {
        return theShaderCache->forceCompileProgram(theKey, m_vertShader, m_fragShader, nullptr, nullptr, m_geometryShader, theFlags, inFeatureSet, false);
    }

    return theShaderCache->compileProgram(theKey, m_vertShader, m_fragShader, nullptr, nullptr, m_geometryShader, theFlags, inFeatureSet);
}

QByteArray QSSGDynamicObjectSystem::getShaderSource(const QByteArray &inPath)
{
//    QByteArray source(QByteArrayLiteral("#define FRAGMENT_SHADER\n"));
//    source.append(doLoadShader(inPath));
    return doLoadShader(inPath);
}

TShaderAndFlags QSSGDynamicObjectSystem::getShaderProgram(const QByteArray &inPath,
                                                            const QByteArray &inProgramMacro,
                                                            const ShaderFeatureSetList &inFeatureSet,
                                                            const dynamic::QSSGDynamicShaderProgramFlags &inFlags,
                                                            bool inForceCompilation)
{
    dynamic::QSSGDynamicShaderMapKey shaderMapKey(TStrStrPair(inPath, inProgramMacro), inFeatureSet, inFlags.tessMode, inFlags.wireframeMode);
    auto theInserter = m_shaderMap.find(shaderMapKey);
    const bool found = (theInserter != m_shaderMap.end());

    if (!found)
        theInserter = m_shaderMap.insert(shaderMapKey, TShaderAndFlags());

    // TODO: This looks funky (if found)...
    if (found || inForceCompilation) {
        QSSGRef<QSSGRenderShaderProgram> theProgram = m_context->shaderCache()
                                                                  ->getProgram(getShaderCacheKey(inPath,
                                                                                                 inProgramMacro,
                                                                                                 inFlags),
                                                                               inFeatureSet);
        dynamic::QSSGDynamicShaderProgramFlags theFlags(inFlags);
        if (!theProgram || inForceCompilation) {
            QSSGDynamicObjectShaderInfo
                    &theShaderInfo = m_shaderInfoMap.insert(inPath, QSSGDynamicObjectShaderInfo()).value();
            if (!theShaderInfo.m_isComputeShader) {
                QByteArray programSource = doLoadShader(inPath);
                if (theShaderInfo.m_hasGeomShader)
                    theFlags |= ShaderCacheProgramFlagValues::GeometryShaderEnabled;
                theProgram = compileShader(inPath, programSource.constData(), nullptr, inProgramMacro, inFeatureSet, theFlags, inForceCompilation);
            } else {
                QByteArray theShaderBuffer;
                const char *shaderVersionStr = "#version 430\n";
                if (m_context->renderContext()->renderContextType() == QSSGRenderContextType::GLES3PLUS)
                    shaderVersionStr = "#version 310 es\n";
                theShaderBuffer = doLoadShader(inPath);
                theShaderBuffer.insert(0, shaderVersionStr);
                theProgram = m_context->renderContext()->compileComputeSource(inPath, toByteView(theShaderBuffer)).m_shader;
            }
        }
        theInserter.value() = TShaderAndFlags(theProgram, theFlags);
    }
    return theInserter.value();
}

TShaderAndFlags QSSGDynamicObjectSystem::getDepthPrepassShader(const QByteArray &inPath, const QByteArray &inPMacro, const ShaderFeatureSetList &inFeatureSet)
{
    QSSGDynamicObjectShaderInfo &theShaderInfo = m_shaderInfoMap.insert(inPath, QSSGDynamicObjectShaderInfo()).value();
    if (!theShaderInfo.m_hasGeomShader)
        return TShaderAndFlags();
    // else, here we go...
    dynamic::QSSGDynamicShaderProgramFlags theFlags;
    const QByteArray shaderKey = inPMacro + QByteArrayLiteral("depthprepass");

    const QByteArray &theProgramMacro = shaderKey;

    const dynamic::QSSGDynamicShaderMapKey shaderMapKey(TStrStrPair(inPath, theProgramMacro),
                                                          inFeatureSet,
                                                          theFlags.tessMode,
                                                          theFlags.wireframeMode);
    const TShaderAndFlags shaderFlags;
    auto theInserter = m_shaderMap.find(shaderMapKey);
    const bool found = theInserter != m_shaderMap.end();
    if (found) {
        QSSGRef<QSSGRenderShaderProgram> theProgram = m_context->shaderCache()
                                                                  ->getProgram(getShaderCacheKey(inPath,
                                                                                                 theProgramMacro,
                                                                                                 theFlags),
                                                                               inFeatureSet);
        dynamic::QSSGDynamicShaderProgramFlags flags(theFlags);
        if (!theProgram) {
            QByteArray geomSource = doLoadShader(inPath);
            QSSGShaderVertexCodeGenerator vertexShader(m_context->renderContext()->renderContextType());
            QSSGShaderFragmentCodeGenerator fragmentShader(vertexShader, m_context->renderContext()->renderContextType());

            vertexShader.addAttribute("attr_pos", "vec3");
            vertexShader.addUniform("modelViewProjection", "mat4");
            vertexShader.append("void main() {");
            vertexShader.append("\tgl_Position = modelViewProjection * vec4(attr_pos, 1.0);");
            vertexShader.append("}");
            fragmentShader.append("void main() {");
            fragmentShader.append("\tfragOutput = vec4(0.0, 0.0, 0.0, 0.0);");
            fragmentShader.append("}");
            QByteArray vertexSource = vertexShader.buildShaderSource();
            QByteArray fragmentSource = fragmentShader.buildShaderSource();

            QByteArray programBuffer(QByteArrayLiteral("#ifdef VERTEX_SHADER\n"));
            programBuffer.append(vertexSource);
            programBuffer.append("\n#endif\n");
            programBuffer.append("\n#ifdef FRAGMENT_SHADER\n");
            programBuffer.append(fragmentSource);
            programBuffer.append("\n#endif");
            flags |= ShaderCacheProgramFlagValues::GeometryShaderEnabled;
            theProgram = compileShader(inPath, programBuffer, geomSource, theProgramMacro, inFeatureSet, flags);
        }
        theInserter.value() = TShaderAndFlags(theProgram, flags);
    }
    return theInserter.value();
}

void QSSGDynamicObjectSystem::setShaderCodeLibraryVersion(const QByteArray &version)
{
    m_shaderLibraryVersion = version;
}

QByteArray QSSGDynamicObjectSystem::shaderCodeLibraryVersion()
{
    return m_shaderLibraryVersion;
}

void QSSGDynamicObjectSystem::setShaderCodeLibraryPlatformDirectory(const QString &directory)
{
    m_shaderLibraryPlatformDirectory = directory;
}

QString QSSGDynamicObjectSystem::shaderCodeLibraryPlatformDirectory()
{
    return m_shaderLibraryPlatformDirectory;
}

QT_END_NAMESPACE