From 51d40d7e9bdfc63c5109aef5b732aa2ba10f985a Mon Sep 17 00:00:00 2001 From: Sean Harmer Date: Mon, 20 Aug 2012 20:55:40 +0100 Subject: Make gui/math3d classes use float rather than qreal This corrects the mismatch between using floats for internal storage and qreal in the API of QVector*D which leads to lots of implicit casts between double and float. This change also stops users from being surprised by the loss of precision when using these classes on desktop platforms and removes the need for the private constructors taking a dummy int as the final argument. The QMatrix4x4 and QQuaternion classes have been changed to use float for their internal storage since these are meant to be used in conjunction with the QVector*D classes. This is to prevent unexpected loss of precision and to improve performance. The on-disk format has also been changed from double to float thereby reducing the storage required when streaming vectors and matrices. This is potentially a large saving when working with complex 3D meshes etc. This also has a significant performance improvement when passing matrices to QOpenGLShaderProgram (and QGLShaderProgram) as we no longer have to iterate and convert the data to floats. This is an operation that could easily be needed many times per frame. This change also opens the door for further optimisations of these classes to be implemented by using SIMD intrinsics. This needs to be applied in conjunction with https://codereview.qt-project.org/#change,33548 Task-number: QTBUG-21035 Task-number: QTBUG-20661 Change-Id: I9321b06040ffb93ae1cbd72fd2013267ac901b2e Reviewed-by: Lars Knoll --- src/opengl/qglshaderprogram.cpp | 76 +++++------------------------------------ 1 file changed, 9 insertions(+), 67 deletions(-) (limited to 'src/opengl/qglshaderprogram.cpp') diff --git a/src/opengl/qglshaderprogram.cpp b/src/opengl/qglshaderprogram.cpp index 4680131e1d..1062669b52 100644 --- a/src/opengl/qglshaderprogram.cpp +++ b/src/opengl/qglshaderprogram.cpp @@ -2191,58 +2191,6 @@ void QGLShaderProgram::setUniformValue(const char *name, const QSizeF& size) setUniformValue(uniformLocation(name), size); } -// We have to repack matrices from qreal to GLfloat. -#define setUniformMatrix(func,location,value,cols,rows) \ - if (location == -1) \ - return; \ - if (sizeof(qreal) == sizeof(GLfloat)) { \ - func(location, 1, GL_FALSE, \ - reinterpret_cast(value.constData())); \ - } else { \ - GLfloat mat[cols * rows]; \ - const qreal *data = value.constData(); \ - for (int i = 0; i < cols * rows; ++i) \ - mat[i] = data[i]; \ - func(location, 1, GL_FALSE, mat); \ - } -#if !defined(QT_OPENGL_ES_2) -#define setUniformGenericMatrix(func,colfunc,location,value,cols,rows) \ - if (location == -1) \ - return; \ - if (sizeof(qreal) == sizeof(GLfloat)) { \ - const GLfloat *data = reinterpret_cast \ - (value.constData()); \ - if (func) \ - func(location, 1, GL_FALSE, data); \ - else \ - colfunc(location, cols, data); \ - } else { \ - GLfloat mat[cols * rows]; \ - const qreal *data = value.constData(); \ - for (int i = 0; i < cols * rows; ++i) \ - mat[i] = data[i]; \ - if (func) \ - func(location, 1, GL_FALSE, mat); \ - else \ - colfunc(location, cols, mat); \ - } -#else -#define setUniformGenericMatrix(func,colfunc,location,value,cols,rows) \ - if (location == -1) \ - return; \ - if (sizeof(qreal) == sizeof(GLfloat)) { \ - const GLfloat *data = reinterpret_cast \ - (value.constData()); \ - colfunc(location, cols, data); \ - } else { \ - GLfloat mat[cols * rows]; \ - const qreal *data = value.constData(); \ - for (int i = 0; i < cols * rows; ++i) \ - mat[i] = data[i]; \ - colfunc(location, cols, mat); \ - } -#endif - /*! Sets the uniform variable at \a location in the current context to a 2x2 matrix \a value. @@ -2253,7 +2201,7 @@ void QGLShaderProgram::setUniformValue(int location, const QMatrix2x2& value) { Q_D(QGLShaderProgram); Q_UNUSED(d); - setUniformMatrix(glUniformMatrix2fv, location, value, 2, 2); + glUniformMatrix2fv(location, 1, GL_FALSE, value.constData()); } /*! @@ -2279,8 +2227,7 @@ void QGLShaderProgram::setUniformValue(int location, const QMatrix2x3& value) { Q_D(QGLShaderProgram); Q_UNUSED(d); - setUniformGenericMatrix - (glUniformMatrix2x3fv, glUniform3fv, location, value, 2, 3); + glUniform3fv(location, 2, value.constData()); } /*! @@ -2306,8 +2253,7 @@ void QGLShaderProgram::setUniformValue(int location, const QMatrix2x4& value) { Q_D(QGLShaderProgram); Q_UNUSED(d); - setUniformGenericMatrix - (glUniformMatrix2x4fv, glUniform4fv, location, value, 2, 4); + glUniform4fv(location, 2, value.constData()); } /*! @@ -2333,8 +2279,7 @@ void QGLShaderProgram::setUniformValue(int location, const QMatrix3x2& value) { Q_D(QGLShaderProgram); Q_UNUSED(d); - setUniformGenericMatrix - (glUniformMatrix3x2fv, glUniform2fv, location, value, 3, 2); + glUniform2fv(location, 3, value.constData()); } /*! @@ -2360,7 +2305,7 @@ void QGLShaderProgram::setUniformValue(int location, const QMatrix3x3& value) { Q_D(QGLShaderProgram); Q_UNUSED(d); - setUniformMatrix(glUniformMatrix3fv, location, value, 3, 3); + glUniformMatrix3fv(location, 1, GL_FALSE, value.constData()); } /*! @@ -2386,8 +2331,7 @@ void QGLShaderProgram::setUniformValue(int location, const QMatrix3x4& value) { Q_D(QGLShaderProgram); Q_UNUSED(d); - setUniformGenericMatrix - (glUniformMatrix3x4fv, glUniform4fv, location, value, 3, 4); + glUniform4fv(location, 3, value.constData()); } /*! @@ -2413,8 +2357,7 @@ void QGLShaderProgram::setUniformValue(int location, const QMatrix4x2& value) { Q_D(QGLShaderProgram); Q_UNUSED(d); - setUniformGenericMatrix - (glUniformMatrix4x2fv, glUniform2fv, location, value, 4, 2); + glUniform2fv(location, 4, value.constData()); } /*! @@ -2440,8 +2383,7 @@ void QGLShaderProgram::setUniformValue(int location, const QMatrix4x3& value) { Q_D(QGLShaderProgram); Q_UNUSED(d); - setUniformGenericMatrix - (glUniformMatrix4x3fv, glUniform3fv, location, value, 4, 3); + glUniform3fv(location, 4, value.constData()); } /*! @@ -2467,7 +2409,7 @@ void QGLShaderProgram::setUniformValue(int location, const QMatrix4x4& value) { Q_D(QGLShaderProgram); Q_UNUSED(d); - setUniformMatrix(glUniformMatrix4fv, location, value, 4, 4); + glUniformMatrix4fv(location, 1, GL_FALSE, value.constData()); } /*! -- cgit v1.2.3