Add SwiftShader dump from Feb 6 2013
diff --git a/src/Shader/Constants.cpp b/src/Shader/Constants.cpp
index d111e5e..88a6a81 100644
--- a/src/Shader/Constants.cpp
+++ b/src/Shader/Constants.cpp
@@ -1,6 +1,6 @@
 // SwiftShader Software Renderer
 //
-// Copyright(c) 2005-2011 TransGaming Inc.
+// Copyright(c) 2005-2012 TransGaming Inc.
 //
 // All rights reserved. No part of this software may be copied, distributed, transmitted,
 // transcribed, stored in a retrieval system, translated into any human or computer
@@ -11,13 +11,15 @@
 
 #include "Constants.hpp"
 
-#include "Math.hpp"
-#include "Half.hpp"
+#include "Common/Math.hpp"
+#include "Common/Half.hpp"
 
 #include <memory.h>
 
 namespace sw
 {
+	Constants constants;
+
 	Constants::Constants()
 	{
 		for(int i = 0; i < 256; i++)
diff --git a/src/Shader/Constants.hpp b/src/Shader/Constants.hpp
index d803ed3..a4adc3e 100644
--- a/src/Shader/Constants.hpp
+++ b/src/Shader/Constants.hpp
@@ -1,6 +1,6 @@
 // SwiftShader Software Renderer

 //

-// Copyright(c) 2005-2011 TransGaming Inc.

+// Copyright(c) 2005-2012 TransGaming Inc.

 //

 // All rights reserved. No part of this software may be copied, distributed, transmitted,

 // transcribed, stored in a retrieval system, translated into any human or computer

@@ -9,7 +9,10 @@
 // or implied, including but not limited to any patent rights, are granted to you.

 //

 

-#include "Types.hpp"

+#ifndef sw_Constants_hpp

+#define sw_Constants_hpp

+

+#include "Common/Types.hpp"

 

 namespace sw

 {

@@ -98,5 +101,7 @@
 		float half2float[65536];

 	};

 

-	static Constants constants;

+	extern Constants constants;

 }

+

+#endif   // sw_Constants_hpp

diff --git a/src/Shader/PixelRoutine.cpp b/src/Shader/PixelRoutine.cpp
index e1b735d..def4354 100644
--- a/src/Shader/PixelRoutine.cpp
+++ b/src/Shader/PixelRoutine.cpp
@@ -1,6 +1,6 @@
 // SwiftShader Software Renderer
 //
-// Copyright(c) 2005-2012 TransGaming Inc.
+// Copyright(c) 2005-2013 TransGaming Inc.
 //
 // All rights reserved. No part of this software may be copied, distributed, transmitted,
 // transcribed, stored in a retrieval system, translated into any human or computer
@@ -249,7 +249,7 @@
 				if(shader->vFaceDeclared)
 				{
 					Float4 area = *Pointer<Float>(r.primitive + OFFSET(Primitive,area));
-					Float4 face = booleanFaceRegister ? As<Float4>(CmpNLT(area, Float4(0.0f))) : area;
+					Float4 face = booleanFaceRegister ? Float4(As<Float4>(CmpNLT(area, Float4(0.0f)))) : area;
 
 					r.vFace.x = face;
 					r.vFace.y = face;
@@ -370,7 +370,7 @@
 					if(colorUsed())
 					{
 						#if PERF_PROFILE
-							AddAtomic(Pointer<Long>(&profiler.ropOperations), Long(4));
+							AddAtomic(Pointer<Long>(&profiler.ropOperations), 4);
 						#endif
 
 						if(integerPipeline)
@@ -2139,6 +2139,7 @@
 			case FORMAT_A16B16G16R16:
 			case FORMAT_A8R8G8B8:
 			case FORMAT_X8R8G8B8:
+			case FORMAT_A8:
 			case FORMAT_G16R16:
 				oC[index].x = Max(oC[index].x, Float4(0.0f)); oC[index].x = Min(oC[index].x, Float4(1.0f));
 				oC[index].y = Max(oC[index].y, Float4(0.0f)); oC[index].y = Min(oC[index].y, Float4(1.0f));
@@ -2168,6 +2169,7 @@
 		{
 		case FORMAT_X8R8G8B8:
 		case FORMAT_A8R8G8B8:
+		case FORMAT_A8:
 		case FORMAT_G16R16:
 		case FORMAT_A16B16G16R16:
 			if(!postBlendSRGB && state.writeSRGB)
@@ -2244,6 +2246,7 @@
 			{
 			case FORMAT_X8R8G8B8:
 			case FORMAT_A8R8G8B8:
+			case FORMAT_A8:
 			case FORMAT_G16R16:
 			case FORMAT_A16B16G16R16:
 				for(unsigned int q = 0; q < state.multiSample; q++)
@@ -2450,6 +2453,16 @@
 			pixel.z = UnpackLow(As<Byte8>(pixel.z), As<Byte8>(pixel.z));
 			pixel.w = UnpackHigh(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
 			break;
+		case FORMAT_A8:
+			buffer = cBuffer + 1 * x;
+			pixel.w = Insert(pixel.w, *Pointer<Short>(buffer), 0);
+			buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
+			pixel.w = Insert(pixel.w, *Pointer<Short>(buffer), 1);
+			pixel.w = UnpackLow(As<Byte8>(pixel.w), As<Byte8>(pixel.w));
+			pixel.x = Short4(0x0000);
+			pixel.y = Short4(0x0000);
+			pixel.z = Short4(0x0000);
+			break;
 		case FORMAT_X8R8G8B8:
 			buffer = cBuffer + 4 * x;
 			c01 = *Pointer<Short4>(buffer);
@@ -2714,6 +2727,10 @@
 				current.y = As<Short4>(UnpackHigh(current.y, current.x));
 			}
 			break;
+		case FORMAT_A8:
+			current.w = As<Short4>(As<UShort4>(current.w) >> 8);
+			current.w = As<Short4>(Pack(As<UShort4>(current.w), As<UShort4>(current.w)));
+			break;
 		case FORMAT_G16R16:
 			current.z = current.x;
 			current.x = As<Short4>(UnpackLow(current.x, current.y));
@@ -2840,6 +2857,23 @@
 			c23 |= value;
 			*Pointer<Short4>(buffer) = c23;
 			break;
+		case FORMAT_A8:
+			if(rgbaWriteMask & 0x00000008)
+			{
+				buffer = cBuffer + 1 * x;
+				Insert(value, *Pointer<Short>(buffer), 0);
+				Int pitch = *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
+				Insert(value, *Pointer<Short>(buffer + pitch), 1);
+				value = UnpackLow(As<Byte8>(value), As<Byte8>(value));
+
+				current.w &= *Pointer<Short4>(r.constants + OFFSET(Constants,maskB4Q) + 8 * xMask);
+				value &= *Pointer<Short4>(r.constants + OFFSET(Constants,invMaskB4Q) + 8 * xMask);
+				current.w |= value;
+
+				*Pointer<Short>(buffer) = Extract(current.w, 0);
+				*Pointer<Short>(buffer + pitch) = Extract(current.w, 1);
+			}
+			break;
 		case FORMAT_G16R16:
 			buffer = cBuffer + 4 * x;
 
@@ -3135,15 +3169,25 @@
 			pixel.z = convertUnsigned16(As<UShort4>(color.z));
 			pixel.w = Float4(1.0f);
 			break;
+		case FORMAT_A8:
+			buffer = cBuffer + 1 * x;
+			c01 = Insert(c01, *Pointer<Short>(buffer), 0);
+			buffer += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
+			c01 = Insert(c01, *Pointer<Short>(buffer), 1);
+			pixel.w = convertUnsigned16(As<UShort4>(UnpackLow(As<Byte8>(c01), As<Byte8>(c01))));
+			pixel.x = Float4(0.0f);
+			pixel.y = Float4(0.0f);
+			pixel.z = Float4(0.0f);
+			break;
 		case FORMAT_A8G8R8B8Q:
-UNIMPLEMENTED();
+			UNIMPLEMENTED();
 		//	UnpackLow(pixel.z, qword_ptr [cBuffer+8*x+0]);
 		//	UnpackHigh(pixel.x, qword_ptr [cBuffer+8*x+0]);
 		//	UnpackLow(pixel.y, qword_ptr [cBuffer+8*x+8]);
 		//	UnpackHigh(pixel.w, qword_ptr [cBuffer+8*x+8]);
 			break;
 		case FORMAT_X8G8R8B8Q:
-UNIMPLEMENTED();
+			UNIMPLEMENTED();
 		//	UnpackLow(pixel.z, qword_ptr [cBuffer+8*x+0]);
 		//	UnpackHigh(pixel.x, qword_ptr [cBuffer+8*x+0]);
 		//	UnpackLow(pixel.y, qword_ptr [cBuffer+8*x+8]);
@@ -3349,6 +3393,7 @@
 		{
 		case FORMAT_X8R8G8B8:
 		case FORMAT_A8R8G8B8:
+		case FORMAT_A8:
 		case FORMAT_G16R16:
 		case FORMAT_A16B16G16R16:
 			convertFixed16(color, oC, true);
@@ -5326,7 +5371,7 @@
 		Nucleus::setInsertBlock(testBlock);
 		r.enableContinue = restoreContinue;
 
-		Vector4f &src = reg(r, temporaryRegister);
+		const Vector4f &src = reg(r, temporaryRegister);
 		Int4 condition = As<Int4>(src.x);
 		condition &= r.enableStack[r.enableIndex - 1];
 		r.enableStack[r.enableIndex] = condition;
@@ -5359,7 +5404,7 @@
 				// FIXME: Encapsulate
 				UInt index = r.callStack[--r.stackIndex];
  
-				llvm::Value *value = Nucleus::createLoad(index.address);
+				llvm::Value *value = index.loadValue();
 				llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
 
 				for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
@@ -5450,10 +5495,10 @@
 			ASSERT(false);
 		}
 
-		Short4 &x = (*reg)[(src.swizzle >> 0) & 0x3];
-		Short4 &y = (*reg)[(src.swizzle >> 2) & 0x3];
-		Short4 &z = (*reg)[(src.swizzle >> 4) & 0x3];
-		Short4 &w = (*reg)[(src.swizzle >> 6) & 0x3];
+		const Short4 &x = (*reg)[(src.swizzle >> 0) & 0x3];
+		const Short4 &y = (*reg)[(src.swizzle >> 2) & 0x3];
+		const Short4 &z = (*reg)[(src.swizzle >> 4) & 0x3];
+		const Short4 &w = (*reg)[(src.swizzle >> 6) & 0x3];
 
 		Vector4i mod;
 
@@ -5630,10 +5675,10 @@
 			ASSERT(false);
 		}
 
-		Float4 &x = reg[(src.swizzle >> 0) & 0x3];
-		Float4 &y = reg[(src.swizzle >> 2) & 0x3];
-		Float4 &z = reg[(src.swizzle >> 4) & 0x3];
-		Float4 &w = reg[(src.swizzle >> 6) & 0x3];
+		const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
+		const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
+		const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
+		const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
 
 		Vector4f mod;
 
@@ -5663,6 +5708,12 @@
 			mod.z = -Abs(z);
 			mod.w = -Abs(w);
 			break;
+		case Shader::MODIFIER_NOT:
+			mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
+			mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
+			mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
+			mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
+			break;
 		default:
 			ASSERT(false);
 		}
diff --git a/src/Shader/PixelRoutine.hpp b/src/Shader/PixelRoutine.hpp
index 29ea75a..56bcddc 100644
--- a/src/Shader/PixelRoutine.hpp
+++ b/src/Shader/PixelRoutine.hpp
@@ -1,327 +1,327 @@
-// SwiftShader Software Renderer

-//

-// Copyright(c) 2005-2012 TransGaming Inc.

-//

-// All rights reserved. No part of this software may be copied, distributed, transmitted,

-// transcribed, stored in a retrieval system, translated into any human or computer

-// language by any means, or disclosed to third parties without the explicit written

-// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express

-// or implied, including but not limited to any patent rights, are granted to you.

-//

-

-#ifndef sw_PixelRoutine_hpp

-#define sw_PixelRoutine_hpp

-

-#include "Rasterizer.hpp"

-#include "ShaderCore.hpp"

-#include "PixelShader.hpp"

-

-#include "Types.hpp"

-

-namespace sw

-{

-	extern bool forceClearRegisters;

-

-	class PixelShader;

-	class SamplerCore;

-

-	class PixelRoutine : public Rasterizer, public ShaderCore

-	{

-		friend PixelProcessor;   // FIXME

-

-	public:

-		PixelRoutine(const PixelProcessor::State &state, const PixelShader *shader);

-

-		~PixelRoutine();

-

-	protected:

-		struct Registers

-		{

-			Registers(const PixelShader *shader) :

-				current(ri[0]), diffuse(vi[0]), specular(vi[1]),

-				rf(shader && shader->dynamicallyIndexedTemporaries),

-				vf(shader && shader->dynamicallyIndexedInput)

-			{

-				if(!shader || shader->getVersion() < 0x0200 || forceClearRegisters)

-				{

-					for(int i = 0; i < 10; i++)

-					{

-						vf[i].x = Float4(0.0f);

-						vf[i].y = Float4(0.0f);

-						vf[i].z = Float4(0.0f);

-						vf[i].w = Float4(0.0f);

-					}

-				}

-

-				loopDepth = -1;

-				enableStack[0] = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);

-				

-				if(shader && shader->containsBreakInstruction())

-				{

-					enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);

-				}

-

-				if(shader && shader->containsContinueInstruction())

-				{

-					enableContinue = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);

-				}

-

-				if(shader && shader->containsLeaveInstruction())

-				{

-					enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);

-				}

-

-				occlusion = 0;

-				

-				#if PERF_PROFILE

-					for(int i = 0; i < PERF_TIMERS; i++)

-					{

-						cycles[i] = 0;

-					}

-				#endif

-			}

-

-			Pointer<Byte> constants;

-

-			Pointer<Byte> primitive;

-			Int cluster;

-			Pointer<Byte> data;

-

-			Float4 z[4];

-			Float4 w;

-			Float4 rhw;

-

-			Float4 Dz[4];

-			Float4 Dw;

-			Float4 Dv[10][4];

-			Float4 Df;

-

-			Vector4i &current;

-			Vector4i &diffuse;

-			Vector4i &specular;

-

-			Vector4i ri[6];

-			Vector4i vi[2];

-			Vector4i ti[6];

-

-			RegisterArray<4096> rf;

-			RegisterArray<10> vf;

-

-			Vector4f vPos;

-			Vector4f vFace;

-

-			Vector4f oC[4];

-			Float4 oDepth;

-

-			Vector4f p0;

-			Array<Int, 4> aL;

-

-			Array<Int, 4> increment;

-			Array<Int, 4> iteration;

-

-			Int loopDepth;

-			Int stackIndex;   // FIXME: Inc/decrement callStack

-			Array<UInt, 4> callStack;

-

-			Int enableIndex;

-			Array<Int4, 1 + 24> enableStack;

-			Int4 enableBreak;

-			Int4 enableContinue;

-			Int4 enableLeave;

-

-			// bem(l) offsets and luminance

-			Float4 du;

-			Float4 dv;

-			Short4 L;

-

-			// texm3x3 temporaries

-			Float4 u_;   // FIXME

-			Float4 v_;   // FIXME

-			Float4 w_;   // FIXME

-			Float4 U;   // FIXME

-			Float4 V;   // FIXME

-			Float4 W;   // FIXME

-

-			UInt occlusion;

-

-			#if PERF_PROFILE

-				Long cycles[PERF_TIMERS];

-			#endif

-		};

-

-		typedef Shader::DestinationParameter Dst;

-		typedef Shader::SourceParameter Src;

-		typedef Shader::Control Control;

-

-		void quad(Registers &r, Pointer<Byte> cBuffer[4], Pointer<Byte> &zBuffer, Pointer<Byte> &sBuffer, Int cMask[4], Int &x, Int &y);

-

-		Float4 interpolate(Float4 &x, Float4 &D, Float4 &rhw, Pointer<Byte> planeEquation, bool flat, bool perspective);

-		Float4 interpolateCentroid(Float4 &x, Float4 &y, Float4 &rhw, Pointer<Byte> planeEquation, bool flat, bool perspective);

-		void stencilTest(Registers &r, Pointer<Byte> &sBuffer, int q, Int &x, Int &sMask, Int &cMask);

-		void stencilTest(Registers &r, Byte8 &value, Context::StencilCompareMode stencilCompareMode, bool CCW);

-		void stencilOperation(Registers &r, Byte8 &newValue, Byte8 &bufferValue, Context::StencilOperation stencilPassOperation, Context::StencilOperation stencilZFailOperation, Context::StencilOperation stencilFailOperation, bool CCW, Int &zMask, Int &sMask);

-		void stencilOperation(Registers &r, Byte8 &output, Byte8 &bufferValue, Context::StencilOperation operation, bool CCW);

-		Bool depthTest(Registers &r, Pointer<Byte> &zBuffer, int q, Int &x, Float4 &z, Int &sMask, Int &zMask, Int &cMask);

-		void blendTexture(Registers &r, Vector4i &current, Vector4i &temp, Vector4i &texture, int stage);

-		void alphaTest(Registers &r, Int &aMask, Short4 &alpha);

-		void alphaToCoverage(Registers &r, Int cMask[4], Float4 &alpha);

-		Bool alphaTest(Registers &r, Int cMask[4], Vector4i &current);

-		Bool alphaTest(Registers &r, Int cMask[4], Vector4f &c0);

-		void fogBlend(Registers &r, Vector4i &current, Float4 &fog, Float4 &z, Float4 &rhw);

-		void fogBlend(Registers &r, Vector4f &c0, Float4 &fog, Float4 &z, Float4 &rhw);

-		void pixelFog(Registers &r, Float4 &visibility, Float4 &z, Float4 &rhw);

-		void specularPixel(Vector4i &current, Vector4i &specular);

-

-		void sampleTexture(Registers &r, Vector4i &c, int coordinates, int sampler, bool project = false);

-		void sampleTexture(Registers &r, Vector4i &c, int sampler, Float4 &u, Float4 &v, Float4 &w, Float4 &q, bool project = false, bool bias = false, bool fixed12 = true);

-		void sampleTexture(Registers &r, Vector4i &c, int sampler, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &dsx, Vector4f &dsy, bool project = false, bool bias = false, bool fixed12 = true, bool gradients = false, bool lodProvided = false);

-		void sampleTexture(Registers &r, Vector4f &c, const Src &sampler, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &dsx, Vector4f &dsy, bool project = false, bool bias = false, bool gradients = false, bool lodProvided = false);

-		void sampleTexture(Registers &r, Vector4f &c, int sampler, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &dsx, Vector4f &dsy, bool project = false, bool bias = false, bool gradients = false, bool lodProvided = false);

-	

-		// Raster operations

-		void clampColor(Vector4f oC[4]);

-		void rasterOperation(Vector4i &current, Registers &r, Float4 &fog, Pointer<Byte> &cBuffer, Int &x, Int sMask[4], Int zMask[4], Int cMask[4]);

-		void rasterOperation(Vector4f oC[4], Registers &r, Float4 &fog, Pointer<Byte> cBuffer[4], Int &x, Int sMask[4], Int zMask[4], Int cMask[4]);

-		void blendFactor(Registers &r, const Vector4i &blendFactor, const Vector4i &current, const Vector4i &pixel, Context::BlendFactor blendFactorActive);

-		void blendFactorAlpha(Registers &r, const Vector4i &blendFactor, const Vector4i &current, const Vector4i &pixel, Context::BlendFactor blendFactorAlphaActive);

-		void alphaBlend(Registers &r, int index, Pointer<Byte> &cBuffer, Vector4i &current, Int &x);

-		void writeColor(Registers &r, int index, Pointer<Byte> &cBuffer, Int &i, Vector4i &current, Int &sMask, Int &zMask, Int &cMask);

-		void blendFactor(Registers &r, const Vector4f &blendFactor, const Vector4f &oC, const Vector4f &pixel, Context::BlendFactor blendFactorActive);

-		void blendFactorAlpha(Registers &r, const Vector4f &blendFactor, const Vector4f &oC, const Vector4f &pixel, Context::BlendFactor blendFactorAlphaActive);

-		void alphaBlend(Registers &r, int index, Pointer<Byte> &cBuffer, Vector4f &oC, Int &x);

-		void writeColor(Registers &r, int index, Pointer<Byte> &cBuffer, Int &i, Vector4f &oC, Int &sMask, Int &zMask, Int &cMask);

-		void writeStencil(Registers &r, Pointer<Byte> &sBuffer, int q, Int &x, Int &sMask, Int &zMask, Int &cMask);

-		void writeDepth(Registers &r, Pointer<Byte> &zBuffer, int q, Int &x, Float4 &z, Int &zMask);

-

-		void ps_1_x(Registers &r, Int cMask[4]);

-		void ps_2_x(Registers &r, Int cMask[4]);

-

-		Short4 convertFixed12(RValue<Float4> cf);

-		void convertFixed12(Vector4i &ci, Vector4f &cf);

-		Float4 convertSigned12(Short4 &ci);

-		void convertSigned12(Vector4f &cf, Vector4i &ci);

-		Float4 convertUnsigned16(UShort4 ci);

-		UShort4 convertFixed16(Float4 &cf, bool saturate = true);

-		void convertFixed16(Vector4i &ci, Vector4f &cf, bool saturate = true);

-		void sRGBtoLinear16_16(Registers &r, Vector4i &c);

-		void sRGBtoLinear12_16(Registers &r, Vector4i &c);

-		void linearToSRGB16_16(Registers &r, Vector4i &c);

-		void linearToSRGB12_16(Registers &r, Vector4i &c);

-		Float4 sRGBtoLinear(const Float4 &x);

-		Float4 linearToSRGB(const Float4 &x);

-

-		// ps_1_x instructions

-		void MOV(Vector4i &dst, Vector4i &src0);

-		void ADD(Vector4i &dst, Vector4i &src0, Vector4i &src1);

-		void SUB(Vector4i &dst, Vector4i &src0, Vector4i &src1);

-		void MAD(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2);

-		void MUL(Vector4i &dst, Vector4i &src0, Vector4i &src1);

-		void DP3(Vector4i &dst, Vector4i &src0, Vector4i &src1);

-		void DP4(Vector4i &dst, Vector4i &src0, Vector4i &src1);

-		void LRP(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2);

-		void TEXCOORD(Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int coordinate);

-		void TEXCRD(Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int coordinate, bool project);

-		void TEXDP3(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, Vector4i &src);

-		void TEXDP3TEX(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0);

-		void TEXKILL(Int cMask[4], Float4 &u, Float4 &v, Float4 &s);

-		void TEXKILL(Int cMask[4], Vector4i &dst);

-		void TEX(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, bool project);

-		void TEXLD(Registers &r, Vector4i &dst, Vector4i &src, int stage, bool project);

-		void TEXBEM(Registers &r, Vector4i &dst, Vector4i &src, Float4 &u, Float4 &v, Float4 &s, int stage);

-		void TEXBEML(Registers &r, Vector4i &dst, Vector4i &src, Float4 &u, Float4 &v, Float4 &s, int stage);

-		void TEXREG2AR(Registers &r, Vector4i &dst, Vector4i &src0, int stage);

-		void TEXREG2GB(Registers &r, Vector4i &dst, Vector4i &src0, int stage);

-		void TEXREG2RGB(Registers &r, Vector4i &dst, Vector4i &src0, int stage);

-		void TEXM3X2DEPTH(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, Vector4i &src, bool signedScaling);

-		void TEXM3X2PAD(Registers &r, Float4 &u, Float4 &v, Float4 &s, Vector4i &src0, int component, bool signedScaling);

-		void TEXM3X2TEX(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0, bool signedScaling);

-		void TEXM3X3(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, Vector4i &src0, bool signedScaling);

-		void TEXM3X3PAD(Registers &r, Float4 &u, Float4 &v, Float4 &s, Vector4i &src0, int component, bool signedScaling);

-		void TEXM3X3SPEC(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0, Vector4i &src1);

-		void TEXM3X3TEX(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0, bool singedScaling);

-		void TEXM3X3VSPEC(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0);

-		void TEXDEPTH(Registers &r);

-		void CND(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2);

-		void CMP(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2);

-		void BEM(Registers &r, Vector4i &dst, Vector4i &src0, Vector4i &src1, int stage);

-

-		// ps_2_x instructions

-		void M3X2(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1);

-		void M3X3(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1);

-		void M3X4(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1);

-		void M4X3(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1);

-		void M4X4(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1);

-		void TEXLD(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1, bool project, bool bias);

-		void TEXLDD(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1, Vector4f &src2,  Vector4f &src3, bool project, bool bias);

-		void TEXLDL(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1, bool project, bool bias);

-		void TEXKILL(Int cMask[4], Vector4f &src, unsigned char mask);

-		void DISCARD(Registers &r, Int cMask[4], const Shader::Instruction *instruction);

-		void DFDX(Vector4f &dst, Vector4f &src);

-		void DFDY(Vector4f &dst, Vector4f &src);

-		void FWIDTH(Vector4f &dst, Vector4f &src);

-		void BREAK(Registers &r);

-		void BREAKC(Registers &r, Vector4f &src0, Vector4f &src1, Control);

-		void BREAKP(Registers &r, const Src &predicateRegister);

-		void BREAK(Registers &r, Int4 &condition);

-		void CONTINUE(Registers &r);

-		void TEST();

-		void CALL(Registers &r, int labelIndex, int callSiteIndex);

-		void CALLNZ(Registers &r, int labelIndex, int callSiteIndex, const Src &src);

-		void CALLNZb(Registers &r, int labelIndex, int callSiteIndex, const Src &boolRegister);

-		void CALLNZp(Registers &r, int labelIndex, int callSiteIndex, const Src &predicateRegister);

-		void ELSE(Registers &r);

-		void ENDIF(Registers &r);

-		void ENDLOOP(Registers &r);

-		void ENDREP(Registers &r);

-		void ENDWHILE(Registers &r);

-		void IF(Registers &r, const Src &src);

-		void IFb(Registers &r, const Src &boolRegister);

-		void IFp(Registers &r, const Src &predicateRegister);

-		void IFC(Registers &r, Vector4f &src0, Vector4f &src1, Control);

-		void IF(Registers &r, Int4 &condition);

-		void LABEL(int labelIndex);

-		void LOOP(Registers &r, const Src &integerRegister);

-		void REP(Registers &r, const Src &integerRegister);

-		void WHILE(Registers &r, const Src &temporaryRegister);

-		void RET(Registers &r);

-		void LEAVE(Registers &r);

-

-		void writeDestination(Registers &r, Vector4i &d, const Dst &dst);

-		Vector4i regi(Registers &r, const Src &src);

-		Vector4f reg(Registers &r, const Src &src, int offset = 0);

-		Vector4f readConstant(Registers &r, const Src &src, int offset = 0);

-		Int relativeAddress(Registers &r, const Shader::Parameter &var);

-		Int4 enableMask(Registers &r, const Shader::Instruction *instruction);

-

-		bool colorUsed();

-		unsigned short shaderVersion() const;

-		bool interpolateZ() const;

-		bool interpolateW() const;

-

-		const PixelShader *const shader;

-

-	private:

-		SamplerCore *sampler[16];

-

-		bool perturbate;

-		bool luminance;

-		bool previousScaling;

-

-		int ifDepth;

-		int loopRepDepth;

-		int breakDepth;

-		int currentLabel;

-		bool whileTest;

-

-		// FIXME: Get rid of llvm::

-		llvm::BasicBlock *ifFalseBlock[24 + 24];

-		llvm::BasicBlock *loopRepTestBlock[4];

-		llvm::BasicBlock *loopRepEndBlock[4];

-		llvm::BasicBlock *labelBlock[2048];

-		std::vector<llvm::BasicBlock*> callRetBlock[2048];

-		llvm::BasicBlock *returnBlock;

-		bool isConditionalIf[24 + 24];

-	};

