summaryrefslogtreecommitdiffstats
path: root/src/imports/qtcanvas3d/canvasglstatedump.cpp
blob: abcdb379eb163993078fcb40e44cee1ec4559868 (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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCanvas3D 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 "canvasglstatedump_p.h"
#include "enumtostringmap_p.h"
#include "glcommandqueue_p.h"

#include <QtCore/QDebug>
#include <QtGui/QColor>
#include <QtGui/QOpenGLContext>
#include <QtGui/QOpenGLFunctions>

#define BOOL_TO_STR(a) a ? "true" : "false"

QT_BEGIN_NAMESPACE
QT_CANVAS3D_BEGIN_NAMESPACE


/*!
   \qmltype GLStateDumpExt
   \since QtCanvas3D 1.0
   \inqmlmodule QtCanvas3D
   \brief Provides means to print current GL driver state info.

   An uncreatable QML type that provides an extension API that can be used to dump current OpenGL
   driver state as a string that can be then, for example, be printed on the console log.
   You can get it by calling \l{Context3D::getExtension}{Context3D.getExtension} with
   \c{"QTCANVAS3D_gl_state_dump"} as parameter.

   Typical usage could be something like this:
   \code
    // Declare the variable to contain the extension
    var stateDumpExt;
    .
    .
    // After the context has been created from Canvas3D get the extension
    stateDumpExt = gl.getExtension("QTCANVAS3D_gl_state_dump");
    .
    .
    // When you want to print the current GL state with everything enabled
    // Check that you indeed have a valid extension (for portability) then use it
    if (stateDumpExt)
        log("GL STATE DUMP:\n"+stateDumpExt.getGLStateDump(stateDumpExt.DUMP_FULL));
    \endcode

   \sa Context3D
 */
CanvasGLStateDump::CanvasGLStateDump(CanvasContext *canvasContext, bool isEs, QObject *parent) :
    QObject(parent),
    m_maxVertexAttribs(0),
    m_map(EnumToStringMap::newInstance()),
    m_isOpenGLES2(isEs),
    m_canvasContext(canvasContext),
    m_options(DUMP_FULL)
{
}

CanvasGLStateDump::~CanvasGLStateDump()
{
    EnumToStringMap::deleteInstance();
    m_map = 0;
}

void CanvasGLStateDump::getGLArrayObjectDump(int target, int arrayObject, int type)
{
    if (!arrayObject)
        m_stateDumpStr.append("no buffer bound");

    QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions();

    funcs->glBindBuffer(target, arrayObject);

    GLint size;
    funcs->glGetBufferParameteriv(target, GL_BUFFER_SIZE, &size);

    if (type == GL_FLOAT) {
        m_stateDumpStr.append("ARRAY_BUFFER_TYPE......................FLOAT\n");

        m_stateDumpStr.append("ARRAY_BUFFER_SIZE......................");
        m_stateDumpStr.append(QString::number(size));
        m_stateDumpStr.append("\n");

    } else if (type == GL_UNSIGNED_SHORT) {
        m_stateDumpStr.append("ARRAY_BUFFER_TYPE......................UNSIGNED_SHORT\n");

        m_stateDumpStr.append("ARRAY_BUFFER_SIZE......................");
        m_stateDumpStr.append(QString::number(size));
        m_stateDumpStr.append("\n");
    }
}

/*!
 * This function does the actual state dump. It must be called from renderer thread and
 * inside a valid context.
 */
void CanvasGLStateDump::doGLStateDump()
{
#if !defined(QT_OPENGL_ES_2)
    GLint drawFramebuffer;
    GLint readFramebuffer;
    GLboolean polygonOffsetLineEnabled;
    GLboolean polygonOffsetPointEnabled;
    GLint boundVertexArray;
#endif

    QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions();

    if (!m_maxVertexAttribs)
        funcs->glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &m_maxVertexAttribs);

    GLint renderbuffer;
    GLfloat clearColor[4];
    GLfloat clearDepth;
    GLboolean isBlendingEnabled = funcs->glIsEnabled(GL_BLEND);
    GLboolean isDepthTestEnabled = funcs->glIsEnabled(GL_DEPTH_TEST);
    GLint depthFunc;
    GLboolean isDepthWriteEnabled;
    GLint currentProgram;
    GLint *vertexAttribArrayEnabledStates = new GLint[m_maxVertexAttribs];
    GLint *vertexAttribArrayBoundBuffers = new GLint[m_maxVertexAttribs];
    GLint *vertexAttribArraySizes = new GLint[m_maxVertexAttribs];
    GLint *vertexAttribArrayTypes = new GLint[m_maxVertexAttribs];
    GLint *vertexAttribArrayNormalized = new GLint[m_maxVertexAttribs];
    GLint *vertexAttribArrayStrides = new GLint[m_maxVertexAttribs];
    GLint activeTexture;
    GLint texBinding2D;
    GLint arrayBufferBinding;
    GLint frontFace;
    GLboolean isCullFaceEnabled = funcs->glIsEnabled(GL_CULL_FACE);
    GLint cullFaceMode;
    GLint blendEquationRGB;
    GLint blendEquationAlpha;

    GLint blendDestAlpha;
    GLint blendDestRGB;
    GLint blendSrcAlpha;
    GLint blendSrcRGB;
    GLint scissorBox[4];
    GLboolean isScissorTestEnabled = funcs->glIsEnabled(GL_SCISSOR_TEST);
    GLint boundElementArrayBuffer;
    GLboolean polygonOffsetFillEnabled;
    GLfloat polygonOffsetFactor;
    GLfloat polygonOffsetUnits;

#if !defined(QT_OPENGL_ES_2)
    if (!m_isOpenGLES2) {
        funcs->glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &drawFramebuffer);
        funcs->glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &readFramebuffer);
        funcs->glGetBooleanv(GL_POLYGON_OFFSET_LINE, &polygonOffsetLineEnabled);
        funcs->glGetBooleanv(GL_POLYGON_OFFSET_POINT, &polygonOffsetPointEnabled);
        funcs->glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &boundVertexArray);
    }
