summaryrefslogtreecommitdiffstats
path: root/src/render/backend/renderer.cpp
blob: ba0e28adbb435f92c9f7f41094c68531d8f525ba (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
/****************************************************************************
**
** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
** Copyright (C) 2015 The Qt Company Ltd and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** 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 http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "renderer_p.h"

#include <Qt3DCore/qentity.h>

#include <Qt3DRender/qmaterial.h>
#include <Qt3DRender/qmesh.h>
#include <Qt3DRender/qparametermapping.h>
#include <Qt3DRender/qrenderpass.h>
#include <Qt3DRender/qshaderprogram.h>
#include <Qt3DRender/qtechnique.h>
#include <Qt3DRender/qrenderaspect.h>
#include <Qt3DRender/qeffect.h>
#include <Qt3DRender/qabstractsceneparser.h>

#include <Qt3DRender/private/renderviewjob_p.h>
#include <Qt3DRender/private/renderstates_p.h>
#include <Qt3DRender/private/cameraselectornode_p.h>
#include <Qt3DRender/private/framegraphvisitor_p.h>
#include <Qt3DRender/private/graphicscontext_p.h>
#include <Qt3DRender/private/cameralens_p.h>
#include <Qt3DRender/private/rendercommand_p.h>
#include <Qt3DRender/private/entity_p.h>
#include <Qt3DRender/private/renderlogging_p.h>
#include <Qt3DRender/private/material_p.h>
#include <Qt3DRender/private/renderpassfilternode_p.h>
#include <Qt3DRender/private/renderqueue_p.h>
#include <Qt3DRender/private/shader_p.h>
#include <Qt3DRender/private/buffer_p.h>
#include <Qt3DRender/private/renderstateset_p.h>
#include <Qt3DRender/private/technique_p.h>
#include <Qt3DRender/private/renderthread_p.h>
#include <Qt3DRender/private/renderview_p.h>
#include <Qt3DRender/private/techniquefilternode_p.h>
#include <Qt3DRender/private/viewportnode_p.h>
#include <Qt3DRender/private/vsyncframeadvanceservice_p.h>
#include <Qt3DRender/private/pickeventfilter_p.h>
#include <Qt3DRender/private/managers_p.h>
#include <Qt3DRender/private/buffermanager_p.h>
#include <Qt3DRender/private/nodemanagers_p.h>
#include <Qt3DRender/private/geometryrenderermanager_p.h>
#include <Qt3DRender/private/openglvertexarrayobject_p.h>

#include <Qt3DRender/qcameralens.h>
#include <Qt3DCore/private/qeventfilterservice_p.h>
#include <Qt3DCore/private/qabstractaspectjobmanager_p.h>

#include <QStack>
#include <QSurface>
#include <QElapsedTimer>
#include <QLibraryInfo>
#include <QPluginLoader>
#include <QDir>
#include <QUrl>
#include <QOffscreenSurface>
#include <QWindow>

// For Debug purposes only
#include <QThread>

QT_BEGIN_NAMESPACE

using namespace Qt3DCore;

namespace Qt3DRender {
namespace Render {

const QString SCENE_PARSERS_PATH = QStringLiteral("/sceneparsers");

const RendererSettings Renderer::ms_defaultSettings;

/*!
    \internal

    Renderer shutdown procedure:

    Since the renderer relies on the surface and OpenGLContext to perform its cleanup,
    it is shutdown when the surface is set to Q_NULLPTR

    When the surface is set to Q_NULLPTR this will request the RenderThread to terminate
    and will prevent createRenderBinJobs from returning a set of jobs as there is nothing
    more to be rendered.

    In turn, this will call shutdown which will make the OpenGL context current one last time
    to allow cleanups requiring a call to QOpenGLContext::currentContext to execute properly.
    At the end of that function, the GraphicsContext is set to null.

    At this point though, the QAspectThread is still running its event loop and will only stop
    a short while after.
 */

Renderer::Renderer(QRenderAspect::RenderType type)
    : m_services(Q_NULLPTR)
    , m_nodesManager(Q_NULLPTR)
    , m_graphicsContext(Q_NULLPTR)
    , m_renderQueue(new RenderQueue())
    , m_renderThread(type == QRenderAspect::Threaded ? new RenderThread(this) : Q_NULLPTR)
    , m_vsyncFrameAdvanceService(new VSyncFrameAdvanceService())
    , m_pickEventFilter(new PickEventFilter())
    , m_exposed(0)
    , m_glContext(Q_NULLPTR)
    , m_pickBoundingVolumeJob(Q_NULLPTR)
    , m_time(0)
    , m_settings(const_cast<RendererSettings *>(&ms_defaultSettings))
    , m_waitForInitializationToBeCompleted(0)
{
    // Set renderer as running - it will wait in the context of the
    // RenderThread for RenderViews to be submitted
    m_running.fetchAndStoreOrdered(1);
    if (m_renderThread)
        m_renderThread->waitForStart();
}

Renderer::~Renderer()
{
    // CLean up settings if not using defaults
    if (m_settings != &ms_defaultSettings)
        delete m_settings;
}

qint64 Renderer::time() const
{
    return m_time;
}

void Renderer::setTime(qint64 time)
{
    m_time = time;
}

NodeManagers *Renderer::nodeManagers() const
{
    return m_nodesManager;
}

void Renderer::setOpenGLContext(QOpenGLContext *context)
{
    m_glContext = context;
}

void Renderer::buildDefaultTechnique()
{
    Q_ASSERT(m_graphicsContext);
    Q_ASSERT(m_graphicsContext->openGLContext());

    // TODO: Either use public API only or just go direct to the private backend API here
    m_defaultTechnique = new QTechnique;
    m_defaultTechnique->setObjectName(QStringLiteral("default-technique"));

    QShaderProgram* defaultShader = new QShaderProgram;
    QString vertexFileName;
    QString fragmentFileName;
    if (m_graphicsContext->openGLContext()->isOpenGLES()) {
        vertexFileName = QStringLiteral("qrc:/shaders/es2/phong.vert");
        fragmentFileName = QStringLiteral("qrc:/shaders/es2/phong.frag");
    } else {
        if (m_graphicsContext->openGLContext()->format().profile() == QSurfaceFormat::CoreProfile) {
            vertexFileName = QStringLiteral("qrc:/shaders/gl3/phong.vert");
            fragmentFileName = QStringLiteral("qrc:/shaders/gl3/phong.frag");
        } else {
            vertexFileName = QStringLiteral("qrc:/shaders/es2/phong.vert");
            fragmentFileName = QStringLiteral("qrc:/shaders/es2/phong.frag");
        }
    }
    defaultShader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(vertexFileName)));
    defaultShader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(fragmentFileName)));
    defaultShader->setObjectName(QStringLiteral("DefaultShader"));

    QRenderPass* basicPass = new QRenderPass;
    basicPass->setShaderProgram(defaultShader);

    m_defaultRenderStateSet = new RenderStateSet;
    m_defaultRenderStateSet->addState(DepthTest::getOrCreate(GL_LESS));
    m_defaultRenderStateSet->addState(CullFace::getOrCreate(GL_BACK));
    m_defaultRenderStateSet->addState(ColorMask::getOrCreate(true, true, true, true));
    //basicPass->setStateSet(m_defaultRenderStateSet);

    m_defaultTechnique->addPass(basicPass);

    QParameter* ka = new QParameter(QStringLiteral("ambient"), QVector3D(0.2f, 0.2f, 0.2f));
    m_defaultTechnique->addParameter(ka);
    basicPass->addBinding(new QParameterMapping(QStringLiteral("ambient"), QStringLiteral("ka"), QParameterMapping::Uniform));

    QParameter* kd = new QParameter(QStringLiteral("diffuse"), QVector3D(1.0f, 0.5f, 0.0f));
    m_defaultTechnique->addParameter(kd);
    basicPass->addBinding(new QParameterMapping(QStringLiteral("diffuse"), QStringLiteral("kd"), QParameterMapping::Uniform));

    QParameter* ks = new QParameter(QStringLiteral("specular"), QVector3D(0.01f, 0.01f, 0.01f));
    m_defaultTechnique->addParameter(ks);
    basicPass->addBinding(new QParameterMapping(QStringLiteral("specular"), QStringLiteral("ks"), QParameterMapping::Uniform));

    m_defaultTechnique->addParameter(new QParameter(QStringLiteral("shininess"), 150.0f));
}