-}

-

-#endif   // sw_PixelRoutine_hpp

+// SwiftShader Software Renderer
+//
+// Copyright(c) 2005-2012 TransGaming Inc.
+//
+// All rights reserved. No part of this software may be copied, distributed, transmitted,
+// transcribed, stored in a retrieval system, translated into any human or computer
+// language by any means, or disclosed to third parties without the explicit written
+// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
+// or implied, including but not limited to any patent rights, are granted to you.
+//
+
+#ifndef sw_PixelRoutine_hpp
+#define sw_PixelRoutine_hpp
+
+#include "Rasterizer.hpp"
+#include "ShaderCore.hpp"
+#include "PixelShader.hpp"
+
+#include "Types.hpp"
+
+namespace sw
+{
+	extern bool forceClearRegisters;
+
+	class PixelShader;
+	class SamplerCore;
+
+	class PixelRoutine : public Rasterizer, public ShaderCore
+	{
+		friend class PixelProcessor;   // FIXME
+
+	public:
+		PixelRoutine(const PixelProcessor::State &state, const PixelShader *shader);
+
+		~PixelRoutine();
+
+	protected:
+		struct Registers
+		{
+			Registers(const PixelShader *shader) :
+				current(ri[0]), diffuse(vi[0]), specular(vi[1]),
+				rf(shader && shader->dynamicallyIndexedTemporaries),
+				vf(shader && shader->dynamicallyIndexedInput)
+			{
+				if(!shader || shader->getVersion() < 0x0200 || forceClearRegisters)
+				{
+					for(int i = 0; i < 10; i++)
+					{
+						vf[i].x = Float4(0.0f);
+						vf[i].y = Float4(0.0f);
+						vf[i].z = Float4(0.0f);
+						vf[i].w = Float4(0.0f);
+					}
+				}
+
+				loopDepth = -1;
+				enableStack[0] = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
+				
+				if(shader && shader->containsBreakInstruction())
+				{
+					enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
+				}
+
+				if(shader && shader->containsContinueInstruction())
+				{
+					enableContinue = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
+				}
+
+				if(shader && shader->containsLeaveInstruction())
+				{
+					enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
+				}
+
+				occlusion = 0;
+				
+				#if PERF_PROFILE
+					for(int i = 0; i < PERF_TIMERS; i++)
+					{
+						cycles[i] = 0;
+					}
+				#endif
+			}
+
+			Pointer<Byte> constants;
+
+			Pointer<Byte> primitive;
+			Int cluster;
+			Pointer<Byte> data;
+
+			Float4 z[4];
+			Float4 w;
+			Float4 rhw;
+
+			Float4 Dz[4];
+			Float4 Dw;
+			Float4 Dv[10][4];
+			Float4 Df;
+
+			Vector4i &current;
+			Vector4i &diffuse;
+			Vector4i &specular;
+
+			Vector4i ri[6];
+			Vector4i vi[2];
+			Vector4i ti[6];
+
+			RegisterArray<4096> rf;
+			RegisterArray<10> vf;
+
+			Vector4f vPos;
+			Vector4f vFace;
+
+			Vector4f oC[4];
+			Float4 oDepth;
+
+			Vector4f p0;
+			Array<Int, 4> aL;
+
+			Array<Int, 4> increment;
+			Array<Int, 4> iteration;
+
+			Int loopDepth;
+			Int stackIndex;   // FIXME: Inc/decrement callStack
+			Array<UInt, 4> callStack;
+
+			Int enableIndex;
+			Array<Int4, 1 + 24> enableStack;
+			Int4 enableBreak;
+			Int4 enableContinue;
+			Int4 enableLeave;
+
+			// bem(l) offsets and luminance
+			Float4 du;
+			Float4 dv;
+			Short4 L;
+
+			// texm3x3 temporaries
+			Float4 u_;   // FIXME
+			Float4 v_;   // FIXME
+			Float4 w_;   // FIXME
+			Float4 U;   // FIXME
+			Float4 V;   // FIXME
+			Float4 W;   // FIXME
+
+			UInt occlusion;
+
+			#if PERF_PROFILE
+				Long cycles[PERF_TIMERS];
+			#endif
+		};
+
+		typedef Shader::DestinationParameter Dst;
+		typedef Shader::SourceParameter Src;
+		typedef Shader::Control Control;
+
+		void quad(Registers &r, Pointer<Byte> cBuffer[4], Pointer<Byte> &zBuffer, Pointer<Byte> &sBuffer, Int cMask[4], Int &x, Int &y);
+
+		Float4 interpolate(Float4 &x, Float4 &D, Float4 &rhw, Pointer<Byte> planeEquation, bool flat, bool perspective);
+		Float4 interpolateCentroid(Float4 &x, Float4 &y, Float4 &rhw, Pointer<Byte> planeEquation, bool flat, bool perspective);
+		void stencilTest(Registers &r, Pointer<Byte> &sBuffer, int q, Int &x, Int &sMask, Int &cMask);
+		void stencilTest(Registers &r, Byte8 &value, Context::StencilCompareMode stencilCompareMode, bool CCW);
+		void stencilOperation(Registers &r, Byte8 &newValue, Byte8 &bufferValue, Context::StencilOperation stencilPassOperation, Context::StencilOperation stencilZFailOperation, Context::StencilOperation stencilFailOperation, bool CCW, Int &zMask, Int &sMask);
+		void stencilOperation(Registers &r, Byte8 &output, Byte8 &bufferValue, Context::StencilOperation operation, bool CCW);
+		Bool depthTest(Registers &r, Pointer<Byte> &zBuffer, int q, Int &x, Float4 &z, Int &sMask, Int &zMask, Int &cMask);
+		void blendTexture(Registers &r, Vector4i &current, Vector4i &temp, Vector4i &texture, int stage);
+		void alphaTest(Registers &r, Int &aMask, Short4 &alpha);
+		void alphaToCoverage(Registers &r, Int cMask[4], Float4 &alpha);
+		Bool alphaTest(Registers &r, Int cMask[4], Vector4i &current);
+		Bool alphaTest(Registers &r, Int cMask[4], Vector4f &c0);
+		void fogBlend(Registers &r, Vector4i &current, Float4 &fog, Float4 &z, Float4 &rhw);
+		void fogBlend(Registers &r, Vector4f &c0, Float4 &fog, Float4 &z, Float4 &rhw);
+		void pixelFog(Registers &r, Float4 &visibility, Float4 &z, Float4 &rhw);
+		void specularPixel(Vector4i &current, Vector4i &specular);
+
+		void sampleTexture(Registers &r, Vector4i &c, int coordinates, int sampler, bool project = false);
+		void sampleTexture(Registers &r, Vector4i &c, int sampler, Float4 &u, Float4 &v, Float4 &w, Float4 &q, bool project = false, bool bias = false, bool fixed12 = true);
+		void sampleTexture(Registers &r, Vector4i &c, int sampler, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &dsx, Vector4f &dsy, bool project = false, bool bias = false, bool fixed12 = true, bool gradients = false, bool lodProvided = false);
+		void sampleTexture(Registers &r, Vector4f &c, const Src &sampler, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &dsx, Vector4f &dsy, bool project = false, bool bias = false, bool gradients = false, bool lodProvided = false);
+		void sampleTexture(Registers &r, Vector4f &c, int sampler, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &dsx, Vector4f &dsy, bool project = false, bool bias = false, bool gradients = false, bool lodProvided = false);
+	
+		// Raster operations
+		void clampColor(Vector4f oC[4]);
+		void rasterOperation(Vector4i &current, Registers &r, Float4 &fog, Pointer<Byte> &cBuffer, Int &x, Int sMask[4], Int zMask[4], Int cMask[4]);
+		void rasterOperation(Vector4f oC[4], Registers &r, Float4 &fog, Pointer<Byte> cBuffer[4], Int &x, Int sMask[4], Int zMask[4], Int cMask[4]);
+		void blendFactor(Registers &r, const Vector4i &blendFactor, const Vector4i &current, const Vector4i &pixel, Context::BlendFactor blendFactorActive);
+		void blendFactorAlpha(Registers &r, const Vector4i &blendFactor, const Vector4i &current, const Vector4i &pixel, Context::BlendFactor blendFactorAlphaActive);
+		void alphaBlend(Registers &r, int index, Pointer<Byte> &cBuffer, Vector4i &current, Int &x);
+		void writeColor(Registers &r, int index, Pointer<Byte> &cBuffer, Int &i, Vector4i &current, Int &sMask, Int &zMask, Int &cMask);
+		void blendFactor(Registers &r, const Vector4f &blendFactor, const Vector4f &oC, const Vector4f &pixel, Context::BlendFactor blendFactorActive);
+		void blendFactorAlpha(Registers &r, const Vector4f &blendFactor, const Vector4f &oC, const Vector4f &pixel, Context::BlendFactor blendFactorAlphaActive);
+		void alphaBlend(Registers &r, int index, Pointer<Byte> &cBuffer, Vector4f &oC, Int &x);
+		void writeColor(Registers &r, int index, Pointer<Byte> &cBuffer, Int &i, Vector4f &oC, Int &sMask, Int &zMask, Int &cMask);
+		void writeStencil(Registers &r, Pointer<Byte> &sBuffer, int q, Int &x, Int &sMask, Int &zMask, Int &cMask);
+		void writeDepth(Registers &r, Pointer<Byte> &zBuffer, int q, Int &x, Float4 &z, Int &zMask);
+
+		void ps_1_x(Registers &r, Int cMask[4]);
+		void ps_2_x(Registers &r, Int cMask[4]);
+
+		Short4 convertFixed12(RValue<Float4> cf);
+		void convertFixed12(Vector4i &ci, Vector4f &cf);
+		Float4 convertSigned12(Short4 &ci);
+		void convertSigned12(Vector4f &cf, Vector4i &ci);
+		Float4 convertUnsigned16(UShort4 ci);
+		UShort4 convertFixed16(Float4 &cf, bool saturate = true);
+		void convertFixed16(Vector4i &ci, Vector4f &cf, bool saturate = true);
+		void sRGBtoLinear16_16(Registers &r, Vector4i &c);
+		void sRGBtoLinear12_16(Registers &r, Vector4i &c);
+		void linearToSRGB16_16(Registers &r, Vector4i &c);
+		void linearToSRGB12_16(Registers &r, Vector4i &c);
+		Float4 sRGBtoLinear(const Float4 &x);
+		Float4 linearToSRGB(const Float4 &x);
+
+		// ps_1_x instructions
+		void MOV(Vector4i &dst, Vector4i &src0);
+		void ADD(Vector4i &dst, Vector4i &src0, Vector4i &src1);
+		void SUB(Vector4i &dst, Vector4i &src0, Vector4i &src1);
+		void MAD(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2);
+		void MUL(Vector4i &dst, Vector4i &src0, Vector4i &src1);
+		void DP3(Vector4i &dst, Vector4i &src0, Vector4i &src1);
+		void DP4(Vector4i &dst, Vector4i &src0, Vector4i &src1);
+		void LRP(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2);
+		void TEXCOORD(Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int coordinate);
+		void TEXCRD(Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int coordinate, bool project);
+		void TEXDP3(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, Vector4i &src);
+		void TEXDP3TEX(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0);
+		void TEXKILL(Int cMask[4], Float4 &u, Float4 &v, Float4 &s);
+		void TEXKILL(Int cMask[4], Vector4i &dst);
+		void TEX(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, bool project);
+		void TEXLD(Registers &r, Vector4i &dst, Vector4i &src, int stage, bool project);
+		void TEXBEM(Registers &r, Vector4i &dst, Vector4i &src, Float4 &u, Float4 &v, Float4 &s, int stage);
+		void TEXBEML(Registers &r, Vector4i &dst, Vector4i &src, Float4 &u, Float4 &v, Float4 &s, int stage);
+		void TEXREG2AR(Registers &r, Vector4i &dst, Vector4i &src0, int stage);
+		void TEXREG2GB(Registers &r, Vector4i &dst, Vector4i &src0, int stage);
+		void TEXREG2RGB(Registers &r, Vector4i &dst, Vector4i &src0, int stage);
+		void TEXM3X2DEPTH(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, Vector4i &src, bool signedScaling);
+		void TEXM3X2PAD(Registers &r, Float4 &u, Float4 &v, Float4 &s, Vector4i &src0, int component, bool signedScaling);
+		void TEXM3X2TEX(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0, bool signedScaling);
+		void TEXM3X3(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, Vector4i &src0, bool signedScaling);
+		void TEXM3X3PAD(Registers &r, Float4 &u, Float4 &v, Float4 &s, Vector4i &src0, int component, bool signedScaling);
+		void TEXM3X3SPEC(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0, Vector4i &src1);
+		void TEXM3X3TEX(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0, bool singedScaling);
+		void TEXM3X3VSPEC(Registers &r, Vector4i &dst, Float4 &u, Float4 &v, Float4 &s, int stage, Vector4i &src0);
+		void TEXDEPTH(Registers &r);
+		void CND(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2);
+		void CMP(Vector4i &dst, Vector4i &src0, Vector4i &src1, Vector4i &src2);
+		void BEM(Registers &r, Vector4i &dst, Vector4i &src0, Vector4i &src1, int stage);
+
+		// ps_2_x instructions
+		void M3X2(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1);
+		void M3X3(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1);
+		void M3X4(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1);
+		void M4X3(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1);
+		void M4X4(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1);
+		void TEXLD(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1, bool project, bool bias);
+		void TEXLDD(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1, Vector4f &src2,  Vector4f &src3, bool project, bool bias);
+		void TEXLDL(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1, bool project, bool bias);
+		void TEXKILL(Int cMask[4], Vector4f &src, unsigned char mask);
+		void DISCARD(Registers &r, Int cMask[4], const Shader::Instruction *instruction);
+		void DFDX(Vector4f &dst, Vector4f &src);
+		void DFDY(Vector4f &dst, Vector4f &src);
+		void FWIDTH(Vector4f &dst, Vector4f &src);
+		void BREAK(Registers &r);
+		void BREAKC(Registers &r, Vector4f &src0, Vector4f &src1, Control);
+		void BREAKP(Registers &r, const Src &predicateRegister);
+		void BREAK(Registers &r, Int4 &condition);
+		void CONTINUE(Registers &r);
+		void TEST();
+		void CALL(Registers &r, int labelIndex, int callSiteIndex);
+		void CALLNZ(Registers &r, int labelIndex, int callSiteIndex, const Src &src);
+		void CALLNZb(Registers &r, int labelIndex, int callSiteIndex, const Src &boolRegister);
+		void CALLNZp(Registers &r, int labelIndex, int callSiteIndex, const Src &predicateRegister);
+		void ELSE(Registers &r);
+		void ENDIF(Registers &r);
+		void ENDLOOP(Registers &r);
+		void ENDREP(Registers &r);
+		void ENDWHILE(Registers &r);
+		void IF(Registers &r, const Src &src);
+		void IFb(Registers &r, const Src &boolRegister);
+		void IFp(Registers &r, const Src &predicateRegister);
+		void IFC(Registers &r, Vector4f &src0, Vector4f &src1, Control);
+		void IF(Registers &r, Int4 &condition);
+		void LABEL(int labelIndex);
+		void LOOP(Registers &r, const Src &integerRegister);
+		void REP(Registers &r, const Src &integerRegister);
+		void WHILE(Registers &r, const Src &temporaryRegister);
+		void RET(Registers &r);
+		void LEAVE(Registers &r);
+
+		void writeDestination(Registers &r, Vector4i &d, const Dst &dst);
+		Vector4i regi(Registers &r, const Src &src);
+		Vector4f reg(Registers &r, const Src &src, int offset = 0);
+		Vector4f readConstant(Registers &r, const Src &src, int offset = 0);
+		Int relativeAddress(Registers &r, const Shader::Parameter &var);
+		Int4 enableMask(Registers &r, const Shader::Instruction *instruction);
+
+		bool colorUsed();
+		unsigned short shaderVersion() const;
+		bool interpolateZ() const;
+		bool interpolateW() const;
+
+		const PixelShader *const shader;
+
+	private:
+		SamplerCore *sampler[16];
+
+		bool perturbate;
+		bool luminance;
+		bool previousScaling;
+
+		int ifDepth;
+		int loopRepDepth;
+		int breakDepth;
+		int currentLabel;
+		bool whileTest;
+
+		// FIXME: Get rid of llvm::
+		llvm::BasicBlock *ifFalseBlock[24 + 24];
+		llvm::BasicBlock *loopRepTestBlock[4];
+		llvm::BasicBlock *loopRepEndBlock[4];
+		llvm::BasicBlock *labelBlock[2048];
+		std::vector<llvm::BasicBlock*> callRetBlock[2048];
+		llvm::BasicBlock *returnBlock;
+		bool isConditionalIf[24 + 24];
+	};
+}
+
+#endif   // sw_PixelRoutine_hpp
diff --git a/src/Shader/PixelShader.cpp b/src/Shader/PixelShader.cpp
index 3a43892..939ead6 100644
--- a/src/Shader/PixelShader.cpp
+++ b/src/Shader/PixelShader.cpp
@@ -1,6 +1,6 @@
 // SwiftShader Software Renderer
 //
