Apply the Apache 2.0 license.

Change-Id: I4a7aeefedcd2d891093520d5a10ebefadcddb5be
Reviewed-on: https://swiftshader-review.googlesource.com/5320
Reviewed-by: Nicolas Capens <capn@google.com>
Tested-by: Nicolas Capens <capn@google.com>
diff --git a/src/Renderer/Blitter.cpp b/src/Renderer/Blitter.cpp
index 64ad6d0..95590ef 100644
--- a/src/Renderer/Blitter.cpp
+++ b/src/Renderer/Blitter.cpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2013 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #include "Blitter.hpp"
 
diff --git a/src/Renderer/Blitter.hpp b/src/Renderer/Blitter.hpp
index 25c837d..52d3cee 100644
--- a/src/Renderer/Blitter.hpp
+++ b/src/Renderer/Blitter.hpp
@@ -1,97 +1,100 @@
-// 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_Blitter_hpp

-#define sw_Blitter_hpp

-

-#include "Surface.hpp"

-#include "RoutineCache.hpp"

-#include "Reactor/Nucleus.hpp"

-

-#include <string.h>

-

-namespace sw

-{

-	class Blitter

-	{

-		enum Options : unsigned char

-		{

-			FILTER_POINT = 0x00,

-			WRITE_RED = 0x01,

-			WRITE_GREEN = 0x02,

-			WRITE_BLUE = 0x04,

-			WRITE_ALPHA = 0x08,

-			WRITE_RGBA = WRITE_RED | WRITE_GREEN | WRITE_BLUE | WRITE_ALPHA,

-			FILTER_LINEAR = 0x10,

-			CLEAR_OPERATION = 0x20

-		};

-

-		struct BlitState

-		{

-			bool operator==(const BlitState &state) const

-			{

-				return memcmp(this, &state, sizeof(BlitState)) == 0;

-			}

-

-			Format sourceFormat;

-			Format destFormat;

-			Blitter::Options options;

-		};

-

-		struct BlitData

-		{

-			void *source;

-			void *dest;

-			int sPitchB;

-			int dPitchB;

-

-			float x0;

-			float y0;

-			float w;

-			float h;

-

-			int y0d;

-			int y1d;

-			int x0d;

-			int x1d;

-

-			int sWidth;

-			int sHeight;

-		};

-

-	public:

-		Blitter();

-

-		virtual ~Blitter();

-

-		void clear(void* pixel, sw::Format format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask);

-		void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter);

-		void blit3D(Surface *source, Surface *dest);

-

-	private:

-		bool read(Float4 &color, Pointer<Byte> element, Format format);

-		bool write(Float4 &color, Pointer<Byte> element, Format format, const Blitter::Options& options);

-		bool read(Int4 &color, Pointer<Byte> element, Format format);

-		bool write(Int4 &color, Pointer<Byte> element, Format format, const Blitter::Options& options);

-		static bool GetScale(float4& scale, Format format);

-		static bool ApplyScaleAndClamp(Float4& value, const BlitState& state);

-		void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, const Blitter::Options& options);

-		bool blitReactor(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, const Blitter::Options& options);

-		Routine *generate(BlitState &state);

-

-		RoutineCache<BlitState> *blitCache;

-		BackoffLock criticalSection;

-	};

-

-	extern Blitter blitter;

-}

-

-#endif   // sw_Blitter_hpp

+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef sw_Blitter_hpp
+#define sw_Blitter_hpp
+
+#include "Surface.hpp"
+#include "RoutineCache.hpp"
+#include "Reactor/Nucleus.hpp"
+
+#include <string.h>
+
+namespace sw
+{
+	class Blitter
+	{
+		enum Options : unsigned char
+		{
+			FILTER_POINT = 0x00,
+			WRITE_RED = 0x01,
+			WRITE_GREEN = 0x02,
+			WRITE_BLUE = 0x04,
+			WRITE_ALPHA = 0x08,
+			WRITE_RGBA = WRITE_RED | WRITE_GREEN | WRITE_BLUE | WRITE_ALPHA,
+			FILTER_LINEAR = 0x10,
+			CLEAR_OPERATION = 0x20
+		};
+
+		struct BlitState
+		{
+			bool operator==(const BlitState &state) const
+			{
+				return memcmp(this, &state, sizeof(BlitState)) == 0;
+			}
+
+			Format sourceFormat;
+			Format destFormat;
+			Blitter::Options options;
+		};
+
+		struct BlitData
+		{
+			void *source;
+			void *dest;
+			int sPitchB;
+			int dPitchB;
+
+			float x0;
+			float y0;
+			float w;
+			float h;
+
+			int y0d;
+			int y1d;
+			int x0d;
+			int x1d;
+
+			int sWidth;
+			int sHeight;
+		};
+
+	public:
+		Blitter();
+
+		virtual ~Blitter();
+
+		void clear(void* pixel, sw::Format format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask);
+		void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter);
+		void blit3D(Surface *source, Surface *dest);
+
+	private:
+		bool read(Float4 &color, Pointer<Byte> element, Format format);
+		bool write(Float4 &color, Pointer<Byte> element, Format format, const Blitter::Options& options);
+		bool read(Int4 &color, Pointer<Byte> element, Format format);
+		bool write(Int4 &color, Pointer<Byte> element, Format format, const Blitter::Options& options);
+		static bool GetScale(float4& scale, Format format);
+		static bool ApplyScaleAndClamp(Float4& value, const BlitState& state);
+		void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, const Blitter::Options& options);
+		bool blitReactor(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, const Blitter::Options& options);
+		Routine *generate(BlitState &state);
+
+		RoutineCache<BlitState> *blitCache;
+		BackoffLock criticalSection;
+	};
+
+	extern Blitter blitter;
+}
+
+#endif   // sw_Blitter_hpp
diff --git a/src/Renderer/Clipper.cpp b/src/Renderer/Clipper.cpp
index d575bc3..d5aeaed 100644
--- a/src/Renderer/Clipper.cpp
+++ b/src/Renderer/Clipper.cpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2012 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #include "Clipper.hpp"
 
diff --git a/src/Renderer/Clipper.hpp b/src/Renderer/Clipper.hpp
index fca7b0c..ad1af93e 100644
--- a/src/Renderer/Clipper.hpp
+++ b/src/Renderer/Clipper.hpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2011 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #ifndef sw_Clipper_hpp
 #define sw_Clipper_hpp
diff --git a/src/Renderer/Color.cpp b/src/Renderer/Color.cpp
index 5565460..9ad6767 100644
--- a/src/Renderer/Color.cpp
+++ b/src/Renderer/Color.cpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2011 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #include "Color.hpp"
 
diff --git a/src/Renderer/Color.hpp b/src/Renderer/Color.hpp
index 51e8015..7afe61f 100644
--- a/src/Renderer/Color.hpp
+++ b/src/Renderer/Color.hpp
@@ -1,469 +1,472 @@
-// SwiftShader Software Renderer

-//

-// Copyright(c) 2005-2011 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_Color_hpp

-#define sw_Color_hpp

-

-#include "Common/Types.hpp"

-#include "Common/Math.hpp"

-

-namespace sw

-{

-	template<class T>

-	struct Color

-	{

-		Color();

-		

-		Color(const Color<byte> &c);

-		Color(const Color<short> &c);

-		Color(const Color<float> &c);

-		

-		Color(int c);

-		Color(unsigned short c);

-		Color(unsigned long c);

-		Color(unsigned int c);

-		

-		Color(T r, T g, T b, T a = 1);

-

-		operator unsigned int() const;

-

-		T &operator[](int i);

-		const T &operator[](int i) const;

-

-		Color<T> operator+() const;

-		Color<T> operator-() const;

-

-		Color<T>& operator=(const Color<T>& c);

-

-		Color<T> &operator+=(const Color<T> &c);

-		Color<T> &operator*=(float l);

-

-		static Color<T> gradient(const Color<T> &c1, const Color<T>  &c2, float d);

-		static Color<T> shade(const Color<T> &c1, const Color<T>  &c2, float d);

-

-		template<class S>

-		friend Color<S> operator+(const Color<S> &c1, const Color<S> &c2);

-		template<class S>

-		friend Color<S> operator-(const Color<S> &c1, const Color<S> &c2);

-

-		template<class S>

-		friend Color<S> operator*(float l, const Color<S> &c);

-		template<class S>

-		friend Color<S> operator*(const Color<S> &c1, const Color<S> &c2);

-		template<class S>

-		friend Color<S> operator/(const Color<S> &c, float l);

-

-		T r;

-		T g;

-		T b;

-		T a;

-	};

-}

-

-#include "Common/Math.hpp"

-

-namespace sw

-{

-	template<class T>

-	inline Color<T>::Color()

-	{

-	}

-

-	template<>

-	inline Color<byte>::Color(const Color<byte> &c)

-	{

-		r = c.r;

-		g = c.g;

-		b = c.b;

-		a = c.a;

-	}

-

-	template<>

-	inline Color<byte>::Color(const Color<short> &c)

-	{

-		r = clamp(c.r >> 4, 0, 255);

-		g = clamp(c.g >> 4, 0, 255);

-		b = clamp(c.b >> 4, 0, 255);

-		a = clamp(c.a >> 4, 0, 255);

-	}

-

-	template<>

-	inline Color<byte>::Color(const Color<float> &c)

-	{

-		r = ifloor(clamp(c.r * 256.0f, 0.0f, 255.0f));

-		g = ifloor(clamp(c.g * 256.0f, 0.0f, 255.0f));

-		b = ifloor(clamp(c.b * 256.0f, 0.0f, 255.0f));

-		a = ifloor(clamp(c.a * 256.0f, 0.0f, 255.0f));

-	}

-

-	template<>

-	inline Color<short>::Color(const Color<short> &c)

-	{

-		r = c.r;

-		g = c.g;

-		b = c.b;

-		a = c.a;

-	}

-

-	template<>

-	inline Color<short>::Color(const Color<byte> &c)

-	{

-		r = c.r << 4;

-		g = c.g << 4;

-		b = c.b << 4;

-		a = c.a << 4;

-	}

-

-	template<>

-	inline Color<float>::Color(const Color<float> &c)

-	{

-		r = c.r;

-		g = c.g;

-		b = c.b;

-		a = c.a;

-	}

-

-	template<>

-	inline Color<short>::Color(const Color<float> &c)

-	{

-		r = iround(clamp(c.r * 4095.0f, -4096.0f, 4095.0f));

-		g = iround(clamp(c.g * 4095.0f, -4096.0f, 4095.0f));

-		b = iround(clamp(c.b * 4095.0f, -4096.0f, 4095.0f));

-		a = iround(clamp(c.a * 4095.0f, -4096.0f, 4095.0f));

-	}

-

-	template<>

-	inline Color<float>::Color(const Color<byte> &c)

-	{

-		r = c.r / 255.0f;

-		g = c.g / 255.0f;

-		b = c.b / 255.0f;

-		a = c.a / 255.0f;

-	}

-

-	template<>

-	inline Color<float>::Color(const Color<short> &c)

-	{

-		r = c.r / 4095.0f;

-		g = c.g / 4095.0f;

-		b = c.b / 4095.0f;

-		a = c.a / 4095.0f;

-	}

-

-	template<>

-	inline Color<float>::Color(unsigned short c)

-	{

-		r = (float)(c & 0xF800) / (float)0xF800;

-		g = (float)(c & 0x07E0) / (float)0x07E0;

-		b = (float)(c & 0x001F) / (float)0x001F;

-		a = 1;

-	}

-

-	template<>

-	inline Color<short>::Color(unsigned short c)

-	{

-		// 4.12 fixed-point format

-		r = ((c & 0xF800) >> 4) + ((c & 0xF800) >> 9) + ((c & 0xF800) >> 14);

-		g = ((c & 0x07E0) << 1) + ((c & 0x07E0) >> 5);

-		b = ((c & 0x001F) << 7) + ((c & 0x001F) << 2) + ((c & 0x001F) >> 3);

-		a = 0x1000;

-	}

-

-	template<>

-	inline Color<byte>::Color(unsigned short c)

-	{

-		r = (byte)(((c & 0xF800) >> 8) + ((c & 0xE000) >> 13));

-		g = (byte)(((c & 0x07E0) >> 3) + ((c & 0x0600) >> 9));

-		b = (byte)(((c & 0x001F) << 3) + ((c & 0x001C) >> 2));

-		a = 0xFF;

-	}

-

-	template<>

-	inline Color<float>::Color(int c)

-	{

-		const float d = 1.0f / 255.0f;

-

-		r = (float)((c & 0x00FF0000) >> 16) * d;

-		g = (float)((c & 0x0000FF00) >> 8) * d;

-		b = (float)((c & 0x000000FF) >> 0) * d;

-		a = (float)((c & 0xFF000000) >> 24) * d;

-	}

-

-	template<>

-	inline Color<short>::Color(int c)

-	{

-		// 4.12 fixed-point format

-		r = (short)((c & 0x00FF0000) >> 12);

-		g = (short)((c & 0x0000FF00) >> 4);

-		b = (short)((c & 0x000000FF) << 4);

-		a = (short)((c & 0xFF000000) >> 20);

-	}

-

-	template<>

-	inline Color<byte>::Color(int c)

-	{

-		r = (byte)((c & 0x00FF0000) >> 16);

-		g = (byte)((c & 0x0000FF00) >> 8);

-		b = (byte)((c & 0x000000FF) >> 0);

-		a = (byte)((c & 0xFF000000) >> 24);

-	}

-

-	template<>

-	inline Color<float>::Color(unsigned int c)

-	{

-		const float d = 1.0f / 255.0f;

-

-		r = (float)((c & 0x00FF0000) >> 16) * d;

-		g = (float)((c & 0x0000FF00) >> 8) * d;

-		b = (float)((c & 0x000000FF) >> 0) * d;

-		a = (float)((c & 0xFF000000) >> 24) * d;

-	}

-

-	template<>

-	inline Color<short>::Color(unsigned int c)

-	{

-		// 4.12 fixed-point format

-		r = (short)((c & 0x00FF0000) >> 12);

-		g = (short)((c & 0x0000FF00) >> 4);

-		b = (short)((c & 0x000000FF) << 4);

-		a = (short)((c & 0xFF000000) >> 20);

-	}

-

-	template<>

-	inline Color<byte>::Color(unsigned int c)

-	{

-		r = (byte)((c & 0x00FF0000) >> 16);

-		g = (byte)((c & 0x0000FF00) >> 8);

-		b = (byte)((c & 0x000000FF) >> 0);

-		a = (byte)((c & 0xFF000000) >> 24);

-	}

-

-	template<>

-	inline Color<float>::Color(unsigned long c)

-	{

-		const float d = 1.0f / 255.0f;

-

-		r = (float)((c & 0x00FF0000) >> 16) * d;

-		g = (float)((c & 0x0000FF00) >> 8) * d;

-		b = (float)((c & 0x000000FF) >> 0) * d;

-		a = (float)((c & 0xFF000000) >> 24) * d;

-	}

-

-	template<>

-	inline Color<short>::Color(unsigned long c)

-	{

-		// 4.12 fixed-point format

-		r = (short)((c & 0x00FF0000) >> 12);

-		g = (short)((c & 0x0000FF00) >> 4);

-		b = (short)((c & 0x000000FF) << 4);

-		a = (short)((c & 0xFF000000) >> 20);

-	}

-

-	template<>

-	inline Color<byte>::Color(unsigned long c)

-	{

-		r = (byte)((c & 0x00FF0000) >> 16);

-		g = (byte)((c & 0x0000FF00) >> 8);

-		b = (byte)((c & 0x000000FF) >> 0);

-		a = (byte)((c & 0xFF000000) >> 24);

-	}

-

-	template<class T>

-	inline Color<T>::Color(T r_, T g_, T b_, T a_)

-	{

-		r = r_;

-		g = g_;

-		b = b_;

-		a = a_;

-	}

-

-	template<>

-	inline Color<float>::operator unsigned int() const

-	{

-		return ((unsigned int)min(b * 255.0f, 255.0f) << 0) |

-		       ((unsigned int)min(g * 255.0f, 255.0f) << 8) |

-		       ((unsigned int)min(r * 255.0f, 255.0f) << 16) |

-		       ((unsigned int)min(a * 255.0f, 255.0f) << 24);

-	}

-

-	template<>

-	inline Color<short>::operator unsigned int() const

-	{

-		return ((unsigned int)min(b >> 4, 255) << 0) |

-		       ((unsigned int)min(g >> 4, 255) << 8) |

-		       ((unsigned int)min(r >> 4, 255) << 16) |

-		       ((unsigned int)min(a >> 4, 255) << 24);

-	}

-

-	template<>

-	inline Color<byte>::operator unsigned int() const

-	{

-		return (b << 0) +

-		       (g << 8) +

-		       (r << 16) +

-			   (a << 24);

-	}

-

-	template<class T>

-	inline T &Color<T>::operator[](int i)

-	{

-		return (&r)[i];

-	}

-

-	template<class T>

-	inline const T &Color<T>::operator[](int i) const

-	{

-		return (&r)[i];

-	}

-

-	template<class T>

-	inline Color<T> Color<T>::operator+() const

-	{

-		return *this;

-	}

-

-	template<class T>

-	inline Color<T> Color<T>::operator-() const

-	{

-		return Color(-r, -g, -b, -a);

-	}

-

-	template<class T>

-	inline Color<T> &Color<T>::operator=(const Color& c)

-	{

-		r = c.r;

-		g = c.g;

-		b = c.b;

-		a = c.a;

-

-		return *this;

-	}

-

-	template<class T>

-	inline Color<T> &Color<T>::operator+=(const Color &c)

-	{

-		r += c.r;

-		g += c.g;

-		b += c.b;

-		a += c.a;

-

-		return *this;

-	}

-

-	template<class T>

-	inline Color<T> &Color<T>::operator*=(float l)

-	{

-		*this = l * *this;

-

-		return *this;

-	}

-

-	template<class T>

-	inline Color<T> operator+(const Color<T> &c1, const Color<T> &c2)

-	{

-		return Color<T>(c1.r + c2.r,

-		                c1.g + c2.g,

-		                c1.b + c2.b,

-		                c1.a + c2.a);	

-	}

-

-	template<class T>

-	inline Color<T> operator-(const Color<T> &c1, const Color<T> &c2)

-	{

-		return Color<T>(c1.r - c2.r,

-		                c1.g - c2.g,

-		                c1.b - c2.b,

-		                c1.a - c2.a);	

-	}

-

-	template<class T>

-	inline Color<T> operator*(float l, const Color<T> &c)

-	{

-		T r = (T)(l * c.r);

-		T g = (T)(l * c.g);

-		T b = (T)(l * c.b);

-		T a = (T)(l * c.a);

-

-		return Color<T>(r, g, b, a);

-	}

-

-	template<class T>

-	inline Color<T> operator*(const Color<T> &c1, const Color<T> &c2)

-	{

-		T r = c1.r * c2.r;

-		T g = c1.g * c2.g;

-		T b = c1.b * c2.b;

-		T a = c1.a * c2.a;

-

-		return Color<T>(r, g, b, a);

-	}

-

-	template<>

-	inline Color<short> operator*(const Color<short> &c1, const Color<short> &c2)

-	{

-		short r = c1.r * c2.r >> 12;

-		short g = c1.g * c2.g >> 12;

-		short b = c1.b * c2.b >> 12;

-		short a = c1.a * c2.a >> 12;

-

-		return Color<short>(r, g, b, a);

-	}

-

-	template<>

-	inline Color<byte> operator*(const Color<byte> &c1, const Color<byte> &c2)

-	{

-		byte r = c1.r * c2.r >> 8;

-		byte g = c1.g * c2.g >> 8;

-		byte b = c1.b * c2.b >> 8;

-		byte a = c1.a * c2.a >> 8;

-

-		return Color<byte>(r, g, b, a);

-	}

-

-	template<class T>

-	inline Color<T> operator/(const Color<T> &c, float l)

-	{

-		l = 1.0f / l; 

-

-		T r = (T)(l * c.r);

-		T g = (T)(l * c.g);

-		T b = (T)(l * c.b);

-		T a = (T)(l * c.a);

-

-		return Color<T>(r, g, b, a);

-	}

-

-	template<class T>

-	inline Color<T> Color<T>::gradient(const Color<T> &c1, const Color<T> &c2, float d)

-	{

-		d = 1.0f / d; 

-

-		T r = (c2.r - c1.r) * d;

-		T g = (c2.g - c1.g) * d;

-		T b = (c2.b - c1.b) * d;

-		T a = (c2.a - c1.a) * d;

-

-		return Color<T>(r, g, b, a);

-	}

-

-	template<class T>

-	inline Color<T> Color<T>::shade(const Color<T> &c1, const Color<T>  &c2, float d)

-	{

-		T r = c1.r + (T)(d * (c2.r - c1.r));

-		T g = c1.g + (T)(d * (c2.g - c1.g));

-		T b = c1.b + (T)(d * (c2.b - c1.b));

-		T a = c1.a + (T)(d * (c2.a - c1.a));

-

-		return Color<T>(r, g, b, a);

-	}

-}

-

-#endif   // sw_Color_hpp

+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef sw_Color_hpp
+#define sw_Color_hpp
+
+#include "Common/Types.hpp"
+#include "Common/Math.hpp"
+
+namespace sw
+{
+	template<class T>
+	struct Color
+	{
+		Color();
+	
+		Color(const Color<byte> &c);
+		Color(const Color<short> &c);
+		Color(const Color<float> &c);
+		
+		Color(int c);
+		Color(unsigned short c);
+		Color(unsigned long c);
+		Color(unsigned int c);
+		
+		Color(T r, T g, T b, T a = 1);
+
+		operator unsigned int() const;
+
+		T &operator[](int i);
+		const T &operator[](int i) const;
+
+		Color<T> operator+() const;
+		Color<T> operator-() const;
+
+		Color<T>& operator=(const Color<T>& c);
+
+		Color<T> &operator+=(const Color<T> &c);
+		Color<T> &operator*=(float l);
+
+		static Color<T> gradient(const Color<T> &c1, const Color<T>  &c2, float d);
+		static Color<T> shade(const Color<T> &c1, const Color<T>  &c2, float d);
+
+		template<class S>
+		friend Color<S> operator+(const Color<S> &c1, const Color<S> &c2);
+		template<class S>
+		friend Color<S> operator-(const Color<S> &c1, const Color<S> &c2);
+
+		template<class S>
+		friend Color<S> operator*(float l, const Color<S> &c);
+		template<class S>
+		friend Color<S> operator*(const Color<S> &c1, const Color<S> &c2);
+		template<class S>
+		friend Color<S> operator/(const Color<S> &c, float l);
+
+		T r;
+		T g;
+		T b;
+		T a;
+	};
+}
+
+#include "Common/Math.hpp"
+
+namespace sw
+{
+	template<class T>
+	inline Color<T>::Color()
+	{
+	}
+
+	template<>
+	inline Color<byte>::Color(const Color<byte> &c)
+	{
+		r = c.r;
+		g = c.g;
+		b = c.b;
+		a = c.a;
+	}
+
+	template<>
+	inline Color<byte>::Color(const Color<short> &c)
+	{
+		r = clamp(c.r >> 4, 0, 255);
+		g = clamp(c.g >> 4, 0, 255);
+		b = clamp(c.b >> 4, 0, 255);
+		a = clamp(c.a >> 4, 0, 255);
+	}
+
+	template<>
+	inline Color<byte>::Color(const Color<float> &c)
+	{
+		r = ifloor(clamp(c.r * 256.0f, 0.0f, 255.0f));
+		g = ifloor(clamp(c.g * 256.0f, 0.0f, 255.0f));
+		b = ifloor(clamp(c.b * 256.0f, 0.0f, 255.0f));
+		a = ifloor(clamp(c.a * 256.0f, 0.0f, 255.0f));
+	}
+
+	template<>
+	inline Color<short>::Color(const Color<short> &c)
+	{
+		r = c.r;
+		g = c.g;
+		b = c.b;
+		a = c.a;
+	}
+
+	template<>
+	inline Color<short>::Color(const Color<byte> &c)
+	{
+		r = c.r << 4;
+		g = c.g << 4;
+		b = c.b << 4;
+		a = c.a << 4;
+	}
+
+	template<>
+	inline Color<float>::Color(const Color<float> &c)
+	{
+		r = c.r;
+		g = c.g;
+		b = c.b;
+		a = c.a;
+	}
+
+	template<>
+	inline Color<short>::Color(const Color<float> &c)
+	{
+		r = iround(clamp(c.r * 4095.0f, -4096.0f, 4095.0f));
+		g = iround(clamp(c.g * 4095.0f, -4096.0f, 4095.0f));
+		b = iround(clamp(c.b * 4095.0f, -4096.0f, 4095.0f));
+		a = iround(clamp(c.a * 4095.0f, -4096.0f, 4095.0f));
+	}
+
+	template<>
+	inline Color<float>::Color(const Color<byte> &c)
+	{
+		r = c.r / 255.0f;
+		g = c.g / 255.0f;
+		b = c.b / 255.0f;
+		a = c.a / 255.0f;
+	}
+
+	template<>
+	inline Color<float>::Color(const Color<short> &c)
+	{
+		r = c.r / 4095.0f;
+		g = c.g / 4095.0f;
+		b = c.b / 4095.0f;
+		a = c.a / 4095.0f;
+	}
+
+	template<>
+	inline Color<float>::Color(unsigned short c)
+	{
+		r = (float)(c & 0xF800) / (float)0xF800;
+		g = (float)(c & 0x07E0) / (float)0x07E0;
+		b = (float)(c & 0x001F) / (float)0x001F;
+		a = 1;
+	}
+
+	template<>
+	inline Color<short>::Color(unsigned short c)
+	{
+		// 4.12 fixed-point format
+		r = ((c & 0xF800) >> 4) + ((c & 0xF800) >> 9) + ((c & 0xF800) >> 14);
+		g = ((c & 0x07E0) << 1) + ((c & 0x07E0) >> 5);
+		b = ((c & 0x001F) << 7) + ((c & 0x001F) << 2) + ((c & 0x001F) >> 3);
+		a = 0x1000;
+	}
+
+	template<>
+	inline Color<byte>::Color(unsigned short c)
+	{
+		r = (byte)(((c & 0xF800) >> 8) + ((c & 0xE000) >> 13));
+		g = (byte)(((c & 0x07E0) >> 3) + ((c & 0x0600) >> 9));
+		b = (byte)(((c & 0x001F) << 3) + ((c & 0x001C) >> 2));
+		a = 0xFF;
+	}
+
+	template<>
+	inline Color<float>::Color(int c)
+	{
+		const float d = 1.0f / 255.0f;
+
+		r = (float)((c & 0x00FF0000) >> 16) * d;
+		g = (float)((c & 0x0000FF00) >> 8) * d;
+		b = (float)((c & 0x000000FF) >> 0) * d;
+		a = (float)((c & 0xFF000000) >> 24) * d;
+	}
+
+	template<>
+	inline Color<short>::Color(int c)
+	{
+		// 4.12 fixed-point format
+		r = (short)((c & 0x00FF0000) >> 12);
+		g = (short)((c & 0x0000FF00) >> 4);
+		b = (short)((c & 0x000000FF) << 4);
+		a = (short)((c & 0xFF000000) >> 20);
+	}
+
+	template<>
+	inline Color<byte>::Color(int c)
+	{
+		r = (byte)((c & 0x00FF0000) >> 16);
+		g = (byte)((c & 0x0000FF00) >> 8);
+		b = (byte)((c & 0x000000FF) >> 0);
+		a = (byte)((c & 0xFF000000) >> 24);
+	}
+
+	template<>
+	inline Color<float>::Color(unsigned int c)
+	{
+		const float d = 1.0f / 255.0f;
+
+		r = (float)((c & 0x00FF0000) >> 16) * d;
+		g = (float)((c & 0x0000FF00) >> 8) * d;
+		b = (float)((c & 0x000000FF) >> 0) * d;
+		a = (float)((c & 0xFF000000) >> 24) * d;
+	}
+
+	template<>
+	inline Color<short>::Color(unsigned int c)
+	{
+		// 4.12 fixed-point format
+		r = (short)((c & 0x00FF0000) >> 12);
+		g = (short)((c & 0x0000FF00) >> 4);
+		b = (short)((c & 0x000000FF) << 4);
+		a = (short)((c & 0xFF000000) >> 20);
+	}
+
+	template<>
+	inline Color<byte>::Color(unsigned int c)
+	{
+		r = (byte)((c & 0x00FF0000) >> 16);
+		g = (byte)((c & 0x0000FF00) >> 8);
+		b = (byte)((c & 0x000000FF) >> 0);
+		a = (byte)((c & 0xFF000000) >> 24);
+	}
+
+	template<>
+	inline Color<float>::Color(unsigned long c)
+	{
+		const float d = 1.0f / 255.0f;
+
+		r = (float)((c & 0x00FF0000) >> 16) * d;
+		g = (float)((c & 0x0000FF00) >> 8) * d;
+		b = (float)((c & 0x000000FF) >> 0) * d;
+		a = (float)((c & 0xFF000000) >> 24) * d;
+	}
+
+	template<>
+	inline Color<short>::Color(unsigned long c)
+	{
+		// 4.12 fixed-point format
+		r = (short)((c & 0x00FF0000) >> 12);
+		g = (short)((c & 0x0000FF00) >> 4);
+		b = (short)((c & 0x000000FF) << 4);
+		a = (short)((c & 0xFF000000) >> 20);
+	}
+
+	template<>
+	inline Color<byte>::Color(unsigned long c)
+	{
+		r = (byte)((c & 0x00FF0000) >> 16);
+		g = (byte)((c & 0x0000FF00) >> 8);
+		b = (byte)((c & 0x000000FF) >> 0);
+		a = (byte)((c & 0xFF000000) >> 24);
+	}
+
+	template<class T>
+	inline Color<T>::Color(T r_, T g_, T b_, T a_)
+	{
+		r = r_;
+		g = g_;
+		b = b_;
+		a = a_;
+	}
+
+	template<>
+	inline Color<float>::operator unsigned int() const
+	{
+		return ((unsigned int)min(b * 255.0f, 255.0f) << 0) |
+		       ((unsigned int)min(g * 255.0f, 255.0f) << 8) |
+		       ((unsigned int)min(r * 255.0f, 255.0f) << 16) |
+		       ((unsigned int)min(a * 255.0f, 255.0f) << 24);
+	}
+
+	template<>
+	inline Color<short>::operator unsigned int() const
+	{
+		return ((unsigned int)min(b >> 4, 255) << 0) |
+		       ((unsigned int)min(g >> 4, 255) << 8) |
+		       ((unsigned int)min(r >> 4, 255) << 16) |
+		       ((unsigned int)min(a >> 4, 255) << 24);
+	}
+
+	template<>
+	inline Color<byte>::operator unsigned int() const
+	{
+		return (b << 0) +
+		       (g << 8) +
+		       (r << 16) +
+			   (a << 24);
+	}
+
+	template<class T>
+	inline T &Color<T>::operator[](int i)
+	{
+		return (&r)[i];
+	}
+
+	template<class T>
+	inline const T &Color<T>::operator[](int i) const
+	{
+		return (&r)[i];
+	}
+
+	template<class T>
+	inline Color<T> Color<T>::operator+() const
+	{
+		return *this;
+	}
+
+	template<class T>
+	inline Color<T> Color<T>::operator-() const
+	{
+		return Color(-r, -g, -b, -a);
+	}
+
+	template<class T>
+	inline Color<T> &Color<T>::operator=(const Color& c)
+	{
+		r = c.r;
+		g = c.g;
+		b = c.b;
+		a = c.a;
+
+		return *this;
+	}
+
+	template<class T>
+	inline Color<T> &Color<T>::operator+=(const Color &c)
+	{
+		r += c.r;
+		g += c.g;
+		b += c.b;
+		a += c.a;
+
+		return *this;
+	}
+
+	template<class T>
+	inline Color<T> &Color<T>::operator*=(float l)
+	{
+		*this = l * *this;
+
+		return *this;
+	}
+
+	template<class T>
+	inline Color<T> operator+(const Color<T> &c1, const Color<T> &c2)
+	{
+		return Color<T>(c1.r + c2.r,
+		                c1.g + c2.g,
+		                c1.b + c2.b,
+		                c1.a + c2.a);	
+	}
+
+	template<class T>
+	inline Color<T> operator-(const Color<T> &c1, const Color<T> &c2)
+	{
+		return Color<T>(c1.r - c2.r,
+		                c1.g - c2.g,
+		                c1.b - c2.b,
+		                c1.a - c2.a);	
+	}
+
+	template<class T>
+	inline Color<T> operator*(float l, const Color<T> &c)
+	{
+		T r = (T)(l * c.r);
+		T g = (T)(l * c.g);
+		T b = (T)(l * c.b);
+		T a = (T)(l * c.a);
+
+		return Color<T>(r, g, b, a);
+	}
+
+	template<class T>
+	inline Color<T> operator*(const Color<T> &c1, const Color<T> &c2)
+	{
+		T r = c1.r * c2.r;
+		T g = c1.g * c2.g;
+		T b = c1.b * c2.b;
+		T a = c1.a * c2.a;
+
+		return Color<T>(r, g, b, a);
+	}
+
+	template<>
+	inline Color<short> operator*(const Color<short> &c1, const Color<short> &c2)
+	{
+		short r = c1.r * c2.r >> 12;
+		short g = c1.g * c2.g >> 12;
+		short b = c1.b * c2.b >> 12;
+		short a = c1.a * c2.a >> 12;
+
+		return Color<short>(r, g, b, a);
+	}
+
+	template<>
+	inline Color<byte> operator*(const Color<byte> &c1, const Color<byte> &c2)
+	{
+		byte r = c1.r * c2.r >> 8;
+		byte g = c1.g * c2.g >> 8;
+		byte b = c1.b * c2.b >> 8;
+		byte a = c1.a * c2.a >> 8;
+
+		return Color<byte>(r, g, b, a);
+	}
+
+	template<class T>
+	inline Color<T> operator/(const Color<T> &c, float l)
+	{
+		l = 1.0f / l; 
+
+		T r = (T)(l * c.r);
+		T g = (T)(l * c.g);
+		T b = (T)(l * c.b);
+		T a = (T)(l * c.a);
+
+		return Color<T>(r, g, b, a);
+	}
+
+	template<class T>
+	inline Color<T> Color<T>::gradient(const Color<T> &c1, const Color<T> &c2, float d)
+	{
+		d = 1.0f / d; 
+
+		T r = (c2.r - c1.r) * d;
+		T g = (c2.g - c1.g) * d;
+		T b = (c2.b - c1.b) * d;
+		T a = (c2.a - c1.a) * d;
+
+		return Color<T>(r, g, b, a);
+	}
+
+	template<class T>
+	inline Color<T> Color<T>::shade(const Color<T> &c1, const Color<T>  &c2, float d)
+	{
+		T r = c1.r + (T)(d * (c2.r - c1.r));
+		T g = c1.g + (T)(d * (c2.g - c1.g));
+		T b = c1.b + (T)(d * (c2.b - c1.b));
+		T a = c1.a + (T)(d * (c2.a - c1.a));
+
+		return Color<T>(r, g, b, a);
+	}
+}
+
+#endif   // sw_Color_hpp
diff --git a/src/Renderer/Context.cpp b/src/Renderer/Context.cpp
index b1891b2..1edecd8 100644
--- a/src/Renderer/Context.cpp
+++ b/src/Renderer/Context.cpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2012 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #include "Context.hpp"
 
