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

#include "qquicknvprfunctions_p.h"

#if QT_CONFIG(opengl)

#include <QOpenGLContext>
#include <QOffscreenSurface>
#include <QOpenGLExtraFunctions>
#include "qquicknvprfunctions_p_p.h"

QT_BEGIN_NAMESPACE

/*!
    \class QQuickNvprFunctions

    \brief Function resolvers and other helpers for GL_NV_path_rendering
    for both desktop (GL 4.3+) and mobile/embedded (GLES 3.1+) in a manner
    that does not distract builds that do not have NVPR support either at
    compile or run time.

    \internal
 */

QQuickNvprFunctions::QQuickNvprFunctions()
    : d(new QQuickNvprFunctionsPrivate(this))
{
}

QQuickNvprFunctions::~QQuickNvprFunctions()
{
    delete d;
}

/*!
   \return a recommended QSurfaceFormat suitable for GL_NV_path_rendering on top
   of OpenGL 4.3 or OpenGL ES 3.1.
 */
QSurfaceFormat QQuickNvprFunctions::format()
{
    QSurfaceFormat fmt;
    fmt.setDepthBufferSize(24);
    fmt.setStencilBufferSize(8);
    if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) {
        fmt.setVersion(4, 3);
        fmt.setProfile(QSurfaceFormat::CompatibilityProfile);
    } else if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGLES) {
        fmt.setVersion(3, 1);
    }
    return fmt;
}

#define PROC(type, name) reinterpret_cast<type>(ctx->getProcAddress(#name))

/*!
  \return true if GL_NV_path_rendering is supported with the current OpenGL
  context.

  When there is no current context, a temporary dummy one will be created and
  made current.
 */
bool QQuickNvprFunctions::isSupported()
{
    QOpenGLContext *ctx = QOpenGLContext::currentContext();
    QScopedPointer<QOpenGLContext> tempContext;
    QScopedPointer<QOffscreenSurface> tempSurface;
    if (!ctx) {
        tempContext.reset(new QOpenGLContext);
        if (!tempContext->create())
            return false;
        ctx = tempContext.data();
        tempSurface.reset(new QOffscreenSurface);
        tempSurface->setFormat(ctx->format());
        tempSurface->create();
        if (!ctx->makeCurrent(tempSurface.data()))
            return false;
    }

    if (!ctx->hasExtension(QByteArrayLiteral("GL_NV_path_rendering")))
        return false;

    // Check that GL_NV_Path_rendering extension is at least API revision 1.3
    if (!PROC(PFNGLPROGRAMPATHFRAGMENTINPUTGENNVPROC, glProgramPathFragmentInputGenNV))
        return false;

    // Do not check for DSA as the string may not be exposed on ES
    // drivers, yet the functions we need are resolvable.
#if 0
    if (!ctx->hasExtension(QByteArrayLiteral("GL_EXT_direct_state_access"))) {
        qWarning("QtQuickPath/NVPR: GL_EXT_direct_state_access not supported");
        return false;
    }
#endif

    return true;
}

/*!
    Initializes using the current OpenGL context.

    \return true when GL_NV_path_rendering is supported and initialization was
    successful.
 */
bool QQuickNvprFunctions::create()
{
    return isSupported() && d->resolve();
}

/*!
    Creates a program pipeline consisting of a separable fragment shader program.

    This is essential for using NVPR with OpenGL ES 3.1+ since normal,
    GLES2-style programs would not work without a vertex shader.

    \note \a fragmentShaderSource should be a \c{version 310 es} shader since
    this works both on desktop and embedded NVIDIA drivers, thus avoiding the
    need to fight GLSL and GLSL ES differences.

    The pipeline object is stored into \a pipeline, the fragment shader program
    into \a program.

    Use QOpenGLExtraFunctions to set uniforms, bind the pipeline, etc.

    \return \c false on failure in which case the error log is printed on the
    debug output. \c true on success.
 */