-// Copyright(c) 2005-2012 TransGaming Inc.
+// Copyright(c) 2005-2013 TransGaming Inc.
 //
 // All rights reserved. No part of this software may be copied, distributed, transmitted,
 // transcribed, stored in a retrieval system, translated into any human or computer
@@ -13,6 +13,8 @@
 
 #include "Debug.hpp"
 
+#include <string.h>
+
 namespace sw
 {
 	PixelShader::PixelShader(const PixelShader *ps) : Shader()
@@ -216,7 +218,7 @@
 				}
 			}
 
-			bool interpolant[10][4] = {false};   // Interpolants in use
+			bool interpolant[MAX_INPUT_VARYINGS][4] = {false};   // Interpolants in use
 
 			for(unsigned int i = 0; i < instruction.size(); i++)
 			{
@@ -642,7 +644,7 @@
 				}
 			}
 
-			for(int index = 0; index < 10; index++)
+			for(int index = 0; index < MAX_INPUT_VARYINGS; index++)
 			{
 				for(int component = 0; component < 4; component++)
 				{
diff --git a/src/Shader/PixelShader.hpp b/src/Shader/PixelShader.hpp
index 83ca253..00bf789 100644
--- a/src/Shader/PixelShader.hpp
+++ b/src/Shader/PixelShader.hpp
@@ -1,53 +1,54 @@
-// SwiftShader Software Renderer

-//

-// Copyright(c) 2005-2012 TransGaming Inc.

-//

-// All rights reserved. No part of this software may be copied, distributed, transmitted,

-// transcribed, stored in a retrieval system, translated into any human or computer

-// language by any means, or disclosed to third parties without the explicit written

-// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express

-// or implied, including but not limited to any patent rights, are granted to you.

-//

-

-#ifndef sw_PixelShader_hpp

-#define sw_PixelShader_hpp

-

-#include "Shader.hpp"

-

-namespace sw

-{

-	class PixelShader : public Shader

-	{

-	public:

-		explicit PixelShader(const PixelShader *ps = 0);

-		explicit PixelShader(const unsigned long *token);

-

-		virtual ~PixelShader();

-

-		static int validate(const unsigned long *const token);   // Returns number of instructions if valid

-		bool depthOverride() const;

-		bool containsKill() const;

-		bool containsCentroid() const;

-		bool usesDiffuse(int component) const;

-		bool usesSpecular(int component) const;

-		bool usesTexture(int coordinate, int component) const;

-

-		virtual void analyze();

-

-		Semantic semantic[10][4];   // FIXME: Private

-

-		bool vPosDeclared;

-		bool vFaceDeclared;

-

-	private:

-		void analyzeZOverride();

-		void analyzeKill();

-		void analyzeInterpolants();

-

-		bool zOverride;

-		bool kill;

-		bool centroid;

-	};

-}

-

-#endif   // sw_PixelShader_hpp

+// SwiftShader Software Renderer
+//
+// Copyright(c) 2005-2013 TransGaming Inc.
+//
+// All rights reserved. No part of this software may be copied, distributed, transmitted,
+// transcribed, stored in a retrieval system, translated into any human or computer
+// language by any means, or disclosed to third parties without the explicit written
+// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
+// or implied, including but not limited to any patent rights, are granted to you.
+//
+
+#ifndef sw_PixelShader_hpp
+#define sw_PixelShader_hpp
+
+#include "Shader.hpp"
+
+namespace sw
+{
+	class PixelShader : public Shader
+	{
+	public:
+		explicit PixelShader(const PixelShader *ps = 0);
+		explicit PixelShader(const unsigned long *token);
+
+		virtual ~PixelShader();
+
+		static int validate(const unsigned long *const token);   // Returns number of instructions if valid
+		bool depthOverride() const;
+		bool containsKill() const;
+		bool containsCentroid() const;
+		bool usesDiffuse(int component) const;
+		bool usesSpecular(int component) const;
+		bool usesTexture(int coordinate, int component) const;
+
+		virtual void analyze();
+
+		enum {MAX_INPUT_VARYINGS = 11};
+		Semantic semantic[MAX_INPUT_VARYINGS][4];   // FIXME: Private
+
+		bool vPosDeclared;
+		bool vFaceDeclared;
+
+	private:
+		void analyzeZOverride();
+		void analyzeKill();
+		void analyzeInterpolants();
+
+		bool zOverride;
+		bool kill;
+		bool centroid;
+	};
+}
+
+#endif   // sw_PixelShader_hpp
diff --git a/src/Shader/SamplerCore.cpp b/src/Shader/SamplerCore.cpp
index dbd98b8..1c428f2 100644
--- a/src/Shader/SamplerCore.cpp
+++ b/src/Shader/SamplerCore.cpp
@@ -1,6 +1,6 @@
 // SwiftShader Software Renderer
 //
-// Copyright(c) 2005-2012 TransGaming Inc.
+// Copyright(c) 2005-2013 TransGaming Inc.
 //
 // All rights reserved. No part of this software may be copied, distributed, transmitted,
 // transcribed, stored in a retrieval system, translated into any human or computer
@@ -23,11 +23,11 @@
 	void SamplerCore::sampleTexture(Pointer<Byte> &texture, Vector4i &c, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &dsx, Vector4f &dsy, bool bias, bool fixed12, bool gradients, bool lodProvided)
 	{
 		#if PERF_PROFILE
-			AddAtomic(Pointer<Long>(&profiler.texOperations), Long(4));
+			AddAtomic(Pointer<Long>(&profiler.texOperations), 4);
 
 			if(state.compressedFormat)
 			{
-				AddAtomic(Pointer<Long>(&profiler.compressedTex), Long(4));
+				AddAtomic(Pointer<Long>(&profiler.compressedTex), 4);
 			}
 		#endif
 
@@ -73,16 +73,16 @@
 			{
 				if(!cubeTexture)
 				{
-					computeLod(texture, lod, anisotropy, uDelta, vDelta, uuuu, vvvv, Float(q.x), dsx, dsy, bias, gradients, lodProvided);
+					computeLod(texture, lod, anisotropy, uDelta, vDelta, uuuu, vvvv, q.x, dsx, dsy, bias, gradients, lodProvided);
 				}
 				else
 				{
-					computeLod(texture, lod, anisotropy, uDelta, vDelta, lodU, lodV, Float(q.x), dsx, dsy, bias, gradients, lodProvided);
+					computeLod(texture, lod, anisotropy, uDelta, vDelta, lodU, lodV, q.x, dsx, dsy, bias, gradients, lodProvided);
 				}
 			}
 			else
 			{
-				computeLod3D(texture, lod, uuuu, vvvv, wwww, Float(q.x), dsx, dsy, bias, gradients, lodProvided);
+				computeLod3D(texture, lod, uuuu, vvvv, wwww, q.x, dsx, dsy, bias, gradients, lodProvided);
 			}
 
 			if(cubeTexture)
@@ -173,8 +173,8 @@
 				case FORMAT_A32B32G32R32F:
 					break;
 				case FORMAT_D32F_LOCKABLE:
-				case FORMAT_D32F_TEXTURE:
-				case FORMAT_D32F_SHADOW:
+				case FORMAT_D32FS8_TEXTURE:
+				case FORMAT_D32FS8_SHADOW:
 					c.y = c.x;
 					c.z = c.x;
 					c.w = c.x;
@@ -189,11 +189,11 @@
 	void SamplerCore::sampleTexture(Pointer<Byte> &texture, Vector4f &c, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &dsx, Vector4f &dsy, bool bias, bool gradients, bool lodProvided)
 	{
 		#if PERF_PROFILE
-			AddAtomic(Pointer<Long>(&profiler.texOperations), Long(4));
+			AddAtomic(Pointer<Long>(&profiler.texOperations), 4);
 
 			if(state.compressedFormat)
 			{
-				AddAtomic(Pointer<Long>(&profiler.compressedTex), Long(4));
+				AddAtomic(Pointer<Long>(&profiler.compressedTex), 4);
 			}
 		#endif
 
@@ -233,16 +233,16 @@
 				{
 					if(!cubeTexture)
 					{
-						computeLod(texture, lod, anisotropy, uDelta, vDelta, uuuu, vvvv, Float(q.x), dsx, dsy, bias, gradients, lodProvided);
+						computeLod(texture, lod, anisotropy, uDelta, vDelta, uuuu, vvvv, q.x, dsx, dsy, bias, gradients, lodProvided);
 					}
 					else
 					{
-						computeLod(texture, lod, anisotropy, uDelta, vDelta, lodU, lodV, Float(q.x), dsx, dsy, bias, gradients, lodProvided);
+						computeLod(texture, lod, anisotropy, uDelta, vDelta, lodU, lodV, q.x, dsx, dsy, bias, gradients, lodProvided);
 					}
 				}
 				else
 				{
-					computeLod3D(texture, lod, uuuu, vvvv, wwww, Float(q.x), dsx, dsy, bias, gradients, lodProvided);
+					computeLod3D(texture, lod, uuuu, vvvv, wwww, q.x, dsx, dsy, bias, gradients, lodProvided);
 				}
 
 				if(cubeTexture)
@@ -327,8 +327,8 @@
 				case FORMAT_A32B32G32R32F:
 					break;
 				case FORMAT_D32F_LOCKABLE:
-				case FORMAT_D32F_TEXTURE:
-				case FORMAT_D32F_SHADOW:
+				case FORMAT_D32FS8_TEXTURE:
+				case FORMAT_D32FS8_SHADOW:
 					c.y = c.x;
 					c.z = c.x;
 					c.w = c.x;
@@ -1197,7 +1197,7 @@
 		}
 	}
 
-	void SamplerCore::computeLod(Pointer<Byte> &texture, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, Float4 &uuuu, Float4 &vvvv, Float &lodBias, Vector4f &dsx, Vector4f &dsy, bool bias, bool gradients, bool lodProvided)
+	void SamplerCore::computeLod(Pointer<Byte> &texture, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, Float4 &uuuu, Float4 &vvvv, const Float &lodBias, Vector4f &dsx, Vector4f &dsy, bool bias, bool gradients, bool lodProvided)
 	{
 		if(!lodProvided)
 		{
@@ -1263,11 +1263,11 @@
 			lod = lodBias + *Pointer<Float>(texture + OFFSET(Texture,LOD));
 		}
 
-		lod = Max(lod, Float(0));
+		lod = Max(lod, 0.0f);
 		lod = Min(lod, Float(MIPMAP_LEVELS - 2));   // Trilinear accesses lod+1
 	}
 
-	void SamplerCore::computeLod3D(Pointer<Byte> &texture, Float &lod, Float4 &uuuu, Float4 &vvvv, Float4 &wwww, Float &lodBias, Vector4f &dsx, Vector4f &dsy, bool bias, bool gradients, bool lodProvided)
+	void SamplerCore::computeLod3D(Pointer<Byte> &texture, Float &lod, Float4 &uuuu, Float4 &vvvv, Float4 &wwww, const Float &lodBias, Vector4f &dsx, Vector4f &dsy, bool bias, bool gradients, bool lodProvided)
 	{
 		if(state.mipmapFilter == MIPMAP_NONE)
 		{
@@ -1662,7 +1662,7 @@
 			c.x.z = *Pointer<Float>(buffer[f2] + index[2] * 4);
 			c.x.w = *Pointer<Float>(buffer[f3] + index[3] * 4);
 
-			if(state.textureFormat == FORMAT_D32F_SHADOW && state.textureFilter != FILTER_GATHER)
+			if(state.textureFormat == FORMAT_D32FS8_SHADOW && state.textureFilter != FILTER_GATHER)
 			{
 				Float4 d = Min(Max(z, Float4(0.0f)), Float4(1.0f));
 
@@ -1698,13 +1698,13 @@
 
 		if(state.textureType != TEXTURE_CUBE)
 		{
-			buffer[0] = *Pointer<Pointer<Byte>>(mipmap + OFFSET(Mipmap,buffer[0]));
+			buffer[0] = *Pointer<Pointer<Byte> >(mipmap + OFFSET(Mipmap,buffer[0]));
 		}
 		else
 		{
 			for(int i = 0; i < 4; i++)
 			{
-				buffer[i] = *Pointer<Pointer<Byte>>(mipmap + OFFSET(Mipmap,buffer) + face[i] * sizeof(void*));
+				buffer[i] = *Pointer<Pointer<Byte> >(mipmap + OFFSET(Mipmap,buffer) + face[i] * sizeof(void*));
 			}
 		}
 	}
@@ -1824,8 +1824,8 @@
 		case FORMAT_L8:
 		case FORMAT_A8L8:
 		case FORMAT_D32F_LOCKABLE:
-		case FORMAT_D32F_TEXTURE:
-		case FORMAT_D32F_SHADOW:
+		case FORMAT_D32FS8_TEXTURE:
+		case FORMAT_D32FS8_SHADOW:
 			return false;
 		case FORMAT_L16:
 		case FORMAT_G16R16:
@@ -1845,28 +1845,28 @@
 	{
 		switch(state.textureFormat)
 		{
-		case FORMAT_G8R8:          return component < 2;
-		case FORMAT_X8R8G8B8:      return component < 3;
-		case FORMAT_A8R8G8B8:      return component < 3;
-		case FORMAT_V8U8:          return false;
-		case FORMAT_Q8W8V8U8:      return false;
-		case FORMAT_X8L8V8U8:      return false;
-		case FORMAT_R32F:          return component < 1;
-		case FORMAT_G32R32F:       return component < 2;
-		case FORMAT_A32B32G32R32F: return component < 3;
-		case FORMAT_A8:            return false;
-		case FORMAT_R8:            return component < 1;
-		case FORMAT_L8:            return component < 1;
-		case FORMAT_A8L8:          return component < 1;
-		case FORMAT_D32F_LOCKABLE: return false;
-		case FORMAT_D32F_TEXTURE:  return false;
-		case FORMAT_D32F_SHADOW:   return false;
-		case FORMAT_L16:           return component < 1;
-		case FORMAT_G16R16:        return component < 2;
-		case FORMAT_A16B16G16R16:  return component < 3;
-		case FORMAT_V16U16:        return false;
-		case FORMAT_A16W16V16U16:  return false;
-		case FORMAT_Q16W16V16U16:  return false;
+		case FORMAT_G8R8:           return component < 2;
+		case FORMAT_X8R8G8B8:       return component < 3;
+		case FORMAT_A8R8G8B8:       return component < 3;
+		case FORMAT_V8U8:           return false;
+		case FORMAT_Q8W8V8U8:       return false;
+		case FORMAT_X8L8V8U8:       return false;
+		case FORMAT_R32F:           return component < 1;
+		case FORMAT_G32R32F:        return component < 2;
+		case FORMAT_A32B32G32R32F:  return component < 3;
+		case FORMAT_A8:             return false;
+		case FORMAT_R8:             return component < 1;
+		case FORMAT_L8:             return component < 1;
+		case FORMAT_A8L8:           return component < 1;
+		case FORMAT_D32F_LOCKABLE:  return false;
+		case FORMAT_D32FS8_TEXTURE: return false;
+		case FORMAT_D32FS8_SHADOW:  return false;
+		case FORMAT_L16:            return component < 1;
+		case FORMAT_G16R16:         return component < 2;
+		case FORMAT_A16B16G16R16:   return component < 3;
+		case FORMAT_V16U16:         return false;
+		case FORMAT_A16W16V16U16:   return false;
+		case FORMAT_Q16W16V16U16:   return false;
 		default:
 			ASSERT(false);
 		}
diff --git a/src/Shader/SamplerCore.hpp b/src/Shader/SamplerCore.hpp
index c03e30c..656851d 100644
--- a/src/Shader/SamplerCore.hpp
+++ b/src/Shader/SamplerCore.hpp
@@ -39,8 +39,8 @@
 		void sampleFloat(Pointer<Byte> &texture, Vector4f &c, Float4 &u, Float4 &v, Float4 &w, Float &lod, Int face[4], bool secondLOD);

 		void sampleFloat2D(Pointer<Byte> &texture, Vector4f &c, Float4 &u, Float4 &v, Float4 &z, Float &lod, Int face[4], bool secondLOD);

 		void sampleFloat3D(Pointer<Byte> &texture, Vector4f &c, Float4 &u, Float4 &v, Float4 &w, Float &lod, bool secondLOD);

-		void computeLod(Pointer<Byte> &texture, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, Float4 &u, Float4 &v, Float &lodBias, Vector4f &dsx, Vector4f &dsy, bool bias, bool gradients, bool lodProvided);

-		void computeLod3D(Pointer<Byte> &texture, Float &lod, Float4 &u, Float4 &v, Float4 &w, Float &lodBias, Vector4f &dsx, Vector4f &dsy, bool bias, bool gradients, bool lodProvided);

+		void computeLod(Pointer<Byte> &texture, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, Float4 &u, Float4 &v, const Float &lodBias, Vector4f &dsx, Vector4f &dsy, bool bias, bool gradients, bool lodProvided);

+		void computeLod3D(Pointer<Byte> &texture, Float &lod, Float4 &u, Float4 &v, Float4 &w, const Float &lodBias, Vector4f &dsx, Vector4f &dsy, bool bias, bool gradients, bool lodProvided);

 		void cubeFace(Int face[4], Float4 &U, Float4 &V, Float4 &lodU, Float4 &lodV, Float4 &x, Float4 &y, Float4 &z);

 		void computeIndices(Int index[4], Short4 uuuu, Short4 vvvv, Short4 wwww, const Pointer<Byte> &mipmap);

 		void sampleTexel(Vector4i &c, Short4 &u, Short4 &v, Short4 &s, Pointer<Byte> &mipmap, Pointer<Byte> buffer[4]);

diff --git a/src/Shader/SetupRoutine.cpp b/src/Shader/SetupRoutine.cpp
index d77a21e..8c5835c 100644
--- a/src/Shader/SetupRoutine.cpp
+++ b/src/Shader/SetupRoutine.cpp
@@ -33,14 +33,14 @@
 
 	void SetupRoutine::generate()
 	{
-		Function<Bool, Pointer<Byte>, Pointer<Byte>, Pointer<Byte>, Pointer<Byte>> function;
+		Function<Bool, Pointer<Byte>, Pointer<Byte>, Pointer<Byte>, Pointer<Byte> > function;
 		{
 			Pointer<Byte> primitive(function.arg(0));
 			Pointer<Byte> tri(function.arg(1));
 			Pointer<Byte> polygon(function.arg(2));
 			Pointer<Byte> data(function.arg(3));
 
-			Pointer<Byte> constants = *Pointer<Pointer<Byte>>(data + OFFSET(DrawData,constants));
+			Pointer<Byte> constants = *Pointer<Pointer<Byte> >(data + OFFSET(DrawData,constants));
 
 			const bool point = state.isDrawPoint;
 			const bool sprite = state.pointSprite;
@@ -84,7 +84,7 @@
 
 				Float A = (y2 - y0) * x1 + (y1 - y2) * x0 + (y0 - y1) * x2;   // Area
 
-				If(A == Float(0.0f))
+				If(A == 0.0f)
 				{
 					Return(false);
 				}
@@ -93,22 +93,22 @@
 							   *Pointer<Float>(v1 + pos * 16 + 12) *
 							   *Pointer<Float>(v2 + pos * 16 + 12);
 
-				A = IfThenElse(w0w1w2 < Float(0.0f), -A, A);
+				A = IfThenElse(w0w1w2 < 0.0f, -A, A);
 
 				if(state.cullMode == Context::CULL_CLOCKWISE)
 				{
-					If(A >= Float(0.0f)) Return(false);
+					If(A >= 0.0f) Return(false);
 				}
 				else if(state.cullMode == Context::CULL_COUNTERCLOCKWISE)
 				{
-					If(A <= Float(0.0f)) Return(false);
+					If(A <= 0.0f) Return(false);
 				}
 
-				d = IfThenElse(A < Float(0.0f), d, Int(0));
+				d = IfThenElse(A < 0.0f, d, Int(0));
 
 				if(state.twoSidedStencil)
 				{
-					If(A > Float(0.0f))
+					If(A > 0.0f)
 					{
 						*Pointer<Byte8>(primitive + OFFSET(Primitive,clockwiseMask)) =  Byte8(0xFFFFFFFFFFFFFFFF);
 						*Pointer<Byte8>(primitive + OFFSET(Primitive,invClockwiseMask)) = Byte8(0x0000000000000000);
@@ -122,7 +122,7 @@
 
 				if(state.vFace)
 				{
-					*Pointer<Float>(primitive + OFFSET(Primitive,area)) = Float(0.5f) * A;
+					*Pointer<Float>(primitive + OFFSET(Primitive,area)) = 0.5f * A;
 				}
 			}
 			else
@@ -145,11 +145,11 @@
 
 				Do
 				{
-					Pointer<Float4> p = *Pointer<Pointer<Float4>>(V + i * sizeof(void*));
+					Pointer<Float4> p = *Pointer<Pointer<Float4> >(V + i * sizeof(void*));
 					Float4 v = *Pointer<Float4>(p, 16);
 
 					Float w = v.w;
-					Float rhw = IfThenElse(w != Float(0.0f), Float(1.0f) / w, Float(1.0f));
+					Float rhw = IfThenElse(w != 0.0f, 1.0f / w, Float(1.0f));
 
 					X[i] = RoundInt(*Pointer<Float>(data + OFFSET(DrawData,X0x16)) + v.x * rhw * *Pointer<Float>(data + OFFSET(DrawData,Wx16)));
 					Y[i] = RoundInt(*Pointer<Float>(data + OFFSET(DrawData,Y0x16)) + v.y * rhw * *Pointer<Float>(data + OFFSET(DrawData,Hx16)));
@@ -238,7 +238,7 @@
 
 					Do
 					{
-						edge(primitive, data, Int(Xq[i + 1 - d]), Int(Yq[i + 1 - d]), Int(Xq[i + d]), Int(Yq[i + d]), q);
+						edge(primitive, data, Xq[i + 1 - d], Yq[i + 1 - d], Xq[i + d], Yq[i + d], q);
 
 						i++;
 					}
@@ -329,8 +329,8 @@
 				Y2 = Y1 + X0 - X1;
 			}
 
-			Float dx = Float(X0) * Float(1.0f / 16.0f);
-			Float dy = Float(Y0) * Float(1.0f / 16.0f);
+			Float dx = Float(X0) * (1.0f / 16.0f);
+			Float dy = Float(Y0) * (1.0f / 16.0f);
 
 			X1 -= X0;
 			Y1 -= Y0;
@@ -338,11 +338,11 @@
 			X2 -= X0;
 			Y2 -= Y0;
 
-			Float x1 = w1 * Float(1.0f / 16.0f) * Float(X1);
-			Float y1 = w1 * Float(1.0f / 16.0f) * Float(Y1);
+			Float x1 = w1 * (1.0f / 16.0f) * Float(X1);
+			Float y1 = w1 * (1.0f / 16.0f) * Float(Y1);
 
-			Float x2 = w2 * Float(1.0f / 16.0f) * Float(X2);
-			Float y2 = w2 * Float(1.0f / 16.0f) * Float(Y2);
+			Float x2 = w2 * (1.0f / 16.0f) * Float(X2);
+			Float y2 = w2 * (1.0f / 16.0f) * Float(Y2);
 
 			Float a = x1 * y2 - x2 * y1;
 
@@ -360,9 +360,9 @@
 
 			M[0].z = rhw0;
 
-			If(a != Float(0.0f))
+			If(a != 0.0f)
 			{
-				Float A = Float(1) / a;
+				Float A = 1.0f / a;
 				Float D = A * rhw0;
 
 				M[0].x = (y1 * w2 - y2 * w1) * D;
@@ -409,10 +409,10 @@
 
 				if(!point)
 				{
-					Float x1 = Float(X1) * Float(1.0f / 16.0f);
-					Float y1 = Float(Y1) * Float(1.0f / 16.0f);
-					Float x2 = Float(X2) * Float(1.0f / 16.0f);
-					Float y2 = Float(Y2) * Float(1.0f / 16.0f);
+					Float x1 = Float(X1) * (1.0f / 16.0f);
+					Float y1 = Float(Y1) * (1.0f / 16.0f);
+					Float x2 = Float(X2) * (1.0f / 16.0f);
+					Float y2 = Float(Y2) * (1.0f / 16.0f);
 
 					Float D = *Pointer<Float>(data + OFFSET(DrawData,depthRange)) / (x1 * y2 - x2 * y1);
 
@@ -489,20 +489,20 @@
 			}
 			else
 			{
-				if(component == 0) i.x = Float(0.5f);
-				if(component == 1) i.x = Float(0.5f);
-				if(component == 2) i.x = Float(0.0f);
-				if(component == 3) i.x = Float(1.0f);
+				if(component == 0) i.x = 0.5f;
+				if(component == 1) i.x = 0.5f;
+				if(component == 2) i.x = 0.0f;
+				if(component == 3) i.x = 1.0f;
 
-				if(component == 0) i.y = Float(1.0f);
-				if(component == 1) i.y = Float(0.5f);
-				if(component == 2) i.y = Float(0.0f);
-				if(component == 3) i.y = Float(1.0f);
+				if(component == 0) i.y = 1.0f;
+				if(component == 1) i.y = 0.5f;
+				if(component == 2) i.y = 0.0f;
+				if(component == 3) i.y = 1.0f;
 
-				if(component == 0) i.z = Float(0.5f);
-				if(component == 1) i.z = Float(1.0f);
-				if(component == 2) i.z = Float(0.0f);
-				if(component == 3) i.z = Float(1.0f);
+				if(component == 0) i.z = 0.5f;
+				if(component == 1) i.z = 1.0f;
+				if(component == 2) i.z = 0.0f;
+				if(component == 3) i.z = 1.0f;
 				
 				i.w = 0;
 			}
@@ -514,12 +514,12 @@
 				m = *Pointer<Float>(v0 + attribute);
 				m = Max(m, *Pointer<Float>(v1 + attribute));
 				m = Max(m, *Pointer<Float>(v2 + attribute));
-				m -= Float(0.5f);   // FIXME: m -= 0.5f;
+				m -= 0.5f;
 
 				// FIXME: Vectorize
-				If(Float(i.x) < m) i.x = Float(i.x) + Float(1.0f);
-				If(Float(i.y) < m) i.y = Float(i.y) + Float(1.0f);
-				If(Float(i.z) < m) i.z = Float(i.z) + Float(1.0f);
+				If(Float(i.x) < m) i.x = i.x + 1.0f;
+				If(Float(i.y) < m) i.y = i.y + 1.0f;
+				If(Float(i.z) < m) i.z = i.z + 1.0f;
 			}
 
 			if(!perspective)
@@ -551,7 +551,7 @@
 		}
 	}
 
-	void SetupRoutine::edge(Pointer<Byte> &primitive, Pointer<Byte> &data, Int &X1, Int &Y1, Int &X2, Int &Y2, Int &q)
+	void SetupRoutine::edge(Pointer<Byte> &primitive, Pointer<Byte> &data, const Int &X1, const Int &Y1, const Int &X2, const Int &Y2, Int &q)
 	{
 		If(Y1 != Y2)
 		{
diff --git a/src/Shader/SetupRoutine.hpp b/src/Shader/SetupRoutine.hpp
index dd280c7..ea89a04b 100644
--- a/src/Shader/SetupRoutine.hpp
+++ b/src/Shader/SetupRoutine.hpp
@@ -31,7 +31,7 @@
 

 	private:

 		void setupGradient(Pointer<Byte> &primitive, Pointer<Byte> &triangle, Float4 &w012, Float4 (&m)[3], Pointer<Byte> &v0, Pointer<Byte> &v1, Pointer<Byte> &v2, int attribute, int planeEquation, bool flatShading, bool sprite, bool perspective, bool wrap, int component);

-		void edge(Pointer<Byte> &primitive, Pointer<Byte> &data, Int &X1, Int &Y1, Int &X2, Int &Y2, Int &q);

+		void edge(Pointer<Byte> &primitive, Pointer<Byte> &data, const Int &X1, const Int &Y1, const Int &X2, const Int &Y2, Int &q);

 		void conditionalRotate1(Bool condition, Pointer<Byte> &v0, Pointer<Byte> &v1, Pointer<Byte> &v2);

 		void conditionalRotate2(Bool condition, Pointer<Byte> &v0, Pointer<Byte> &v1, Pointer<Byte> &v2);

 

diff --git a/src/Shader/Shader.cpp b/src/Shader/Shader.cpp
index fcbc839..e08b786 100644
--- a/src/Shader/Shader.cpp
+++ b/src/Shader/Shader.cpp
@@ -18,6 +18,7 @@
 
 #include <fstream>
 #include <sstream>
+#include <stdarg.h>
 
 namespace sw
 {
diff --git a/src/Shader/Shader.hpp b/src/Shader/Shader.hpp
index b08b56e..7d47553 100644
--- a/src/Shader/Shader.hpp
+++ b/src/Shader/Shader.hpp
@@ -317,19 +317,6 @@
 			ANALYSIS_LEAVE    = 0x00000008,

 		};

 

-		struct RelativeAddress

-		{

-			RelativeAddress() : type(PARAMETER_VOID), index(0), swizzle(0), scale(1), deterministic(false)

-			{

-			}

-

-			ParameterType type : 8;

-			unsigned int index;

-			unsigned int swizzle : 8;

-			unsigned int scale;

-			bool deterministic;   // Equal accross shader instances run in lockstep (e.g. unrollable loop couters)

-		};

-

 		struct Parameter

 		{

 			union

@@ -338,7 +325,14 @@
 				{

 					unsigned int index;   // For registers types

 

-					RelativeAddress rel;

+					struct

+					{

+						ParameterType type : 8;

+						unsigned int index;

+						unsigned int swizzle : 8;

+						unsigned int scale;

+						bool deterministic;   // Equal accross shader instances run in lockstep (e.g. unrollable loop couters)

+					} rel;

 				};

 

 				float value[4];       // For float constants

@@ -354,6 +348,11 @@
 

 			Parameter() : type(PARAMETER_VOID), index(0)

 			{

+				rel.type = PARAMETER_VOID;

+				rel.index = 0;

+				rel.swizzle = 0;

+				rel.scale = 1;

+				rel.deterministic = false;

 			}

 

 			std::string string(ShaderType shaderType, unsigned short version) const;

diff --git a/src/Shader/ShaderCore.hpp b/src/Shader/ShaderCore.hpp
index 6cb42f4..6225fe9 100644
--- a/src/Shader/ShaderCore.hpp
+++ b/src/Shader/ShaderCore.hpp
@@ -84,7 +84,7 @@
 	class Register

 	{

 	public:

-		Register(Reference<Float4> &x, Reference<Float4> &y, Reference<Float4> &z, Reference<Float4> &w) : x(x), y(y), z(z), w(w)

+		Register(const Reference<Float4> &x, const Reference<Float4> &y, const Reference<Float4> &z, const Reference<Float4> &w) : x(x), y(y), z(z), w(w)

 		{

 		}

 

diff --git a/src/Shader/VertexPipeline.cpp b/src/Shader/VertexPipeline.cpp
index 1e39ca7..c5c8bb1 100644
--- a/src/Shader/VertexPipeline.cpp
+++ b/src/Shader/VertexPipeline.cpp
@@ -32,7 +32,7 @@
 	{
 	}
 
-	Vector4f VertexPipeline::transformBlend(Registers &r, Register &src, Pointer<Byte> &matrix, bool homogeneous)
+	Vector4f VertexPipeline::transformBlend(Registers &r, const Register &src, const Pointer<Byte> &matrix, bool homogeneous)
 	{
 		Vector4f dst;
 
@@ -62,20 +62,20 @@
 					case 3: indices = As<UInt>(Float(B.w)); break;
 					}
 
-					index0[i] = (indices & UInt(0x000000FF)) << UInt(6);   // FIXME: (indices & 0x000000FF) << 6
-					index1[i] = (indices & UInt(0x0000FF00)) >> UInt(2);
-					index2[i] = (indices & UInt(0x00FF0000)) >> UInt(10);
-					index3[i] = (indices & UInt(0xFF000000)) >> UInt(18);
+					index0[i] = (indices & 0x000000FF) << 6;
+					index1[i] = (indices & 0x0000FF00) >> 2;
+					index2[i] = (indices & 0x00FF0000) >> 10;
+					index3[i] = (indices & 0xFF000000) >> 18;
 				}
 			}
 			else
 			{
 				for(int i = 0; i < 4; i++)
 				{
-					index0[i] = Int(0 * 64);   // FIXME: index0 = 0 * 64;
-					index1[i] = Int(1 * 64);   // FIXME: index1 = 1 * 64;
-					index2[i] = Int(2 * 64);   // FIXME: index2 = 2 * 64;
-					index3[i] = Int(3 * 64);   // FIXME: index3 = 3 * 64;
+					index0[i] = 0 * 64;
+					index1[i] = 1 * 64;
+					index2[i] = 2 * 64;
+					index3[i] = 3 * 64;
 				}
 			}
 
@@ -785,9 +785,9 @@
 			return;   // Use global pointsize
 		}
 
-		if(state.input[PSize])
+		if(state.input[PointSize])
 		{
-			r.o[Pts].y = r.v[PSize].x;
+			r.o[Pts].y = r.v[PointSize].x;
 		}
 		else
 		{
@@ -810,7 +810,7 @@
 		}
 	}
 
-	Vector4f VertexPipeline::transform(Register &src, Pointer<Byte> &matrix, bool homogeneous)
+	Vector4f VertexPipeline::transform(const Register &src, const Pointer<Byte> &matrix, bool homogeneous)
 	{
 		Vector4f dst;
 
@@ -857,7 +857,7 @@
 		return dst;
 	}
 
-	Vector4f VertexPipeline::transform(Register &src, Pointer<Byte> &matrix, UInt index[4], bool homogeneous)
+	Vector4f VertexPipeline::transform(const Register &src, const Pointer<Byte> &matrix, UInt index[4], bool homogeneous)
 	{
 		Vector4f dst;
 
diff --git a/src/Shader/VertexPipeline.hpp b/src/Shader/VertexPipeline.hpp
index bfc3093..24b8a25 100644
--- a/src/Shader/VertexPipeline.hpp
+++ b/src/Shader/VertexPipeline.hpp
@@ -31,9 +31,9 @@
 		void processTextureCoordinate(Registers &r, int stage, Vector4f &normal, Vector4f &position);

 		void processPointSize(Registers &r);

 

-		Vector4f transformBlend(Registers &r, Register &src, Pointer<Byte> &matrix, bool homogenous);

-		Vector4f transform(Register &src, Pointer<Byte> &matrix, bool homogenous);

-		Vector4f transform(Register &src, Pointer<Byte> &matrix, UInt index[4], bool homogenous);

+		Vector4f transformBlend(Registers &r, const Register &src, const Pointer<Byte> &matrix, bool homogenous);

+		Vector4f transform(const Register &src, const Pointer<Byte> &matrix, bool homogenous);

+		Vector4f transform(const Register &src, const Pointer<Byte> &matrix, UInt index[4], bool homogenous);

 		Vector4f normalize(Vector4f &src);

 		Float4 power(Float4 &src0, Float4 &src1);

 	};

diff --git a/src/Shader/VertexProgram.cpp b/src/Shader/VertexProgram.cpp
index a292a8e..0d93220 100644
--- a/src/Shader/VertexProgram.cpp
+++ b/src/Shader/VertexProgram.cpp
@@ -587,7 +587,7 @@
 				r.o[T0 + i].w = r.v[TexCoord0 + i].w;
 			}
 
-			r.o[Pts].y = r.v[PSize].x;
+			r.o[Pts].y = r.v[PointSize].x;
 		}
 	}
 
@@ -658,64 +658,44 @@
 			ASSERT(false);
 		}
 
-		Vector4f mod;
+		const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
+		const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
+		const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
+		const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
 
-		mod.x = reg[(src.swizzle >> 0) & 0x03];
-		mod.y = reg[(src.swizzle >> 2) & 0x03];
-		mod.z = reg[(src.swizzle >> 4) & 0x03];
-		mod.w = reg[(src.swizzle >> 6) & 0x03];
+		Vector4f mod;
 
 		switch(src.modifier)
 		{
 		case Shader::MODIFIER_NONE:
+			mod.x = x;
+			mod.y = y;
+			mod.z = z;
+			mod.w = w;
 			break;
 		case Shader::MODIFIER_NEGATE:
-			mod.x = -mod.x;
-			mod.y = -mod.y;
-			mod.z = -mod.z;
-			mod.w = -mod.w;
-			break;
-		case Shader::MODIFIER_BIAS:
-			ASSERT(false);   // NOTE: Unimplemented
-			break;
-		case Shader::MODIFIER_BIAS_NEGATE:
-			ASSERT(false);   // NOTE: Unimplemented
-			break;
-		case Shader::MODIFIER_SIGN:
-			ASSERT(false);   // NOTE: Unimplemented
-			break;
-		case Shader::MODIFIER_SIGN_NEGATE:
-			ASSERT(false);   // NOTE: Unimplemented
-			break;
-		case Shader::MODIFIER_COMPLEMENT:
-			ASSERT(false);   // NOTE: Unimplemented
-			break;
-		case Shader::MODIFIER_X2:
-			ASSERT(false);   // NOTE: Unimplemented
-			break;
-		case Shader::MODIFIER_X2_NEGATE:
-			ASSERT(false);   // NOTE: Unimplemented
-			break;
-		case Shader::MODIFIER_DZ:
-			ASSERT(false);   // NOTE: Unimplemented
-			break;
-		case Shader::MODIFIER_DW:
-			ASSERT(false);   // NOTE: Unimplemented
+			mod.x = -x;
+			mod.y = -y;
+			mod.z = -z;
+			mod.w = -w;
 			break;
 		case Shader::MODIFIER_ABS:
-			mod.x = Abs(mod.x);
-			mod.y = Abs(mod.y);
-			mod.z = Abs(mod.z);
-			mod.w = Abs(mod.w);
+			mod.x = Abs(x);
+			mod.y = Abs(y);
+			mod.z = Abs(z);
+			mod.w = Abs(w);
 			break;
 		case Shader::MODIFIER_ABS_NEGATE:
-			mod.x = -Abs(mod.x);
-			mod.y = -Abs(mod.y);
-			mod.z = -Abs(mod.z);
-			mod.w = -Abs(mod.w);
+			mod.x = -Abs(x);
+			mod.y = -Abs(y);
+			mod.z = -Abs(z);
+			mod.w = -Abs(w);
 			break;
 		case Shader::MODIFIER_NOT:
-			UNIMPLEMENTED();
+			mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
+			mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
+			mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
+			mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
 			break;
 		default:
 			ASSERT(false);
@@ -795,7 +775,7 @@
 				case Shader::PARAMETER_TEMP:   a = r.r[src.rel.index][component]; break;
 				case Shader::PARAMETER_INPUT:  a = r.v[src.rel.index][component]; break;
 				case Shader::PARAMETER_OUTPUT: a = r.o[src.rel.index][component]; break;
-				case Shader::PARAMETER_CONST:  a = Float4(*Pointer<Float>(r.data + OFFSET(DrawData,vs.c[src.rel.index][component]))); break;
+				case Shader::PARAMETER_CONST:  a = *Pointer<Float>(r.data + OFFSET(DrawData,vs.c[src.rel.index][component])); break;
 				default: ASSERT(false);
 				}
 
@@ -1370,7 +1350,7 @@
 		Nucleus::setInsertBlock(testBlock);
 		r.enableContinue = restoreContinue;
 
-		Vector4f &src = reg(r, temporaryRegister);
+		const Vector4f &src = reg(r, temporaryRegister);
 		Int4 condition = As<Int4>(src.x);
 		condition &= r.enableStack[r.enableIndex - 1];
 		r.enableStack[r.enableIndex] = condition;
@@ -1403,7 +1383,7 @@
 				// FIXME: Encapsulate
 				UInt index = r.callStack[--r.stackIndex];
  
-				llvm::Value *value = Nucleus::createLoad(index.address);
+				llvm::Value *value = index.loadValue();
 				llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
 
 				for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
diff --git a/src/Shader/VertexProgram.hpp b/src/Shader/VertexProgram.hpp
index 7423e5b..902cf75 100644
--- a/src/Shader/VertexProgram.hpp
+++ b/src/Shader/VertexProgram.hpp
@@ -1,104 +1,104 @@
-// SwiftShader Software Renderer

-//

-// Copyright(c) 2005-2012 TransGaming Inc.

-//

-// All rights reserved. No part of this software may be copied, distributed, transmitted,

-// transcribed, stored in a retrieval system, translated into any human or computer

-// language by any means, or disclosed to third parties without the explicit written

-// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express

-// or implied, including but not limited to any patent rights, are granted to you.

-//

-

-#ifndef sw_VertexProgram_hpp

-#define sw_VertexProgram_hpp

-

-#include "VertexRoutine.hpp"

-#include "ShaderCore.hpp"

-

-#include "Stream.hpp"

-#include "Types.hpp"

-

-namespace sw

-{

-	struct Stream;

-	class VertexShader;

-	class SamplerCore;

-

-	class VertexProgram : public VertexRoutine, public ShaderCore

-	{

-	public:

-		VertexProgram(const VertexProcessor::State &state, const VertexShader *vertexShader);

-

-		virtual ~VertexProgram();

-

-	private:

-		typedef Shader::DestinationParameter Dst;

-		typedef Shader::SourceParameter Src;

-		typedef Shader::Control Control;

-		typedef Shader::Usage Usage;

-

-		void pipeline(Registers &r);

-		void program(Registers &r);

-		void passThrough(Registers &r);

-

-		Vector4f reg(Registers &r, const Src &src, int offset = 0);

-		Vector4f readConstant(Registers &r, const Src &src, int offset = 0);

-		Int relativeAddress(Registers &r, const Shader::Parameter &var);

-		Int4 enableMask(Registers &r, const Shader::Instruction *instruction);

-

-		void M3X2(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1);

-		void M3X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1);

-		void M3X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1);

-		void M4X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1);

-		void M4X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1);

