From 39df6c56243117bad6b3746dc045b05a88ab070d Mon Sep 17 00:00:00 2001
From: hrydgard <hrydgard@gmail.com>
Date: Wed, 22 Oct 2008 20:54:40 +0000
Subject: [PATCH] Move out Cg shader generators to VideoCommon (hope to use
 this in the DX plugin in the future). Also move out stats code. Comment a lot
 and cleanup. Kill DX9 Globals.cpp.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@938 8ced0084-cf51-0410-be5f-012b33b47a6e
---
 Source/Core/DebuggerWX/Src/BreakpointView.cpp |   2 +-
 .../VideoCommon}/Src/PixelShader.cpp          |  12 +-
 .../VideoCommon}/Src/PixelShader.h            |   0
 Source/Core/VideoCommon/Src/SConscript        |   3 +
 Source/Core/VideoCommon/Src/Statistics.cpp    |  43 +++++++
 .../VideoCommon/Src/Statistics.h}             |  60 +--------
 .../VideoCommon}/Src/VertexShader.cpp         |   6 +-
 .../VideoCommon}/Src/VertexShader.h           |   3 +-
 Source/Core/VideoCommon/Src/XFBConvert.h      |   3 +-
 Source/Core/VideoCommon/VideoCommon.vcproj    |  76 ++++++++----
 .../Plugin_VideoDX9/Plugin_VideoDX9.vcproj    |   4 +-
 .../Plugins/Plugin_VideoDX9/Src/BPStructs.cpp |   2 +-
 .../Src/{Globals.cpp => Config.cpp}           |   4 +-
 Source/Plugins/Plugin_VideoDX9/Src/Config.h   |  67 ++++++++++
 .../Plugin_VideoDX9/Src/D3DPostprocess.cpp    |   2 +-
 .../Plugins/Plugin_VideoDX9/Src/D3DShader.cpp |   2 +-
 .../Plugin_VideoDX9/Src/DlgSettings.cpp       |   2 +-
 .../Plugins/Plugin_VideoDX9/Src/EmuWindow.cpp |   2 +-
 .../Plugin_VideoDX9/Src/OpcodeDecoding.cpp    |  12 +-
 .../Plugin_VideoDX9/Src/PixelShader.cpp       |   1 -
 Source/Plugins/Plugin_VideoDX9/Src/Render.cpp |   4 +-
 .../Plugin_VideoDX9/Src/ShaderManager.cpp     |   2 +-
 .../Plugin_VideoDX9/Src/TextureCache.cpp      |   3 +-
 .../Plugin_VideoDX9/Src/TransformEngine.cpp   |   2 +-
 .../Plugin_VideoDX9/Src/VertexHandler.cpp     |   5 +-
 Source/Plugins/Plugin_VideoDX9/Src/main.cpp   |   2 +-
 .../Plugin_VideoOGL/Plugin_VideoOGL.vcproj    |  16 ---
 Source/Plugins/Plugin_VideoOGL/Src/GLInit.h   |   2 -
 .../Plugins/Plugin_VideoOGL/Src/Globals.cpp   |   7 --
 Source/Plugins/Plugin_VideoOGL/Src/Globals.h  |  56 ---------
 .../Plugin_VideoOGL/Src/OpcodeDecoding.cpp    |  25 +---
 .../Src/PixelShaderManager.cpp                |   5 +-
 .../Plugin_VideoOGL/Src/PixelShaderManager.h  | 114 ++++++++----------
 Source/Plugins/Plugin_VideoOGL/Src/Render.cpp |   1 +
 Source/Plugins/Plugin_VideoOGL/Src/SConscript |   2 -
 .../Plugin_VideoOGL/Src/TextureMngr.cpp       |  12 +-
 .../Plugin_VideoOGL/Src/VertexLoader.cpp      |  93 +++++++-------
 .../Plugin_VideoOGL/Src/VertexLoader.h        |  23 ++--
 .../Plugin_VideoOGL/Src/VertexManager.cpp     |  25 ++--
 .../Src/VertexShaderManager.cpp               |  27 +++--
 .../Plugin_VideoOGL/Src/VertexShaderManager.h | 100 +++++++--------
 41 files changed, 416 insertions(+), 416 deletions(-)
 rename Source/{Plugins/Plugin_VideoOGL => Core/VideoCommon}/Src/PixelShader.cpp (96%)
 rename Source/{Plugins/Plugin_VideoOGL => Core/VideoCommon}/Src/PixelShader.h (100%)
 create mode 100644 Source/Core/VideoCommon/Src/Statistics.cpp
 rename Source/{Plugins/Plugin_VideoDX9/Src/Globals.h => Core/VideoCommon/Src/Statistics.h} (62%)
 rename Source/{Plugins/Plugin_VideoOGL => Core/VideoCommon}/Src/VertexShader.cpp (97%)
 rename Source/{Plugins/Plugin_VideoOGL => Core/VideoCommon}/Src/VertexShader.h (93%)
 rename Source/Plugins/Plugin_VideoDX9/Src/{Globals.cpp => Config.cpp} (96%)
 create mode 100644 Source/Plugins/Plugin_VideoDX9/Src/Config.h

diff --git a/Source/Core/DebuggerWX/Src/BreakpointView.cpp b/Source/Core/DebuggerWX/Src/BreakpointView.cpp
index cde9a9519d..2f863af897 100644
--- a/Source/Core/DebuggerWX/Src/BreakpointView.cpp
+++ b/Source/Core/DebuggerWX/Src/BreakpointView.cpp
@@ -115,7 +115,7 @@ void CBreakPointView::DeleteCurrentSelection()
     int Item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
     if (Item >= 0)
     {
-        u32 Address = GetItemData(Item);
+        u32 Address = (u32)GetItemData(Item);
         CBreakPoints::DeleteElementByAddress(Address);
     }
 }
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShader.cpp b/Source/Core/VideoCommon/Src/PixelShader.cpp
similarity index 96%
rename from Source/Plugins/Plugin_VideoOGL/Src/PixelShader.cpp
rename to Source/Core/VideoCommon/Src/PixelShader.cpp
index 31cc8d7935..0e9691ff75 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShader.cpp
+++ b/Source/Core/VideoCommon/Src/PixelShader.cpp
@@ -15,8 +15,6 @@
 // Official SVN repository and contact information can be found at
 // http://code.google.com/p/dolphin-emu/
 
-#include "Globals.h"
-
 #include <stdio.h>
 #include <cmath>
 #include <assert.h>
@@ -429,14 +427,16 @@ char *GeneratePixelShader(u32 texture_mask, bool has_zbuffer_target, bool bRende
     }
     else {
         if (!bRenderZToCol0) {
-            /* NEEDS FIX - dstalpha does not change how fragments are blended with the EFB
+            /* donkopunchstania: NEEDS FIX - dstalpha does not change how fragments are blended with the EFB
 			   once the blending is done, the dstalpha is written to the EFB in place of the
 			   fragment alpha if dstalpha is enabled. this only matters if the EFB supports alpha.
+               Commenting this out fixed Metroids but causes glitches in Super Mario Sunshine.
+
 			if (bpmem.dstalpha.enable)
                 WRITE(p, "  ocol0 = float4(prev.rgb,"I_ALPHA"[0].w);\n");
-            else*/
-            
-			WRITE(p, "  ocol0 = prev;\n");
+            else
+			   */
+            	WRITE(p, "  ocol0 = prev;\n");
         }
     }
 
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShader.h b/Source/Core/VideoCommon/Src/PixelShader.h
similarity index 100%
rename from Source/Plugins/Plugin_VideoOGL/Src/PixelShader.h
rename to Source/Core/VideoCommon/Src/PixelShader.h
diff --git a/Source/Core/VideoCommon/Src/SConscript b/Source/Core/VideoCommon/Src/SConscript
index 39187ad818..4a4348a9f1 100644
--- a/Source/Core/VideoCommon/Src/SConscript
+++ b/Source/Core/VideoCommon/Src/SConscript
@@ -9,6 +9,9 @@ files = [
 	"TextureDecoder.cpp",
 	"XFMemory.cpp",
 	"XFBConvert.cpp",
+	"PixelShader.cpp",
+	"VertexShader.cpp",
+	"Statistics.cpp",
 	"Fifo.cpp",
 	"VideoState.cpp",
 	"Profiler.cpp",
diff --git a/Source/Core/VideoCommon/Src/Statistics.cpp b/Source/Core/VideoCommon/Src/Statistics.cpp
new file mode 100644
index 0000000000..df0dbec877
--- /dev/null
+++ b/Source/Core/VideoCommon/Src/Statistics.cpp
@@ -0,0 +1,43 @@
+// Copyright (C) 2003-2008 Dolphin Project.
+
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, version 2.0.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License 2.0 for more details.
+
+// A copy of the GPL 2.0 should have been included with the program.
+// If not, see http://www.gnu.org/licenses/
+
+// Official SVN repository and contact information can be found at
+// http://code.google.com/p/dolphin-emu/
+
+#include <string.h>
+
+#include "Statistics.h"
+
+Statistics stats;
+
+template <class T>
+void Xchg(T& a, T&b)
+{
+	T c = a;
+	a = b;
+	b = c;
+}
+
+void Statistics::ResetFrame()
+{
+	memset(&thisFrame, 0, sizeof(ThisFrame));
+}
+
+void Statistics::SwapDL()
+{
+	Xchg(stats.thisFrame.numDLPrims, stats.thisFrame.numPrims);
+	Xchg(stats.thisFrame.numXFLoadsInDL, stats.thisFrame.numXFLoads);
+	Xchg(stats.thisFrame.numCPLoadsInDL, stats.thisFrame.numCPLoads);
+	Xchg(stats.thisFrame.numBPLoadsInDL, stats.thisFrame.numBPLoads);
+}
diff --git a/Source/Plugins/Plugin_VideoDX9/Src/Globals.h b/Source/Core/VideoCommon/Src/Statistics.h
similarity index 62%
rename from Source/Plugins/Plugin_VideoDX9/Src/Globals.h
rename to Source/Core/VideoCommon/Src/Statistics.h
index 94b82b063d..72e1cb3e82 100644
--- a/Source/Plugins/Plugin_VideoDX9/Src/Globals.h
+++ b/Source/Core/VideoCommon/Src/Statistics.h
@@ -15,56 +15,8 @@
 // Official SVN repository and contact information can be found at
 // http://code.google.com/p/dolphin-emu/
 
-#ifndef _GLOBALS_H
-#define _GLOBALS_H
-
-#include <string>
-
-struct Config 
-{
-	Config();
-	void Load();
-	void Save();
-
-	int iAdapter;
-	int iFSResolution;
-	int iMultisampleMode;
-
-	int iPostprocessEffect;
-	int iCompileDLsLevel;
-
-	bool renderToMainframe;
-	bool bFullscreen;
-	bool bVsync;
-	bool bWireFrame;
-	bool bOverlayStats;
-	bool bDumpTextures;
-	bool bOldCard;
-	bool bShowShaderErrors;
-	//enhancements
-	bool bForceFiltering;
-	bool bForceMaxAniso;
-
-	bool bPreUpscale;
-	int iPreUpscaleFilter;
-
-	bool bTruform;
-	int iTruformLevel;
-
-	int iWindowedRes;
-
-	char psProfile[16];
-	char vsProfile[16];
-
-	bool bTexFmtOverlayEnable;
-	bool bTexFmtOverlayCenter;
-
-	std::string texDumpPath;
-};
-
-
-extern Config g_Config;
-
+#ifndef _STATISTICS_H
+#define _STATISTICS_H
 
 struct Statistics
 {
@@ -101,12 +53,12 @@ struct Statistics
         int numDLPrims;
         int numPrims;
         int numShaderChanges;
-        int numBadCommands; //hope this always is zero ;)
 
 		int numDListsCalled;
     };
     ThisFrame thisFrame;
-	void ResetFrame() {memset(&thisFrame,0,sizeof(ThisFrame));}
+    void ResetFrame();
+	static void SwapDL();
 };
 
 extern Statistics stats;