void Renderer::buildDefaultMaterial()
{
    m_defaultMaterial = new QMaterial();
    m_defaultMaterial->setObjectName(QStringLiteral("DefaultMaterial"));
    m_defaultMaterial->addParameter(new QParameter(QStringLiteral("ambient"), QVector3D(0.2f, 0.2f, 0.2f)));
    m_defaultMaterial->addParameter(new QParameter(QStringLiteral("diffuse"), QVector3D(1.0f, 0.5f, 0.0f)));
    m_defaultMaterial->addParameter(new QParameter(QStringLiteral("specular"), QVector3D(0.01f, 0.01f, 0.01f)));
    m_defaultMaterial->addParameter(new QParameter(QStringLiteral("shininess"), 150.0f));

    QEffect* defEff = new QEffect;
    defEff->addTechnique(m_defaultTechnique);
    m_defaultMaterial->setEffect(defEff);
}

void Renderer::createAllocators(QAbstractAspectJobManager *jobManager)
{
    // Issue a set of jobs to create an allocator in TLS for each worker thread
    Q_ASSERT(jobManager);
    jobManager->waitForPerThreadFunction(Renderer::createThreadLocalAllocator, this);
}

void Renderer::destroyAllocators(Qt3DCore::QAbstractAspectJobManager *jobManager)
{
    // Issue a set of jobs to destroy the allocator in TLS for each worker thread
    Q_ASSERT(jobManager);
    jobManager->waitForPerThreadFunction(Renderer::destroyThreadLocalAllocator, this);
}

QThreadStorage<QFrameAllocator *> *Renderer::tlsAllocators()
{
    return &m_tlsAllocators;
}

