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/Common/CPUID.cpp b/src/Common/CPUID.cpp
index e2879e2..23d76d6 100644
--- a/src/Common/CPUID.cpp
+++ b/src/Common/CPUID.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 "CPUID.hpp"
 
@@ -164,7 +167,7 @@
 		#if defined(_WIN32)
 			__cpuid(registers, info);
 		#else
-            __asm volatile("cpuid": "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]): "a" (info));
+			__asm volatile("cpuid": "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]): "a" (info));
 		#endif
 	}
 
diff --git a/src/Common/CPUID.hpp b/src/Common/CPUID.hpp
index 43c9e69..73c8e94 100644
--- a/src/Common/CPUID.hpp
+++ b/src/Common/CPUID.hpp
@@ -1,130 +1,133 @@
-// 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_CPUID_hpp

-#define sw_CPUID_hpp

-

-namespace sw

-{

-	#if !defined(__x86_64__) && (defined(_M_AMD64) || defined (_M_X64))

-		#define __x86_64__ 1

-	#endif

-

-	class CPUID

-	{

-	public:

-		static bool supportsMMX();

-		static bool supportsCMOV();

-		static bool supportsMMX2();   // MMX instructions added by SSE: pshufw, pmulhuw, pmovmskb, pavgw/b, pextrw, pinsrw, pmaxsw/ub, etc.

-		static bool supportsSSE();

-		static bool supportsSSE2();

-		static bool supportsSSE3();

-		static bool supportsSSSE3();

-		static bool supportsSSE4_1();

-		static int coreCount();

-		static int processAffinity();

-

-		static void setEnableMMX(bool enable);

-		static void setEnableCMOV(bool enable);

-		static void setEnableSSE(bool enable);

-		static void setEnableSSE2(bool enable);

-		static void setEnableSSE3(bool enable);

-		static void setEnableSSSE3(bool enable);

-		static void setEnableSSE4_1(bool enable);

-

-		static void setFlushToZero(bool enable);        // Denormal results are written as zero

-		static void setDenormalsAreZero(bool enable);   // Denormal inputs are read as zero

-

-	private:

-		static bool MMX;

-		static bool CMOV;

-		static bool SSE;

-		static bool SSE2;

-		static bool SSE3;

-		static bool SSSE3;

-		static bool SSE4_1;

-		static int cores;

-		static int affinity;

-

-		static bool enableMMX;

-		static bool enableCMOV;

-		static bool enableSSE;

-		static bool enableSSE2;

-		static bool enableSSE3;

-		static bool enableSSSE3;

-		static bool enableSSE4_1;

-

-		static bool detectMMX();

-		static bool detectCMOV();

-		static bool detectSSE();

-		static bool detectSSE2();

-		static bool detectSSE3();

-		static bool detectSSSE3();

-		static bool detectSSE4_1();

-		static int detectCoreCount();

-		static int detectAffinity();

-	};

-}

-

-namespace sw

-{

-	inline bool CPUID::supportsMMX()

-	{

-		return MMX && enableMMX;

-	}

-

-	inline bool CPUID::supportsCMOV()

-	{

-		return CMOV && enableCMOV;

-	}

-

-	inline bool CPUID::supportsMMX2()

-	{

-		return supportsSSE();   // Coincides with 64-bit integer vector instructions supported by SSE

-	}

-

-	inline bool CPUID::supportsSSE()

-	{

-		return SSE && enableSSE;

-	}

-

-	inline bool CPUID::supportsSSE2()

-	{

-		return SSE2 && enableSSE2;

-	}

-

-	inline bool CPUID::supportsSSE3()

-	{

-		return SSE3 && enableSSE3;

-	}

-

-	inline bool CPUID::supportsSSSE3()

-	{

-		return SSSE3 && enableSSSE3;

-	}

-

-	inline bool CPUID::supportsSSE4_1()

-	{

-		return SSE4_1 && enableSSE4_1;

-	}

-

-	inline int CPUID::coreCount()

-	{

-		return cores;

-	}

-

-	inline int CPUID::processAffinity()

-	{

-		return affinity;

-	}

-}

-

-#endif   // sw_CPUID_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_CPUID_hpp
+#define sw_CPUID_hpp
+
+namespace sw
+{
+	#if !defined(__x86_64__) && (defined(_M_AMD64) || defined (_M_X64))
+		#define __x86_64__ 1
+	#endif
+
+	class CPUID
+	{
+	public:
+		static bool supportsMMX();
+		static bool supportsCMOV();
+		static bool supportsMMX2();   // MMX instructions added by SSE: pshufw, pmulhuw, pmovmskb, pavgw/b, pextrw, pinsrw, pmaxsw/ub, etc.
+		static bool supportsSSE();
+		static bool supportsSSE2();
+		static bool supportsSSE3();
+		static bool supportsSSSE3();
+		static bool supportsSSE4_1();
+		static int coreCount();
+		static int processAffinity();
+
+		static void setEnableMMX(bool enable);
+		static void setEnableCMOV(bool enable);
+		static void setEnableSSE(bool enable);
+		static void setEnableSSE2(bool enable);
+		static void setEnableSSE3(bool enable);
+		static void setEnableSSSE3(bool enable);
+		static void setEnableSSE4_1(bool enable);
+
+		static void setFlushToZero(bool enable);        // Denormal results are written as zero
+		static void setDenormalsAreZero(bool enable);   // Denormal inputs are read as zero
+
+	private:
+		static bool MMX;
+		static bool CMOV;
+		static bool SSE;
+		static bool SSE2;
+		static bool SSE3;
+		static bool SSSE3;
+		static bool SSE4_1;
+		static int cores;
+		static int affinity;
+
+		static bool enableMMX;
+		static bool enableCMOV;
+		static bool enableSSE;
+		static bool enableSSE2;
+		static bool enableSSE3;
+		static bool enableSSSE3;
+		static bool enableSSE4_1;
+
+		static bool detectMMX();
+		static bool detectCMOV();
+		static bool detectSSE();
+		static bool detectSSE2();
+		static bool detectSSE3();
+		static bool detectSSSE3();
+		static bool detectSSE4_1();
+		static int detectCoreCount();
+		static int detectAffinity();
+	};
+}
+
+namespace sw
+{
+	inline bool CPUID::supportsMMX()
+	{
+		return MMX && enableMMX;
+	}
+
+	inline bool CPUID::supportsCMOV()
+	{
+		return CMOV && enableCMOV;
+	}
+
+	inline bool CPUID::supportsMMX2()
+	{
+		return supportsSSE();   // Coincides with 64-bit integer vector instructions supported by SSE
+	}
+
+	inline bool CPUID::supportsSSE()
+	{
+		return SSE && enableSSE;
+	}
+
+	inline bool CPUID::supportsSSE2()
+	{
+		return SSE2 && enableSSE2;
+	}
+
+	inline bool CPUID::supportsSSE3()
+	{
+		return SSE3 && enableSSE3;
+	}
+
+	inline bool CPUID::supportsSSSE3()
+	{
+		return SSSE3 && enableSSSE3;
+	}
+
+	inline bool CPUID::supportsSSE4_1()
+	{
+		return SSE4_1 && enableSSE4_1;
+	}
+
+	inline int CPUID::coreCount()
+	{
+		return cores;
+	}
+
+	inline int CPUID::processAffinity()
+	{
+		return affinity;
+	}
+}
+
+#endif   // sw_CPUID_hpp
diff --git a/src/Common/Configurator.cpp b/src/Common/Configurator.cpp
index 2a8ad16..994d105 100644
--- a/src/Common/Configurator.cpp
+++ b/src/Common/Configurator.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 "Configurator.hpp"
 
@@ -189,7 +192,7 @@
 		}
 
 		int valueID = findValue(keyID, valueName);
-		
+
 		if(valueID == -1)
 		{
 			sections[keyID].names.resize(sections[keyID].names.size() + 1, valueName);
@@ -217,7 +220,7 @@
 
 		sprintf(svalue, "%d", defaultValue);
 
-		return atoi(getValue(keyName, valueName, svalue).c_str()); 
+		return atoi(getValue(keyName, valueName, svalue).c_str());
 	}
 
 	bool Configurator::getBoolean(string keyName, string valueName, bool defaultValue) const
@@ -231,7 +234,7 @@
 
 		sprintf(svalue, "%f", defaultValue);
 