-		void BREAK(Registers &r);

-		void BREAKC(Registers &r, Vector4f &src0, Vector4f &src1, Control);

-		void BREAKP(Registers &r, const Src &predicateRegister);

-		void BREAK(Registers &r, Int4 &condition);

-		void CONTINUE(Registers &r);

-		void TEST();

-		void CALL(Registers &r, int labelIndex, int callSiteIndex);

-		void CALLNZ(Registers &r, int labelIndex, int callSiteIndex, const Src &src);

-		void CALLNZb(Registers &r, int labelIndex, int callSiteIndex, const Src &boolRegister);

-		void CALLNZp(Registers &r, int labelIndex, int callSiteIndex, const Src &predicateRegister);

-		void ELSE(Registers &r);

-		void ENDIF(Registers &r);

-		void ENDLOOP(Registers &r);

-		void ENDREP(Registers &r);

-		void ENDWHILE(Registers &r);

-		void IF(Registers &r, const Src &src);

-		void IFb(Registers &r, const Src &boolRegister);

-		void IFp(Registers &r, const Src &predicateRegister);

-		void IFC(Registers &r, Vector4f &src0, Vector4f &src1, Control);

-		void IF(Registers &r, Int4 &condition);

-		void LABEL(int labelIndex);

-		void LOOP(Registers &r, const Src &integerRegister);