/*!
 * For each worker thread we create a QFrameAllocatorQueue which contains m_cachedFrameCount + 1
 * QFrameAllocators. We need an additional QFrameAllocator otherwise we may be clearing the QFrameAllocator
 * of the frame we are currently rendering.
 */
void Renderer::createThreadLocalAllocator(void *renderer)
{
    Q_ASSERT(renderer);
    Renderer *theRenderer = static_cast<Renderer *>(renderer);
    if (!theRenderer->tlsAllocators()->hasLocalData()) {
        // RenderView has a sizeof 72
        // RenderCommand has a sizeof 128
        // QMatrix4x4 has a sizeof 68
        // May need to fine tune parameters passed to QFrameAllocator for best performances
        QFrameAllocator *allocator = new QFrameAllocator(192, 16, 128);
        theRenderer->tlsAllocators()->setLocalData(allocator);

        // Add the allocator to the renderer
        // so that it can be accessed
        theRenderer->addAllocator(allocator);
    }
}


/*!
 * Returns the a FrameAllocator for the caller thread.
 */
Qt3DCore::QFrameAllocator *Renderer::currentFrameAllocator()
{
    // return the QFrameAllocator for the current thread
    // It is never cleared as each renderview when it is destroyed
    // takes care of releasing anything that may have been allocated
    // using the allocator
    return m_tlsAllocators.localData();
}

void Renderer::destroyThreadLocalAllocator(void *renderer)
{
    Q_ASSERT(renderer);
    Renderer *theRenderer = static_cast<Renderer *>(renderer);
    if (theRenderer->tlsAllocators()->hasLocalData()) {
        QFrameAllocator *allocator = theRenderer->tlsAllocators()->localData();
        allocator->clear();
        // Setting the local data to null actually deletes the allocatorQeue
        // as the tls object takes ownership of pointers
        theRenderer->tlsAllocators()->setLocalData(Q_NULLPTR);
    }
}

// Called in RenderThread context by the run method of RenderThread
// RenderThread has locked the mutex already and unlocks it when this
// method termintates
void Renderer::initialize()
{
    m_graphicsContext.reset(new GraphicsContext);
    m_graphicsContext->setRenderer(this);

    QOpenGLContext* ctx = m_glContext ? m_glContext : new QOpenGLContext;

    // If we are using our own context (not provided by QtQuick),
    // we need to create it
    if (!m_glContext) {
        // TO DO: Shouldn't we use the highest context available and trust
        // QOpenGLContext to fall back on the best lowest supported ?
        const QByteArray debugLoggingMode = qgetenv("QT3DRENDER_DEBUG_LOGGING");

        if (!debugLoggingMode.isEmpty()) {
            QSurfaceFormat sf = ctx->format();
            sf.setOption(QSurfaceFormat::DebugContext);
            ctx->setFormat(sf);
        }

        // Create OpenGL context
        if (ctx->create())
            qCDebug(Backend) << "OpenGL context created with actual format" << ctx->format();
        else
            qCWarning(Backend) << Q_FUNC_INFO << "OpenGL context creation failed";
    }

    // Note: we don't have a surface at this point
    // The context will be made current later on (at render time)
    m_graphicsContext->setOpenGLContext(ctx);

    // Awake setScenegraphRoot in case it was waiting
    m_waitForInitializationToBeCompleted.release(1);
    // Allow the aspect manager to proceed
    m_vsyncFrameAdvanceService->proceedToNextFrame();
}

/*!
    \internal

    Called in the context of the RenderThread to do any shutdown and cleanup
    that needs to be performed in the thread where the OpenGL context lives
*/
void Renderer::shutdown()
{
    // Clean up the graphics context
    m_graphicsContext.reset(Q_NULLPTR);
    qCDebug(Backend) << Q_FUNC_INFO << "Renderer properly shutdown";
}

void Renderer::setSurfaceExposed(bool exposed)
{
    qCDebug(Backend) << "Window exposed: " << exposed;
    m_exposed.fetchAndStoreOrdered(exposed);
}

void Renderer::setFrameGraphRoot(const Qt3DCore::QNodeId fgRootId)
{
    m_frameGraphRootUuid = fgRootId;
    qCDebug(Backend) << Q_FUNC_INFO << m_frameGraphRootUuid;
}

Render::FrameGraphNode *Renderer::frameGraphRoot() const
{
    return m_nodesManager->frameGraphManager()->lookupNode(m_frameGraphRootUuid);
}

