Don't upload array texture images six times.

Change-Id: I5be0cc629477b6cfb3ede6b4484f067d1c30b0ec
Reviewed-on: https://swiftshader-review.googlesource.com/17929
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
diff --git a/src/OpenGL/libGLES_CM/libGLES_CM.cpp b/src/OpenGL/libGLES_CM/libGLES_CM.cpp
index e717d60..cfd17a9 100644
--- a/src/OpenGL/libGLES_CM/libGLES_CM.cpp
+++ b/src/OpenGL/libGLES_CM/libGLES_CM.cpp
@@ -4249,7 +4249,7 @@
 		return error(GL_INVALID_VALUE);
 	}
 
-	GLenum sizedInternalFormat = gl::GetSizedInternalFormat(internalformat, type);
+	GLint sizedInternalFormat = gl::GetSizedInternalFormat(internalformat, type);
 
 	es1::Context *context = es1::getContext();
 
diff --git a/src/OpenGL/libGLESv2/libGLESv2.cpp b/src/OpenGL/libGLESv2/libGLESv2.cpp
index 4be0273..56b2d06 100644
--- a/src/OpenGL/libGLESv2/libGLESv2.cpp
+++ b/src/OpenGL/libGLESv2/libGLESv2.cpp
@@ -925,7 +925,7 @@
 			{
 				return error(GL_INVALID_VALUE);
 			}
-			// Fall through
+			// Fall through to GL_TEXTURE_2D case.
 		case GL_TEXTURE_2D:
 			if(width > (es2::IMPLEMENTATION_MAX_TEXTURE_SIZE >> level) ||
 			   height > (es2::IMPLEMENTATION_MAX_TEXTURE_SIZE >> level))
@@ -3463,24 +3463,24 @@
 	case GL_RENDERER:
 		return (GLubyte*)"Google SwiftShader";
 	case GL_VERSION:
-	{
-		es2::Context *context = es2::getContext();
-		return (context && (context->getClientVersion() >= 3)) ?
-		       (GLubyte*)"OpenGL ES 3.0 SwiftShader " VERSION_STRING :
-		       (GLubyte*)"OpenGL ES 2.0 SwiftShader " VERSION_STRING;
-	}
+		{
+			es2::Context *context = es2::getContext();
+			return (context && (context->getClientVersion() >= 3)) ?
+			       (GLubyte*)"OpenGL ES 3.0 SwiftShader " VERSION_STRING :
+			       (GLubyte*)"OpenGL ES 2.0 SwiftShader " VERSION_STRING;
+		}
 	case GL_SHADING_LANGUAGE_VERSION:
-	{
-		es2::Context *context = es2::getContext();
-		return (context && (context->getClientVersion() >= 3)) ?
-		       (GLubyte*)"OpenGL ES GLSL ES 3.00 SwiftShader " VERSION_STRING :
-		       (GLubyte*)"OpenGL ES GLSL ES 1.00 SwiftShader " VERSION_STRING;
-	}
+		{
+			es2::Context *context = es2::getContext();
+			return (context && (context->getClientVersion() >= 3)) ?
+			       (GLubyte*)"OpenGL ES GLSL ES 3.00 SwiftShader " VERSION_STRING :
+			       (GLubyte*)"OpenGL ES GLSL ES 1.00 SwiftShader " VERSION_STRING;
+		}
 	case GL_EXTENSIONS:
-	{
-		es2::Context *context = es2::getContext();
-		return context ? context->getExtensions(GL_INVALID_INDEX) : (GLubyte*)nullptr;
-	}
+		{
+			es2::Context *context = es2::getContext();
+			return context ? context->getExtensions(GL_INVALID_INDEX) : (GLubyte*)nullptr;
+		}
 	default:
 		return error(GL_INVALID_ENUM, (GLubyte*)nullptr);
 	}
@@ -4971,7 +4971,7 @@
 			}
 		}
 