-		void REP(Registers &r, const Src &integerRegister);

-		void WHILE(Registers &r, const Src &temporaryRegister);

-		void RET(Registers &r);

-		void LEAVE(Registers &r);

-		void TEXLDL(Registers &r, Vector4f &dst, Vector4f &src, const Src&);

-		void TEX(Registers &r, Vector4f &dst, Vector4f &src, const Src&);

-

-		void sampleTexture(Registers &r, Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q);

-

-		SamplerCore *sampler[4];

-

-		int ifDepth;

-		int loopRepDepth;

-		int breakDepth;

-		int currentLabel;

-		bool whileTest;

-

-		// FIXME: Get rid of llvm::

-		llvm::BasicBlock *ifFalseBlock[24 + 24];

-		llvm::BasicBlock *loopRepTestBlock[4];

-		llvm::BasicBlock *loopRepEndBlock[4];

-		llvm::BasicBlock *labelBlock[2048];

-		std::vector<llvm::BasicBlock*> callRetBlock[2048];

-		llvm::BasicBlock *returnBlock;

-		bool isConditionalIf[24 + 24];

-	};

-}

-

-#endif   // sw_VertexProgram_hpp

+// SwiftShader Software Renderer
+//
+// Copyright(c) 2005-2012 TransGaming Inc.
+//
+// All rights reserved. No part of this software may be copied, distributed, transmitted,
+// transcribed, stored in a retrieval system, translated into any human or computer
+// language by any means, or disclosed to third parties without the explicit written
+// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
+// or implied, including but not limited to any patent rights, are granted to you.
+//
+
+#ifndef sw_VertexProgram_hpp
+#define sw_VertexProgram_hpp
+
+#include "VertexRoutine.hpp"
+#include "ShaderCore.hpp"
+
+#include "Stream.hpp"
+#include "Types.hpp"
+
+namespace sw
+{
+	struct Stream;
+	class VertexShader;
+	class SamplerCore;
+
+	class VertexProgram : public VertexRoutine, public ShaderCore
+	{
+	public:
+		VertexProgram(const VertexProcessor::State &state, const VertexShader *vertexShader);
+
+		virtual ~VertexProgram();
+
+	private:
+		typedef Shader::DestinationParameter Dst;
+		typedef Shader::SourceParameter Src;
+		typedef Shader::Control Control;
+		typedef Shader::Usage Usage;
+
+		void pipeline(Registers &r);
+		void program(Registers &r);
+		void passThrough(Registers &r);
+
+		Vector4f reg(Registers &r, const Src &src, int offset = 0);
+		Vector4f readConstant(Registers &r, const Src &src, int offset = 0);
+		Int relativeAddress(Registers &r, const Shader::Parameter &var);
+		Int4 enableMask(Registers &r, const Shader::Instruction *instruction);
+
+		void M3X2(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1);
+		void M3X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1);
+		void M3X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1);
+		void M4X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1);
+		void M4X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1);
+		void BREAK(Registers &r);
+		void BREAKC(Registers &r, Vector4f &src0, Vector4f &src1, Control);
+		void BREAKP(Registers &r, const Src &predicateRegister);
+		void BREAK(Registers &r, Int4 &condition);
+		void CONTINUE(Registers &r);
+		void TEST();
+		void CALL(Registers &r, int labelIndex, int callSiteIndex);
+		void CALLNZ(Registers &r, int labelIndex, int callSiteIndex, const Src &src);
+		void CALLNZb(Registers &r, int labelIndex, int callSiteIndex, const Src &boolRegister);
+		void CALLNZp(Registers &r, int labelIndex, int callSiteIndex, const Src &predicateRegister);
+		void ELSE(Registers &r);
+		void ENDIF(Registers &r);
+		void ENDLOOP(Registers &r);
+		void ENDREP(Registers &r);
+		void ENDWHILE(Registers &r);
+		void IF(Registers &r, const Src &src);
+		void IFb(Registers &r, const Src &boolRegister);
+		void IFp(Registers &r, const Src &predicateRegister);
+		void IFC(Registers &r, Vector4f &src0, Vector4f &src1, Control);
+		void IF(Registers &r, Int4 &condition);
+		void LABEL(int labelIndex);
+		void LOOP(Registers &r, const Src &integerRegister);
+		void REP(Registers &r, const Src &integerRegister);
+		void WHILE(Registers &r, const Src &temporaryRegister);
+		void RET(Registers &r);
+		void LEAVE(Registers &r);
+		void TEXLDL(Registers &r, Vector4f &dst, Vector4f &src, const Src&);
+		void TEX(Registers &r, Vector4f &dst, Vector4f &src, const Src&);
+
+		void sampleTexture(Registers &r, Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q);
+
+		SamplerCore *sampler[4];
+
+		int ifDepth;
+		int loopRepDepth;
+		int breakDepth;
+		int currentLabel;
+		bool whileTest;
+
+		// FIXME: Get rid of llvm::
+		llvm::BasicBlock *ifFalseBlock[24 + 24];
+		llvm::BasicBlock *loopRepTestBlock[4];
+		llvm::BasicBlock *loopRepEndBlock[4];
+		llvm::BasicBlock *labelBlock[2048];
+		std::vector<llvm::BasicBlock*> callRetBlock[2048];
+		llvm::BasicBlock *returnBlock;
+		bool isConditionalIf[24 + 24];
+	};
+}
+
+#endif   // sw_VertexProgram_hpp
diff --git a/src/Shader/VertexRoutine.cpp b/src/Shader/VertexRoutine.cpp
index 1c2be07..0e21eee 100644
--- a/src/Shader/VertexRoutine.cpp
+++ b/src/Shader/VertexRoutine.cpp
@@ -34,7 +34,7 @@
 
 	void VertexRoutine::generate()
 	{
-		Function<Void, Pointer<Byte>, Pointer<Byte>, Pointer<Byte>, Pointer<Byte>> function;
+		Function<Void, Pointer<Byte>, Pointer<Byte>, Pointer<Byte>, Pointer<Byte> > function;
 		{
 			Pointer<Byte> vertex(function.arg(0));
 			Pointer<Byte> batch(function.arg(1));
@@ -51,13 +51,13 @@
 
 			Registers r(shader);
 			r.data = data;
-			r.constants = *Pointer<Pointer<Byte>>(data + OFFSET(DrawData,constants));
+			r.constants = *Pointer<Pointer<Byte> >(data + OFFSET(DrawData,constants));
 
 			Do
 			{
 				UInt index = *Pointer<UInt>(batch);
-				UInt tagIndex = index & UInt(0x0000003C);
-				UInt indexQ = !texldl ? index & UInt(0xFFFFFFFC) : index;   // FIXME: TEXLDL hack to have independent LODs, hurts performance.
+				UInt tagIndex = index & 0x0000003C;
+				UInt indexQ = !texldl ? UInt(index & 0xFFFFFFFC) : index;   // FIXME: TEXLDL hack to have independent LODs, hurts performance.
 
 				If(*Pointer<UInt>(tagCache + tagIndex) != indexQ)
 				{
@@ -72,7 +72,7 @@
 					writeCache(cacheLine0, r);
 				}
 
-				UInt cacheIndex = index & UInt(0x0000003F);
+				UInt cacheIndex = index & 0x0000003F;
 				Pointer<Byte> cacheLine = vertexCache + cacheIndex * UInt((int)sizeof(Vertex));
 				writeVertex(vertex, cacheLine);
 
@@ -80,7 +80,7 @@
 				batch += sizeof(unsigned int);
 				count--;
 			}
-			Until(count == UInt(0))
+			Until(count == 0)
 
 			Return();
 		}
@@ -97,7 +97,7 @@
 	{
 		for(int i = 0; i < 16; i++)
 		{
-			Pointer<Byte> input = *Pointer<Pointer<Byte>>(r.data + OFFSET(DrawData,input) + sizeof(void*) * i);
+			Pointer<Byte> input = *Pointer<Pointer<Byte> >(r.data + OFFSET(DrawData,input) + sizeof(void*) * i);
 			UInt stride = *Pointer<UInt>(r.data + OFFSET(DrawData,stride) + sizeof(unsigned int) * i);
 
 			r.v[i] = readStream(r, input, stride, state.input[i], index);
diff --git a/src/Shader/VertexRoutine.hpp b/src/Shader/VertexRoutine.hpp
index 72323f2..8947b06 100644
--- a/src/Shader/VertexRoutine.hpp
+++ b/src/Shader/VertexRoutine.hpp
@@ -1,105 +1,105 @@
-// SwiftShader Software Renderer

-//

-// Copyright(c) 2005-2012 TransGaming Inc.

-//

-// All rights reserved. No part of this software may be copied, distributed, transmitted,

-// transcribed, stored in a retrieval system, translated into any human or computer

-// language by any means, or disclosed to third parties without the explicit written

-// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express

-// or implied, including but not limited to any patent rights, are granted to you.

-//

-

-#ifndef sw_VertexRoutine_hpp

-#define sw_VertexRoutine_hpp

-

-#include "Renderer/Color.hpp"

-#include "Renderer/VertexProcessor.hpp"

-#include "ShaderCore.hpp"

-#include "VertexShader.hpp"

-

-namespace sw

-{

-	class VertexRoutine

-	{

-	protected:

-		struct Registers

-		{

-			Registers(const VertexShader *shader) :

-				r(shader && shader->dynamicallyIndexedTemporaries),

-				v(shader && shader->dynamicallyIndexedInput),

-				o(shader && shader->dynamicallyIndexedOutput)

-			{

-				loopDepth = -1;

-				enableStack[0] = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);

-				

-				if(shader && shader->containsBreakInstruction())

-				{

-					enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);

-				}

-

-				if(shader && shader->containsContinueInstruction())

-				{

-					enableContinue = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);

-				}

-

-				if(shader && shader->containsLeaveInstruction())

-				{

-					enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);

-				}

-			}

-

-			Pointer<Byte> data;

-			Pointer<Byte> constants;

-

-			Int clipFlags;

-

-			RegisterArray<16> v;

-			RegisterArray<4096> r;

-			RegisterArray<12> o;

-			Vector4f a0;

-			Array<Int, 4> aL;

-			Vector4f p0;

-

-			Array<Int, 4> increment;

-			Array<Int, 4> iteration;

-

-			Int loopDepth;

-			Int stackIndex;   // FIXME: Inc/decrement callStack

-			Array<UInt, 4> callStack;

-

-			Int enableIndex;

-			Array<Int4, 1 + 24> enableStack;

-			Int4 enableBreak;

-			Int4 enableContinue;

-			Int4 enableLeave;

-		};

-

-	public:

-		VertexRoutine(const VertexProcessor::State &state, const VertexShader *shader);

-

-		virtual ~VertexRoutine();

-

-		void generate();

-		Routine *getRoutine();

-

-	protected:

-		const VertexProcessor::State &state;

-		const VertexShader *const shader;

-

-	private:		

-		virtual void pipeline(Registers &r) = 0;

-

-		typedef VertexProcessor::State::Input Stream;

-		

-		Vector4f readStream(Registers &r, Pointer<Byte> &buffer, UInt &stride, const Stream &stream, const UInt &index);

-		void readInput(Registers &r, UInt &index);

-		void computeClipFlags(Registers &r);

-		void postTransform(Registers &r);

-		void writeCache(Pointer<Byte> &cacheLine, Registers &r);

-		void writeVertex(Pointer<Byte> &vertex, Pointer<Byte> &cacheLine);

-

-		Routine *routine;

-	};