-		return atof(getValue(keyName, valueName, svalue).c_str()); 
+		return atof(getValue(keyName, valueName, svalue).c_str());
 	}
 
 	unsigned int Configurator::getFormatted(string keyName, string valueName, char *format,
diff --git a/src/Common/Configurator.hpp b/src/Common/Configurator.hpp
index a9bf156..6fd930c 100644
--- a/src/Common/Configurator.hpp
+++ b/src/Common/Configurator.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_Configurator_hpp
 #define sw_Configurator_hpp
@@ -19,22 +22,22 @@
 
 namespace sw
 {
-	class Configurator  
+	class Configurator
 	{
 	public:
 		Configurator(std::string iniPath = "");
 
 		~Configurator();
 
-		std::string getValue(std::string sectionName, std::string valueName, std::string defaultValue = "") const; 
+		std::string getValue(std::string sectionName, std::string valueName, std::string defaultValue = "") const;
 		int getInteger(std::string sectionName, std::string valueName, int defaultValue = 0) const;
 		bool getBoolean(std::string sectionName, std::string valueName, bool defaultValue = false) const;
 		double getFloat(std::string sectionName, std::string valueName, double defaultValue = 0.0) const;
 		unsigned int getFormatted(std::string sectionName, std::string valueName, char *format,
-								void *v1 = 0, void *v2 = 0, void *v3 = 0, void *v4 = 0,
-  								void *v5 = 0, void *v6 = 0, void *v7 = 0, void *v8 = 0,
-  								void *v9 = 0, void *v10 = 0, void *v11 = 0, void *v12 = 0,
-  								void *v13 = 0, void *v14 = 0, void *v15 = 0, void *v16 = 0);
+		                          void *v1 = 0, void *v2 = 0, void *v3 = 0, void *v4 = 0,
+		                          void *v5 = 0, void *v6 = 0, void *v7 = 0, void *v8 = 0,
+		                          void *v9 = 0, void *v10 = 0, void *v11 = 0, void *v12 = 0,
+		                          void *v13 = 0, void *v14 = 0, void *v15 = 0, void *v16 = 0);
 
 		void addValue(std::string sectionName, std::string valueName, std::string value);
 
@@ -52,7 +55,7 @@
 		struct Section
 		{
 			std::vector<std::string> names;
-			std::vector<std::string> values; 
+			std::vector<std::string> values;
 		};
 
 		std::vector<Section> sections;
diff --git a/src/Common/Debug.cpp b/src/Common/Debug.cpp
index a122c42..c3a1921 100644
--- a/src/Common/Debug.cpp
+++ b/src/Common/Debug.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 "Debug.hpp"
 
diff --git a/src/Common/Debug.hpp b/src/Common/Debug.hpp
index 569e9a3..0632743 100644
--- a/src/Common/Debug.hpp
+++ b/src/Common/Debug.hpp
@@ -1,46 +1,49 @@
-// 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 Debug_hpp

-#define Debug_hpp

-

-#ifdef __ANDROID__

-#include "DebugAndroid.hpp"

-#else

-

-#include <assert.h>

-#include <stdio.h>

-

-#undef min

-#undef max

-

-void trace(const char *format, ...);

-

-#ifndef NDEBUG

-	#define TRACE(format, ...) trace("[0x%0.8X]%s(" format ")\n", this, __FUNCTION__, ##__VA_ARGS__)

-#else

-	#define TRACE(...) ((void)0)

-#endif

-

-#ifndef NDEBUG

-	#define UNIMPLEMENTED() {trace("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__); ASSERT(false);}

-#else

-	#define UNIMPLEMENTED() ((void)0)

-#endif

-

-#ifndef NDEBUG

-	#define ASSERT(expression) {if(!(expression)) trace("\t! Assert failed in %s(%d): " #expression "\n", __FUNCTION__, __LINE__); assert(expression);}

-#else

-	#define ASSERT assert

-#endif

-

-#endif   // __ANDROID__

-#endif   // Debug_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 Debug_hpp
+#define Debug_hpp
+
+#ifdef __ANDROID__
+#include "DebugAndroid.hpp"
+#else
+
+#include <assert.h>
+#include <stdio.h>
+
+#undef min
+#undef max
+
+void trace(const char *format, ...);
+
+#ifndef NDEBUG
+	#define TRACE(format, ...) trace("[0x%0.8X]%s(" format ")\n", this, __FUNCTION__, ##__VA_ARGS__)
+#else
+	#define TRACE(...) ((void)0)
+#endif
+
+#ifndef NDEBUG
+	#define UNIMPLEMENTED() {trace("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__); ASSERT(false);}
+#else
+	#define UNIMPLEMENTED() ((void)0)
+#endif
+
+#ifndef NDEBUG
+	#define ASSERT(expression) {if(!(expression)) trace("\t! Assert failed in %s(%d): " #expression "\n", __FUNCTION__, __LINE__); assert(expression);}
+#else
+	#define ASSERT assert
+#endif
+
+#endif   // __ANDROID__
+#endif   // Debug_hpp
diff --git a/src/Common/DebugAndroid.cpp b/src/Common/DebugAndroid.cpp
index d8b616e..0d37c0f 100644
--- a/src/Common/DebugAndroid.cpp
+++ b/src/Common/DebugAndroid.cpp
@@ -7,31 +7,33 @@
 
 void AndroidEnterDebugger()
 {
-    ALOGE(__FUNCTION__);
+	ALOGE(__FUNCTION__);
 #ifndef NDEBUG
-    static volatile int * const makefault = nullptr;
-    char value[PROPERTY_VALUE_MAX];
-    property_get("debug.db.uid", value, "-1");
-    int debug_uid = atoi(value);
-    if ((debug_uid >= 0) && (geteuid() < static_cast<uid_t>(debug_uid)))
-    {
-        ALOGE("Waiting for debugger: gdbserver :${PORT} --attach %u. Look for thread %u", getpid(), gettid());
-        volatile int waiting = 1;
-        while (waiting) {
-            sleep(1);
-        }
-    } else {
-        ALOGE("No debugger");
-    }
+	static volatile int * const makefault = nullptr;
+	char value[PROPERTY_VALUE_MAX];
+	property_get("debug.db.uid", value, "-1");
+	int debug_uid = atoi(value);
+	if((debug_uid >= 0) && (geteuid() < static_cast<uid_t>(debug_uid)))
+	{
+		ALOGE("Waiting for debugger: gdbserver :${PORT} --attach %u. Look for thread %u", getpid(), gettid());
+		volatile int waiting = 1;
+		while (waiting) {
+			sleep(1);
+		}
+	}
+	else
+	{
+		ALOGE("No debugger");
+	}
 #endif
 }
 
 void trace(const char *format, ...)
 {
 #ifndef NDEBUG
-    va_list vararg;
-    va_start(vararg, format);
-    android_vprintLog(ANDROID_LOG_VERBOSE, NULL, LOG_TAG, format, vararg);
-    va_end(vararg);
+	va_list vararg;
+	va_start(vararg, format);
+	android_vprintLog(ANDROID_LOG_VERBOSE, NULL, LOG_TAG, format, vararg);
+	va_end(vararg);
 #endif
 }
diff --git a/src/Common/GrallocAndroid.cpp b/src/Common/GrallocAndroid.cpp
index f96d62a..ae5fc09 100644
--- a/src/Common/GrallocAndroid.cpp
+++ b/src/Common/GrallocAndroid.cpp
@@ -1,22 +1,36 @@
+// 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 "GrallocAndroid.hpp"
 
 #include <cutils/log.h>
 
 GrallocModule *GrallocModule::getInstance()
 {
-    static GrallocModule instance;
-    return &instance;
+	static GrallocModule instance;
+	return &instance;
 }
 
 GrallocModule::GrallocModule()
 {
-    const hw_module_t *module = nullptr;
-    hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
+	const hw_module_t *module = nullptr;
+	hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
 
-    if(!module)
-    {
-        ALOGE("Failed to load standard gralloc");
-    }
+	if(!module)
+	{
+		ALOGE("Failed to load standard gralloc");
+	}
 
-    m_module = reinterpret_cast<const gralloc_module_t*>(module);
+	m_module = reinterpret_cast<const gralloc_module_t*>(module);
 }
diff --git a/src/Common/GrallocAndroid.hpp b/src/Common/GrallocAndroid.hpp
index 805850d..ffc8fc6 100644
--- a/src/Common/GrallocAndroid.hpp
+++ b/src/Common/GrallocAndroid.hpp
@@ -6,20 +6,20 @@
 class GrallocModule
 {
 public:
-    static GrallocModule *getInstance();
-    int lock(buffer_handle_t handle, int usage, int left, int top, int width, int height, void **vaddr)
+	static GrallocModule *getInstance();
+	int lock(buffer_handle_t handle, int usage, int left, int top, int width, int height, void **vaddr)
 	{
-        return m_module->lock(m_module, handle, usage, left, top, width, height, vaddr);
-    }
+		return m_module->lock(m_module, handle, usage, left, top, width, height, vaddr);
+	}
 
-    int unlock(buffer_handle_t handle)
-    {
-        return m_module->unlock(m_module, handle);
-    }
+	int unlock(buffer_handle_t handle)
+	{
+		return m_module->unlock(m_module, handle);
+	}
 
 private:
-    GrallocModule();
-    const gralloc_module_t *m_module;
+	GrallocModule();
+	const gralloc_module_t *m_module;
 };
 
 #endif  // GRALLOC_ANDROID
diff --git a/src/Common/Half.cpp b/src/Common/Half.cpp
index fea54ac..cde8190 100644
--- a/src/Common/Half.cpp
+++ b/src/Common/Half.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 "Half.hpp"
 
@@ -15,34 +18,34 @@
 {
 	half::half(float fp32)
 	{
-        unsigned int fp32i = *(unsigned int*)&fp32;
-        unsigned int sign = (fp32i & 0x80000000) >> 16;
-        unsigned int abs = fp32i & 0x7FFFFFFF;
+		unsigned int fp32i = *(unsigned int*)&fp32;
+		unsigned int sign = (fp32i & 0x80000000) >> 16;
+		unsigned int abs = fp32i & 0x7FFFFFFF;
 
-        if(abs > 0x47FFEFFF)   // Infinity
-        {
-            fp16i = sign | 0x7FFF;
-        }
-        else if(abs < 0x38800000)   // Denormal
-        {
-            unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000;   
-            int e = 113 - (abs >> 23);
+		if(abs > 0x47FFEFFF)   // Infinity
+		{
+			fp16i = sign | 0x7FFF;
+		}
+		else if(abs < 0x38800000)   // Denormal
+		{
+			unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000;
+			int e = 113 - (abs >> 23);
 
-            if(e < 24)
-            {
-                abs = mantissa >> e;
-            }
-            else
-            {
-                abs = 0;
-            }
+			if(e < 24)
+			{
+				abs = mantissa >> e;
+			}
+			else
+			{
+				abs = 0;
+			}
 
-            fp16i = sign | (abs + 0x00000FFF + ((abs >> 13) & 1)) >> 13;
-        }
-        else
-        {
-            fp16i = sign | (abs + 0xC8000000 + 0x00000FFF + ((abs >> 13) & 1)) >> 13;
-        }
+			fp16i = sign | (abs + 0x00000FFF + ((abs >> 13) & 1)) >> 13;
+		}
+		else
+		{
+			fp16i = sign | (abs + 0xC8000000 + 0x00000FFF + ((abs >> 13) & 1)) >> 13;
+		}
 	}
 
 	half::operator float() const
@@ -58,7 +61,7 @@
 			if(m == 0)
 			{
 				fp32i = s << 31;
-			
+
 				return (float&)fp32i;
 			}
 			else
@@ -85,7 +88,7 @@
 	half &half::operator=(half h)
 	{
 		fp16i = h.fp16i;
-		
+
 		return *this;
 	}
 
diff --git a/src/Common/Half.hpp b/src/Common/Half.hpp
index fc412c9..f62ea27 100644
--- a/src/Common/Half.hpp
+++ b/src/Common/Half.hpp
@@ -1,32 +1,35 @@
-// 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_Half_hpp

-#define sw_Half_hpp

-

-namespace sw

-{

-	class half

-	{

-	public:

-		explicit half(float f);

-

-		operator float() const;

-

-		half &operator=(half h);

-		half &operator=(float f);

-

-	private:

-		unsigned short fp16i;

-	};

-}

-

-#endif   // sw_Half_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_Half_hpp
+#define sw_Half_hpp
+
+namespace sw
+{
+	class half
+	{
+	public:
+		explicit half(float f);
+
+		operator float() const;
+
+		half &operator=(half h);
+		half &operator=(float f);
+
+	private:
+		unsigned short fp16i;
+	};
+}
+
+#endif   // sw_Half_hpp
diff --git a/src/Common/Math.cpp b/src/Common/Math.cpp
index e8ae3d2..fcf7eca 100644
--- a/src/Common/Math.cpp
+++ b/src/Common/Math.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 "Math.hpp"
 
diff --git a/src/Common/Math.hpp b/src/Common/Math.hpp
index 9e51850..0b4abe6 100644
--- a/src/Common/Math.hpp
+++ b/src/Common/Math.hpp
@@ -1,477 +1,480 @@
-// 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_Math_hpp

-#define sw_Math_hpp

-

-#include "Types.hpp"

-

-#include <cmath>

-#if defined(_MSC_VER)

-	#include <intrin.h>

-#endif

-

-namespace sw

-{

-	using std::abs;

-

-	#undef min

-	#undef max

-

-	template<class T>

-	inline T max(T a, T b)

-	{

-		return a > b ? a : b;

-	}

-

-	template<class T>

-	inline T min(T a, T b)

-	{

-		return a < b ? a : b;

-	}

-

-	template<class T>

-	inline T max(T a, T b, T c)

-	{

-		return max(max(a, b), c);

-	}

-

-	template<class T>

-	inline T min(T a, T b, T c)

-	{

-		return min(min(a, b), c);

-	}

-

-	template<class T>

-	inline T max(T a, T b, T c, T d)

-	{

-		return max(max(a, b), max(c, d));

-	}

-

-	template<class T>

-	inline T min(T a, T b, T c, T d)

-	{

-		return min(min(a, b), min(c, d));

-	}

-

-	template<class T>

-	inline void swap(T &a, T &b)

-	{

-		T t = a;

-		a = b;

-		b = t;

-	}

-

-	inline int iround(float x)

-	{

-		return (int)floor(x + 0.5f);

-	//	return _mm_cvtss_si32(_mm_load_ss(&x));   // FIXME: Demands SSE support

-	}

-

-	inline int ifloor(float x)

-	{

-		return (int)floor(x);

-	}

-

-	inline int ceilFix4(int x)

-	{

-		return (x + 0xF) & 0xFFFFFFF0;

-	}

-

-	inline int ceilInt4(int x)

-	{

-		return (x + 0xF) >> 4;

-	}

-

-	#define BITS(x)    ( \

-	!!((x) & 0x80000000) + \

-	!!((x) & 0xC0000000) + \

-	!!((x) & 0xE0000000) + \

-	!!((x) & 0xF0000000) + \

-	!!((x) & 0xF8000000) + \

-	!!((x) & 0xFC000000) + \

-	!!((x) & 0xFE000000) + \

-	!!((x) & 0xFF000000) + \

-	!!((x) & 0xFF800000) + \

-	!!((x) & 0xFFC00000) + \

-	!!((x) & 0xFFE00000) + \

-	!!((x) & 0xFFF00000) + \

-	!!((x) & 0xFFF80000) + \

-	!!((x) & 0xFFFC0000) + \

-	!!((x) & 0xFFFE0000) + \

-	!!((x) & 0xFFFF0000) + \

-	!!((x) & 0xFFFF8000) + \

-	!!((x) & 0xFFFFC000) + \

-	!!((x) & 0xFFFFE000) + \

-	!!((x) & 0xFFFFF000) + \

-	!!((x) & 0xFFFFF800) + \

-	!!((x) & 0xFFFFFC00) + \

-	!!((x) & 0xFFFFFE00) + \

-	!!((x) & 0xFFFFFF00) + \

-	!!((x) & 0xFFFFFF80) + \

-	!!((x) & 0xFFFFFFC0) + \

-	!!((x) & 0xFFFFFFE0) + \

-	!!((x) & 0xFFFFFFF0) + \

-	!!((x) & 0xFFFFFFF8) + \

-	!!((x) & 0xFFFFFFFC) + \

-	!!((x) & 0xFFFFFFFE) + \

-	!!((x) & 0xFFFFFFFF))

-

-	#define MAX(x, y) ((x) > (y) ? (x) : (y))

-	#define MIN(x, y) ((x) < (y) ? (x) : (y))

-

-	inline float exp2(float x)

-	{

-		return exp2f(x);

-	}

-

-	inline int exp2(int x)

-	{

-		return 1 << x;

-	}

-

-	inline unsigned long log2(int x)

-	{

-		#if defined(_MSC_VER)

-			unsigned long y;

-			_BitScanReverse(&y, x);

-			return y;

-		#else

-			return 31 - __builtin_clz(x);

-		#endif

-	}

-

-	inline int ilog2(float x)

-	{

-		unsigned int y = *(unsigned int*)&x;

-

-		return ((y & 0x7F800000) >> 23) - 127;

-	}

-

-	inline float log2(float x)

-	{

-		return logf(x) * 1.44269504f;   // 1.0 / log[e](2)

-	}

-

-	inline bool isPow2(int x)

-	{

-		return (x & -x) == x;

-	}

-

-	template<class T>

-	inline T clamp(T x, T a, T b)

-	{

-		if(x < a) x = a;

-		if(x > b) x = b;

-

-		return x;

-	}

-

-	inline float clamp01(float x)

-	{

-		return clamp(x, 0.0f, 1.0f);

-	}

-

-	inline int ceilPow2(int x)

-	{

-		int i = 1;

-

-		while(i < x)

-		{

-			i <<= 1;

-		}

-

-		return i;

-	}

-

-	inline int floorDiv(int a, int b)

-	{

-		return a / b + ((a % b) >> 31);

-	}

-

-	inline int floorMod(int a, int b)

-	{

-		int r = a % b;

-		return r + ((r >> 31) & b);

-	}

-

-	inline int ceilDiv(int a, int b)

-	{

-		return a / b - (-(a % b) >> 31);

-	}

-

-	inline int ceilMod(int a, int b)

-	{

-		int r = a % b;

-		return r - ((-r >> 31) & b);

-	}

-

-	template<const int n>

-	inline unsigned int unorm(float x)

-	{

-		static const unsigned int max = 0xFFFFFFFF >> (32 - n);

-		static const float maxf = static_cast<float>(max);

-

-		if(x >= 1.0f)

-		{

-			return max;

-		}

-		else if(x <= 0.0f)

-		{

-			return 0;

-		}

-		else

-		{

-			return static_cast<unsigned int>(maxf * x + 0.5f);

-		}

-	}

-

-	template<const int n>

-	inline int snorm(float x)

-	{

-		static const unsigned int min = 0x80000000 >> (32 - n);

-		static const unsigned int max = 0xFFFFFFFF >> (32 - n + 1);

-		static const float maxf = static_cast<float>(max);

-		static const unsigned int range = 0xFFFFFFFF >> (32 - n);

-

-		if(x >= 0.0f)

-		{

-			if(x >= 1.0f)

-			{

-				return max;

-			}

-			else

-			{

-				return static_cast<int>(maxf * x + 0.5f);

-			}

-		}

-		else

-		{

-			if(x <= -1.0f)

-			{

-				return min;

-			}

-			else

-			{

-				return static_cast<int>(maxf * x - 0.5f) & range;

-			}

-		}

-	}

-

-	template<const int n>

-	inline unsigned int ucast(float x)

-	{

-		static const unsigned int max = 0xFFFFFFFF >> (32 - n);

-		static const float maxf = static_cast<float>(max);

-

-		if(x >= maxf)

-		{

-			return max;

-		}

-		else if(x <= 0.0f)

-		{

-			return 0;

-		}

-		else

-		{

-			return static_cast<unsigned int>(x + 0.5f);

-		}

-	}

-

-	template<const int n>

-	inline int scast(float x)

-	{

-		static const unsigned int min = 0x80000000 >> (32 - n);

-		static const unsigned int max = 0xFFFFFFFF >> (32 - n + 1);

-		static const float maxf = static_cast<float>(max);

-		static const unsigned int range = 0xFFFFFFFF >> (32 - n);

-

-		if(x > 0.0f)

-		{

-			if(x >= maxf)

-			{

-				return max;

-			}

-			else

-			{

-				return static_cast<int>(maxf * x + 0.5f);

-			}

-		}

-		else

-		{

-			if(x <= -1.0f)

-			{

-				return min;

-			}

-			else

-			{

-				return static_cast<int>(maxf * x - 0.5f) & range;

-			}

-		}

-	}

-

-	inline float sRGBtoLinear(float c)

-	{

-		if(c <= 0.04045f)

-		{

-			return c * 0.07739938f;   // 1.0f / 12.92f;

-		}

-		else

-		{

-			return powf((c + 0.055f) * 0.9478673f, 2.4f);   // 1.0f / 1.055f

-		}

-	}

-

-	inline float linearToSRGB(float c)

-	{

-		if(c <= 0.0031308f)

-		{

-			return c * 12.92f;

-		}

-		else

-		{

-			return 1.055f * powf(c, 0.4166667f) - 0.055f;   // 1.0f / 2.4f

-		}

-	}

-

-	unsigned char sRGB8toLinear8(unsigned char value);

-

-	uint64_t FNV_1a(const unsigned char *data, int size);   // Fowler-Noll-Vo hash function

-

-	// Round up to the next multiple of alignment

-	inline unsigned int align(unsigned int value, unsigned int alignment)

-	{

-		return ((value + alignment - 1) / alignment) * alignment;

-	}

-

-	inline int clampToSignedInt(unsigned int x)

-	{

-		return static_cast<int>(min(x, 0x7FFFFFFFu));

-	}

-

-	class RGB9E5Data

-	{

-		unsigned int R : 9;

-		unsigned int G : 9;

-		unsigned int B : 9;

-		unsigned int E : 5;

-

-	public:

-		void toRGBFloats(float* rgb) const

-		{

-			static const float Offset = -24.0f; // Exponent Bias (15) + Number of mantissa bits per component (9) = 24

-

-			const float factor = powf(2.0f, static_cast<float>(E) + Offset);

-			rgb[0] = static_cast<float>(R) * factor;

-			rgb[1] = static_cast<float>(G) * factor;

-			rgb[2] = static_cast<float>(B) * factor;

-		}

-	};

-

-	class R11G11B10FData

-	{

-		unsigned int R : 11;

-		unsigned int G : 11;

-		unsigned int B : 10;

-

-		static inline float float11ToFloat32(unsigned short fp11)

-		{

-			unsigned short exponent = (fp11 >> 6) & 0x1F;

-			unsigned short mantissa = fp11 & 0x3F;

-

-			unsigned int output;

-			if(exponent == 0x1F)

-			{

-				// INF or NAN

-				output = 0x7f800000 | (mantissa << 17);

-			}

-			else

-			{

-				if(exponent != 0)

-				{

-					// normalized

-				}

-				else if(mantissa != 0)

-				{

-					// The value is denormalized

-					exponent = 1;

-

-					do

-					{

-						exponent--;

-						mantissa <<= 1;

-					} while((mantissa & 0x40) == 0);

-

-					mantissa = mantissa & 0x3F;

-				}

-				else // The value is zero

-				{

-					exponent = static_cast<unsigned short>(-112);

-				}

-

-				output = ((exponent + 112) << 23) | (mantissa << 17);

-			}

-

-			return *(float*)(&output);

-		}

-

-		static inline float float10ToFloat32(unsigned short fp10)

-		{

-			unsigned short exponent = (fp10 >> 5) & 0x1F;

-			unsigned short mantissa = fp10 & 0x1F;

-

-			unsigned int output;

-			if(exponent == 0x1F)

-			{

-				// INF or NAN

-				output = 0x7f800000 | (mantissa << 17);

-			}

-			else

-			{

-				if(exponent != 0)

-				{

-					// normalized

-				}

-				else if(mantissa != 0)

-				{

-					// The value is denormalized

-					exponent = 1;

-

-					do

-					{

-						exponent--;

-						mantissa <<= 1;

-					} while((mantissa & 0x20) == 0);

-

-					mantissa = mantissa & 0x1F;

-				}

-				else // The value is zero

-				{

-					exponent = static_cast<unsigned short>(-112);

-				}

-

-				output = ((exponent + 112) << 23) | (mantissa << 18);

-			}

-

-			return *(float*)(&output);

-		}

-

-	public:

-		void toRGBFloats(float* rgb) const

-		{

-			rgb[0] = float11ToFloat32(R);

-			rgb[1] = float11ToFloat32(G);

-			rgb[2] = float10ToFloat32(B);

-		}

-	};

-}

-

-#endif   // sw_Math_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_Math_hpp
+#define sw_Math_hpp
+
+#include "Types.hpp"
+
+#include <cmath>
+#if defined(_MSC_VER)
+	#include <intrin.h>
+#endif
+
+namespace sw
+{
+	using std::abs;
+
+	#undef min
+	#undef max
+
+	template<class T>
+	inline T max(T a, T b)
+	{
+		return a > b ? a : b;
+	}
+
+	template<class T>
+	inline T min(T a, T b)
+	{
+		return a < b ? a : b;
+	}
+
+	template<class T>
+	inline T max(T a, T b, T c)
+	{
+		return max(max(a, b), c);
+	}
+
+	template<class T>
+	inline T min(T a, T b, T c)
+	{
+		return min(min(a, b), c);
+	}
+
+	template<class T>
+	inline T max(T a, T b, T c, T d)
+	{
+		return max(max(a, b), max(c, d));
+	}
+
+	template<class T>
+	inline T min(T a, T b, T c, T d)
+	{
+		return min(min(a, b), min(c, d));
+	}
+
+	template<class T>
+	inline void swap(T &a, T &b)
+	{
+		T t = a;
+		a = b;
+		b = t;
+	}
+
+	inline int iround(float x)
+	{
+		return (int)floor(x + 0.5f);
+	//	return _mm_cvtss_si32(_mm_load_ss(&x));   // FIXME: Demands SSE support
+	}
+
+	inline int ifloor(float x)
+	{
+		return (int)floor(x);
+	}
+
+	inline int ceilFix4(int x)
+	{
+		return (x + 0xF) & 0xFFFFFFF0;
+	}
+
+	inline int ceilInt4(int x)
+	{
+		return (x + 0xF) >> 4;
+	}
+
+	#define BITS(x)    ( \
+	!!((x) & 0x80000000) + \
+	!!((x) & 0xC0000000) + \
+	!!((x) & 0xE0000000) + \
+	!!((x) & 0xF0000000) + \
+	!!((x) & 0xF8000000) + \
+	!!((x) & 0xFC000000) + \
+	!!((x) & 0xFE000000) + \
+	!!((x) & 0xFF000000) + \
+	!!((x) & 0xFF800000) + \
+	!!((x) & 0xFFC00000) + \
+	!!((x) & 0xFFE00000) + \
+	!!((x) & 0xFFF00000) + \
+	!!((x) & 0xFFF80000) + \
+	!!((x) & 0xFFFC0000) + \
+	!!((x) & 0xFFFE0000) + \
+	!!((x) & 0xFFFF0000) + \
+	!!((x) & 0xFFFF8000) + \
+	!!((x) & 0xFFFFC000) + \
+	!!((x) & 0xFFFFE000) + \
+	!!((x) & 0xFFFFF000) + \
+	!!((x) & 0xFFFFF800) + \
+	!!((x) & 0xFFFFFC00) + \
+	!!((x) & 0xFFFFFE00) + \
+	!!((x) & 0xFFFFFF00) + \
+	!!((x) & 0xFFFFFF80) + \
+	!!((x) & 0xFFFFFFC0) + \
+	!!((x) & 0xFFFFFFE0) + \
+	!!((x) & 0xFFFFFFF0) + \
+	!!((x) & 0xFFFFFFF8) + \
+	!!((x) & 0xFFFFFFFC) + \
+	!!((x) & 0xFFFFFFFE) + \
+	!!((x) & 0xFFFFFFFF))
+
+	#define MAX(x, y) ((x) > (y) ? (x) : (y))
+	#define MIN(x, y) ((x) < (y) ? (x) : (y))
+
+	inline float exp2(float x)
+	{
+		return exp2f(x);
+	}
+
+	inline int exp2(int x)
+	{
+		return 1 << x;
+	}
+
+	inline unsigned long log2(int x)
+	{
+		#if defined(_MSC_VER)
+			unsigned long y;
+			_BitScanReverse(&y, x);
+			return y;
+		#else
+			return 31 - __builtin_clz(x);
+		#endif
+	}
+
+	inline int ilog2(float x)
+	{
+		unsigned int y = *(unsigned int*)&x;
+
+		return ((y & 0x7F800000) >> 23) - 127;
+	}
+
+	inline float log2(float x)
+	{
+		return logf(x) * 1.44269504f;   // 1.0 / log[e](2)
+	}
+
+	inline bool isPow2(int x)
+	{
+		return (x & -x) == x;
+	}
+
+	template<class T>
+	inline T clamp(T x, T a, T b)
+	{
+		if(x < a) x = a;
+		if(x > b) x = b;
+
+		return x;
+	}
+
+	inline float clamp01(float x)
+	{
+		return clamp(x, 0.0f, 1.0f);
+	}
+
+	inline int ceilPow2(int x)
+	{
+		int i = 1;
+
+		while(i < x)
+		{
+			i <<= 1;
+		}
+
+		return i;
+	}
+
+	inline int floorDiv(int a, int b)
+	{
+		return a / b + ((a % b) >> 31);
+	}
+
+	inline int floorMod(int a, int b)
+	{
+		int r = a % b;
+		return r + ((r >> 31) & b);
+	}
+
+	inline int ceilDiv(int a, int b)
+	{
+		return a / b - (-(a % b) >> 31);
+	}
+
+	inline int ceilMod(int a, int b)
+	{
+		int r = a % b;
+		return r - ((-r >> 31) & b);
+	}
+
+	template<const int n>
+	inline unsigned int unorm(float x)
+	{
+		static const unsigned int max = 0xFFFFFFFF >> (32 - n);
+		static const float maxf = static_cast<float>(max);
+
+		if(x >= 1.0f)
+		{
+			return max;
+		}
+		else if(x <= 0.0f)
+		{
+			return 0;
+		}
+		else
+		{
+			return static_cast<unsigned int>(maxf * x + 0.5f);
+		}
+	}
+
+	template<const int n>
+	inline int snorm(float x)
+	{
+		static const unsigned int min = 0x80000000 >> (32 - n);
+		static const unsigned int max = 0xFFFFFFFF >> (32 - n + 1);
+		static const float maxf = static_cast<float>(max);
+		static const unsigned int range = 0xFFFFFFFF >> (32 - n);
+
+		if(x >= 0.0f)
+		{
+			if(x >= 1.0f)
+			{
+				return max;
+			}
+			else
+			{
+				return static_cast<int>(maxf * x + 0.5f);
+			}
+		}
+		else
+		{
+			if(x <= -1.0f)
+			{
+				return min;
+			}
+			else
+			{
+				return static_cast<int>(maxf * x - 0.5f) & range;
+			}
+		}
+	}
+
+	template<const int n>
+	inline unsigned int ucast(float x)
+	{
+		static const unsigned int max = 0xFFFFFFFF >> (32 - n);
+		static const float maxf = static_cast<float>(max);
+
+		if(x >= maxf)
+		{
+			return max;
+		}
+		else if(x <= 0.0f)
+		{
+			return 0;
+		}
+		else
+		{
+			return static_cast<unsigned int>(x + 0.5f);
+		}
+	}
+
+	template<const int n>
+	inline int scast(float x)
+	{
+		static const unsigned int min = 0x80000000 >> (32 - n);
+		static const unsigned int max = 0xFFFFFFFF >> (32 - n + 1);
+		static const float maxf = static_cast<float>(max);
+		static const unsigned int range = 0xFFFFFFFF >> (32 - n);
+
+		if(x > 0.0f)
+		{
+			if(x >= maxf)
+			{
+				return max;
+			}
+			else
+			{
+				return static_cast<int>(maxf * x + 0.5f);
+			}
+		}
+		else
+		{
+			if(x <= -1.0f)
+			{
+				return min;
+			}
+			else
+			{
+				return static_cast<int>(maxf * x - 0.5f) & range;
+			}
+		}
+	}
+
+	inline float sRGBtoLinear(float c)
+	{
+		if(c <= 0.04045f)
+		{
+			return c * 0.07739938f;   // 1.0f / 12.92f;
+		}
+		else
+		{
+			return powf((c + 0.055f) * 0.9478673f, 2.4f);   // 1.0f / 1.055f
+		}
+	}
+
+	inline float linearToSRGB(float c)
+	{
+		if(c <= 0.0031308f)
+		{
+			return c * 12.92f;
+		}
+		else
+		{
+			return 1.055f * powf(c, 0.4166667f) - 0.055f;   // 1.0f / 2.4f
+		}
+	}
+
+	unsigned char sRGB8toLinear8(unsigned char value);
+
+	uint64_t FNV_1a(const unsigned char *data, int size);   // Fowler-Noll-Vo hash function
+
+	// Round up to the next multiple of alignment
+	inline unsigned int align(unsigned int value, unsigned int alignment)
+	{
+		return ((value + alignment - 1) / alignment) * alignment;
+	}
+
+	inline int clampToSignedInt(unsigned int x)
+	{
+		return static_cast<int>(min(x, 0x7FFFFFFFu));
+	}
+
+	class RGB9E5Data
+	{
+		unsigned int R : 9;
+		unsigned int G : 9;
+		unsigned int B : 9;
+		unsigned int E : 5;
+
+	public:
+		void toRGBFloats(float* rgb) const
+		{
+			static const float Offset = -24.0f; // Exponent Bias (15) + Number of mantissa bits per component (9) = 24
+
+			const float factor = powf(2.0f, static_cast<float>(E) + Offset);
+			rgb[0] = static_cast<float>(R) * factor;
+			rgb[1] = static_cast<float>(G) * factor;
+			rgb[2] = static_cast<float>(B) * factor;
+		}
+	};
+
+	class R11G11B10FData
+	{
+		unsigned int R : 11;
+		unsigned int G : 11;
+		unsigned int B : 10;
+
+		static inline float float11ToFloat32(unsigned short fp11)
+		{
+			unsigned short exponent = (fp11 >> 6) & 0x1F;
+			unsigned short mantissa = fp11 & 0x3F;
+
+			unsigned int output;
+			if(exponent == 0x1F)
+			{
+				// INF or NAN
+				output = 0x7f800000 | (mantissa << 17);
+			}
+			else
+			{
+				if(exponent != 0)
+				{
+					// normalized
+				}
+				else if(mantissa != 0)
+				{
+					// The value is denormalized
+					exponent = 1;
+
+					do
+					{
+						exponent--;
+						mantissa <<= 1;
+					} while((mantissa & 0x40) == 0);
+
+					mantissa = mantissa & 0x3F;
+				}
+				else // The value is zero
+				{
+					exponent = static_cast<unsigned short>(-112);
+				}
+
+				output = ((exponent + 112) << 23) | (mantissa << 17);
+			}
+
+			return *(float*)(&output);
+		}
+
+		static inline float float10ToFloat32(unsigned short fp10)
+		{
+			unsigned short exponent = (fp10 >> 5) & 0x1F;
+			unsigned short mantissa = fp10 & 0x1F;
+
+			unsigned int output;
+			if(exponent == 0x1F)
+			{
+				// INF or NAN
+				output = 0x7f800000 | (mantissa << 17);
+			}
+			else
+			{
+				if(exponent != 0)
+				{
+					// normalized
+				}
+				else if(mantissa != 0)
+				{
+					// The value is denormalized
+					exponent = 1;
+
+					do
+					{
+						exponent--;
+						mantissa <<= 1;
+					} while((mantissa & 0x20) == 0);
+
+					mantissa = mantissa & 0x1F;
+				}
+				else // The value is zero
+				{
+					exponent = static_cast<unsigned short>(-112);
+				}
+
+				output = ((exponent + 112) << 23) | (mantissa << 18);
+			}
+
+			return *(float*)(&output);
+		}
+
+	public:
+		void toRGBFloats(float* rgb) const
+		{
+			rgb[0] = float11ToFloat32(R);
+			rgb[1] = float11ToFloat32(G);
+			rgb[2] = float10ToFloat32(B);
+		}
+	};
+}
+
+#endif   // sw_Math_hpp
diff --git a/src/Common/Memory.cpp b/src/Common/Memory.cpp
index 12f21b3..d87575c 100644
--- a/src/Common/Memory.cpp
+++ b/src/Common/Memory.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 "Memory.hpp"
 
