diff --git a/src/quick/scenegraph/coreapi/qsgmaterial.cpp b/src/quick/scenegraph/coreapi/qsgmaterial.cpp
index c0794d0d69b92b17bec8994d3874f3acb71a5029..9346236db9de3d2b8b4fce94c440e8dbbbc81cc4 100644
--- a/src/quick/scenegraph/coreapi/qsgmaterial.cpp
+++ b/src/quick/scenegraph/coreapi/qsgmaterial.cpp
@@ -556,6 +556,7 @@ static void qt_print_material_count()
 
 QSGMaterial::QSGMaterial()
     : m_flags(0)
+    , m_reserved(0)
 {
 #ifndef QT_NO_DEBUG
     if (qsg_leak_check) {
diff --git a/src/quick/scenegraph/coreapi/qsgmaterial.h b/src/quick/scenegraph/coreapi/qsgmaterial.h
index ee8889deac845a131f6d8a017379b5ec4793aa57..20ab21ad285a713d7be7fb34e1948dfd25544628 100644
--- a/src/quick/scenegraph/coreapi/qsgmaterial.h
+++ b/src/quick/scenegraph/coreapi/qsgmaterial.h
@@ -133,6 +133,7 @@ public:
     void setFlag(Flags flags, bool on = true);
 
 private:
+    friend class QSGContext;
     Flags m_flags;
     void *m_reserved;
     Q_DISABLE_COPY(QSGMaterial)
diff --git a/src/quick/scenegraph/qsgcontext.cpp b/src/quick/scenegraph/qsgcontext.cpp
index 14f0fabbdd78bc336d8ad4e3276dd6dca48e43ed..d3710c7bd595fc619df10317bed9673677cb70a7 100644
--- a/src/quick/scenegraph/qsgcontext.cpp
+++ b/src/quick/scenegraph/qsgcontext.cpp
@@ -478,10 +478,16 @@ QSGDepthStencilBufferManager *QSGContext::depthStencilBufferManager()
 QSGMaterialShader *QSGContext::prepareMaterial(QSGMaterial *material)
 {
     Q_D(QSGContext);
+
+    if (material->m_reserved)
+        return reinterpret_cast<QSGMaterialShader *>(material->m_reserved);
+
     QSGMaterialType *type = material->type();
     QSGMaterialShader *shader = d->materials.value(type);
-    if (shader)
+    if (shader) {
+        material->m_reserved = shader;
         return shader;
+    }
 
 #ifndef QSG_NO_RENDER_TIMING
     if (qsg_render_timing  || QQmlProfilerService::enabled)
@@ -489,6 +495,7 @@ QSGMaterialShader *QSGContext::prepareMaterial(QSGMaterial *material)
 #endif
 
     shader = material->createShader();
+    material->m_reserved = shader;
     shader->compile();
     shader->initialize();
     d->materials[type] = shader;