Re-order the error generation for ES3 TexImage calls.

Explictly check if the internal format enum is ever valid before checking
if it is valid in combination with other parameters. Some WebGL tests
expect a certain order for generated errors.

BUG=angleproject:2009
TEST=conformance2/textures/misc/tex-input-validation

Change-Id: I31166a78d00629f8281ef53eced72575497ae448
Reviewed-on: https://chromium-review.googlesource.com/486099
Commit-Queue: Geoff Lang <geofflang@chromium.org>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
diff --git a/src/libANGLE/formatutils.cpp b/src/libANGLE/formatutils.cpp
index 5274b4a..d0b5eb8 100644
--- a/src/libANGLE/formatutils.cpp
+++ b/src/libANGLE/formatutils.cpp
@@ -1892,6 +1892,12 @@
     }
 }
 
+bool ValidES3InternalFormat(GLenum internalFormat)
+{
+    const InternalFormatInfoMap &formatMap = GetInternalFormatMap();
+    return internalFormat != GL_NONE && formatMap.find(internalFormat) != formatMap.end();
+}
+
 VertexFormat::VertexFormat(GLenum typeIn, GLboolean normalizedIn, GLuint componentsIn, bool pureIntegerIn)
     : type(typeIn),
       normalized(normalizedIn),
diff --git a/src/libANGLE/formatutils.h b/src/libANGLE/formatutils.h
index 6abc3e1..c6a2cfb 100644
--- a/src/libANGLE/formatutils.h
+++ b/src/libANGLE/formatutils.h
@@ -305,6 +305,10 @@
 const VertexFormat &GetVertexFormatFromType(VertexFormatType vertexFormatType);
 size_t GetVertexFormatTypeSize(VertexFormatType vertexFormatType);
 
+// Check if an internal format is ever valid in ES3.  Makes no checks about support for a specific
+// context.
+bool ValidES3InternalFormat(GLenum internalFormat);
+
 // Implemented in format_map_autogen.cpp
 bool ValidES3Format(GLenum format);
 bool ValidES3Type(GLenum type);
diff --git a/src/libANGLE/validationES3.cpp b/src/libANGLE/validationES3.cpp
index 9070a15..7460d8d 100644
--- a/src/libANGLE/validationES3.cpp
+++ b/src/libANGLE/validationES3.cpp
@@ -32,9 +32,25 @@
 {
 
     // The type and format are valid if any supported internal format has that type and format
-    if (!ValidES3Format(format) || !ValidES3Type(type))
+    if (!ValidES3Format(format))
     {
-        context->handleError(Error(GL_INVALID_ENUM));
+        context->handleError(Error(GL_INVALID_ENUM, "Invalid format."));
+        return false;
+    }
+
+    if (!ValidES3Type(type))
+    {
+        context->handleError(Error(GL_INVALID_ENUM, "Invalid type."));
+        return false;
+    }
+
+    // For historical reasons, glTexImage2D and glTexImage3D pass in their internal format as a
+    // GLint instead of a GLenum. Therefor an invalid internal format gives a GL_INVALID_VALUE
+    // error instead of a GL_INVALID_ENUM error. As this validation function is only called in
+    // the validation codepaths for glTexImage2D/3D, we record a GL_INVALID_VALUE error.
+    if (!ValidES3InternalFormat(internalFormat))
+    {
+        context->handleError(Error(GL_INVALID_VALUE, "Invalid internalFormat."));
         return false;
     }
 
@@ -51,21 +67,18 @@
         return false;
     }
 
-    // For historical reasons, glTexImage2D and glTexImage3D pass in their internal format as a
-    // GLint instead of a GLenum. Therefor an invalid internal format gives a GL_INVALID_VALUE
-    // error instead of a GL_INVALID_ENUM error. As this validation function is only called in
-    // the validation codepaths for glTexImage2D/3D, we record a GL_INVALID_VALUE error.
-    const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat, type);
-    if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
-    {
-        context->handleError(Error(GL_INVALID_VALUE));
-        return false;
-    }
-
     // Check if this is a valid format combination to load texture data
     if (!ValidES3FormatCombination(format, type, internalFormat))
     {
-        context->handleError(Error(GL_INVALID_OPERATION));
+        context->handleError(
+            Error(GL_INVALID_OPERATION, "Invalid combination of format, type and internalFormat."));
+        return false;
+    }
+
+    const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat, type);
+    if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
+    {
+        context->handleError(Error(GL_INVALID_VALUE, "Unsupported internal format."));
         return false;
     }