diff --git a/src/Common/Memory.hpp b/src/Common/Memory.hpp
index 36b84e5..3cf4be1 100644
--- a/src/Common/Memory.hpp
+++ b/src/Common/Memory.hpp
@@ -1,30 +1,33 @@
-// 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 Memory_hpp

-#define Memory_hpp

-

-#include <stddef.h>

-

-namespace sw

-{

-size_t memoryPageSize();

-

-void *allocate(size_t bytes, size_t alignment = 16);

-void *allocateZero(size_t bytes, size_t alignment = 16);

-void deallocate(void *memory);

-

-void *allocateExecutable(size_t bytes);   // Allocates memory that can be made executable using markExecutable()

-void markExecutable(void *memory, size_t bytes);

-void deallocateExecutable(void *memory, size_t bytes);

-}

-

-#endif   // Memory_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 Memory_hpp
+#define Memory_hpp
+
+#include <stddef.h>
+
+namespace sw
+{
+size_t memoryPageSize();
+
+void *allocate(size_t bytes, size_t alignment = 16);
+void *allocateZero(size_t bytes, size_t alignment = 16);
+void deallocate(void *memory);
+
+void *allocateExecutable(size_t bytes);   // Allocates memory that can be made executable using markExecutable()
+void markExecutable(void *memory, size_t bytes);
+void deallocateExecutable(void *memory, size_t bytes);
+}
+
+#endif   // Memory_hpp
diff --git a/src/Common/MetaMacro.hpp b/src/Common/MetaMacro.hpp
index 1ceb70c..f717d75 100644
--- a/src/Common/MetaMacro.hpp
+++ b/src/Common/MetaMacro.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_MetaMacro_hpp
 #define sw_MetaMacro_hpp