bool QQuickNvprFunctions::createFragmentOnlyPipeline(const char *fragmentShaderSource, GLuint *pipeline, GLuint *program)
{
    QOpenGLContext *ctx = QOpenGLContext::currentContext();
    if (!ctx)
        return false;

    QOpenGLExtraFunctions *f = ctx->extraFunctions();
    *program = f->glCreateShaderProgramv(GL_FRAGMENT_SHADER, 1, &fragmentShaderSource);
    GLint status = 0;
    f->glGetProgramiv(*program, GL_LINK_STATUS, &status);
    if (!status) {
        GLint len = 0;
        f->glGetProgramiv(*program, GL_INFO_LOG_LENGTH, &len);
        if (len) {
            QByteArray s;
            s.resize(len);
            f->glGetProgramInfoLog(*program, s.count(), nullptr, s.data());
            qWarning("Failed to create separable shader program:\n%s", s.constData());
        }
        return false;
    }

    f->glGenProgramPipelines(1, pipeline);
    f->glUseProgramStages(*pipeline, GL_FRAGMENT_SHADER_BIT, *program);
    f->glActiveShaderProgram(*pipeline, *program);

    f->glValidateProgramPipeline(*pipeline);
    status = 0;
    f->glGetProgramPipelineiv(*pipeline, GL_VALIDATE_STATUS, &status);
    if (!status) {
        GLint len = 0;
        f->glGetProgramPipelineiv(*pipeline, GL_INFO_LOG_LENGTH, &len);
        if (len) {
            QByteArray s;
            s.resize(len);
            f->glGetProgramPipelineInfoLog(*pipeline, s.count(), nullptr, s.data());
            qWarning("Program pipeline validation failed:\n%s", s.constData());
        }
        return false;
    }

    return true;
}