@@ -95,8 +98,8 @@
 		case DRAW_INDEXEDTRIANGLESTRIP32:
 		case DRAW_INDEXEDTRIANGLEFAN32:
 			return fillModeAware ? fillMode == FILL_VERTEX : false;
-        case DRAW_QUADLIST:
-            return false;
+		case DRAW_QUADLIST:
+			return false;
 		default:
 			ASSERT(false);
 		}
@@ -139,8 +142,8 @@
 		case DRAW_INDEXEDTRIANGLESTRIP32:
 		case DRAW_INDEXEDTRIANGLEFAN32:
 			return fillModeAware ? fillMode == FILL_WIREFRAME : false;
-        case DRAW_QUADLIST:
-            return false;
+		case DRAW_QUADLIST:
+			return false;
 		default:
 			ASSERT(false);
 		}
@@ -183,9 +186,9 @@
 		case DRAW_INDEXEDTRIANGLESTRIP32:
 		case DRAW_INDEXEDTRIANGLEFAN32:
 			return fillModeAware ? fillMode == FILL_SOLID : true;
-        case DRAW_QUADLIST:
+		case DRAW_QUADLIST:
 			// Quads are broken up into triangles
-            return fillModeAware ? fillMode == FILL_SOLID : true;
+			return fillModeAware ? fillMode == FILL_SOLID : true;
 		default:
 			ASSERT(false);
 		}
@@ -603,8 +606,8 @@
 	{
 		return texCoordActive(coordinate, 0) ||
 		       texCoordActive(coordinate, 1) ||
-			   texCoordActive(coordinate, 2) ||
-			   texCoordActive(coordinate, 3);
+		       texCoordActive(coordinate, 2) ||
+		       texCoordActive(coordinate, 3);
 	}
 
 	bool Context::isProjectionComponent(unsigned int coordinate, int component)
diff --git a/src/Renderer/Context.hpp b/src/Renderer/Context.hpp
index c8f9624..6e68ecb 100644
--- a/src/Renderer/Context.hpp
+++ b/src/Renderer/Context.hpp
@@ -1,531 +1,534 @@
-// 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_Context_hpp

-#define sw_Context_hpp

-

-#include "Sampler.hpp"

-#include "TextureStage.hpp"

-#include "Stream.hpp"

-#include "Point.hpp"

-#include "Vertex.hpp"

-

-namespace sw

-{

-	class Sampler;

-	class Surface;

-	class PixelShader;

-	class VertexShader;

-	struct Triangle;

-	struct Primitive;

-	struct Vertex;

-	class Resource;

-

-	enum In   // Default input stream semantic

-	{

-		Position = 0,

-		BlendWeight = 1,

-		BlendIndices = 2,

-		Normal = 3,

-		PointSize = 4,

-		Color0 = 5,

-		Color1 = 6,

-		TexCoord0 = 7,

-		TexCoord1 = 8,

-		TexCoord2 = 9,

-		TexCoord3 = 10,

-		TexCoord4 = 11,

-		TexCoord5 = 12,

-		TexCoord6 = 13,

-		TexCoord7 = 14,

-		PositionT = 15

-	};

-

-	enum DrawType : unsigned int

-	{

-		// These types must stay ordered by vertices per primitive. Also, if these basic types

-		// are modified, verify the value assigned to task->verticesPerPrimitive in Renderer.cpp

-		DRAW_POINTLIST     = 0x00,

-		DRAW_LINELIST      = 0x01,

-		DRAW_LINESTRIP     = 0x02,

-		DRAW_LINELOOP      = 0x03,

-		DRAW_TRIANGLELIST  = 0x04,

-		DRAW_TRIANGLESTRIP = 0x05,

-		DRAW_TRIANGLEFAN   = 0x06,

-		DRAW_QUADLIST      = 0x07,

-

-		DRAW_NONINDEXED = 0x00,

-		DRAW_INDEXED8   = 0x10,

-		DRAW_INDEXED16  = 0x20,

-		DRAW_INDEXED32  = 0x30,

-

-		DRAW_INDEXEDPOINTLIST8 = DRAW_POINTLIST | DRAW_INDEXED8,

-		DRAW_INDEXEDLINELIST8  = DRAW_LINELIST  | DRAW_INDEXED8,

-		DRAW_INDEXEDLINESTRIP8 = DRAW_LINESTRIP | DRAW_INDEXED8,

-		DRAW_INDEXEDLINELOOP8  = DRAW_LINELOOP  | DRAW_INDEXED8,

-		DRAW_INDEXEDTRIANGLELIST8  = DRAW_TRIANGLELIST  | DRAW_INDEXED8,

-		DRAW_INDEXEDTRIANGLESTRIP8 = DRAW_TRIANGLESTRIP | DRAW_INDEXED8,

-		DRAW_INDEXEDTRIANGLEFAN8   = DRAW_TRIANGLEFAN   | DRAW_INDEXED8,

-

-		DRAW_INDEXEDPOINTLIST16 = DRAW_POINTLIST | DRAW_INDEXED16,

-		DRAW_INDEXEDLINELIST16  = DRAW_LINELIST  | DRAW_INDEXED16,

-		DRAW_INDEXEDLINESTRIP16 = DRAW_LINESTRIP | DRAW_INDEXED16,

-		DRAW_INDEXEDLINELOOP16  = DRAW_LINELOOP  | DRAW_INDEXED16,

-		DRAW_INDEXEDTRIANGLELIST16  = DRAW_TRIANGLELIST  | DRAW_INDEXED16,

-		DRAW_INDEXEDTRIANGLESTRIP16 = DRAW_TRIANGLESTRIP | DRAW_INDEXED16,

-		DRAW_INDEXEDTRIANGLEFAN16   = DRAW_TRIANGLEFAN   | DRAW_INDEXED16,

-

-		DRAW_INDEXEDPOINTLIST32 = DRAW_POINTLIST | DRAW_INDEXED32,

-		DRAW_INDEXEDLINELIST32  = DRAW_LINELIST  | DRAW_INDEXED32,

-		DRAW_INDEXEDLINESTRIP32 = DRAW_LINESTRIP | DRAW_INDEXED32,

-		DRAW_INDEXEDLINELOOP32  = DRAW_LINELOOP  | DRAW_INDEXED32,

-		DRAW_INDEXEDTRIANGLELIST32  = DRAW_TRIANGLELIST  | DRAW_INDEXED32,

-		DRAW_INDEXEDTRIANGLESTRIP32 = DRAW_TRIANGLESTRIP | DRAW_INDEXED32,

-		DRAW_INDEXEDTRIANGLEFAN32   = DRAW_TRIANGLEFAN   | DRAW_INDEXED32,

-

-		DRAW_LAST = DRAW_INDEXEDTRIANGLEFAN32

-	};

-

-	enum FillMode : unsigned int

-	{

-		FILL_SOLID,

-		FILL_WIREFRAME,

-		FILL_VERTEX,

-

-		FILL_LAST = FILL_VERTEX

-	};

-

-	enum ShadingMode : unsigned int

-	{

-		SHADING_FLAT,

-		SHADING_GOURAUD,

-

-		SHADING_LAST = SHADING_GOURAUD

-	};

-

-	enum DepthCompareMode : unsigned int

-	{

-		DEPTH_ALWAYS,

-		DEPTH_NEVER,

-		DEPTH_EQUAL,

-		DEPTH_NOTEQUAL,

-		DEPTH_LESS,

-		DEPTH_LESSEQUAL,

-		DEPTH_GREATER,

-		DEPTH_GREATEREQUAL,

-

-		DEPTH_LAST = DEPTH_GREATEREQUAL

-	};

-

-	enum StencilCompareMode : unsigned int

-	{

-		STENCIL_ALWAYS,

-		STENCIL_NEVER,

-		STENCIL_EQUAL,

-		STENCIL_NOTEQUAL,

-		STENCIL_LESS,

-		STENCIL_LESSEQUAL,

-		STENCIL_GREATER,

-		STENCIL_GREATEREQUAL,

-

-		STENCIL_LAST = STENCIL_GREATEREQUAL

-	};

-

-	enum StencilOperation : unsigned int

-	{

-		OPERATION_KEEP,

-		OPERATION_ZERO,

-		OPERATION_REPLACE,

-		OPERATION_INCRSAT,

-		OPERATION_DECRSAT,

-		OPERATION_INVERT,

-		OPERATION_INCR,

-		OPERATION_DECR,

-

-		OPERATION_LAST = OPERATION_DECR

-	};

-

-	enum AlphaCompareMode : unsigned int

-	{

-		ALPHA_ALWAYS,

-		ALPHA_NEVER,

-		ALPHA_EQUAL,

-		ALPHA_NOTEQUAL,

-		ALPHA_LESS,

-		ALPHA_LESSEQUAL,

-		ALPHA_GREATER,

-		ALPHA_GREATEREQUAL,

-

-		ALPHA_LAST = ALPHA_GREATEREQUAL

-	};

-

-	enum CullMode : unsigned int

-	{

-		CULL_NONE,

-		CULL_CLOCKWISE,

-		CULL_COUNTERCLOCKWISE,

-

-		CULL_LAST = CULL_COUNTERCLOCKWISE

-	};

-

-	enum BlendFactor : unsigned int

-	{

-		BLEND_ZERO,

-		BLEND_ONE,

-		BLEND_SOURCE,

-		BLEND_INVSOURCE,

-		BLEND_DEST,

-		BLEND_INVDEST,

-		BLEND_SOURCEALPHA,

-		BLEND_INVSOURCEALPHA,

-		BLEND_DESTALPHA,

-		BLEND_INVDESTALPHA,

-		BLEND_SRCALPHASAT,

-		BLEND_CONSTANT,

-		BLEND_INVCONSTANT,

-		BLEND_CONSTANTALPHA,

-		BLEND_INVCONSTANTALPHA,

-

-		BLEND_LAST = BLEND_INVCONSTANT

-	};

-

-	enum BlendOperation : unsigned int

-	{

-		BLENDOP_ADD,

-		BLENDOP_SUB,

-		BLENDOP_INVSUB,

-		BLENDOP_MIN,

-		BLENDOP_MAX,

-

-		BLENDOP_SOURCE,   // Copy source

-		BLENDOP_DEST,     // Copy dest

-		BLENDOP_NULL,     // Nullify result

-

-		BLENDOP_LAST = BLENDOP_NULL

-	};

-

-	enum LogicalOperation : unsigned int

-	{

-		LOGICALOP_CLEAR,

-		LOGICALOP_SET,

-		LOGICALOP_COPY,

-		LOGICALOP_COPY_INVERTED,

-		LOGICALOP_NOOP,

-		LOGICALOP_INVERT,

-		LOGICALOP_AND,

-		LOGICALOP_NAND,

-		LOGICALOP_OR,

-		LOGICALOP_NOR,

-		LOGICALOP_XOR,

-		LOGICALOP_EQUIV,

-		LOGICALOP_AND_REVERSE,

-		LOGICALOP_AND_INVERTED,

-		LOGICALOP_OR_REVERSE,

-		LOGICALOP_OR_INVERTED,

-

-		LOGICALOP_LAST = LOGICALOP_OR_INVERTED

-	};

-

-	enum MaterialSource : unsigned int

-	{

-		MATERIAL_MATERIAL,

-		MATERIAL_COLOR1,

-		MATERIAL_COLOR2,

-

-		MATERIAL_LAST = MATERIAL_COLOR2

-	};

-

-	enum FogMode : unsigned int

-	{

-		FOG_NONE,

-		FOG_LINEAR,

-		FOG_EXP,

-		FOG_EXP2,

-

-		FOG_LAST = FOG_EXP2

-	};

-

-	enum TexGen : unsigned int

-	{

-		TEXGEN_PASSTHRU,

-		TEXGEN_NORMAL,

-		TEXGEN_POSITION,

-		TEXGEN_REFLECTION,

-		TEXGEN_SPHEREMAP,

-		TEXGEN_NONE,

-

-		TEXGEN_LAST = TEXGEN_NONE

-	};

-

-	enum TransparencyAntialiasing : unsigned int

-	{

-		TRANSPARENCY_NONE,

-		TRANSPARENCY_ALPHA_TO_COVERAGE,

-

-		TRANSPARENCY_LAST = TRANSPARENCY_ALPHA_TO_COVERAGE

-	};

-

-	class Context

-	{

-	public:

-		Context();

-

-		~Context();

-

-		void *operator new(size_t bytes);

-		void operator delete(void *pointer, size_t bytes);

-

-		bool isDrawPoint(bool fillModeAware = false) const;

-		bool isDrawLine(bool fillModeAware = false) const;

-		bool isDrawTriangle(bool fillModeAware = false) const;

-

-		void init();

-

-		const float &exp2Bias();   // NOTE: Needs address for JIT

-

-		const Point &getLightPosition(int light);

-

-		void setGlobalMipmapBias(float bias);

-

-		// Set fixed-function vertex pipeline states

-		void setLightingEnable(bool lightingEnable);

-		void setSpecularEnable(bool specularEnable);

-		void setLightEnable(int light, bool lightEnable);

-		void setLightPosition(int light, Point worldLightPosition);

-

-		void setColorVertexEnable(bool colorVertexEnable);

-		void setAmbientMaterialSource(MaterialSource ambientMaterialSource);

-		void setDiffuseMaterialSource(MaterialSource diffuseMaterialSource);

-		void setSpecularMaterialSource(MaterialSource specularMaterialSource);

-		void setEmissiveMaterialSource(MaterialSource emissiveMaterialSource);

-

-		void setPointSpriteEnable(bool pointSpriteEnable);

-		void setPointScaleEnable(bool pointScaleEnable);

-

-		// Set fixed-function pixel pipeline states, return true when modified

-		bool setDepthBufferEnable(bool depthBufferEnable);

-

-		bool setAlphaBlendEnable(bool alphaBlendEnable);

-		bool setSourceBlendFactor(BlendFactor sourceBlendFactor);

-		bool setDestBlendFactor(BlendFactor destBlendFactor);

-		bool setBlendOperation(BlendOperation blendOperation);

-

-		bool setSeparateAlphaBlendEnable(bool separateAlphaBlendEnable);

-		bool setSourceBlendFactorAlpha(BlendFactor sourceBlendFactorAlpha);

-		bool setDestBlendFactorAlpha(BlendFactor destBlendFactorAlpha);

-		bool setBlendOperationAlpha(BlendOperation blendOperationAlpha);

-

-		bool setColorWriteMask(int index, int colorWriteMask);

-		bool setWriteSRGB(bool sRGB);

-

-		bool setColorLogicOpEnabled(bool colorLogicOpEnabled);

-		bool setLogicalOperation(LogicalOperation logicalOperation);

-

-		// Active fixed-function pixel pipeline states

-		bool fogActive();

-		bool pointSizeActive();

-		FogMode pixelFogActive();

-		bool depthWriteActive();

-		bool alphaTestActive();

-		bool depthBufferActive();

-		bool stencilActive();

-

-		bool perspectiveActive();

-

-		// Active fixed-function vertex pipeline states

-		bool vertexLightingActive();

-		bool texCoordActive(int coordinate, int component);

-		bool texCoordActive(int coordinate);

-		bool isProjectionComponent(unsigned int coordinate, int component);

-		bool vertexSpecularInputActive();

-		bool vertexSpecularActive();

-		bool vertexNormalActive();

-		bool vertexLightActive();

-		bool vertexLightActive(int i);

-		MaterialSource vertexDiffuseMaterialSourceActive();

-		MaterialSource vertexSpecularMaterialSourceActive();

-		MaterialSource vertexAmbientMaterialSourceActive();

-		MaterialSource vertexEmissiveMaterialSourceActive();

-

-		bool pointSpriteActive();

-		bool pointScaleActive();

-

-		bool alphaBlendActive();

-		BlendFactor sourceBlendFactor();

-		BlendFactor destBlendFactor();

-		BlendOperation blendOperation();

-

-		BlendFactor sourceBlendFactorAlpha();

-		BlendFactor destBlendFactorAlpha();

-		BlendOperation blendOperationAlpha();

-

-		LogicalOperation colorLogicOp();

-		LogicalOperation indexLogicOp();

-

-		bool indexedVertexBlendActive();

-		int vertexBlendMatrixCountActive();

-		bool localViewerActive();

-		bool normalizeNormalsActive();

-		FogMode vertexFogModeActive();

-		bool rangeFogActive();

-

-		TexGen texGenActive(int stage);

-		int textureTransformCountActive(int stage);

-		int texCoordIndexActive(int stage);

-

-		// Active context states

-		bool diffuseUsed();     // Used by pixel processor but not provided by vertex processor

-		bool diffuseUsed(int component);     // Used by pixel processor but not provided by vertex processor

-		bool diffuseActive();

-		bool diffuseActive(int component);

-		bool specularUsed();

-		bool specularUsed(int component);

-		bool specularActive();

-		bool specularActive(int component);

-		bool colorActive(int color, int component);

-		bool textureActive();

-		bool textureActive(int coordinate);

-		bool textureActive(int coordinate, int component);

-

-		unsigned short pixelShaderVersion() const;

-		unsigned short vertexShaderVersion() const;

-

-		int getMultiSampleCount() const;

-		int getSuperSampleCount() const;

-

-		DrawType drawType;

-

-		bool stencilEnable;

-		StencilCompareMode stencilCompareMode;

-		int stencilReference;

-		int stencilMask;

-		StencilOperation stencilFailOperation;

-		StencilOperation stencilPassOperation;

-		StencilOperation stencilZFailOperation;

-		int stencilWriteMask;

-

-		bool twoSidedStencil;

-		StencilCompareMode stencilCompareModeCCW;

-		int stencilReferenceCCW;

-		int stencilMaskCCW;

-		StencilOperation stencilFailOperationCCW;

-		StencilOperation stencilPassOperationCCW;

-		StencilOperation stencilZFailOperationCCW;

-		int stencilWriteMaskCCW;

-

-		// Pixel processor states

-		AlphaCompareMode alphaCompareMode;

-		bool alphaTestEnable;

-		FillMode fillMode;

-		ShadingMode shadingMode;

-

-		CullMode cullMode;

-		float alphaReference;

-

-		TextureStage textureStage[8];

-		Sampler sampler[TOTAL_IMAGE_UNITS];

-

-		Format renderTargetInternalFormat(int index);

-		int colorWriteActive();

-		int colorWriteActive(int index);

-		bool colorUsed();

-

-		Resource *texture[TOTAL_IMAGE_UNITS];

-		Stream input[VERTEX_ATTRIBUTES];

-		Resource *indexBuffer;

-

-		bool preTransformed;   // FIXME: Private

-

-		float fogStart;

-		float fogEnd;

-

-		void computeIllumination();

-

-		bool textureWrapActive;

-		unsigned char textureWrap[TEXTURE_IMAGE_UNITS];

-		TexGen texGen[8];

-		bool localViewer;

-		bool normalizeNormals;

-		int textureTransformCount[8];

-		bool textureTransformProject[8];

-

-		Surface *renderTarget[RENDERTARGETS];

-		Surface *depthBuffer;

-		Surface *stencilBuffer;

-

-		// Fog

-		bool fogEnable;

-		FogMode pixelFogMode;

-		FogMode vertexFogMode;

-		bool wBasedFog;

-		bool rangeFogEnable;

-

-		// Vertex blending

-		bool indexedVertexBlendEnable;

-		int vertexBlendMatrixCount;

-

-		// Shaders

-		const PixelShader *pixelShader;

-		const VertexShader *vertexShader;

-

-		// Global mipmap bias

-		float bias;

-

-		// Instancing

-		int instanceID;

-

-		// Fixed-function vertex pipeline state

-		bool lightingEnable;

-		bool specularEnable;

-		bool lightEnable[8];

-		Point worldLightPosition[8];

-

-		MaterialSource ambientMaterialSource;

-		MaterialSource diffuseMaterialSource;

-		MaterialSource specularMaterialSource;

-		MaterialSource emissiveMaterialSource;

-		bool colorVertexEnable;

-

-		bool occlusionEnabled;

-		bool transformFeedbackQueryEnabled;

-		uint64_t transformFeedbackEnabled;

-

-		// Pixel processor states

-		bool rasterizerDiscard;

-		bool depthBufferEnable;

-		DepthCompareMode depthCompareMode;

-		bool depthWriteEnable;

-

-		bool alphaBlendEnable;

-		BlendFactor sourceBlendFactorState;

-		BlendFactor destBlendFactorState;

-		BlendOperation blendOperationState;

-

-		bool separateAlphaBlendEnable;

-		BlendFactor sourceBlendFactorStateAlpha;

-		BlendFactor destBlendFactorStateAlpha;

-		BlendOperation blendOperationStateAlpha;

-

-		bool pointSpriteEnable;

-		bool pointScaleEnable;

-		float lineWidth;

-

-		int colorWriteMask[RENDERTARGETS];   // RGBA

-		bool writeSRGB;

-		unsigned int sampleMask;

-		unsigned int multiSampleMask;

-

-		bool colorLogicOpEnabled;

-		LogicalOperation logicalOperation;

-	};

-}

-

-#endif   // sw_Context_hpp

+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef sw_Context_hpp
+#define sw_Context_hpp
+
+#include "Sampler.hpp"
+#include "TextureStage.hpp"
+#include "Stream.hpp"
+#include "Point.hpp"
+#include "Vertex.hpp"
+
+namespace sw
+{
+	class Sampler;
+	class Surface;
+	class PixelShader;
+	class VertexShader;
+	struct Triangle;
+	struct Primitive;
+	struct Vertex;
+	class Resource;
+
+	enum In   // Default input stream semantic
+	{
+		Position = 0,
+		BlendWeight = 1,
+		BlendIndices = 2,
+		Normal = 3,
+		PointSize = 4,
+		Color0 = 5,
+		Color1 = 6,
+		TexCoord0 = 7,
+		TexCoord1 = 8,
+		TexCoord2 = 9,
+		TexCoord3 = 10,
+		TexCoord4 = 11,
+		TexCoord5 = 12,
+		TexCoord6 = 13,
+		TexCoord7 = 14,
+		PositionT = 15
+	};
+
+	enum DrawType : unsigned int
+	{
+		// These types must stay ordered by vertices per primitive. Also, if these basic types
+		// are modified, verify the value assigned to task->verticesPerPrimitive in Renderer.cpp
+		DRAW_POINTLIST     = 0x00,
+		DRAW_LINELIST      = 0x01,
+		DRAW_LINESTRIP     = 0x02,
+		DRAW_LINELOOP      = 0x03,
+		DRAW_TRIANGLELIST  = 0x04,
+		DRAW_TRIANGLESTRIP = 0x05,
+		DRAW_TRIANGLEFAN   = 0x06,
+		DRAW_QUADLIST      = 0x07,
+
+		DRAW_NONINDEXED = 0x00,
+		DRAW_INDEXED8   = 0x10,
+		DRAW_INDEXED16  = 0x20,
+		DRAW_INDEXED32  = 0x30,
+
+		DRAW_INDEXEDPOINTLIST8 = DRAW_POINTLIST | DRAW_INDEXED8,
+		DRAW_INDEXEDLINELIST8  = DRAW_LINELIST  | DRAW_INDEXED8,
+		DRAW_INDEXEDLINESTRIP8 = DRAW_LINESTRIP | DRAW_INDEXED8,
+		DRAW_INDEXEDLINELOOP8  = DRAW_LINELOOP  | DRAW_INDEXED8,
+		DRAW_INDEXEDTRIANGLELIST8  = DRAW_TRIANGLELIST  | DRAW_INDEXED8,
+		DRAW_INDEXEDTRIANGLESTRIP8 = DRAW_TRIANGLESTRIP | DRAW_INDEXED8,
+		DRAW_INDEXEDTRIANGLEFAN8   = DRAW_TRIANGLEFAN   | DRAW_INDEXED8,
+
+		DRAW_INDEXEDPOINTLIST16 = DRAW_POINTLIST | DRAW_INDEXED16,
+		DRAW_INDEXEDLINELIST16  = DRAW_LINELIST  | DRAW_INDEXED16,
+		DRAW_INDEXEDLINESTRIP16 = DRAW_LINESTRIP | DRAW_INDEXED16,
+		DRAW_INDEXEDLINELOOP16  = DRAW_LINELOOP  | DRAW_INDEXED16,
+		DRAW_INDEXEDTRIANGLELIST16  = DRAW_TRIANGLELIST  | DRAW_INDEXED16,
+		DRAW_INDEXEDTRIANGLESTRIP16 = DRAW_TRIANGLESTRIP | DRAW_INDEXED16,
+		DRAW_INDEXEDTRIANGLEFAN16   = DRAW_TRIANGLEFAN   | DRAW_INDEXED16,
+
+		DRAW_INDEXEDPOINTLIST32 = DRAW_POINTLIST | DRAW_INDEXED32,
+		DRAW_INDEXEDLINELIST32  = DRAW_LINELIST  | DRAW_INDEXED32,
+		DRAW_INDEXEDLINESTRIP32 = DRAW_LINESTRIP | DRAW_INDEXED32,
+		DRAW_INDEXEDLINELOOP32  = DRAW_LINELOOP  | DRAW_INDEXED32,
+		DRAW_INDEXEDTRIANGLELIST32  = DRAW_TRIANGLELIST  | DRAW_INDEXED32,
+		DRAW_INDEXEDTRIANGLESTRIP32 = DRAW_TRIANGLESTRIP | DRAW_INDEXED32,
+		DRAW_INDEXEDTRIANGLEFAN32   = DRAW_TRIANGLEFAN   | DRAW_INDEXED32,
+
+		DRAW_LAST = DRAW_INDEXEDTRIANGLEFAN32
+	};
+
+	enum FillMode : unsigned int
+	{
+		FILL_SOLID,
+		FILL_WIREFRAME,
+		FILL_VERTEX,
+
+		FILL_LAST = FILL_VERTEX
+	};
+
+	enum ShadingMode : unsigned int
+	{
+		SHADING_FLAT,
+		SHADING_GOURAUD,
+
+		SHADING_LAST = SHADING_GOURAUD
+	};
+
+	enum DepthCompareMode : unsigned int
+	{
+		DEPTH_ALWAYS,
+		DEPTH_NEVER,
+		DEPTH_EQUAL,
+		DEPTH_NOTEQUAL,
+		DEPTH_LESS,
+		DEPTH_LESSEQUAL,
+		DEPTH_GREATER,
+		DEPTH_GREATEREQUAL,
+
+		DEPTH_LAST = DEPTH_GREATEREQUAL
+	};
+
+	enum StencilCompareMode : unsigned int
+	{
+		STENCIL_ALWAYS,
+		STENCIL_NEVER,
+		STENCIL_EQUAL,
+		STENCIL_NOTEQUAL,
+		STENCIL_LESS,
+		STENCIL_LESSEQUAL,
+		STENCIL_GREATER,
+		STENCIL_GREATEREQUAL,
+
+		STENCIL_LAST = STENCIL_GREATEREQUAL
+	};
+
+	enum StencilOperation : unsigned int
+	{
+		OPERATION_KEEP,
+		OPERATION_ZERO,
+		OPERATION_REPLACE,
+		OPERATION_INCRSAT,
+		OPERATION_DECRSAT,
+		OPERATION_INVERT,
+		OPERATION_INCR,
+		OPERATION_DECR,
+
+		OPERATION_LAST = OPERATION_DECR
+	};
+
+	enum AlphaCompareMode : unsigned int
+	{
+		ALPHA_ALWAYS,
+		ALPHA_NEVER,
+		ALPHA_EQUAL,
+		ALPHA_NOTEQUAL,
+		ALPHA_LESS,
+		ALPHA_LESSEQUAL,
+		ALPHA_GREATER,
+		ALPHA_GREATEREQUAL,
+
+		ALPHA_LAST = ALPHA_GREATEREQUAL
+	};
+
+	enum CullMode : unsigned int
+	{
+		CULL_NONE,
+		CULL_CLOCKWISE,
+		CULL_COUNTERCLOCKWISE,
+
+		CULL_LAST = CULL_COUNTERCLOCKWISE
+	};
+
+	enum BlendFactor : unsigned int
+	{
+		BLEND_ZERO,
+		BLEND_ONE,
+		BLEND_SOURCE,
+		BLEND_INVSOURCE,
+		BLEND_DEST,
+		BLEND_INVDEST,
+		BLEND_SOURCEALPHA,
+		BLEND_INVSOURCEALPHA,
+		BLEND_DESTALPHA,
+		BLEND_INVDESTALPHA,
+		BLEND_SRCALPHASAT,
+		BLEND_CONSTANT,
+		BLEND_INVCONSTANT,
+		BLEND_CONSTANTALPHA,
+		BLEND_INVCONSTANTALPHA,
+
+		BLEND_LAST = BLEND_INVCONSTANT
+	};
+
+	enum BlendOperation : unsigned int
+	{
+		BLENDOP_ADD,
+		BLENDOP_SUB,
+		BLENDOP_INVSUB,
+		BLENDOP_MIN,
+		BLENDOP_MAX,
+
+		BLENDOP_SOURCE,   // Copy source
+		BLENDOP_DEST,     // Copy dest
+		BLENDOP_NULL,     // Nullify result
+
+		BLENDOP_LAST = BLENDOP_NULL
+	};
+
+	enum LogicalOperation : unsigned int
+	{
+		LOGICALOP_CLEAR,
+		LOGICALOP_SET,
+		LOGICALOP_COPY,
+		LOGICALOP_COPY_INVERTED,
+		LOGICALOP_NOOP,
+		LOGICALOP_INVERT,
+		LOGICALOP_AND,
+		LOGICALOP_NAND,
+		LOGICALOP_OR,
+		LOGICALOP_NOR,
+		LOGICALOP_XOR,
+		LOGICALOP_EQUIV,
+		LOGICALOP_AND_REVERSE,
+		LOGICALOP_AND_INVERTED,
+		LOGICALOP_OR_REVERSE,
+		LOGICALOP_OR_INVERTED,
+
+		LOGICALOP_LAST = LOGICALOP_OR_INVERTED
+	};
+
+	enum MaterialSource : unsigned int
+	{
+		MATERIAL_MATERIAL,
+		MATERIAL_COLOR1,
+		MATERIAL_COLOR2,
+
+		MATERIAL_LAST = MATERIAL_COLOR2
+	};
+
+	enum FogMode : unsigned int
+	{
+		FOG_NONE,
+		FOG_LINEAR,
+		FOG_EXP,
+		FOG_EXP2,
+
+		FOG_LAST = FOG_EXP2
+	};
+
+	enum TexGen : unsigned int
+	{
+		TEXGEN_PASSTHRU,
+		TEXGEN_NORMAL,
+		TEXGEN_POSITION,
+		TEXGEN_REFLECTION,
+		TEXGEN_SPHEREMAP,
+		TEXGEN_NONE,
+
+		TEXGEN_LAST = TEXGEN_NONE
+	};
+
+	enum TransparencyAntialiasing : unsigned int
+	{
+		TRANSPARENCY_NONE,
+		TRANSPARENCY_ALPHA_TO_COVERAGE,
+
+		TRANSPARENCY_LAST = TRANSPARENCY_ALPHA_TO_COVERAGE
+	};
+
+	class Context
+	{
+	public:
+		Context();
+
+		~Context();
+
+		void *operator new(size_t bytes);
+		void operator delete(void *pointer, size_t bytes);
+
+		bool isDrawPoint(bool fillModeAware = false) const;
+		bool isDrawLine(bool fillModeAware = false) const;
+		bool isDrawTriangle(bool fillModeAware = false) const;
+
+		void init();
+
+		const float &exp2Bias();   // NOTE: Needs address for JIT
+
+		const Point &getLightPosition(int light);
+
+		void setGlobalMipmapBias(float bias);
+
+		// Set fixed-function vertex pipeline states
+		void setLightingEnable(bool lightingEnable);
+		void setSpecularEnable(bool specularEnable);
+		void setLightEnable(int light, bool lightEnable);
+		void setLightPosition(int light, Point worldLightPosition);
+
+		void setColorVertexEnable(bool colorVertexEnable);
+		void setAmbientMaterialSource(MaterialSource ambientMaterialSource);
+		void setDiffuseMaterialSource(MaterialSource diffuseMaterialSource);
+		void setSpecularMaterialSource(MaterialSource specularMaterialSource);
+		void setEmissiveMaterialSource(MaterialSource emissiveMaterialSource);
+
+		void setPointSpriteEnable(bool pointSpriteEnable);
+		void setPointScaleEnable(bool pointScaleEnable);
+
+		// Set fixed-function pixel pipeline states, return true when modified
+		bool setDepthBufferEnable(bool depthBufferEnable);
+
+		bool setAlphaBlendEnable(bool alphaBlendEnable);
+		bool setSourceBlendFactor(BlendFactor sourceBlendFactor);
+		bool setDestBlendFactor(BlendFactor destBlendFactor);
+		bool setBlendOperation(BlendOperation blendOperation);
+
+		bool setSeparateAlphaBlendEnable(bool separateAlphaBlendEnable);
+		bool setSourceBlendFactorAlpha(BlendFactor sourceBlendFactorAlpha);
+		bool setDestBlendFactorAlpha(BlendFactor destBlendFactorAlpha);
+		bool setBlendOperationAlpha(BlendOperation blendOperationAlpha);
+
+		bool setColorWriteMask(int index, int colorWriteMask);
+		bool setWriteSRGB(bool sRGB);
+
+		bool setColorLogicOpEnabled(bool colorLogicOpEnabled);
+		bool setLogicalOperation(LogicalOperation logicalOperation);
+
+		// Active fixed-function pixel pipeline states
+		bool fogActive();
+		bool pointSizeActive();
+		FogMode pixelFogActive();
+		bool depthWriteActive();
+		bool alphaTestActive();
+		bool depthBufferActive();
+		bool stencilActive();
+
+		bool perspectiveActive();
+
+		// Active fixed-function vertex pipeline states
+		bool vertexLightingActive();
+		bool texCoordActive(int coordinate, int component);
+		bool texCoordActive(int coordinate);
+		bool isProjectionComponent(unsigned int coordinate, int component);
+		bool vertexSpecularInputActive();
+		bool vertexSpecularActive();
+		bool vertexNormalActive();
+		bool vertexLightActive();
+		bool vertexLightActive(int i);
+		MaterialSource vertexDiffuseMaterialSourceActive();
+		MaterialSource vertexSpecularMaterialSourceActive();
+		MaterialSource vertexAmbientMaterialSourceActive();
+		MaterialSource vertexEmissiveMaterialSourceActive();
+
+		bool pointSpriteActive();
+		bool pointScaleActive();
+
+		bool alphaBlendActive();
+		BlendFactor sourceBlendFactor();
+		BlendFactor destBlendFactor();
+		BlendOperation blendOperation();
+
+		BlendFactor sourceBlendFactorAlpha();
+		BlendFactor destBlendFactorAlpha();
+		BlendOperation blendOperationAlpha();
+
+		LogicalOperation colorLogicOp();
+		LogicalOperation indexLogicOp();
+
+		bool indexedVertexBlendActive();
+		int vertexBlendMatrixCountActive();
+		bool localViewerActive();
+		bool normalizeNormalsActive();
+		FogMode vertexFogModeActive();
+		bool rangeFogActive();
+
+		TexGen texGenActive(int stage);
+		int textureTransformCountActive(int stage);
+		int texCoordIndexActive(int stage);
+
+		// Active context states
+		bool diffuseUsed();     // Used by pixel processor but not provided by vertex processor
+		bool diffuseUsed(int component);     // Used by pixel processor but not provided by vertex processor
+		bool diffuseActive();
+		bool diffuseActive(int component);
+		bool specularUsed();
+		bool specularUsed(int component);
+		bool specularActive();
+		bool specularActive(int component);
+		bool colorActive(int color, int component);
+		bool textureActive();
+		bool textureActive(int coordinate);
+		bool textureActive(int coordinate, int component);
+
+		unsigned short pixelShaderVersion() const;
+		unsigned short vertexShaderVersion() const;
+
+		int getMultiSampleCount() const;
+		int getSuperSampleCount() const;
+
+		DrawType drawType;
+
+		bool stencilEnable;
+		StencilCompareMode stencilCompareMode;
+		int stencilReference;
+		int stencilMask;
+		StencilOperation stencilFailOperation;
+		StencilOperation stencilPassOperation;
+		StencilOperation stencilZFailOperation;
+		int stencilWriteMask;
+
+		bool twoSidedStencil;
+		StencilCompareMode stencilCompareModeCCW;
+		int stencilReferenceCCW;
+		int stencilMaskCCW;
+		StencilOperation stencilFailOperationCCW;
+		StencilOperation stencilPassOperationCCW;
+		StencilOperation stencilZFailOperationCCW;
+		int stencilWriteMaskCCW;
+
+		// Pixel processor states
+		AlphaCompareMode alphaCompareMode;
+		bool alphaTestEnable;
+		FillMode fillMode;
+		ShadingMode shadingMode;
+
+		CullMode cullMode;
+		float alphaReference;
+
+		TextureStage textureStage[8];
+		Sampler sampler[TOTAL_IMAGE_UNITS];
+
+		Format renderTargetInternalFormat(int index);
+		int colorWriteActive();
+		int colorWriteActive(int index);
+		bool colorUsed();
+
+		Resource *texture[TOTAL_IMAGE_UNITS];
+		Stream input[VERTEX_ATTRIBUTES];
+		Resource *indexBuffer;
+
+		bool preTransformed;   // FIXME: Private
+
+		float fogStart;
+		float fogEnd;
+
+		void computeIllumination();
+
+		bool textureWrapActive;
+		unsigned char textureWrap[TEXTURE_IMAGE_UNITS];
+		TexGen texGen[8];
+		bool localViewer;
+		bool normalizeNormals;
+		int textureTransformCount[8];
+		bool textureTransformProject[8];
+
+		Surface *renderTarget[RENDERTARGETS];
+		Surface *depthBuffer;
+		Surface *stencilBuffer;
+
+		// Fog
+		bool fogEnable;
+		FogMode pixelFogMode;
+		FogMode vertexFogMode;
+		bool wBasedFog;
+		bool rangeFogEnable;
+
+		// Vertex blending
+		bool indexedVertexBlendEnable;
+		int vertexBlendMatrixCount;
+
+		// Shaders
+		const PixelShader *pixelShader;
+		const VertexShader *vertexShader;
+
+		// Global mipmap bias
+		float bias;
+
+		// Instancing
+		int instanceID;
+
+		// Fixed-function vertex pipeline state
+		bool lightingEnable;
+		bool specularEnable;
+		bool lightEnable[8];
+		Point worldLightPosition[8];
+
+		MaterialSource ambientMaterialSource;
+		MaterialSource diffuseMaterialSource;
+		MaterialSource specularMaterialSource;
+		MaterialSource emissiveMaterialSource;
+		bool colorVertexEnable;
+
+		bool occlusionEnabled;
+		bool transformFeedbackQueryEnabled;
+		uint64_t transformFeedbackEnabled;
+
+		// Pixel processor states
+		bool rasterizerDiscard;
+		bool depthBufferEnable;
+		DepthCompareMode depthCompareMode;
+		bool depthWriteEnable;
+
+		bool alphaBlendEnable;
+		BlendFactor sourceBlendFactorState;
+		BlendFactor destBlendFactorState;
+		BlendOperation blendOperationState;
+
+		bool separateAlphaBlendEnable;
+		BlendFactor sourceBlendFactorStateAlpha;
+		BlendFactor destBlendFactorStateAlpha;
+		BlendOperation blendOperationStateAlpha;
+
+		bool pointSpriteEnable;
+		bool pointScaleEnable;
+		float lineWidth;
+
+		int colorWriteMask[RENDERTARGETS];   // RGBA
+		bool writeSRGB;
+		unsigned int sampleMask;
+		unsigned int multiSampleMask;
+
+		bool colorLogicOpEnabled;
+		LogicalOperation logicalOperation;
+	};
+}
+
+#endif   // sw_Context_hpp
diff --git a/src/Renderer/ETC_Decoder.cpp b/src/Renderer/ETC_Decoder.cpp
index 6d98aa2..8e109f3 100644
--- a/src/Renderer/ETC_Decoder.cpp
+++ b/src/Renderer/ETC_Decoder.cpp
@@ -1,709 +1,712 @@
-// SwiftShader Software Renderer