diff --git a/src/Common/MutexLock.hpp b/src/Common/MutexLock.hpp
index 31e5189..16835ed 100644
--- a/src/Common/MutexLock.hpp
+++ b/src/Common/MutexLock.hpp
@@ -1,126 +1,129 @@
-// 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_MutexLock_hpp

-#define sw_MutexLock_hpp

-

-#include "Thread.hpp"

-

-namespace sw

-{

-	class BackoffLock

-	{

-	public:

-		BackoffLock()

-		{

-			mutex = 0;

-		}

-

-		bool attemptLock()

-		{

-			if(!isLocked())

-			{

-				if(atomicExchange(&mutex, 1) == 0)

-				{

-					return true;

-				}

-			}

-

-			return false;

-		}

-

-		void lock()

-		{

-			int backoff = 1;

-

-			while(!attemptLock())

-			{

-				if(backoff <= 64)

-				{

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

-					{

-						nop();

-						nop();

-						nop();

-						nop();

-						nop();

-

-						nop();

-						nop();

-						nop();

-						nop();

-						nop();

-

-						nop();

-						nop();

-						nop();

-						nop();

-						nop();

-

-						nop();

-						nop();

-						nop();

-						nop();

-						nop();

-

-						nop();

-						nop();

-						nop();

-						nop();

-						nop();

-

-						nop();

-						nop();

-						nop();

-						nop();

-						nop();

-

-						nop();

-						nop();

-						nop();

-						nop();

-						nop();

-					}

-

-					backoff *= 2;

-				}

-				else

-				{

-					Thread::yield();

-

-					backoff = 1;

-				}

-			};

-		}

-

-		void unlock()

-		{

-			mutex = 0;

-		}

-

-		bool isLocked()

-		{

-			return mutex != 0;

-		}

-

-	private:

-		struct

-		{

-			// Ensure that the mutex variable is on its own 64-byte cache line to avoid false sharing

-			// Padding must be public to avoid compiler warnings

-			volatile int padding1[16];

-			volatile int mutex;

-			volatile int padding2[15];

-		};

-	};

-}