@@ -116,11 +68,11 @@ extern Statistics stats;
 #ifdef STATISTICS
 #define INCSTAT(a) (a)++;
 #define ADDSTAT(a,b) (a)+=(b);
-#define SETSTAT(a,x) (a)=(x);
+#define SETSTAT(a,x) (a)=(int)(x);
 #else
 #define INCSTAT(a) ;
 #define ADDSTAT(a,b) ;
 #define SETSTAT(a,x) ;
 #endif
 
-#endif
\ No newline at end of file
+#endif  // _STATISTICS_H
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShader.cpp b/Source/Core/VideoCommon/Src/VertexShader.cpp
similarity index 97%
rename from Source/Plugins/Plugin_VideoOGL/Src/VertexShader.cpp
rename to Source/Core/VideoCommon/Src/VertexShader.cpp
index f7caa2ae9e..556aefb153 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShader.cpp
+++ b/Source/Core/VideoCommon/Src/VertexShader.cpp
@@ -15,16 +15,12 @@
 // Official SVN repository and contact information can be found at
 // http://code.google.com/p/dolphin-emu/
 
-#include "Globals.h"
 #include "Profiler.h"
 #include <math.h>
 
-#include "BPStructs.h"
+#include "BPMemory.h"
 #include "VertexShader.h"
 
-// This is the tricky one to get rid off.
-// #include "VertexLoader.h"
-
 static char text[16384];
 
 #define WRITE p+=sprintf
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShader.h b/Source/Core/VideoCommon/Src/VertexShader.h
similarity index 93%
rename from Source/Plugins/Plugin_VideoOGL/Src/VertexShader.h
rename to Source/Core/VideoCommon/Src/VertexShader.h
index 551a7fa631..4845241d56 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShader.h
+++ b/Source/Core/VideoCommon/Src/VertexShader.h
@@ -36,7 +36,8 @@ enum {
     VB_HAS_TEXMTXIDX6=(1<<8),
     VB_HAS_TEXMTXIDX7=(1<<9),
     VB_HAS_TEXMTXIDXALL=(0xff<<2),
-    //VB_HAS_POS=0, // Implied, it always has pos! don't bother testing
+
+	//VB_HAS_POS=0, // Implied, it always has pos! don't bother testing
     VB_HAS_NRM0=(1<<10),
     VB_HAS_NRM1=(1<<11),
     VB_HAS_NRM2=(1<<12),
diff --git a/Source/Core/VideoCommon/Src/XFBConvert.h b/Source/Core/VideoCommon/Src/XFBConvert.h
index e67d1d47b0..1756d3af0a 100644
--- a/Source/Core/VideoCommon/Src/XFBConvert.h
+++ b/Source/Core/VideoCommon/Src/XFBConvert.h
@@ -22,9 +22,10 @@
 
 void InitXFBConvTables();
 
+// Converts 4:2:2 YUV (YUYV) data to 32-bit RGBA data.
 void ConvertFromXFB(u32 *dst, const u8* _pXFB, int width, int height);
 
-// converts 32-bit RGBA data to 16-bit 4:2:2 YUV data
+// Converts 32-bit RGBA data to 4:2:2 YUV (YUYV) data.
 void ConvertToXFB(u32 *dst, const u8* _pEFB, int width, int height);
 
 #endif
diff --git a/Source/Core/VideoCommon/VideoCommon.vcproj b/Source/Core/VideoCommon/VideoCommon.vcproj
index ce0a0a536c..e2795fde9b 100644
--- a/Source/Core/VideoCommon/VideoCommon.vcproj
+++ b/Source/Core/VideoCommon/VideoCommon.vcproj
@@ -411,22 +411,54 @@
 	<References>
 	</References>
 	<Files>
-		<File
-			RelativePath=".\Src\BPMemory.cpp"
+		<Filter
+			Name="ShaderGenerators"
 			>
-		</File>
-		<File
-			RelativePath=".\Src\BPMemory.h"
+			<File
+				RelativePath=".\Src\PixelShader.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\Src\PixelShader.h"
+				>
+			</File>
+			<File
+				RelativePath=".\Src\VertexShader.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\Src\VertexShader.h"
+				>
+			</File>
+		</Filter>
+		<Filter
+			Name="Sections"
 			>
-		</File>
-		<File
-			RelativePath=".\Src\CPMemory.cpp"
-			>
-		</File>
-		<File
-			RelativePath=".\Src\CPMemory.h"
-			>
-		</File>
+			<File
+				RelativePath=".\Src\BPMemory.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\Src\BPMemory.h"
+				>
+			</File>
+			<File
+				RelativePath=".\Src\CPMemory.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\Src\CPMemory.h"
+				>
+			</File>
+			<File
+				RelativePath=".\Src\XFMemory.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\Src\XFMemory.h"
+				>
+			</File>
+		</Filter>
 		<File
 			RelativePath=".\Src\DataReader.h"
 			>
@@ -463,6 +495,14 @@
 			RelativePath=".\Src\SConscript"
 			>
 		</File>
+		<File
+			RelativePath=".\Src\Statistics.cpp"
+			>
+		</File>
+		<File
+			RelativePath=".\Src\Statistics.h"
+			>
+		</File>
 		<File
 			RelativePath=".\Src\TextureDecoder.cpp"
 			>
@@ -503,14 +543,6 @@
 			RelativePath=".\Src\XFBConvert.h"
 			>
 		</File>
-		<File
-			RelativePath=".\Src\XFMemory.cpp"
-			>
-		</File>
-		<File
-			RelativePath=".\Src\XFMemory.h"
-			>
-		</File>
 	</Files>
 	<Globals>
 	</Globals>
diff --git a/Source/Plugins/Plugin_VideoDX9/Plugin_VideoDX9.vcproj b/Source/Plugins/Plugin_VideoDX9/Plugin_VideoDX9.vcproj
index 497727f6f2..f63de1d494 100644
--- a/Source/Plugins/Plugin_VideoDX9/Plugin_VideoDX9.vcproj
+++ b/Source/Plugins/Plugin_VideoDX9/Plugin_VideoDX9.vcproj
@@ -1375,11 +1375,11 @@
 			</File>
 		</Filter>
 		<File
-			RelativePath=".\Src\Globals.cpp"
+			RelativePath=".\Src\Config.cpp"
 			>
 		</File>
 		<File
-			RelativePath=".\Src\Globals.h"
+			RelativePath=".\Src\Config.h"
 			>
 		</File>
 		<File
diff --git a/Source/Plugins/Plugin_VideoDX9/Src/BPStructs.cpp b/Source/Plugins/Plugin_VideoDX9/Src/BPStructs.cpp
index 30e1a4731b..b7090deae7 100644
--- a/Source/Plugins/Plugin_VideoDX9/Src/BPStructs.cpp
+++ b/Source/Plugins/Plugin_VideoDX9/Src/BPStructs.cpp
@@ -17,7 +17,7 @@
 
 #include "D3DBase.h"
 
-#include "Globals.h"
+#include "Config.h"
 #include "Common.h"
 #include "Profiler.h"
 #include "BPStructs.h"
diff --git a/Source/Plugins/Plugin_VideoDX9/Src/Globals.cpp b/Source/Plugins/Plugin_VideoDX9/Src/Config.cpp
similarity index 96%
rename from Source/Plugins/Plugin_VideoDX9/Src/Globals.cpp
rename to Source/Plugins/Plugin_VideoDX9/Src/Config.cpp
index 7bcfa47d92..1228ab5e89 100644
--- a/Source/Plugins/Plugin_VideoDX9/Src/Globals.cpp
+++ b/Source/Plugins/Plugin_VideoDX9/Src/Config.cpp
@@ -15,7 +15,7 @@
 // Official SVN repository and contact information can be found at
 // http://code.google.com/p/dolphin-emu/
 
-#include "Globals.h"
+#include "Config.h"
 #include "IniFile.h"
 
 Config g_Config;
@@ -80,5 +80,3 @@ void Config::Save()
 	iniFile.Set("Enhancements", "ForceMaxAniso", bForceMaxAniso);
 	iniFile.Save("gfx_dx9.ini");
 }