-//

-// Copyright(c) 2015 Google 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 Google Inc. Without such an agreement, no rights or licenses, express

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

-//

-

-#include "ETC_Decoder.hpp"

-

-namespace

-{

-	inline int clampByte(int value)

-	{

-		return (value < 0) ? 0 : ((value > 255) ? 255 : value);

-	}

-

-	inline int clampSByte(int value)

-	{

-		return (value < -128) ? -128 : ((value > 127) ? 127 : value);

-	}

-

-	struct bgra8

-	{

-		unsigned char b;

-		unsigned char g;

-		unsigned char r;

-		unsigned char a;

-

-		inline bgra8()

-		{

-		}

-

-		inline void set(int red, int green, int blue)

-		{

-			r = static_cast<unsigned char>(clampByte(red));

-			g = static_cast<unsigned char>(clampByte(green));

-			b = static_cast<unsigned char>(clampByte(blue));

-		}

-

-		inline void set(int red, int green, int blue, int alpha)

-		{

-			r = static_cast<unsigned char>(clampByte(red));

-			g = static_cast<unsigned char>(clampByte(green));

-			b = static_cast<unsigned char>(clampByte(blue));

-			a = static_cast<unsigned char>(clampByte(alpha));

-		}

-

-		const bgra8& addA(int alpha)

-		{

-			a = alpha;

-			return *this;

-		}

-	};

-

-	inline int extend_4to8bits(int x)

-	{

-		return (x << 4) | x;

-	}

-

-	inline int extend_5to8bits(int x)

-	{

-		return (x << 3) | (x >> 2);

-	}

-

-	inline int extend_6to8bits(int x)

-	{

-		return (x << 2) | (x >> 4);

-	}

-

-	inline int extend_7to8bits(int x)

-	{

-		return (x << 1) | (x >> 6);

-	}

-

-	struct ETC2

-	{

-		// Decodes unsigned single or dual channel block to bytes

-		static void DecodeBlock(const ETC2** sources, unsigned char *dest, int nbChannels, int x, int y, int w, int h, int pitch, bool isSigned)

-		{

-			if(isSigned)

-			{

-				signed char* sDst = reinterpret_cast<signed char*>(dest);

-				for(int j = 0; j < 4 && (y + j) < h; j++)

-				{

-					for(int i = 0; i < 4 && (x + i) < w; i++)

-					{

-						for(int c = nbChannels - 1; c >= 0; c--)

-						{

-							sDst[i * nbChannels + c] = clampSByte(sources[c]->getSingleChannel(i, j, isSigned));

-						}

-					}

-					sDst += pitch;

-				}

-			}

-			else

-			{

-				for(int j = 0; j < 4 && (y + j) < h; j++)

-				{

-					for(int i = 0; i < 4 && (x + i) < w; i++)

-					{

-						for(int c = nbChannels - 1; c >= 0; c--)

-						{

-							dest[i * nbChannels + c] = clampByte(sources[c]->getSingleChannel(i, j, isSigned));

-						}

-					}

-					dest += pitch;

-				}

-			}

-		}

-

-		// Decodes RGB block to bgra8

-		void decodeBlock(unsigned char *dest, int x, int y, int w, int h, int pitch, unsigned char alphaValues[4][4], bool punchThroughAlpha) const

-		{

-			bool opaqueBit = diffbit;

-			bool nonOpaquePunchThroughAlpha = punchThroughAlpha && !opaqueBit;

-

-			// Select mode

-			if(diffbit || punchThroughAlpha)

-			{

-				int r = (R + dR);

-				int g = (G + dG);

-				int b = (B + dB);

-				if(r < 0 || r > 31)

-				{

-					decodeTBlock(dest, x, y, w, h, pitch, alphaValues, nonOpaquePunchThroughAlpha);

-				}

-				else if(g < 0 || g > 31)

-				{

-					decodeHBlock(dest, x, y, w, h, pitch, alphaValues, nonOpaquePunchThroughAlpha);

-				}

-				else if(b < 0 || b > 31)

-				{

-					decodePlanarBlock(dest, x, y, w, h, pitch, alphaValues);

-				}

-				else

-				{

-					decodeDifferentialBlock(dest, x, y, w, h, pitch, alphaValues, nonOpaquePunchThroughAlpha);

-				}

-			}

-			else

-			{

-				decodeIndividualBlock(dest, x, y, w, h, pitch, alphaValues, nonOpaquePunchThroughAlpha);

-			}

-		}

-

-	private:

-		struct

-		{

-			union

-			{

-				// Individual, differential, H and T modes

-				struct

-				{

-					union

-					{

-						// Individual and differential modes

-						struct

-						{

-							union

-							{

-								struct   // Individual colors

-								{

-									unsigned char R2 : 4;

-									unsigned char R1 : 4;

-									unsigned char G2 : 4;

-									unsigned char G1 : 4;

-									unsigned char B2 : 4;

-									unsigned char B1 : 4;

-								};

-

-								struct   // Differential colors

-								{

-									signed char dR : 3;

-									unsigned char R : 5;

-									signed char dG : 3;

-									unsigned char G : 5;

-									signed char dB : 3;

-									unsigned char B : 5;

-								};

-							};

-

-							bool flipbit : 1;

-							bool diffbit : 1;

-							unsigned char cw2 : 3;

-							unsigned char cw1 : 3;

-						};

-

-						// T mode

-						struct

-						{

-							// Byte 1

-							unsigned char TR1b : 2;

-							unsigned char TdummyB : 1;

-							unsigned char TR1a : 2;

-							unsigned char TdummyA : 3;

-

-							// Byte 2

-							unsigned char TB1 : 4;

-							unsigned char TG1 : 4;

-

-							// Byte 3

-							unsigned char TG2 : 4;

-							unsigned char TR2 : 4;

-

-							// Byte 4

-							unsigned char Tdb : 1;

-							bool Tflipbit : 1;

-							unsigned char Tda : 2;

-							unsigned char TB2 : 4;

-						};

-

-						// H mode

-						struct

-						{

-							// Byte 1

-							unsigned char HG1a : 3;

-							unsigned char HR1 : 4;

-							unsigned char HdummyA : 1;

-

-							// Byte 2

-							unsigned char HB1b : 2;

-							unsigned char HdummyC : 1;

-							unsigned char HB1a : 1;

-							unsigned char HG1b : 1;

-							unsigned char HdummyB : 3;

-

-							// Byte 3

-							unsigned char HG2a : 3;

-							unsigned char HR2 : 4;

-							unsigned char HB1c : 1;

-

-							// Byte 4

-							unsigned char Hdb : 1;

-							bool Hflipbit : 1;

-							unsigned char Hda : 1;

-							unsigned char HB2 : 4;

-							unsigned char HG2b : 1;

-						};

-					};

-

-					unsigned char pixelIndexMSB[2];

-					unsigned char pixelIndexLSB[2];

-				};

-

-				// planar mode

-				struct

-				{

-					// Byte 1

-					unsigned char GO1 : 1;

-					unsigned char RO : 6;

-					unsigned char PdummyA : 1;

-

-					// Byte 2

-					unsigned char BO1 : 1;

-					unsigned char GO2 : 6;

-					unsigned char PdummyB : 1;

-

-					// Byte 3

-					unsigned char BO3a : 2;

-					unsigned char PdummyD : 1;

-					unsigned char BO2 : 2;

-					unsigned char PdummyC : 3;

-

-					// Byte 4

-					unsigned char RH2 : 1;

-					bool Pflipbit : 1;

-					unsigned char RH1 : 5;

-					unsigned char BO3b : 1;

-

-					// Byte 5

-					unsigned char BHa : 1;

-					unsigned char GH : 7;

-

-					// Byte 6

-					unsigned char RVa : 3;

-					unsigned char BHb : 5;

-

-					// Byte 7

-					unsigned char GVa : 5;

-					unsigned char RVb : 3;

-

-					// Byte 8

-					unsigned char BV : 6;

-					unsigned char GVb : 2;

-				};

-

-				// Single channel block

-				struct

-				{

-					union

-					{

-						unsigned char base_codeword;

-						signed char signed_base_codeword;

-					};

-

-					unsigned char table_index : 4;

-					unsigned char multiplier : 4;

-

-					unsigned char mc1 : 2;

-					unsigned char mb : 3;

-					unsigned char ma : 3;

-

-					unsigned char mf1 : 1;

-					unsigned char me : 3;

-					unsigned char md : 3;

-					unsigned char mc2 : 1;

-

-					unsigned char mh : 3;

-					unsigned char mg : 3;

-					unsigned char mf2 : 2;

-

-					unsigned char mk1 : 2;

-					unsigned char mj : 3;

-					unsigned char mi : 3;

-

-					unsigned char mn1 : 1;

-					unsigned char mm : 3;

-					unsigned char ml : 3;

-					unsigned char mk2 : 1;

-

-					unsigned char mp : 3;

-					unsigned char mo : 3;

-					unsigned char mn2 : 2;

-				};

-			};

-		};

-

-		void decodeIndividualBlock(unsigned char *dest, int x, int y, int w, int h, int pitch, unsigned char alphaValues[4][4], bool nonOpaquePunchThroughAlpha) const

-		{

-			int r1 = extend_4to8bits(R1);

-			int g1 = extend_4to8bits(G1);

-			int b1 = extend_4to8bits(B1);

-

-			int r2 = extend_4to8bits(R2);

-			int g2 = extend_4to8bits(G2);

-			int b2 = extend_4to8bits(B2);

-

-			decodeIndividualOrDifferentialBlock(dest, x, y, w, h, pitch, r1, g1, b1, r2, g2, b2, alphaValues, nonOpaquePunchThroughAlpha);

-		}

-

-		void decodeDifferentialBlock(unsigned char *dest, int x, int y, int w, int h, int pitch, unsigned char alphaValues[4][4], bool nonOpaquePunchThroughAlpha) const

-		{

-			int b1 = extend_5to8bits(B);

-			int g1 = extend_5to8bits(G);

-			int r1 = extend_5to8bits(R);

-

-			int r2 = extend_5to8bits(R + dR);

-			int g2 = extend_5to8bits(G + dG);

-			int b2 = extend_5to8bits(B + dB);

-

-			decodeIndividualOrDifferentialBlock(dest, x, y, w, h, pitch, r1, g1, b1, r2, g2, b2, alphaValues, nonOpaquePunchThroughAlpha);

-		}

-

-		void decodeIndividualOrDifferentialBlock(unsigned char *dest, int x, int y, int w, int h, int pitch, int r1, int g1, int b1, int r2, int g2, int b2, unsigned char alphaValues[4][4], bool nonOpaquePunchThroughAlpha) const

-		{

-			// Table 3.17.2 sorted according to table 3.17.3

-			static const int intensityModifierDefault[8][4] =

-			{

-				{ 2, 8, -2, -8 },

-				{ 5, 17, -5, -17 },

-				{ 9, 29, -9, -29 },

-				{ 13, 42, -13, -42 },

-				{ 18, 60, -18, -60 },

-				{ 24, 80, -24, -80 },

-				{ 33, 106, -33, -106 },

-				{ 47, 183, -47, -183 }

-			};

-

-			// Table C.12, intensity modifier for non opaque punchthrough alpha

-			static const int intensityModifierNonOpaque[8][4] =

-			{

-				{ 0, 8, 0, -8 },

-				{ 0, 17, 0, -17 },

-				{ 0, 29, 0, -29 },

-				{ 0, 42, 0, -42 },

-				{ 0, 60, 0, -60 },

-				{ 0, 80, 0, -80 },

-				{ 0, 106, 0, -106 },

-				{ 0, 183, 0, -183 }

-			};

-

-			const int(&intensityModifier)[8][4] = nonOpaquePunchThroughAlpha ? intensityModifierNonOpaque : intensityModifierDefault;

-

-			bgra8 subblockColors0[4];

-			bgra8 subblockColors1[4];

-

-			const int i10 = intensityModifier[cw1][0];

-			const int i11 = intensityModifier[cw1][1];

-			const int i12 = intensityModifier[cw1][2];

-			const int i13 = intensityModifier[cw1][3];

-

-			subblockColors0[0].set(r1 + i10, g1 + i10, b1 + i10);

-			subblockColors0[1].set(r1 + i11, g1 + i11, b1 + i11);

-			subblockColors0[2].set(r1 + i12, g1 + i12, b1 + i12);

-			subblockColors0[3].set(r1 + i13, g1 + i13, b1 + i13);

-

-			const int i20 = intensityModifier[cw2][0];

-			const int i21 = intensityModifier[cw2][1];

-			const int i22 = intensityModifier[cw2][2];

-			const int i23 = intensityModifier[cw2][3];

-

-			subblockColors1[0].set(r2 + i20, g2 + i20, b2 + i20);

-			subblockColors1[1].set(r2 + i21, g2 + i21, b2 + i21);

-			subblockColors1[2].set(r2 + i22, g2 + i22, b2 + i22);

-			subblockColors1[3].set(r2 + i23, g2 + i23, b2 + i23);

-

-			unsigned char* destStart = dest;

-

-			if(flipbit)

-			{

-				for(int j = 0; j < 2 && (y + j) < h; j++)

-				{

-					bgra8* color = (bgra8*)dest;

-					if((x + 0) < w) color[0] = subblockColors0[getIndex(0, j)].addA(alphaValues[j][0]);

-					if((x + 1) < w) color[1] = subblockColors0[getIndex(1, j)].addA(alphaValues[j][1]);

-					if((x + 2) < w) color[2] = subblockColors0[getIndex(2, j)].addA(alphaValues[j][2]);

-					if((x + 3) < w) color[3] = subblockColors0[getIndex(3, j)].addA(alphaValues[j][3]);

-					dest += pitch;

-				}

-

-				for(int j = 2; j < 4 && (y + j) < h; j++)

-				{

-					bgra8* color = (bgra8*)dest;

-					if((x + 0) < w) color[0] = subblockColors1[getIndex(0, j)].addA(alphaValues[j][0]);

-					if((x + 1) < w) color[1] = subblockColors1[getIndex(1, j)].addA(alphaValues[j][1]);

-					if((x + 2) < w) color[2] = subblockColors1[getIndex(2, j)].addA(alphaValues[j][2]);

-					if((x + 3) < w) color[3] = subblockColors1[getIndex(3, j)].addA(alphaValues[j][3]);

-					dest += pitch;

-				}

-			}

-			else

-			{

-				for(int j = 0; j < 4 && (y + j) < h; j++)

-				{

-					bgra8* color = (bgra8*)dest;

-					if((x + 0) < w) color[0] = subblockColors0[getIndex(0, j)].addA(alphaValues[j][0]);

-					if((x + 1) < w) color[1] = subblockColors0[getIndex(1, j)].addA(alphaValues[j][1]);

-					if((x + 2) < w) color[2] = subblockColors1[getIndex(2, j)].addA(alphaValues[j][2]);

-					if((x + 3) < w) color[3] = subblockColors1[getIndex(3, j)].addA(alphaValues[j][3]);

-					dest += pitch;

-				}

-			}

-

-			if(nonOpaquePunchThroughAlpha)

-			{

-				decodePunchThroughAlphaBlock(destStart, x, y, w, h, pitch);

-			}

-		}

-

-		void decodeTBlock(unsigned char *dest, int x, int y, int w, int h, int pitch, unsigned char alphaValues[4][4], bool nonOpaquePunchThroughAlpha) const

-		{

-			// Table C.8, distance index fot T and H modes

-			static const int distance[8] = { 3, 6, 11, 16, 23, 32, 41, 64 };

-

-			bgra8 paintColors[4];

-

-			int r1 = extend_4to8bits(TR1a << 2 | TR1b);

-			int g1 = extend_4to8bits(TG1);

-			int b1 = extend_4to8bits(TB1);

-

-			int r2 = extend_4to8bits(TR2);

-			int g2 = extend_4to8bits(TG2);

-			int b2 = extend_4to8bits(TB2);

-

-			const int d = distance[Tda << 1 | Tdb];

-

-			paintColors[0].set(r1, g1, b1);

-			paintColors[1].set(r2 + d, g2 + d, b2 + d);

-			paintColors[2].set(r2, g2, b2);

-			paintColors[3].set(r2 - d, g2 - d, b2 - d);

-

-			unsigned char* destStart = dest;

-

-			for(int j = 0; j < 4 && (y + j) < h; j++)

-			{

-				bgra8* color = (bgra8*)dest;

-				if((x + 0) < w) color[0] = paintColors[getIndex(0, j)].addA(alphaValues[j][0]);

-				if((x + 1) < w) color[1] = paintColors[getIndex(1, j)].addA(alphaValues[j][1]);

-				if((x + 2) < w) color[2] = paintColors[getIndex(2, j)].addA(alphaValues[j][2]);

-				if((x + 3) < w) color[3] = paintColors[getIndex(3, j)].addA(alphaValues[j][3]);

-				dest += pitch;

-			}

-

-			if(nonOpaquePunchThroughAlpha)

-			{

-				decodePunchThroughAlphaBlock(destStart, x, y, w, h, pitch);

-			}

-		}

-

-		void decodeHBlock(unsigned char *dest, int x, int y, int w, int h, int pitch, unsigned char alphaValues[4][4], bool nonOpaquePunchThroughAlpha) const

-		{

-			// Table C.8, distance index fot T and H modes

-			static const int distance[8] = { 3, 6, 11, 16, 23, 32, 41, 64 };

-

-			bgra8 paintColors[4];

-

-			int r1 = extend_4to8bits(HR1);

-			int g1 = extend_4to8bits(HG1a << 1 | HG1b);

-			int b1 = extend_4to8bits(HB1a << 3 | HB1b << 1 | HB1c);

-

-			int r2 = extend_4to8bits(HR2);

-			int g2 = extend_4to8bits(HG2a << 1 | HG2b);

-			int b2 = extend_4to8bits(HB2);

-

-			const int d = distance[(Hda << 2) | (Hdb << 1) | ((r1 << 16 | g1 << 8 | b1) >= (r2 << 16 | g2 << 8 | b2) ? 1 : 0)];

-

-			paintColors[0].set(r1 + d, g1 + d, b1 + d);

-			paintColors[1].set(r1 - d, g1 - d, b1 - d);

-			paintColors[2].set(r2 + d, g2 + d, b2 + d);

-			paintColors[3].set(r2 - d, g2 - d, b2 - d);

-

-			unsigned char* destStart = dest;

-

-			for(int j = 0; j < 4 && (y + j) < h; j++)

-			{

-				bgra8* color = (bgra8*)dest;

-				if((x + 0) < w) color[0] = paintColors[getIndex(0, j)].addA(alphaValues[j][0]);

-				if((x + 1) < w) color[1] = paintColors[getIndex(1, j)].addA(alphaValues[j][1]);

-				if((x + 2) < w) color[2] = paintColors[getIndex(2, j)].addA(alphaValues[j][2]);

-				if((x + 3) < w) color[3] = paintColors[getIndex(3, j)].addA(alphaValues[j][3]);

-				dest += pitch;

-			}

-

-			if(nonOpaquePunchThroughAlpha)

-			{

-				decodePunchThroughAlphaBlock(destStart, x, y, w, h, pitch);

-			}

-		}

-

-		void decodePlanarBlock(unsigned char *dest, int x, int y, int w, int h, int pitch, unsigned char alphaValues[4][4]) const

-		{

-			int ro = extend_6to8bits(RO);

-			int go = extend_7to8bits(GO1 << 6 | GO2);

-			int bo = extend_6to8bits(BO1 << 5 | BO2 << 3 | BO3a << 1 | BO3b);

-

-			int rh = extend_6to8bits(RH1 << 1 | RH2);

-			int gh = extend_7to8bits(GH);

-			int bh = extend_6to8bits(BHa << 5 | BHb);

-

-			int rv = extend_6to8bits(RVa << 3 | RVb);

-			int gv = extend_7to8bits(GVa << 2 | GVb);

-			int bv = extend_6to8bits(BV);

-

-			for(int j = 0; j < 4 && (y + j) < h; j++)

-			{

-				int ry = j * (rv - ro) + 2;

-				int gy = j * (gv - go) + 2;

-				int by = j * (bv - bo) + 2;

-				for(int i = 0; i < 4 && (x + i) < w; i++)

-				{

-					((bgra8*)(dest))[i].set(((i * (rh - ro) + ry) >> 2) + ro,

-						((i * (gh - go) + gy) >> 2) + go,

-						((i * (bh - bo) + by) >> 2) + bo,

-						alphaValues[j][i]);

-				}

-				dest += pitch;

-			}

-		}

-

-		// Index for individual, differential, H and T modes

-		inline int getIndex(int x, int y) const

-		{

-			int bitIndex = x * 4 + y;

-			int bitOffset = bitIndex & 7;

-			int lsb = (pixelIndexLSB[1 - (bitIndex >> 3)] >> bitOffset) & 1;

-			int msb = (pixelIndexMSB[1 - (bitIndex >> 3)] >> bitOffset) & 1;

-

-			return (msb << 1) | lsb;

-		}

-

-		void decodePunchThroughAlphaBlock(unsigned char *dest, int x, int y, int w, int h, int pitch) const

-		{

-			for(int j = 0; j < 4 && (y + j) < h; j++)

-			{

-				for(int i = 0; i < 4 && (x + i) < w; i++)

-				{

-					if(getIndex(i, j) == 2) //  msb == 1 && lsb == 0

-					{

-						((bgra8*)dest)[i].set(0, 0, 0, 0);

-					}

-				}

-				dest += pitch;

-			}

-		}

-

-		// Single channel utility functions

-		inline int getSingleChannel(int x, int y, bool isSigned) const

-		{

-			int codeword = isSigned ? signed_base_codeword : base_codeword;

-			return codeword + getSingleChannelModifier(x, y) * multiplier;

-		}

-

-		inline int getSingleChannelIndex(int x, int y) const

-		{

-			switch(x * 4 + y)

-			{

-			case 0: return ma;

-			case 1: return mb;

-			case 2: return mc1 << 1 | mc2;

-			case 3: return md;

-			case 4: return me;

-			case 5: return mf1 << 2 | mf2;

-			case 6: return mg;

-			case 7: return mh;

-			case 8: return mi;

-			case 9: return mj;

-			case 10: return mk1 << 1 | mk2;

-			case 11: return ml;

-			case 12: return mm;

-			case 13: return mn1 << 2 | mn2;

-			case 14: return mo;

-			default: return mp; // 15

-			}

-		}

-

-		inline int getSingleChannelModifier(int x, int y) const

-		{

-			static const int modifierTable[16][8] = { { -3, -6, -9, -15, 2, 5, 8, 14 },

-			{ -3, -7, -10, -13, 2, 6, 9, 12 },

-			{ -2, -5, -8, -13, 1, 4, 7, 12 },

-			{ -2, -4, -6, -13, 1, 3, 5, 12 },

-			{ -3, -6, -8, -12, 2, 5, 7, 11 },

-			{ -3, -7, -9, -11, 2, 6, 8, 10 },

-			{ -4, -7, -8, -11, 3, 6, 7, 10 },

-			{ -3, -5, -8, -11, 2, 4, 7, 10 },

-			{ -2, -6, -8, -10, 1, 5, 7, 9 },

-			{ -2, -5, -8, -10, 1, 4, 7, 9 },

-			{ -2, -4, -8, -10, 1, 3, 7, 9 },

-			{ -2, -5, -7, -10, 1, 4, 6, 9 },

-			{ -3, -4, -7, -10, 2, 3, 6, 9 },

-			{ -1, -2, -3, -10, 0, 1, 2, 9 },

-			{ -4, -6, -8, -9, 3, 5, 7, 8 },

-			{ -3, -5, -7, -9, 2, 4, 6, 8 } };

-

-			return modifierTable[table_index][getSingleChannelIndex(x, y)];

-		}

-	};

-}

-

-// Decodes 1 to 4 channel images to 8 bit output

-bool ETC_Decoder::Decode(const unsigned char* src, unsigned char *dst, int w, int h, int dstW, int dstH, int dstPitch, int dstBpp, InputType inputType)

-{

-	const ETC2* sources[2];

-	sources[0] = (const ETC2*)src;

-

-	unsigned char alphaValues[4][4] = { { 255, 255, 255, 255 }, { 255, 255, 255, 255 }, { 255, 255, 255, 255 }, { 255, 255, 255, 255 } };

-

-	switch(inputType)

-	{

-	case ETC_R_SIGNED:

-	case ETC_R_UNSIGNED:

-		for(int y = 0; y < h; y += 4)

-		{

-			unsigned char *dstRow = dst + (y * dstPitch);

-			for(int x = 0; x < w; x += 4, sources[0]++)

-			{

-				ETC2::DecodeBlock(sources, dstRow + (x * dstBpp), 1, x, y, dstW, dstH, dstPitch, inputType == ETC_R_SIGNED);

-			}

-		}

-		break;

-	case ETC_RG_SIGNED:

-	case ETC_RG_UNSIGNED:

-		sources[1] = sources[0] + 1;

-		for(int y = 0; y < h; y += 4)

-		{

-			unsigned char *dstRow = dst + (y * dstPitch);

-			for(int x = 0; x < w; x += 4, sources[0] += 2, sources[1] += 2)

-			{

-				ETC2::DecodeBlock(sources, dstRow + (x * dstBpp), 2, x, y, dstW, dstH, dstPitch, inputType == ETC_RG_SIGNED);

-			}

-		}

-		break;

-	case ETC_RGB:

-	case ETC_RGB_PUNCHTHROUGH_ALPHA:

-		for(int y = 0; y < h; y += 4)

-		{

-			unsigned char *dstRow = dst + (y * dstPitch);

-			for(int x = 0; x < w; x += 4, sources[0]++)

-			{

-				sources[0]->decodeBlock(dstRow + (x * dstBpp), x, y, dstW, dstH, dstPitch, alphaValues, inputType == ETC_RGB_PUNCHTHROUGH_ALPHA);

-			}

-		}

-		break;

-	case ETC_RGBA:

-		for(int y = 0; y < h; y += 4)

-		{

-			unsigned char *dstRow = dst + (y * dstPitch);

-			for(int x = 0; x < w; x += 4)

-			{

-				// Decode Alpha

-				ETC2::DecodeBlock(&sources[0], &(alphaValues[0][0]), 1, x, y, dstW, dstH, 4, false);

-				sources[0]++; // RGBA packets are 128 bits, so move on to the next 64 bit packet to decode the RGB color

-

-				// Decode RGB

-				sources[0]->decodeBlock(dstRow + (x * dstBpp), x, y, dstW, dstH, dstPitch, alphaValues, false);

-				sources[0]++;

-			}

-		}

-		break;

-	default:

-		return false;

-	}

-

-	return true;

-}