-

-#endif   // sw_MutexLock_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_MutexLock_hpp
+#define sw_MutexLock_hpp
+
+#include "Thread.hpp"
+
+namespace sw
+{
+	class BackoffLock
+	{
+	public:
+		BackoffLock()
+		{
+			mutex = 0;
+		}
+
+		bool attemptLock()
+		{
+			if(!isLocked())
+			{
+				if(atomicExchange(&mutex, 1) == 0)
+				{
+					return true;
+				}
+			}
+
+			return false;
+		}
+
+		void lock()
+		{
+			int backoff = 1;
+
+			while(!attemptLock())
+			{
+				if(backoff <= 64)
+				{
+					for(int i = 0; i < backoff; i++)
+					{
+						nop();
+						nop();
+						nop();
+						nop();
+						nop();
+
+						nop();
+						nop();
+						nop();
+						nop();
+						nop();
+
+						nop();
+						nop();
+						nop();
+						nop();
+						nop();
+
+						nop();
+						nop();
+						nop();
+						nop();
+						nop();
+
+						nop();
+						nop();
+						nop();
+						nop();
+						nop();
+
+						nop();
+						nop();
+						nop();
+						nop();
+						nop();
+
+						nop();
+						nop();
+						nop();
+						nop();
+						nop();
+					}
+
+					backoff *= 2;
+				}
+				else
+				{
+					Thread::yield();
+
+					backoff = 1;
+				}
+			};
+		}
+
+		void unlock()
+		{
+			mutex = 0;
+		}
+
+		bool isLocked()
+		{
+			return mutex != 0;
+		}
+
+	private:
+		struct
+		{
+			// Ensure that the mutex variable is on its own 64-byte cache line to avoid false sharing
+			// Padding must be public to avoid compiler warnings
+			volatile int padding1[16];
+			volatile int mutex;
+			volatile int padding2[15];
+		};
+	};
+}
+
+#endif   // sw_MutexLock_hpp
diff --git a/src/Common/Resource.cpp b/src/Common/Resource.cpp
index e96d7d0..b016c95 100644
--- a/src/Common/Resource.cpp
+++ b/src/Common/Resource.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 "Resource.hpp"
 
