519 QGLShaderProgramPrivate(const QGLContext *context) |
527 QGLShaderProgramPrivate(const QGLContext *context) |
520 : programGuard(context) |
528 : programGuard(context) |
521 , linked(false) |
529 , linked(false) |
522 , inited(false) |
530 , inited(false) |
523 , removingShaders(false) |
531 , removingShaders(false) |
524 , vertexShader(0) |
532 , geometryVertexCount(64) |
525 , fragmentShader(0) |
533 , geometryInputType(0) |
|
534 , geometryOutputType(0) |
526 { |
535 { |
527 } |
536 } |
528 ~QGLShaderProgramPrivate(); |
537 ~QGLShaderProgramPrivate(); |
529 |
538 |
530 QGLSharedResourceGuard programGuard; |
539 QGLSharedResourceGuard programGuard; |
531 bool linked; |
540 bool linked; |
532 bool inited; |
541 bool inited; |
533 bool removingShaders; |
542 bool removingShaders; |
|
543 |
|
544 int geometryVertexCount; |
|
545 GLenum geometryInputType; |
|
546 GLenum geometryOutputType; |
|
547 |
534 QString log; |
548 QString log; |
535 QList<QGLShader *> shaders; |
549 QList<QGLShader *> shaders; |
536 QList<QGLShader *> anonShaders; |
550 QList<QGLShader *> anonShaders; |
537 QGLShader *vertexShader; |
|
538 QGLShader *fragmentShader; |
|
539 |
551 |
540 bool hasShader(QGLShader::ShaderType type) const; |
552 bool hasShader(QGLShader::ShaderType type) const; |
541 }; |
553 }; |
542 |
554 |
543 QGLShaderProgramPrivate::~QGLShaderProgramPrivate() |
555 QGLShaderProgramPrivate::~QGLShaderProgramPrivate() |
841 glGetProgramiv(program, GL_LINK_STATUS, &value); |
855 glGetProgramiv(program, GL_LINK_STATUS, &value); |
842 d->linked = (value != 0); |
856 d->linked = (value != 0); |
843 if (d->linked) |
857 if (d->linked) |
844 return true; |
858 return true; |
845 } |
859 } |
|
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 |
846 glLinkProgram(program); |
876 glLinkProgram(program); |
847 value = 0; |
877 value = 0; |
848 glGetProgramiv(program, GL_LINK_STATUS, &value); |
878 glGetProgramiv(program, GL_LINK_STATUS, &value); |
849 d->linked = (value != 0); |
879 d->linked = (value != 0); |
850 value = 0; |
880 value = 0; |
1431 stride, values); |
1462 stride, values); |
1432 } |
1463 } |
1433 } |
1464 } |
1434 |
1465 |
1435 /*! |
1466 /*! |
|
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 */ |
|
1487 void 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_FALSE, |
|
1494 stride, values); |
|
1495 } |
|
1496 } |
|
1497 |
|
1498 /*! |
1436 \overload |
1499 \overload |
1437 |
1500 |
1438 Sets an array of vertex \a values on the attribute called \a name |
1501 Sets an array of vertex \a values on the attribute called \a name |
1439 in this shader program. The \a tupleSize indicates the number of |
1502 in this shader program. The \a tupleSize indicates the number of |
1440 components per vertex (1, 2, 3, or 4), and the \a stride indicates |
1503 components per vertex (1, 2, 3, or 4), and the \a stride indicates |
1513 */ |
1576 */ |
1514 void QGLShaderProgram::setAttributeArray |
1577 void QGLShaderProgram::setAttributeArray |
1515 (const char *name, const QVector4D *values, int stride) |
1578 (const char *name, const QVector4D *values, int stride) |
1516 { |
1579 { |
1517 setAttributeArray(attributeLocation(name), values, stride); |
1580 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 */ |
|
1606 void 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 */ |
|
1631 void 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_FALSE, 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 */ |
|
1663 void QGLShaderProgram::setAttributeBuffer |
|
1664 (const char *name, GLenum type, int offset, int tupleSize, int stride) |
|
1665 { |
|
1666 setAttributeBuffer(attributeLocation(name), type, offset, tupleSize, stride); |
1518 } |
1667 } |
1519 |
1668 |
1520 /*! |
1669 /*! |
1521 Enables the vertex array at \a location in this shader program |
1670 Enables the vertex array at \a location in this shader program |
1522 so that the value set by setAttributeArray() on \a location |
1671 so that the value set by setAttributeArray() on \a location |
2312 |
2462 |
2313 /*! |
2463 /*! |
2314 \overload |
2464 \overload |
2315 |
2465 |
2316 Sets the uniform variable at \a location in the current context |
2466 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 */ |
|
2473 void 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 */ |
|
2491 void 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 |
2317 to a 4x4 matrix \a value. The matrix elements must be specified |
2503 to a 4x4 matrix \a value. The matrix elements must be specified |
2318 in column-major order. |
2504 in column-major order. |
2319 |
2505 |
2320 \sa setAttributeValue() |
2506 \sa setAttributeValue() |
2321 */ |
2507 */ |
2323 { |
2509 { |
2324 Q_D(QGLShaderProgram); |
2510 Q_D(QGLShaderProgram); |
2325 Q_UNUSED(d); |
2511 Q_UNUSED(d); |
2326 if (location != -1) |
2512 if (location != -1) |
2327 glUniformMatrix4fv(location, 1, GL_FALSE, value[0]); |
2513 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 */ |
|
2527 void 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 */ |
|
2542 void QGLShaderProgram::setUniformValue(const char *name, const GLfloat value[3][3]) |
|
2543 { |
|
2544 setUniformValue(uniformLocation(name), value); |
2328 } |
2545 } |
2329 |
2546 |
2330 /*! |
2547 /*! |
2331 \overload |
2548 \overload |
2332 |
2549 |
2352 { |
2569 { |
2353 Q_D(QGLShaderProgram); |
2570 Q_D(QGLShaderProgram); |
2354 Q_UNUSED(d); |
2571 Q_UNUSED(d); |
2355 if (location != -1) { |
2572 if (location != -1) { |
2356 GLfloat mat[3][3] = { |
2573 GLfloat mat[3][3] = { |
2357 {value.m11(), value.m12(), value.m13()}, |
2574 {GLfloat(value.m11()), GLfloat(value.m12()), GLfloat(value.m13())}, |
2358 {value.m21(), value.m22(), value.m23()}, |
2575 {GLfloat(value.m21()), GLfloat(value.m22()), GLfloat(value.m23())}, |
2359 {value.m31(), value.m32(), value.m33()} |
2576 {GLfloat(value.m31()), GLfloat(value.m32()), GLfloat(value.m33())} |
2360 }; |
2577 }; |
2361 glUniformMatrix3fv(location, 1, GL_FALSE, mat[0]); |
2578 glUniformMatrix3fv(location, 1, GL_FALSE, mat[0]); |
2362 } |
2579 } |
2363 } |
2580 } |
2364 |
2581 |
2867 } |
3084 } |
2868 |
3085 |
2869 #undef ctx |
3086 #undef ctx |
2870 |
3087 |
2871 /*! |
3088 /*! |
|
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 */ |
|
3096 int 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 */ |
|
3111 void 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 */ |
|
3132 int 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 */ |
|
3143 void 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 |
|
3157 GLenum 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 */ |
|
3171 void 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 */ |
|
3184 GLenum QGLShaderProgram::geometryOutputType() const |
|
3185 { |
|
3186 return d_func()->geometryOutputType; |
|
3187 } |
|
3188 |
|
3189 |
|
3190 /*! |
2872 Returns true if shader programs written in the OpenGL Shading |
3191 Returns true if shader programs written in the OpenGL Shading |
2873 Language (GLSL) are supported on this system; false otherwise. |
3192 Language (GLSL) are supported on this system; false otherwise. |
2874 |
3193 |
2875 The \a context is used to resolve the GLSL extensions. |
3194 The \a context is used to resolve the GLSL extensions. |
2876 If \a context is null, then QGLContext::currentContext() is used. |
3195 If \a context is null, then QGLContext::currentContext() is used. |
2898 QGLShader *shader = qobject_cast<QGLShader *>(sender()); |
3217 QGLShader *shader = qobject_cast<QGLShader *>(sender()); |
2899 if (shader && !d->removingShaders) |
3218 if (shader && !d->removingShaders) |
2900 removeShader(shader); |
3219 removeShader(shader); |
2901 } |
3220 } |
2902 |
3221 |
|
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 */ |
|
3235 bool 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 |
2903 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS |
3257 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS |
2904 /*! \internal */ |
3258 /*! \internal */ |
|
3259 void 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 */ |
|
3266 void 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 */ |
|
3273 void 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 */ |
|
3280 void 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 */ |
2905 void QGLShaderProgram::setUniformValue(int location, QMacCompatGLint value) |
3287 void QGLShaderProgram::setUniformValue(int location, QMacCompatGLint value) |
2906 { |
3288 { |
2907 setUniformValue(location, GLint(value)); |
3289 setUniformValue(location, GLint(value)); |
2908 } |
3290 } |
2909 |
3291 |