#endif

    funcs->glGetBooleanv(GL_DEPTH_WRITEMASK, &isDepthWriteEnabled);
    funcs->glGetIntegerv(GL_RENDERBUFFER_BINDING, &renderbuffer);
    funcs->glGetFloatv(GL_COLOR_CLEAR_VALUE, clearColor);
    funcs->glGetFloatv(GL_DEPTH_CLEAR_VALUE, &clearDepth);
    funcs->glGetIntegerv(GL_DEPTH_FUNC, &depthFunc);
    funcs->glGetBooleanv(GL_POLYGON_OFFSET_FILL, &polygonOffsetFillEnabled);
    funcs->glGetFloatv(GL_POLYGON_OFFSET_FACTOR, &polygonOffsetFactor);
    funcs->glGetFloatv(GL_POLYGON_OFFSET_UNITS, &polygonOffsetUnits);

    funcs->glGetIntegerv(GL_CURRENT_PROGRAM, &currentProgram);
    funcs->glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTexture);
    funcs->glGetIntegerv(GL_TEXTURE_BINDING_2D, &texBinding2D );
    funcs->glGetIntegerv(GL_FRONT_FACE, &frontFace);
    funcs->glGetIntegerv(GL_CULL_FACE_MODE, &cullFaceMode);
    funcs->glGetIntegerv(GL_BLEND_EQUATION_RGB, &blendEquationRGB);
    funcs->glGetIntegerv(GL_BLEND_EQUATION_ALPHA, &blendEquationAlpha);
    funcs->glGetIntegerv(GL_BLEND_DST_ALPHA, &blendDestAlpha);
    funcs->glGetIntegerv(GL_BLEND_DST_RGB, &blendDestRGB);
    funcs->glGetIntegerv(GL_BLEND_SRC_ALPHA, &blendSrcAlpha);
    funcs->glGetIntegerv(GL_BLEND_SRC_RGB, &blendSrcRGB);
    funcs->glGetIntegerv(GL_SCISSOR_BOX, scissorBox);
    funcs->glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &boundElementArrayBuffer);
    funcs->glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &arrayBufferBinding);


#if !defined(QT_OPENGL_ES_2)
    if (!m_isOpenGLES2) {
        m_stateDumpStr.append("GL_DRAW_FRAMEBUFFER_BINDING.....");
        m_stateDumpStr.append(QString::number(drawFramebuffer));
        m_stateDumpStr.append("\n");

        m_stateDumpStr.append("GL_READ_FRAMEBUFFER_BINDING.....");
        m_stateDumpStr.append(QString::number(readFramebuffer));
        m_stateDumpStr.append("\n");
    }