+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "ETC_Decoder.hpp"
+
+namespace
+{
+	inline int clampByte(int value)
+	{
+		return (value < 0) ? 0 : ((value > 255) ? 255 : value);
+	}
+
+	inline int clampSByte(int value)
+	{
+		return (value < -128) ? -128 : ((value > 127) ? 127 : value);
+	}
+
+	struct bgra8
+	{
+		unsigned char b;
+		unsigned char g;
+		unsigned char r;
+		unsigned char a;
+
+		inline bgra8()
+		{
+		}
+
+		inline void set(int red, int green, int blue)
+		{
+			r = static_cast<unsigned char>(clampByte(red));
+			g = static_cast<unsigned char>(clampByte(green));
+			b = static_cast<unsigned char>(clampByte(blue));
+		}
+
+		inline void set(int red, int green, int blue, int alpha)
+		{
+			r = static_cast<unsigned char>(clampByte(red));
+			g = static_cast<unsigned char>(clampByte(green));
+			b = static_cast<unsigned char>(clampByte(blue));
+			a = static_cast<unsigned char>(clampByte(alpha));
+		}
+
+		const bgra8& addA(int alpha)
+		{
+			a = alpha;
+			return *this;
+		}
+	};
+
+	inline int extend_4to8bits(int x)
+	{
+		return (x << 4) | x;
+	}
+
+	inline int extend_5to8bits(int x)
+	{
+		return (x << 3) | (x >> 2);
+	}
+
+	inline int extend_6to8bits(int x)
+	{
+		return (x << 2) | (x >> 4);
+	}
+
+	inline int extend_7to8bits(int x)
+	{
+		return (x << 1) | (x >> 6);
+	}
+
+	struct ETC2
+	{
+		// Decodes unsigned single or dual channel block to bytes
+		static void DecodeBlock(const ETC2** sources, unsigned char *dest, int nbChannels, int x, int y, int w, int h, int pitch, bool isSigned)
+		{
+			if(isSigned)
+			{
+				signed char* sDst = reinterpret_cast<signed char*>(dest);
+				for(int j = 0; j < 4 && (y + j) < h; j++)
+				{
+					for(int i = 0; i < 4 && (x + i) < w; i++)
+					{
+						for(int c = nbChannels - 1; c >= 0; c--)
+						{
+							sDst[i * nbChannels + c] = clampSByte(sources[c]->getSingleChannel(i, j, isSigned));
+						}
+					}
+					sDst += pitch;
+				}
+			}
+			else
+			{
+				for(int j = 0; j < 4 && (y + j) < h; j++)
+				{
+					for(int i = 0; i < 4 && (x + i) < w; i++)
+					{
+						for(int c = nbChannels - 1; c >= 0; c--)
+						{
+							dest[i * nbChannels + c] = clampByte(sources[c]->getSingleChannel(i, j, isSigned));
+						}
+					}
+					dest += pitch;
+				}
+			}
+		}
+
+		// Decodes RGB block to bgra8
+		void decodeBlock(unsigned char *dest, int x, int y, int w, int h, int pitch, unsigned char alphaValues[4][4], bool punchThroughAlpha) const
+		{
+			bool opaqueBit = diffbit;
+			bool nonOpaquePunchThroughAlpha = punchThroughAlpha && !opaqueBit;
+
+			// Select mode
+			if(diffbit || punchThroughAlpha)
+			{
+				int r = (R + dR);
+				int g = (G + dG);
+				int b = (B + dB);
+				if(r < 0 || r > 31)
+				{
+					decodeTBlock(dest, x, y, w, h, pitch, alphaValues, nonOpaquePunchThroughAlpha);
+				}
+				else if(g < 0 || g > 31)
+				{
+					decodeHBlock(dest, x, y, w, h, pitch, alphaValues, nonOpaquePunchThroughAlpha);
+				}
+				else if(b < 0 || b > 31)
+				{
+					decodePlanarBlock(dest, x, y, w, h, pitch, alphaValues);
+				}
+				else
+				{
+					decodeDifferentialBlock(dest, x, y, w, h, pitch, alphaValues, nonOpaquePunchThroughAlpha);
+				}
+			}
+			else
+			{
+				decodeIndividualBlock(dest, x, y, w, h, pitch, alphaValues, nonOpaquePunchThroughAlpha);
+			}
+		}
+
+	private:
+		struct
+		{
+			union
+			{
+				// Individual, differential, H and T modes
+				struct
+				{
+					union
+					{
+						// Individual and differential modes
+						struct
+						{
+							union
+							{
+								struct   // Individual colors
+								{
+									unsigned char R2 : 4;
+									unsigned char R1 : 4;
+									unsigned char G2 : 4;
+									unsigned char G1 : 4;
+									unsigned char B2 : 4;
+									unsigned char B1 : 4;
+								};
+
+								struct   // Differential colors
+								{
+									signed char dR : 3;
+									unsigned char R : 5;
+									signed char dG : 3;
+									unsigned char G : 5;
+									signed char dB : 3;
+									unsigned char B : 5;
+								};
+							};
+
+							bool flipbit : 1;
+							bool diffbit : 1;
+							unsigned char cw2 : 3;
+							unsigned char cw1 : 3;
+						};
+
+						// T mode
+						struct
+						{
+							// Byte 1
+							unsigned char TR1b : 2;
+							unsigned char TdummyB : 1;
+							unsigned char TR1a : 2;
+							unsigned char TdummyA : 3;
+
+							// Byte 2
+							unsigned char TB1 : 4;
+							unsigned char TG1 : 4;
+
+							// Byte 3
+							unsigned char TG2 : 4;
+							unsigned char TR2 : 4;
+
+							// Byte 4
+							unsigned char Tdb : 1;
+							bool Tflipbit : 1;
+							unsigned char Tda : 2;
+							unsigned char TB2 : 4;
+						};
+
+						// H mode
+						struct
+						{
+							// Byte 1
+							unsigned char HG1a : 3;
+							unsigned char HR1 : 4;
+							unsigned char HdummyA : 1;
+
+							// Byte 2
+							unsigned char HB1b : 2;
+							unsigned char HdummyC : 1;
+							unsigned char HB1a : 1;
+							unsigned char HG1b : 1;
+							unsigned char HdummyB : 3;
+
+							// Byte 3
+							unsigned char HG2a : 3;
+							unsigned char HR2 : 4;
+							unsigned char HB1c : 1;
+
+							// Byte 4
+							unsigned char Hdb : 1;
+							bool Hflipbit : 1;
+							unsigned char Hda : 1;
+							unsigned char HB2 : 4;
+							unsigned char HG2b : 1;
+						};
+					};
+
+					unsigned char pixelIndexMSB[2];
+					unsigned char pixelIndexLSB[2];
+				};
+
+				// planar mode
+				struct
+				{
+					// Byte 1
+					unsigned char GO1 : 1;
+					unsigned char RO : 6;
+					unsigned char PdummyA : 1;
+
+					// Byte 2
+					unsigned char BO1 : 1;
+					unsigned char GO2 : 6;
+					unsigned char PdummyB : 1;
+
+					// Byte 3
+					unsigned char BO3a : 2;
+					unsigned char PdummyD : 1;
+					unsigned char BO2 : 2;
+					unsigned char PdummyC : 3;
+
+					// Byte 4
+					unsigned char RH2 : 1;
+					bool Pflipbit : 1;
+					unsigned char RH1 : 5;
+					unsigned char BO3b : 1;
+
+					// Byte 5
+					unsigned char BHa : 1;
+					unsigned char GH : 7;
+
+					// Byte 6
+					unsigned char RVa : 3;
+					unsigned char BHb : 5;
+
+					// Byte 7
+					unsigned char GVa : 5;
+					unsigned char RVb : 3;
+
+					// Byte 8
+					unsigned char BV : 6;
+					unsigned char GVb : 2;
+				};
+
+				// Single channel block
+				struct
+				{
+					union
+					{
+						unsigned char base_codeword;
+						signed char signed_base_codeword;
+					};
+
+					unsigned char table_index : 4;
+					unsigned char multiplier : 4;
+
+					unsigned char mc1 : 2;
+					unsigned char mb : 3;
+					unsigned char ma : 3;
+
+					unsigned char mf1 : 1;
+					unsigned char me : 3;
+					unsigned char md : 3;
+					unsigned char mc2 : 1;
+
+					unsigned char mh : 3;
+					unsigned char mg : 3;
+					unsigned char mf2 : 2;
+
+					unsigned char mk1 : 2;
+					unsigned char mj : 3;
+					unsigned char mi : 3;
+
+					unsigned char mn1 : 1;
+					unsigned char mm : 3;
+					unsigned char ml : 3;
+					unsigned char mk2 : 1;
+
+					unsigned char mp : 3;
+					unsigned char mo : 3;
+					unsigned char mn2 : 2;
+				};
+			};
+		};
+
+		void decodeIndividualBlock(unsigned char *dest, int x, int y, int w, int h, int pitch, unsigned char alphaValues[4][4], bool nonOpaquePunchThroughAlpha) const
+		{
+			int r1 = extend_4to8bits(R1);
+			int g1 = extend_4to8bits(G1);
+			int b1 = extend_4to8bits(B1);
+
+			int r2 = extend_4to8bits(R2);
+			int g2 = extend_4to8bits(G2);
+			int b2 = extend_4to8bits(B2);
+
+			decodeIndividualOrDifferentialBlock(dest, x, y, w, h, pitch, r1, g1, b1, r2, g2, b2, alphaValues, nonOpaquePunchThroughAlpha);
+		}
+
+		void decodeDifferentialBlock(unsigned char *dest, int x, int y, int w, int h, int pitch, unsigned char alphaValues[4][4], bool nonOpaquePunchThroughAlpha) const
+		{
+			int b1 = extend_5to8bits(B);
+			int g1 = extend_5to8bits(G);
+			int r1 = extend_5to8bits(R);
+
+			int r2 = extend_5to8bits(R + dR);
+			int g2 = extend_5to8bits(G + dG);
+			int b2 = extend_5to8bits(B + dB);
+
+			decodeIndividualOrDifferentialBlock(dest, x, y, w, h, pitch, r1, g1, b1, r2, g2, b2, alphaValues, nonOpaquePunchThroughAlpha);
+		}
+
+		void decodeIndividualOrDifferentialBlock(unsigned char *dest, int x, int y, int w, int h, int pitch, int r1, int g1, int b1, int r2, int g2, int b2, unsigned char alphaValues[4][4], bool nonOpaquePunchThroughAlpha) const
+		{
+			// Table 3.17.2 sorted according to table 3.17.3
+			static const int intensityModifierDefault[8][4] =
+			{
+				{ 2, 8, -2, -8 },
+				{ 5, 17, -5, -17 },
+				{ 9, 29, -9, -29 },
+				{ 13, 42, -13, -42 },
+				{ 18, 60, -18, -60 },
+				{ 24, 80, -24, -80 },
+				{ 33, 106, -33, -106 },
+				{ 47, 183, -47, -183 }
+			};
+
+			// Table C.12, intensity modifier for non opaque punchthrough alpha
+			static const int intensityModifierNonOpaque[8][4] =
+			{
+				{ 0, 8, 0, -8 },
+				{ 0, 17, 0, -17 },
+				{ 0, 29, 0, -29 },
+				{ 0, 42, 0, -42 },
+				{ 0, 60, 0, -60 },
+				{ 0, 80, 0, -80 },
+				{ 0, 106, 0, -106 },
+				{ 0, 183, 0, -183 }
+			};
+
+			const int(&intensityModifier)[8][4] = nonOpaquePunchThroughAlpha ? intensityModifierNonOpaque : intensityModifierDefault;
+
+			bgra8 subblockColors0[4];
+			bgra8 subblockColors1[4];
+
+			const int i10 = intensityModifier[cw1][0];
+			const int i11 = intensityModifier[cw1][1];
+			const int i12 = intensityModifier[cw1][2];
+			const int i13 = intensityModifier[cw1][3];
+
+			subblockColors0[0].set(r1 + i10, g1 + i10, b1 + i10);
+			subblockColors0[1].set(r1 + i11, g1 + i11, b1 + i11);
+			subblockColors0[2].set(r1 + i12, g1 + i12, b1 + i12);
+			subblockColors0[3].set(r1 + i13, g1 + i13, b1 + i13);
+
+			const int i20 = intensityModifier[cw2][0];
+			const int i21 = intensityModifier[cw2][1];
+			const int i22 = intensityModifier[cw2][2];
+			const int i23 = intensityModifier[cw2][3];
+
+			subblockColors1[0].set(r2 + i20, g2 + i20, b2 + i20);
+			subblockColors1[1].set(r2 + i21, g2 + i21, b2 + i21);
+			subblockColors1[2].set(r2 + i22, g2 + i22, b2 + i22);
+			subblockColors1[3].set(r2 + i23, g2 + i23, b2 + i23);
+
+			unsigned char* destStart = dest;
+
+			if(flipbit)
+			{
+				for(int j = 0; j < 2 && (y + j) < h; j++)
+				{
+					bgra8* color = (bgra8*)dest;
+					if((x + 0) < w) color[0] = subblockColors0[getIndex(0, j)].addA(alphaValues[j][0]);
+					if((x + 1) < w) color[1] = subblockColors0[getIndex(1, j)].addA(alphaValues[j][1]);
+					if((x + 2) < w) color[2] = subblockColors0[getIndex(2, j)].addA(alphaValues[j][2]);
+					if((x + 3) < w) color[3] = subblockColors0[getIndex(3, j)].addA(alphaValues[j][3]);
+					dest += pitch;
+				}
+
+				for(int j = 2; j < 4 && (y + j) < h; j++)
+				{
+					bgra8* color = (bgra8*)dest;
+					if((x + 0) < w) color[0] = subblockColors1[getIndex(0, j)].addA(alphaValues[j][0]);
+					if((x + 1) < w) color[1] = subblockColors1[getIndex(1, j)].addA(alphaValues[j][1]);
+					if((x + 2) < w) color[2] = subblockColors1[getIndex(2, j)].addA(alphaValues[j][2]);
+					if((x + 3) < w) color[3] = subblockColors1[getIndex(3, j)].addA(alphaValues[j][3]);
+					dest += pitch;
+				}
+			}
+			else
+			{
+				for(int j = 0; j < 4 && (y + j) < h; j++)
+				{
+					bgra8* color = (bgra8*)dest;
+					if((x + 0) < w) color[0] = subblockColors0[getIndex(0, j)].addA(alphaValues[j][0]);
+					if((x + 1) < w) color[1] = subblockColors0[getIndex(1, j)].addA(alphaValues[j][1]);
+					if((x + 2) < w) color[2] = subblockColors1[getIndex(2, j)].addA(alphaValues[j][2]);
+					if((x + 3) < w) color[3] = subblockColors1[getIndex(3, j)].addA(alphaValues[j][3]);
+					dest += pitch;
+				}
+			}
+
+			if(nonOpaquePunchThroughAlpha)
+			{
+				decodePunchThroughAlphaBlock(destStart, x, y, w, h, pitch);
+			}
+		}
+
+		void decodeTBlock(unsigned char *dest, int x, int y, int w, int h, int pitch, unsigned char alphaValues[4][4], bool nonOpaquePunchThroughAlpha) const
+		{
+			// Table C.8, distance index fot T and H modes
+			static const int distance[8] = { 3, 6, 11, 16, 23, 32, 41, 64 };
+
+			bgra8 paintColors[4];
+
+			int r1 = extend_4to8bits(TR1a << 2 | TR1b);
+			int g1 = extend_4to8bits(TG1);
+			int b1 = extend_4to8bits(TB1);
+
+			int r2 = extend_4to8bits(TR2);
+			int g2 = extend_4to8bits(TG2);
+			int b2 = extend_4to8bits(TB2);
+
+			const int d = distance[Tda << 1 | Tdb];
+
+			paintColors[0].set(r1, g1, b1);
+			paintColors[1].set(r2 + d, g2 + d, b2 + d);
+			paintColors[2].set(r2, g2, b2);
+			paintColors[3].set(r2 - d, g2 - d, b2 - d);
+
+			unsigned char* destStart = dest;
+
+			for(int j = 0; j < 4 && (y + j) < h; j++)
+			{
+				bgra8* color = (bgra8*)dest;
+				if((x + 0) < w) color[0] = paintColors[getIndex(0, j)].addA(alphaValues[j][0]);
+				if((x + 1) < w) color[1] = paintColors[getIndex(1, j)].addA(alphaValues[j][1]);
+				if((x + 2) < w) color[2] = paintColors[getIndex(2, j)].addA(alphaValues[j][2]);
+				if((x + 3) < w) color[3] = paintColors[getIndex(3, j)].addA(alphaValues[j][3]);
+				dest += pitch;
+			}
+
+			if(nonOpaquePunchThroughAlpha)
+			{
+				decodePunchThroughAlphaBlock(destStart, x, y, w, h, pitch);
+			}
+		}
+
+		void decodeHBlock(unsigned char *dest, int x, int y, int w, int h, int pitch, unsigned char alphaValues[4][4], bool nonOpaquePunchThroughAlpha) const
+		{
+			// Table C.8, distance index fot T and H modes
+			static const int distance[8] = { 3, 6, 11, 16, 23, 32, 41, 64 };
+
+			bgra8 paintColors[4];
+
+			int r1 = extend_4to8bits(HR1);
+			int g1 = extend_4to8bits(HG1a << 1 | HG1b);
+			int b1 = extend_4to8bits(HB1a << 3 | HB1b << 1 | HB1c);
+
+			int r2 = extend_4to8bits(HR2);
+			int g2 = extend_4to8bits(HG2a << 1 | HG2b);
+			int b2 = extend_4to8bits(HB2);
+
+			const int d = distance[(Hda << 2) | (Hdb << 1) | ((r1 << 16 | g1 << 8 | b1) >= (r2 << 16 | g2 << 8 | b2) ? 1 : 0)];
+
+			paintColors[0].set(r1 + d, g1 + d, b1 + d);
+			paintColors[1].set(r1 - d, g1 - d, b1 - d);
+			paintColors[2].set(r2 + d, g2 + d, b2 + d);
+			paintColors[3].set(r2 - d, g2 - d, b2 - d);
+
+			unsigned char* destStart = dest;
+
+			for(int j = 0; j < 4 && (y + j) < h; j++)
+			{
+				bgra8* color = (bgra8*)dest;
+				if((x + 0) < w) color[0] = paintColors[getIndex(0, j)].addA(alphaValues[j][0]);
+				if((x + 1) < w) color[1] = paintColors[getIndex(1, j)].addA(alphaValues[j][1]);
+				if((x + 2) < w) color[2] = paintColors[getIndex(2, j)].addA(alphaValues[j][2]);
+				if((x + 3) < w) color[3] = paintColors[getIndex(3, j)].addA(alphaValues[j][3]);
+				dest += pitch;
+			}
+
+			if(nonOpaquePunchThroughAlpha)
+			{
+				decodePunchThroughAlphaBlock(destStart, x, y, w, h, pitch);
+			}
+		}
+
+		void decodePlanarBlock(unsigned char *dest, int x, int y, int w, int h, int pitch, unsigned char alphaValues[4][4]) const
+		{
+			int ro = extend_6to8bits(RO);
+			int go = extend_7to8bits(GO1 << 6 | GO2);
+			int bo = extend_6to8bits(BO1 << 5 | BO2 << 3 | BO3a << 1 | BO3b);
+
+			int rh = extend_6to8bits(RH1 << 1 | RH2);
+			int gh = extend_7to8bits(GH);
+			int bh = extend_6to8bits(BHa << 5 | BHb);
+
+			int rv = extend_6to8bits(RVa << 3 | RVb);
+			int gv = extend_7to8bits(GVa << 2 | GVb);
+			int bv = extend_6to8bits(BV);
+
+			for(int j = 0; j < 4 && (y + j) < h; j++)
+			{
+				int ry = j * (rv - ro) + 2;
+				int gy = j * (gv - go) + 2;
+				int by = j * (bv - bo) + 2;
+				for(int i = 0; i < 4 && (x + i) < w; i++)
+				{
+					((bgra8*)(dest))[i].set(((i * (rh - ro) + ry) >> 2) + ro,
+						((i * (gh - go) + gy) >> 2) + go,
+						((i * (bh - bo) + by) >> 2) + bo,
+						alphaValues[j][i]);
+				}
+				dest += pitch;
+			}
+		}
+
+		// Index for individual, differential, H and T modes
+		inline int getIndex(int x, int y) const
+		{
+			int bitIndex = x * 4 + y;
+			int bitOffset = bitIndex & 7;
+			int lsb = (pixelIndexLSB[1 - (bitIndex >> 3)] >> bitOffset) & 1;
+			int msb = (pixelIndexMSB[1 - (bitIndex >> 3)] >> bitOffset) & 1;
+
+			return (msb << 1) | lsb;
+		}
+
+		void decodePunchThroughAlphaBlock(unsigned char *dest, int x, int y, int w, int h, int pitch) const
+		{
+			for(int j = 0; j < 4 && (y + j) < h; j++)
+			{
+				for(int i = 0; i < 4 && (x + i) < w; i++)
+				{
+					if(getIndex(i, j) == 2) //  msb == 1 && lsb == 0
+					{
+						((bgra8*)dest)[i].set(0, 0, 0, 0);
+					}
+				}
+				dest += pitch;
+			}
+		}
+
+		// Single channel utility functions
+		inline int getSingleChannel(int x, int y, bool isSigned) const
+		{
+			int codeword = isSigned ? signed_base_codeword : base_codeword;
+			return codeword + getSingleChannelModifier(x, y) * multiplier;
+		}
+
+		inline int getSingleChannelIndex(int x, int y) const
+		{
+			switch(x * 4 + y)
+			{
+			case 0: return ma;
+			case 1: return mb;
+			case 2: return mc1 << 1 | mc2;
+			case 3: return md;
+			case 4: return me;
+			case 5: return mf1 << 2 | mf2;
+			case 6: return mg;
+			case 7: return mh;
+			case 8: return mi;
+			case 9: return mj;
+			case 10: return mk1 << 1 | mk2;
+			case 11: return ml;
+			case 12: return mm;
+			case 13: return mn1 << 2 | mn2;
+			case 14: return mo;
+			default: return mp; // 15
+			}
+		}
+
+		inline int getSingleChannelModifier(int x, int y) const
+		{
+			static const int modifierTable[16][8] = { { -3, -6, -9, -15, 2, 5, 8, 14 },
+			{ -3, -7, -10, -13, 2, 6, 9, 12 },
+			{ -2, -5, -8, -13, 1, 4, 7, 12 },
+			{ -2, -4, -6, -13, 1, 3, 5, 12 },
+			{ -3, -6, -8, -12, 2, 5, 7, 11 },
+			{ -3, -7, -9, -11, 2, 6, 8, 10 },
+			{ -4, -7, -8, -11, 3, 6, 7, 10 },
+			{ -3, -5, -8, -11, 2, 4, 7, 10 },
+			{ -2, -6, -8, -10, 1, 5, 7, 9 },
+			{ -2, -5, -8, -10, 1, 4, 7, 9 },
+			{ -2, -4, -8, -10, 1, 3, 7, 9 },
+			{ -2, -5, -7, -10, 1, 4, 6, 9 },
+			{ -3, -4, -7, -10, 2, 3, 6, 9 },
+			{ -1, -2, -3, -10, 0, 1, 2, 9 },
+			{ -4, -6, -8, -9, 3, 5, 7, 8 },
+			{ -3, -5, -7, -9, 2, 4, 6, 8 } };
+
+			return modifierTable[table_index][getSingleChannelIndex(x, y)];
+		}
+	};
+}
+
+// Decodes 1 to 4 channel images to 8 bit output
+bool ETC_Decoder::Decode(const unsigned char* src, unsigned char *dst, int w, int h, int dstW, int dstH, int dstPitch, int dstBpp, InputType inputType)
+{
+	const ETC2* sources[2];
+	sources[0] = (const ETC2*)src;
+
+	unsigned char alphaValues[4][4] = { { 255, 255, 255, 255 }, { 255, 255, 255, 255 }, { 255, 255, 255, 255 }, { 255, 255, 255, 255 } };
+
+	switch(inputType)
+	{
+	case ETC_R_SIGNED:
+	case ETC_R_UNSIGNED:
+		for(int y = 0; y < h; y += 4)
+		{
+			unsigned char *dstRow = dst + (y * dstPitch);
+			for(int x = 0; x < w; x += 4, sources[0]++)
+			{
+				ETC2::DecodeBlock(sources, dstRow + (x * dstBpp), 1, x, y, dstW, dstH, dstPitch, inputType == ETC_R_SIGNED);
+			}
+		}
+		break;
+	case ETC_RG_SIGNED:
+	case ETC_RG_UNSIGNED:
+		sources[1] = sources[0] + 1;
+		for(int y = 0; y < h; y += 4)
+		{
+			unsigned char *dstRow = dst + (y * dstPitch);
+			for(int x = 0; x < w; x += 4, sources[0] += 2, sources[1] += 2)
+			{
+				ETC2::DecodeBlock(sources, dstRow + (x * dstBpp), 2, x, y, dstW, dstH, dstPitch, inputType == ETC_RG_SIGNED);
+			}
+		}
+		break;
+	case ETC_RGB:
+	case ETC_RGB_PUNCHTHROUGH_ALPHA:
+		for(int y = 0; y < h; y += 4)
+		{
+			unsigned char *dstRow = dst + (y * dstPitch);
+			for(int x = 0; x < w; x += 4, sources[0]++)
+			{
+				sources[0]->decodeBlock(dstRow + (x * dstBpp), x, y, dstW, dstH, dstPitch, alphaValues, inputType == ETC_RGB_PUNCHTHROUGH_ALPHA);
+			}
+		}
+		break;
+	case ETC_RGBA:
+		for(int y = 0; y < h; y += 4)
+		{
+			unsigned char *dstRow = dst + (y * dstPitch);
+			for(int x = 0; x < w; x += 4)
+			{
+				// Decode Alpha
+				ETC2::DecodeBlock(&sources[0], &(alphaValues[0][0]), 1, x, y, dstW, dstH, 4, false);
+				sources[0]++; // RGBA packets are 128 bits, so move on to the next 64 bit packet to decode the RGB color
+
+				// Decode RGB
+				sources[0]->decodeBlock(dstRow + (x * dstBpp), x, y, dstW, dstH, dstPitch, alphaValues, false);
+				sources[0]++;
+			}
+		}
+		break;
+	default:
+		return false;
+	}
+
+	return true;
+}
diff --git a/src/Renderer/ETC_Decoder.hpp b/src/Renderer/ETC_Decoder.hpp
index 11b1395..1039b37 100644
--- a/src/Renderer/ETC_Decoder.hpp
+++ b/src/Renderer/ETC_Decoder.hpp
@@ -1,38 +1,41 @@
-// SwiftShader Software Renderer

-//

-// Copyright(c) 2015 Google 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 Google Inc. Without such an agreement, no rights or licenses, express

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

-//

-

-class ETC_Decoder

-{

-public:

-	enum InputType

-	{

-		ETC_R_SIGNED,

-		ETC_R_UNSIGNED,

-		ETC_RG_SIGNED,

-		ETC_RG_UNSIGNED,

-		ETC_RGB,

-		ETC_RGB_PUNCHTHROUGH_ALPHA,

-		ETC_RGBA

-	};

-

-	/// ETC_Decoder::Decode - Decodes 1 to 4 channel images to 8 bit output

-	/// @param src            Pointer to ETC2 encoded image

-	/// @param dst            Pointer to BGRA, 8 bit output

-	/// @param w              src image width

-	/// @param h              src image height

-	/// @param dstW           dst image width

-	/// @param dstH           dst image height

-	/// @param dstPitch       dst image pitch (bytes per row)

-	/// @param dstBpp         dst image bytes per pixel

-	/// @param inputType      src's format

-	/// @return               true if the decoding was performed

-	static bool Decode(const unsigned char* src, unsigned char *dst, int w, int h, int dstW, int dstH, int dstPitch, int dstBpp, InputType inputType);

-};

+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+class ETC_Decoder
+{
+public:
+	enum InputType
+	{
+		ETC_R_SIGNED,
+		ETC_R_UNSIGNED,
+		ETC_RG_SIGNED,
+		ETC_RG_UNSIGNED,
+		ETC_RGB,
+		ETC_RGB_PUNCHTHROUGH_ALPHA,
+		ETC_RGBA
+	};
+
+	/// ETC_Decoder::Decode - Decodes 1 to 4 channel images to 8 bit output
+	/// @param src            Pointer to ETC2 encoded image
+	/// @param dst            Pointer to BGRA, 8 bit output
+	/// @param w              src image width
+	/// @param h              src image height
+	/// @param dstW           dst image width
+	/// @param dstH           dst image height
+	/// @param dstPitch       dst image pitch (bytes per row)
+	/// @param dstBpp         dst image bytes per pixel
+	/// @param inputType      src's format
+	/// @return               true if the decoding was performed
+	static bool Decode(const unsigned char* src, unsigned char *dst, int w, int h, int dstW, int dstH, int dstPitch, int dstBpp, InputType inputType);
+};
diff --git a/src/Renderer/LRUCache.hpp b/src/Renderer/LRUCache.hpp
index 3ce15fb..77f9ab7 100644
--- a/src/Renderer/LRUCache.hpp
+++ b/src/Renderer/LRUCache.hpp
@@ -1,142 +1,145 @@
-// SwiftShader Software Renderer

-//

-// Copyright(c) 2005-2011 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_LRUCache_hpp

-#define sw_LRUCache_hpp

-

-#include "Common/Math.hpp"

-

-namespace sw

-{

-	template<class Key, class Data>

-	class LRUCache

-	{

-	public:

-		LRUCache(int n);

-

-		~LRUCache();

-

-		Data *query(const Key &key) const;

-		Data *add(const Key &key, Data *data);

-		

-		int getSize() {return size;}

-		Key &getKey(int i) {return key[i];}

-

-	private:

-		int size;

-		int mask;

-		int top;

-		int fill;

-

-		Key *key;

-		Key **ref;

-		Data **data;

-	};

-}

-

-namespace sw

-{

-	template<class Key, class Data>

-	LRUCache<Key, Data>::LRUCache(int n)

-	{

-		size = ceilPow2(n);

-		mask = size - 1;

-		top = 0;

-		fill = 0;

-

-		key = new Key[size];

-		ref = new Key*[size];

-		data = new Data*[size];

-		

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

-		{

-			data[i] = 0;

-

-			ref[i] = &key[i];

-		}

-	}

-

-	template<class Key, class Data>

-	LRUCache<Key, Data>::~LRUCache()

-	{

-		delete[] key;

-		key = 0;

-

-		delete[] ref;

-		ref = 0;

-

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

-		{

-			if(data[i])

-			{

-				data[i]->unbind();

-				data[i] = 0;

-			}

-		}

-

-		delete[] data;

-		data = 0;

-	}

-

-	template<class Key, class Data>

-	Data *LRUCache<Key, Data>::query(const Key &key) const

-	{

-		for(int i = top; i > top - fill; i--)

-		{

-			int j = i & mask;

-

-			if(key == *ref[j])

-			{

-				Data *hit = data[j];

-

-				if(i != top)

-				{

-					// Move one up

-					int k = (j + 1) & mask;

-

-					Data *swapD = data[k];

-					data[k] = data[j];

-					data[j] = swapD;

-

-					Key *swapK = ref[k];

-					ref[k] = ref[j];

-					ref[j] = swapK;

-				}

-

-				return hit;

-			}

-		}

-

-		return 0;   // Not found

-	}

-	

-	template<class Key, class Data>

-	Data *LRUCache<Key, Data>::add(const Key &key, Data *data)

-	{

-		top = (top + 1) & mask;

-		fill = fill + 1 < size ? fill + 1 : size;

-

-		*ref[top] = key;

-	

-		data->bind();

-		

-		if(this->data[top])

-		{

-			this->data[top]->unbind();

-		}

-

-		this->data[top] = data;

-

-		return data;

-	}

-}

-

-#endif   // sw_LRUCache_hpp

+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef sw_LRUCache_hpp
+#define sw_LRUCache_hpp
+
+#include "Common/Math.hpp"
+
+namespace sw
+{
+	template<class Key, class Data>
+	class LRUCache
+	{
+	public:
+		LRUCache(int n);
+
+		~LRUCache();
+
+		Data *query(const Key &key) const;
+		Data *add(const Key &key, Data *data);
+	
+		int getSize() {return size;}
+		Key &getKey(int i) {return key[i];}
+
+	private:
+		int size;
+		int mask;
+		int top;
+		int fill;
+
+		Key *key;
+		Key **ref;
+		Data **data;
+	};
+}
+
+namespace sw
+{
+	template<class Key, class Data>
+	LRUCache<Key, Data>::LRUCache(int n)
+	{
+		size = ceilPow2(n);
+		mask = size - 1;
+		top = 0;
+		fill = 0;
+
+		key = new Key[size];
+		ref = new Key*[size];
+		data = new Data*[size];
+		
+		for(int i = 0; i < size; i++)
+		{
+			data[i] = 0;
+
+			ref[i] = &key[i];
+		}
+	}
+
+	template<class Key, class Data>
+	LRUCache<Key, Data>::~LRUCache()
+	{
+		delete[] key;
+		key = 0;
+
+		delete[] ref;
+		ref = 0;
+
+		for(int i = 0; i < size; i++)
+		{
+			if(data[i])
+			{
+				data[i]->unbind();
+				data[i] = 0;
+			}
+		}
+
+		delete[] data;
+		data = 0;
+	}
+
+	template<class Key, class Data>
+	Data *LRUCache<Key, Data>::query(const Key &key) const
+	{
+		for(int i = top; i > top - fill; i--)
+		{
+			int j = i & mask;
+
+			if(key == *ref[j])
+			{
+				Data *hit = data[j];
+
+				if(i != top)
+				{
+					// Move one up
+					int k = (j + 1) & mask;
+
+					Data *swapD = data[k];
+					data[k] = data[j];
+					data[j] = swapD;
+
+					Key *swapK = ref[k];
+					ref[k] = ref[j];
+					ref[j] = swapK;
+				}
+
+				return hit;
+			}
+		}
+
+		return 0;   // Not found
+	}
+	
+	template<class Key, class Data>
+	Data *LRUCache<Key, Data>::add(const Key &key, Data *data)
+	{
+		top = (top + 1) & mask;
+		fill = fill + 1 < size ? fill + 1 : size;
+
+		*ref[top] = key;
+	
+		data->bind();
+		
+		if(this->data[top])
+		{
+			this->data[top]->unbind();
+		}
+
+		this->data[top] = data;
+
+		return data;
+	}
+}
+
+#endif   // sw_LRUCache_hpp
diff --git a/src/Renderer/Matrix.cpp b/src/Renderer/Matrix.cpp
index 3c3567a..1b27bf8 100644
--- a/src/Renderer/Matrix.cpp
+++ b/src/Renderer/Matrix.cpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2011 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #include "Matrix.hpp"
 
diff --git a/src/Renderer/Matrix.hpp b/src/Renderer/Matrix.hpp
index fe93ae2..41281a6 100644
--- a/src/Renderer/Matrix.hpp
+++ b/src/Renderer/Matrix.hpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2011 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #ifndef Matrix_hpp
 #define Matrix_hpp
@@ -91,7 +94,7 @@
 
 		static Matrix eulerRotate(const Vector &v);
 		static Matrix eulerRotate(float x, float y, float z);
-		
+	
 		static Matrix translate(const Vector &v);
 		static Matrix translate(float x, float y, float z);
 		
diff --git a/src/Renderer/PixelProcessor.cpp b/src/Renderer/PixelProcessor.cpp
index 636351a..0c90cbc 100644
--- a/src/Renderer/PixelProcessor.cpp
+++ b/src/Renderer/PixelProcessor.cpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2013 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #include "PixelProcessor.hpp"
 
diff --git a/src/Renderer/PixelProcessor.hpp b/src/Renderer/PixelProcessor.hpp
index 104f377..88fd654 100644
--- a/src/Renderer/PixelProcessor.hpp
+++ b/src/Renderer/PixelProcessor.hpp
@@ -1,331 +1,334 @@
-// 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_PixelProcessor_hpp

-#define sw_PixelProcessor_hpp

-

-#include "Context.hpp"

-#include "RoutineCache.hpp"

-

-namespace sw

