From e8620eaa9a0079406fea2b3e8d8965262e983499 Mon Sep 17 00:00:00 2001
From: ReinUsesLisp <reinuseslisp@airmail.cc>
Date: Thu, 29 Nov 2018 03:24:21 -0300
Subject: [PATCH] gl_shader_manager: Update pipeline when programs have changed

---
 .../renderer_opengl/gl_shader_manager.h       | 21 +++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/src/video_core/renderer_opengl/gl_shader_manager.h b/src/video_core/renderer_opengl/gl_shader_manager.h
index b757f5f447..4970aafed6 100644
--- a/src/video_core/renderer_opengl/gl_shader_manager.h
+++ b/src/video_core/renderer_opengl/gl_shader_manager.h
@@ -60,6 +60,17 @@ public:
     }
 
     void ApplyTo(OpenGLState& state) {
+        UpdatePipeline();
+        state.draw.shader_program = 0;
+        state.draw.program_pipeline = pipeline.handle;
+        state.geometry_shaders.enabled = (gs != 0);
+    }
+
+private:
+    void UpdatePipeline() {
+        // Avoid updating the pipeline when values have no changed
+        if (old_vs == vs && old_fs == fs && old_gs == gs)
+            return;
         // Workaround for AMD bug
         glUseProgramStages(pipeline.handle,
                            GL_VERTEX_SHADER_BIT | GL_GEOMETRY_SHADER_BIT | GL_FRAGMENT_SHADER_BIT,
@@ -68,14 +79,16 @@ public:
         glUseProgramStages(pipeline.handle, GL_VERTEX_SHADER_BIT, vs);
         glUseProgramStages(pipeline.handle, GL_GEOMETRY_SHADER_BIT, gs);
         glUseProgramStages(pipeline.handle, GL_FRAGMENT_SHADER_BIT, fs);
-        state.draw.shader_program = 0;
-        state.draw.program_pipeline = pipeline.handle;
-        state.geometry_shaders.enabled = (gs != 0);
+
+        // Update the old values
+        old_vs = vs;
+        old_fs = fs;
+        old_gs = gs;
     }
 
-private:
     OGLPipeline pipeline;
     GLuint vs{}, fs{}, gs{};
+    GLuint old_vs{}, old_fs{}, old_gs{};
 };
 
 } // namespace OpenGL::GLShader