#endif

    m_stateDumpStr.append("GL_RENDERBUFFER_BINDING.........");
    m_stateDumpStr.append(QString::number(renderbuffer));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_SCISSOR_TEST.................");
    m_stateDumpStr.append(BOOL_TO_STR(isScissorTestEnabled));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_SCISSOR_BOX..................");
    m_stateDumpStr.append(QString::number(scissorBox[0]));
    m_stateDumpStr.append(", ");
    m_stateDumpStr.append(QString::number(scissorBox[1]));
    m_stateDumpStr.append(", ");
    m_stateDumpStr.append(QString::number(scissorBox[2]));
    m_stateDumpStr.append(", ");
    m_stateDumpStr.append(QString::number(scissorBox[3]));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_COLOR_CLEAR_VALUE............");
    m_stateDumpStr.append("r:");
    m_stateDumpStr.append(QString::number(clearColor[0]));
    m_stateDumpStr.append(" g:");
    m_stateDumpStr.append(QString::number(clearColor[1]));
    m_stateDumpStr.append(" b:");
    m_stateDumpStr.append(QString::number(clearColor[2]));
    m_stateDumpStr.append(" a:");
    m_stateDumpStr.append(QString::number(clearColor[3]));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_DEPTH_CLEAR_VALUE............");
    m_stateDumpStr.append(QString::number(clearDepth));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_BLEND........................");
    m_stateDumpStr.append(BOOL_TO_STR(isBlendingEnabled));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_BLEND_EQUATION_RGB...........");
    m_stateDumpStr.append(m_map->lookUp(blendEquationRGB));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_BLEND_EQUATION_ALPHA.........");
    m_stateDumpStr.append(m_map->lookUp(blendEquationAlpha));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_DEPTH_TEST...................");
    m_stateDumpStr.append(BOOL_TO_STR(isDepthTestEnabled));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_DEPTH_FUNC...................");
    m_stateDumpStr.append(m_map->lookUp(depthFunc));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_DEPTH_WRITEMASK..............");
    m_stateDumpStr.append(BOOL_TO_STR(isDepthWriteEnabled));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_POLYGON_OFFSET_FILL..........");
    m_stateDumpStr.append(BOOL_TO_STR(polygonOffsetFillEnabled));
    m_stateDumpStr.append("\n");

#if !defined(QT_OPENGL_ES_2)
    if (!m_isOpenGLES2) {
        m_stateDumpStr.append("GL_POLYGON_OFFSET_LINE..........");
        m_stateDumpStr.append(BOOL_TO_STR(polygonOffsetLineEnabled));
        m_stateDumpStr.append("\n");

        m_stateDumpStr.append("GL_POLYGON_OFFSET_POINT.........");
        m_stateDumpStr.append(BOOL_TO_STR(polygonOffsetPointEnabled));
        m_stateDumpStr.append("\n");
    }
#endif

    m_stateDumpStr.append("GL_POLYGON_OFFSET_FACTOR........");
    m_stateDumpStr.append(QString::number(polygonOffsetFactor));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_POLYGON_OFFSET_UNITS.........");
    m_stateDumpStr.append(QString::number(polygonOffsetUnits));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_CULL_FACE....................");
    m_stateDumpStr.append(BOOL_TO_STR(isCullFaceEnabled));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_CULL_FACE_MODE...............");
    m_stateDumpStr.append(m_map->lookUp(cullFaceMode));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_FRONT_FACE...................");
    m_stateDumpStr.append(m_map->lookUp(frontFace));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_CURRENT_PROGRAM..............");
    m_stateDumpStr.append(QString::number(currentProgram));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_ACTIVE_TEXTURE...............");
    m_stateDumpStr.append(m_map->lookUp(activeTexture));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_TEXTURE_BINDING_2D...........");
    m_stateDumpStr.append(QString::number(texBinding2D));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_ELEMENT_ARRAY_BUFFER_BINDING.");
    m_stateDumpStr.append(QString::number(boundElementArrayBuffer));
    m_stateDumpStr.append("\n");

    m_stateDumpStr.append("GL_ARRAY_BUFFER_BINDING.........");
    m_stateDumpStr.append(QString::number(arrayBufferBinding));
    m_stateDumpStr.append("\n");

#if !defined(QT_OPENGL_ES_2)
    if (!m_isOpenGLES2) {
        m_stateDumpStr.append("GL_VERTEX_ARRAY_BINDING.........");
        m_stateDumpStr.append(QString::number(boundVertexArray));
        m_stateDumpStr.append("\n");
    }