-{

-	class PixelShader;

-	class Rasterizer;

-	struct Texture;

-	struct DrawData;

-

-	class PixelProcessor

-	{

-	public:

-		struct States

-		{

-			unsigned int computeHash();

-

-			int shaderID;

-

-			bool depthOverride                        : 1;

-			bool shaderContainsKill                   : 1;

-

-			DepthCompareMode depthCompareMode         : BITS(DEPTH_LAST);

-			AlphaCompareMode alphaCompareMode         : BITS(ALPHA_LAST);

-			bool depthWriteEnable                     : 1;

-			bool quadLayoutDepthBuffer                : 1;

-

-			bool stencilActive                        : 1;

-			StencilCompareMode stencilCompareMode     : BITS(STENCIL_LAST);

-			StencilOperation stencilFailOperation     : BITS(OPERATION_LAST);

-			StencilOperation stencilPassOperation     : BITS(OPERATION_LAST);

-			StencilOperation stencilZFailOperation    : BITS(OPERATION_LAST);

-			bool noStencilMask                        : 1;

-			bool noStencilWriteMask                   : 1;

-			bool stencilWriteMasked                   : 1;

-			bool twoSidedStencil                      : 1;

-			StencilCompareMode stencilCompareModeCCW  : BITS(STENCIL_LAST);

-			StencilOperation stencilFailOperationCCW  : BITS(OPERATION_LAST);

-			StencilOperation stencilPassOperationCCW  : BITS(OPERATION_LAST);

-			StencilOperation stencilZFailOperationCCW : BITS(OPERATION_LAST);

-			bool noStencilMaskCCW                     : 1;

-			bool noStencilWriteMaskCCW                : 1;

-			bool stencilWriteMaskedCCW                : 1;

-

-			bool depthTestActive                      : 1;

-			bool fogActive                            : 1;

-			FogMode pixelFogMode                      : BITS(FOG_LAST);

-			bool specularAdd                          : 1;

-			bool occlusionEnabled                     : 1;

-			bool wBasedFog                            : 1;

-			bool perspective                          : 1;

-

-			bool alphaBlendActive                     : 1;

-			BlendFactor sourceBlendFactor             : BITS(BLEND_LAST);

-			BlendFactor destBlendFactor               : BITS(BLEND_LAST);

-			BlendOperation blendOperation             : BITS(BLENDOP_LAST);

-			BlendFactor sourceBlendFactorAlpha        : BITS(BLEND_LAST);

-			BlendFactor destBlendFactorAlpha          : BITS(BLEND_LAST);

-			BlendOperation blendOperationAlpha        : BITS(BLENDOP_LAST);

-

-			unsigned int colorWriteMask                       : RENDERTARGETS * 4;   // Four component bit masks

-			Format targetFormat[RENDERTARGETS];

-			bool writeSRGB                                    : 1;

-			unsigned int multiSample                          : 3;

-			unsigned int multiSampleMask                      : 4;

-			TransparencyAntialiasing transparencyAntialiasing : BITS(TRANSPARENCY_LAST);

-			bool centroid                                     : 1;

-

-			LogicalOperation logicalOperation : BITS(LOGICALOP_LAST);

-

-			Sampler::State sampler[TEXTURE_IMAGE_UNITS];

-			TextureStage::State textureStage[8];

-

-			struct Interpolant

-			{

-				unsigned char component : 4;

-				unsigned char flat : 4;

-				unsigned char project : 2;

-				bool centroid : 1;

-			};

-

-			union

-			{

-				struct

-				{

-					Interpolant color[2];

-					Interpolant texture[8];

-				};

-

-				Interpolant interpolant[10];

-			};

-

-			Interpolant fog;

-		};

-

-		struct State : States

-		{

-			State();

-

-			bool operator==(const State &state) const;

-

-			int colorWriteActive(int index) const

-			{

-				return (colorWriteMask >> (index * 4)) & 0xF;

-			}

-

-			bool alphaTestActive() const

-			{

-				return alphaCompareMode != ALPHA_ALWAYS;

-			}

-

-			bool pixelFogActive() const

-			{

-				return pixelFogMode != FOG_NONE;

-			}

-

-			unsigned int hash;

-		};

-

-		struct Stencil

-		{

-			int64_t testMaskQ;

-			int64_t referenceMaskedQ;

-			int64_t referenceMaskedSignedQ;

-			int64_t writeMaskQ;

-			int64_t invWriteMaskQ;

-			int64_t referenceQ;

-

-			void set(int reference, int testMask, int writeMask)

-			{

-				referenceQ = replicate(reference);

-				testMaskQ = replicate(testMask);

-				writeMaskQ = replicate(writeMask);

-				invWriteMaskQ = ~writeMaskQ;

-				referenceMaskedQ = referenceQ & testMaskQ;

-				referenceMaskedSignedQ = replicate((reference + 0x80) & 0xFF & testMask);

-			}

-

-			static int64_t replicate(int b)

-			{

-				int64_t w = b & 0xFF;

-

-				return (w << 0) | (w << 8) | (w << 16) | (w << 24) | (w << 32) | (w << 40) | (w << 48) | (w << 56);

-			}

-		};

-

-		struct Fog

-		{

-			float4 scale;

-			float4 offset;

-			word4 color4[3];

-			float4 colorF[3];

-			float4 densityE;

-			float4 density2E;

-		};

-

-		struct Factor

-		{

-			word4 textureFactor4[4];

-

-			word4 alphaReference4;

-

-			word4 blendConstant4W[4];

-			float4 blendConstant4F[4];

-			word4 invBlendConstant4W[4];

-			float4 invBlendConstant4F[4];

-		};

-

-	public:

-		typedef void (*RoutinePointer)(const Primitive *primitive, int count, int thread, DrawData *draw);

-

-		PixelProcessor(Context *context);

-

-		virtual ~PixelProcessor();

-

-		virtual void setFloatConstant(unsigned int index, const float value[4]);

-		virtual void setIntegerConstant(unsigned int index, const int value[4]);

-		virtual void setBooleanConstant(unsigned int index, int boolean);

-

-		virtual void setUniformBuffer(int index, sw::Resource* buffer, int offset);

-		virtual void lockUniformBuffers(byte** u, sw::Resource* uniformBuffers[]);

-

-		virtual void setRenderTarget(int index, Surface *renderTarget);

-		virtual void setDepthBuffer(Surface *depthBuffer);

-		virtual void setStencilBuffer(Surface *stencilBuffer);

-

-		virtual void setTexCoordIndex(unsigned int stage, int texCoordIndex);

-		virtual void setStageOperation(unsigned int stage, TextureStage::StageOperation stageOperation);

-		virtual void setFirstArgument(unsigned int stage, TextureStage::SourceArgument firstArgument);

-		virtual void setSecondArgument(unsigned int stage, TextureStage::SourceArgument secondArgument);

-		virtual void setThirdArgument(unsigned int stage, TextureStage::SourceArgument thirdArgument);

-		virtual void setStageOperationAlpha(unsigned int stage, TextureStage::StageOperation stageOperationAlpha);

-		virtual void setFirstArgumentAlpha(unsigned int stage, TextureStage::SourceArgument firstArgumentAlpha);

-		virtual void setSecondArgumentAlpha(unsigned int stage, TextureStage::SourceArgument secondArgumentAlpha);

-		virtual void setThirdArgumentAlpha(unsigned int stage, TextureStage::SourceArgument thirdArgumentAlpha);

-		virtual void setFirstModifier(unsigned int stage, TextureStage::ArgumentModifier firstModifier);

-		virtual void setSecondModifier(unsigned int stage, TextureStage::ArgumentModifier secondModifier);

-		virtual void setThirdModifier(unsigned int stage, TextureStage::ArgumentModifier thirdModifier);

-		virtual void setFirstModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier firstModifierAlpha);

-		virtual void setSecondModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier secondModifierAlpha);

-		virtual void setThirdModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier thirdModifierAlpha);

-		virtual void setDestinationArgument(unsigned int stage, TextureStage::DestinationArgument destinationArgument);

-		virtual void setConstantColor(unsigned int stage, const Color<float> &constantColor);

-		virtual void setBumpmapMatrix(unsigned int stage, int element, float value);

-		virtual void setLuminanceScale(unsigned int stage, float value);

-		virtual void setLuminanceOffset(unsigned int stage, float value);

-

-		virtual void setTextureFilter(unsigned int sampler, FilterType textureFilter);

-		virtual void setMipmapFilter(unsigned int sampler, MipmapType mipmapFilter);

-		virtual void setGatherEnable(unsigned int sampler, bool enable);

-		virtual void setAddressingModeU(unsigned int sampler, AddressingMode addressingMode);

-		virtual void setAddressingModeV(unsigned int sampler, AddressingMode addressingMode);

-		virtual void setAddressingModeW(unsigned int sampler, AddressingMode addressingMode);

-		virtual void setReadSRGB(unsigned int sampler, bool sRGB);

-		virtual void setMipmapLOD(unsigned int sampler, float bias);

-		virtual void setBorderColor(unsigned int sampler, const Color<float> &borderColor);

-		virtual void setMaxAnisotropy(unsigned int sampler, float maxAnisotropy);

-		virtual void setSwizzleR(unsigned int sampler, SwizzleType swizzleR);

-		virtual void setSwizzleG(unsigned int sampler, SwizzleType swizzleG);

-		virtual void setSwizzleB(unsigned int sampler, SwizzleType swizzleB);

-		virtual void setSwizzleA(unsigned int sampler, SwizzleType swizzleA);

-

-		virtual void setWriteSRGB(bool sRGB);

-		virtual void setDepthBufferEnable(bool depthBufferEnable);

-		virtual void setDepthCompare(DepthCompareMode depthCompareMode);

-		virtual void setAlphaCompare(AlphaCompareMode alphaCompareMode);

-		virtual void setDepthWriteEnable(bool depthWriteEnable);

-		virtual void setAlphaTestEnable(bool alphaTestEnable);

-		virtual void setCullMode(CullMode cullMode);

-		virtual void setColorWriteMask(int index, int rgbaMask);

-

-		virtual void setColorLogicOpEnabled(bool colorLogicOpEnabled);

-		virtual void setLogicalOperation(LogicalOperation logicalOperation);

-

-		virtual void setStencilEnable(bool stencilEnable);

-		virtual void setStencilCompare(StencilCompareMode stencilCompareMode);

-		virtual void setStencilReference(int stencilReference);

-		virtual void setStencilMask(int stencilMask);

-		virtual void setStencilFailOperation(StencilOperation stencilFailOperation);

-		virtual void setStencilPassOperation(StencilOperation stencilPassOperation);

-		virtual void setStencilZFailOperation(StencilOperation stencilZFailOperation);

-		virtual void setStencilWriteMask(int stencilWriteMask);

-		virtual void setTwoSidedStencil(bool enable);

-		virtual void setStencilCompareCCW(StencilCompareMode stencilCompareMode);

-		virtual void setStencilReferenceCCW(int stencilReference);

-		virtual void setStencilMaskCCW(int stencilMask);

-		virtual void setStencilFailOperationCCW(StencilOperation stencilFailOperation);

-		virtual void setStencilPassOperationCCW(StencilOperation stencilPassOperation);

-		virtual void setStencilZFailOperationCCW(StencilOperation stencilZFailOperation);

-		virtual void setStencilWriteMaskCCW(int stencilWriteMask);

-

-		virtual void setTextureFactor(const Color<float> &textureFactor);

-		virtual void setBlendConstant(const Color<float> &blendConstant);

-

-		virtual void setFillMode(FillMode fillMode);

-		virtual void setShadingMode(ShadingMode shadingMode);

-		

-		virtual void setAlphaBlendEnable(bool alphaBlendEnable);

-		virtual void setSourceBlendFactor(BlendFactor sourceBlendFactor);

-		virtual void setDestBlendFactor(BlendFactor destBlendFactor);

-		virtual void setBlendOperation(BlendOperation blendOperation);

-

-		virtual void setSeparateAlphaBlendEnable(bool separateAlphaBlendEnable);

-		virtual void setSourceBlendFactorAlpha(BlendFactor sourceBlendFactorAlpha);

-		virtual void setDestBlendFactorAlpha(BlendFactor destBlendFactorAlpha);

-		virtual void setBlendOperationAlpha(BlendOperation blendOperationAlpha);

-

-		virtual void setAlphaReference(float alphaReference);

-

-		virtual void setGlobalMipmapBias(float bias);

-

-		virtual void setFogStart(float start);

-		virtual void setFogEnd(float end);

-		virtual void setFogColor(Color<float> fogColor);

-		virtual void setFogDensity(float fogDensity);

-		virtual void setPixelFogMode(FogMode fogMode);

-

-		virtual void setPerspectiveCorrection(bool perspectiveCorrection);

-

-		virtual void setOcclusionEnabled(bool enable);

-

-	protected:

-		const State update() const;

-		Routine *routine(const State &state);

-		void setRoutineCacheSize(int routineCacheSize);

-

-		// Shader constants

-		word4 cW[8][4];

-		float4 c[FRAGMENT_UNIFORM_VECTORS];

-		int4 i[16];

-		bool b[16];

-

-		// Other semi-constants

-		Stencil stencil;

-		Stencil stencilCCW;

-		Fog fog;

-		Factor factor;

-

-	private:

-		struct UniformBufferInfo

-		{

-			UniformBufferInfo();

-

-			Resource* buffer;

-			int offset;

-		};

-		UniformBufferInfo uniformBufferInfo[MAX_UNIFORM_BUFFER_BINDINGS];

-

-		void setFogRanges(float start, float end);

-

-		Context *const context;

-

-		RoutineCache<State> *routineCache;

-	};

-}

-

-#endif   // sw_PixelProcessor_hpp

+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef sw_PixelProcessor_hpp
+#define sw_PixelProcessor_hpp
+
+#include "Context.hpp"
+#include "RoutineCache.hpp"
+
+namespace sw
+{
+	class PixelShader;
+	class Rasterizer;
+	struct Texture;
+	struct DrawData;
+
+	class PixelProcessor
+	{
+	public:
+		struct States
+		{
+			unsigned int computeHash();
+
+			int shaderID;
+
+			bool depthOverride                        : 1;
+			bool shaderContainsKill                   : 1;
+
+			DepthCompareMode depthCompareMode         : BITS(DEPTH_LAST);
+			AlphaCompareMode alphaCompareMode         : BITS(ALPHA_LAST);
+			bool depthWriteEnable                     : 1;
+			bool quadLayoutDepthBuffer                : 1;
+
+			bool stencilActive                        : 1;
+			StencilCompareMode stencilCompareMode     : BITS(STENCIL_LAST);
+			StencilOperation stencilFailOperation     : BITS(OPERATION_LAST);
+			StencilOperation stencilPassOperation     : BITS(OPERATION_LAST);
+			StencilOperation stencilZFailOperation    : BITS(OPERATION_LAST);
+			bool noStencilMask                        : 1;
+			bool noStencilWriteMask                   : 1;
+			bool stencilWriteMasked                   : 1;
+			bool twoSidedStencil                      : 1;
+			StencilCompareMode stencilCompareModeCCW  : BITS(STENCIL_LAST);
+			StencilOperation stencilFailOperationCCW  : BITS(OPERATION_LAST);
+			StencilOperation stencilPassOperationCCW  : BITS(OPERATION_LAST);
+			StencilOperation stencilZFailOperationCCW : BITS(OPERATION_LAST);
+			bool noStencilMaskCCW                     : 1;
+			bool noStencilWriteMaskCCW                : 1;
+			bool stencilWriteMaskedCCW                : 1;
+
+			bool depthTestActive                      : 1;
+			bool fogActive                            : 1;
+			FogMode pixelFogMode                      : BITS(FOG_LAST);
+			bool specularAdd                          : 1;
+			bool occlusionEnabled                     : 1;
+			bool wBasedFog                            : 1;
+			bool perspective                          : 1;
+
+			bool alphaBlendActive                     : 1;
+			BlendFactor sourceBlendFactor             : BITS(BLEND_LAST);
+			BlendFactor destBlendFactor               : BITS(BLEND_LAST);
+			BlendOperation blendOperation             : BITS(BLENDOP_LAST);
+			BlendFactor sourceBlendFactorAlpha        : BITS(BLEND_LAST);
+			BlendFactor destBlendFactorAlpha          : BITS(BLEND_LAST);
+			BlendOperation blendOperationAlpha        : BITS(BLENDOP_LAST);
+
+			unsigned int colorWriteMask                       : RENDERTARGETS * 4;   // Four component bit masks
+			Format targetFormat[RENDERTARGETS];
+			bool writeSRGB                                    : 1;
+			unsigned int multiSample                          : 3;
+			unsigned int multiSampleMask                      : 4;
+			TransparencyAntialiasing transparencyAntialiasing : BITS(TRANSPARENCY_LAST);
+			bool centroid                                     : 1;
+
+			LogicalOperation logicalOperation : BITS(LOGICALOP_LAST);
+
+			Sampler::State sampler[TEXTURE_IMAGE_UNITS];
+			TextureStage::State textureStage[8];
+
+			struct Interpolant
+			{
+				unsigned char component : 4;
+				unsigned char flat : 4;
+				unsigned char project : 2;
+				bool centroid : 1;
+			};
+
+			union
+			{
+				struct
+				{
+					Interpolant color[2];
+					Interpolant texture[8];
+				};
+
+				Interpolant interpolant[10];
+			};
+
+			Interpolant fog;
+		};
+
+		struct State : States
+		{
+			State();
+
+			bool operator==(const State &state) const;
+
+			int colorWriteActive(int index) const
+			{
+				return (colorWriteMask >> (index * 4)) & 0xF;
+			}
+
+			bool alphaTestActive() const
+			{
+				return alphaCompareMode != ALPHA_ALWAYS;
+			}
+
+			bool pixelFogActive() const
+			{
+				return pixelFogMode != FOG_NONE;
+			}
+
+			unsigned int hash;
+		};
+
+		struct Stencil
+		{
+			int64_t testMaskQ;
+			int64_t referenceMaskedQ;
+			int64_t referenceMaskedSignedQ;
+			int64_t writeMaskQ;
+			int64_t invWriteMaskQ;
+			int64_t referenceQ;
+
+			void set(int reference, int testMask, int writeMask)
+			{
+				referenceQ = replicate(reference);
+				testMaskQ = replicate(testMask);
+				writeMaskQ = replicate(writeMask);
+				invWriteMaskQ = ~writeMaskQ;
+				referenceMaskedQ = referenceQ & testMaskQ;
+				referenceMaskedSignedQ = replicate((reference + 0x80) & 0xFF & testMask);
+			}
+
+			static int64_t replicate(int b)
+			{
+				int64_t w = b & 0xFF;
+
+				return (w << 0) | (w << 8) | (w << 16) | (w << 24) | (w << 32) | (w << 40) | (w << 48) | (w << 56);
+			}
+		};
+
+		struct Fog
+		{
+			float4 scale;
+			float4 offset;
+			word4 color4[3];
+			float4 colorF[3];
+			float4 densityE;
+			float4 density2E;
+		};
+
+		struct Factor
+		{
+			word4 textureFactor4[4];
+
+			word4 alphaReference4;
+
+			word4 blendConstant4W[4];
+			float4 blendConstant4F[4];
+			word4 invBlendConstant4W[4];
+			float4 invBlendConstant4F[4];
+		};
+
+	public:
+		typedef void (*RoutinePointer)(const Primitive *primitive, int count, int thread, DrawData *draw);
+
+		PixelProcessor(Context *context);
+
+		virtual ~PixelProcessor();
+
+		virtual void setFloatConstant(unsigned int index, const float value[4]);
+		virtual void setIntegerConstant(unsigned int index, const int value[4]);
+		virtual void setBooleanConstant(unsigned int index, int boolean);
+
+		virtual void setUniformBuffer(int index, sw::Resource* buffer, int offset);
+		virtual void lockUniformBuffers(byte** u, sw::Resource* uniformBuffers[]);
+
+		virtual void setRenderTarget(int index, Surface *renderTarget);
+		virtual void setDepthBuffer(Surface *depthBuffer);
+		virtual void setStencilBuffer(Surface *stencilBuffer);
+
+		virtual void setTexCoordIndex(unsigned int stage, int texCoordIndex);
+		virtual void setStageOperation(unsigned int stage, TextureStage::StageOperation stageOperation);
+		virtual void setFirstArgument(unsigned int stage, TextureStage::SourceArgument firstArgument);
+		virtual void setSecondArgument(unsigned int stage, TextureStage::SourceArgument secondArgument);
+		virtual void setThirdArgument(unsigned int stage, TextureStage::SourceArgument thirdArgument);
+		virtual void setStageOperationAlpha(unsigned int stage, TextureStage::StageOperation stageOperationAlpha);
+		virtual void setFirstArgumentAlpha(unsigned int stage, TextureStage::SourceArgument firstArgumentAlpha);
+		virtual void setSecondArgumentAlpha(unsigned int stage, TextureStage::SourceArgument secondArgumentAlpha);
+		virtual void setThirdArgumentAlpha(unsigned int stage, TextureStage::SourceArgument thirdArgumentAlpha);
+		virtual void setFirstModifier(unsigned int stage, TextureStage::ArgumentModifier firstModifier);
+		virtual void setSecondModifier(unsigned int stage, TextureStage::ArgumentModifier secondModifier);
+		virtual void setThirdModifier(unsigned int stage, TextureStage::ArgumentModifier thirdModifier);
+		virtual void setFirstModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier firstModifierAlpha);
+		virtual void setSecondModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier secondModifierAlpha);
+		virtual void setThirdModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier thirdModifierAlpha);
+		virtual void setDestinationArgument(unsigned int stage, TextureStage::DestinationArgument destinationArgument);
+		virtual void setConstantColor(unsigned int stage, const Color<float> &constantColor);
+		virtual void setBumpmapMatrix(unsigned int stage, int element, float value);
+		virtual void setLuminanceScale(unsigned int stage, float value);
+		virtual void setLuminanceOffset(unsigned int stage, float value);
+
+		virtual void setTextureFilter(unsigned int sampler, FilterType textureFilter);
+		virtual void setMipmapFilter(unsigned int sampler, MipmapType mipmapFilter);
+		virtual void setGatherEnable(unsigned int sampler, bool enable);
+		virtual void setAddressingModeU(unsigned int sampler, AddressingMode addressingMode);
+		virtual void setAddressingModeV(unsigned int sampler, AddressingMode addressingMode);
+		virtual void setAddressingModeW(unsigned int sampler, AddressingMode addressingMode);
+		virtual void setReadSRGB(unsigned int sampler, bool sRGB);
+		virtual void setMipmapLOD(unsigned int sampler, float bias);
+		virtual void setBorderColor(unsigned int sampler, const Color<float> &borderColor);
+		virtual void setMaxAnisotropy(unsigned int sampler, float maxAnisotropy);
+		virtual void setSwizzleR(unsigned int sampler, SwizzleType swizzleR);
+		virtual void setSwizzleG(unsigned int sampler, SwizzleType swizzleG);
+		virtual void setSwizzleB(unsigned int sampler, SwizzleType swizzleB);
+		virtual void setSwizzleA(unsigned int sampler, SwizzleType swizzleA);
+
+		virtual void setWriteSRGB(bool sRGB);
+		virtual void setDepthBufferEnable(bool depthBufferEnable);
+		virtual void setDepthCompare(DepthCompareMode depthCompareMode);
+		virtual void setAlphaCompare(AlphaCompareMode alphaCompareMode);
+		virtual void setDepthWriteEnable(bool depthWriteEnable);
+		virtual void setAlphaTestEnable(bool alphaTestEnable);
+		virtual void setCullMode(CullMode cullMode);
+		virtual void setColorWriteMask(int index, int rgbaMask);
+
+		virtual void setColorLogicOpEnabled(bool colorLogicOpEnabled);
+		virtual void setLogicalOperation(LogicalOperation logicalOperation);
+
+		virtual void setStencilEnable(bool stencilEnable);
+		virtual void setStencilCompare(StencilCompareMode stencilCompareMode);
+		virtual void setStencilReference(int stencilReference);
+		virtual void setStencilMask(int stencilMask);
+		virtual void setStencilFailOperation(StencilOperation stencilFailOperation);
+		virtual void setStencilPassOperation(StencilOperation stencilPassOperation);
+		virtual void setStencilZFailOperation(StencilOperation stencilZFailOperation);
+		virtual void setStencilWriteMask(int stencilWriteMask);
+		virtual void setTwoSidedStencil(bool enable);
+		virtual void setStencilCompareCCW(StencilCompareMode stencilCompareMode);
+		virtual void setStencilReferenceCCW(int stencilReference);
+		virtual void setStencilMaskCCW(int stencilMask);
+		virtual void setStencilFailOperationCCW(StencilOperation stencilFailOperation);
+		virtual void setStencilPassOperationCCW(StencilOperation stencilPassOperation);
+		virtual void setStencilZFailOperationCCW(StencilOperation stencilZFailOperation);
+		virtual void setStencilWriteMaskCCW(int stencilWriteMask);
+
+		virtual void setTextureFactor(const Color<float> &textureFactor);
+		virtual void setBlendConstant(const Color<float> &blendConstant);
+
+		virtual void setFillMode(FillMode fillMode);
+		virtual void setShadingMode(ShadingMode shadingMode);
+	
+		virtual void setAlphaBlendEnable(bool alphaBlendEnable);
+		virtual void setSourceBlendFactor(BlendFactor sourceBlendFactor);
+		virtual void setDestBlendFactor(BlendFactor destBlendFactor);
+		virtual void setBlendOperation(BlendOperation blendOperation);
+
+		virtual void setSeparateAlphaBlendEnable(bool separateAlphaBlendEnable);
+		virtual void setSourceBlendFactorAlpha(BlendFactor sourceBlendFactorAlpha);
+		virtual void setDestBlendFactorAlpha(BlendFactor destBlendFactorAlpha);
+		virtual void setBlendOperationAlpha(BlendOperation blendOperationAlpha);
+
+		virtual void setAlphaReference(float alphaReference);
+
+		virtual void setGlobalMipmapBias(float bias);
+
+		virtual void setFogStart(float start);
+		virtual void setFogEnd(float end);
+		virtual void setFogColor(Color<float> fogColor);
+		virtual void setFogDensity(float fogDensity);
+		virtual void setPixelFogMode(FogMode fogMode);
+
+		virtual void setPerspectiveCorrection(bool perspectiveCorrection);
+
+		virtual void setOcclusionEnabled(bool enable);
+
+	protected:
+		const State update() const;
+		Routine *routine(const State &state);
+		void setRoutineCacheSize(int routineCacheSize);
+
+		// Shader constants
+		word4 cW[8][4];
+		float4 c[FRAGMENT_UNIFORM_VECTORS];
+		int4 i[16];
+		bool b[16];
+
+		// Other semi-constants
+		Stencil stencil;
+		Stencil stencilCCW;
+		Fog fog;
+		Factor factor;
+
+	private:
+		struct UniformBufferInfo
+		{
+			UniformBufferInfo();
+
+			Resource* buffer;
+			int offset;
+		};
+		UniformBufferInfo uniformBufferInfo[MAX_UNIFORM_BUFFER_BINDINGS];
+
+		void setFogRanges(float start, float end);
+
+		Context *const context;
+
+		RoutineCache<State> *routineCache;
+	};
+}
+
+#endif   // sw_PixelProcessor_hpp
diff --git a/src/Renderer/Plane.cpp b/src/Renderer/Plane.cpp
index ab9ea77..095b7f2 100644
--- a/src/Renderer/Plane.cpp
+++ b/src/Renderer/Plane.cpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2011 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #include "Plane.hpp"
 
diff --git a/src/Renderer/Plane.hpp b/src/Renderer/Plane.hpp
index f831339..962b9ae 100644
--- a/src/Renderer/Plane.hpp
+++ b/src/Renderer/Plane.hpp
@@ -1,37 +1,40 @@
-// SwiftShader Software Renderer

-//

-// Copyright(c) 2005-2011 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 Plane_hpp

-#define Plane_hpp

-

-#include "Vector.hpp"

-

-namespace sw

-{

-	struct Matrix;

-

-	struct Plane

-	{

-		float A;

-		float B;

-		float C;

-		float D;

-

-		Plane();

-		Plane(float A, float B, float C, float D);   // Plane equation 

-		Plane(const float ABCD[4]);

-

-		friend Plane operator*(const Plane &p, const Matrix &A);   // Transform plane by matrix (post-multiply)

-		friend Plane operator*(const Matrix &A, const Plane &p);   // Transform plane by matrix (pre-multiply)

-	};

-}

-

-#endif   // Plane_hpp

+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef Plane_hpp
+#define Plane_hpp
+
+#include "Vector.hpp"
+
+namespace sw
+{
+	struct Matrix;
+
+	struct Plane
+	{
+		float A;
+		float B;
+		float C;
+		float D;
+
+		Plane();
+		Plane(float A, float B, float C, float D);   // Plane equation 
+		Plane(const float ABCD[4]);
+
+		friend Plane operator*(const Plane &p, const Matrix &A);   // Transform plane by matrix (post-multiply)
+		friend Plane operator*(const Matrix &A, const Plane &p);   // Transform plane by matrix (pre-multiply)
+	};
+}
+
+#endif   // Plane_hpp
diff --git a/src/Renderer/Point.cpp b/src/Renderer/Point.cpp
index 93355f1..e7e33dd 100644
--- a/src/Renderer/Point.cpp
+++ b/src/Renderer/Point.cpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2011 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #include "Point.hpp"
 
diff --git a/src/Renderer/Point.hpp b/src/Renderer/Point.hpp
index 7b50f58..85198c5 100644
--- a/src/Renderer/Point.hpp
+++ b/src/Renderer/Point.hpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2011 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #ifndef Point_hpp
 #define Point_hpp
@@ -32,7 +35,7 @@
 			float p[3];
 
 			struct
-			{		
+			{	
 				float x;
 				float y;
 				float z;
diff --git a/src/Renderer/Polygon.hpp b/src/Renderer/Polygon.hpp
index db41627..0b94132 100644
--- a/src/Renderer/Polygon.hpp
+++ b/src/Renderer/Polygon.hpp
@@ -1,51 +1,54 @@
-// SwiftShader Software Renderer

-//

-// Copyright(c) 2005-2011 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_Polygon_hpp

-#define sw_Polygon_hpp

-

-#include "Vertex.hpp"

-

-namespace sw

-{

-	struct Polygon

-	{

-		Polygon(const float4 *P0, const float4 *P1, const float4 *P2)

-		{

-			P[0][0] = P0;

-			P[0][1] = P1;

-			P[0][2] = P2;

-

-			n = 3;

-			i = 0;

-		}

-

-		Polygon(const float4 *P, int n)

-		{

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

-			{

-				this->P[0][i] = &P[i];

-			}

-

-			this->n = n;

-			this->i = 0;

-		}

-

-		float4 B[16];              // Buffer for clipped vertices

-		const float4 *P[16][16];   // Pointers to clipped polygon's vertices

-

-		int i;

-		int b;

-		int n;

-	};

-}

-

-#endif   // sw_Polygon_hpp

+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef sw_Polygon_hpp
+#define sw_Polygon_hpp
+
+#include "Vertex.hpp"
+
+namespace sw
+{
+	struct Polygon
+	{
+		Polygon(const float4 *P0, const float4 *P1, const float4 *P2)
+		{
+			P[0][0] = P0;
+			P[0][1] = P1;
+			P[0][2] = P2;
+
+			n = 3;
+			i = 0;
+		}
+
+		Polygon(const float4 *P, int n)
+		{
+			for(int i = 0; i < n; i++)
+			{
+				this->P[0][i] = &P[i];
+			}
+
+			this->n = n;
+			this->i = 0;
+		}
+
+		float4 B[16];              // Buffer for clipped vertices
+		const float4 *P[16][16];   // Pointers to clipped polygon's vertices
+
+		int i;
+		int b;
+		int n;
+	};
+}
+
+#endif   // sw_Polygon_hpp
diff --git a/src/Renderer/Primitive.hpp b/src/Renderer/Primitive.hpp
index ac37814..83e3a77 100644
--- a/src/Renderer/Primitive.hpp
+++ b/src/Renderer/Primitive.hpp
@@ -1,76 +1,79 @@
-// SwiftShader Software Renderer

-//

-// Copyright(c) 2005-2011 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_Primitive_hpp

-#define sw_Primitive_hpp

-

-#include "Vertex.hpp"

-#include "Config.hpp"

-

-namespace sw

-{

-	struct Triangle

-	{

-		Vertex v0;

-		Vertex v1;

-		Vertex v2;

-	};

-

-	struct PlaneEquation   // z = A * x + B * y + C

-	{

-		float4 A;

-		float4 B;

-		float4 C;

-	};

-

-	struct Primitive

-	{

-		int yMin;

-		int yMax;

-

-		float4 xQuad;

-		float4 yQuad;

-

-		PlaneEquation z;

-		PlaneEquation w;

-

-		union

-		{

-			struct

-			{

-				PlaneEquation C[2][4];

-				PlaneEquation T[8][4];

-			};

-

-			PlaneEquation V[10][4];

-		};

-

-		PlaneEquation f;

-

-		float area;

-

-		// Masks for two-sided stencil

-		int64_t clockwiseMask;

-		int64_t invClockwiseMask;

-

-		struct Span

-		{

-			unsigned short left;

-			unsigned short right;

-		};

-

-		Span outlineUnderflow;

-		Span outline[OUTLINE_RESOLUTION];

-		Span outlineOverflow;

-	};

-}

-

-#endif   // sw_Primitive_hpp

+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef sw_Primitive_hpp
+#define sw_Primitive_hpp
+
+#include "Vertex.hpp"
+#include "Config.hpp"
+
+namespace sw
+{
+	struct Triangle
+	{
+		Vertex v0;
+		Vertex v1;
+		Vertex v2;
+	};
+
+	struct PlaneEquation   // z = A * x + B * y + C
+	{
+		float4 A;
+		float4 B;
+		float4 C;
+	};
+
+	struct Primitive
+	{
+		int yMin;
+		int yMax;
+
+		float4 xQuad;
+		float4 yQuad;
+
+		PlaneEquation z;
+		PlaneEquation w;
+
+		union
+		{
+			struct
+			{
+				PlaneEquation C[2][4];
+				PlaneEquation T[8][4];
+			};
+
+			PlaneEquation V[10][4];
+		};
+
+		PlaneEquation f;
+
+		float area;
+
+		// Masks for two-sided stencil
+		int64_t clockwiseMask;
+		int64_t invClockwiseMask;
+
+		struct Span
+		{
+			unsigned short left;
+			unsigned short right;
+		};
+
+		Span outlineUnderflow;
+		Span outline[OUTLINE_RESOLUTION];
+		Span outlineOverflow;
+	};
+}
+
+#endif   // sw_Primitive_hpp
diff --git a/src/Renderer/QuadRasterizer.cpp b/src/Renderer/QuadRasterizer.cpp
index 6e390b0..5bc500f 100644
--- a/src/Renderer/QuadRasterizer.cpp
+++ b/src/Renderer/QuadRasterizer.cpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2012 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #include "QuadRasterizer.hpp"
 
diff --git a/src/Renderer/QuadRasterizer.hpp b/src/Renderer/QuadRasterizer.hpp
index 7d307a4..51d578d 100644
--- a/src/Renderer/QuadRasterizer.hpp
+++ b/src/Renderer/QuadRasterizer.hpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2011 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #ifndef sw_QuadRasterizer_hpp
 #define sw_QuadRasterizer_hpp
diff --git a/src/Renderer/Rasterizer.hpp b/src/Renderer/Rasterizer.hpp
index 7e3a857..3c746c8 100644
--- a/src/Renderer/Rasterizer.hpp
+++ b/src/Renderer/Rasterizer.hpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2011 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #ifndef sw_Rasterizer_hpp
 #define sw_Rasterizer_hpp
diff --git a/src/Renderer/Renderer.cpp b/src/Renderer/Renderer.cpp
index a77ec3b..40d9de6 100644
--- a/src/Renderer/Renderer.cpp
+++ b/src/Renderer/Renderer.cpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2012 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #include "Renderer.hpp"
 
@@ -834,7 +837,7 @@
 		case Task::PRIMITIVES:
 			{
 				int unit = task[threadIndex].primitiveUnit;
-				
+			
 				int input = primitiveProgress[unit].firstPrimitive;
 				int count = primitiveProgress[unit].primitiveCount;
 				DrawCall *draw = drawList[primitiveProgress[unit].drawCall % DRAW_COUNT];
@@ -1434,7 +1437,7 @@
 				}
 			}
 			break;
-        case DRAW_QUADLIST:
+		case DRAW_QUADLIST:
 			{
 				unsigned int index = 4 * start / 2;
 
@@ -1444,7 +1447,7 @@
 					batch[i+0][1] = index + 1;
 					batch[i+0][2] = index + 2;
 
-                    batch[i+1][0] = index + 0;
+					batch[i+1][0] = index + 0;
 					batch[i+1][1] = index + 2;
 					batch[i+1][2] = index + 3;
 
@@ -1457,11 +1460,11 @@
 			return;
 		}
 
-		task->vertexStart = start * 3;

-		task->vertexCount = triangleCount * 3;

-		// Note: Quads aren't handled for verticesPerPrimitive, but verticesPerPrimitive is used for transform feedback,

-		//       which is an OpenGL ES 3.0 feature, and OpenGL ES 3.0 doesn't support quads as a primitive type.

-		DrawType type = static_cast<DrawType>(static_cast<unsigned int>(draw->drawType) & 0xF);

+		task->vertexStart = start * 3;
+		task->vertexCount = triangleCount * 3;
+		// Note: Quads aren't handled for verticesPerPrimitive, but verticesPerPrimitive is used for transform feedback,
+		//       which is an OpenGL ES 3.0 feature, and OpenGL ES 3.0 doesn't support quads as a primitive type.
+		DrawType type = static_cast<DrawType>(static_cast<unsigned int>(draw->drawType) & 0xF);
 		task->verticesPerPrimitive = 1 + (type >= DRAW_LINELIST) + (type >= DRAW_TRIANGLELIST);
 		vertexRoutine(&triangle->v0, (unsigned int*)&batch, task, data);
 	}