// QAspectThread context
// Order of execution :
// 1) RenderThread is created -> release 1 of m_waitForInitializationToBeCompleted when started
// 2) setSceneRoot waits to acquire initialization
// 3) submitRenderView -> check for surface
//    -> make surface current + create proper glHelper if needed
void Renderer::setSceneRoot(QBackendNodeFactory *factory, Entity *sgRoot)
{
    Q_ASSERT(sgRoot);

    // If initialization hasn't been completed we must wait
    m_waitForInitializationToBeCompleted.acquire();

    m_renderSceneRoot = sgRoot;
    if (!m_renderSceneRoot)
        qCWarning(Backend) << "Failed to build render scene";
    m_renderSceneRoot->dump();
    qCDebug(Backend) << Q_FUNC_INFO << "DUMPING SCENE";


    // Create the default materials ....
    // Needs a QOpenGLContext (for things like isOpenGLES ...)
    // TO DO: Maybe this should be moved elsewhere
    buildDefaultTechnique();
    buildDefaultMaterial();

    factory->createBackendNode(m_defaultMaterial);
    factory->createBackendNode(m_defaultMaterial->effect());
    factory->createBackendNode(m_defaultTechnique);
    factory->createBackendNode(m_defaultTechnique->renderPasses().first());
    factory->createBackendNode(m_defaultTechnique->renderPasses().first()->shaderProgram());

    // We create backend resources for all the parameters
    Q_FOREACH (QParameter *p, m_defaultMaterial->parameters())
        factory->createBackendNode(p);
    Q_FOREACH (QParameter *p, m_defaultTechnique->parameters())
        factory->createBackendNode(p);
    Q_FOREACH (QParameter *p, m_defaultMaterial->effect()->parameters())
        factory->createBackendNode(p);

    m_defaultMaterialHandle = nodeManagers()->lookupHandle<Material, MaterialManager, HMaterial>(m_defaultMaterial->id());
    m_defaultEffectHandle = nodeManagers()->lookupHandle<Effect, EffectManager, HEffect>(m_defaultMaterial->effect()->id());
    m_defaultTechniqueHandle = nodeManagers()->lookupHandle<Technique, TechniqueManager, HTechnique>(m_defaultTechnique->id());
    m_defaultRenderPassHandle = nodeManagers()->lookupHandle<RenderPass, RenderPassManager, HRenderPass>(m_defaultTechnique->renderPasses().first()->id());
    m_defaultRenderShader = nodeManagers()->lookupResource<Shader, ShaderManager>(m_defaultTechnique->renderPasses().first()->shaderProgram()->id());
}

void Renderer::registerEventFilter(QEventFilterService *service)
{
    qCDebug(Backend) << Q_FUNC_INFO << QThread::currentThread();
    service->registerEventFilter(m_pickEventFilter.data(), 1024);
}

void Renderer::setSettings(RendererSettings *settings)
{
    // If default settings not in use, clean up
    if (m_settings != &ms_defaultSettings)
        delete m_settings;

    // If removing settings, restore to default
    m_settings = (settings != Q_NULLPTR) ? settings : const_cast<RendererSettings *>(&ms_defaultSettings);
}

RendererSettings *Renderer::settings() const
{
    return m_settings;
}

void Renderer::render()
{
    // Traversing the framegraph tree from root to lead node
    // Allows us to define the rendering set up
    // Camera, RenderTarget ...

    // Utimately the renderer should be a framework
    // For the processing of the list of renderviews

    // Matrice update, bounding volumes computation ...
    // Should be jobs

    // namespace Qt3DCore has 2 distincts node trees
    // One scene description
    // One framegraph description

    while (m_running.load() > 0) {
        doRender();
        // TO DO: Restore windows exposed detection
        // Probably needs to happens some place else though
    }
}

void Renderer::doRender()
{
    // Render using current device state and renderer configuration
    const bool submissionSucceeded = submitRenderViews();

    // Note: submitRenderViews returns false when
    // * we cannot render because a shutdown has been scheduled
    // * the renderview is incomplete (only when rendering with a Scene3D)
    // Otherwise returns true even for cases like
    // * No render view
    // * No surface set
    // * OpenGLContext failed to be set current
    // This behavior is important as we need to
    // call proceedToNextFrame despite rendering errors that aren't fatal

    // Only reset renderQueue and proceed to next frame if the submission
    // succeeded or it we are using a render thread.

    // If submissionSucceeded isn't true this implies that something went wrong
    // with the rendering and/or the renderqueue is incomplete from some reason
    // (in the case of scene3d the render jobs may be taking too long ....)
    if (m_renderThread || submissionSucceeded) {
        // Reset the m_renderQueue so that we won't try to render
        // with a queue used by a previous frame with corrupted content
        // if the current queue was correctly submitted
        m_renderQueue->reset();

        if (m_running.load()) { // Are we still running ?
            // Make sure that all the RenderViews, RenderCommands,
            // UniformValues ... have been completely destroyed and are leak
            // free
            // Note: we can check for non render thread cases (scene3d)
            // only when we are sure that a full frame was previously submitted
            // (submissionSucceeded == true)
            Q_FOREACH (QFrameAllocator *allocator, m_allocators)
                Q_ASSERT(allocator->isEmpty());
        }
        // We allow the RenderTickClock service to proceed to the next frame
        // In turn this will allow the aspect manager to request a new set of jobs
        // to be performed for each aspect
        m_vsyncFrameAdvanceService->proceedToNextFrame();
    }
}