@@ -28,7 +31,7 @@
 
 	Resource::~Resource()
 	{
- 		deallocate(buffer);
+		deallocate(buffer);
 	}
 
 	void *Resource::lock(Accessor claimer)
diff --git a/src/Common/Resource.hpp b/src/Common/Resource.hpp
index 6a275f1..6d2577e 100644
--- a/src/Common/Resource.hpp
+++ b/src/Common/Resource.hpp
@@ -1,57 +1,60 @@
-// 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_Resource_hpp

-#define sw_Resource_hpp

-

-#include "MutexLock.hpp"

-

-namespace sw

-{

-	enum Accessor

-	{

-		PUBLIC,    // Application/API access

-		PRIVATE,   // Renderer access, shared by multiple threads if read-only

-		MANAGED,   // Renderer access, shared read/write access if partitioned

-		DESTRUCT

-	};

-

-	class Resource

-	{

-	public:

-		Resource(size_t bytes);

-

-		void destruct();   // Asynchronous destructor

-

-		void *lock(Accessor claimer);

-		void *lock(Accessor relinquisher, Accessor claimer);

-		void unlock();

-		void unlock(Accessor relinquisher);

-

-		const void *data() const;

-		const size_t size;

-

-	private:

-		~Resource();   // Always call destruct() instead

-

-		BackoffLock criticalSection;

-		Event unblock;

-		volatile int blocked;

-

-		volatile Accessor accessor;

-		volatile int count;

-		bool orphaned;

-

-		void *buffer;

-	};

-}

-

-#endif   // sw_Resource_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_Resource_hpp
+#define sw_Resource_hpp
+
+#include "MutexLock.hpp"
+
+namespace sw
+{
+	enum Accessor
+	{
+		PUBLIC,    // Application/API access
+		PRIVATE,   // Renderer access, shared by multiple threads if read-only
+		MANAGED,   // Renderer access, shared read/write access if partitioned
+		DESTRUCT
+	};
+
+	class Resource
+	{
+	public:
+		Resource(size_t bytes);
+
+		void destruct();   // Asynchronous destructor
+
+		void *lock(Accessor claimer);
+		void *lock(Accessor relinquisher, Accessor claimer);
+		void unlock();
+		void unlock(Accessor relinquisher);
+
+		const void *data() const;
+		const size_t size;
+
+	private:
+		~Resource();   // Always call destruct() instead
+
+		BackoffLock criticalSection;
+		Event unblock;
+		volatile int blocked;
+
+		volatile Accessor accessor;
+		volatile int count;
+		bool orphaned;
+
+		void *buffer;
+	};
+}
+
+#endif   // sw_Resource_hpp
diff --git a/src/Common/SharedLibrary.hpp b/src/Common/SharedLibrary.hpp
index 1046d64..8d881c1 100644
--- a/src/Common/SharedLibrary.hpp
+++ b/src/Common/SharedLibrary.hpp
@@ -1,129 +1,132 @@
-// 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 SharedLibrary_hpp

-#define SharedLibrary_hpp

-

-#if defined(_WIN32)

-	#include <Windows.h>

-#else

-	#include <dlfcn.h>

-#endif

-

-void *getLibraryHandle(const char *path);

-void *loadLibrary(const char *path);

-void freeLibrary(void *library);

-void *getProcAddress(void *library, const char *name);

-

-template<int n>

-void *loadLibrary(const char *(&names)[n], const char *mustContainSymbol = nullptr)

-{

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

-	{

-		void *library = getLibraryHandle(names[i]);

-

-		if(library)

-		{

-			if(!mustContainSymbol || getProcAddress(library, mustContainSymbol))

-			{

-				return library;

-			}

-

-			freeLibrary(library);

-		}

-	}

-

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

-	{

-		void *library = loadLibrary(names[i]);

-

-		if(library)

-		{

-			if(!mustContainSymbol || getProcAddress(library, mustContainSymbol))

-			{

-				return library;

-			}

-

-			freeLibrary(library);

-		}

-	}

-

-	return 0;

-}

-

-#if defined(_WIN32)

-	inline void *loadLibrary(const char *path)

-	{

-		return (void*)LoadLibrary(path);

-	}

-

-	inline void *getLibraryHandle(const char *path)

-	{

-		HMODULE module = 0;

-		GetModuleHandleEx(0, path, &module);

-		return (void*)module;

-	}

-

-	inline void freeLibrary(void *library)

-	{

-		FreeLibrary((HMODULE)library);

-	}

-

-	inline void *getProcAddress(void *library, const char *name)

-	{

-		return (void*)GetProcAddress((HMODULE)library, name);

-	}

-#else

-	inline void *loadLibrary(const char *path)

-	{

-		return dlopen(path, RTLD_LAZY | RTLD_LOCAL);

-	}

-

-	inline void *getLibraryHandle(const char *path)

-	{

-		#ifdef __ANDROID__

-			// bionic doesn't support RTLD_NOLOAD before L

-			return dlopen(path, RTLD_NOW | RTLD_LOCAL);

-		#else

-			void *resident = dlopen(path, RTLD_LAZY | RTLD_NOLOAD | RTLD_LOCAL);

-

-			if(resident)

-			{

-				return dlopen(path, RTLD_LAZY | RTLD_LOCAL);   // Increment reference count

-			}

-

-			return 0;

-		#endif

-	}

-

-    inline void freeLibrary(void *library)

-    {

-        if(library)

-        {

-            dlclose(library);

-        }

-    }

-

-	inline void *getProcAddress(void *library, const char *name)

-	{

-		void *symbol = dlsym(library, name);

-

-		if(!symbol)

-		{

-			const char *reason = dlerror();   // Silence the error

-			(void)reason;

-		}

-

-		return symbol;

-    }

-#endif

-

-#endif   // SharedLibrary_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 SharedLibrary_hpp
+#define SharedLibrary_hpp
+
+#if defined(_WIN32)
+	#include <Windows.h>
+#else
+	#include <dlfcn.h>
+#endif
+
+void *getLibraryHandle(const char *path);
+void *loadLibrary(const char *path);
+void freeLibrary(void *library);
+void *getProcAddress(void *library, const char *name);
+
+template<int n>
+void *loadLibrary(const char *(&names)[n], const char *mustContainSymbol = nullptr)
+{
+	for(int i = 0; i < n; i++)
+	{
+		void *library = getLibraryHandle(names[i]);
+
+		if(library)
+		{
+			if(!mustContainSymbol || getProcAddress(library, mustContainSymbol))
+			{
+				return library;
+			}
+
+			freeLibrary(library);
+		}
+	}
+
+	for(int i = 0; i < n; i++)
+	{
+		void *library = loadLibrary(names[i]);
+
+		if(library)
+		{
+			if(!mustContainSymbol || getProcAddress(library, mustContainSymbol))
+			{
+				return library;
+			}
+
+			freeLibrary(library);
+		}
+	}
+
+	return 0;
+}
+
+#if defined(_WIN32)
+	inline void *loadLibrary(const char *path)
+	{
+		return (void*)LoadLibrary(path);
+	}
+
+	inline void *getLibraryHandle(const char *path)
+	{
+		HMODULE module = 0;
+		GetModuleHandleEx(0, path, &module);
+		return (void*)module;
+	}
+
+	inline void freeLibrary(void *library)
+	{
+		FreeLibrary((HMODULE)library);
+	}
+
+	inline void *getProcAddress(void *library, const char *name)
+	{
+		return (void*)GetProcAddress((HMODULE)library, name);
+	}
+#else
+	inline void *loadLibrary(const char *path)
+	{
+		return dlopen(path, RTLD_LAZY | RTLD_LOCAL);
+	}
+
+	inline void *getLibraryHandle(const char *path)
+	{
+		#ifdef __ANDROID__
+			// bionic doesn't support RTLD_NOLOAD before L
+			return dlopen(path, RTLD_NOW | RTLD_LOCAL);
+		#else
+			void *resident = dlopen(path, RTLD_LAZY | RTLD_NOLOAD | RTLD_LOCAL);
+
+			if(resident)
+			{
+				return dlopen(path, RTLD_LAZY | RTLD_LOCAL);   // Increment reference count
+			}
+
+			return 0;
+		#endif
+	}
+
+	inline void freeLibrary(void *library)
+	{
+		if(library)
+		{
+			dlclose(library);
+		}
+	}
+
+	inline void *getProcAddress(void *library, const char *name)
+	{
+		void *symbol = dlsym(library, name);
+
+		if(!symbol)
+		{
+			const char *reason = dlerror();   // Silence the error
+			(void)reason;
+		}
+
+		return symbol;
+	}
+#endif
+
+#endif   // SharedLibrary_hpp
diff --git a/src/Common/Socket.cpp b/src/Common/Socket.cpp
index a0ac3f4..a19f574 100644
--- a/src/Common/Socket.cpp
+++ b/src/Common/Socket.cpp
@@ -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.

-//

-

-#include "Socket.hpp"

-

-#if defined(_WIN32)

-	#include <ws2tcpip.h>

-#else

-	#include <unistd.h>

-	#include <netdb.h>

-	#include <netinet/in.h>

-#endif

-

-namespace sw