-
-Statistics stats;
\ No newline at end of file
diff --git a/Source/Plugins/Plugin_VideoDX9/Src/Config.h b/Source/Plugins/Plugin_VideoDX9/Src/Config.h
new file mode 100644
index 0000000000..5057cc1c4b
--- /dev/null
+++ b/Source/Plugins/Plugin_VideoDX9/Src/Config.h
@@ -0,0 +1,67 @@
+// Copyright (C) 2003-2008 Dolphin Project.
+
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, version 2.0.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License 2.0 for more details.
+
+// A copy of the GPL 2.0 should have been included with the program.
+// If not, see http://www.gnu.org/licenses/
+
+// Official SVN repository and contact information can be found at
+// http://code.google.com/p/dolphin-emu/
+
+#ifndef _GLOBALS_H
+#define _GLOBALS_H
+
+#include <string>
+
+struct Config 
+{
+	Config();
+	void Load();
+	void Save();
+
+	int iAdapter;
+	int iFSResolution;
+	int iMultisampleMode;
+
+	int iPostprocessEffect;
+	int iCompileDLsLevel;
+
+	bool renderToMainframe;
+	bool bFullscreen;
+	bool bVsync;
+	bool bWireFrame;
+	bool bOverlayStats;
+	bool bDumpTextures;
+	bool bOldCard;
+	bool bShowShaderErrors;
+	//enhancements
+	bool bForceFiltering;
+	bool bForceMaxAniso;
+
+	bool bPreUpscale;
+	int iPreUpscaleFilter;
+
+	bool bTruform;
+	int iTruformLevel;
+
+	int iWindowedRes;
+
+	char psProfile[16];
+	char vsProfile[16];
+
+	bool bTexFmtOverlayEnable;
+	bool bTexFmtOverlayCenter;
+
+	std::string texDumpPath;
+};
+
+extern Config g_Config;
+
+#endif
\ No newline at end of file
diff --git a/Source/Plugins/Plugin_VideoDX9/Src/D3DPostprocess.cpp b/Source/Plugins/Plugin_VideoDX9/Src/D3DPostprocess.cpp
index 8ccd3fa710..bda69bbeba 100644
--- a/Source/Plugins/Plugin_VideoDX9/Src/D3DPostprocess.cpp
+++ b/Source/Plugins/Plugin_VideoDX9/Src/D3DPostprocess.cpp
@@ -21,7 +21,7 @@
 #include "D3DTexture.h"
 #include "D3DUtil.h"
 
-#include "Globals.h"
+#include "Config.h"
 
 #include "Render.h"
 
diff --git a/Source/Plugins/Plugin_VideoDX9/Src/D3DShader.cpp b/Source/Plugins/Plugin_VideoDX9/Src/D3DShader.cpp
index 14b8000ceb..172f02dac2 100644
--- a/Source/Plugins/Plugin_VideoDX9/Src/D3DShader.cpp
+++ b/Source/Plugins/Plugin_VideoDX9/Src/D3DShader.cpp
@@ -18,7 +18,7 @@
 #include <d3dx9.h>
 #include <string>
 
-#include "Globals.h"
+#include "Config.h"
 #include "D3DShader.h"
 
 
diff --git a/Source/Plugins/Plugin_VideoDX9/Src/DlgSettings.cpp b/Source/Plugins/Plugin_VideoDX9/Src/DlgSettings.cpp
index 4f555630db..bc78409cbf 100644
--- a/Source/Plugins/Plugin_VideoDX9/Src/DlgSettings.cpp
+++ b/Source/Plugins/Plugin_VideoDX9/Src/DlgSettings.cpp
@@ -24,7 +24,7 @@
 #include "D3DBase.h"
 #include "D3DPostprocess.h"
 
-#include "Globals.h"
+#include "Config.h"
 
 #include "TextureCache.h"
 
diff --git a/Source/Plugins/Plugin_VideoDX9/Src/EmuWindow.cpp b/Source/Plugins/Plugin_VideoDX9/Src/EmuWindow.cpp
index eb81239957..49cfc08df1 100644
--- a/Source/Plugins/Plugin_VideoDX9/Src/EmuWindow.cpp
+++ b/Source/Plugins/Plugin_VideoDX9/Src/EmuWindow.cpp
@@ -2,7 +2,7 @@
 #include <windows.h>
 
 #include "../../Core/Src/Core.h"
-#include "Globals.h"
+#include "Config.h"
 #include "main.h"
 #include "EmuWindow.h"
 
diff --git a/Source/Plugins/Plugin_VideoDX9/Src/OpcodeDecoding.cpp b/Source/Plugins/Plugin_VideoDX9/Src/OpcodeDecoding.cpp
index 00397e7b37..d2beb01396 100644
--- a/Source/Plugins/Plugin_VideoDX9/Src/OpcodeDecoding.cpp
+++ b/Source/Plugins/Plugin_VideoDX9/Src/OpcodeDecoding.cpp
@@ -28,8 +28,8 @@
 #include "D3DBase.h"
 
 #include "Common.h"
+#include "Statistics.h"
 #include "Profiler.h"
-#include "Globals.h"
 #include "VertexHandler.h"
 #include "TransformEngine.h"
 #include "OpcodeDecoding.h"
@@ -72,10 +72,7 @@ void ExecuteDisplayList(u32 address, u32 size)
 	g_pVideoData = startAddress;
 
 	// temporarily swap dl and non-dl(small "hack" for the stats)
