path: root/src/gui/opengl/qopenglprogrambinarycache_p.h
diff options
authorLaszlo Agocs <>2016-09-29 12:59:06 +0200
committerLaszlo Agocs <>2016-11-24 10:27:16 +0000
commit85f868e73e4cf9dffe27b737f8dc3f5bb626ed04 (patch)
tree60f19e5319e14957e98edeeafaa8da8279972b75 /src/gui/opengl/qopenglprogrambinarycache_p.h
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 <> Reviewed-by: Andy Nichols <>
Diffstat (limited to 'src/gui/opengl/qopenglprogrambinarycache_p.h')
1 files changed, 89 insertions, 0 deletions
diff --git a/src/gui/opengl/qopenglprogrambinarycache_p.h b/src/gui/opengl/qopenglprogrambinarycache_p.h
new file mode 100644
index 0000000000..5625ad9c21
--- /dev/null
+++ b/src/gui/opengl/qopenglprogrambinarycache_p.h
@@ -0,0 +1,89 @@
+** Copyright (C) 2016 The Qt Company Ltd.
+** Contact:
+** This file is part of the QtGui module of the Qt Toolkit.
+** 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 For further
+** information use the contact form at
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met:
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: and
+#include <QtGui/qtguiglobal.h>
+#include <QtGui/qopenglshaderprogram.h>
+#include <QtCore/qcache.h>
+class QOpenGLProgramBinaryCache
+ struct ShaderDesc {
+ ShaderDesc() { }
+ ShaderDesc(QOpenGLShader::ShaderType type, const QByteArray &source = QByteArray())
+ : type(type), source(source)
+ { }
+ QOpenGLShader::ShaderType type;
+ QByteArray source;
+ };
+ struct ProgramDesc {
+ QVector<ShaderDesc> shaders;
+ };
+ QOpenGLProgramBinaryCache();
+ bool load(const QByteArray &cacheKey, uint programId);
+ void save(const QByteArray &cacheKey, uint programId);
+ QString cacheFileName(const QByteArray &cacheKey) const;
+ bool verifyHeader(const QByteArray &buf) const;
+ bool setProgramBinary(uint programId, uint blobFormat, const void *p, uint blobSize);
+ QString m_cacheDir;
+ bool m_cacheWritable;
+ struct MemCacheEntry {
+ MemCacheEntry(const void *p, int size, uint format)
+ : blob(reinterpret_cast<const char *>(p), size),
+ format(format)
+ { }
+ QByteArray blob;
+ uint format;
+ };
+ QCache<QByteArray, MemCacheEntry> m_memCache;