-}

-

-#endif   // sw_VertexRoutine_hpp

+// SwiftShader Software Renderer
+//
+// Copyright(c) 2005-2012 TransGaming Inc.
+//
+// All rights reserved. No part of this software may be copied, distributed, transmitted,
+// transcribed, stored in a retrieval system, translated into any human or computer
+// language by any means, or disclosed to third parties without the explicit written
+// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
+// or implied, including but not limited to any patent rights, are granted to you.
+//
+
+#ifndef sw_VertexRoutine_hpp
+#define sw_VertexRoutine_hpp
+
+#include "Renderer/Color.hpp"
+#include "Renderer/VertexProcessor.hpp"
+#include "ShaderCore.hpp"
+#include "VertexShader.hpp"
+
+namespace sw
+{
+	class VertexRoutine
+	{
+	protected:
+		struct Registers
+		{
+			Registers(const VertexShader *shader) :
+				r(shader && shader->dynamicallyIndexedTemporaries),
+				v(shader && shader->dynamicallyIndexedInput),
+				o(shader && shader->dynamicallyIndexedOutput)
+			{
+				loopDepth = -1;
+				enableStack[0] = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
+				
+				if(shader && shader->containsBreakInstruction())
+				{
+					enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
+				}
+
+				if(shader && shader->containsContinueInstruction())
+				{
+					enableContinue = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
+				}
+
+				if(shader && shader->containsLeaveInstruction())
+				{
+					enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
+				}
+			}
+
+			Pointer<Byte> data;
+			Pointer<Byte> constants;
+
+			Int clipFlags;
+
+			RegisterArray<16> v;
+			RegisterArray<4096> r;
+			RegisterArray<12> o;
+			Vector4f a0;
+			Array<Int, 4> aL;
+			Vector4f p0;
+
+			Array<Int, 4> increment;
+			Array<Int, 4> iteration;
+
+			Int loopDepth;
+			Int stackIndex;   // FIXME: Inc/decrement callStack
+			Array<UInt, 4> callStack;
+
+			Int enableIndex;
+			Array<Int4, 1 + 24> enableStack;
+			Int4 enableBreak;
+			Int4 enableContinue;
+			Int4 enableLeave;
+		};
+
+	public:
+		VertexRoutine(const VertexProcessor::State &state, const VertexShader *shader);
+
+		virtual ~VertexRoutine();
+
+		void generate();
+		Routine *getRoutine();
+
+	protected:
+		const VertexProcessor::State &state;
+		const VertexShader *const shader;
+
+	private:		
+		virtual void pipeline(Registers &r) = 0;
+
+		typedef VertexProcessor::State::Input Stream;
+		
+		Vector4f readStream(Registers &r, Pointer<Byte> &buffer, UInt &stride, const Stream &stream, const UInt &index);
+		void readInput(Registers &r, UInt &index);
+		void computeClipFlags(Registers &r);
+		void postTransform(Registers &r);
+		void writeCache(Pointer<Byte> &cacheLine, Registers &r);
+		void writeVertex(Pointer<Byte> &vertex, Pointer<Byte> &cacheLine);
+
+		Routine *routine;
+	};
+}
+
+#endif   // sw_VertexRoutine_hpp
diff --git a/src/Shader/VertexShader.cpp b/src/Shader/VertexShader.cpp
index d5b1171..dcf73be 100644
--- a/src/Shader/VertexShader.cpp
+++ b/src/Shader/VertexShader.cpp
@@ -1,6 +1,6 @@
 // SwiftShader Software Renderer
 //