bool QQuickNvprFunctionsPrivate::resolve()
{
    QOpenGLContext *ctx = QOpenGLContext::currentContext();

    q->genPaths = PROC(PFNGLGENPATHSNVPROC, glGenPathsNV);
    q->deletePaths = PROC(PFNGLDELETEPATHSNVPROC, glDeletePathsNV);
    q->isPath = PROC(PFNGLISPATHNVPROC, glIsPathNV);
    q->pathCommands = PROC(PFNGLPATHCOMMANDSNVPROC, glPathCommandsNV);
    q->pathCoords = PROC(PFNGLPATHCOORDSNVPROC, glPathCoordsNV);
    q->pathSubCommands = PROC(PFNGLPATHSUBCOMMANDSNVPROC, glPathSubCommandsNV);
    q->pathSubCoords = PROC(PFNGLPATHSUBCOORDSNVPROC, glPathSubCoordsNV);
    q->pathString = PROC(PFNGLPATHSTRINGNVPROC, glPathStringNV);
    q->pathGlyphs = PROC(PFNGLPATHGLYPHSNVPROC, glPathGlyphsNV);
    q->pathGlyphRange = PROC(PFNGLPATHGLYPHRANGENVPROC, glPathGlyphRangeNV);
    q->weightPaths = PROC(PFNGLWEIGHTPATHSNVPROC, glWeightPathsNV);
    q->copyPath = PROC(PFNGLCOPYPATHNVPROC, glCopyPathNV);
    q->interpolatePaths = PROC(PFNGLINTERPOLATEPATHSNVPROC, glInterpolatePathsNV);
    q->transformPath = PROC(PFNGLTRANSFORMPATHNVPROC, glTransformPathNV);
    q->pathParameteriv = PROC(PFNGLPATHPARAMETERIVNVPROC, glPathParameterivNV);
    q->pathParameteri = PROC(PFNGLPATHPARAMETERINVPROC, glPathParameteriNV);
    q->pathParameterfv = PROC(PFNGLPATHPARAMETERFVNVPROC, glPathParameterfvNV);
    q->pathParameterf = PROC(PFNGLPATHPARAMETERFNVPROC, glPathParameterfNV);
    q->pathDashArray = PROC(PFNGLPATHDASHARRAYNVPROC, glPathDashArrayNV);
    q->pathStencilFunc = PROC(PFNGLPATHSTENCILFUNCNVPROC, glPathStencilFuncNV);
    q->pathStencilDepthOffset = PROC(PFNGLPATHSTENCILDEPTHOFFSETNVPROC, glPathStencilDepthOffsetNV);
    q->stencilFillPath = PROC(PFNGLSTENCILFILLPATHNVPROC, glStencilFillPathNV);
    q->stencilStrokePath = PROC(PFNGLSTENCILSTROKEPATHNVPROC, glStencilStrokePathNV);
    q->stencilFillPathInstanced = PROC(PFNGLSTENCILFILLPATHINSTANCEDNVPROC, glStencilFillPathInstancedNV);
    q->stencilStrokePathInstanced = PROC(PFNGLSTENCILSTROKEPATHINSTANCEDNVPROC, glStencilStrokePathInstancedNV);
    q->pathCoverDepthFunc = PROC(PFNGLPATHCOVERDEPTHFUNCNVPROC,  glPathCoverDepthFuncNV);
    q->coverFillPath = PROC(PFNGLCOVERFILLPATHNVPROC, glCoverFillPathNV);
    q->coverStrokePath = PROC(PFNGLCOVERSTROKEPATHNVPROC, glCoverStrokePathNV);
    q->coverFillPathInstanced = PROC(PFNGLCOVERFILLPATHINSTANCEDNVPROC, glCoverFillPathInstancedNV);
    q->coverStrokePathInstanced = PROC(PFNGLCOVERSTROKEPATHINSTANCEDNVPROC, glCoverStrokePathInstancedNV);
    q->getPathParameteriv = PROC(PFNGLGETPATHPARAMETERIVNVPROC, glGetPathParameterivNV);
    q->getPathParameterfv = PROC(PFNGLGETPATHPARAMETERFVNVPROC, glGetPathParameterfvNV);
    q->getPathCommands = PROC(PFNGLGETPATHCOMMANDSNVPROC, glGetPathCommandsNV);
    q->getPathCoords = PROC(PFNGLGETPATHCOORDSNVPROC, glGetPathCoordsNV);
    q->getPathDashArray = PROC(PFNGLGETPATHDASHARRAYNVPROC, glGetPathDashArrayNV);
    q->getPathMetrics = PROC(PFNGLGETPATHMETRICSNVPROC, glGetPathMetricsNV);
    q->getPathMetricRange = PROC(PFNGLGETPATHMETRICRANGENVPROC, glGetPathMetricRangeNV);
    q->getPathSpacing = PROC(PFNGLGETPATHSPACINGNVPROC, glGetPathSpacingNV);
    q->isPointInFillPath = PROC(PFNGLISPOINTINFILLPATHNVPROC, glIsPointInFillPathNV);
    q->isPointInStrokePath = PROC(PFNGLISPOINTINSTROKEPATHNVPROC, glIsPointInStrokePathNV);
    q->getPathLength = PROC(PFNGLGETPATHLENGTHNVPROC, glGetPathLengthNV);
    q->getPointAlongPath = PROC(PFNGLPOINTALONGPATHNVPROC, glPointAlongPathNV);
    q->matrixLoad3x2f = PROC(PFNGLMATRIXLOAD3X2FNVPROC, glMatrixLoad3x2fNV);
    q->matrixLoad3x3f = PROC(PFNGLMATRIXLOAD3X3FNVPROC, glMatrixLoad3x3fNV);
    q->matrixLoadTranspose3x3f = PROC(PFNGLMATRIXLOADTRANSPOSE3X3FNVPROC, glMatrixLoadTranspose3x3fNV);
    q->matrixMult3x2f = PROC(PFNGLMATRIXMULT3X2FNVPROC, glMatrixMult3x2fNV);
    q->matrixMult3x3f = PROC(PFNGLMATRIXMULT3X3FNVPROC, glMatrixMult3x3fNV);
    q->matrixMultTranspose3x3f = PROC(PFNGLMATRIXMULTTRANSPOSE3X3FNVPROC, glMatrixMultTranspose3x3fNV);
    q->stencilThenCoverFillPath = PROC(PFNGLSTENCILTHENCOVERFILLPATHNVPROC, glStencilThenCoverFillPathNV);
    q->stencilThenCoverStrokePath = PROC(PFNGLSTENCILTHENCOVERSTROKEPATHNVPROC, glStencilThenCoverStrokePathNV);
    q->stencilThenCoverFillPathInstanced = PROC(PFNGLSTENCILTHENCOVERFILLPATHINSTANCEDNVPROC, glStencilThenCoverFillPathInstancedNV);
    q->stencilThenCoverStrokePathInstanced = PROC(PFNGLSTENCILTHENCOVERSTROKEPATHINSTANCEDNVPROC, glStencilThenCoverStrokePathInstancedNV);
    q->pathGlyphIndexRange = PROC(PFNGLPATHGLYPHINDEXRANGENVPROC, glPathGlyphIndexRangeNV);
    q->pathGlyphIndexArray = PROC(PFNGLPATHGLYPHINDEXARRAYNVPROC, glPathGlyphIndexArrayNV);
    q->pathMemoryGlyphIndexArray = PROC(PFNGLPATHMEMORYGLYPHINDEXARRAYNVPROC, glPathMemoryGlyphIndexArrayNV);
    q->programPathFragmentInputGen = PROC(PFNGLPROGRAMPATHFRAGMENTINPUTGENNVPROC, glProgramPathFragmentInputGenNV);
    q->getProgramResourcefv = PROC(PFNGLGETPROGRAMRESOURCEFVNVPROC, glGetProgramResourcefvNV);

    q->matrixLoadf = PROC(PFNGLMATRIXLOADFEXTPROC, glMatrixLoadfEXT);
    q->matrixLoadIdentity = PROC(PFNGLMATRIXLOADIDENTITYEXTPROC, glMatrixLoadIdentityEXT);

    return q->genPaths != nullptr // base path rendering ext
        && q->programPathFragmentInputGen != nullptr // updated path rendering ext
        && q->matrixLoadf != nullptr // direct state access ext
        && q->matrixLoadIdentity != nullptr;
}

QT_END_NAMESPACE

#endif // QT_CONFIG(opengl)