-	Xchg(stats.thisFrame.numDLPrims, stats.thisFrame.numPrims);
-	Xchg(stats.thisFrame.numXFLoadsInDL, stats.thisFrame.numXFLoads);
-	Xchg(stats.thisFrame.numCPLoadsInDL, stats.thisFrame.numCPLoads);
-	Xchg(stats.thisFrame.numBPLoadsInDL, stats.thisFrame.numBPLoads);
+	Statistics::SwapDL();
 
     while((u32)(g_pVideoData - startAddress) < size)
     {
@@ -85,10 +82,7 @@ void ExecuteDisplayList(u32 address, u32 size)
     INCSTAT(stats.thisFrame.numDListsCalled);
 
 	// un-swap
-	Xchg(stats.thisFrame.numDLPrims, stats.thisFrame.numPrims);
-	Xchg(stats.thisFrame.numXFLoadsInDL, stats.thisFrame.numXFLoads);
-	Xchg(stats.thisFrame.numCPLoadsInDL, stats.thisFrame.numCPLoads);
-	Xchg(stats.thisFrame.numBPLoadsInDL, stats.thisFrame.numBPLoads);
+	Statistics::SwapDL();
 
     // reset to the old pointer
 	g_pVideoData = old_pVideoData;
diff --git a/Source/Plugins/Plugin_VideoDX9/Src/PixelShader.cpp b/Source/Plugins/Plugin_VideoDX9/Src/PixelShader.cpp
index e2dc4ccf6a..a6787a4f88 100644
--- a/Source/Plugins/Plugin_VideoDX9/Src/PixelShader.cpp
+++ b/Source/Plugins/Plugin_VideoDX9/Src/PixelShader.cpp
@@ -15,7 +15,6 @@
 // Official SVN repository and contact information can be found at
 // http://code.google.com/p/dolphin-emu/
 
-#include "Globals.h"
 #include "PixelShader.h"
 #include "BPStructs.h"
 #include "XFStructs.h"
diff --git a/Source/Plugins/Plugin_VideoDX9/Src/Render.cpp b/Source/Plugins/Plugin_VideoDX9/Src/Render.cpp
index ebc37b69e1..d81252bb95 100644
--- a/Source/Plugins/Plugin_VideoDX9/Src/Render.cpp
+++ b/Source/Plugins/Plugin_VideoDX9/Src/Render.cpp
@@ -18,8 +18,9 @@
 #include <d3dx9.h>
 
 #include "Common.h"
+#include "Statistics.h"
 
-#include "Globals.h"
+#include "Config.h"
 #include "main.h"
 #include "VertexHandler.h"
 #include "Render.h"
@@ -209,7 +210,6 @@ void Renderer::SwapBuffers(void)
 		p+=sprintf(p,"Num strip joins: %i\n",stats.numJoins);
 		p+=sprintf(p,"Num primitives: %i\n",stats.thisFrame.numPrims);
 		p+=sprintf(p,"Num primitives (DL): %i\n",stats.thisFrame.numDLPrims);
-		p+=sprintf(p,"Num bad commands: %i%s\n",stats.thisFrame.numBadCommands,stats.thisFrame.numBadCommands?"!!!":"");
 		p+=sprintf(p,"Num XF loads: %i\n",stats.thisFrame.numXFLoads);
 		p+=sprintf(p,"Num XF loads (DL): %i\n",stats.thisFrame.numXFLoadsInDL);
 		p+=sprintf(p,"Num CP loads: %i\n",stats.thisFrame.numCPLoads);
diff --git a/Source/Plugins/Plugin_VideoDX9/Src/ShaderManager.cpp b/Source/Plugins/Plugin_VideoDX9/Src/ShaderManager.cpp
index 6db6e0801d..5320fbaa21 100644
--- a/Source/Plugins/Plugin_VideoDX9/Src/ShaderManager.cpp
+++ b/Source/Plugins/Plugin_VideoDX9/Src/ShaderManager.cpp
@@ -16,9 +16,9 @@
 // http://code.google.com/p/dolphin-emu/
 
 #include "D3DBase.h"
+#include "Statistics.h"
 #include "Utils.h"
 #include "Profiler.h"
-#include "Globals.h"
 #include "ShaderManager.h"
 #include "VertexLoader.h"
 #include "BPMemory.h"
diff --git a/Source/Plugins/Plugin_VideoDX9/Src/TextureCache.cpp b/Source/Plugins/Plugin_VideoDX9/Src/TextureCache.cpp
index d21b7e0a8f..4cd62306bb 100644
--- a/Source/Plugins/Plugin_VideoDX9/Src/TextureCache.cpp
+++ b/Source/Plugins/Plugin_VideoDX9/Src/TextureCache.cpp
@@ -18,6 +18,7 @@
 #include <d3dx9.h>
 
 #include "Common.h"
+#include "Statistics.h"
 
 #include "D3DBase.h"
 #include "D3DTexture.h"
@@ -27,7 +28,7 @@
 #include "TextureDecoder.h"
 #include "TextureCache.h"
 
-#include "Globals.h"
+#include "Config.h"
 #include "main.h"
 
 u8 *TextureCache::temp = NULL;
diff --git a/Source/Plugins/Plugin_VideoDX9/Src/TransformEngine.cpp b/Source/Plugins/Plugin_VideoDX9/Src/TransformEngine.cpp
index 74fabafae0..fc1ba1dd2f 100644
--- a/Source/Plugins/Plugin_VideoDX9/Src/TransformEngine.cpp
+++ b/Source/Plugins/Plugin_VideoDX9/Src/TransformEngine.cpp
@@ -19,7 +19,7 @@
 
 #include "Common.h"
 #include "Profiler.h"
-#include "Globals.h"
+// #include "Globals.h"
 #include "Vec3.h"
 #include "TransformEngine.h"
 #include "VertexHandler.h"
diff --git a/Source/Plugins/Plugin_VideoDX9/Src/VertexHandler.cpp b/Source/Plugins/Plugin_VideoDX9/Src/VertexHandler.cpp
index 96a742e86c..f3d290a629 100644
--- a/Source/Plugins/Plugin_VideoDX9/Src/VertexHandler.cpp
+++ b/Source/Plugins/Plugin_VideoDX9/Src/VertexHandler.cpp
@@ -15,11 +15,12 @@
 // Official SVN repository and contact information can be found at
 // http://code.google.com/p/dolphin-emu/
 
+#include "Common.h"
+
 #include "D3DBase.h"
 
-#include "Common.h"
+#include "Statistics.h"
 #include "Profiler.h"
-#include "Globals.h"
 #include "VertexHandler.h"
 #include "OpcodeDecoding.h"
 #include "TransformEngine.h"
diff --git a/Source/Plugins/Plugin_VideoDX9/Src/main.cpp b/Source/Plugins/Plugin_VideoDX9/Src/main.cpp
index 777d3d8c67..1710cc0884 100644
--- a/Source/Plugins/Plugin_VideoDX9/Src/main.cpp
+++ b/Source/Plugins/Plugin_VideoDX9/Src/main.cpp
@@ -24,7 +24,7 @@
 #include "svnrev.h"
 #include "resource.h"
 #include "main.h"
-#include "Globals.h"
+#include "Config.h"
 #include "Fifo.h"
 #include "OpcodeDecoding.h"
 #include "TextureCache.h"
diff --git a/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcproj b/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcproj
index 719465d0df..fc713d7140 100644
--- a/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcproj
+++ b/Source/Plugins/Plugin_VideoOGL/Plugin_VideoOGL.vcproj
@@ -772,14 +772,6 @@
 		<Filter
 			Name="Render"
 			>
-			<File
-				RelativePath=".\Src\PixelShader.cpp"
-				>
-			</File>
-			<File
-				RelativePath=".\Src\PixelShader.h"
-				>
-			</File>
 			<File
 				RelativePath=".\Src\PixelShaderManager.cpp"
 				>
@@ -828,14 +820,6 @@
 				RelativePath=".\Src\TextureMngr.h"
 				>
 			</File>
-			<File
-				RelativePath=".\Src\VertexShader.cpp"
-				>
-			</File>
-			<File
-				RelativePath=".\Src\VertexShader.h"
-				>
-			</File>
 			<File
 				RelativePath=".\Src\VertexShaderManager.cpp"
 				>
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/GLInit.h b/Source/Plugins/Plugin_VideoOGL/Src/GLInit.h
index a598070ce1..0821cdc585 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/GLInit.h
+++ b/Source/Plugins/Plugin_VideoOGL/Src/GLInit.h
@@ -60,6 +60,4 @@ void OpenGL_SetWindowText(const char *text);
 void OpenGL_Shutdown();
 void OpenGL_Update();
 
-
-
 #endif
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Globals.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Globals.cpp
index 50089b1a0b..80c6314eb3 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/Globals.cpp
+++ b/Source/Plugins/Plugin_VideoOGL/Src/Globals.cpp
@@ -31,13 +31,6 @@
 
 Config g_Config;
 
-Statistics stats;
-
-void Statistics::ResetFrame()
-{
-	memset(&thisFrame, 0, sizeof(ThisFrame));
-}
-
 Config::Config()
 {
     memset(this, 0, sizeof(Config));
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Globals.h b/Source/Plugins/Plugin_VideoOGL/Src/Globals.h
index 851bf1b3f0..cf32b3d4c2 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/Globals.h
+++ b/Source/Plugins/Plugin_VideoOGL/Src/Globals.h
@@ -140,62 +140,6 @@ struct Config
 
 extern Config g_Config;
 
-struct Statistics
-{
-    int numPrimitives;
-
-    int numPixelShadersCreated;
-    int numPixelShadersAlive;
-    int numVertexShadersCreated;
-    int numVertexShadersAlive;
-
-    int numTexturesCreated;
-    int numTexturesAlive;
-
-    int numRenderTargetsCreated;
-    int numRenderTargetsAlive;
-    
-    int numDListsCalled;
-    int numDListsCreated;
-    int numDListsAlive;
-
-    int numJoins;
-
-    struct ThisFrame
-    {
-        int numBPLoads;
-        int numCPLoads;
-        int numXFLoads;
-        
-        int numBPLoadsInDL;
-        int numCPLoadsInDL;
-        int numXFLoadsInDL;
-        
-        int numDLs;
-        int numDLPrims;
-        int numPrims;
-        int numShaderChanges;
-
-		int numDListsCalled;
-    };
-    ThisFrame thisFrame;
-    void ResetFrame();
-};
-
-extern Statistics stats;
-
-#define STATISTICS
-
-#ifdef STATISTICS
-#define INCSTAT(a) (a)++;
-#define ADDSTAT(a,b) (a)+=(b);
-#define SETSTAT(a,x) (a)=(int)(x);
-#else
-#define INCSTAT(a) ;
-#define ADDSTAT(a,b) ;
-#define SETSTAT(a,x) ;
-#endif
-
 void DebugLog(const char* _fmt, ...);
 void __Log(const char *format, ...);
 void __Log(int type, const char *format, ...);
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/OpcodeDecoding.cpp b/Source/Plugins/Plugin_VideoOGL/Src/OpcodeDecoding.cpp
index bcc6bd14fa..694f207ff7 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/OpcodeDecoding.cpp
+++ b/Source/Plugins/Plugin_VideoOGL/Src/OpcodeDecoding.cpp
@@ -30,6 +30,7 @@
 #include "VertexLoader.h"
 #include "VertexManager.h"
 #include "VertexShaderManager.h"
+#include "Statistics.h"
 
 #include "BPStructs.h"
 #include "Fifo.h"
@@ -44,14 +45,6 @@ extern u8* FAKE_GetFifoEndPtr();
 
 void Decode();
 
-template <class T>
-void Xchg(T& a, T&b)
-{
-	T c = a;
-	a = b;
-	b = c;
-}
-
 void ExecuteDisplayList(u32 address, u32 size)
 {
 	u8* old_pVideoData = g_pVideoData;
@@ -59,13 +52,10 @@ void ExecuteDisplayList(u32 address, u32 size)
 	u8* startAddress = Memory_GetPtr(address);
 	g_pVideoData = startAddress;
 
-	// temporarily swap dl and non-dl(small "hack" for the stats)
-	Xchg(stats.thisFrame.numDLPrims, stats.thisFrame.numPrims);
-	Xchg(stats.thisFrame.numXFLoadsInDL, stats.thisFrame.numXFLoads);
-	Xchg(stats.thisFrame.numCPLoadsInDL, stats.thisFrame.numCPLoads);
-	Xchg(stats.thisFrame.numBPLoadsInDL, stats.thisFrame.numBPLoads);
-
-    while((u32)(g_pVideoData - startAddress) < size)
+	// temporarily swap dl and non-dl (small "hack" for the stats)
+	Statistics::SwapDL();
+	
+	while((u32)(g_pVideoData - startAddress) < size)
     {
         Decode();
     }
@@ -73,10 +63,7 @@ void ExecuteDisplayList(u32 address, u32 size)
     INCSTAT(stats.thisFrame.numDListsCalled);
 
 	// un-swap
-	Xchg(stats.thisFrame.numDLPrims, stats.thisFrame.numPrims);
-	Xchg(stats.thisFrame.numXFLoadsInDL, stats.thisFrame.numXFLoads);
-	Xchg(stats.thisFrame.numCPLoadsInDL, stats.thisFrame.numCPLoads);
-	Xchg(stats.thisFrame.numBPLoadsInDL, stats.thisFrame.numBPLoads);
+	Statistics::SwapDL();
 
     // reset to the old pointer
 	g_pVideoData = old_pVideoData;
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderManager.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderManager.cpp
index 823cf01479..b608f40318 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderManager.cpp
+++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderManager.cpp
@@ -23,6 +23,7 @@
 
 #include <cmath>
 
+#include "Statistics.h"
 #include "ImageWrite.h"
 #include "Common.h"
 #include "Render.h"
@@ -32,7 +33,7 @@
 
 PixelShaderMngr::PSCache PixelShaderMngr::pshaders;
 FRAGMENTSHADER* PixelShaderMngr::pShaderLast = NULL;
-PixelShaderMngr::PIXELSHADERUID PixelShaderMngr::s_curuid;
+PIXELSHADERUID PixelShaderMngr::s_curuid;
 
 static int s_nMaxPixelInstructions;
 static int s_nColorsChanged[2]; // 0 - regular colors, 1 - k colors
@@ -517,7 +518,7 @@ GLuint PixelShaderMngr::GetColorMatrixProgram()
 
 // Mash together all the inputs that contribute to the code of a generated pixel shader into
 // a unique identifier, basically containing all the bits. Yup, it's a lot ....
-void PixelShaderMngr::GetPixelShaderId(PixelShaderMngr::PIXELSHADERUID& uid)
+void PixelShaderMngr::GetPixelShaderId(PIXELSHADERUID &uid)
 {
     u32 projtexcoords = 0;
     for (u32 i = 0; i < (u32)bpmem.genMode.numtevstages + 1; i++) {
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderManager.h b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderManager.h
index b9627f215e..f041130154 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderManager.h
+++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShaderManager.h
@@ -18,12 +18,10 @@
 #ifndef _PIXELSHADERMANAGER_H
 #define _PIXELSHADERMANAGER_H
 
-#include "PixelShader.h"
+#include <map>
 
 #include "BPMemory.h"
 
-#include <map>
-
 struct FRAGMENTSHADER
 {
     FRAGMENTSHADER() : glprogid(0) { }
@@ -33,65 +31,59 @@ struct FRAGMENTSHADER
 #endif
 };
 
+class PIXELSHADERUID
+{
+public:
+	u32 values[4+32+6+11];
+	u16 tevstages, indstages;
+
+	PIXELSHADERUID() {
+		memset(values, 0, (4+32+6+11) * 4);
+		tevstages = indstages = 0;
+	}
+	PIXELSHADERUID(const PIXELSHADERUID& r)
+	{
+		tevstages = r.tevstages;
+		indstages = r.indstages;
+		int N = tevstages + indstages + 3;
+		_assert_(N <= 4+32+6+11);
+		for (int i = 0; i < N; ++i) 
+			values[i] = r.values[i];
+	}
+	int GetNumValues() const {
+		return tevstages + indstages + 3; // numTevStages*3/2+1
+	}
+	bool operator <(const PIXELSHADERUID& _Right) const
+	{
+		if (values[0] < _Right.values[0])
+			return true;
+		else if (values[0] > _Right.values[0])
+			return false;
+		int N = GetNumValues();
+		for (int i = 1; i < N; ++i) {
+			if (values[i] < _Right.values[i])
+				return true;
+			else if (values[i] > _Right.values[i])
+				return false;
+		}
+		return false;
+	}
+	bool operator ==(const PIXELSHADERUID& _Right) const
+	{
+		if (values[0] != _Right.values[0])
+			return false;
+		int N = GetNumValues();
+		for (int i = 1; i < N; ++i) {
+			if (values[i] != _Right.values[i])
+				return false;
+		}
+		return true;
+	}
+};
+
+
 class PixelShaderMngr
 {
-    class PIXELSHADERUID
-    {
-    public:
-        PIXELSHADERUID() {
-			values = new u32[4+32+6+11];
-			memset(values, 0, (4+32+6+11) * 4);
-            tevstages = indstages = 0;
-		}
-        ~PIXELSHADERUID() { delete[] values; values = NULL;}
-        PIXELSHADERUID(const PIXELSHADERUID& r)
-        {
-            values = new u32[4+32+6+11];
-            tevstages = r.tevstages; indstages = r.indstages;
-            int N = tevstages + indstages + 3;
-            _assert_(N <= 4+32+6+11);
-            for(int i = 0; i < N; ++i) 
-				values[i] = r.values[i];
-        }
-
-        bool operator <(const PIXELSHADERUID& _Right) const
-        {
-            if( values[0] < _Right.values[0] )
-                return true;
-            else if( values[0] > _Right.values[0] )
-                return false;
-
-            int N = tevstages + indstages + 3; // numTevStages*3/2+1
-            int i = 1;
-            for(; i < N; ++i) {
-                if( values[i] < _Right.values[i] )
-                    return true;
-                else if( values[i] > _Right.values[i] )
-                    return false;
-            }
-
-            return false;
-        }
-
-        bool operator ==(const PIXELSHADERUID& _Right) const
-        {
-            if( values[0] != _Right.values[0] )
-                return false;
-
-            int N = tevstages + indstages + 3; // numTevStages*3/2+1
-            int i = 1;
-            for(; i < N; ++i) {
-                if( values[i] != _Right.values[i] )
-                    return false;
-            }
-
-            return true;
-        }
-
-        u32* values;
-        u16 tevstages, indstages;
-    };
-
     struct PSCacheEntry
     {
         FRAGMENTSHADER shader;
@@ -105,7 +97,7 @@ class PixelShaderMngr
         }
     };
 
-    typedef std::map<PIXELSHADERUID,PSCacheEntry> PSCache;
+    typedef std::map<PIXELSHADERUID, PSCacheEntry> PSCache;
 
     static FRAGMENTSHADER* pShaderLast; // last used shader
     static PSCache pshaders;
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp
index 9065abcd29..1477d967ad 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp
+++ b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp
@@ -29,6 +29,7 @@
 
 #include "GLInit.h"
 #include "Profiler.h"
+#include "Statistics.h"
 #include "ImageWrite.h"
 #include "Render.h"
 #include "OpcodeDecoding.h"
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/SConscript b/Source/Plugins/Plugin_VideoOGL/Src/SConscript
index 69f15dec10..4128b3d136 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/SConscript
+++ b/Source/Plugins/Plugin_VideoOGL/Src/SConscript
@@ -13,7 +13,6 @@ files = [
 	'main.cpp',
 	'memcpy_amd.cpp',
 	'OpcodeDecoding.cpp',
-	'PixelShader.cpp',
 	'PixelShaderManager.cpp',
 	'rasterfont.cpp',
 	'Render.cpp',
@@ -22,7 +21,6 @@ files = [
 	'VertexManager.cpp',
 	'VertexLoader.cpp',
 	'VertexLoader_Normal.cpp',
-	'VertexShader.cpp',
 	'VertexShaderManager.cpp',
 	'XFB.cpp',
 	'GUI/ConfigDlg.cpp',
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.cpp
index e9e9c11a68..0363fc6f50 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.cpp
+++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.cpp
@@ -29,6 +29,7 @@
 #undef _interlockedbittestandreset64
 #endif
 
+#include "Statistics.h"
 #include "Profiler.h"
 #include "ImageWrite.h"
 
@@ -51,8 +52,15 @@ static u32 s_TempFramebuffer = 0;
 #define TEMP_SIZE (1024*1024*4)
 
 const GLint c_MinLinearFilter[8] = {
-    GL_NEAREST, GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST,
-    GL_LINEAR, GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR};
+	GL_NEAREST,
+	GL_NEAREST_MIPMAP_NEAREST,
+	GL_NEAREST_MIPMAP_LINEAR,
+	GL_NEAREST,
+	GL_LINEAR,
+	GL_LINEAR_MIPMAP_NEAREST,
+	GL_LINEAR_MIPMAP_LINEAR,
+	GL_LINEAR
+};
 
 const GLint c_WrapSettings[4] = { GL_CLAMP_TO_EDGE, GL_REPEAT, GL_MIRRORED_REPEAT, GL_REPEAT };
 
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexLoader.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexLoader.cpp
index eb28e8f107..6d5b9c719d 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/VertexLoader.cpp
+++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexLoader.cpp
@@ -28,6 +28,7 @@
 #include "StringUtil.h"
 
 #include "Render.h"
+#include "VertexShader.h"
 #include "VertexManager.h"
 #include "VertexLoader.h"
 #include "BPStructs.h"
@@ -55,7 +56,6 @@ static int colIndex;
     #define inline
 #endif
 
-TVtxDesc VertexManager::s_GlobalVtxDesc;
 
 // ==============================================================================
 // Direct
@@ -120,7 +120,7 @@ VertexLoader::VertexLoader()
 {
     m_numPipelineStages = 0;
     m_VertexSize = 0;
-    m_AttrDirty = 1;
+    m_AttrDirty = AD_DIRTY;
     VertexLoader_Normal::Init();
 
     m_compiledCode = (u8 *)AllocateExecutableMemory(COMPILED_CODE_SIZE, false);
@@ -136,7 +136,7 @@ VertexLoader::~VertexLoader()
 
 int VertexLoader::ComputeVertexSize()
 {
-    if (!m_AttrDirty) {
+    if (m_AttrDirty == AD_CLEAN) {
 		// Compare the 33 desc bits. 
         if (m_VtxDesc.Hex0 == VertexManager::GetVtxDesc().Hex0 &&
 		    (m_VtxDesc.Hex1 & 1) == (VertexManager::GetVtxDesc().Hex1 & 1))
@@ -152,7 +152,7 @@ int VertexLoader::ComputeVertexSize()
     if (fnSetupVertexPointers != NULL && fnSetupVertexPointers == (void (*)())(void*)m_compiledCode)
         VertexManager::Flush();
 
-    m_AttrDirty = 1;
+    m_AttrDirty = AD_DIRTY;
     m_VertexSize = 0;
     // Position Matrix Index
     if (m_VtxDesc.PosMatIdx)
@@ -257,6 +257,7 @@ int VertexLoader::ComputeVertexSize()
     return m_VertexSize;
 }
 
+
 // Note the use of CallCdeclFunction3I etc.
 // This is a horrible hack that is necessary because in 64-bit mode, Opengl32.dll is based way, way above the 32-bit
 // address space that is within reach of a CALL, and just doing &fn gives us these high uncallable addresses. So we
@@ -269,20 +270,24 @@ DECLARE_IMPORT(glVertexPointer);
 DECLARE_IMPORT(glColorPointer);
 DECLARE_IMPORT(glTexCoordPointer);
 
-void VertexLoader::ProcessFormat()
+void VertexLoader::PrepareForVertexFormat()
 {
     using namespace Gen;
 
     //_assert_( VertexManager::s_pCurBufferPointer == s_pBaseBufferPointer );
 
-    if (!m_AttrDirty)
+    if (m_AttrDirty == AD_CLEAN)
     {
 		// Check if local cached desc (in this VL) matches global desc
         if (m_VtxDesc.Hex0 == VertexManager::GetVtxDesc().Hex0 && (m_VtxDesc.Hex1 & 1)==(VertexManager::GetVtxDesc().Hex1 & 1))
-            return; // same
+		{
+            return;  // same
+		}
     }
-    else 
-        m_AttrDirty = 0;
+    else
+	{
+        m_AttrDirty = AD_CLEAN;
+	}
         
     m_VtxDesc.Hex = VertexManager::GetVtxDesc().Hex;
     DVSTARTPROFILE();
@@ -315,7 +320,7 @@ void VertexLoader::ProcessFormat()
         m_VBVertexStride += 12;
 
     switch (m_VtxDesc.Position) {
-    case NOT_PRESENT:	{_assert_msg_(0,"Vertex descriptor without position!","WTF?");} break;
+    case NOT_PRESENT:	{_assert_msg_(0, "Vertex descriptor without position!", "WTF?");} break;
     case DIRECT:
         {
             switch (m_VtxAttr.PosFormat) {
@@ -409,7 +414,6 @@ void VertexLoader::ProcessFormat()
                 m_VBVertexStride += 6; // still include the texture coordinate, but this time as 6 bytes
                 m_components |= VB_HAS_UV0 << i; // have to include since using now
             }
-            
         }
         else {
             if (tc[i] != NOT_PRESENT)
@@ -425,8 +429,7 @@ void VertexLoader::ProcessFormat()
                     break;
                 }
             }
-
-            if (j == 8 && !((m_components&VB_HAS_TEXMTXIDXALL)&(VB_HAS_TEXMTXIDXALL<<(i+1)))) // no more tex coords and tex matrices, so exit loop
+            if (j == 8 && !((m_components&VB_HAS_TEXMTXIDXALL) & (VB_HAS_TEXMTXIDXALL<<(i+1)))) // no more tex coords and tex matrices, so exit loop
                 break;
         }
     }
@@ -438,18 +441,18 @@ void VertexLoader::ProcessFormat()
 
     if (m_VBVertexStride & 3) {
         // make sure all strides are at least divisible by 4 (some gfx cards experience a 3x speed boost)
-        m_VBStridePad = 4 - (m_VBVertexStride&3);
+        m_VBStridePad = 4 - (m_VBVertexStride & 3);
         m_VBVertexStride += m_VBStridePad;
     }
 
-    // compile the pointer set function
+    // compile the pointer set function - why?
     u8 *old_code_ptr = GetWritableCodePtr();
     SetCodePtr(m_compiledCode);
     Util::EmitPrologue(6);
     int offset = 0;
     
     // Position
-    if (m_VtxDesc.Position != NOT_PRESENT) {
+    if (m_VtxDesc.Position != NOT_PRESENT) {  // TODO: Why the check? Always present, AFAIK!
         CallCdeclFunction4_I(glVertexPointer, 3, GL_FLOAT, m_VBVertexStride, offset);
         offset += 12;
     }
@@ -484,6 +487,8 @@ void VertexLoader::ProcessFormat()
         }
     }
 
+	// TODO : With byte or short normals above, offset will be misaligned (not 4byte aligned)! Ugh!
+
     for (int i = 0; i < 2; i++) {
         if (col[i] != NOT_PRESENT) {
 			if (i)
@@ -496,9 +501,8 @@ void VertexLoader::ProcessFormat()
 
     // TextureCoord
     for (int i = 0; i < 8; i++) {
-        if (tc[i] != NOT_PRESENT || (m_components&(VB_HAS_TEXMTXIDX0<<i))) {
-
-            int id = GL_TEXTURE0+i;
+        if (tc[i] != NOT_PRESENT || (m_components & (VB_HAS_TEXMTXIDX0 << i))) {
+            int id = GL_TEXTURE0 + i;
 #ifdef _M_X64
 #ifdef _MSC_VER
             MOV(32, R(RCX), Imm32(id));
@@ -517,6 +521,7 @@ void VertexLoader::ProcessFormat()
             ABI_RestoreStack(1 * 4);
 #endif
 #endif
+			// TODO : More potential disalignment!
             if (m_components & (VB_HAS_TEXMTXIDX0 << i)) {
                 if (tc[i] != NOT_PRESENT) {
                     CallCdeclFunction4_I(glTexCoordPointer, 3, GL_FLOAT, m_VBVertexStride, offset);
@@ -528,14 +533,14 @@ void VertexLoader::ProcessFormat()
                 }
             }
             else {
-                CallCdeclFunction4_I(glTexCoordPointer, m_VtxAttr.texCoord[i].Elements?2:1, GL_FLOAT, m_VBVertexStride, offset);
+                CallCdeclFunction4_I(glTexCoordPointer, m_VtxAttr.texCoord[i].Elements ? 2 : 1, GL_FLOAT, m_VBVertexStride, offset);
                 offset += 4 * (m_VtxAttr.texCoord[i].Elements?2:1);
             }
         }
     }
 
     if (m_VtxDesc.PosMatIdx) {
-        CallCdeclFunction6((void *)glVertexAttribPointer, SHADER_POSMTX_ATTRIB,1,GL_UNSIGNED_BYTE, GL_FALSE, m_VBVertexStride, offset);
+        CallCdeclFunction6((void *)glVertexAttribPointer, SHADER_POSMTX_ATTRIB, 1, GL_UNSIGNED_BYTE, GL_FALSE, m_VBVertexStride, offset);
         offset += 1;
     }
 
@@ -551,19 +556,6 @@ void VertexLoader::ProcessFormat()
     SetCodePtr(old_code_ptr);
 }
 
-void VertexLoader::PrepareRun()
-{
-    posScale = shiftLookup[m_VtxAttr.PosFrac];
-    if (m_components & VB_HAS_UVALL) {
-        for (int i = 0; i < 8; i++) {
-            tcScaleU[i] = shiftLookup[m_VtxAttr.texCoord[i].Frac];
-            tcScaleV[i] = shiftLookup[m_VtxAttr.texCoord[i].Frac];
-        }
-    }
-    for (int i = 0; i < 2; i++)
-        colElements[i] = m_VtxAttr.color[i].Elements;
-}
-
 void VertexLoader::SetupColor(int num, int mode, int format, int elements)
 {
     // if COL0 not present, then embed COL1 into COL0
@@ -669,38 +661,49 @@ void VertexLoader::RunVertices(int primitive, int count)
 {
     DVSTARTPROFILE();
 
-	ComputeVertexSize();  // HACK for underruns in Super Monkey Ball etc. !!!! dirty handling must be wrong.
-	if (count <= 0)
-        return;
+	// This has dirty handling - won't actually recompute unless necessary.
+	ComputeVertexSize();
 
+	// Figure out a better check. Also, jitting fnSetupVertexPointers seems pretty silly - not likely to be a bottleneck.
     if (fnSetupVertexPointers != NULL && fnSetupVertexPointers != (void (*)())(void*)m_compiledCode)
         VertexManager::Flush();
 
     if (bpmem.genMode.cullmode == 3 && primitive < 5)
 	{
         // if cull mode is none, ignore triangles and quads
-		DataSkip(count*m_VertexSize);
+		DataSkip(count * m_VertexSize);
         return;
     }
 
-    ProcessFormat();
-    fnSetupVertexPointers = (void (*)())(void*)m_compiledCode;
+	// This has dirty handling - won't actually recompute unless necessary.
+    PrepareForVertexFormat();
+
+	fnSetupVertexPointers = (void (*)())(void*)m_compiledCode;
 
 	VertexManager::EnableComponents(m_components);
 
-    PrepareRun();
+    // Load position and texcoord scale factors.
+	// Hm, this could be done when the VtxAttr is set, instead.
+	posScale = shiftLookup[m_VtxAttr.PosFrac];
+    if (m_components & VB_HAS_UVALL) {
+        for (int i = 0; i < 8; i++) {
+            tcScaleU[i] = shiftLookup[m_VtxAttr.texCoord[i].Frac];
+            tcScaleV[i] = shiftLookup[m_VtxAttr.texCoord[i].Frac];
+        }
+    }
+    for (int i = 0; i < 2; i++)
+        colElements[i] = m_VtxAttr.color[i].Elements;
 
     // if strips or fans, make sure all vertices can fit in buffer, otherwise flush
     int granularity = 1;
-
-    switch(primitive) {
+    switch (primitive) {
         case 3: // strip
         case 4: // fan
-            if (VertexManager::GetRemainingSize() < 3*m_VBVertexStride )
+            if (VertexManager::GetRemainingSize() < 3 * m_VBVertexStride )
                 VertexManager::Flush();
             break;
         case 6: // line strip
-            if (VertexManager::GetRemainingSize() < 2*m_VBVertexStride )
+            if (VertexManager::GetRemainingSize() < 2 * m_VBVertexStride )
                 VertexManager::Flush();
             break;
         case 0: // quads
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexLoader.h b/Source/Plugins/Plugin_VideoOGL/Src/VertexLoader.h
index d148d7cf51..80720f33c3 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/VertexLoader.h
+++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexLoader.h
@@ -64,7 +64,11 @@ private:
     void SetupTexCoord(int num, int _iMode, int _iFormat, int _iElements, int _iFrac);
 
 	// The 3 possible values (0, 1, 2) should be documented here.
-    int m_AttrDirty;
+	enum {
+		AD_CLEAN = 0,
+		AD_DIRTY = 1,
+		AD_VAT_DIRTY = 2,
+	} m_AttrDirty;
 
 public:
     // constructor
@@ -72,23 +76,19 @@ public:
     ~VertexLoader();
     
     // run the pipeline 
-    void ProcessFormat();
-    void PrepareRun();
+    void PrepareForVertexFormat();
     void RunVertices(int primitive, int count);
     void WriteCall(void  (LOADERDECL *func)(void *));
     
-    int GetGCVertexSize() const { _assert_( !m_AttrDirty ); return m_VertexSize; }
-    int GetVBVertexStride() const { _assert_( !m_AttrDirty); return m_VBVertexStride; }
+    int GetGCVertexSize()   const { _assert_( !m_AttrDirty ); return m_VertexSize; }
+    int GetVBVertexStride() const { _assert_( !m_AttrDirty);  return m_VBVertexStride; }
 
     int ComputeVertexSize();
 
-     // SetVAT_group
-    // ignore PosFrac, texCoord[i].Frac
-
     void SetVAT_group0(u32 _group0) 
     {
         if ((m_group0.Hex & ~0x3e0001f0) != (_group0 & ~0x3e0001f0)) {
-            m_AttrDirty = 2;
+            m_AttrDirty = AD_VAT_DIRTY;
         }
         m_group0.Hex = _group0;
 
@@ -111,10 +111,9 @@ public:
     void SetVAT_group1(u32 _group1) 
     {
         if ((m_group1.Hex & ~0x7c3e1f0) != (_group1 & ~0x7c3e1f0)) {
-            m_AttrDirty = 2;
+            m_AttrDirty = AD_VAT_DIRTY;
         }
         m_group1.Hex = _group1;
-        
 
         m_VtxAttr.texCoord[1].Elements	= m_group1.Tex1CoordElements;
         m_VtxAttr.texCoord[1].Format	= m_group1.Tex1CoordFormat;
@@ -132,7 +131,7 @@ public:
     void SetVAT_group2(u32 _group2)		  
     {
         if ((m_group2.Hex & ~0xf87c3e1f) != (_group2 & ~0xf87c3e1f)) {
-            m_AttrDirty = 2;
+            m_AttrDirty = AD_VAT_DIRTY;
         }
         m_group2.Hex = _group2;
 
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp
index 9afa9fb6b0..f4931225c6 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp
+++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp
@@ -3,6 +3,7 @@
 #include <fstream>
 #include <vector>
 
+#include "Statistics.h"
 #include "MemoryUtil.h"
 #include "Profiler.h"
 #include "Render.h"
@@ -11,6 +12,7 @@
 #include "TextureMngr.h"
 #include "PixelShaderManager.h"
 #include "VertexShaderManager.h"
+#include "VertexShader.h"
 #include "VertexLoader.h"
 #include "VertexManager.h"
 
@@ -23,6 +25,7 @@ static vector< pair<int, int> > s_vStoredPrimitives; // every element, mode and
 static u32 s_prevcomponents; // previous state set
 
 u8* VertexManager::s_pCurBufferPointer = NULL;
+TVtxDesc VertexManager::s_GlobalVtxDesc;
 
 static const GLenum c_primitiveType[8] =
 {
@@ -294,32 +297,32 @@ void VertexManager::Flush()
 	ResetBuffer();
 }
 
-void VertexManager::LoadCPReg(u32 SubCmd, u32 Value)
+void VertexManager::LoadCPReg(u32 sub_cmd, u32 value)
 {
-	switch (SubCmd & 0xF0)
+	switch (sub_cmd & 0xF0)
 	{
 	case 0x30:
-		VertexShaderMngr::SetTexMatrixChangedA(Value);
+		VertexShaderMngr::SetTexMatrixChangedA(value);
 		break;
 	case 0x40:
-		VertexShaderMngr::SetTexMatrixChangedB(Value);
+		VertexShaderMngr::SetTexMatrixChangedB(value);
 		break;
 
 	case 0x50:
 		s_GlobalVtxDesc.Hex &= ~0x1FFFF; // keep the Upper bits
-		s_GlobalVtxDesc.Hex |= Value;
+		s_GlobalVtxDesc.Hex |= value;
 		break;
 	case 0x60:
 		s_GlobalVtxDesc.Hex &= 0x1FFFF; // keep the lower 17Bits
-		s_GlobalVtxDesc.Hex |= (u64)Value << 17;
+		s_GlobalVtxDesc.Hex |= (u64)value << 17;
 		break;
 
-	case 0x70: g_VertexLoaders[SubCmd & 7].SetVAT_group0(Value); _assert_((SubCmd & 0x0F) < 8); break;
-	case 0x80: g_VertexLoaders[SubCmd & 7].SetVAT_group1(Value); _assert_((SubCmd & 0x0F) < 8); break;
-	case 0x90: g_VertexLoaders[SubCmd & 7].SetVAT_group2(Value); _assert_((SubCmd & 0x0F) < 8); break;
+	case 0x70: g_VertexLoaders[sub_cmd & 7].SetVAT_group0(value); _assert_((sub_cmd & 0x0F) < 8); break;
+	case 0x80: g_VertexLoaders[sub_cmd & 7].SetVAT_group1(value); _assert_((sub_cmd & 0x0F) < 8); break;
+	case 0x90: g_VertexLoaders[sub_cmd & 7].SetVAT_group2(value); _assert_((sub_cmd & 0x0F) < 8); break;
 
-	case 0xA0: arraybases[SubCmd & 0xF]   = Value & 0xFFFFFFFF; break;
-	case 0xB0: arraystrides[SubCmd & 0xF] = Value & 0xFF; break;
+	case 0xA0: arraybases[sub_cmd & 0xF]   = value & 0xFFFFFFFF; break;
+	case 0xB0: arraystrides[sub_cmd & 0xF] = value & 0xFF; break;
 	}
 }
 
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderManager.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderManager.cpp
index 540ad1b5f1..d16c5cf3f6 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderManager.cpp
+++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderManager.cpp
@@ -15,16 +15,15 @@
 // Official SVN repository and contact information can be found at
 // http://code.google.com/p/dolphin-emu/
 
-
 #include "Globals.h"
 #include "Profiler.h"
 
-
 #include <Cg/cg.h>
 #include <Cg/cgGL.h>
 
 #include <math.h>
 
+#include "Statistics.h"
 #include "ImageWrite.h"
 #include "Render.h"
 #include "VertexShader.h"
@@ -366,7 +365,8 @@ void VertexShaderMngr::SetConstants(VERTEXSHADER& vs)
 		int overfl;
 		int xoffs = 0, yoffs = 0;
 		int wid, hei, actualWid, actualHei;
-		int winw = nBackbufferWidth; int winh = nBackbufferHeight;
+		int winw = nBackbufferWidth;
+		int winh = nBackbufferHeight;
 		if (g_Config.bKeepAR)
 		{
 			// Check if height or width is the limiting factor
@@ -405,7 +405,7 @@ void VertexShaderMngr::SetConstants(VERTEXSHADER& vs)
 			hei = ceil(fabs(2 * rawViewport[1]));
 		}
 
-		if(g_Config.bStretchToFit && g_Config.renderToMainframe)
+		if (g_Config.bStretchToFit && g_Config.renderToMainframe)
 		{
 			glViewport(
 				(int)(rawViewport[3]-rawViewport[0]-342-scissorXOff) + xoffs,
@@ -471,7 +471,7 @@ void VertexShaderMngr::SetConstants(VERTEXSHADER& vs)
         }
 
         PRIM_LOG("Projection: %f %f %f %f %f %f\n", rawProjection[0], rawProjection[1], rawProjection[2], rawProjection[3], rawProjection[4], rawProjection[5]);
-        SetVSConstant4fv(C_PROJECTION, &g_fProjectionMatrix[0]);
+        SetVSConstant4fv(C_PROJECTION,   &g_fProjectionMatrix[0]);
         SetVSConstant4fv(C_PROJECTION+1, &g_fProjectionMatrix[4]);
         SetVSConstant4fv(C_PROJECTION+2, &g_fProjectionMatrix[8]);
         SetVSConstant4fv(C_PROJECTION+3, &g_fProjectionMatrix[12]);
@@ -480,8 +480,10 @@ void VertexShaderMngr::SetConstants(VERTEXSHADER& vs)
 
 void VertexShaderMngr::InvalidateXFRange(int start, int end)
 {
-    if( ((u32)start >= (u32)MatrixIndexA.PosNormalMtxIdx*4 && (u32)start < (u32)MatrixIndexA.PosNormalMtxIdx*4+12) ||
-        ((u32)start >= XFMEM_NORMALMATRICES+((u32)MatrixIndexA.PosNormalMtxIdx&31)*3 && (u32)start < XFMEM_NORMALMATRICES+((u32)MatrixIndexA.PosNormalMtxIdx&31)*3+9) ) {
+    if (((u32)start >= (u32)MatrixIndexA.PosNormalMtxIdx*4 &&
+		 (u32)start <  (u32)MatrixIndexA.PosNormalMtxIdx*4 + 12) ||
+        ((u32)start >= XFMEM_NORMALMATRICES + ((u32)MatrixIndexA.PosNormalMtxIdx & 31)*3 &&
+		 (u32)start <  XFMEM_NORMALMATRICES + ((u32)MatrixIndexA.PosNormalMtxIdx & 31)*3 + 9)) {
         bPosNormalMatrixChanged = true;
     }
 
@@ -499,7 +501,7 @@ void VertexShaderMngr::InvalidateXFRange(int start, int end)
         bTexMatricesChanged[1] = true;
     }
 
-    if (start < XFMEM_POSMATRICES_END ) {
+    if (start < XFMEM_POSMATRICES_END) {
         if (nTransformMatricesChanged[0] == -1) {
             nTransformMatricesChanged[0] = start;
             nTransformMatricesChanged[1] = end>XFMEM_POSMATRICES_END?XFMEM_POSMATRICES_END:end;
@@ -510,7 +512,7 @@ void VertexShaderMngr::InvalidateXFRange(int start, int end)
         }
     }
     
-    if (start < XFMEM_NORMALMATRICES_END && end > XFMEM_NORMALMATRICES ) {
+    if (start < XFMEM_NORMALMATRICES_END && end > XFMEM_NORMALMATRICES) {
         int _start = start < XFMEM_NORMALMATRICES ? 0 : start-XFMEM_NORMALMATRICES;
         int _end = end < XFMEM_NORMALMATRICES_END ? end-XFMEM_NORMALMATRICES : XFMEM_NORMALMATRICES_END-XFMEM_NORMALMATRICES;
 
@@ -524,7 +526,7 @@ void VertexShaderMngr::InvalidateXFRange(int start, int end)
         }
     }
 
-    if (start < XFMEM_POSTMATRICES_END && end > XFMEM_POSTMATRICES ) {
+    if (start < XFMEM_POSTMATRICES_END && end > XFMEM_POSTMATRICES) {
         int _start = start < XFMEM_POSTMATRICES ? XFMEM_POSTMATRICES : start-XFMEM_POSTMATRICES;
         int _end = end < XFMEM_POSTMATRICES_END ? end-XFMEM_POSTMATRICES : XFMEM_POSTMATRICES_END-XFMEM_POSTMATRICES;
 
@@ -548,7 +550,7 @@ void VertexShaderMngr::InvalidateXFRange(int start, int end)
         }
         else {
             if (nLightsChanged[0] > _start) nLightsChanged[0] = _start;
-            if (nLightsChanged[1] < _end) nLightsChanged[1] = _end;
+            if (nLightsChanged[1] < _end)   nLightsChanged[1] = _end;
         }
     }
 }
@@ -598,7 +600,6 @@ void VertexShaderMngr::SetProjection(float* _pProjection, int constantIndex)
 // LoadXFReg 0x10
 void VertexShaderMngr::LoadXFReg(u32 transferSize, u32 baseAddress, u32 *pData)
 {	
-
     u32 address = baseAddress;
     for (int i = 0; i < (int)transferSize; i++)
     {
@@ -644,7 +645,7 @@ void VertexShaderMngr::LoadXFReg(u32 transferSize, u32 baseAddress, u32 *pData)
                 xfregs.hostinfo = *(INVTXSPEC*)&data;
                 break;
             case 0x1009: //GXSetNumChans (no)
-              if ((u32)xfregs.nNumChans != (data&3) ) {
+				if ((u32)xfregs.nNumChans != (data&3)) {
                     VertexManager::Flush();
                     xfregs.nNumChans = data&3;
                 }
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderManager.h b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderManager.h
index b3aae823bd..e4d8f66175 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderManager.h
+++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShaderManager.h
@@ -20,9 +20,6 @@
 
 #include <map>
 
-#include "VertexShader.h"
-
-
 struct VERTEXSHADER
 {
     VERTEXSHADER() : glprogid(0) {}
@@ -33,6 +30,55 @@ struct VERTEXSHADER
 #endif
 };
 
+
+class VERTEXSHADERUID
+{
+public:
+	u32 values[9];
+
+	VERTEXSHADERUID() {
+		memset(values, 0, sizeof(values));		
+	}
+
+	VERTEXSHADERUID(const VERTEXSHADERUID& r) {
+		for (size_t i = 0; i < sizeof(values) / sizeof(u32); ++i) 
+			values[i] = r.values[i]; 
+	}
+
+	int GetNumValues() const {
+		return (((values[0] >> 23) & 0xf)*3 + 3)/4 + 3; // numTexGens*3/4+1
+	}
+
+	bool operator <(const VERTEXSHADERUID& _Right) const
+	{
+		if (values[0] < _Right.values[0])
+			return true;
+		else if (values[0] > _Right.values[0])
+			return false;
+		int N = GetNumValues();
+		for (int i = 1; i < N; ++i) {
+			if (values[i] < _Right.values[i])
+				return true;
+			else if (values[i] > _Right.values[i])
+				return false;
+		}
+		return false;
+	}
+
+	bool operator ==(const VERTEXSHADERUID& _Right) const
+	{
+		if (values[0] != _Right.values[0])
+			return false;
+		int N = GetNumValues();
+		for (int i = 1; i < N; ++i) {
+			if (values[i] != _Right.values[i])
+				return false;
+		}
+		return true;
+	}
+};
+
+
 class VertexShaderMngr
 {
     struct VSCacheEntry
@@ -47,53 +93,7 @@ class VertexShaderMngr
         }
     };
 
-    class VERTEXSHADERUID
-    {
-    public:
-        VERTEXSHADERUID() {
-			memset(values, 0, sizeof(values));		
-		}
-        VERTEXSHADERUID(const VERTEXSHADERUID& r) {
-			for(size_t i = 0; i < sizeof(values) / sizeof(u32); ++i) 
-				values[i] = r.values[i]; 
-		}
-
-        bool operator<(const VERTEXSHADERUID& _Right) const
-        {
-            if( values[0] < _Right.values[0] )
-                return true;
-            else if( values[0] > _Right.values[0] )
-                return false;
-
-            int N = (((values[0]>>23)&0xf)*3+3)/4 + 3; // numTexGens*3/4+1
-            for(int i = 1; i < N; ++i) {
-                if( values[i] < _Right.values[i] )
-                    return true;
-                else if( values[i] > _Right.values[i] )
-                    return false;
-            }
-
-            return false;
-        }
-
-        bool operator==(const VERTEXSHADERUID& _Right) const
-        {
-            if( values[0] != _Right.values[0] )
-                return false;
-
-            int N = (((values[0]>>23)&0xf)*3+3)/4 + 3; // numTexGens*3/4+1
-            for(int i = 1; i < N; ++i) {
-                if( values[i] != _Right.values[i] )
-                    return false;
-            }
-
-            return true;
-        }
-
-        u32 values[9];
-    };
-
-    typedef std::map<VERTEXSHADERUID,VSCacheEntry> VSCache;
+    typedef std::map<VERTEXSHADERUID, VSCacheEntry> VSCache;
 
     static VSCache vshaders;
     static VERTEXSHADER* pShaderLast;