// Called by RenderViewJobs
void Renderer::enqueueRenderView(Render::RenderView *renderView, int submitOrder)
{
    QMutexLocker locker(&m_mutex); // Prevent out of order execution
    // We cannot use a lock free primitive here because:
    // - QVector is not thread safe
    // - Even if the insert is made correctly, the isFrameComplete call
    //   could be invalid since depending on the order of execution
    //   the counter could be complete but the renderview not yet added to the
    //   buffer depending on whichever order the cpu decides to process this
    if (m_renderQueue->queueRenderView(renderView, submitOrder)) {
        if (m_renderThread && m_running.load())
            Q_ASSERT(m_submitRenderViewsSemaphore.available() == 0);
        m_submitRenderViewsSemaphore.release(1);
    }
}

bool Renderer::canRender() const
{
    // Make sure that we've not been told to terminate whilst waiting on
    // the above wait condition
    if (m_renderThread && !m_running.load()) {
        qCDebug(Rendering) << "RenderThread termination requested whilst waiting";
        return false;
    }

    // TO DO: Check if all surfaces have been destroyed...
    // It may be better if the last window to be closed trigger a call to shutdown
    // Rather than having checks for the surface everywhere

    return true;
}

// Happens in RenderThread context when all RenderViewJobs are done
bool Renderer::submitRenderViews()
{
    // If we are using a render thread, make sure that
    // we've been told to render before rendering
    if (m_renderThread) { // Prevent ouf of order execution
        m_submitRenderViewsSemaphore.acquire(1);
        // When using Thread rendering, the semaphore should only
        // be released when the frame queue is complete and there's
        // something to render
        // The case of shutdown should have been handled just before
        Q_ASSERT(m_renderQueue->isFrameQueueComplete());
    } else {

        // When using synchronous rendering (QtQuick)
        // We are not sure that the frame queue is actually complete
        // Since a call to render may not be synched with the completions
        // of the RenderViewJobs
        // In such a case we return early, waiting for a next call with
        // the frame queue complete at this point
        QMutexLocker locker(&m_mutex);
        if (!m_renderQueue->isFrameQueueComplete())
            return false;
    }

    QElapsedTimer timer;
    quint64 queueElapsed = 0;
    timer.start();

    // Lock the mutex to protect access to m_surface and check if we are still set
    // to the running state and that we have a valid surface on which to draw
    // TO DO: Is that still needed given the surface changes
    QMutexLocker locker(&m_mutex);
    const QVector<Render::RenderView *> renderViews = m_renderQueue->nextFrameQueue();
    if (!canRender()) {
        qDeleteAll(renderViews);
        return false;
    }

    const int renderViewsCount = renderViews.size();
    quint64 frameElapsed = queueElapsed;

    // Early return if there's actually nothing to render
    if (renderViewsCount <= 0)
        return true;

    qCDebug(Memory) << Q_FUNC_INFO << "rendering frame ";

    // We might not want to render on the default FBO
    bool boundFboIdValid = false;
    GLuint boundFboId = 0;
    QColor previousClearColor = renderViews.first()->clearColor();
    QSurface *surface = Q_NULLPTR;
    QSurface *previousSurface = Q_NULLPTR;
    for (int i = 0; i < renderViewsCount; ++i) {
        // Initialize GraphicsContext for drawing
        // If the RenderView has a RenderStateSet defined
        const RenderView *renderView = renderViews.at(i);

        // Check if using the same surface as the previous RenderView.
        // If not, we have to free up the context from the previous surface
        // and make the context current on the new surface
        surface = renderView->surface();

        // TO DO: Make sure that the surface we are rendering too has not been unset

        // For now, if we do not have a surface, skip this renderview
        // TODO: Investigate if it's worth providing a fallback offscreen surface
        //       to use when surface is null. Or if we should instead expose an
        //       offscreensurface to Qt3D.
        if (!surface)
            continue;

        if (surface != previousSurface && previousSurface)
            m_graphicsContext->endDrawing(boundFboId == m_graphicsContext->defaultFBO());

        if (surface != previousSurface) {
            // If we can't make the context current on the surface, skip to the
            // next RenderView. We won't get the full frame but we may get something
            if (!m_graphicsContext->beginDrawing(surface, previousClearColor)) {
                qWarning() << "Failed to make OpenGL context current on surface";
                continue;
            }

            previousSurface = surface;

            if (!boundFboIdValid) {
                boundFboIdValid = true;
                boundFboId = m_graphicsContext->boundFrameBufferObject();
            }

            // Reset state to the default state
            m_graphicsContext->setCurrentStateSet(m_defaultRenderStateSet);
        }

        // Set RenderView render state
        RenderStateSet *renderViewStateSet = renderView->stateSet();
        if (renderViewStateSet)
            m_graphicsContext->setCurrentStateSet(renderViewStateSet);

        // Set RenderTarget ...
        // Activate RenderTarget
        m_graphicsContext->activateRenderTarget(nodeManagers()->data<RenderTarget, RenderTargetManager>(renderView->renderTargetHandle()),
                                                renderView->attachmentPack(), boundFboId);

        // Set clear color if different
        if (previousClearColor != renderView->clearColor()) {
            previousClearColor = renderView->clearColor();
            m_graphicsContext->clearColor(previousClearColor);
        }

        // Clear BackBuffer
        m_graphicsContext->clearBackBuffer(renderView->clearBuffer());

        // Set the Viewport
        m_graphicsContext->setViewport(renderView->viewport(), renderView->surfaceSize() * renderView->devicePixelRatio());

        // Execute the render commands
        executeCommands(renderView);

        // executeCommands takes care of restoring the stateset to the value
        // of gc->currentContext() at the moment it was called (either
        // renderViewStateSet or m_defaultRenderStateSet)

        frameElapsed = timer.elapsed() - frameElapsed;
        qCDebug(Rendering) << Q_FUNC_INFO << "Submitted Renderview " << i + 1 << "/" << renderViewsCount  << "in " << frameElapsed << "ms";
        frameElapsed = timer.elapsed();
    }

    if (surface) {
        // Reset state to the default state if the last stateset is not the
        // defaultRenderStateSet
        if (m_graphicsContext->currentStateSet() != m_defaultRenderStateSet)
            m_graphicsContext->setCurrentStateSet(m_defaultRenderStateSet);

        // Finish up with last surface used in the list of RenderViews
        m_graphicsContext->endDrawing(boundFboId == m_graphicsContext->defaultFBO());
    }
    // Delete all the RenderViews which will clear the allocators
    // that were used for their allocation
    qDeleteAll(renderViews);

    queueElapsed = timer.elapsed() - queueElapsed;
    qCDebug(Rendering) << Q_FUNC_INFO << "Submission of Queue in " << queueElapsed << "ms <=> " << queueElapsed / renderViewsCount << "ms per RenderView <=> Avg " << 1000.0f / (queueElapsed * 1.0f/ renderViewsCount * 1.0f) << " RenderView/s";
    qCDebug(Rendering) << Q_FUNC_INFO << "Submission Completed in " << timer.elapsed() << "ms";

    return true;
}

