From 87c1e376b9a11f7b1c47bfb1a94667577e3e4544 Mon Sep 17 00:00:00 2001
From: Jonas Kulla <Nyocurio@gmail.com>
Date: Mon, 20 Jan 2014 00:57:40 +0100
Subject: [PATCH] GLState: Add bound shader program to managed state

Squishes a handful of redundant binds per frame.
---
 src/glstate.cpp | 7 +++++++
 src/glstate.h   | 6 ++++++
 src/shader.cpp  | 4 ++--
 3 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/glstate.cpp b/src/glstate.cpp
index f9c3557..2f1c7fe 100644
--- a/src/glstate.cpp
+++ b/src/glstate.cpp
@@ -20,6 +20,7 @@
 */
 
 #include "glstate.h"
+#include "shader.h"
 #include "etc.h"
 
 #include <glew.h>
@@ -95,6 +96,11 @@ void GLViewport::apply(const IntRect &value)
 	glViewport(value.x, value.y, value.w, value.h);
 }
 
+void GLProgram::apply(const unsigned int &value)
+{
+	glUseProgram(value);
+}
+
 GLState::Caps::Caps()
 {
 	glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize);
@@ -110,4 +116,5 @@ GLState::GLState()
 	scissorTest.init(false);
 	scissorBox.init(IntRect(0, 0, 640, 480));
 	texture2D.init(true);
+	program.init(0);
 }
diff --git a/src/glstate.h b/src/glstate.h
index 9ef66de..613c555 100644
--- a/src/glstate.h
+++ b/src/glstate.h
@@ -99,6 +99,11 @@ class GLViewport : public GLProperty<IntRect>
 	void apply(const IntRect &value);
 };
 
+class GLProgram : public GLProperty<unsigned int> /* GLuint */
+{
+	void apply(const unsigned int &value);
+};
+
 
 class GLState
 {
@@ -109,6 +114,7 @@ public:
 	GLTexture2D texture2D;
 	GLBlendMode blendMode;
 	GLViewport viewport;
+	GLProgram program;
 
 	struct Caps
 	{
diff --git a/src/shader.cpp b/src/shader.cpp
index f11e799..9aefb3b 100644
--- a/src/shader.cpp
+++ b/src/shader.cpp
@@ -99,13 +99,13 @@ Shader::~Shader()
 
 void Shader::bind()
 {
-	glUseProgram(program);
+	glState.program.set(program);
 }
 
 void Shader::unbind()
 {
 	glActiveTexture(GL_TEXTURE0);
-	glUseProgram(0);
+	glState.program.set(0);
 }
 
 void Shader::init(const unsigned char *vert, int vertSize,