-{

-	Socket::Socket(SOCKET socket) : socket(socket)

-	{

-	}

-

-	Socket::Socket(const char *address, const char *port)

-	{

-		#if defined(_WIN32)

-			socket = INVALID_SOCKET;

-		#else

-			socket = -1;

-		#endif

-

-		addrinfo hints = {};

-		hints.ai_family = AF_INET;

-		hints.ai_socktype = SOCK_STREAM;

-		hints.ai_protocol = IPPROTO_TCP;

-		hints.ai_flags = AI_PASSIVE;

-

-		addrinfo *info = 0;

-		getaddrinfo(address, port, &hints, &info);

-

-		if(info)

-		{

-			socket = ::socket(info->ai_family, info->ai_socktype, info->ai_protocol);

-			bind(socket, info->ai_addr, (int)info->ai_addrlen);

-		}

-	}

-

-	Socket::~Socket()

-	{

-		#if defined(_WIN32)

-			closesocket(socket);

-		#else

-			close(socket);

-		#endif

-	}

-

-	void Socket::listen(int backlog)

-	{

-		::listen(socket, backlog);

-	}

-

-	bool Socket::select(int us)

-	{

-		fd_set sockets;

-		FD_ZERO(&sockets);

-		FD_SET(socket, &sockets);

-		

-		timeval timeout = {us / 1000000, us % 1000000};

-

-		return ::select(FD_SETSIZE, &sockets, 0, 0, &timeout) >= 1;

-	}

-

-	Socket *Socket::accept()

-	{

-		return new Socket(::accept(socket, 0, 0));

-	}

-

-	int Socket::receive(char *buffer, int length)

-	{

-		return recv(socket, buffer, length, 0);

-	}

-

-	void Socket::send(const char *buffer, int length)

-	{

-		::send(socket, buffer, length, 0);

-	}

-

-	void Socket::startup()

-	{

-		#if defined(_WIN32)

-			WSADATA winsockData;

-			WSAStartup(MAKEWORD(2, 2), &winsockData);

-		#endif

-	}

-

-	void Socket::cleanup()

-	{

-		#if defined(_WIN32)

-			WSACleanup();

-		#endif

-	}

-}

+// 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 "Socket.hpp"
+
+#if defined(_WIN32)
+	#include <ws2tcpip.h>
+#else
+	#include <unistd.h>
+	#include <netdb.h>
+	#include <netinet/in.h>
+#endif
+
+namespace sw
+{
+	Socket::Socket(SOCKET socket) : socket(socket)
+	{
+	}
+
+	Socket::Socket(const char *address, const char *port)
+	{
+		#if defined(_WIN32)
+			socket = INVALID_SOCKET;
+		#else
+			socket = -1;
+		#endif
+
+		addrinfo hints = {};
+		hints.ai_family = AF_INET;
+		hints.ai_socktype = SOCK_STREAM;
+		hints.ai_protocol = IPPROTO_TCP;
+		hints.ai_flags = AI_PASSIVE;
+
+		addrinfo *info = 0;
+		getaddrinfo(address, port, &hints, &info);
+
+		if(info)
+		{
+			socket = ::socket(info->ai_family, info->ai_socktype, info->ai_protocol);
+			bind(socket, info->ai_addr, (int)info->ai_addrlen);
+		}
+	}
+
+	Socket::~Socket()
+	{
+		#if defined(_WIN32)
+			closesocket(socket);
+		#else
+			close(socket);
+		#endif
+	}
+
+	void Socket::listen(int backlog)
+	{
+		::listen(socket, backlog);
+	}
+
+	bool Socket::select(int us)
+	{
+		fd_set sockets;
+		FD_ZERO(&sockets);
+		FD_SET(socket, &sockets);
+
+		timeval timeout = {us / 1000000, us % 1000000};
+
+		return ::select(FD_SETSIZE, &sockets, 0, 0, &timeout) >= 1;
+	}
+
+	Socket *Socket::accept()
+	{
+		return new Socket(::accept(socket, 0, 0));
+	}
+
+	int Socket::receive(char *buffer, int length)
+	{
+		return recv(socket, buffer, length, 0);
+	}
+
+	void Socket::send(const char *buffer, int length)
+	{
+		::send(socket, buffer, length, 0);
+	}
+
+	void Socket::startup()
+	{
+		#if defined(_WIN32)
+			WSADATA winsockData;
+			WSAStartup(MAKEWORD(2, 2), &winsockData);
+		#endif
+	}
+
+	void Socket::cleanup()
+	{
+		#if defined(_WIN32)
+			WSACleanup();
+		#endif
+	}
+}
diff --git a/src/Common/Socket.hpp b/src/Common/Socket.hpp
index b712cd3..b6b9abd 100644
--- a/src/Common/Socket.hpp
+++ b/src/Common/Socket.hpp
@@ -1,46 +1,49 @@
-// 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_Socket_hpp

-#define sw_Socket_hpp

-

-#if defined(_WIN32)

-	#include <winsock2.h>

-#else

-	#include <sys/socket.h>

-	typedef int SOCKET;

-#endif

-

-namespace sw

-{

-	class Socket

-	{

-	public:

-		Socket(SOCKET socket);

-		Socket(const char *address, const char *port);

-		~Socket();

-

-		void listen(int backlog = 1);

-		bool select(int us);

-		Socket *accept();

-		

-		int receive(char *buffer, int length);

-		void send(const char *buffer, int length);

-

-		static void startup();

-		static void cleanup();

-

-	private:

-		SOCKET socket;

-	};

-}

-

-#endif   // sw_Socket_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_Socket_hpp
+#define sw_Socket_hpp
+
+#if defined(_WIN32)
+	#include <winsock2.h>
+#else
+	#include <sys/socket.h>
+	typedef int SOCKET;
+#endif
+
+namespace sw
+{
+	class Socket
+	{
+	public:
+		Socket(SOCKET socket);
+		Socket(const char *address, const char *port);
+		~Socket();
+
+		void listen(int backlog = 1);
+		bool select(int us);
+		Socket *accept();
+		
+		int receive(char *buffer, int length);
+		void send(const char *buffer, int length);
+
+		static void startup();
+		static void cleanup();
+
+	private:
+		SOCKET socket;
+	};
+}
+
+#endif   // sw_Socket_hpp
diff --git a/src/Common/Thread.cpp b/src/Common/Thread.cpp
index 9db3b48..9a7b342 100644
--- a/src/Common/Thread.cpp
+++ b/src/Common/Thread.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 "Thread.hpp"
 
diff --git a/src/Common/Thread.hpp b/src/Common/Thread.hpp
index d57dd41..deb8b29 100644
--- a/src/Common/Thread.hpp
+++ b/src/Common/Thread.hpp
@@ -1,250 +1,253 @@
-// 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_Thread_hpp

-#define sw_Thread_hpp

-

-#if defined(_WIN32)

-	#ifndef WIN32_LEAN_AND_MEAN

-		#define WIN32_LEAN_AND_MEAN

-	#endif

-	#include <windows.h>

-	#include <intrin.h>

-#else

-	#include <pthread.h>

-	#include <sched.h>

-	#include <unistd.h>

-	#define TLS_OUT_OF_INDEXES (~0)

-#endif

-

-namespace sw

-{

-	class Event;

-

-	class Thread

-	{

-	public:

-		Thread(void (*threadFunction)(void *parameters), void *parameters);

-

-		~Thread();

-

-		void join();

-

-		static void yield();

-		static void sleep(int milliseconds);

-

-		#if defined(_WIN32)

-			typedef DWORD LocalStorageKey;

-		#else

-			typedef pthread_key_t LocalStorageKey;

-		#endif

-

-		static LocalStorageKey allocateLocalStorageKey();

-		static void freeLocalStorageKey(LocalStorageKey key);

-		static void setLocalStorage(LocalStorageKey key, void *value);

-		static void *getLocalStorage(LocalStorageKey key);

-

-	private:

-		struct Entry

-		{

-			void (*const threadFunction)(void *parameters);

-			void *threadParameters;

-			Event *init;

-		};

-

-		#if defined(_WIN32)

-			static unsigned long __stdcall startFunction(void *parameters);

-			HANDLE handle;

-		#else

-			static void *startFunction(void *parameters);

-			pthread_t handle;

-		#endif

-	};

-

-	class Event

-	{

-		friend class Thread;

-

-	public:

-		Event();

-

-		~Event();

-

-		void signal();

-		void wait();

-

-	private:

-		#if defined(_WIN32)

-			HANDLE handle;

-		#else

-			pthread_cond_t handle;

-			pthread_mutex_t mutex;

-			volatile bool signaled;

-		#endif

-	};

-

-	#if PERF_PROFILE

-	int64_t atomicExchange(int64_t volatile *target, int64_t value);

-	#endif

-

-	int atomicExchange(int volatile *target, int value);

-	int atomicIncrement(int volatile *value);

-	int atomicDecrement(int volatile *value);

-	int atomicAdd(int volatile *target, int value);

-	void nop();

-}

-

-namespace sw