-		GLenum validationError = ValidateTextureFormatType(format, type, internalformat, target, context->getClientVersion());
+		GLenum validationError = ValidateTextureFormatType(format, type, internalformat, target, clientVersion);
 		if(validationError != GL_NO_ERROR)
 		{
 			return error(validationError);
@@ -4989,7 +4989,7 @@
 			{
 				return error(GL_INVALID_VALUE); // Defining level other than 0 is not allowed
 			}
-			// Fall through
+			// Fall through to GL_TEXTURE_2D case.
 		case GL_TEXTURE_2D:
 			if(width > (es2::IMPLEMENTATION_MAX_TEXTURE_SIZE >> level) ||
 			   height > (es2::IMPLEMENTATION_MAX_TEXTURE_SIZE >> level))
@@ -5018,14 +5018,14 @@
 			return error(GL_INVALID_ENUM);
 		}
 
-		GLenum sizedInternalFormat = gl::GetSizedInternalFormat(internalformat, type);
-
 		validationError = context->getPixels(&data, type, context->getRequiredBufferSize(width, height, 1, format, type));
 		if(validationError != GL_NO_ERROR)
 		{
 			return error(validationError);
 		}
 
+		GLint sizedInternalFormat = gl::GetSizedInternalFormat(internalformat, type);
+
 		if(target == GL_TEXTURE_2D || target == GL_TEXTURE_RECTANGLE_ARB)
 		{
 			es2::Texture2D *texture = context->getTexture2D(target);
@@ -6267,7 +6267,7 @@
 			return error(validationError);
 		}
 
-		GLenum sizedInternalFormat = gl::GetSizedInternalFormat(internalformat, type);
+		GLint sizedInternalFormat = gl::GetSizedInternalFormat(internalformat, type);
 		texture->setImage(level, width, height, depth, sizedInternalFormat, format, type, context->getUnpackParameters(), data);
 	}
 }
diff --git a/src/OpenGL/libGLESv2/libGLESv3.cpp b/src/OpenGL/libGLESv2/libGLESv3.cpp
index f5f3264..a985793 100644
--- a/src/OpenGL/libGLESv2/libGLESv3.cpp
+++ b/src/OpenGL/libGLESv2/libGLESv3.cpp
@@ -264,18 +264,18 @@
 		case GL_COLOR_ATTACHMENT29:
 		case GL_COLOR_ATTACHMENT30:
 		case GL_COLOR_ATTACHMENT31:
-		{
-			GLuint index = (src - GL_COLOR_ATTACHMENT0);
-			if(index >= MAX_COLOR_ATTACHMENTS)
 			{
-				return error(GL_INVALID_OPERATION);
+				GLuint index = (src - GL_COLOR_ATTACHMENT0);
+				if(index >= MAX_COLOR_ATTACHMENTS)
+				{
+					return error(GL_INVALID_OPERATION);
+				}
+				if(readFramebufferName == 0)
+				{
+					return error(GL_INVALID_OPERATION);
+				}
+				context->setFramebufferReadBuffer(src);
 			}
-			if(readFramebufferName == 0)
-			{
-				return error(GL_INVALID_OPERATION);
-			}
-			context->setFramebufferReadBuffer(src);
-		}
 			break;
 		default:
 			return error(GL_INVALID_ENUM);
@@ -387,7 +387,7 @@
 			return error(validationError);
 		}
 
-		GLenum sizedInternalFormat = gl::GetSizedInternalFormat(internalformat, type);
+		GLint sizedInternalFormat = gl::GetSizedInternalFormat(internalformat, type);
 		texture->setImage(level, width, height, depth, sizedInternalFormat, format, type, context->getUnpackParameters(), data);
 	}
 }
@@ -3621,6 +3621,7 @@
 			{
 				return error(GL_INVALID_ENUM);
 			}
+			// Fall through to GL_TEXTURE_2D case.
 		case GL_TEXTURE_2D:
 			{
 				if((width > es2::IMPLEMENTATION_MAX_TEXTURE_SIZE) ||
@@ -3735,10 +3736,8 @@
 
 				for(int level = 0; level < levels; level++)
 				{
-					for(int face = GL_TEXTURE_CUBE_MAP_POSITIVE_X; face <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; face++)
-					{
-						texture->setImage(level, width, height, depth, internalformat, GL_NONE, GL_NONE, context->getUnpackParameters(), nullptr);
-					}
+					texture->setImage(level, width, height, depth, internalformat, GL_NONE, GL_NONE, context->getUnpackParameters(), nullptr);
+
 					width = std::max(1, (width / 2));
 					height = std::max(1, (height / 2));
 				}