@@ -1944,12 +1947,12 @@
 	unsigned int Renderer::computeClipFlags(const float4 &v, const DrawData &data)
 	{
 		return ((v.x > v.w)  << 0) |
-			   ((v.y > v.w)  << 1) |
-			   ((v.z > v.w)  << 2) |
-			   ((v.x < -v.w) << 3) |
+		       ((v.y > v.w)  << 1) |
+		       ((v.z > v.w)  << 2) |
+		       ((v.x < -v.w) << 3) |
 		       ((v.y < -v.w) << 4) |
-			   ((v.z < 0)    << 5) |
-			   Clipper::CLIP_FINITE;   // FIXME: xyz finite
+		       ((v.z < 0)    << 5) |
+		       Clipper::CLIP_FINITE;   // FIXME: xyz finite
 	}
 
 	void Renderer::initializeThreads()
diff --git a/src/Renderer/Renderer.hpp b/src/Renderer/Renderer.hpp
index e0b9141..4b3abbe 100644
--- a/src/Renderer/Renderer.hpp
+++ b/src/Renderer/Renderer.hpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2012 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #ifndef sw_Renderer_hpp
 #define sw_Renderer_hpp
diff --git a/src/Renderer/RoutineCache.hpp b/src/Renderer/RoutineCache.hpp
index 5e421e8..8965a1c 100644
--- a/src/Renderer/RoutineCache.hpp
+++ b/src/Renderer/RoutineCache.hpp
@@ -1,172 +1,175 @@
-// 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_RoutineCache_hpp

-#define sw_RoutineCache_hpp

-

-#include "LRUCache.hpp"

-

-#include "Reactor/Reactor.hpp"

-

-namespace sw

-{

-	template<class State>

-	class RoutineCache : public LRUCache<State, Routine>

-	{

-	public:

-		RoutineCache(int n, const char *precache = 0);

-		~RoutineCache();

-

-	private:

-		const char *precache;

-		#if defined(_WIN32)

-		HMODULE precacheDLL;

-		#endif

-	};

-}

-

-#if defined(_WIN32)

-	#include "Shader/Constants.hpp"

-	#include "Reactor/DLL.hpp"

-#endif

-

-namespace sw

-{

-	template<class State>

-	RoutineCache<State>::RoutineCache(int n, const char *precache) : LRUCache<State, Routine>(n), precache(precache)

-	{

-		#if defined(_WIN32)

-			precacheDLL = 0;

-

-			if(precache)

-			{

-				char dllName[1024]; sprintf(dllName, "%s.dll", precache);

-				char dirName[1024]; sprintf(dirName, "%s.dir", precache);

-

-				precacheDLL = LoadLibrary(dllName);

-				FILE *dir = fopen(dirName, "rb");

-				int ordinal = 1;

-

-				while(precacheDLL && dir)

-				{

-					State state;

-					int offset;

-					int size;

-

-					size_t bytes = fread(&state, 1, sizeof(State), dir);

-					bytes += fread(&offset, 1, sizeof(offset), dir);

-					bytes += fread(&size, 1, sizeof(size), dir);

-

-					if(bytes != sizeof(State) + sizeof(offset) + sizeof(size))

-					{

-						break;

-					}

-

-					void (*routine)(void) = (void(*)(void))GetProcAddress(precacheDLL, (char*)ordinal);

-					ordinal++;

-

-					if(routine)

-					{

-						add(state, new Routine(routine, size, offset));

-					}

-				}

-

-				if(dir)

-				{

-					fclose(dir);

-				}

-			}

-		#endif

-	}

-

-	template<class State>

-	RoutineCache<State>::~RoutineCache()

-	{

-		#if defined(_WIN32)

-			char dllName[1024]; sprintf(dllName, "%s.dll", precache);

-			char dirName[1024]; sprintf(dirName, "%s.dir", precache);

-

-			if(precache)

-			{

-				DLL dll(dllName, &constants, sizeof(Constants));

-				FILE *dir = fopen(dirName, "wb");

-

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

-				{

-					State &state = getKey(i);

-					Routine *routine = query(state);

-

-					if(routine)

-					{

-						unsigned char *buffer = (unsigned char*)routine->getBuffer();

-						unsigned char *entry = (unsigned char*)routine->getEntry();

-						int size = routine->getBufferSize();

-						int codeSize = routine->getCodeSize();

-

-						#ifndef _M_AMD64

-							for(int j = 1; j < codeSize - 4; j++)

-							{

-								unsigned char modRM_SIB = entry[j - 1];

-								unsigned int address = *(unsigned int*)&entry[j];

-

-								if((modRM_SIB & 0x05) == 0x05 && (address % 4) == 0)

-								{

-									if(address >= (unsigned int)buffer && address < (unsigned int)entry)   // Constant stored above the function entry

-									{

-										dll.addRelocation(buffer, &entry[j], true);

-

-										j += 4;

-									}

-								}

-							}

-						#else

-							for(int j = 1; j < codeSize - 4; j++)

-							{

-								unsigned char modRM_SIB = entry[j - 1];

-								uint64_t address = *(uint64_t*)&entry[j];

-

-							//	if((modRM_SIB & 0x05) == 0x05 && (address % 4) == 0)

-								{

-									if(address >= (uint64_t)buffer && address < (uint64_t)entry)   // Constant stored above the function entry

-									{

-										dll.addRelocation(buffer, &entry[j], true);

-

-										j += 4;

-									}

-								}

-							}

-						#endif

-

-						dll.addFunction(buffer, entry, size);

-						fwrite(&state, 1, sizeof(State), dir);

-						int offset = (int)(entry - buffer);

-						fwrite(&offset, 1, sizeof(offset), dir);

-						fwrite(&size, 1, sizeof(size), dir);

-					}

-				}

-

-				FreeLibrary(precacheDLL);

-

-				dll.emit();

-				fclose(dir);

-			}

-			else

-			{

-				FreeLibrary(precacheDLL);

-

-				remove(dllName);

-				remove(dirName);

-			}

-		#endif

-	}

-}

-

-#endif   // sw_RoutineCache_hpp

+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef sw_RoutineCache_hpp
+#define sw_RoutineCache_hpp
+
+#include "LRUCache.hpp"
+
+#include "Reactor/Reactor.hpp"
+
+namespace sw
+{
+	template<class State>
+	class RoutineCache : public LRUCache<State, Routine>
+	{
+	public:
+		RoutineCache(int n, const char *precache = 0);
+		~RoutineCache();
+
+	private:
+		const char *precache;
+		#if defined(_WIN32)
+		HMODULE precacheDLL;
+		#endif
+	};
+}
+
+#if defined(_WIN32)
+	#include "Shader/Constants.hpp"
+	#include "Reactor/DLL.hpp"
+#endif
+
+namespace sw
+{
+	template<class State>
+	RoutineCache<State>::RoutineCache(int n, const char *precache) : LRUCache<State, Routine>(n), precache(precache)
+	{
+		#if defined(_WIN32)
+			precacheDLL = 0;
+
+			if(precache)
+			{
+				char dllName[1024]; sprintf(dllName, "%s.dll", precache);
+				char dirName[1024]; sprintf(dirName, "%s.dir", precache);
+
+				precacheDLL = LoadLibrary(dllName);
+				FILE *dir = fopen(dirName, "rb");
+				int ordinal = 1;
+
+				while(precacheDLL && dir)
+				{
+					State state;
+					int offset;
+					int size;
+
+					size_t bytes = fread(&state, 1, sizeof(State), dir);
+					bytes += fread(&offset, 1, sizeof(offset), dir);
+					bytes += fread(&size, 1, sizeof(size), dir);
+
+					if(bytes != sizeof(State) + sizeof(offset) + sizeof(size))
+					{
+						break;
+					}
+
+					void (*routine)(void) = (void(*)(void))GetProcAddress(precacheDLL, (char*)ordinal);
+					ordinal++;
+
+					if(routine)
+					{
+						add(state, new Routine(routine, size, offset));
+					}
+				}
+
+				if(dir)
+				{
+					fclose(dir);
+				}
+			}
+		#endif
+	}
+
+	template<class State>
+	RoutineCache<State>::~RoutineCache()
+	{
+		#if defined(_WIN32)
+			char dllName[1024]; sprintf(dllName, "%s.dll", precache);
+			char dirName[1024]; sprintf(dirName, "%s.dir", precache);
+
+			if(precache)
+			{
+				DLL dll(dllName, &constants, sizeof(Constants));
+				FILE *dir = fopen(dirName, "wb");
+
+				for(int i = 0; i < getSize(); i++)
+				{
+					State &state = getKey(i);
+					Routine *routine = query(state);
+
+					if(routine)
+					{
+						unsigned char *buffer = (unsigned char*)routine->getBuffer();
+						unsigned char *entry = (unsigned char*)routine->getEntry();
+						int size = routine->getBufferSize();
+						int codeSize = routine->getCodeSize();
+
+						#ifndef _M_AMD64
+							for(int j = 1; j < codeSize - 4; j++)
+							{
+								unsigned char modRM_SIB = entry[j - 1];
+								unsigned int address = *(unsigned int*)&entry[j];
+
+								if((modRM_SIB & 0x05) == 0x05 && (address % 4) == 0)
+								{
+									if(address >= (unsigned int)buffer && address < (unsigned int)entry)   // Constant stored above the function entry
+									{
+										dll.addRelocation(buffer, &entry[j], true);
+
+										j += 4;
+									}
+								}
+							}
+						#else
+							for(int j = 1; j < codeSize - 4; j++)
+							{
+								unsigned char modRM_SIB = entry[j - 1];
+								uint64_t address = *(uint64_t*)&entry[j];
+
+							//	if((modRM_SIB & 0x05) == 0x05 && (address % 4) == 0)
+								{
+									if(address >= (uint64_t)buffer && address < (uint64_t)entry)   // Constant stored above the function entry
+									{
+										dll.addRelocation(buffer, &entry[j], true);
+
+										j += 4;
+									}
+								}
+							}
+						#endif
+
+						dll.addFunction(buffer, entry, size);
+						fwrite(&state, 1, sizeof(State), dir);
+						int offset = (int)(entry - buffer);
+						fwrite(&offset, 1, sizeof(offset), dir);
+						fwrite(&size, 1, sizeof(size), dir);
+					}
+				}
+
+				FreeLibrary(precacheDLL);
+
+				dll.emit();
+				fclose(dir);
+			}
+			else
+			{
+				FreeLibrary(precacheDLL);
+
+				remove(dllName);
+				remove(dirName);
+			}
+		#endif
+	}
+}
+
+#endif   // sw_RoutineCache_hpp
diff --git a/src/Renderer/Sampler.cpp b/src/Renderer/Sampler.cpp
index f446467..7518715 100644
--- a/src/Renderer/Sampler.cpp
+++ b/src/Renderer/Sampler.cpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2012 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #include "Sampler.hpp"
 
@@ -227,7 +230,7 @@
 					unsigned int YStride = pitchP;
 					unsigned int YSize = YStride * height;
 					unsigned int CStride = align(YStride / 2, 16);
- 					unsigned int CSize = CStride * height / 2;
+					unsigned int CSize = CStride * height / 2;
 
 					mipmap.buffer[1] = (byte*)mipmap.buffer[0] + YSize;
 					mipmap.buffer[2] = (byte*)mipmap.buffer[1] + CSize;
@@ -357,8 +360,8 @@
 	{
 		return Surface::isUnsignedComponent(internalTextureFormat, 0) &&
 		       Surface::isUnsignedComponent(internalTextureFormat, 1) &&
-			   Surface::isUnsignedComponent(internalTextureFormat, 2) &&
-			   Surface::isUnsignedComponent(internalTextureFormat, 3);
+		       Surface::isUnsignedComponent(internalTextureFormat, 2) &&
+		       Surface::isUnsignedComponent(internalTextureFormat, 3);
 	}
 
 	bool Sampler::hasCubeTexture() const
diff --git a/src/Renderer/Sampler.hpp b/src/Renderer/Sampler.hpp
index 9580f09..edaa8e1 100644
--- a/src/Renderer/Sampler.hpp
+++ b/src/Renderer/Sampler.hpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2011 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #ifndef sw_Sampler_hpp
 #define sw_Sampler_hpp
diff --git a/src/Renderer/SetupProcessor.cpp b/src/Renderer/SetupProcessor.cpp
index 6d4d7cb..1bedf79 100644
--- a/src/Renderer/SetupProcessor.cpp
+++ b/src/Renderer/SetupProcessor.cpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2012 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #include "SetupProcessor.hpp"
 
diff --git a/src/Renderer/SetupProcessor.hpp b/src/Renderer/SetupProcessor.hpp
index 02133f0..06e4ce9 100644
--- a/src/Renderer/SetupProcessor.hpp
+++ b/src/Renderer/SetupProcessor.hpp
@@ -1,106 +1,109 @@
-// 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_SetupProcessor_hpp

-#define sw_SetupProcessor_hpp

-

-#include "Context.hpp"

-#include "RoutineCache.hpp"

-#include "Shader/VertexShader.hpp"

-#include "Shader/PixelShader.hpp"

-#include "Common/Types.hpp"

-

-namespace sw

-{

-	struct Primitive;

-	struct Triangle;

-	struct Polygon;

-	struct Vertex;

-	struct DrawCall;

-	struct DrawData;

-

-	class SetupProcessor

-	{

-	public:

-		struct States

-		{

-			unsigned int computeHash();

-

-			bool isDrawPoint               : 1;

-			bool isDrawLine                : 1;

-			bool isDrawTriangle            : 1;

-			bool isDrawSolidTriangle       : 1;

-			bool interpolateZ              : 1;

-			bool interpolateW              : 1;

-			bool perspective               : 1;

-			bool pointSprite               : 1;

-			unsigned int positionRegister  : 4;

-			unsigned int pointSizeRegister : 4;

-			CullMode cullMode              : BITS(CULL_LAST);

-			bool twoSidedStencil           : 1;

-			bool slopeDepthBias            : 1;

-			bool vFace                     : 1;

-			unsigned int multiSample       : 3;   // 1, 2 or 4

-			bool rasterizerDiscard         : 1;

-

-			struct Gradient

-			{

-				unsigned char attribute : BITS(Unused);

-				bool flat               : 1;

-				bool wrap               : 1;

-			};

-

-			union

-			{

-				struct

-				{

-					Gradient color[2][4];

-					Gradient texture[8][4];

-				};

-

-				Gradient gradient[10][4];

-			};

-

-			Gradient fog;

-		};

-

-		struct State : States

-		{

-			State(int i = 0);

-

-			bool operator==(const State &states) const;

-

-			unsigned int hash;

-		};

-

-		typedef bool (*RoutinePointer)(Primitive *primitive, const Triangle *triangle, const Polygon *polygon, const DrawData *draw);

-

-		SetupProcessor(Context *context);

-

-		~SetupProcessor();

-

-	protected:

-		State update() const;

-		Routine *routine(const State &state);

-

-		void setRoutineCacheSize(int cacheSize);

-

-		float depthBias;

-		float slopeDepthBias;

-

-	private:

-		Context *const context;

-

-		RoutineCache<State> *routineCache;

-	};

-}

-

-#endif   // sw_SetupProcessor_hpp

+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef sw_SetupProcessor_hpp
+#define sw_SetupProcessor_hpp
+
+#include "Context.hpp"
+#include "RoutineCache.hpp"
+#include "Shader/VertexShader.hpp"
+#include "Shader/PixelShader.hpp"
+#include "Common/Types.hpp"
+
+namespace sw
+{
+	struct Primitive;
+	struct Triangle;
+	struct Polygon;
+	struct Vertex;
+	struct DrawCall;
+	struct DrawData;
+
+	class SetupProcessor
+	{
+	public:
+		struct States
+		{
+			unsigned int computeHash();
+
+			bool isDrawPoint               : 1;
+			bool isDrawLine                : 1;
+			bool isDrawTriangle            : 1;
+			bool isDrawSolidTriangle       : 1;
+			bool interpolateZ              : 1;
+			bool interpolateW              : 1;
+			bool perspective               : 1;
+			bool pointSprite               : 1;
+			unsigned int positionRegister  : 4;
+			unsigned int pointSizeRegister : 4;
+			CullMode cullMode              : BITS(CULL_LAST);
+			bool twoSidedStencil           : 1;
+			bool slopeDepthBias            : 1;
+			bool vFace                     : 1;
+			unsigned int multiSample       : 3;   // 1, 2 or 4
+			bool rasterizerDiscard         : 1;
+
+			struct Gradient
+			{
+				unsigned char attribute : BITS(Unused);
+				bool flat               : 1;
+				bool wrap               : 1;
+			};
+
+			union
+			{
+				struct
+				{
+					Gradient color[2][4];
+					Gradient texture[8][4];
+				};
+
+				Gradient gradient[10][4];
+			};
+
+			Gradient fog;
+		};
+
+		struct State : States
+		{
+			State(int i = 0);
+
+			bool operator==(const State &states) const;
+
+			unsigned int hash;
+		};
+
+		typedef bool (*RoutinePointer)(Primitive *primitive, const Triangle *triangle, const Polygon *polygon, const DrawData *draw);
+
+		SetupProcessor(Context *context);
+
+		~SetupProcessor();
+
+	protected:
+		State update() const;
+		Routine *routine(const State &state);
+
+		void setRoutineCacheSize(int cacheSize);
+
+		float depthBias;
+		float slopeDepthBias;
+
+	private:
+		Context *const context;
+
+		RoutineCache<State> *routineCache;
+	};
+}
+
+#endif   // sw_SetupProcessor_hpp
diff --git a/src/Renderer/Stream.hpp b/src/Renderer/Stream.hpp
index a3555f4..9e06cf3 100644
--- a/src/Renderer/Stream.hpp
+++ b/src/Renderer/Stream.hpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2012 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #ifndef sw_Stream_hpp
 #define sw_Stream_hpp
@@ -75,7 +78,7 @@
 		Stream &defaults()
 		{
 			static const float4 null = {0, 0, 0, 1};
-	
+
 			resource = 0;
 			buffer = &null;
 			stride = 0;
diff --git a/src/Renderer/Surface.cpp b/src/Renderer/Surface.cpp
index d64e041..4d84cf0 100644
--- a/src/Renderer/Surface.cpp
+++ b/src/Renderer/Surface.cpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2013 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #include "Surface.hpp"
 
@@ -2567,7 +2570,7 @@
 				unsigned int YStride = align(width, 16);
 				unsigned int YSize = YStride * height;
 				unsigned int CStride = align(YStride / 2, 16);
- 				unsigned int CSize = CStride * height / 2;
+				unsigned int CSize = CStride * height / 2;
 
 				return YSize + 2 * CSize;
 			}
diff --git a/src/Renderer/Surface.hpp b/src/Renderer/Surface.hpp
index e7764e8..cd89d74 100644
--- a/src/Renderer/Surface.hpp
+++ b/src/Renderer/Surface.hpp
@@ -1,599 +1,602 @@
-// 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_Surface_hpp

-#define sw_Surface_hpp

-

-#include "Color.hpp"

-#include "Main/Config.hpp"

-#include "Common/Resource.hpp"

-

-namespace sw

-{

-	class Resource;

-

-	struct Rect

-	{

-		Rect() {}

-		Rect(int x0i, int y0i, int x1i, int y1i) : x0(x0i), y0(y0i), x1(x1i), y1(y1i) {}

-

-		void clip(int minX, int minY, int maxX, int maxY);

-

-		int width() const  { return x1 - x0; }

-		int height() const { return y1 - y0; }

-

-		int x0;   // Inclusive

-		int y0;   // Inclusive

-		int x1;   // Exclusive

-		int y1;   // Exclusive

-	};

-

-	struct SliceRect : public Rect

-	{

-		SliceRect() : slice(0) {}

-		SliceRect(const Rect& rect) : Rect(rect), slice(0) {}

-		SliceRect(const Rect& rect, int s) : Rect(rect), slice(s) {}

-		SliceRect(int x0, int y0, int x1, int y1, int s) : Rect(x0, y0, x1, y1), slice(s) {}

-		int slice;

-	};

-

-	enum Format : unsigned char

-	{

-		FORMAT_NULL,

-

-		FORMAT_A8,

-		FORMAT_R8I,

-		FORMAT_R8UI,

-		FORMAT_R8I_SNORM,

-		FORMAT_R8, // UI_SNORM

-		FORMAT_R16I,

-		FORMAT_R16UI,

-		FORMAT_R32I,

-		FORMAT_R32UI,

-		FORMAT_R3G3B2,

-		FORMAT_A8R3G3B2,

-		FORMAT_X4R4G4B4,

-		FORMAT_A4R4G4B4,

-		FORMAT_R4G4B4A4,

-		FORMAT_R5G6B5,

-		FORMAT_R8G8B8,

-		FORMAT_B8G8R8,

-		FORMAT_X8R8G8B8,

-		FORMAT_A8R8G8B8,

-		FORMAT_X8B8G8R8I,

-		FORMAT_X8B8G8R8UI,

-		FORMAT_X8B8G8R8I_SNORM,

-		FORMAT_X8B8G8R8, // UI_SNORM

-		FORMAT_A8B8G8R8I,

-		FORMAT_A8B8G8R8UI,

-		FORMAT_A8B8G8R8I_SNORM,

-		FORMAT_A8B8G8R8, // UI_SNORM

-		FORMAT_SRGB8_X8,

-		FORMAT_SRGB8_A8,

-		FORMAT_X1R5G5B5,

-		FORMAT_A1R5G5B5,

-		FORMAT_R5G5B5A1,

-		FORMAT_G8R8I,

-		FORMAT_G8R8UI,

-		FORMAT_G8R8I_SNORM,

-		FORMAT_G8R8, // UI_SNORM

-		FORMAT_G16R16, // D3D format

-		FORMAT_G16R16I,

-		FORMAT_G16R16UI,

-		FORMAT_G32R32I,

-		FORMAT_G32R32UI,

-		FORMAT_A2R10G10B10,

-		FORMAT_A2B10G10R10,

-		FORMAT_A16B16G16R16, // D3D format

-		FORMAT_X16B16G16R16I,

-		FORMAT_X16B16G16R16UI,

-		FORMAT_A16B16G16R16I,

-		FORMAT_A16B16G16R16UI,

-		FORMAT_X32B32G32R32I,

-		FORMAT_X32B32G32R32UI,

-		FORMAT_A32B32G32R32I,

-		FORMAT_A32B32G32R32UI,

-		// Paletted formats

-		FORMAT_P8,

-		FORMAT_A8P8,

-		// Compressed formats

-		FORMAT_DXT1,

-		FORMAT_DXT3,

-		FORMAT_DXT5,

-		FORMAT_ATI1,

-		FORMAT_ATI2,

-		FORMAT_ETC1,

-		FORMAT_R11_EAC,

-		FORMAT_SIGNED_R11_EAC,

-		FORMAT_RG11_EAC,

-		FORMAT_SIGNED_RG11_EAC,

-		FORMAT_RGB8_ETC2,

-		FORMAT_SRGB8_ETC2,

-		FORMAT_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,

-		FORMAT_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,

-		FORMAT_RGBA8_ETC2_EAC,

-		FORMAT_SRGB8_ALPHA8_ETC2_EAC,

-		FORMAT_RGBA_ASTC_4x4_KHR,

-		FORMAT_RGBA_ASTC_5x4_KHR,

-		FORMAT_RGBA_ASTC_5x5_KHR,

-		FORMAT_RGBA_ASTC_6x5_KHR,

-		FORMAT_RGBA_ASTC_6x6_KHR,

-		FORMAT_RGBA_ASTC_8x5_KHR,

-		FORMAT_RGBA_ASTC_8x6_KHR,

-		FORMAT_RGBA_ASTC_8x8_KHR,

-		FORMAT_RGBA_ASTC_10x5_KHR,

-		FORMAT_RGBA_ASTC_10x6_KHR,

-		FORMAT_RGBA_ASTC_10x8_KHR,

-		FORMAT_RGBA_ASTC_10x10_KHR,

-		FORMAT_RGBA_ASTC_12x10_KHR,

-		FORMAT_RGBA_ASTC_12x12_KHR,

-		FORMAT_SRGB8_ALPHA8_ASTC_4x4_KHR,

-		FORMAT_SRGB8_ALPHA8_ASTC_5x4_KHR,

-		FORMAT_SRGB8_ALPHA8_ASTC_5x5_KHR,

-		FORMAT_SRGB8_ALPHA8_ASTC_6x5_KHR,

-		FORMAT_SRGB8_ALPHA8_ASTC_6x6_KHR,

-		FORMAT_SRGB8_ALPHA8_ASTC_8x5_KHR,

-		FORMAT_SRGB8_ALPHA8_ASTC_8x6_KHR,

-		FORMAT_SRGB8_ALPHA8_ASTC_8x8_KHR,

-		FORMAT_SRGB8_ALPHA8_ASTC_10x5_KHR,

-		FORMAT_SRGB8_ALPHA8_ASTC_10x6_KHR,

-		FORMAT_SRGB8_ALPHA8_ASTC_10x8_KHR,

-		FORMAT_SRGB8_ALPHA8_ASTC_10x10_KHR,

-		FORMAT_SRGB8_ALPHA8_ASTC_12x10_KHR,

-		FORMAT_SRGB8_ALPHA8_ASTC_12x12_KHR,

-		// Floating-point formats

-		FORMAT_A16F,

-		FORMAT_R16F,

-		FORMAT_G16R16F,

-		FORMAT_B16G16R16F,

-		FORMAT_A16B16G16R16F,

-		FORMAT_A32F,

-		FORMAT_R32F,

-		FORMAT_G32R32F,

-		FORMAT_B32G32R32F,

-		FORMAT_X32B32G32R32F,

-		FORMAT_A32B32G32R32F,

-		// Bump map formats

-		FORMAT_V8U8,

-		FORMAT_L6V5U5,

-		FORMAT_Q8W8V8U8,

-		FORMAT_X8L8V8U8,

-		FORMAT_A2W10V10U10,

-		FORMAT_V16U16,

-		FORMAT_A16W16V16U16,

-		FORMAT_Q16W16V16U16,

-		// Luminance formats

-		FORMAT_L8,

-		FORMAT_A4L4,

-		FORMAT_L16,

-		FORMAT_A8L8,

-		FORMAT_L16F,

-		FORMAT_A16L16F,

-		FORMAT_L32F,

-		FORMAT_A32L32F,

-		// Depth/stencil formats

-		FORMAT_D16,

-		FORMAT_D32,

-		FORMAT_D24X8,

-		FORMAT_D24S8,

-		FORMAT_D24FS8,

-		FORMAT_D32F,                 // Quad layout

-		FORMAT_D32F_COMPLEMENTARY,   // Quad layout, 1 - z

-		FORMAT_D32F_LOCKABLE,        // Linear layout

-		FORMAT_D32FS8_TEXTURE,       // Linear layout, no PCF

-		FORMAT_D32FS8_SHADOW,        // Linear layout, PCF

-		FORMAT_DF24S8,

-		FORMAT_DF16S8,

-		FORMAT_INTZ,

-		FORMAT_S8,

-		// Quad layout framebuffer

-		FORMAT_X8G8R8B8Q,

-		FORMAT_A8G8R8B8Q,

-		// YUV formats

-		FORMAT_YV12_BT601,

-		FORMAT_YV12_BT709,

-		FORMAT_YV12_JFIF,    // Full-swing BT.601

-

-		FORMAT_LAST = FORMAT_YV12_JFIF

-	};

-

-	enum Lock

-	{

-		LOCK_UNLOCKED,

-		LOCK_READONLY,

-		LOCK_WRITEONLY,

-		LOCK_READWRITE,

-		LOCK_DISCARD

-	};

-

-	class Surface

-	{

-	private:

-		struct Buffer

-		{

-		public:

-			void write(int x, int y, int z, const Color<float> &color);

-			void write(int x, int y, const Color<float> &color);

-			void write(void *element, const Color<float> &color);

-			Color<float> read(int x, int y, int z) const;

-			Color<float> read(int x, int y) const;

-			Color<float> read(void *element) const;

-			Color<float> sample(float x, float y, float z) const;

-			Color<float> sample(float x, float y) const;

-

-			void *lockRect(int x, int y, int z, Lock lock);

-			void unlockRect();

-

-			void *buffer;

-			int width;

-			int height;

-			int depth;

-			int bytes;

-			int pitchB;

-			int pitchP;

-			int sliceB;

-			int sliceP;

-			Format format;

-			Lock lock;

-

-			bool dirty;

-		};

-

-	public:

-		Surface(int width, int height, int depth, Format format, void *pixels, int pitch, int slice);

-		Surface(Resource *texture, int width, int height, int depth, Format format, bool lockable, bool renderTarget, int pitchP = 0);

-

-		virtual ~Surface();

-

-		inline void *lock(int x, int y, int z, Lock lock, Accessor client, bool internal = false);

-		inline void unlock(bool internal = false);

-		inline int getWidth() const;

-		inline int getHeight() const;

-		inline int getDepth() const;

-		inline Format getFormat(bool internal = false) const;

-		inline int getPitchB(bool internal = false) const;

-		inline int getPitchP(bool internal = false) const;

-		inline int getSliceB(bool internal = false) const;

-		inline int getSliceP(bool internal = false) const;

-

-		void *lockExternal(int x, int y, int z, Lock lock, Accessor client);

-		void unlockExternal();

-		inline Format getExternalFormat() const;

-		inline int getExternalPitchB() const;

-		inline int getExternalPitchP() const;

-		inline int getExternalSliceB() const;

-		inline int getExternalSliceP() const;

-

-		virtual void *lockInternal(int x, int y, int z, Lock lock, Accessor client);

-		virtual void unlockInternal();

-		inline Format getInternalFormat() const;

-		inline int getInternalPitchB() const;

-		inline int getInternalPitchP() const;

-		inline int getInternalSliceB() const;

-		inline int getInternalSliceP() const;

-

-		void *lockStencil(int front, Accessor client);

-		void unlockStencil();

-		inline int getStencilPitchB() const;

-		inline int getStencilSliceB() const;

-

-		inline int getMultiSampleCount() const;

-		inline int getSuperSampleCount() const;

-

-		bool isEntire(const SliceRect& rect) const;

-		SliceRect getRect() const;

-		void clearDepth(float depth, int x0, int y0, int width, int height);

-		void clearStencil(unsigned char stencil, unsigned char mask, int x0, int y0, int width, int height);

-		void fill(const Color<float> &color, int x0, int y0, int width, int height);

-

-		Color<float> readExternal(int x, int y, int z) const;

-		Color<float> readExternal(int x, int y) const;

-		Color<float> sampleExternal(float x, float y, float z) const;

-		Color<float> sampleExternal(float x, float y) const;

-		void writeExternal(int x, int y, int z, const Color<float> &color);

-		void writeExternal(int x, int y, const Color<float> &color);

-

-		void copyInternal(const Surface* src, int x, int y, float srcX, float srcY, bool filter);

-		void copyInternal(const Surface* src, int x, int y, int z, float srcX, float srcY, float srcZ, bool filter);

-

-		bool hasStencil() const;

-		bool hasDepth() const;

-		bool hasPalette() const;

-		bool isRenderTarget() const;

-

-		bool hasDirtyMipmaps() const;

-		void cleanMipmaps();

-		inline bool isExternalDirty() const;

-		Resource *getResource();

-

-		static int bytes(Format format);

-		static int pitchB(int width, Format format, bool target);

-		static int pitchP(int width, Format format, bool target);

-		static int sliceB(int width, int height, Format format, bool target);

-		static int sliceP(int width, int height, Format format, bool target);

-		static unsigned int size(int width, int height, int depth, Format format);   // FIXME: slice * depth

-

-		static bool isStencil(Format format);

-		static bool isDepth(Format format);

-		static bool isPalette(Format format);

-

-		static bool isFloatFormat(Format format);

-		static bool isUnsignedComponent(Format format, int component);

-		static bool isSRGBreadable(Format format);

-		static bool isSRGBwritable(Format format);

-		static bool isCompressed(Format format);

-		static bool isNonNormalizedInteger(Format format);

-		static int componentCount(Format format);

-

-		static void setTexturePalette(unsigned int *palette);

-

-	protected:

-		sw::Resource *resource;

-

-	private:

-		typedef unsigned char byte;

-		typedef unsigned short word;

-		typedef unsigned int dword;

-		typedef uint64_t qword;

-

-		#if S3TC_SUPPORT

-		struct DXT1

-		{

-			word c0;

-			word c1;

-			dword lut;

-		};

-

-		struct DXT3

-		{

-			qword a;

-

-			word c0;

-			word c1;

-			dword lut;

-		};

-

-		struct DXT5

-		{

-			union

-			{

-				struct

-				{

-					byte a0;

-					byte a1;

-				};

-

-				qword alut;   // Skip first 16 bit

-			};

-

-			word c0;

-			word c1;

-			dword clut;

-		};

-		#endif

-

-		struct ATI2

-		{

-			union

-			{

-				struct

-				{

-					byte y0;

-					byte y1;

-				};

-

-				qword ylut;   // Skip first 16 bit

-			};

-

-			union

-			{

-				struct

-				{

-					byte x0;

-					byte x1;

-				};

-

-				qword xlut;   // Skip first 16 bit

-			};

-		};

-

-		struct ATI1

-		{

-			union

-			{

-				struct

-				{

-					byte r0;

-					byte r1;

-				};

-

-				qword rlut;   // Skip first 16 bit

-			};

-		};

-

-		static void decodeR8G8B8(Buffer &destination, const Buffer &source);

-		static void decodeX1R5G5B5(Buffer &destination, const Buffer &source);

-		static void decodeA1R5G5B5(Buffer &destination, const Buffer &source);

-		static void decodeX4R4G4B4(Buffer &destination, const Buffer &source);

-		static void decodeA4R4G4B4(Buffer &destination, const Buffer &source);

-		static void decodeP8(Buffer &destination, const Buffer &source);

-

-		#if S3TC_SUPPORT

-		static void decodeDXT1(Buffer &internal, const Buffer &external);

-		static void decodeDXT3(Buffer &internal, const Buffer &external);

-		static void decodeDXT5(Buffer &internal, const Buffer &external);

-		#endif

-		static void decodeATI1(Buffer &internal, const Buffer &external);

-		static void decodeATI2(Buffer &internal, const Buffer &external);

-		static void decodeEAC(Buffer &internal, const Buffer &external, int nbChannels, bool isSigned);

-		static void decodeETC2(Buffer &internal, const Buffer &external, int nbAlphaBits, bool isSRGB);

-		static void decodeASTC(Buffer &internal, const Buffer &external, int xSize, int ySize, int zSize, bool isSRGB);

-

-		static void update(Buffer &destination, Buffer &source);

-		static void genericUpdate(Buffer &destination, Buffer &source);

-		static void *allocateBuffer(int width, int height, int depth, Format format);

-		static void memfill4(void *buffer, int pattern, int bytes);

-

-		bool identicalFormats() const;

-		Format selectInternalFormat(Format format) const;

-

-		void resolve();

-

-		Buffer external;

-		Buffer internal;

-		Buffer stencil;

-

-		const bool lockable;

-		const bool renderTarget;

-

-		bool dirtyMipmaps;

-		unsigned int paletteUsed;

-

-		static unsigned int *palette;   // FIXME: Not multi-device safe

-		static unsigned int paletteID;

-

-		bool hasParent;

-		bool ownExternal;

-	};

-}

