Validate unsized internal formats.

If the format and internalformat parameters are the same, only the
combinations in Table 3.3 of the OpenGL ES 3.0 specification are valid.

GL_RGB10_A2 and GL_RGB10_A2UI have GL_UNSIGNED_INT_2_10_10_10_REV type.
GL_OES_vertex_type_10_10_10_2 is a vertex attribute type, part of the
GL_OES_vertex_type_10_10_10_2 extension.

GL_RGB10_A2 internal format is valid for glReadPixels with a
combination of format GL_RGBA and type GL_UNSIGNED_INT_2_10_10_10_REV.

Change-Id: I590b43fcf9f1dc4beee9a64b45fe94fd74388b7a
Reviewed-on: https://swiftshader-review.googlesource.com/14788
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/OpenGL/libGLESv2/Framebuffer.cpp b/src/OpenGL/libGLESv2/Framebuffer.cpp
index c02725e..174c952 100644
--- a/src/OpenGL/libGLESv2/Framebuffer.cpp
+++ b/src/OpenGL/libGLESv2/Framebuffer.cpp
@@ -598,7 +598,7 @@
 		case GL_R16UI:          return GL_UNSIGNED_INT;
 		case GL_RG16UI:         return GL_UNSIGNED_INT;
 		case GL_RGB16UI:        return GL_UNSIGNED_INT;
-		case GL_RGB10_A2UI:     return GL_UNSIGNED_INT_10_10_10_2_OES;
+		case GL_RGB10_A2UI:     return GL_UNSIGNED_INT_2_10_10_10_REV;
 		case GL_RGBA16UI:       return GL_UNSIGNED_INT;
 		case GL_R32I:           return GL_INT;
 		case GL_RG32I:          return GL_INT;
@@ -617,7 +617,7 @@
 		case GL_RG32F:          return GL_FLOAT;
 		case GL_RGB32F:         return GL_FLOAT;
 		case GL_RGBA32F:        return GL_FLOAT;
-		case GL_RGB10_A2:       return GL_UNSIGNED_INT_10_10_10_2_OES;
+		case GL_RGB10_A2:       return GL_UNSIGNED_INT_2_10_10_10_REV;
 		case GL_SRGB8:          return GL_UNSIGNED_BYTE;
 		case GL_SRGB8_ALPHA8:   return GL_UNSIGNED_BYTE;
 		default:
diff --git a/src/OpenGL/libGLESv2/utilities.cpp b/src/OpenGL/libGLESv2/utilities.cpp
index 1903e3d..b784a4a 100644
--- a/src/OpenGL/libGLESv2/utilities.cpp
+++ b/src/OpenGL/libGLESv2/utilities.cpp
@@ -716,7 +716,7 @@
 		}
 
 		// Additional third combination accepted by OpenGL ES 3.0.
-		if(internalformat == sw::FORMAT_A2B10G10R10)
+		if(internalformat == GL_RGB10_A2)
 		{
 			ASSERT(clientVersion >= 3);
 
@@ -922,6 +922,94 @@
 			}
 		}
 
+		if((GLenum)internalformat == format)
+		{
+			// Validate format, type, and unsized internalformat combinations [OpenGL ES 3.0 Table 3.3]
+			switch(format)
+			{
+			case GL_RGBA:
+				switch(type)
+				{
+				case GL_UNSIGNED_BYTE:
+				case GL_UNSIGNED_SHORT_4_4_4_4:
+				case GL_UNSIGNED_SHORT_5_5_5_1:
+				case GL_FLOAT:            // GL_OES_texture_float
+				case GL_HALF_FLOAT_OES:   // GL_OES_texture_half_float
+					break;
+				default:
+					return GL_INVALID_OPERATION;
+				}
+				break;
+			case GL_RGB:
+				switch(type)
+				{
+				case GL_UNSIGNED_BYTE:
+				case GL_UNSIGNED_SHORT_5_6_5:
+				case GL_FLOAT:            // GL_OES_texture_float
+				case GL_HALF_FLOAT_OES:   // GL_OES_texture_half_float
+					break;
+				default:
+					return GL_INVALID_OPERATION;
+				}
+				break;
+			case GL_LUMINANCE_ALPHA:
+			case GL_LUMINANCE:
+			case GL_ALPHA:
+				switch(type)
+				{
+				case GL_UNSIGNED_BYTE:
+				case GL_FLOAT:            // GL_OES_texture_float
+				case GL_HALF_FLOAT_OES:   // GL_OES_texture_half_float
+					break;
+				default:
+					return GL_INVALID_OPERATION;
+				}
+				break;
+			case GL_DEPTH_COMPONENT:
+				switch(type)
+				{
+				case GL_UNSIGNED_SHORT:   // GL_OES_depth_texture
+				case GL_UNSIGNED_INT:     // GL_OES_depth_texture
+					break;
+				default:
+					return GL_INVALID_OPERATION;
+				}
+				break;
+			case GL_DEPTH_STENCIL_OES:
+				switch(type)
+				{
+				case GL_UNSIGNED_INT_24_8_OES:   // GL_OES_packed_depth_stencil
+					break;
+				default:
+					return GL_INVALID_OPERATION;
+				}
+				break;
+			case GL_RED_EXT:
+			case GL_RG_EXT:
+				switch(type)
+				{
+				case GL_UNSIGNED_BYTE:    // GL_EXT_texture_rg
+				case GL_FLOAT:            // GL_EXT_texture_rg + GL_OES_texture_float
+				case GL_HALF_FLOAT_OES:   // GL_EXT_texture_rg + GL_OES_texture_half_float
+					break;
+				default:
+					return GL_INVALID_OPERATION;
+				}
+				break;
+		//	case GL_BGRA_EXT:
+		//		if(type != GL_UNSIGNED_BYTE)   // GL_APPLE_texture_format_BGRA8888
+		//		{
+		//			return GL_INVALID_OPERATION;
+		//		}
+		//		break;
+			default:
+				UNREACHABLE(format);
+				return GL_INVALID_ENUM;
+			}
+
+			return GL_NONE;
+		}
+
 		// Validate format, type, and sized internalformat combinations [OpenGL ES 3.0 Table 3.2]
 		bool validSizedInternalformat = false;
 		#define VALIDATE_INTERNALFORMAT(...) { GLint validInternalformats[] = {__VA_ARGS__}; for(GLint v : validInternalformats) {if(internalformat == v) validSizedInternalformat = true;} } break;
@@ -1090,7 +1178,7 @@
 
 		#undef VALIDATE_INTERNALFORMAT
 
-		if((GLenum)internalformat != format && !validSizedInternalformat)
+		if(!validSizedInternalformat)
 		{
 			return GL_INVALID_OPERATION;
 		}