#endif

    if (m_options & DUMP_VERTEX_ATTRIB_ARRAYS_BIT) {
        for (int i = 0; i < m_maxVertexAttribs;i++) {
            funcs->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &vertexAttribArrayEnabledStates[i]);
            funcs->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &vertexAttribArrayBoundBuffers[i]);
            funcs->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_SIZE, &vertexAttribArraySizes[i]);
            funcs->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_TYPE, &vertexAttribArrayTypes[i]);
            funcs->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &vertexAttribArrayNormalized[i]);
            funcs->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &vertexAttribArrayStrides[i]);
        }


        for (int i = 0; i < m_maxVertexAttribs;i++) {
            m_stateDumpStr.append("GL_VERTEX_ATTRIB_ARRAY_");
            m_stateDumpStr.append(QString::number(i));
            m_stateDumpStr.append("\n");

            m_stateDumpStr.append("GL_VERTEX_ATTRIB_ARRAY_ENABLED.........");
            m_stateDumpStr.append(BOOL_TO_STR(vertexAttribArrayEnabledStates[i]));
            m_stateDumpStr.append("\n");

            m_stateDumpStr.append("GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING..");
            m_stateDumpStr.append(QString::number(vertexAttribArrayBoundBuffers[i]));
            m_stateDumpStr.append("\n");

            m_stateDumpStr.append("GL_VERTEX_ATTRIB_ARRAY_SIZE............");
            m_stateDumpStr.append(QString::number(vertexAttribArraySizes[i]));
            m_stateDumpStr.append("\n");

            m_stateDumpStr.append("GL_VERTEX_ATTRIB_ARRAY_TYPE............");
            m_stateDumpStr.append(m_map->lookUp(vertexAttribArrayTypes[i]));
            m_stateDumpStr.append("\n");

            m_stateDumpStr.append("GL_VERTEX_ATTRIB_ARRAY_NORMALIZED......");
            m_stateDumpStr.append(QString::number(vertexAttribArrayNormalized[i]));
            m_stateDumpStr.append("\n");

            m_stateDumpStr.append("GL_VERTEX_ATTRIB_ARRAY_STRIDE..........");
            m_stateDumpStr.append(QString::number(vertexAttribArrayStrides[i]));
            m_stateDumpStr.append("\n");
        }
    }

    if (m_options & DUMP_VERTEX_ATTRIB_ARRAYS_BUFFERS_BIT) {
        if (boundElementArrayBuffer != 0) {
            m_stateDumpStr.append("GL_ELEMENT_ARRAY_BUFFER................");
            m_stateDumpStr.append(QString::number(boundElementArrayBuffer));
            m_stateDumpStr.append("\n");

            getGLArrayObjectDump(GL_ELEMENT_ARRAY_BUFFER, boundElementArrayBuffer,
                                 GL_UNSIGNED_SHORT);
        }

        for (int i = 0; i < m_maxVertexAttribs;i++) {
            if (vertexAttribArrayEnabledStates[i]) {
                m_stateDumpStr.append("GL_ARRAY_BUFFER........................");
                m_stateDumpStr.append(QString::number(vertexAttribArrayBoundBuffers[i]));
                m_stateDumpStr.append("\n");

                getGLArrayObjectDump(GL_ARRAY_BUFFER, vertexAttribArrayBoundBuffers[i],
                                     vertexAttribArrayTypes[i]);
            }
        }
    }


    delete[] vertexAttribArrayEnabledStates;
    delete[] vertexAttribArrayBoundBuffers;
    delete[] vertexAttribArraySizes;
    delete[] vertexAttribArrayTypes;
    delete[] vertexAttribArrayNormalized;
    delete[] vertexAttribArrayStrides;
}

/*!
 * \qmlmethod string GLStateDumpExt::getGLStateDump(stateDumpEnums options)
 * \return OpenGL driver state with given options as a human readable string that can be printed.
 * Optional paremeter \a options may contain bitfields masked together from following options:
 * \list
 * \li \c{GLStateDumpExt.DUMP_BASIC_ONLY} Includes only very basic OpenGL state information.
 * \li \c{GLStateDumpExt.DUMP_VERTEX_ATTRIB_ARRAYS_BIT} Includes all vertex attribute array
 * information.
 * \li \c{GLStateDumpExt.DUMP_VERTEX_ATTRIB_ARRAYS_BUFFERS_BIT} Includes size and type
 * from all currently active vertex attribute arrays (including the currently bound element array)
 * to verify that there are actual values in the array.
 * \li \c{GLStateDumpExt.DUMP_FULL} Includes everything.
 * \endlist
 * This command is handled synchronously.
 */
QString CanvasGLStateDump::getGLStateDump(CanvasGLStateDump::stateDumpEnums options)
{
    if (m_canvasContext->isContextLost())
        return QString();

    m_options = options;
    m_stateDumpStr.clear();

    // Schedule a synchronous dump job
    GlSyncCommand syncCommand(CanvasGlCommandQueue::extStateDump);
    syncCommand.returnValue = static_cast<void *>(this);
    m_canvasContext->scheduleSyncCommand(&syncCommand);

    return m_stateDumpStr;
}

QT_CANVAS3D_END_NAMESPACE
QT_END_NAMESPACE