// Waits to be told to create jobs for the next frame
// Called by QRenderAspect jobsToExecute context of QAspectThread
QVector<Qt3DCore::QAspectJobPtr> Renderer::renderBinJobs()
{
    // Traverse the current framegraph. For each leaf node create a
    // RenderView and set its configuration then create a job to
    // populate the RenderView with a set of RenderCommands that get
    // their details from the RenderNodes that are visible to the
    // Camera selected by the framegraph configuration
    QVector<QAspectJobPtr> renderBinJobs;

    FrameGraphVisitor visitor;
    visitor.traverse(frameGraphRoot(), this, &renderBinJobs);

    // Set target number of RenderViews
    m_renderQueue->setTargetRenderViewCount(renderBinJobs.size());
    return renderBinJobs;
}

QAspectJobPtr Renderer::pickBoundingVolumeJob()
{
    // Clear any previous dependency not valid anymore
    if (!m_pickBoundingVolumeJob)
        m_pickBoundingVolumeJob.reset(new PickBoundingVolumeJob(this));
    m_pickBoundingVolumeJob->removeDependency(QWeakPointer<QAspectJob>());
    m_pickBoundingVolumeJob->setRoot(m_renderSceneRoot);
    return m_pickBoundingVolumeJob;
}

// Called during while traversing the FrameGraph for each leaf node context of QAspectThread
Qt3DCore::QAspectJobPtr Renderer::createRenderViewJob(FrameGraphNode *node, int submitOrderIndex)
{
    RenderViewJobPtr job(new RenderViewJob);
    job->setRenderer(this);
    //    if (m_surface)
    //        job->setSurfaceSize(m_surface->size());
    // TO DO: the surface size can only be set by the RenderView
    // since the only the RenderView will know about the surface
    // it should be renderer onto
    job->setFrameGraphLeafNode(node);
    job->setSubmitOrderIndex(submitOrderIndex);
    return job;
}

QAbstractFrameAdvanceService *Renderer::frameAdvanceService() const
{
    return static_cast<Qt3DCore::QAbstractFrameAdvanceService *>(m_vsyncFrameAdvanceService.data());
}

