summaryrefslogtreecommitdiffstats
path: root/src/opengl/qopengltimerquery.cpp
blob: d5ef875efa21326444e57e111e9cc796645304bb (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
/****************************************************************************
**
** Copyright (C) 2013 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtOpenGL module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qopengltimerquery.h"

#include "qopenglqueryhelper_p.h"
#include <QtCore/private/qobject_p.h>
#include <QtGui/QOpenGLContext>
#include <QtGui/QOpenGLFunctions>

QT_BEGIN_NAMESPACE

// Helper class used as fallback if OpenGL <3.3 is being used with EXT_timer_query
class QExtTimerQueryHelper
{
public:
    QExtTimerQueryHelper(QOpenGLContext *context)
    {
        Q_ASSERT(context);
        GetQueryObjectui64vEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLuint64EXT *)>(context->getProcAddress("glGetQueryObjectui64vEXT"));
        GetQueryObjecti64vEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLint64EXT *)>(context->getProcAddress("glGetQueryObjecti64vEXT"));
    }

    inline void glGetQueryObjectui64vEXT(GLuint id, GLenum pname, GLuint64EXT *params)
    {
        GetQueryObjectui64vEXT(id, pname, params);
    }

    inline void glGetQueryObjecti64vEXT(GLuint id, GLenum pname, GLint64EXT *params)
    {
        GetQueryObjecti64vEXT(id, pname, params);
    }

private:
    void (QOPENGLF_APIENTRYP GetQueryObjectui64vEXT)(GLuint id, GLenum pname, GLuint64EXT *params);
    void (QOPENGLF_APIENTRYP GetQueryObjecti64vEXT)(GLuint id, GLenum pname, GLint64EXT *params);
};

class QOpenGLTimerQueryPrivate : public QObjectPrivate
{
public:
    QOpenGLTimerQueryPrivate()
        : QObjectPrivate(),
          context(nullptr),
          ext(nullptr),
          timeInterval(0),
          timer(0)
    {
    }

    ~QOpenGLTimerQueryPrivate()
    {
        delete core;
        delete ext;
    }

    bool create();
    void destroy();
    void begin();
    void end();
    GLuint64 waitForTimeStamp() const;
    void recordTimestamp();
    bool isResultAvailable() const;
    GLuint64 result() const;

    // There are several cases we must handle:
    //   OpenGL >=3.3 includes timer queries as a core feature
    //   ARB_timer_query has same functionality as above. Requires OpenGL 3.2
    //   EXT_timer_query offers limited support. Can be used with OpenGL >=1.5
    //
    // Note that some implementations (OS X) provide OpenGL 3.2 but do not expose the
    // ARB_timer_query extension. In such situations we must also be able to handle
    // using the EXT_timer_query extension with any version of OpenGL.
    //
    // OpenGL 1.5 or above contains the generic query API and OpenGL 3.3 and
    // ARB_timer_query provide the 64-bit query API. These are wrapped by
    // QOpenGLQueryHelper. All we need to handle in addition is the EXT_timer_query
    // case and to take care not to call the Core/ARB functions when we only
    // have EXT_timer_query available.
    QOpenGLContext *context;
    QOpenGLQueryHelper *core;
    QExtTimerQueryHelper *ext;
    mutable GLuint64 timeInterval;
    GLuint timer;
};

bool QOpenGLTimerQueryPrivate::create()
{
    QOpenGLContext *ctx = QOpenGLContext::currentContext();

    if (timer && context == ctx)
        return true;

    context = ctx;
    if (!context) {
        qWarning("A current OpenGL context is required to create timer query objects");
        return false;
    }

    if (context->isOpenGLES()) {
        qWarning("QOpenGLTimerQuery: Not supported on OpenGL ES");
        return false;
    }

    // Resolve the functions provided by OpenGL 1.5 and OpenGL 3.3 or ARB_timer_query
    core = new QOpenGLQueryHelper(context);

    // Check to see if we also need to resolve the functions for EXT_timer_query
    QSurfaceFormat f = context->format();
    if (f.version() <= qMakePair(3, 2)
        && !context->hasExtension(QByteArrayLiteral("GL_ARB_timer_query"))
        && context->hasExtension(QByteArrayLiteral("GL_EXT_timer_query"))) {
        ext = new QExtTimerQueryHelper(context);
    } else if (f.version() <= qMakePair(3, 2)
               && !context->hasExtension(QByteArrayLiteral("GL_ARB_timer_query"))
               && !context->hasExtension(QByteArrayLiteral("GL_EXT_timer_query"))) {
        qWarning("QOpenGLTimerQuery requires one of:\n"
                 "  OpenGL 3.3 or newer,\n"
                 "  OpenGL 3.2 and the ARB_timer_query extension\n"
                 "  or the EXT_timer query extension");
        return false;
    }

    core->glGenQueries(1, &timer);
    return (timer != 0);
}

void QOpenGLTimerQueryPrivate::destroy()
{
    if (!timer)
        return;

    core->glDeleteQueries(1, &timer);
    timer = 0;
    context = nullptr;
}

// GL_TIME_ELAPSED_EXT is not defined on OS X 10.6
#if !defined(GL_TIME_ELAPSED_EXT)
#define GL_TIME_ELAPSED_EXT 0x88BF
#endif

// GL_TIME_ELAPSED is not defined on OS X 10.7 or 10.8 yet
#if !defined(GL_TIME_ELAPSED)
#define GL_TIME_ELAPSED GL_TIME_ELAPSED_EXT
#endif

void QOpenGLTimerQueryPrivate::begin()
{
    core->glBeginQuery(GL_TIME_ELAPSED, timer);
}

void QOpenGLTimerQueryPrivate::end()
{
    core->glEndQuery(GL_TIME_ELAPSED);
}

void QOpenGLTimerQueryPrivate::recordTimestamp()
{
    // Don't call glQueryCounter if we only have EXT_timer_query
#if defined(GL_TIMESTAMP)
    if (!ext)
        core->glQueryCounter(timer, GL_TIMESTAMP);
    else
        qWarning("QOpenGLTimerQuery::recordTimestamp() requires OpenGL 3.3 or GL_ARB_timer_query");
#else
    qWarning("QOpenGLTimerQuery::recordTimestamp() requires OpenGL 3.3 or GL_ARB_timer_query");
#endif
}

GLuint64 QOpenGLTimerQueryPrivate::waitForTimeStamp() const
{
    GLint64 tmp = 0;
#if defined(GL_TIMESTAMP)
    if (!ext)
        core->glGetInteger64v(GL_TIMESTAMP, &tmp);
    else
        qWarning("QOpenGLTimerQuery::waitForTimestamp() requires OpenGL 3.3 or GL_ARB_timer_query");
#else
    qWarning("QOpenGLTimerQuery::waitForTimestamp() requires OpenGL 3.3 or GL_ARB_timer_query");
#endif
    GLuint64 timestamp(tmp);
    return timestamp;
}

bool QOpenGLTimerQueryPrivate::isResultAvailable() const
{
    GLuint available = GL_FALSE;
    core->glGetQueryObjectuiv(timer, GL_QUERY_RESULT_AVAILABLE, &available);
    return available;
}

GLuint64 QOpenGLTimerQueryPrivate::result() const
{
    if (!ext)
        core->glGetQueryObjectui64v(timer, GL_QUERY_RESULT, &timeInterval);
    else
        ext->glGetQueryObjectui64vEXT(timer, GL_QUERY_RESULT, &timeInterval);
    return timeInterval;
}

/*!
    \class QOpenGLTimerQuery
    \brief The QOpenGLTimerQuery class wraps an OpenGL timer query object.
    \inmodule QtOpenGL
    \since 5.1
    \ingroup painting-3D

    OpenGL timer query objects are OpenGL managed resources to measure the
    execution times of sequences of OpenGL commands on the GPU.

    OpenGL offers various levels of support for timer queries, depending on
    the version of OpenGL you have and the presence of the ARB_timer_query or
    EXT_timer_query extensions. The support can be summarized as:

    \list
        \li OpenGL >=3.3 offers full support for all timer query functionality.
        \li OpenGL 3.2 with the ARB_timer_query extension offers full support
            for all timer query functionality.
        \li OpenGL <=3.2 with the EXT_timer_query extension offers limited support
            in that the timestamp of the GPU cannot be queried. Places where this
            impacts functions provided by Qt classes will be highlighted in the
            function documentation.
        \li OpenGL ES 2 (and OpenGL ES 3) do not provide any support for OpenGL
            timer queries.
    \endlist

    OpenGL represents time with a granularity of 1 nanosecond (1e-9 seconds). As a
    consequence of this, 32-bit integers would only give a total possible duration
    of approximately 4 seconds, which would not be difficult to exceed in poorly
    performing or lengthy operations. OpenGL therefore uses 64 bit integer types
    to represent times. A GLuint64 variable has enough width to contain a duration
    of hundreds of years, which is plenty for real-time rendering needs.

    As with the other Qt OpenGL classes, QOpenGLTimerQuery has a create()
    function to create the underlying OpenGL object. This is to allow the developer to
    ensure that there is a valid current OpenGL context at the time.

    Once created, timer queries can be issued in one of several ways. The simplest
    method is to delimit a block of commands with calls to begin() and end(). This
    instructs OpenGL to measure the time taken from completing all commands issued
    prior to begin() until the completion of all commands issued prior to end().

    At the end of a frame we can retrieve the results by calling waitForResult().
    As this function's name implies, it blocks CPU execution until OpenGL notifies
    that the timer query result is available. To avoid blocking, you can check
    if the query result is available by calling isResultAvailable(). Note that
    modern GPUs are deeply pipelined and query results may not become available for
    between 1-5 frames after they were issued.

    Note that OpenGL does not permit nesting or interleaving of multiple timer queries
    using begin() and end(). Using multiple timer queries and recordTimestamp() avoids
    this limitation. When using recordTimestamp() the result can be obtained at
    some later time using isResultAvailable() and waitForResult(). Qt provides the
    convenience class QOpenGLTimeMonitor that helps with using multiple query objects.

    \sa QOpenGLTimeMonitor
*/

/*!
    Creates a QOpenGLTimerQuery instance with the given \a parent. You must call create()
    with a valid OpenGL context before using.
*/
QOpenGLTimerQuery::QOpenGLTimerQuery(QObject *parent)
    : QObject(*new QOpenGLTimerQueryPrivate, parent)
{
}

/*!
    Destroys the QOpenGLTimerQuery and the underlying OpenGL resource.
*/
QOpenGLTimerQuery::~QOpenGLTimerQuery()
{
    QOpenGLContext* ctx = QOpenGLContext::currentContext();

    Q_D(QOpenGLTimerQuery);
    QOpenGLContext *oldContext = nullptr;
    if (d->context != ctx) {
        oldContext = ctx;
        if (d->context->makeCurrent(oldContext->surface())) {
            ctx = d->context;
        } else {
            qWarning("QOpenGLTimerQuery::~QOpenGLTimerQuery() failed to make query objects's context current");
            ctx = nullptr;
        }
    }

    if (ctx)
        destroy();

    if (oldContext) {
        if (!oldContext->makeCurrent(oldContext->surface()))
            qWarning("QOpenGLTimerQuery::~QOpenGLTimerQuery() failed to restore current context");
    }
}

/*!
    Creates the underlying OpenGL timer query object. There must be a valid OpenGL context
    that supports query objects current for this function to succeed.

    Returns \c true if the OpenGL timer query object was successfully created.
*/
bool QOpenGLTimerQuery::create()
{
    Q_D(QOpenGLTimerQuery);
    return d->create();
}

/*!
    Destroys the underlying OpenGL timer query object. The context that was current when
    create() was called must be current when calling this function.
*/
void QOpenGLTimerQuery::destroy()
{
    Q_D(QOpenGLTimerQuery);
    d->destroy();
}

/*!
    Returns \c true if the underlying OpenGL query object has been created. If this
    returns \c true and the associated OpenGL context is current, then you are able to issue
    queries with this object.
*/
bool QOpenGLTimerQuery::isCreated() const
{
    Q_D(const QOpenGLTimerQuery);
    return (d->timer != 0);
}

/*!
    Returns the id of the underlying OpenGL query object.
*/
GLuint QOpenGLTimerQuery::objectId() const
{
    Q_D(const QOpenGLTimerQuery);
    return d->timer;
}

/*!
    Marks the start point in the OpenGL command queue for a sequence of commands to
    be timed by this query object.

    This is useful for simple use-cases. Usually it is better to use recordTimestamp().

    \sa end(), isResultAvailable(), waitForResult(), recordTimestamp()
*/
void QOpenGLTimerQuery::begin()
{
    Q_D(QOpenGLTimerQuery);
    d->begin();
}

/*!
    Marks the end point in the OpenGL command queue for a sequence of commands to
    be timed by this query object.

    This is useful for simple use-cases. Usually it is better to use recordTimestamp().

    \sa begin(), isResultAvailable(), waitForResult(), recordTimestamp()
*/
void QOpenGLTimerQuery::end()
{
    Q_D(QOpenGLTimerQuery);
    d->end();
}

/*!
    Places a marker in the OpenGL command queue for the GPU to record the timestamp
    when this marker is reached by the GPU. This function is non-blocking and the
    result will become available at some later time.

    The availability of the result can be checked with isResultAvailable(). The result
    can be fetched with waitForResult() which will block if the result is not yet
    available.

    \sa waitForResult(), isResultAvailable(), begin(), end()
*/
void QOpenGLTimerQuery::recordTimestamp()
{
    Q_D(QOpenGLTimerQuery);
    return d->recordTimestamp();
}

/*!
    Returns the current timestamp of the GPU when all previously issued OpenGL
    commands have been received but not necessarily executed by the GPU.

    This function blocks until the result is returned.

    \sa recordTimestamp()
*/
GLuint64 QOpenGLTimerQuery::waitForTimestamp() const
{
    Q_D(const QOpenGLTimerQuery);
    return d->waitForTimeStamp();
}

/*!
    Returns \c true if the OpenGL timer query result is available.

    This function is non-blocking and ideally should be used to check for the
    availability of the query result before calling waitForResult().

    \sa waitForResult()
*/
bool QOpenGLTimerQuery::isResultAvailable() const
{
    Q_D(const QOpenGLTimerQuery);
    return d->isResultAvailable();
}

/*!
    Returns the result of the OpenGL timer query.

    This function will block until the result is made available by OpenGL. It is
    recommended to call isResultAvailable() to ensure that the result is available
    to avoid unnecessary blocking and stalling.

    \sa isResultAvailable()
*/
GLuint64 QOpenGLTimerQuery::waitForResult() const
{
    Q_D(const QOpenGLTimerQuery);
    return d->result();
}


class QOpenGLTimeMonitorPrivate : public QObjectPrivate
{
public:
    QOpenGLTimeMonitorPrivate()
        : QObjectPrivate(),
          timers(),
          timeSamples(),
          context(nullptr),
          core(nullptr),
          ext(nullptr),
          requestedSampleCount(2),
          currentSample(-1),
          timerQueryActive(false)
    {
    }

    ~QOpenGLTimeMonitorPrivate()
    {
        delete core;
        delete ext;
    }

    bool create();
    void destroy();
    void recordSample();
    bool isResultAvailable() const;
    QVector<GLuint64> samples() const;
    QVector<GLuint64> intervals() const;
    void reset();

    QVector<GLuint> timers;
    mutable QVector<GLuint64> timeSamples;

    QOpenGLContext *context;
    QOpenGLQueryHelper *core;
    QExtTimerQueryHelper *ext;

    int requestedSampleCount;
    int currentSample;
    mutable bool timerQueryActive;
};

bool QOpenGLTimeMonitorPrivate::create()
{
    if (!timers.isEmpty() && timers.at(0) != 0 && timers.size() == requestedSampleCount)
        return true;

    QOpenGLContext *ctx = QOpenGLContext::currentContext();
    if (context && context != ctx) {
        qWarning("QTimeMonitor: Attempting to use different OpenGL context to recreate timers.\n"
                 "Please call destroy() first or use the same context to previously create");
        return false;
    }

    context = ctx;
    if (!context) {
        qWarning("A current OpenGL context is required to create timer query objects");
        return false;
    }

    // Resize the vectors that hold the timers and the recorded samples
    timers.resize(requestedSampleCount);
    timeSamples.resize(requestedSampleCount);

    // Resolve the functions provided by OpenGL 1.5 and OpenGL 3.3 or ARB_timer_query
    core = new QOpenGLQueryHelper(context);

    // Check to see if we also need to resolve the functions for EXT_timer_query
    QSurfaceFormat f = context->format();
    if (f.version() <= qMakePair(3, 2)
        && !context->hasExtension(QByteArrayLiteral("GL_ARB_timer_query"))
        && context->hasExtension(QByteArrayLiteral("GL_EXT_timer_query"))) {
        ext = new QExtTimerQueryHelper(context);
    } else if (f.version() <= qMakePair(3, 2)
               && !context->hasExtension(QByteArrayLiteral("GL_ARB_timer_query"))
               && !context->hasExtension(QByteArrayLiteral("GL_EXT_timer_query"))) {
        qWarning("QOpenGLTimeMonitor requires one of:\n"
                 "  OpenGL 3.3 or newer,\n"
                 "  OpenGL 3.2 and the ARB_timer_query extension\n"
                 "  or the EXT_timer query extension");
        return false;
    }

    core->glGenQueries(requestedSampleCount, timers.data());
    return (timers.at(0) != 0);
}

void QOpenGLTimeMonitorPrivate::destroy()
{
    if (timers.isEmpty() || timers.at(0) == 0)
        return;

    core->glDeleteQueries(timers.size(), timers.data());
    timers.clear();
    delete core;
    core = nullptr;
    delete ext;
    ext = nullptr;
    context = nullptr;
}

void QOpenGLTimeMonitorPrivate::recordSample()
{
    // Use glQueryCounter() and GL_TIMESTAMP where available.
    // Otherwise, simulate it with glBeginQuery()/glEndQuery()
    if (!ext) {
#if defined(GL_TIMESTAMP)
        core->glQueryCounter(timers.at(++currentSample), GL_TIMESTAMP);
#endif
    } else {
        if (currentSample == -1) {
            core->glBeginQuery(GL_TIME_ELAPSED_EXT, timers.at(++currentSample));
            timerQueryActive = true;
        } else if (currentSample < timers.size() - 1) {
            core->glEndQuery(GL_TIME_ELAPSED_EXT);
            core->glBeginQuery(GL_TIME_ELAPSED_EXT, timers.at(++currentSample));
        } else {
            if (timerQueryActive) {
                core->glEndQuery(GL_TIME_ELAPSED_EXT);
                timerQueryActive = false;
            }
        }
    }
}

bool QOpenGLTimeMonitorPrivate::isResultAvailable() const
{
    // The OpenGL spec says that if a query result is ready then the results of all queries
    // of the same type issued before it must also be ready. Therefore we only need to check
    // the availability of the result for the last issued query
    GLuint available = GL_FALSE;
    core->glGetQueryObjectuiv(timers.at(currentSample), GL_QUERY_RESULT_AVAILABLE, &available);
    return available;
}

QVector<GLuint64> QOpenGLTimeMonitorPrivate::samples() const
{
    // For the Core and ARB options just ask for the timestamp for each timer query.
    // For the EXT implementation we cannot obtain timestamps so we defer any result
    // collection to the intervals() function
    if (!ext) {
        for (int i = 0; i <= currentSample; ++i)
            core->glGetQueryObjectui64v(timers.at(i), GL_QUERY_RESULT, &timeSamples[i]);
    } else {
        qWarning("QOpenGLTimeMonitor::samples() requires OpenGL >=3.3\n"
                 "or OpenGL 3.2 and GL_ARB_timer_query");
    }
    return timeSamples;
}

QVector<GLuint64> QOpenGLTimeMonitorPrivate::intervals() const
{
    QVector<GLuint64> intervals(timers.size() - 1);
    if (!ext) {
        // Obtain the timestamp samples and calculate the interval durations
        const QVector<GLuint64> timeStamps = samples();
        for (int i = 0; i < intervals.size(); ++i)
            intervals[i] = timeStamps[i+1] - timeStamps[i];
    } else {
        // Stop the last timer if needed
        if (timerQueryActive) {
            core->glEndQuery(GL_TIME_ELAPSED_EXT);
            timerQueryActive = false;
        }

        // Obtain the results from all timers apart from the redundant last one. In this
        // case the results actually are the intervals not timestamps
        for (int i = 0; i < currentSample; ++i)
            ext->glGetQueryObjectui64vEXT(timers.at(i), GL_QUERY_RESULT, &intervals[i]);
    }

    return intervals;
}

void QOpenGLTimeMonitorPrivate::reset()
{
    currentSample = -1;
    timeSamples.fill(0);
}


/*!
    \class QOpenGLTimeMonitor
    \brief The QOpenGLTimeMonitor class wraps a sequence of OpenGL timer query objects.
    \inmodule QtOpenGL
    \since 5.1
    \ingroup painting-3D

    The QOpenGLTimeMonitor class is a convenience wrapper around a collection of OpenGL
    timer query objects used to measure intervals of time on the GPU to the level of
    granularity required by your rendering application.

    The OpenGL timer queries objects are queried in sequence to record the GPU
    timestamps at positions of interest in your rendering code. Once the results for
    all issues timer queries become available, the results can be fetched and
    QOpenGLTimerMonitor will calculate the recorded time intervals for you.

    The typical use case of this class is to either profile your application's rendering
    algorithms or to adjust those algorithms in real-time for dynamic performance/quality
    balancing.

    Prior to using QOpenGLTimeMonitor in your rendering function you should set the
    required number of sample points that you wish to record by calling setSamples(). Note
    that measuring N sample points will produce N-1 time intervals. Once you have set the
    number of sample points, call the create() function with a valid current OpenGL context
    to create the necessary query timer objects. These steps are usually performed just
    once in an initialization function.

    Use the recordSample() function to delimit blocks of code containing OpenGL commands
    that you wish to time. You can check availability of the resulting time
    samples and time intervals with isResultAvailable(). The calculated time intervals and
    the raw timestamp samples can be retrieved with the blocking waitForIntervals() and
    waitForSamples() functions respectively.

    After retrieving the results and before starting a new round of taking samples
    (for example, in the next frame) be sure to call the reset() function which will clear
    the cached results and reset the timer index back to the first timer object.

    \sa QOpenGLTimerQuery
*/

/*!
    Creates a QOpenGLTimeMonitor instance with the given \a parent. You must call create()
    with a valid OpenGL context before using.

    \sa setSampleCount(), create()
*/
QOpenGLTimeMonitor::QOpenGLTimeMonitor(QObject *parent)
    : QObject(*new QOpenGLTimeMonitorPrivate, parent)
{
}

/*!
    Destroys the QOpenGLTimeMonitor and any underlying OpenGL resources.
*/
QOpenGLTimeMonitor::~QOpenGLTimeMonitor()
{
    QOpenGLContext* ctx = QOpenGLContext::currentContext();

    Q_D(QOpenGLTimeMonitor);
    QOpenGLContext *oldContext = nullptr;
    if (d->context != ctx) {
        oldContext = ctx;
        if (d->context->makeCurrent(oldContext->surface())) {
            ctx = d->context;
        } else {
            qWarning("QOpenGLTimeMonitor::~QOpenGLTimeMonitor() failed to make time monitor's context current");
            ctx = nullptr;
        }
    }

    if (ctx)
        destroy();

    if (oldContext) {
        if (!oldContext->makeCurrent(oldContext->surface()))
            qWarning("QOpenGLTimeMonitor::~QOpenGLTimeMonitor() failed to restore current context");
    }
}

/*!
    Sets the number of sample points to \a sampleCount. After setting the number
    of samples with this function, you must call create() to instantiate the underlying
    OpenGL timer query objects.

    The new \a sampleCount must be at least 2.

    \sa sampleCount(), create(), recordSample()
*/
void QOpenGLTimeMonitor::setSampleCount(int sampleCount)
{
    // We need at least 2 samples to get an interval
    if (sampleCount < 2)
        return;
    Q_D(QOpenGLTimeMonitor);
    d->requestedSampleCount = sampleCount;
}

/*!
    Returns the number of sample points that have been requested with
    setSampleCount(). If create was successfully called following setSampleCount(),
    then the value returned will be the actual number of sample points
    that can be used.

    The default value for sample count is 2, leading to the measurement of a
    single interval.

    \sa setSampleCount()
*/
int QOpenGLTimeMonitor::sampleCount() const
{
    Q_D(const QOpenGLTimeMonitor);
    return d->requestedSampleCount;
}

/*!
    Instantiate sampleCount() OpenGL timer query objects that will be used
    to track the amount of time taken to execute OpenGL commands between
    successive calls to recordSample().

    Returns \c true if the OpenGL timer query objects could be created.

    \sa destroy(), setSampleCount(), recordSample()
*/
bool QOpenGLTimeMonitor::create()
{
    Q_D(QOpenGLTimeMonitor);
    return d->create();
}

/*!
    Destroys any OpenGL timer query objects used within this instance.

    \sa create()
*/
void QOpenGLTimeMonitor::destroy()
{
    Q_D(QOpenGLTimeMonitor);
    d->destroy();
}

/*!
    Returns \c true if the underlying OpenGL query objects have been created. If this
    returns \c true and the associated OpenGL context is current, then you are able to record
    time samples with this object.
*/
bool QOpenGLTimeMonitor::isCreated() const
{
    Q_D(const QOpenGLTimeMonitor);
    return (!d->timers.isEmpty() && d->timers.at(0) != 0);
}

/*!
    Returns a QVector containing the object Ids of the OpenGL timer query objects.
*/
QVector<GLuint> QOpenGLTimeMonitor::objectIds() const
{
    Q_D(const QOpenGLTimeMonitor);
    return d->timers;
}

/*!
    Issues an OpenGL timer query at this point in the OpenGL command queue. Calling this
    function in a sequence in your application's rendering function, will build up
    details of the GPU time taken to execute the OpenGL commands between successive
    calls to this function.

    \sa setSampleCount(), isResultAvailable(), waitForSamples(), waitForIntervals()
*/
int QOpenGLTimeMonitor::recordSample()
{
    Q_D(QOpenGLTimeMonitor);
    d->recordSample();
    return d->currentSample;
}

/*!
    Returns \c true if the OpenGL timer query results are available.

    \sa waitForSamples(), waitForIntervals()
*/
bool QOpenGLTimeMonitor::isResultAvailable() const
{
    Q_D(const QOpenGLTimeMonitor);
    return d->isResultAvailable();
}

/*!
    Returns a QVector containing the GPU timestamps taken with recordSample().

    This function will block until OpenGL indicates the results are available. It
    is recommended to check the availability of the result prior to calling this
    function with isResultAvailable().

    \note This function only works on systems that have OpenGL >=3.3 or the
          ARB_timer_query extension. See QOpenGLTimerQuery for more details.

    \sa waitForIntervals(), isResultAvailable()
*/
QVector<GLuint64> QOpenGLTimeMonitor::waitForSamples() const
{
    Q_D(const QOpenGLTimeMonitor);
    return d->samples();
}

/*!
    Returns a QVector containing the time intervals delimited by the calls to
    recordSample(). The resulting vector will contain one fewer element as
    this represents the intervening intervals rather than the actual timestamp
    samples.

    This function will block until OpenGL indicates the results are available. It
    is recommended to check the availability of the result prior to calling this
    function with isResultAvailable().

    \sa waitForSamples(), isResultAvailable()
*/
QVector<GLuint64> QOpenGLTimeMonitor::waitForIntervals() const
{
    Q_D(const QOpenGLTimeMonitor);
    return d->intervals();
}

/*!
    Resets the time monitor ready for use in another frame of rendering. Call
    this once you have obtained the previous results and before calling
    recordSample() for the first time on the next frame.

    \sa recordSample()
*/
void QOpenGLTimeMonitor::reset()
{
    Q_D(QOpenGLTimeMonitor);
    d->reset();
}

QT_END_NAMESPACE