summaryrefslogtreecommitdiffstats
path: root/src/gui/opengl/qopenglengineshadermanager.cpp
diff options
context:
space:
mode:
authorLaszlo Agocs <laszlo.agocs@qt.io>2016-09-29 12:59:06 +0200
committerLaszlo Agocs <laszlo.agocs@qt.io>2016-11-24 10:27:16 +0000
commit85f868e73e4cf9dffe27b737f8dc3f5bb626ed04 (patch)
tree60f19e5319e14957e98edeeafaa8da8279972b75 /src/gui/opengl/qopenglengineshadermanager.cpp
parentffd316ebe3963b7ff8d94a3484ac85de793b8299 (diff)
Add an OpenGL program binary disk cache
Introduce a glProgramBinary-based disk cache in QOpenGLShaderProgram. By switching the typical program->addShaderFromSourceCode(QOpenGLShader::Vertex, ...) program->addShaderFromSourceCode(QOpenGLShader::Fragment, ...) invocations to program->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, ...) program->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, ...) the compilation may be skipped via gl(Get)ProgramBinary and a disk cache, when supported. Such QOpenGLShaderProgram instances will have no QOpenGLShader instances attached. Instead, the entire program binary (which is driver-specific) is loaded as-is. Support means OpenGL ES 3.0 or the presence of GL_ARB_get_program_binary, in combination with >= 1 supported binary formats. Note that some drivers claim program binary support but expose no formats. This amounts to no support in practice. When support is not present, calling the new functions is equivalent to the non-cacheable variants. If the OpenGL driver changes (vendor, renderer, version strings), recompilation and storage of the new, potentially incompatible binary program will happen transparently. The cache can always be disabled by setting QT_DISABLE_SHADER_DISK_CACHE=1 or the new application attribute Qt::AA_DisableShaderDiskCache. Location-wise the primary choice is the shared cache (GenericCacheLocation). If this is not available or is not writable, the per-process one (CacheLocation) is used instead. In addition to the new public APIs in QOpenGLShaderProgram, the main shader users in QtGui are migrated as well. (OpenGL paint engine, glyph cache, blitter, eglfs mouse cursor). This means that any application using QPainter on OpenGL or widgets with eglfs will benefit from the improved startup times. Qt Quick will follow suit as well. [ChangeLog][QtGui][OpenGL] QOpenGLShaderProgram offers a built-in program binary disk cache for systems with OpenGL ES 3.x or GL_ARB_get_program_binary. This can lead to significant increases in performance when it comes to application startup times for example. Usage is opt-in for direct C++ users of the class, however Qt's own main users of shaders, including Qt Quick and QPainter's OpenGL engine, are migrated to use the new, cache-enabled APIs. Opting out on application level is always possible via Qt::AA_DisableShaderDiskCache. Task-number: QTBUG-55496 Change-Id: I556f053d258bfa6887b1d5238c9f6396914c5421 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Andy Nichols <andy.nichols@qt.io>
Diffstat (limited to 'src/gui/opengl/qopenglengineshadermanager.cpp')
-rw-r--r--src/gui/opengl/qopenglengineshadermanager.cpp97
1 files changed, 27 insertions, 70 deletions
diff --git a/src/gui/opengl/qopenglengineshadermanager.cpp b/src/gui/opengl/qopenglengineshadermanager.cpp
index 1d3e47f93b..c7e457b364 100644
--- a/src/gui/opengl/qopenglengineshadermanager.cpp
+++ b/src/gui/opengl/qopenglengineshadermanager.cpp
@@ -208,8 +208,6 @@ QOpenGLEngineSharedShaders::QOpenGLEngineSharedShaders(QOpenGLContext* context)
snippetsPopulated = true;
}
- QOpenGLShader* fragShader;
- QOpenGLShader* vertexShader;
QByteArray vertexSource;
QByteArray fragSource;
@@ -227,19 +225,11 @@ QOpenGLEngineSharedShaders::QOpenGLEngineSharedShaders(QOpenGLContext* context)
bool inCache = simpleShaderCache.load(simpleShaderProg, context);
if (!inCache) {
- vertexShader = new QOpenGLShader(QOpenGLShader::Vertex);
- shaders.append(vertexShader);
- if (!vertexShader->compileSourceCode(vertexSource))
+ if (!simpleShaderProg->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, vertexSource))
qWarning("Vertex shader for simpleShaderProg (MainVertexShader & PositionOnlyVertexShader) failed to compile");
-
- fragShader = new QOpenGLShader(QOpenGLShader::Fragment);
- shaders.append(fragShader);
- if (!fragShader->compileSourceCode(fragSource))
+ if (!simpleShaderProg->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, fragSource))
qWarning("Fragment shader for simpleShaderProg (MainFragmentShader & ShockingPinkSrcFragmentShader) failed to compile");
- simpleShaderProg->addShader(vertexShader);
- simpleShaderProg->addShader(fragShader);
-
simpleShaderProg->bindAttributeLocation("vertexCoordsArray", QT_VERTEX_COORDS_ATTR);
simpleShaderProg->bindAttributeLocation("pmvMatrix1", QT_PMV_MATRIX_1_ATTR);
simpleShaderProg->bindAttributeLocation("pmvMatrix2", QT_PMV_MATRIX_2_ATTR);
@@ -271,19 +261,11 @@ QOpenGLEngineSharedShaders::QOpenGLEngineSharedShaders(QOpenGLContext* context)
inCache = blitShaderCache.load(blitShaderProg, context);
if (!inCache) {
- vertexShader = new QOpenGLShader(QOpenGLShader::Vertex);
- shaders.append(vertexShader);
- if (!vertexShader->compileSourceCode(vertexSource))
+ if (!blitShaderProg->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, vertexSource))
qWarning("Vertex shader for blitShaderProg (MainWithTexCoordsVertexShader & UntransformedPositionVertexShader) failed to compile");
-
- fragShader = new QOpenGLShader(QOpenGLShader::Fragment);
- shaders.append(fragShader);
- if (!fragShader->compileSourceCode(fragSource))
+ if (!blitShaderProg->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, fragSource))
qWarning("Fragment shader for blitShaderProg (MainFragmentShader & ImageSrcFragmentShader) failed to compile");
- blitShaderProg->addShader(vertexShader);
- blitShaderProg->addShader(fragShader);
-
blitShaderProg->bindAttributeLocation("textureCoordArray", QT_TEXTURE_COORDS_ATTR);
blitShaderProg->bindAttributeLocation("vertexCoordsArray", QT_VERTEX_COORDS_ATTR);
}
@@ -306,9 +288,6 @@ QOpenGLEngineSharedShaders::~QOpenGLEngineSharedShaders()
#ifdef QT_GL_SHARED_SHADER_DEBUG
qDebug(" -> ~QOpenGLEngineSharedShaders() %p for thread %p.", this, QThread::currentThread());
#endif
- qDeleteAll(shaders);
- shaders.clear();
-
qDeleteAll(cachedPrograms);
cachedPrograms.clear();
@@ -370,50 +349,37 @@ QOpenGLEngineShaderProg *QOpenGLEngineSharedShaders::findProgramInCache(const QO
bool inCache = shaderCache.load(shaderProgram.data(), QOpenGLContext::currentContext());
if (!inCache) {
-
- QScopedPointer<QOpenGLShader> fragShader(new QOpenGLShader(QOpenGLShader::Fragment));
- QByteArray description;
+ if (!shaderProgram->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, vertexSource)) {
+ QByteArray description;
#if defined(QT_DEBUG)
- // Name the shader for easier debugging
- description.append("Fragment shader: main=");
- description.append(snippetNameStr(prog.mainFragShader));
- description.append(", srcPixel=");
- description.append(snippetNameStr(prog.srcPixelFragShader));
- if (prog.compositionFragShader) {
- description.append(", composition=");
- description.append(snippetNameStr(prog.compositionFragShader));
- }
- if (prog.maskFragShader) {
- description.append(", mask=");
- description.append(snippetNameStr(prog.maskFragShader));
- }
- fragShader->setObjectName(QString::fromLatin1(description));
+ description.append("Vertex shader: main=");
+ description.append(snippetNameStr(prog.mainVertexShader));
+ description.append(", position=");
+ description.append(snippetNameStr(prog.positionVertexShader));
#endif
- if (!fragShader->compileSourceCode(fragSource)) {
qWarning("Warning: \"%s\" failed to compile!", description.constData());
break;
}
-
- QScopedPointer<QOpenGLShader> vertexShader(new QOpenGLShader(QOpenGLShader::Vertex));
+ if (!shaderProgram->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, fragSource)) {
+ QByteArray description;
#if defined(QT_DEBUG)
- // Name the shader for easier debugging
- description.clear();
- description.append("Vertex shader: main=");
- description.append(snippetNameStr(prog.mainVertexShader));
- description.append(", position=");
- description.append(snippetNameStr(prog.positionVertexShader));
- vertexShader->setObjectName(QString::fromLatin1(description));
+ description.append("Fragment shader: main=");
+ description.append(snippetNameStr(prog.mainFragShader));
+ description.append(", srcPixel=");
+ description.append(snippetNameStr(prog.srcPixelFragShader));
+ if (prog.compositionFragShader) {
+ description.append(", composition=");
+ description.append(snippetNameStr(prog.compositionFragShader));
+ }
+ if (prog.maskFragShader) {
+ description.append(", mask=");
+ description.append(snippetNameStr(prog.maskFragShader));
+ }
#endif
- if (!vertexShader->compileSourceCode(vertexSource)) {
qWarning("Warning: \"%s\" failed to compile!", description.constData());
break;
}
- shaders.append(vertexShader.data());
- shaders.append(fragShader.data());
- shaderProgram->addShader(vertexShader.take());
- shaderProgram->addShader(fragShader.take());
-
// We have to bind the vertex attribute names before the program is linked:
shaderProgram->bindAttributeLocation("vertexCoordsArray", QT_VERTEX_COORDS_ATTR);
if (prog.useTextureCoords)
@@ -436,18 +402,9 @@ QOpenGLEngineShaderProg *QOpenGLEngineSharedShaders::findProgramInCache(const QO
shaderCache.store(newProg->program, QOpenGLContext::currentContext());
} else {
QString error;
- error = QLatin1String("Shader program failed to link,");
-#if defined(QT_DEBUG)
- QLatin1String br("\n");
- error += QLatin1String("\n Shaders Used:\n");
- for (int i = 0; i < newProg->program->shaders().count(); ++i) {
- QOpenGLShader *shader = newProg->program->shaders().at(i);
- error += QLatin1String(" ") + shader->objectName() + QLatin1String(": \n")
- + QLatin1String(shader->sourceCode()) + br;
- }
-#endif
- error += QLatin1String(" Error Log:\n")
- + QLatin1String(" ") + newProg->program->log();
+ error = QLatin1String("Shader program failed to link")
+ + QLatin1String(" Error Log:\n")
+ + QLatin1String(" ") + newProg->program->log();
qWarning() << error;
break;
}