-// Copyright(c) 2005-2012 TransGaming Inc.
+// Copyright(c) 2005-2013 TransGaming Inc.
 //
 // All rights reserved. No part of this software may be copied, distributed, transmitted,
 // transcribed, stored in a retrieval system, translated into any human or computer
@@ -14,6 +14,8 @@
 #include "Vertex.hpp"
 #include "Debug.hpp"
 
+#include <string.h>
+
 namespace sw
 {
 	VertexShader::VertexShader(const VertexShader *vs) : Shader()
@@ -22,7 +24,7 @@
 		positionRegister = Pos;
 		pointSizeRegister = -1;   // No vertex point size
 
-		for(int i = 0; i < 16; i++)
+		for(int i = 0; i < MAX_INPUT_ATTRIBUTES; i++)
 		{
 			input[i] = Semantic(-1, -1);
 		}
@@ -51,7 +53,7 @@
 		positionRegister = Pos;
 		pointSizeRegister = -1;   // No vertex point size
 
-		for(int i = 0; i < 16; i++)
+		for(int i = 0; i < MAX_INPUT_ATTRIBUTES; i++)
 		{
 			input[i] = Semantic(-1, -1);
 		}
@@ -218,7 +220,7 @@
 					if(dst.x) output[T0 + dst.index][0] = Semantic(Shader::USAGE_TEXCOORD, dst.index);
 					if(dst.y) output[T0 + dst.index][1] = Semantic(Shader::USAGE_TEXCOORD, dst.index);
 					if(dst.z) output[T0 + dst.index][2] = Semantic(Shader::USAGE_TEXCOORD, dst.index);
-					if(dst.w) output[T0 + dst.index][3] = Semantic(Shader::USAGE_TEXCOORD, dst.index);	
+					if(dst.w) output[T0 + dst.index][3] = Semantic(Shader::USAGE_TEXCOORD, dst.index);
 					break;
 				default:
 					break;
diff --git a/src/Shader/VertexShader.hpp b/src/Shader/VertexShader.hpp
index 3bfc0f4..3d292c0 100644
--- a/src/Shader/VertexShader.hpp
+++ b/src/Shader/VertexShader.hpp
@@ -1,47 +1,50 @@
-// SwiftShader Software Renderer

-//

-// Copyright(c) 2005-2012 TransGaming Inc.

-//

-// All rights reserved. No part of this software may be copied, distributed, transmitted,

-// transcribed, stored in a retrieval system, translated into any human or computer

-// language by any means, or disclosed to third parties without the explicit written

-// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express

-// or implied, including but not limited to any patent rights, are granted to you.

-//

-

-#ifndef sw_VertexShader_hpp

-#define sw_VertexShader_hpp

-

-#include "Shader.hpp"

-

-namespace sw

-{

-	class VertexShader : public Shader

-	{

-	public:

-		explicit VertexShader(const VertexShader *vs = 0);

-		explicit VertexShader(const unsigned long *token);

-

-		virtual ~VertexShader();

-

-		static int validate(const unsigned long *const token);   // Returns number of instructions if valid

-		bool containsTexldl() const;

-		

-		virtual void analyze();

-

-		int positionRegister;     // FIXME: Private

-		int pointSizeRegister;    // FIXME: Private

-

-		Semantic input[16];       // FIXME: Private

-		Semantic output[12][4];   // FIXME: Private

-

-	private:

-		void analyzeInput();

-		void analyzeOutput();

-		void analyzeTexldl();

-

-		bool texldl;

-	};

-}

-

-#endif   // sw_VertexShader_hpp

+// SwiftShader Software Renderer
+//
+// Copyright(c) 2005-2013 TransGaming Inc.
+//
+// All rights reserved. No part of this software may be copied, distributed, transmitted,
+// transcribed, stored in a retrieval system, translated into any human or computer
+// language by any means, or disclosed to third parties without the explicit written
+// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
+// or implied, including but not limited to any patent rights, are granted to you.
+//
+
+#ifndef sw_VertexShader_hpp
+#define sw_VertexShader_hpp
+
+#include "Shader.hpp"
+
+namespace sw
+{
+	class VertexShader : public Shader
+	{
+	public:
+		explicit VertexShader(const VertexShader *vs = 0);
+		explicit VertexShader(const unsigned long *token);
+
+		virtual ~VertexShader();
+
+		static int validate(const unsigned long *const token);   // Returns number of instructions if valid
+		bool containsTexldl() const;
+		
+		virtual void analyze();
+
+		int positionRegister;     // FIXME: Private
+		int pointSizeRegister;    // FIXME: Private
+
+		enum {MAX_INPUT_ATTRIBUTES = 16};
+		Semantic input[MAX_INPUT_ATTRIBUTES];       // FIXME: Private
+
+		enum {MAX_OUTPUT_VARYINGS = 12};
+		Semantic output[MAX_OUTPUT_VARYINGS][4];   // FIXME: Private
+
+	private:
+		void analyzeInput();
+		void analyzeOutput();
+		void analyzeTexldl();
+
+		bool texldl;
+	};
+}
+
+#endif   // sw_VertexShader_hpp