-

-#undef min

-#undef max

-

-namespace sw

-{

-	void *Surface::lock(int x, int y, int z, Lock lock, Accessor client, bool internal)

-	{

-		return internal ? lockInternal(x, y, z, lock, client) : lockExternal(x, y, z, lock, client);

-	}

-

-	void Surface::unlock(bool internal)

-	{

-		return internal ? unlockInternal() : unlockExternal();

-	}

-

-	int Surface::getWidth() const

-	{

-		return external.width;

-	}

-

-	int Surface::getHeight() const

-	{

-		return external.height;

-	}

-

-	int Surface::getDepth() const

-	{

-		return external.depth;

-	}

-

-	Format Surface::getFormat(bool internal) const

-	{

-		return internal ? getInternalFormat() : getExternalFormat();

-	}

-

-	int Surface::getPitchB(bool internal) const

-	{

-		return internal ? getInternalPitchB() : getExternalPitchB();

-	}

-

-	int Surface::getPitchP(bool internal) const

-	{

-		return internal ? getInternalPitchP() : getExternalPitchB();

-	}

-

-	int Surface::getSliceB(bool internal) const

-	{

-		return internal ? getInternalSliceB() : getExternalSliceB();

-	}

-

-	int Surface::getSliceP(bool internal) const

-	{

-		return internal ? getInternalSliceP() : getExternalSliceB();

-	}

-

-	Format Surface::getExternalFormat() const

-	{

-		return external.format;

-	}

-

-	int Surface::getExternalPitchB() const

-	{

-		return external.pitchB;

-	}

-

-	int Surface::getExternalPitchP() const

-	{

-		return external.pitchP;

-	}

-

-	int Surface::getExternalSliceB() const

-	{

-		return external.sliceB;

-	}

-

-	int Surface::getExternalSliceP() const

-	{

-		return external.sliceP;

-	}

-

-	Format Surface::getInternalFormat() const

-	{

-		return internal.format;

-	}

-

-	int Surface::getInternalPitchB() const

-	{

-		return internal.pitchB;

-	}

-

-	int Surface::getInternalPitchP() const

-	{

-		return internal.pitchP;

-	}

-

-	int Surface::getInternalSliceB() const

-	{

-		return internal.sliceB;

-	}

-

-	int Surface::getInternalSliceP() const

-	{

-		return internal.sliceP;

-	}

-

-	int Surface::getStencilPitchB() const

-	{

-		return stencil.pitchB;

-	}

-

-	int Surface::getStencilSliceB() const

-	{

-		return stencil.sliceB;

-	}

-

-	int Surface::getMultiSampleCount() const

-	{

-		return sw::min(internal.depth, 4);

-	}

-

-	int Surface::getSuperSampleCount() const

-	{

-		return internal.depth > 4 ? internal.depth / 4 : 1;

-	}

-

-	bool Surface::isExternalDirty() const

-	{

-		return external.buffer && external.buffer != internal.buffer && external.dirty;

-	}

-}

-

-#endif   // sw_Surface_hpp

+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef sw_Surface_hpp
+#define sw_Surface_hpp
+
+#include "Color.hpp"
+#include "Main/Config.hpp"
+#include "Common/Resource.hpp"
+
+namespace sw
+{
+	class Resource;
+
+	struct Rect
+	{
+		Rect() {}
+		Rect(int x0i, int y0i, int x1i, int y1i) : x0(x0i), y0(y0i), x1(x1i), y1(y1i) {}
+
+		void clip(int minX, int minY, int maxX, int maxY);
+
+		int width() const  { return x1 - x0; }
+		int height() const { return y1 - y0; }
+
+		int x0;   // Inclusive
+		int y0;   // Inclusive
+		int x1;   // Exclusive
+		int y1;   // Exclusive
+	};
+
+	struct SliceRect : public Rect
+	{
+		SliceRect() : slice(0) {}
+		SliceRect(const Rect& rect) : Rect(rect), slice(0) {}
+		SliceRect(const Rect& rect, int s) : Rect(rect), slice(s) {}
+		SliceRect(int x0, int y0, int x1, int y1, int s) : Rect(x0, y0, x1, y1), slice(s) {}
+		int slice;
+	};
+
+	enum Format : unsigned char
+	{
+		FORMAT_NULL,
+
+		FORMAT_A8,
+		FORMAT_R8I,
+		FORMAT_R8UI,
+		FORMAT_R8I_SNORM,
+		FORMAT_R8, // UI_SNORM
+		FORMAT_R16I,
+		FORMAT_R16UI,
+		FORMAT_R32I,
+		FORMAT_R32UI,
+		FORMAT_R3G3B2,
+		FORMAT_A8R3G3B2,
+		FORMAT_X4R4G4B4,
+		FORMAT_A4R4G4B4,
+		FORMAT_R4G4B4A4,
+		FORMAT_R5G6B5,
+		FORMAT_R8G8B8,
+		FORMAT_B8G8R8,
+		FORMAT_X8R8G8B8,
+		FORMAT_A8R8G8B8,
+		FORMAT_X8B8G8R8I,
+		FORMAT_X8B8G8R8UI,
+		FORMAT_X8B8G8R8I_SNORM,
+		FORMAT_X8B8G8R8, // UI_SNORM
+		FORMAT_A8B8G8R8I,
+		FORMAT_A8B8G8R8UI,
+		FORMAT_A8B8G8R8I_SNORM,
+		FORMAT_A8B8G8R8, // UI_SNORM
+		FORMAT_SRGB8_X8,
+		FORMAT_SRGB8_A8,
+		FORMAT_X1R5G5B5,
+		FORMAT_A1R5G5B5,
+		FORMAT_R5G5B5A1,
+		FORMAT_G8R8I,
+		FORMAT_G8R8UI,
+		FORMAT_G8R8I_SNORM,
+		FORMAT_G8R8, // UI_SNORM
+		FORMAT_G16R16, // D3D format
+		FORMAT_G16R16I,
+		FORMAT_G16R16UI,
+		FORMAT_G32R32I,
+		FORMAT_G32R32UI,
+		FORMAT_A2R10G10B10,
+		FORMAT_A2B10G10R10,
+		FORMAT_A16B16G16R16, // D3D format
+		FORMAT_X16B16G16R16I,
+		FORMAT_X16B16G16R16UI,
+		FORMAT_A16B16G16R16I,
+		FORMAT_A16B16G16R16UI,
+		FORMAT_X32B32G32R32I,
+		FORMAT_X32B32G32R32UI,
+		FORMAT_A32B32G32R32I,
+		FORMAT_A32B32G32R32UI,
+		// Paletted formats
+		FORMAT_P8,
+		FORMAT_A8P8,
+		// Compressed formats
+		FORMAT_DXT1,
+		FORMAT_DXT3,
+		FORMAT_DXT5,
+		FORMAT_ATI1,
+		FORMAT_ATI2,
+		FORMAT_ETC1,
+		FORMAT_R11_EAC,
+		FORMAT_SIGNED_R11_EAC,
+		FORMAT_RG11_EAC,
+		FORMAT_SIGNED_RG11_EAC,
+		FORMAT_RGB8_ETC2,
+		FORMAT_SRGB8_ETC2,
+		FORMAT_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
+		FORMAT_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,
+		FORMAT_RGBA8_ETC2_EAC,
+		FORMAT_SRGB8_ALPHA8_ETC2_EAC,
+		FORMAT_RGBA_ASTC_4x4_KHR,
+		FORMAT_RGBA_ASTC_5x4_KHR,
+		FORMAT_RGBA_ASTC_5x5_KHR,
+		FORMAT_RGBA_ASTC_6x5_KHR,
+		FORMAT_RGBA_ASTC_6x6_KHR,
+		FORMAT_RGBA_ASTC_8x5_KHR,
+		FORMAT_RGBA_ASTC_8x6_KHR,
+		FORMAT_RGBA_ASTC_8x8_KHR,
+		FORMAT_RGBA_ASTC_10x5_KHR,
+		FORMAT_RGBA_ASTC_10x6_KHR,
+		FORMAT_RGBA_ASTC_10x8_KHR,
+		FORMAT_RGBA_ASTC_10x10_KHR,
+		FORMAT_RGBA_ASTC_12x10_KHR,
+		FORMAT_RGBA_ASTC_12x12_KHR,
+		FORMAT_SRGB8_ALPHA8_ASTC_4x4_KHR,
+		FORMAT_SRGB8_ALPHA8_ASTC_5x4_KHR,
+		FORMAT_SRGB8_ALPHA8_ASTC_5x5_KHR,
+		FORMAT_SRGB8_ALPHA8_ASTC_6x5_KHR,
+		FORMAT_SRGB8_ALPHA8_ASTC_6x6_KHR,
+		FORMAT_SRGB8_ALPHA8_ASTC_8x5_KHR,
+		FORMAT_SRGB8_ALPHA8_ASTC_8x6_KHR,
+		FORMAT_SRGB8_ALPHA8_ASTC_8x8_KHR,
+		FORMAT_SRGB8_ALPHA8_ASTC_10x5_KHR,
+		FORMAT_SRGB8_ALPHA8_ASTC_10x6_KHR,
+		FORMAT_SRGB8_ALPHA8_ASTC_10x8_KHR,
+		FORMAT_SRGB8_ALPHA8_ASTC_10x10_KHR,
+		FORMAT_SRGB8_ALPHA8_ASTC_12x10_KHR,
+		FORMAT_SRGB8_ALPHA8_ASTC_12x12_KHR,
+		// Floating-point formats
+		FORMAT_A16F,
+		FORMAT_R16F,
+		FORMAT_G16R16F,
+		FORMAT_B16G16R16F,
+		FORMAT_A16B16G16R16F,
+		FORMAT_A32F,
+		FORMAT_R32F,
+		FORMAT_G32R32F,
+		FORMAT_B32G32R32F,
+		FORMAT_X32B32G32R32F,
+		FORMAT_A32B32G32R32F,
+		// Bump map formats
+		FORMAT_V8U8,
+		FORMAT_L6V5U5,
+		FORMAT_Q8W8V8U8,
+		FORMAT_X8L8V8U8,
+		FORMAT_A2W10V10U10,
+		FORMAT_V16U16,
+		FORMAT_A16W16V16U16,
+		FORMAT_Q16W16V16U16,
+		// Luminance formats
+		FORMAT_L8,
+		FORMAT_A4L4,
+		FORMAT_L16,
+		FORMAT_A8L8,
+		FORMAT_L16F,
+		FORMAT_A16L16F,
+		FORMAT_L32F,
+		FORMAT_A32L32F,
+		// Depth/stencil formats
+		FORMAT_D16,
+		FORMAT_D32,
+		FORMAT_D24X8,
+		FORMAT_D24S8,
+		FORMAT_D24FS8,
+		FORMAT_D32F,                 // Quad layout
+		FORMAT_D32F_COMPLEMENTARY,   // Quad layout, 1 - z
+		FORMAT_D32F_LOCKABLE,        // Linear layout
+		FORMAT_D32FS8_TEXTURE,       // Linear layout, no PCF
+		FORMAT_D32FS8_SHADOW,        // Linear layout, PCF
+		FORMAT_DF24S8,
+		FORMAT_DF16S8,
+		FORMAT_INTZ,
+		FORMAT_S8,
+		// Quad layout framebuffer
+		FORMAT_X8G8R8B8Q,
+		FORMAT_A8G8R8B8Q,
+		// YUV formats
+		FORMAT_YV12_BT601,
+		FORMAT_YV12_BT709,
+		FORMAT_YV12_JFIF,    // Full-swing BT.601
+
+		FORMAT_LAST = FORMAT_YV12_JFIF
+	};
+
+	enum Lock
+	{
+		LOCK_UNLOCKED,
+		LOCK_READONLY,
+		LOCK_WRITEONLY,
+		LOCK_READWRITE,
+		LOCK_DISCARD
+	};
+
+	class Surface
+	{
+	private:
+		struct Buffer
+		{
+		public:
+			void write(int x, int y, int z, const Color<float> &color);
+			void write(int x, int y, const Color<float> &color);
+			void write(void *element, const Color<float> &color);
+			Color<float> read(int x, int y, int z) const;
+			Color<float> read(int x, int y) const;
+			Color<float> read(void *element) const;
+			Color<float> sample(float x, float y, float z) const;
+			Color<float> sample(float x, float y) const;
+
+			void *lockRect(int x, int y, int z, Lock lock);
+			void unlockRect();
+
+			void *buffer;
+			int width;
+			int height;
+			int depth;
+			int bytes;
+			int pitchB;
+			int pitchP;
+			int sliceB;
+			int sliceP;
+			Format format;
+			Lock lock;
+
+			bool dirty;
+		};
+
+	public:
+		Surface(int width, int height, int depth, Format format, void *pixels, int pitch, int slice);
+		Surface(Resource *texture, int width, int height, int depth, Format format, bool lockable, bool renderTarget, int pitchP = 0);
+
+		virtual ~Surface();
+
+		inline void *lock(int x, int y, int z, Lock lock, Accessor client, bool internal = false);
+		inline void unlock(bool internal = false);
+		inline int getWidth() const;
+		inline int getHeight() const;
+		inline int getDepth() const;
+		inline Format getFormat(bool internal = false) const;
+		inline int getPitchB(bool internal = false) const;
+		inline int getPitchP(bool internal = false) const;
+		inline int getSliceB(bool internal = false) const;
+		inline int getSliceP(bool internal = false) const;
+
+		void *lockExternal(int x, int y, int z, Lock lock, Accessor client);
+		void unlockExternal();
+		inline Format getExternalFormat() const;
+		inline int getExternalPitchB() const;
+		inline int getExternalPitchP() const;
+		inline int getExternalSliceB() const;
+		inline int getExternalSliceP() const;
+
+		virtual void *lockInternal(int x, int y, int z, Lock lock, Accessor client);
+		virtual void unlockInternal();
+		inline Format getInternalFormat() const;
+		inline int getInternalPitchB() const;
+		inline int getInternalPitchP() const;
+		inline int getInternalSliceB() const;
+		inline int getInternalSliceP() const;
+
+		void *lockStencil(int front, Accessor client);
+		void unlockStencil();
+		inline int getStencilPitchB() const;
+		inline int getStencilSliceB() const;
+
+		inline int getMultiSampleCount() const;
+		inline int getSuperSampleCount() const;
+
+		bool isEntire(const SliceRect& rect) const;
+		SliceRect getRect() const;
+		void clearDepth(float depth, int x0, int y0, int width, int height);
+		void clearStencil(unsigned char stencil, unsigned char mask, int x0, int y0, int width, int height);
+		void fill(const Color<float> &color, int x0, int y0, int width, int height);
+
+		Color<float> readExternal(int x, int y, int z) const;
+		Color<float> readExternal(int x, int y) const;
+		Color<float> sampleExternal(float x, float y, float z) const;
+		Color<float> sampleExternal(float x, float y) const;
+		void writeExternal(int x, int y, int z, const Color<float> &color);
+		void writeExternal(int x, int y, const Color<float> &color);
+
+		void copyInternal(const Surface* src, int x, int y, float srcX, float srcY, bool filter);
+		void copyInternal(const Surface* src, int x, int y, int z, float srcX, float srcY, float srcZ, bool filter);
+
+		bool hasStencil() const;
+		bool hasDepth() const;
+		bool hasPalette() const;
+		bool isRenderTarget() const;
+
+		bool hasDirtyMipmaps() const;
+		void cleanMipmaps();
+		inline bool isExternalDirty() const;
+		Resource *getResource();
+
+		static int bytes(Format format);
+		static int pitchB(int width, Format format, bool target);
+		static int pitchP(int width, Format format, bool target);
+		static int sliceB(int width, int height, Format format, bool target);
+		static int sliceP(int width, int height, Format format, bool target);
+		static unsigned int size(int width, int height, int depth, Format format);   // FIXME: slice * depth
+
+		static bool isStencil(Format format);
+		static bool isDepth(Format format);
+		static bool isPalette(Format format);
+
+		static bool isFloatFormat(Format format);
+		static bool isUnsignedComponent(Format format, int component);
+		static bool isSRGBreadable(Format format);
+		static bool isSRGBwritable(Format format);
+		static bool isCompressed(Format format);
+		static bool isNonNormalizedInteger(Format format);
+		static int componentCount(Format format);
+
+		static void setTexturePalette(unsigned int *palette);
+
+	protected:
+		sw::Resource *resource;
+
+	private:
+		typedef unsigned char byte;
+		typedef unsigned short word;
+		typedef unsigned int dword;
+		typedef uint64_t qword;
+
+		#if S3TC_SUPPORT
+		struct DXT1
+		{
+			word c0;
+			word c1;
+			dword lut;
+		};
+
+		struct DXT3
+		{
+			qword a;
+
+			word c0;
+			word c1;
+			dword lut;
+		};
+
+		struct DXT5
+		{
+			union
+			{
+				struct
+				{
+					byte a0;
+					byte a1;
+				};
+
+				qword alut;   // Skip first 16 bit
+			};
+
+			word c0;
+			word c1;
+			dword clut;
+		};
+		#endif
+
+		struct ATI2
+		{
+			union
+			{
+				struct
+				{
+					byte y0;
+					byte y1;
+				};
+
+				qword ylut;   // Skip first 16 bit
+			};
+
+			union
+			{
+				struct
+				{
+					byte x0;
+					byte x1;
+				};
+
+				qword xlut;   // Skip first 16 bit
+			};
+		};
+
+		struct ATI1
+		{
+			union
+			{
+				struct
+				{
+					byte r0;
+					byte r1;
+				};
+
+				qword rlut;   // Skip first 16 bit
+			};
+		};
+
+		static void decodeR8G8B8(Buffer &destination, const Buffer &source);
+		static void decodeX1R5G5B5(Buffer &destination, const Buffer &source);
+		static void decodeA1R5G5B5(Buffer &destination, const Buffer &source);
+		static void decodeX4R4G4B4(Buffer &destination, const Buffer &source);
+		static void decodeA4R4G4B4(Buffer &destination, const Buffer &source);
+		static void decodeP8(Buffer &destination, const Buffer &source);
+
+		#if S3TC_SUPPORT
+		static void decodeDXT1(Buffer &internal, const Buffer &external);
+		static void decodeDXT3(Buffer &internal, const Buffer &external);
+		static void decodeDXT5(Buffer &internal, const Buffer &external);
+		#endif
+		static void decodeATI1(Buffer &internal, const Buffer &external);
+		static void decodeATI2(Buffer &internal, const Buffer &external);
+		static void decodeEAC(Buffer &internal, const Buffer &external, int nbChannels, bool isSigned);
+		static void decodeETC2(Buffer &internal, const Buffer &external, int nbAlphaBits, bool isSRGB);
+		static void decodeASTC(Buffer &internal, const Buffer &external, int xSize, int ySize, int zSize, bool isSRGB);
+
+		static void update(Buffer &destination, Buffer &source);
+		static void genericUpdate(Buffer &destination, Buffer &source);
+		static void *allocateBuffer(int width, int height, int depth, Format format);
+		static void memfill4(void *buffer, int pattern, int bytes);
+
+		bool identicalFormats() const;
+		Format selectInternalFormat(Format format) const;
+
+		void resolve();
+
+		Buffer external;
+		Buffer internal;
+		Buffer stencil;
+
+		const bool lockable;
+		const bool renderTarget;
+
+		bool dirtyMipmaps;
+		unsigned int paletteUsed;
+
+		static unsigned int *palette;   // FIXME: Not multi-device safe
+		static unsigned int paletteID;
+
+		bool hasParent;
+		bool ownExternal;
+	};
+}
+
+#undef min
+#undef max
+
+namespace sw
+{
+	void *Surface::lock(int x, int y, int z, Lock lock, Accessor client, bool internal)
+	{
+		return internal ? lockInternal(x, y, z, lock, client) : lockExternal(x, y, z, lock, client);
+	}
+
+	void Surface::unlock(bool internal)
+	{
+		return internal ? unlockInternal() : unlockExternal();
+	}
+
+	int Surface::getWidth() const
+	{
+		return external.width;
+	}
+
+	int Surface::getHeight() const
+	{
+		return external.height;
+	}
+
+	int Surface::getDepth() const
+	{
+		return external.depth;
+	}
+
+	Format Surface::getFormat(bool internal) const
+	{
+		return internal ? getInternalFormat() : getExternalFormat();
+	}
+
+	int Surface::getPitchB(bool internal) const
+	{
+		return internal ? getInternalPitchB() : getExternalPitchB();
+	}
+
+	int Surface::getPitchP(bool internal) const
+	{
+		return internal ? getInternalPitchP() : getExternalPitchB();
+	}
+
+	int Surface::getSliceB(bool internal) const
+	{
+		return internal ? getInternalSliceB() : getExternalSliceB();
+	}
+
+	int Surface::getSliceP(bool internal) const
+	{
+		return internal ? getInternalSliceP() : getExternalSliceB();
+	}
+
+	Format Surface::getExternalFormat() const
+	{
+		return external.format;
+	}
+
+	int Surface::getExternalPitchB() const
+	{
+		return external.pitchB;
+	}
+
+	int Surface::getExternalPitchP() const
+	{
+		return external.pitchP;
+	}
+
+	int Surface::getExternalSliceB() const
+	{
+		return external.sliceB;
+	}
+
+	int Surface::getExternalSliceP() const
+	{
+		return external.sliceP;
+	}
+
+	Format Surface::getInternalFormat() const
+	{
+		return internal.format;
+	}
+
+	int Surface::getInternalPitchB() const
+	{
+		return internal.pitchB;
+	}
+
+	int Surface::getInternalPitchP() const
+	{
+		return internal.pitchP;
+	}
+
+	int Surface::getInternalSliceB() const
+	{
+		return internal.sliceB;
+	}
+
+	int Surface::getInternalSliceP() const
+	{
+		return internal.sliceP;
+	}
+
+	int Surface::getStencilPitchB() const
+	{
+		return stencil.pitchB;
+	}
+
+	int Surface::getStencilSliceB() const
+	{
+		return stencil.sliceB;
+	}
+
+	int Surface::getMultiSampleCount() const
+	{
+		return sw::min(internal.depth, 4);
+	}
+
+	int Surface::getSuperSampleCount() const
+	{
+		return internal.depth > 4 ? internal.depth / 4 : 1;
+	}
+
+	bool Surface::isExternalDirty() const
+	{
+		return external.buffer && external.buffer != internal.buffer && external.dirty;
+	}
+}
+
+#endif   // sw_Surface_hpp
diff --git a/src/Renderer/TextureStage.cpp b/src/Renderer/TextureStage.cpp
index d102d19..583f82e 100644
--- a/src/Renderer/TextureStage.cpp
+++ b/src/Renderer/TextureStage.cpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2012 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #include "TextureStage.hpp"
 
@@ -238,7 +241,7 @@
 				return thirdArgument == source;
 			}
 		}
-		
+	
 		return false;
 	}
 
diff --git a/src/Renderer/TextureStage.hpp b/src/Renderer/TextureStage.hpp
index 199043f..2c9ecbd 100644
--- a/src/Renderer/TextureStage.hpp
+++ b/src/Renderer/TextureStage.hpp
@@ -1,195 +1,198 @@
-// 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_TextureStage_hpp

-#define sw_TextureStage_hpp

-

-#include "Common/Types.hpp"

-#include "Common/Math.hpp"

-#include "Renderer/Color.hpp"

-

-namespace sw

-{

-	class Sampler;

-	class PixelRoutine;

-	class Context;

-

-	class TextureStage

-	{

-		friend class Context;   // FIXME

-

-	public:

-		enum StageOperation

-		{

-			STAGE_DISABLE,

-			STAGE_SELECTARG1,

-			STAGE_SELECTARG2,

-			STAGE_SELECTARG3,

-			STAGE_MODULATE,

-			STAGE_MODULATE2X,

-			STAGE_MODULATE4X,

-			STAGE_ADD,

-			STAGE_ADDSIGNED,

-			STAGE_ADDSIGNED2X,

-			STAGE_SUBTRACT,

-			STAGE_ADDSMOOTH,

-			STAGE_MULTIPLYADD,

-			STAGE_LERP,

-			STAGE_DOT3,

-			STAGE_BLENDCURRENTALPHA,

-			STAGE_BLENDDIFFUSEALPHA,

-			STAGE_BLENDFACTORALPHA,

-			STAGE_BLENDTEXTUREALPHA,

-			STAGE_BLENDTEXTUREALPHAPM,

-			STAGE_PREMODULATE,

-			STAGE_MODULATEALPHA_ADDCOLOR,

-			STAGE_MODULATECOLOR_ADDALPHA,

-			STAGE_MODULATEINVALPHA_ADDCOLOR,

-			STAGE_MODULATEINVCOLOR_ADDALPHA,

-			STAGE_BUMPENVMAP,

-			STAGE_BUMPENVMAPLUMINANCE,

-

-			STAGE_LAST = STAGE_BUMPENVMAPLUMINANCE

-		};

-

-		enum SourceArgument

-		{

-			SOURCE_TEXTURE,

-			SOURCE_CONSTANT,

-			SOURCE_CURRENT,

-			SOURCE_DIFFUSE,

-			SOURCE_SPECULAR,

-			SOURCE_TEMP,

-			SOURCE_TFACTOR,

-

-			SOURCE_LAST = SOURCE_TFACTOR

-		};

-

-		enum DestinationArgument

-		{

-			DESTINATION_CURRENT,

-			DESTINATION_TEMP,

-

-			DESTINATION_LAST = DESTINATION_TEMP

-		};

-

-		enum ArgumentModifier

-		{

-			MODIFIER_COLOR,

-			MODIFIER_INVCOLOR,

-			MODIFIER_ALPHA,

-			MODIFIER_INVALPHA,

-

-			MODIFIER_LAST = MODIFIER_INVALPHA

-		};

-

-		struct State

-		{

-			State();

-

-			unsigned int stageOperation			: BITS(STAGE_LAST);

-			unsigned int firstArgument			: BITS(SOURCE_LAST);

-			unsigned int secondArgument			: BITS(SOURCE_LAST);

-			unsigned int thirdArgument			: BITS(SOURCE_LAST);

-			unsigned int stageOperationAlpha	: BITS(STAGE_LAST);

-			unsigned int firstArgumentAlpha		: BITS(SOURCE_LAST);

-			unsigned int secondArgumentAlpha	: BITS(SOURCE_LAST);

-			unsigned int thirdArgumentAlpha		: BITS(SOURCE_LAST);

-			unsigned int firstModifier			: BITS(MODIFIER_LAST);

-			unsigned int secondModifier			: BITS(MODIFIER_LAST);

-			unsigned int thirdModifier			: BITS(MODIFIER_LAST);

-			unsigned int firstModifierAlpha		: BITS(MODIFIER_LAST);

-			unsigned int secondModifierAlpha	: BITS(MODIFIER_LAST);

-			unsigned int thirdModifierAlpha		: BITS(MODIFIER_LAST);

-			unsigned int destinationArgument	: BITS(DESTINATION_LAST);

-			unsigned int texCoordIndex			: BITS(7);

-

-			unsigned int cantUnderflow			: 1;

-			unsigned int usesTexture			: 1;

-		};

-

-		struct Uniforms

-		{

-			word4 constantColor4[4];

-			float4 bumpmapMatrix4F[2][2];

-			word4 bumpmapMatrix4W[2][2];

-			word4 luminanceScale4;

-			word4 luminanceOffset4;

-		};

-

-		TextureStage();

-

-		~TextureStage();

-

-		void init(int stage, const Sampler *sampler, const TextureStage *previousStage);

-

-		State textureStageState() const;

-

-		void setConstantColor(const Color<float> &constantColor);

-		void setBumpmapMatrix(int element, float value);

-		void setLuminanceScale(float value);

-		void setLuminanceOffset(float value);

-

-		void setTexCoordIndex(unsigned int texCoordIndex);

-		void setStageOperation(StageOperation stageOperation);

-		void setFirstArgument(SourceArgument firstArgument);

-		void setSecondArgument(SourceArgument secondArgument);

-		void setThirdArgument(SourceArgument thirdArgument);

-		void setStageOperationAlpha(StageOperation stageOperationAlpha);

-		void setFirstArgumentAlpha(SourceArgument firstArgumentAlpha);

-		void setSecondArgumentAlpha(SourceArgument secondArgumentAlpha);

-		void setThirdArgumentAlpha(SourceArgument thirdArgumentAlpha);

-		void setFirstModifier(ArgumentModifier firstModifier);

-		void setSecondModifier(ArgumentModifier secondModifier);

-		void setThirdModifier(ArgumentModifier thirdModifier);

-		void setFirstModifierAlpha(ArgumentModifier firstModifierAlpha);

-		void setSecondModifierAlpha(ArgumentModifier secondModifierAlpha);

-		void setThirdModifierAlpha(ArgumentModifier thirdModifierAlpha);

-		void setDestinationArgument(DestinationArgument destinationArgument);

-

-		Uniforms uniforms;   // FIXME: Private

-

-	private:

-		bool usesColor(SourceArgument source) const;

-		bool usesAlpha(SourceArgument source) const;

-		bool uses(SourceArgument source) const;

-		bool usesCurrent() const;

-		bool usesDiffuse() const;

-		bool usesSpecular() const;

-		bool usesTexture() const;

-		bool isStageDisabled() const;

-		bool writesCurrent() const;

-

-		int stage;

-

-		StageOperation stageOperation;

-		SourceArgument firstArgument;

-		SourceArgument secondArgument;

-		SourceArgument thirdArgument;

-		StageOperation stageOperationAlpha;

-		SourceArgument firstArgumentAlpha;

-		SourceArgument secondArgumentAlpha;

-		SourceArgument thirdArgumentAlpha;

-		ArgumentModifier firstModifier;

-		ArgumentModifier secondModifier;

-		ArgumentModifier thirdModifier;

-		ArgumentModifier firstModifierAlpha;

-		ArgumentModifier secondModifierAlpha;

-		ArgumentModifier thirdModifierAlpha;

-		DestinationArgument destinationArgument;

-

-		int texCoordIndex;

-		const Sampler *sampler;

-		const TextureStage *previousStage;

-	};

-}

-

-#endif  // sw_TextureStage_hpp

