From 4ddeb6e460eba94bfc65b07559160c441545a8d4 Mon Sep 17 00:00:00 2001
From: Gunnar Sletta <gunnar.sletta@digia.com>
Date: Fri, 19 Apr 2013 15:30:41 +0200
Subject: [PATCH] Precompile the most common materials

Change-Id: Idfa76f9fcdbac61a248b38e1f793a6628af12552
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@digia.com>
---
 src/quick/scenegraph/qsgcontext.cpp | 28 ++++++++++++++++++++++++++++
 src/quick/scenegraph/qsgcontext_p.h |  1 +
 2 files changed, 29 insertions(+)

diff --git a/src/quick/scenegraph/qsgcontext.cpp b/src/quick/scenegraph/qsgcontext.cpp
index 4ebae38699..61fb1bc5b9 100644
--- a/src/quick/scenegraph/qsgcontext.cpp
+++ b/src/quick/scenegraph/qsgcontext.cpp
@@ -47,7 +47,9 @@
 #include <QtQuick/private/qsgdefaultimagenode_p.h>
 #include <QtQuick/private/qsgdefaultglyphnode_p.h>
 #include <QtQuick/private/qsgdistancefieldglyphnode_p.h>
+#include <QtQuick/private/qsgdistancefieldglyphnode_p_p.h>
 #include <QtQuick/private/qsgshareddistancefieldglyphcache_p.h>
+#include <QtQuick/QSGFlatColorMaterial>
 
 #include <QtQuick/private/qsgtexture_p.h>
 #include <QtQuick/private/qquickpixmapcache_p.h>
@@ -263,9 +265,35 @@ void QSGContext::initialize(QOpenGLContext *context)
     Q_ASSERT(!d->gl);
     d->gl = context;
 
+    precompileMaterials();
+
     emit initialized();
 }
 
+#define QSG_PRECOMPILE_MATERIAL(name) { name m; prepareMaterial(&m); }
+
+/*
+ * Some glsl compilers take their time compiling materials, and
+ * the way the scene graph is being processed, these materials
+ * get compiled when they are first taken into use. This can
+ * easily lead to skipped frames. By precompiling the most
+ * common materials, we potentially add a few milliseconds to the
+ * start up, and reduce the chance of avoiding skipped frames
+ * later on.
+ */
+void QSGContext::precompileMaterials()
+{
+    if (qEnvironmentVariableIsEmpty("QSG_NO_MATERIAL_PRELOADING")) {
+        QSG_PRECOMPILE_MATERIAL(QSGVertexColorMaterial);
+        QSG_PRECOMPILE_MATERIAL(QSGFlatColorMaterial);
+        QSG_PRECOMPILE_MATERIAL(QSGOpaqueTextureMaterial);
+        QSG_PRECOMPILE_MATERIAL(QSGTextureMaterial);
+        QSG_PRECOMPILE_MATERIAL(SmoothTextureMaterial);
+        QSG_PRECOMPILE_MATERIAL(SmoothColorMaterial);
+        QSG_PRECOMPILE_MATERIAL(QSGDistanceFieldTextMaterial);
+    }
+}
+
 
 /*!
     Returns if the scene graph context is ready or not, meaning that it has a valid
diff --git a/src/quick/scenegraph/qsgcontext_p.h b/src/quick/scenegraph/qsgcontext_p.h
index b069c53dd3..bbc42674c6 100644
--- a/src/quick/scenegraph/qsgcontext_p.h
+++ b/src/quick/scenegraph/qsgcontext_p.h
@@ -90,6 +90,7 @@ public:
 
     bool isReady() const;
 
+    virtual void precompileMaterials();
     QSGMaterialShader *prepareMaterial(QSGMaterial *material);
 
     virtual void renderNextFrame(QSGRenderer *renderer, GLuint fboId);
-- 
GitLab