Ignore:
Timestamp:
May 5, 2011, 5:36:53 AM (14 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/opengl/qglshaderprogram.cpp

    r769 r846  
    11/****************************************************************************
    22**
    3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
    44** All rights reserved.
    55** Contact: Nokia Corporation (qt-info@nokia.com)
     
    5151QT_BEGIN_NAMESPACE
    5252
    53 #if !defined(QT_OPENGL_ES_1_CL) && !defined(QT_OPENGL_ES_1)
     53#if !defined(QT_OPENGL_ES_1)
    5454
    5555/*!
     
    144144    \value Vertex Vertex shader written in the OpenGL Shading Language (GLSL).
    145145    \value Fragment Fragment shader written in the OpenGL Shading Language (GLSL).
     146    \value Geometry Geometry shaders written in the OpenGL Shading
     147           Language (GLSL), based on the GL_EXT_geometry_shader4 extension.
    146148*/
    147149
     
    227229        if (shaderType == QGLShader::Vertex)
    228230            shader = glCreateShader(GL_VERTEX_SHADER);
     231        else if (shaderType == QGLShader::Geometry)
     232            shader = glCreateShader(GL_GEOMETRY_SHADER_EXT);
    229233        else
    230234            shader = glCreateShader(GL_FRAGMENT_SHADER);
     
    510514}
    511515
     516
     517
     518
     519
    512520#undef ctx
    513521#define ctx programGuard.context()
     
    522530        , inited(false)
    523531        , removingShaders(false)
    524         , vertexShader(0)
    525         , fragmentShader(0)
     532        , geometryVertexCount(64)
     533        , geometryInputType(0)
     534        , geometryOutputType(0)
    526535    {
    527536    }
     
    532541    bool inited;
    533542    bool removingShaders;
     543
     544    int geometryVertexCount;
     545    GLenum geometryInputType;
     546    GLenum geometryOutputType;
     547
    534548    QString log;
    535549    QList<QGLShader *> shaders;
    536550    QList<QGLShader *> anonShaders;
    537     QGLShader *vertexShader;
    538     QGLShader *fragmentShader;
    539551
    540552    bool hasShader(QGLShader::ShaderType type) const;
     
    605617        d->programGuard.setContext(context);
    606618    }
     619
    607620    if (!context)
    608621        return false;
     
    832845    if (!program)
    833846        return false;
     847
    834848    GLint value;
    835849    if (d->shaders.isEmpty()) {
     
    844858            return true;
    845859    }
     860
     861    // Set up the geometry shader parameters
     862    if (glProgramParameteriEXT) {
     863        foreach (QGLShader *shader, d->shaders) {
     864            if (shader->shaderType() & QGLShader::Geometry) {
     865                glProgramParameteriEXT(program, GL_GEOMETRY_INPUT_TYPE_EXT,
     866                                       d->geometryInputType);
     867                glProgramParameteriEXT(program, GL_GEOMETRY_OUTPUT_TYPE_EXT,
     868                                       d->geometryOutputType);
     869                glProgramParameteriEXT(program, GL_GEOMETRY_VERTICES_OUT_EXT,
     870                                       d->geometryVertexCount);
     871                break;
     872            }
     873        }
     874    }
     875
    846876    glLinkProgram(program);
    847877    value = 0;
     
    12681298    Q_UNUSED(d);
    12691299    if (location != -1) {
    1270         GLfloat values[4] = {value.redF(), value.greenF(), value.blueF(), value.alphaF()};
     1300        GLfloat values[4] = {GLfloat(value.redF()), GLfloat(value.greenF()),
     1301                             GLfloat(value.blueF()), GLfloat(value.alphaF())};
    12711302        glVertexAttrib4fv(location, values);
    12721303    }
     
    14341465
    14351466/*!
     1467    Sets an array of vertex \a values on the attribute at \a location
     1468    in this shader program.  The \a stride indicates the number of bytes
     1469    between vertices.  A default \a stride value of zero indicates that
     1470    the vertices are densely packed in \a values.
     1471
     1472    The \a type indicates the type of elements in the \a values array,
     1473    usually \c{GL_FLOAT}, \c{GL_UNSIGNED_BYTE}, etc.  The \a tupleSize
     1474    indicates the number of components per vertex: 1, 2, 3, or 4.
     1475
     1476    The array will become active when enableAttributeArray() is called
     1477    on the \a location.  Otherwise the value specified with
     1478    setAttributeValue() for \a location will be used.
     1479
     1480    The setAttributeBuffer() function can be used to set the attribute
     1481    array to an offset within a vertex buffer.
     1482
     1483    \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
     1484    \sa disableAttributeArray(), setAttributeBuffer()
     1485    \since 4.7
     1486*/
     1487void QGLShaderProgram::setAttributeArray
     1488    (int location, GLenum type, const void *values, int tupleSize, int stride)
     1489{
     1490    Q_D(QGLShaderProgram);
     1491    Q_UNUSED(d);
     1492    if (location != -1) {
     1493        glVertexAttribPointer(location, tupleSize, type, GL_TRUE,
     1494                              stride, values);
     1495    }
     1496}
     1497
     1498/*!
    14361499    \overload
    14371500
     
    15161579{
    15171580    setAttributeArray(attributeLocation(name), values, stride);
     1581}
     1582
     1583/*!
     1584    \overload
     1585
     1586    Sets an array of vertex \a values on the attribute called \a name
     1587    in this shader program.  The \a stride indicates the number of bytes
     1588    between vertices.  A default \a stride value of zero indicates that
     1589    the vertices are densely packed in \a values.
     1590
     1591    The \a type indicates the type of elements in the \a values array,
     1592    usually \c{GL_FLOAT}, \c{GL_UNSIGNED_BYTE}, etc.  The \a tupleSize
     1593    indicates the number of components per vertex: 1, 2, 3, or 4.
     1594
     1595    The array will become active when enableAttributeArray() is called
     1596    on the \a name.  Otherwise the value specified with
     1597    setAttributeValue() for \a name will be used.
     1598
     1599    The setAttributeBuffer() function can be used to set the attribute
     1600    array to an offset within a vertex buffer.
     1601
     1602    \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
     1603    \sa disableAttributeArray(), setAttributeBuffer()
     1604    \since 4.7
     1605*/
     1606void QGLShaderProgram::setAttributeArray
     1607    (const char *name, GLenum type, const void *values, int tupleSize, int stride)
     1608{
     1609    setAttributeArray(attributeLocation(name), type, values, tupleSize, stride);
     1610}
     1611
     1612/*!
     1613    Sets an array of vertex values on the attribute at \a location in
     1614    this shader program, starting at a specific \a offset in the
     1615    currently bound vertex buffer.  The \a stride indicates the number
     1616    of bytes between vertices.  A default \a stride value of zero
     1617    indicates that the vertices are densely packed in the value array.
     1618
     1619    The \a type indicates the type of elements in the vertex value
     1620    array, usually \c{GL_FLOAT}, \c{GL_UNSIGNED_BYTE}, etc.  The \a
     1621    tupleSize indicates the number of components per vertex: 1, 2, 3,
     1622    or 4.
     1623
     1624    The array will become active when enableAttributeArray() is called
     1625    on the \a location.  Otherwise the value specified with
     1626    setAttributeValue() for \a location will be used.
     1627
     1628    \sa setAttributeArray()
     1629    \since 4.7
     1630*/
     1631void QGLShaderProgram::setAttributeBuffer
     1632    (int location, GLenum type, int offset, int tupleSize, int stride)
     1633{
     1634    Q_D(QGLShaderProgram);
     1635    Q_UNUSED(d);
     1636    if (location != -1) {
     1637        glVertexAttribPointer(location, tupleSize, type, GL_TRUE, stride,
     1638                              reinterpret_cast<const void *>(offset));
     1639    }
     1640}
     1641
     1642/*!
     1643    \overload
     1644
     1645    Sets an array of vertex values on the attribute called \a name
     1646    in this shader program, starting at a specific \a offset in the
     1647    currently bound vertex buffer.  The \a stride indicates the number
     1648    of bytes between vertices.  A default \a stride value of zero
     1649    indicates that the vertices are densely packed in the value array.
     1650
     1651    The \a type indicates the type of elements in the vertex value
     1652    array, usually \c{GL_FLOAT}, \c{GL_UNSIGNED_BYTE}, etc.  The \a
     1653    tupleSize indicates the number of components per vertex: 1, 2, 3,
     1654    or 4.
     1655
     1656    The array will become active when enableAttributeArray() is called
     1657    on the \a name.  Otherwise the value specified with
     1658    setAttributeValue() for \a name will be used.
     1659
     1660    \sa setAttributeArray()
     1661    \since 4.7
     1662*/
     1663void QGLShaderProgram::setAttributeBuffer
     1664    (const char *name, GLenum type, int offset, int tupleSize, int stride)
     1665{
     1666    setAttributeBuffer(attributeLocation(name), type, offset, tupleSize, stride);
    15181667}
    15191668
     
    18852034    Q_UNUSED(d);
    18862035    if (location != -1) {
    1887         GLfloat values[4] = {color.redF(), color.greenF(), color.blueF(), color.alphaF()};
     2036        GLfloat values[4] = {GLfloat(color.redF()), GLfloat(color.greenF()),
     2037                             GLfloat(color.blueF()), GLfloat(color.alphaF())};
    18882038        glUniform4fv(location, 1, values);
    18892039    }
     
    19142064    Q_UNUSED(d);
    19152065    if (location != -1) {
    1916         GLfloat values[4] = {point.x(), point.y()};
     2066        GLfloat values[4] = {GLfloat(point.x()), GLfloat(point.y())};
    19172067        glUniform2fv(location, 1, values);
    19182068    }
     
    19432093    Q_UNUSED(d);
    19442094    if (location != -1) {
    1945         GLfloat values[4] = {point.x(), point.y()};
     2095        GLfloat values[4] = {GLfloat(point.x()), GLfloat(point.y())};
    19462096        glUniform2fv(location, 1, values);
    19472097    }
     
    19722122    Q_UNUSED(d);
    19732123    if (location != -1) {
    1974         GLfloat values[4] = {size.width(), size.width()};
     2124        GLfloat values[4] = {GLfloat(size.width()), GLfloat(size.height())};
    19752125        glUniform2fv(location, 1, values);
    19762126    }
     
    20012151    Q_UNUSED(d);
    20022152    if (location != -1) {
    2003         GLfloat values[4] = {size.width(), size.height()};
     2153        GLfloat values[4] = {GLfloat(size.width()), GLfloat(size.height())};
    20042154        glUniform2fv(location, 1, values);
    20052155    }
     
    23152465
    23162466    Sets the uniform variable at \a location in the current context
     2467    to a 2x2 matrix \a value.  The matrix elements must be specified
     2468    in column-major order.
     2469
     2470    \sa setAttributeValue()
     2471    \since 4.7
     2472*/
     2473void QGLShaderProgram::setUniformValue(int location, const GLfloat value[2][2])
     2474{
     2475    Q_D(QGLShaderProgram);
     2476    Q_UNUSED(d);
     2477    if (location != -1)
     2478        glUniformMatrix2fv(location, 1, GL_FALSE, value[0]);
     2479}
     2480
     2481/*!
     2482    \overload
     2483
     2484    Sets the uniform variable at \a location in the current context
     2485    to a 3x3 matrix \a value.  The matrix elements must be specified
     2486    in column-major order.
     2487
     2488    \sa setAttributeValue()
     2489    \since 4.7
     2490*/
     2491void QGLShaderProgram::setUniformValue(int location, const GLfloat value[3][3])
     2492{
     2493    Q_D(QGLShaderProgram);
     2494    Q_UNUSED(d);
     2495    if (location != -1)
     2496        glUniformMatrix3fv(location, 1, GL_FALSE, value[0]);
     2497}
     2498
     2499/*!
     2500    \overload
     2501
     2502    Sets the uniform variable at \a location in the current context
    23172503    to a 4x4 matrix \a value.  The matrix elements must be specified
    23182504    in column-major order.
     
    23262512    if (location != -1)
    23272513        glUniformMatrix4fv(location, 1, GL_FALSE, value[0]);
     2514}
     2515
     2516
     2517/*!
     2518    \overload
     2519
     2520    Sets the uniform variable called \a name in the current context
     2521    to a 2x2 matrix \a value.  The matrix elements must be specified
     2522    in column-major order.
     2523
     2524    \sa setAttributeValue()
     2525    \since 4.7
     2526*/
     2527void QGLShaderProgram::setUniformValue(const char *name, const GLfloat value[2][2])
     2528{
     2529    setUniformValue(uniformLocation(name), value);
     2530}
     2531
     2532/*!
     2533    \overload
     2534
     2535    Sets the uniform variable called \a name in the current context
     2536    to a 3x3 matrix \a value.  The matrix elements must be specified
     2537    in column-major order.
     2538
     2539    \sa setAttributeValue()
     2540    \since 4.7
     2541*/
     2542void QGLShaderProgram::setUniformValue(const char *name, const GLfloat value[3][3])
     2543{
     2544    setUniformValue(uniformLocation(name), value);
    23282545}
    23292546
     
    23552572    if (location != -1) {
    23562573        GLfloat mat[3][3] = {
    2357             {value.m11(), value.m12(), value.m13()},
    2358             {value.m21(), value.m22(), value.m23()},
    2359             {value.m31(), value.m32(), value.m33()}
     2574            {GLfloat(value.m11()), GLfloat(value.m12()), GLfloat(value.m13())},
     2575            {GLfloat(value.m21()), GLfloat(value.m22()), GLfloat(value.m23())},
     2576            {GLfloat(value.m31()), GLfloat(value.m32()), GLfloat(value.m33())}
    23602577        };
    23612578        glUniformMatrix3fv(location, 1, GL_FALSE, mat[0]);
     
    28703087
    28713088/*!
     3089    Returns the hardware limit for how many vertices a geometry shader
     3090    can output.
     3091
     3092    \since 4.7
     3093
     3094    \sa setGeometryOutputVertexCount()
     3095*/
     3096int QGLShaderProgram::maxGeometryOutputVertices() const
     3097{
     3098    GLint n;
     3099    glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT, &n);
     3100    return n;
     3101}
     3102
     3103/*!
     3104    Sets the maximum number of vertices the current geometry shader
     3105    program will produce, if active, to \a count.
     3106
     3107    \since 4.7
     3108
     3109    This parameter takes effect the next time the program is linked.
     3110*/
     3111void QGLShaderProgram::setGeometryOutputVertexCount(int count)
     3112{
     3113#ifndef QT_NO_DEBUG
     3114    int max = maxGeometryOutputVertices();
     3115    if (count > max) {
     3116        qWarning("QGLShaderProgram::setGeometryOutputVertexCount: count: %d higher than maximum: %d",
     3117                 count, max);
     3118    }
     3119#endif
     3120    d_func()->geometryVertexCount = count;
     3121}
     3122
     3123
     3124/*!
     3125    Returns the maximum number of vertices the current geometry shader
     3126    program will produce, if active.
     3127
     3128    \since 4.7
     3129
     3130    This parameter takes effect the ntext time the program is linked.
     3131*/
     3132int QGLShaderProgram::geometryOutputVertexCount() const
     3133{
     3134    return d_func()->geometryVertexCount;
     3135}
     3136
     3137
     3138/*!
     3139    Sets the input type from \a inputType.
     3140
     3141    This parameter takes effect the next time the program is linked.
     3142*/
     3143void QGLShaderProgram::setGeometryInputType(GLenum inputType)
     3144{
     3145    d_func()->geometryInputType = inputType;
     3146}
     3147
     3148
     3149/*!
     3150    Returns the geometry shader input type, if active.
     3151
     3152    This parameter takes effect the next time the program is linked.
     3153
     3154    \since 4.7
     3155 */
     3156
     3157GLenum QGLShaderProgram::geometryInputType() const
     3158{
     3159    return d_func()->geometryInputType;
     3160}
     3161
     3162
     3163/*!
     3164    Sets the output type from the geometry shader, if active, to
     3165    \a outputType.
     3166
     3167    This parameter takes effect the next time the program is linked.
     3168
     3169    \since 4.7
     3170*/
     3171void QGLShaderProgram::setGeometryOutputType(GLenum outputType)
     3172{
     3173    d_func()->geometryOutputType = outputType;
     3174}
     3175
     3176
     3177/*!
     3178    Returns the geometry shader output type, if active.
     3179
     3180    This parameter takes effect the next time the program is linked.
     3181
     3182    \since 4.7
     3183 */
     3184GLenum QGLShaderProgram::geometryOutputType() const
     3185{
     3186    return d_func()->geometryOutputType;
     3187}
     3188
     3189
     3190/*!
    28723191    Returns true if shader programs written in the OpenGL Shading
    28733192    Language (GLSL) are supported on this system; false otherwise.
     
    29013220}
    29023221
     3222
     3223#undef ctx
     3224#undef context
     3225
     3226/*!
     3227    Returns true if shader programs of type \a type are supported on
     3228    this system; false otherwise.
     3229
     3230    The \a context is used to resolve the GLSL extensions.
     3231    If \a context is null, then QGLContext::currentContext() is used.
     3232
     3233    \since 4.7
     3234*/
     3235bool QGLShader::hasOpenGLShaders(ShaderType type, const QGLContext *context)
     3236{
     3237    if (!context)
     3238        context = QGLContext::currentContext();
     3239    if (!context)
     3240        return false;
     3241
     3242    if ((type & ~(Geometry | Vertex | Fragment)) || type == 0)
     3243        return false;
     3244
     3245    bool resolved = qt_resolve_glsl_extensions(const_cast<QGLContext *>(context));
     3246    if (!resolved)
     3247        return false;
     3248
     3249    if ((type & Geometry) && !QByteArray((const char *) glGetString(GL_EXTENSIONS)).contains("GL_EXT_geometry_shader4"))
     3250        return false;
     3251
     3252    return true;
     3253}
     3254
     3255
     3256
    29033257#ifdef Q_MAC_COMPAT_GL_FUNCTIONS
    29043258/*! \internal */
     3259void QGLShaderProgram::setAttributeArray
     3260    (int location, QMacCompatGLenum type, const void *values, int tupleSize, int stride)
     3261{
     3262    setAttributeArray(location, GLenum(type), values, tupleSize, stride);
     3263}
     3264
     3265/*! \internal */
     3266void QGLShaderProgram::setAttributeArray
     3267    (const char *name, QMacCompatGLenum type, const void *values, int tupleSize, int stride)
     3268{
     3269    setAttributeArray(name, GLenum(type), values, tupleSize, stride);
     3270}
     3271
     3272/*! \internal */
     3273void QGLShaderProgram::setAttributeBuffer
     3274    (int location, QMacCompatGLenum type, int offset, int tupleSize, int stride)
     3275{
     3276    setAttributeBuffer(location, GLenum(type), offset, tupleSize, stride);
     3277}
     3278
     3279/*! \internal */
     3280void QGLShaderProgram::setAttributeBuffer
     3281    (const char *name, QMacCompatGLenum type, int offset, int tupleSize, int stride)
     3282{
     3283    setAttributeBuffer(name, GLenum(type), offset, tupleSize, stride);
     3284}
     3285
     3286/*! \internal */
    29053287void QGLShaderProgram::setUniformValue(int location, QMacCompatGLint value)
    29063288{
     
    29513333#endif
    29523334
    2953 #endif // !defined(QT_OPENGL_ES_1_CL) && !defined(QT_OPENGL_ES_1)
     3335#endif // !defined(QT_OPENGL_ES_1)
    29543336
    29553337QT_END_NAMESPACE
Note: See TracChangeset for help on using the changeset viewer.