-{

-	inline void Thread::yield()

-	{

-		#if defined(_WIN32)

-			Sleep(0);

-		#elif defined(__APPLE__)

-			pthread_yield_np();

-		#else

-			sched_yield();

-		#endif

-	}

-

-	inline void Thread::sleep(int milliseconds)

-	{

-		#if defined(_WIN32)

-			Sleep(milliseconds);

-		#else

-			usleep(1000 * milliseconds);

-		#endif

-	}

-

-	inline Thread::LocalStorageKey Thread::allocateLocalStorageKey()

-	{

-		#if defined(_WIN32)

-			return TlsAlloc();

-		#else

-			LocalStorageKey key;

-			pthread_key_create(&key, 0);

-			return key;

-		#endif

-	}

-	

-	inline void Thread::freeLocalStorageKey(LocalStorageKey key)

-	{

-		#if defined(_WIN32)

-			TlsFree(key);

-		#else

-			pthread_key_delete(key);

-		#endif

-	}

-

-	inline void Thread::setLocalStorage(LocalStorageKey key, void *value)

-	{

-		#if defined(_WIN32)

-			TlsSetValue(key, value);

-		#else

-			pthread_setspecific(key, value);

-		#endif

-	}

-

-	inline void *Thread::getLocalStorage(LocalStorageKey key)

-	{

-		#if defined(_WIN32)

-			return TlsGetValue(key);

-		#else

-			return pthread_getspecific(key);

-		#endif

-	}

-

-	inline void Event::signal()

-	{

-		#if defined(_WIN32)

-			SetEvent(handle);

-		#else

-			pthread_mutex_lock(&mutex);

-			signaled = true;

-			pthread_cond_signal(&handle);

-			pthread_mutex_unlock(&mutex);

-		#endif

-	}

-

-	inline void Event::wait()

-	{

-		#if defined(_WIN32)

-			WaitForSingleObject(handle, INFINITE);

-		#else

-			pthread_mutex_lock(&mutex);

-			while(!signaled) pthread_cond_wait(&handle, &mutex);

-			signaled = false;

-			pthread_mutex_unlock(&mutex);

-		#endif

-	}

-

-	#if PERF_PROFILE

-	inline int64_t atomicExchange(volatile int64_t *target, int64_t value)

-	{

-		#if defined(_WIN32)

-			return InterlockedExchange64(target, value);

-		#else

-			int ret;

-			__asm__ __volatile__("lock; xchg8 %0,(%1)" : "=r" (ret) :"r" (target), "0" (value) : "memory" );

-			return ret;

-		#endif

-	}

-	#endif

-

-	inline int atomicExchange(volatile int *target, int value)

-	{

-		#if defined(_WIN32)

-			return InterlockedExchange((volatile long*)target, (long)value);

-		#else

-			int ret;

-			__asm__ __volatile__("lock; xchgl %0,(%1)" : "=r" (ret) :"r" (target), "0" (value) : "memory" );

-			return ret;

-		#endif

-	}

-

-	inline int atomicIncrement(volatile int *value)

-	{

-		#if defined(_WIN32)

-			return InterlockedIncrement((volatile long*)value);

-		#else

-			return __sync_add_and_fetch(value, 1);

-		#endif

-	}

-

-	inline int atomicDecrement(volatile int *value)

-	{

-		#if defined(_WIN32)

-			return InterlockedDecrement((volatile long*)value);

-		#else

-			return __sync_sub_and_fetch(value, 1);

-		#endif

-	}

-

-	inline int atomicAdd(volatile int* target, int value)

-	{

-		#if defined(_MSC_VER)

-			return InterlockedExchangeAdd((volatile long*)target, value) + value;

-		#else

-			return __sync_add_and_fetch(target, value);

-		#endif

-	}

-

-	inline void nop()

-	{

-		#if defined(_WIN32)

-			__nop();

-		#else

-			__asm__ __volatile__ ("nop");

-		#endif

-	}

-}

-

-#endif   // sw_Thread_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_Thread_hpp
+#define sw_Thread_hpp
+
+#if defined(_WIN32)
+	#ifndef WIN32_LEAN_AND_MEAN
+		#define WIN32_LEAN_AND_MEAN
+	#endif
+	#include <windows.h>
+	#include <intrin.h>
+#else
+	#include <pthread.h>
+	#include <sched.h>
+	#include <unistd.h>
+	#define TLS_OUT_OF_INDEXES (~0)
+#endif
+
+namespace sw
+{
+	class Event;
+
+	class Thread
+	{
+	public:
+		Thread(void (*threadFunction)(void *parameters), void *parameters);
+
+		~Thread();
+
+		void join();
+
+		static void yield();
+		static void sleep(int milliseconds);
+
+		#if defined(_WIN32)
+			typedef DWORD LocalStorageKey;
+		#else
+			typedef pthread_key_t LocalStorageKey;
+		#endif
+
+		static LocalStorageKey allocateLocalStorageKey();
+		static void freeLocalStorageKey(LocalStorageKey key);
+		static void setLocalStorage(LocalStorageKey key, void *value);
+		static void *getLocalStorage(LocalStorageKey key);
+
+	private:
+		struct Entry
+		{
+			void (*const threadFunction)(void *parameters);
+			void *threadParameters;
+			Event *init;
+		};
+
+		#if defined(_WIN32)
+			static unsigned long __stdcall startFunction(void *parameters);
+			HANDLE handle;
+		#else
+			static void *startFunction(void *parameters);
+			pthread_t handle;
+		#endif
+	};
+
+	class Event
+	{
+		friend class Thread;
+
+	public:
+		Event();
+
+		~Event();
+
+		void signal();
+		void wait();
+
+	private:
+		#if defined(_WIN32)
+			HANDLE handle;
+		#else
+			pthread_cond_t handle;
+			pthread_mutex_t mutex;
+			volatile bool signaled;
+		#endif
+	};
+
+	#if PERF_PROFILE
+	int64_t atomicExchange(int64_t volatile *target, int64_t value);
+	#endif
+
+	int atomicExchange(int volatile *target, int value);
+	int atomicIncrement(int volatile *value);
+	int atomicDecrement(int volatile *value);
+	int atomicAdd(int volatile *target, int value);
+	void nop();
+}
+
+namespace sw
+{
+	inline void Thread::yield()
+	{
+		#if defined(_WIN32)
+			Sleep(0);
+		#elif defined(__APPLE__)
+			pthread_yield_np();
+		#else
+			sched_yield();
+		#endif
+	}
+
+	inline void Thread::sleep(int milliseconds)
+	{
+		#if defined(_WIN32)
+			Sleep(milliseconds);
+		#else
+			usleep(1000 * milliseconds);
+		#endif
+	}
+
+	inline Thread::LocalStorageKey Thread::allocateLocalStorageKey()
+	{
+		#if defined(_WIN32)
+			return TlsAlloc();
+		#else
+			LocalStorageKey key;
+			pthread_key_create(&key, 0);
+			return key;
+		#endif
+	}
+
+	inline void Thread::freeLocalStorageKey(LocalStorageKey key)
+	{
+		#if defined(_WIN32)
+			TlsFree(key);
+		#else
+			pthread_key_delete(key);
+		#endif
+	}
+
+	inline void Thread::setLocalStorage(LocalStorageKey key, void *value)
+	{
+		#if defined(_WIN32)
+			TlsSetValue(key, value);
+		#else
+			pthread_setspecific(key, value);
+		#endif
+	}
+
+	inline void *Thread::getLocalStorage(LocalStorageKey key)
+	{
+		#if defined(_WIN32)
+			return TlsGetValue(key);
+		#else
+			return pthread_getspecific(key);
+		#endif
+	}
+
+	inline void Event::signal()
+	{
+		#if defined(_WIN32)
+			SetEvent(handle);
+		#else
+			pthread_mutex_lock(&mutex);
+			signaled = true;
+			pthread_cond_signal(&handle);
+			pthread_mutex_unlock(&mutex);
+		#endif
+	}
+
+	inline void Event::wait()
+	{
+		#if defined(_WIN32)
+			WaitForSingleObject(handle, INFINITE);
+		#else
+			pthread_mutex_lock(&mutex);
+			while(!signaled) pthread_cond_wait(&handle, &mutex);
+			signaled = false;
+			pthread_mutex_unlock(&mutex);
+		#endif
+	}
+
+	#if PERF_PROFILE
+	inline int64_t atomicExchange(volatile int64_t *target, int64_t value)
+	{
+		#if defined(_WIN32)
+			return InterlockedExchange64(target, value);
+		#else
+			int ret;
+			__asm__ __volatile__("lock; xchg8 %0,(%1)" : "=r" (ret) :"r" (target), "0" (value) : "memory" );
+			return ret;
+		#endif
+	}
+	#endif
+
+	inline int atomicExchange(volatile int *target, int value)
+	{
+		#if defined(_WIN32)
+			return InterlockedExchange((volatile long*)target, (long)value);
+		#else
+			int ret;
+			__asm__ __volatile__("lock; xchgl %0,(%1)" : "=r" (ret) :"r" (target), "0" (value) : "memory" );
+			return ret;
+		#endif
+	}
+
+	inline int atomicIncrement(volatile int *value)
+	{
+		#if defined(_WIN32)
+			return InterlockedIncrement((volatile long*)value);
+		#else
+			return __sync_add_and_fetch(value, 1);
+		#endif
+	}
+
+	inline int atomicDecrement(volatile int *value)
+	{
+		#if defined(_WIN32)
+			return InterlockedDecrement((volatile long*)value);
+		#else
+			return __sync_sub_and_fetch(value, 1);
+		#endif
+	}
+
+	inline int atomicAdd(volatile int* target, int value)
+	{
+		#if defined(_MSC_VER)
+			return InterlockedExchangeAdd((volatile long*)target, value) + value;
+		#else
+			return __sync_add_and_fetch(target, value);
+		#endif
+	}
+
+	inline void nop()
+	{
+		#if defined(_WIN32)
+			__nop();
+		#else
+			__asm__ __volatile__ ("nop");
+		#endif
+	}
+}
+
+#endif   // sw_Thread_hpp
diff --git a/src/Common/Timer.cpp b/src/Common/Timer.cpp
index b2bbd1b..a147c8b 100644
--- a/src/Common/Timer.cpp
+++ b/src/Common/Timer.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 "Timer.hpp"
 
diff --git a/src/Common/Timer.hpp b/src/Common/Timer.hpp
index 0b8b6c4..977c877 100644
--- a/src/Common/Timer.hpp
+++ b/src/Common/Timer.hpp
@@ -1,34 +1,37 @@
-// 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_Timer_hpp

-#define sw_Timer_hpp

-

-#include "Types.hpp"

-

-namespace sw

-{

-	class Timer

-	{

-	public:

-		Timer();

-

-		~Timer();

-

-		static double seconds();

-		static int64_t ticks();

-

-		static int64_t counter();

-		static int64_t frequency();

-	};

-}

-

-#endif   // sw_Timer_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_Timer_hpp
+#define sw_Timer_hpp
+
+#include "Types.hpp"
+
+namespace sw
+{
+	class Timer
+	{
+	public:
+		Timer();
+
+		~Timer();
+
+		static double seconds();
+		static int64_t ticks();
+
+		static int64_t counter();
+		static int64_t frequency();
+	};
+}
+
+#endif   // sw_Timer_hpp
diff --git a/src/Common/Types.hpp b/src/Common/Types.hpp
index 1337f4d..5675ccb 100644
--- a/src/Common/Types.hpp
+++ b/src/Common/Types.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_Types_hpp
 #define sw_Types_hpp
@@ -123,7 +126,7 @@
 
 		return v;
 	}
-	
+
 	inline float4 replicate(float f)
 	{
 		float4 v;