+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef sw_TextureStage_hpp
+#define sw_TextureStage_hpp
+
+#include "Common/Types.hpp"
+#include "Common/Math.hpp"
+#include "Renderer/Color.hpp"
+
+namespace sw
+{
+	class Sampler;
+	class PixelRoutine;
+	class Context;
+
+	class TextureStage
+	{
+		friend class Context;   // FIXME
+
+	public:
+		enum StageOperation
+		{
+			STAGE_DISABLE,
+			STAGE_SELECTARG1,
+			STAGE_SELECTARG2,
+			STAGE_SELECTARG3,
+			STAGE_MODULATE,
+			STAGE_MODULATE2X,
+			STAGE_MODULATE4X,
+			STAGE_ADD,
+			STAGE_ADDSIGNED,
+			STAGE_ADDSIGNED2X,
+			STAGE_SUBTRACT,
+			STAGE_ADDSMOOTH,
+			STAGE_MULTIPLYADD,
+			STAGE_LERP,
+			STAGE_DOT3,
+			STAGE_BLENDCURRENTALPHA,
+			STAGE_BLENDDIFFUSEALPHA,
+			STAGE_BLENDFACTORALPHA,
+			STAGE_BLENDTEXTUREALPHA,
+			STAGE_BLENDTEXTUREALPHAPM,
+			STAGE_PREMODULATE,
+			STAGE_MODULATEALPHA_ADDCOLOR,
+			STAGE_MODULATECOLOR_ADDALPHA,
+			STAGE_MODULATEINVALPHA_ADDCOLOR,
+			STAGE_MODULATEINVCOLOR_ADDALPHA,
+			STAGE_BUMPENVMAP,
+			STAGE_BUMPENVMAPLUMINANCE,
+
+			STAGE_LAST = STAGE_BUMPENVMAPLUMINANCE
+		};
+
+		enum SourceArgument
+		{
+			SOURCE_TEXTURE,
+			SOURCE_CONSTANT,
+			SOURCE_CURRENT,
+			SOURCE_DIFFUSE,
+			SOURCE_SPECULAR,
+			SOURCE_TEMP,
+			SOURCE_TFACTOR,
+
+			SOURCE_LAST = SOURCE_TFACTOR
+		};
+
+		enum DestinationArgument
+		{
+			DESTINATION_CURRENT,
+			DESTINATION_TEMP,
+
+			DESTINATION_LAST = DESTINATION_TEMP
+		};
+
+		enum ArgumentModifier
+		{
+			MODIFIER_COLOR,
+			MODIFIER_INVCOLOR,
+			MODIFIER_ALPHA,
+			MODIFIER_INVALPHA,
+
+			MODIFIER_LAST = MODIFIER_INVALPHA
+		};
+
+		struct State
+		{
+			State();
+
+			unsigned int stageOperation			: BITS(STAGE_LAST);
+			unsigned int firstArgument			: BITS(SOURCE_LAST);
+			unsigned int secondArgument			: BITS(SOURCE_LAST);
+			unsigned int thirdArgument			: BITS(SOURCE_LAST);
+			unsigned int stageOperationAlpha	: BITS(STAGE_LAST);
+			unsigned int firstArgumentAlpha		: BITS(SOURCE_LAST);
+			unsigned int secondArgumentAlpha	: BITS(SOURCE_LAST);
+			unsigned int thirdArgumentAlpha		: BITS(SOURCE_LAST);
+			unsigned int firstModifier			: BITS(MODIFIER_LAST);
+			unsigned int secondModifier			: BITS(MODIFIER_LAST);
+			unsigned int thirdModifier			: BITS(MODIFIER_LAST);
+			unsigned int firstModifierAlpha		: BITS(MODIFIER_LAST);
+			unsigned int secondModifierAlpha	: BITS(MODIFIER_LAST);
+			unsigned int thirdModifierAlpha		: BITS(MODIFIER_LAST);
+			unsigned int destinationArgument	: BITS(DESTINATION_LAST);
+			unsigned int texCoordIndex			: BITS(7);
+
+			unsigned int cantUnderflow			: 1;
+			unsigned int usesTexture			: 1;
+		};
+
+		struct Uniforms
+		{
+			word4 constantColor4[4];
+			float4 bumpmapMatrix4F[2][2];
+			word4 bumpmapMatrix4W[2][2];
+			word4 luminanceScale4;
+			word4 luminanceOffset4;
+		};
+
+		TextureStage();
+
+		~TextureStage();
+
+		void init(int stage, const Sampler *sampler, const TextureStage *previousStage);
+
+		State textureStageState() const;
+
+		void setConstantColor(const Color<float> &constantColor);
+		void setBumpmapMatrix(int element, float value);
+		void setLuminanceScale(float value);
+		void setLuminanceOffset(float value);
+
+		void setTexCoordIndex(unsigned int texCoordIndex);
+		void setStageOperation(StageOperation stageOperation);
+		void setFirstArgument(SourceArgument firstArgument);
+		void setSecondArgument(SourceArgument secondArgument);
+		void setThirdArgument(SourceArgument thirdArgument);
+		void setStageOperationAlpha(StageOperation stageOperationAlpha);
+		void setFirstArgumentAlpha(SourceArgument firstArgumentAlpha);
+		void setSecondArgumentAlpha(SourceArgument secondArgumentAlpha);
+		void setThirdArgumentAlpha(SourceArgument thirdArgumentAlpha);
+		void setFirstModifier(ArgumentModifier firstModifier);
+		void setSecondModifier(ArgumentModifier secondModifier);
+		void setThirdModifier(ArgumentModifier thirdModifier);
+		void setFirstModifierAlpha(ArgumentModifier firstModifierAlpha);
+		void setSecondModifierAlpha(ArgumentModifier secondModifierAlpha);
+		void setThirdModifierAlpha(ArgumentModifier thirdModifierAlpha);
+		void setDestinationArgument(DestinationArgument destinationArgument);
+
+		Uniforms uniforms;   // FIXME: Private
+
+	private:
+		bool usesColor(SourceArgument source) const;
+		bool usesAlpha(SourceArgument source) const;
+		bool uses(SourceArgument source) const;
+		bool usesCurrent() const;
+		bool usesDiffuse() const;
+		bool usesSpecular() const;
+		bool usesTexture() const;
+		bool isStageDisabled() const;
+		bool writesCurrent() const;
+
+		int stage;
+
+		StageOperation stageOperation;
+		SourceArgument firstArgument;
+		SourceArgument secondArgument;
+		SourceArgument thirdArgument;
+		StageOperation stageOperationAlpha;
+		SourceArgument firstArgumentAlpha;
+		SourceArgument secondArgumentAlpha;
+		SourceArgument thirdArgumentAlpha;
+		ArgumentModifier firstModifier;
+		ArgumentModifier secondModifier;
+		ArgumentModifier thirdModifier;
+		ArgumentModifier firstModifierAlpha;
+		ArgumentModifier secondModifierAlpha;
+		ArgumentModifier thirdModifierAlpha;
+		DestinationArgument destinationArgument;
+
+		int texCoordIndex;
+		const Sampler *sampler;
+		const TextureStage *previousStage;
+	};
+}
+
+#endif  // sw_TextureStage_hpp
diff --git a/src/Renderer/Triangle.hpp b/src/Renderer/Triangle.hpp
index 845fec8..8a91fab 100644
--- a/src/Renderer/Triangle.hpp
+++ b/src/Renderer/Triangle.hpp
@@ -1,27 +1,30 @@
-// SwiftShader Software Renderer

-//

-// Copyright(c) 2005-2011 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_Triangle_hpp

-#define sw_Triangle_hpp

-

-#include "Vertex.hpp"

-

-namespace sw

-{

-	struct Triangle

-	{

-		Vertex V0;

-		Vertex V1;

-		Vertex V2;

-	};

-}

-

-#endif   // sw_Triangle_hpp

+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef sw_Triangle_hpp
+#define sw_Triangle_hpp
+
+#include "Vertex.hpp"
+
+namespace sw
+{
+	struct Triangle
+	{
+		Vertex V0;
+		Vertex V1;
+		Vertex V2;
+	};
+}
+
+#endif   // sw_Triangle_hpp
diff --git a/src/Renderer/Vector.cpp b/src/Renderer/Vector.cpp
index c67a644..a212f3d 100644
--- a/src/Renderer/Vector.cpp
+++ b/src/Renderer/Vector.cpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2011 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #include "Vector.hpp"
 
diff --git a/src/Renderer/Vector.hpp b/src/Renderer/Vector.hpp
index 2ff1713..e7f261d 100644
--- a/src/Renderer/Vector.hpp
+++ b/src/Renderer/Vector.hpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2011 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #ifndef Vector_hpp
 #define Vector_hpp
diff --git a/src/Renderer/Vertex.hpp b/src/Renderer/Vertex.hpp
index 129d258..01a7273 100644
--- a/src/Renderer/Vertex.hpp
+++ b/src/Renderer/Vertex.hpp
@@ -1,100 +1,103 @@
-// 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 Vertex_hpp

-#define Vertex_hpp

-

-#include "Color.hpp"

-#include "Common/MetaMacro.hpp"

-#include "Common/Types.hpp"

-

-namespace sw

-{

-	enum Out   // Default vertex output semantic

-	{

-		Pos = 0,

-		D0 = 1,   // Diffuse

-		D1 = 2,   // Specular

-		T0 = 3,

-		T1 = 4,

-		T2 = 5,

-		T3 = 6,

-		T4 = 7,

-		T5 = 8,

-		T6 = 9,

-		T7 = 10,

-		Fog = 11,    // x component

-		Pts = Fog,   // y component

-		Unused

-	};

-

-	struct UVWQ

-	{

-		float u;

-		float v;

-		float w;

-		float q;

-

-		float &operator[](int i)

-		{

-			return (&u)[i];

-		}

-	};

-

-	ALIGN(16, struct Vertex

-	{

-		union

-		{

-			struct   // Fixed semantics

-			{

-				union   // Position

-				{

-					struct

-					{

-						float x;

-						float y;

-						float z;

-						float w;

-					};

-

-					struct

-					{

-						float4 P;

-					};

-				};

-

-				float4 C[2];   // Diffuse and specular color

-

-				UVWQ T[8];           // Texture coordinates

-

-				float f;             // Fog

-				float pSize;         // Point size

-				unsigned char padding0[4];

-				unsigned char clipFlags;

-				unsigned char padding1[3];

-			};

-

-			float4 v[12];   // Generic components using semantic declaration

-		};

-

-		struct   // Projected coordinates

-		{

-			int X;

-			int Y;

-			float Z;

-			float W;

-		};

-	});

-

-	META_ASSERT((sizeof(Vertex) & 0x0000000F) == 0);

-}

-

-#endif   // Vertex_hpp

+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef Vertex_hpp
+#define Vertex_hpp
+
+#include "Color.hpp"
+#include "Common/MetaMacro.hpp"
+#include "Common/Types.hpp"
+
+namespace sw
+{
+	enum Out   // Default vertex output semantic
+	{
+		Pos = 0,
+		D0 = 1,   // Diffuse
+		D1 = 2,   // Specular
+		T0 = 3,
+		T1 = 4,
+		T2 = 5,
+		T3 = 6,
+		T4 = 7,
+		T5 = 8,
+		T6 = 9,
+		T7 = 10,
+		Fog = 11,    // x component
+		Pts = Fog,   // y component
+		Unused
+	};
+
+	struct UVWQ
+	{
+		float u;
+		float v;
+		float w;
+		float q;
+
+		float &operator[](int i)
+		{
+			return (&u)[i];
+		}
+	};
+
+	ALIGN(16, struct Vertex
+	{
+		union
+		{
+			struct   // Fixed semantics
+			{
+				union   // Position
+				{
+					struct
+					{
+						float x;
+						float y;
+						float z;
+						float w;
+					};
+
+					struct
+					{
+						float4 P;
+					};
+				};
+
+				float4 C[2];   // Diffuse and specular color
+
+				UVWQ T[8];           // Texture coordinates
+
+				float f;             // Fog
+				float pSize;         // Point size
+				unsigned char padding0[4];
+				unsigned char clipFlags;
+				unsigned char padding1[3];
+			};
+
+			float4 v[12];   // Generic components using semantic declaration
+		};
+
+		struct   // Projected coordinates
+		{
+			int X;
+			int Y;
+			float Z;
+			float W;
+		};
+	});
+
+	META_ASSERT((sizeof(Vertex) & 0x0000000F) == 0);
+}
+
+#endif   // Vertex_hpp
diff --git a/src/Renderer/VertexProcessor.cpp b/src/Renderer/VertexProcessor.cpp
index 24f40cc..0b76520 100644
--- a/src/Renderer/VertexProcessor.cpp
+++ b/src/Renderer/VertexProcessor.cpp
@@ -1,13 +1,16 @@
-// SwiftShader Software Renderer
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
 //
-// Copyright(c) 2005-2012 TransGaming Inc.
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
 //
-// 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.
+//    http://www.apache.org/licenses/LICENSE-2.0
 //
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 #include "VertexProcessor.hpp"
 
diff --git a/src/Renderer/VertexProcessor.hpp b/src/Renderer/VertexProcessor.hpp
index caebbfa..14758bb 100644
--- a/src/Renderer/VertexProcessor.hpp
+++ b/src/Renderer/VertexProcessor.hpp
@@ -1,340 +1,343 @@
-// 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_VertexProcessor_hpp

-#define sw_VertexProcessor_hpp

-

-#include "Matrix.hpp"

-#include "Context.hpp"

-#include "RoutineCache.hpp"

-

-namespace sw

-{

-	struct DrawData;

-

-	struct VertexCache   // FIXME: Variable size

-	{

-		void clear();

-

-		Vertex vertex[16][4];

-		unsigned int tag[16];

-

-		int drawCall;

-	};

-

-	struct VertexTask

-	{

-		unsigned int vertexStart;

-		unsigned int vertexCount;

-		unsigned int verticesPerPrimitive;

-		VertexCache vertexCache;

-	};

-

-	class VertexProcessor

-	{

-	public:

-		struct States

-		{

-			unsigned int computeHash();

-

-			uint64_t shaderID;

-

-			bool fixedFunction             : 1;

-			bool textureSampling           : 1;

-			unsigned int positionRegister  : 4;

-			unsigned int pointSizeRegister : 4;   // 0xF signifies no vertex point size

-

-			unsigned int vertexBlendMatrixCount               : 3;

-			bool indexedVertexBlendEnable                     : 1;

-			bool vertexNormalActive                           : 1;

-			bool normalizeNormals                             : 1;

-			bool vertexLightingActive                         : 1;

-			bool diffuseActive                                : 1;

-			bool specularActive                               : 1;

-			bool vertexSpecularActive                         : 1;

-			unsigned int vertexLightActive                    : 8;

-			MaterialSource vertexDiffuseMaterialSourceActive  : BITS(MATERIAL_LAST);

-			MaterialSource vertexSpecularMaterialSourceActive : BITS(MATERIAL_LAST);

-			MaterialSource vertexAmbientMaterialSourceActive  : BITS(MATERIAL_LAST);

-			MaterialSource vertexEmissiveMaterialSourceActive : BITS(MATERIAL_LAST);

-			bool fogActive                                    : 1;

-			FogMode vertexFogMode                             : BITS(FOG_LAST);

-			bool rangeFogActive                               : 1;

-			bool localViewerActive                            : 1;

-			bool pointSizeActive                              : 1;

-			bool pointScaleActive                             : 1;

-			bool transformFeedbackQueryEnabled                : 1;

-			uint64_t transformFeedbackEnabled                 : 64;

-

-			bool preTransformed : 1;

-			bool superSampling  : 1;

-			bool multiSampling  : 1;

-

-			struct TextureState

-			{

-				TexGen texGenActive                       : BITS(TEXGEN_LAST);

-				unsigned char textureTransformCountActive : 3;

-				unsigned char texCoordIndexActive         : 3;

-			};

-

-			TextureState textureState[8];

-

-			Sampler::State samplerState[VERTEX_TEXTURE_IMAGE_UNITS];

-

-			struct Input

-			{

-				operator bool() const   // Returns true if stream contains data

-				{

-					return count != 0;

-				}

-

-				StreamType type    : BITS(STREAMTYPE_LAST);

-				unsigned int count : 3;

-				bool normalized    : 1;

-			};

-

-			struct Output

-			{

-				union

-				{

-					unsigned char write : 4;

-

-					struct

-					{

-						unsigned char xWrite : 1;

-						unsigned char yWrite : 1;

-						unsigned char zWrite : 1;

-						unsigned char wWrite : 1;

-					};

-				};

-

-				union

-				{

-					unsigned char clamp : 4;

-

-					struct

-					{

-						unsigned char xClamp : 1;

-						unsigned char yClamp : 1;

-						unsigned char zClamp : 1;

-						unsigned char wClamp : 1;

-					};

-				};

-			};

-

-			Input input[VERTEX_ATTRIBUTES];

-			Output output[12];

-		};

-

-		struct State : States

-		{

-			State();

-

-			bool operator==(const State &state) const;

-

-			unsigned int hash;

-		};

-

-		struct FixedFunction

-		{

-			float4 transformT[12][4];

-			float4 cameraTransformT[12][4];

-			float4 normalTransformT[12][4];

-			float4 textureTransform[8][4];

-

-			float4 lightPosition[8];

-			float4 lightAmbient[8];

-			float4 lightSpecular[8];

-			float4 lightDiffuse[8];

-			float4 attenuationConstant[8];

-			float4 attenuationLinear[8];

-			float4 attenuationQuadratic[8];

-			float lightRange[8];

-			float4 materialDiffuse;

-			float4 materialSpecular;

-			float materialShininess;

-			float4 globalAmbient;

-			float4 materialEmission;

-			float4 materialAmbient;

-		};

-

-		struct PointSprite

-		{

-			float4 pointSize;

-			float pointSizeMin;

-			float pointSizeMax;

-			float pointScaleA;

-			float pointScaleB;

-			float pointScaleC;

-		};

-

-		typedef void (*RoutinePointer)(Vertex *output, unsigned int *batch, VertexTask *vertexTask, DrawData *draw);

-

-		VertexProcessor(Context *context);

-

-		virtual ~VertexProcessor();

-

-		virtual void setInputStream(int index, const Stream &stream);

-		virtual void resetInputStreams(bool preTransformed);

-

-		virtual void setFloatConstant(unsigned int index, const float value[4]);

-		virtual void setIntegerConstant(unsigned int index, const int integer[4]);

-		virtual void setBooleanConstant(unsigned int index, int boolean);

-

-		virtual void setUniformBuffer(int index, sw::Resource* uniformBuffer, int offset);

-		virtual void lockUniformBuffers(byte** u, sw::Resource* uniformBuffers[]);

-

-		virtual void setTransformFeedbackBuffer(int index, sw::Resource* transformFeedbackBuffer, int offset, unsigned int reg, unsigned int row, unsigned int col, size_t stride);

-		virtual void lockTransformFeedbackBuffers(byte** t, unsigned int* v, unsigned int* r, unsigned int* c, unsigned int* s, sw::Resource* transformFeedbackBuffers[]);

-

-		// Transformations

-		virtual void setModelMatrix(const Matrix &M, int i = 0);

-		virtual void setViewMatrix(const Matrix &V);

-		virtual void setBaseMatrix(const Matrix &B);

-		virtual void setProjectionMatrix(const Matrix &P);

-

-		// Lighting

-		virtual void setLightingEnable(bool lightingEnable);

-		virtual void setLightEnable(unsigned int light, bool lightEnable);

-		virtual void setSpecularEnable(bool specularEnable);

-

-		virtual void setGlobalAmbient(const Color<float> &globalAmbient);

-		virtual void setLightPosition(unsigned int light, const Point &lightPosition);

-		virtual void setLightViewPosition(unsigned int light, const Point &lightPosition);

-		virtual void setLightDiffuse(unsigned int light, const Color<float> &lightDiffuse);

-		virtual void setLightSpecular(unsigned int light, const Color<float> &lightSpecular);

-		virtual void setLightAmbient(unsigned int light, const Color<float> &lightAmbient);

-		virtual void setLightAttenuation(unsigned int light, float constant, float linear, float quadratic);

-		virtual void setLightRange(unsigned int light, float lightRange);

-

-		virtual void setInstanceID(int instanceID);

-

-		virtual void setFogEnable(bool fogEnable);

-		virtual void setVertexFogMode(FogMode fogMode);

-		virtual void setRangeFogEnable(bool enable);

-

-		virtual void setColorVertexEnable(bool colorVertexEnable);

-		virtual void setDiffuseMaterialSource(MaterialSource diffuseMaterialSource);

-		virtual void setSpecularMaterialSource(MaterialSource specularMaterialSource);

-		virtual void setAmbientMaterialSource(MaterialSource ambientMaterialSource);

-		virtual void setEmissiveMaterialSource(MaterialSource emissiveMaterialSource);

-

-		virtual void setMaterialEmission(const Color<float> &emission);

-		virtual void setMaterialAmbient(const Color<float> &materialAmbient);

-		virtual void setMaterialDiffuse(const Color<float> &diffuseColor);

-		virtual void setMaterialSpecular(const Color<float> &specularColor);

-		virtual void setMaterialShininess(float specularPower);

-

-		virtual void setIndexedVertexBlendEnable(bool indexedVertexBlendEnable);

-		virtual void setVertexBlendMatrixCount(unsigned int vertexBlendMatrixCount);

-

-		virtual void setTextureWrap(unsigned int stage, int mask);

-		virtual void setTexGen(unsigned int stage, TexGen texGen);

-		virtual void setLocalViewer(bool localViewer);

-		virtual void setNormalizeNormals(bool normalizeNormals);

-		virtual void setTextureMatrix(int stage, const Matrix &T);

-		virtual void setTextureTransform(int stage, int count, bool project);

-

-		virtual void setTextureFilter(unsigned int sampler, FilterType textureFilter);

-		virtual void setMipmapFilter(unsigned int sampler, MipmapType mipmapFilter);

-		virtual void setGatherEnable(unsigned int sampler, bool enable);

-		virtual void setAddressingModeU(unsigned int sampler, AddressingMode addressingMode);

-		virtual void setAddressingModeV(unsigned int sampler, AddressingMode addressingMode);

-		virtual void setAddressingModeW(unsigned int sampler, AddressingMode addressingMode);

-		virtual void setReadSRGB(unsigned int sampler, bool sRGB);

-		virtual void setMipmapLOD(unsigned int sampler, float bias);

-		virtual void setBorderColor(unsigned int sampler, const Color<float> &borderColor);

-		virtual void setMaxAnisotropy(unsigned int stage, float maxAnisotropy);

-		virtual void setSwizzleR(unsigned int sampler, SwizzleType swizzleR);

-		virtual void setSwizzleG(unsigned int sampler, SwizzleType swizzleG);

-		virtual void setSwizzleB(unsigned int sampler, SwizzleType swizzleB);

-		virtual void setSwizzleA(unsigned int sampler, SwizzleType swizzleA);

-

-		virtual void setPointSize(float pointSize);

-		virtual void setPointSizeMin(float pointSizeMin);

-		virtual void setPointSizeMax(float pointSizeMax);

-		virtual void setPointScaleA(float pointScaleA);

-		virtual void setPointScaleB(float pointScaleB);

-		virtual void setPointScaleC(float pointScaleC);

-

-		virtual void setTransformFeedbackQueryEnabled(bool enable);

-		virtual void enableTransformFeedback(uint64_t enable);

-

-	protected:

-		const Matrix &getModelTransform(int i);

-		const Matrix &getViewTransform();

-

-		const State update();

-		Routine *routine(const State &state);

-

-		bool isFixedFunction();

-		void setRoutineCacheSize(int cacheSize);

-

-		// Shader constants

-		float4 c[VERTEX_UNIFORM_VECTORS + 1];   // One extra for indices out of range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}

-		int4 i[16];

-		bool b[16];

-

-		PointSprite point;

-		FixedFunction ff;

-

-	private:

-		struct UniformBufferInfo

-		{

-			UniformBufferInfo();

-

-			Resource* buffer;

-			int offset;

-		};

-		UniformBufferInfo uniformBufferInfo[MAX_UNIFORM_BUFFER_BINDINGS];

-

-		struct TransformFeedbackInfo

-		{

-			TransformFeedbackInfo();

-

-			Resource* buffer;

-			int offset;

-			int reg;

-			int row;

-			int col;

-			size_t stride;

-		};

-		TransformFeedbackInfo transformFeedbackInfo[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS];

-

-		void updateTransform();

-		void setTransform(const Matrix &M, int i);

-		void setCameraTransform(const Matrix &M, int i);

-		void setNormalTransform(const Matrix &M, int i);

-

-		Context *const context;

-

-		RoutineCache<State> *routineCache;

-

-	protected:

-		Matrix M[12];      // Model/Geometry/World matrix

-		Matrix V;          // View/Camera/Eye matrix

-		Matrix B;          // Base matrix

-		Matrix P;          // Projection matrix

-		Matrix PB;         // P * B

-		Matrix PBV;        // P * B * V

-		Matrix PBVM[12];   // P * B * V * M

-

-		// Update hierarchy

-		bool updateMatrix;

-		bool updateModelMatrix[12];

-		bool updateViewMatrix;

-		bool updateBaseMatrix;

-		bool updateProjectionMatrix;

-		bool updateLighting;

-	};

-}

-

-#endif   // sw_VertexProcessor_hpp

+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef sw_VertexProcessor_hpp
+#define sw_VertexProcessor_hpp
+
+#include "Matrix.hpp"
+#include "Context.hpp"
+#include "RoutineCache.hpp"
+
+namespace sw
+{
+	struct DrawData;
+
+	struct VertexCache   // FIXME: Variable size
+	{
+		void clear();
+
+		Vertex vertex[16][4];
+		unsigned int tag[16];
+
+		int drawCall;
+	};
+
+	struct VertexTask
+	{
+		unsigned int vertexStart;
+		unsigned int vertexCount;
+		unsigned int verticesPerPrimitive;
+		VertexCache vertexCache;
+	};
+
+	class VertexProcessor
+	{
+	public:
+		struct States
+		{
+			unsigned int computeHash();
+
+			uint64_t shaderID;
+
+			bool fixedFunction             : 1;
+			bool textureSampling           : 1;
+			unsigned int positionRegister  : 4;
+			unsigned int pointSizeRegister : 4;   // 0xF signifies no vertex point size
+
+			unsigned int vertexBlendMatrixCount               : 3;
+			bool indexedVertexBlendEnable                     : 1;
+			bool vertexNormalActive                           : 1;
+			bool normalizeNormals                             : 1;
+			bool vertexLightingActive                         : 1;
+			bool diffuseActive                                : 1;
+			bool specularActive                               : 1;
+			bool vertexSpecularActive                         : 1;
+			unsigned int vertexLightActive                    : 8;
+			MaterialSource vertexDiffuseMaterialSourceActive  : BITS(MATERIAL_LAST);
+			MaterialSource vertexSpecularMaterialSourceActive : BITS(MATERIAL_LAST);
+			MaterialSource vertexAmbientMaterialSourceActive  : BITS(MATERIAL_LAST);
+			MaterialSource vertexEmissiveMaterialSourceActive : BITS(MATERIAL_LAST);
+			bool fogActive                                    : 1;
+			FogMode vertexFogMode                             : BITS(FOG_LAST);
+			bool rangeFogActive                               : 1;
+			bool localViewerActive                            : 1;
+			bool pointSizeActive                              : 1;
+			bool pointScaleActive                             : 1;
+			bool transformFeedbackQueryEnabled                : 1;
+			uint64_t transformFeedbackEnabled                 : 64;
+
+			bool preTransformed : 1;
+			bool superSampling  : 1;
+			bool multiSampling  : 1;
+
+			struct TextureState
+			{
+				TexGen texGenActive                       : BITS(TEXGEN_LAST);
+				unsigned char textureTransformCountActive : 3;
+				unsigned char texCoordIndexActive         : 3;
+			};
+
+			TextureState textureState[8];
+
+			Sampler::State samplerState[VERTEX_TEXTURE_IMAGE_UNITS];
+
+			struct Input
+			{
+				operator bool() const   // Returns true if stream contains data
+				{
+					return count != 0;
+				}
+
+				StreamType type    : BITS(STREAMTYPE_LAST);
+				unsigned int count : 3;
+				bool normalized    : 1;
+			};
+
+			struct Output
+			{
+				union
+				{
+					unsigned char write : 4;
+
+					struct
+					{
+						unsigned char xWrite : 1;
+						unsigned char yWrite : 1;
+						unsigned char zWrite : 1;
+						unsigned char wWrite : 1;
+					};
+				};
+
+				union
+				{
+					unsigned char clamp : 4;
+
+					struct
+					{
+						unsigned char xClamp : 1;
+						unsigned char yClamp : 1;
+						unsigned char zClamp : 1;
+						unsigned char wClamp : 1;
+					};
+				};
+			};
+
+			Input input[VERTEX_ATTRIBUTES];
+			Output output[12];
+		};
+
+		struct State : States
+		{
+			State();
+
+			bool operator==(const State &state) const;
+
+			unsigned int hash;
+		};
+
+		struct FixedFunction
+		{
+			float4 transformT[12][4];
+			float4 cameraTransformT[12][4];
+			float4 normalTransformT[12][4];
+			float4 textureTransform[8][4];
+
+			float4 lightPosition[8];
+			float4 lightAmbient[8];
+			float4 lightSpecular[8];
+			float4 lightDiffuse[8];
+			float4 attenuationConstant[8];
+			float4 attenuationLinear[8];
+			float4 attenuationQuadratic[8];
+			float lightRange[8];
+			float4 materialDiffuse;
+			float4 materialSpecular;
+			float materialShininess;
+			float4 globalAmbient;
+			float4 materialEmission;
+			float4 materialAmbient;
+		};
+
+		struct PointSprite
+		{
+			float4 pointSize;
+			float pointSizeMin;
+			float pointSizeMax;
+			float pointScaleA;
+			float pointScaleB;
+			float pointScaleC;
+		};
+
+		typedef void (*RoutinePointer)(Vertex *output, unsigned int *batch, VertexTask *vertexTask, DrawData *draw);
+
+		VertexProcessor(Context *context);
+
+		virtual ~VertexProcessor();
+
+		virtual void setInputStream(int index, const Stream &stream);
+		virtual void resetInputStreams(bool preTransformed);
+
+		virtual void setFloatConstant(unsigned int index, const float value[4]);
+		virtual void setIntegerConstant(unsigned int index, const int integer[4]);
+		virtual void setBooleanConstant(unsigned int index, int boolean);
+
+		virtual void setUniformBuffer(int index, sw::Resource* uniformBuffer, int offset);
+		virtual void lockUniformBuffers(byte** u, sw::Resource* uniformBuffers[]);
+
+		virtual void setTransformFeedbackBuffer(int index, sw::Resource* transformFeedbackBuffer, int offset, unsigned int reg, unsigned int row, unsigned int col, size_t stride);
+		virtual void lockTransformFeedbackBuffers(byte** t, unsigned int* v, unsigned int* r, unsigned int* c, unsigned int* s, sw::Resource* transformFeedbackBuffers[]);
+
+		// Transformations
+		virtual void setModelMatrix(const Matrix &M, int i = 0);
+		virtual void setViewMatrix(const Matrix &V);
+		virtual void setBaseMatrix(const Matrix &B);
+		virtual void setProjectionMatrix(const Matrix &P);
+
+		// Lighting
+		virtual void setLightingEnable(bool lightingEnable);
+		virtual void setLightEnable(unsigned int light, bool lightEnable);
+		virtual void setSpecularEnable(bool specularEnable);
+
+		virtual void setGlobalAmbient(const Color<float> &globalAmbient);
+		virtual void setLightPosition(unsigned int light, const Point &lightPosition);
+		virtual void setLightViewPosition(unsigned int light, const Point &lightPosition);
+		virtual void setLightDiffuse(unsigned int light, const Color<float> &lightDiffuse);
+		virtual void setLightSpecular(unsigned int light, const Color<float> &lightSpecular);
+		virtual void setLightAmbient(unsigned int light, const Color<float> &lightAmbient);
+		virtual void setLightAttenuation(unsigned int light, float constant, float linear, float quadratic);
+		virtual void setLightRange(unsigned int light, float lightRange);
+
+		virtual void setInstanceID(int instanceID);
+
+		virtual void setFogEnable(bool fogEnable);
+		virtual void setVertexFogMode(FogMode fogMode);
+		virtual void setRangeFogEnable(bool enable);
+
+		virtual void setColorVertexEnable(bool colorVertexEnable);
+		virtual void setDiffuseMaterialSource(MaterialSource diffuseMaterialSource);
+		virtual void setSpecularMaterialSource(MaterialSource specularMaterialSource);
+		virtual void setAmbientMaterialSource(MaterialSource ambientMaterialSource);
+		virtual void setEmissiveMaterialSource(MaterialSource emissiveMaterialSource);
+
+		virtual void setMaterialEmission(const Color<float> &emission);
+		virtual void setMaterialAmbient(const Color<float> &materialAmbient);
+		virtual void setMaterialDiffuse(const Color<float> &diffuseColor);
+		virtual void setMaterialSpecular(const Color<float> &specularColor);
+		virtual void setMaterialShininess(float specularPower);
+
+		virtual void setIndexedVertexBlendEnable(bool indexedVertexBlendEnable);
+		virtual void setVertexBlendMatrixCount(unsigned int vertexBlendMatrixCount);
+
+		virtual void setTextureWrap(unsigned int stage, int mask);
+		virtual void setTexGen(unsigned int stage, TexGen texGen);
+		virtual void setLocalViewer(bool localViewer);
+		virtual void setNormalizeNormals(bool normalizeNormals);
+		virtual void setTextureMatrix(int stage, const Matrix &T);
+		virtual void setTextureTransform(int stage, int count, bool project);
+
+		virtual void setTextureFilter(unsigned int sampler, FilterType textureFilter);
+		virtual void setMipmapFilter(unsigned int sampler, MipmapType mipmapFilter);
+		virtual void setGatherEnable(unsigned int sampler, bool enable);
+		virtual void setAddressingModeU(unsigned int sampler, AddressingMode addressingMode);
+		virtual void setAddressingModeV(unsigned int sampler, AddressingMode addressingMode);
+		virtual void setAddressingModeW(unsigned int sampler, AddressingMode addressingMode);
+		virtual void setReadSRGB(unsigned int sampler, bool sRGB);
+		virtual void setMipmapLOD(unsigned int sampler, float bias);
+		virtual void setBorderColor(unsigned int sampler, const Color<float> &borderColor);
+		virtual void setMaxAnisotropy(unsigned int stage, float maxAnisotropy);
+		virtual void setSwizzleR(unsigned int sampler, SwizzleType swizzleR);
+		virtual void setSwizzleG(unsigned int sampler, SwizzleType swizzleG);
+		virtual void setSwizzleB(unsigned int sampler, SwizzleType swizzleB);
+		virtual void setSwizzleA(unsigned int sampler, SwizzleType swizzleA);
+
+		virtual void setPointSize(float pointSize);
+		virtual void setPointSizeMin(float pointSizeMin);
+		virtual void setPointSizeMax(float pointSizeMax);
+		virtual void setPointScaleA(float pointScaleA);
+		virtual void setPointScaleB(float pointScaleB);
+		virtual void setPointScaleC(float pointScaleC);
+
+		virtual void setTransformFeedbackQueryEnabled(bool enable);
+		virtual void enableTransformFeedback(uint64_t enable);
+
+	protected:
+		const Matrix &getModelTransform(int i);
+		const Matrix &getViewTransform();
+
+		const State update();
+		Routine *routine(const State &state);
+
+		bool isFixedFunction();
+		void setRoutineCacheSize(int cacheSize);
+
+		// Shader constants
+		float4 c[VERTEX_UNIFORM_VECTORS + 1];   // One extra for indices out of range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}
+		int4 i[16];
+		bool b[16];
+
+		PointSprite point;
+		FixedFunction ff;
+
+	private:
+		struct UniformBufferInfo
+		{
+			UniformBufferInfo();
+
+			Resource* buffer;
+			int offset;
+		};
+		UniformBufferInfo uniformBufferInfo[MAX_UNIFORM_BUFFER_BINDINGS];
+
+		struct TransformFeedbackInfo
+		{
+			TransformFeedbackInfo();
+
+			Resource* buffer;
+			int offset;
+			int reg;
+			int row;
+			int col;
+			size_t stride;
+		};
+		TransformFeedbackInfo transformFeedbackInfo[MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS];
+
+		void updateTransform();
+		void setTransform(const Matrix &M, int i);
+		void setCameraTransform(const Matrix &M, int i);
+		void setNormalTransform(const Matrix &M, int i);
+
+		Context *const context;
+
+		RoutineCache<State> *routineCache;
+
+	protected:
+		Matrix M[12];      // Model/Geometry/World matrix
+		Matrix V;          // View/Camera/Eye matrix
+		Matrix B;          // Base matrix
+		Matrix P;          // Projection matrix
+		Matrix PB;         // P * B
+		Matrix PBV;        // P * B * V
+		Matrix PBVM[12];   // P * B * V * M
+
+		// Update hierarchy
+		bool updateMatrix;
+		bool updateModelMatrix[12];
+		bool updateViewMatrix;
+		bool updateBaseMatrix;
+		bool updateProjectionMatrix;
+		bool updateLighting;
+	};
+}
+
+#endif   // sw_VertexProcessor_hpp