// Called by RenderView->submit() in RenderThread context
void Renderer::executeCommands(const RenderView *rv)
{
    // Render drawing commands
    const QVector<RenderCommand *> commands = rv->commands();

    // Use the graphicscontext to submit the commands to the underlying
    // graphics API (OpenGL)

    // Save the RenderView base stateset
    RenderStateSet *globalState = m_graphicsContext->currentStateSet();
    OpenGLVertexArrayObject *vao = Q_NULLPTR;
    HVao previousVaoHandle;

    Q_FOREACH (RenderCommand *command, commands) {

        if (command->m_type == RenderCommand::Compute) {
            Shader *shader = m_nodesManager->data<Shader, ShaderManager>(command->m_shader);
            if (shader != Q_NULLPTR) {
                m_graphicsContext->activateShader(shader);
                m_graphicsContext->setParameters(command->m_parameterPack);

                const int *workGroups = rv->computeWorkGroups();
                m_graphicsContext->dispatchCompute(workGroups[0],
                        workGroups[1],
                        workGroups[2]);

                int err = m_graphicsContext->openGLContext()->functions()->glGetError();
                if (err)
                    qCWarning(Rendering) << "GL error after drawing mesh:" << QString::number(err, 16);
            }
        } else { // Draw Command

            // Check if we have a valid GeometryRenderer + Geometry
            Geometry *rGeometry = m_nodesManager->data<Geometry, GeometryManager>(command->m_geometry);
            GeometryRenderer *rGeometryRenderer = m_nodesManager->data<GeometryRenderer, GeometryRendererManager>(command->m_geometryRenderer);
            const bool hasGeometryRenderer = rGeometry != Q_NULLPTR && rGeometryRenderer != Q_NULLPTR && !rGeometry->attributes().isEmpty();

            if (!hasGeometryRenderer) {
                qCWarning(Rendering) << "RenderCommand should have a mesh to render";
                continue;
            }

            Shader *shader = m_nodesManager->data<Shader, ShaderManager>(command->m_shader);
            if (shader == Q_NULLPTR) {
                shader = m_defaultRenderShader;
                command->m_parameterAttributeToShaderNames = m_defaultParameterToGLSLAttributeNames;
                command->m_parameterPack = m_defaultUniformPack;
            }

            // The VAO should be created only once for a QGeometry and a ShaderProgram
            // Manager should have a VAO Manager that are indexed by QMeshData and Shader
            // RenderCommand should have a handle to the corresponding VAO for the Mesh and Shader
            bool needsToBindVAO = false;
            VAOManager *vaoManager = m_nodesManager->vaoManager();
            if (m_graphicsContext->supportsVAO()) {
                command->m_vao = vaoManager->lookupHandle(QPair<HGeometry, HShader>(command->m_geometry, command->m_shader));

                if (command->m_vao.isNull()) {
                    qCDebug(Rendering) << Q_FUNC_INFO << "Allocating new VAO";
                    command->m_vao = vaoManager->getOrAcquireHandle(QPair<HGeometry, HShader>(command->m_geometry, command->m_shader));
                    vaoManager->data(command->m_vao)->setVao(new QOpenGLVertexArrayObject());
                    vaoManager->data(command->m_vao)->create();
                }

                if (previousVaoHandle != command->m_vao) {
                    needsToBindVAO = true;
                    previousVaoHandle = command->m_vao;
                    vao = vaoManager->data(command->m_vao);
                }
                Q_ASSERT(vao);
            }

            //// We activate the shader here
            // This will fill the attributes & uniforms info the first time the shader is loaded
            m_graphicsContext->activateShader(shader);

            //// Initialize GL
            // The initialization is performed only once parameters in the command are set
            // Which indicates that the shader has been initialized and that renderview jobs were able to retrieve
            // Uniform and Attributes info from the shader
            // Otherwise we might create a VAO without attribute bindings as the RenderCommand had no way to know about attributes
            // Before the shader was loader
            Attribute *indexAttribute = Q_NULLPTR;
            bool specified = false;
            const bool requiresVAOUpdate = (!vao || !vao->isSpecified()) || (rGeometry->isDirty() || rGeometryRenderer->isDirty());
            GLsizei primitiveCount = rGeometryRenderer->primitiveCount();

            // Append dirty Geometry to temporary vector
            // so that its dirtiness can be unset later
            if (rGeometry->isDirty())
                m_dirtyGeometry.push_back(rGeometry);

            if (needsToBindVAO && vao) {
                vao->bind();
            }

            if (!command->m_parameterAttributeToShaderNames.isEmpty()) {
                // Update or set Attributes and Buffers for the given rGeometry and Command
                indexAttribute = updateBuffersAndAttributes(rGeometry, command, primitiveCount, requiresVAOUpdate);
                specified = true;
                if (vao)
                    vao->setSpecified(true);
            }
            //// Update program uniforms
            m_graphicsContext->setParameters(command->m_parameterPack);

            //// Draw Calls
            // Set state
            RenderStateSet *localState = command->m_stateSet;

            // Merge the RenderCommand state with the globalState of the RenderView
            // Or restore the globalState if no stateSet for the RenderCommand
            if (localState != Q_NULLPTR) {
                command->m_stateSet->merge(globalState);
                m_graphicsContext->setCurrentStateSet(command->m_stateSet);
            } else {
                m_graphicsContext->setCurrentStateSet(globalState);
            }
            // All Uniforms for a pass are stored in the QUniformPack of the command
            // Uniforms for Effect, Material and Technique should already have been correctly resolved
            // at that point
            if (primitiveCount && (specified || (vao && vao->isSpecified()))) {
                const GLint primType = rGeometryRenderer->primitiveType();
                const bool drawInstanced = rGeometryRenderer->instanceCount() > 1;
                const bool drawIndexed = indexAttribute != Q_NULLPTR;
                const GLint indexType = drawIndexed ? GraphicsContext::glDataTypeFromAttributeDataType(indexAttribute->dataType()) : 0;

                if (rGeometryRenderer->primitiveType() == QGeometryRenderer::Patches)
                    m_graphicsContext->setVerticesPerPatch(rGeometry->verticesPerPatch());

                if (rGeometryRenderer->primitiveRestart())
                    m_graphicsContext->enablePrimitiveRestart(rGeometryRenderer->restartIndex());

                // TO DO: Add glMulti Draw variants
                if (!drawInstanced) { // Non instanced Rendering
                    if (drawIndexed)
                        m_graphicsContext->drawElements(primType,
                                                        primitiveCount,
                                                        indexType,
                                                        reinterpret_cast<void*>(quintptr(indexAttribute->byteOffset())),
                                                        rGeometryRenderer->baseVertex());

                    else
                        m_graphicsContext->drawArrays(primType, 0, primitiveCount);
                } else { // Instanced Rendering
                    if (drawIndexed)
                        m_graphicsContext->drawElementsInstanced(primType,
                                                                 primitiveCount,
                                                                 indexType,
                                                                 reinterpret_cast<void*>(quintptr(indexAttribute->byteOffset())),
                                                                 rGeometryRenderer->instanceCount());
                    else
                        m_graphicsContext->drawArraysInstanced(primType,
                                                               rGeometryRenderer->baseInstance(),
                                                               primitiveCount,
                                                               rGeometryRenderer->instanceCount());
                }

                int err = m_graphicsContext->openGLContext()->functions()->glGetError();
                if (err)
                    qCWarning(Rendering) << "GL error after drawing mesh:" << QString::number(err, 16);

                if (rGeometryRenderer->primitiveRestart())
                    m_graphicsContext->disablePrimitiveRestart();


                // Unset dirtiness on rGeometryRenderer only
                // The rGeometry may be shared by several rGeometryRenderer
                // so we cannot unset its dirtiness at this point
                rGeometryRenderer->unsetDirty();

            }
        }
    } // end of RenderCommands loop

    // We cache the VAO and release it only at the end of the exectute frame
    // We try to minimize VAO binding between RenderCommands
    if (vao)
        vao->release();

    // Reset to the state we were in before executing the render commands
    m_graphicsContext->setCurrentStateSet(globalState);

    // Unset dirtiness on Geometry and Attributes
    Q_FOREACH (Attribute *attribute, m_dirtyAttributes)
        attribute->unsetDirty();
    m_dirtyAttributes.clear();

    Q_FOREACH (Geometry *geometry, m_dirtyGeometry)
        geometry->unsetDirty();
    m_dirtyGeometry.clear();
}

Attribute *Renderer::updateBuffersAndAttributes(Geometry *geometry, RenderCommand *command, GLsizei &count, bool forceUpdate)
{
    Attribute *indexAttribute = Q_NULLPTR;
    uint estimatedCount = 0;

    m_dirtyAttributes.reserve(m_dirtyAttributes.size() + geometry->attributes().size());
    Q_FOREACH (const QNodeId &attributeId, geometry->attributes()) {
        // TO DO: Improvement we could store handles and use the non locking policy on the attributeManager
        Attribute *attribute = m_nodesManager->attributeManager()->lookupResource(attributeId);

        if (attribute == Q_NULLPTR)
            continue;

        Buffer *buffer = m_nodesManager->bufferManager()->lookupResource(attribute->bufferId());

        if (buffer == Q_NULLPTR)
            continue;

        if (buffer->isDirty()) {
            // Reupload buffer data
            m_graphicsContext->updateBuffer(buffer);

            // Clear dirtiness of buffer so that it is not reuploaded every frame
            buffer->unsetDirty();
        }

        // Update attribute and create buffer if needed

        // Index Attribute
        if (attribute->attributeType() == QAttribute::IndexAttribute) {
            if (attribute->isDirty() || forceUpdate)
                m_graphicsContext->specifyIndices(buffer);
            indexAttribute = attribute;
            // Vertex Attribute
        } else if (command->m_parameterAttributeToShaderNames.contains(attribute->name())) {
            if (attribute->isDirty() || forceUpdate)
                m_graphicsContext->specifyAttribute(attribute, buffer, command->m_parameterAttributeToShaderNames.value(attribute->name()));
            estimatedCount = qMax(attribute->count(), estimatedCount);
        }

        // Append attribute to temporary vector so that its dirtiness
        // can be cleared at the end of the frame
        m_dirtyAttributes.push_back(attribute);

        // Note: We cannont call unsertDirty on the Attributeat this
        // point as we don't know if the attributes are being shared
        // with other geometry / geometryRenderer in which case they still
        // should remain dirty so that VAO for these commands are properly
        // updated
    }

    // If the count was not specified by the geometry renderer
    // we set it to what we estimated it to be
    if (count == 0)
        count = indexAttribute ? indexAttribute->count() : estimatedCount;
    return indexAttribute;
}

void Renderer::addAllocator(Qt3DCore::QFrameAllocator *allocator)
{
    QMutexLocker lock(&m_mutex);
    m_allocators.append(allocator);
}

QList<QMouseEvent> Renderer::pendingPickingEvents() const
{
    return m_pickEventFilter->pendingEvents();
}

QGraphicsApiFilter *Renderer::contextInfo() const
{
    return m_graphicsContext->contextInfo();
}

} // namespace Render
} // namespace Qt3DRender

QT_END_NAMESPACE