Support more glCopyTexImage unsized formats.

Convert packed types to the next greater non-packed component type.
Specifically, when the red component has fewer than 8 bits, use the
corresponding sized internal format with 8-bit components.

Note that this is still not fully compliant.
The OpenGL ES 3.0 spec section 3.8.5 states that:

If internalformat is unsized, the internal format of the new texel array is determined
by the following rules, applied in order. If an effective internal format exists
that has
1. the same component sizes as,
2. component sizes greater than or equal to, or
3. component sizes smaller than or equal to
those of the source buffer’s effective internal format (for all matching components
in internalformat), that format is chosen for the new image array, and this is also
the new texel array’s effective internal format.

This means that in theory when copying an RGBA4 framebuffer into an RGB
texture, the effective internal format should be RGB565, not RGB8.
However, dEQP does not enforce this, and ANGLE always assumes
UNSIGNED_BYTE components.

Change-Id: Id243b963779108e205c275499ddfb6e041a873d6
Reviewed-on: https://swiftshader-review.googlesource.com/17969
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 cfd17a9..49eae83 100644
--- a/src/OpenGL/libGLES_CM/libGLES_CM.cpp
+++ b/src/OpenGL/libGLES_CM/libGLES_CM.cpp
@@ -842,12 +842,13 @@
 			return error(GL_INVALID_FRAMEBUFFER_OPERATION_OES);
 		}
 
-		if(context->getFramebufferName() != 0 && framebuffer->getColorbuffer()->getSamples() > 1)
+		es1::Renderbuffer *source = framebuffer->getColorbuffer();
+
+		if(!source || source->getSamples() > 1)
 		{
 			return error(GL_INVALID_OPERATION);
 		}
 
-		es1::Renderbuffer *source = framebuffer->getColorbuffer();
 		GLenum colorbufferFormat = source->getFormat();
 
 		// [OpenGL ES 1.1.12] table 3.9
@@ -902,7 +903,7 @@
 		{
 			internalformat = colorbufferFormat;
 		}
-		else if(GetRedSize(colorbufferFormat) == 8)
+		else if(GetRedSize(colorbufferFormat) <= 8)
 		{
 			internalformat = gl::GetSizedInternalFormat(internalformat, GL_UNSIGNED_BYTE);
 		}
diff --git a/src/OpenGL/libGLESv2/libGLESv2.cpp b/src/OpenGL/libGLESv2/libGLESv2.cpp
index 56b2d06..8fb198f 100644
--- a/src/OpenGL/libGLESv2/libGLESv2.cpp
+++ b/src/OpenGL/libGLESv2/libGLESv2.cpp
@@ -977,10 +977,25 @@
 			{
 				internalformat = colorbufferFormat;
 			}
-			else if(GetRedSize(colorbufferFormat) == 8)
+			else if(GetColorComponentType(colorbufferFormat) == GL_UNSIGNED_NORMALIZED && GetRedSize(colorbufferFormat) <= 8)
 			{
+				// TODO: Convert to the smallest format that fits all components.
+				// e.g. Copying RGBA4 to RGB should result in RGB565, not RGB8.
+
 				internalformat = gl::GetSizedInternalFormat(internalformat, GL_UNSIGNED_BYTE);
 			}
+			else if(GetColorComponentType(colorbufferFormat) == GL_INT)
+			{
+				internalformat = gl::GetSizedInternalFormat(internalformat, GL_INT);
+			}
+			else if(GetColorComponentType(colorbufferFormat) == GL_UNSIGNED_INT)
+			{
+				internalformat = gl::GetSizedInternalFormat(internalformat, GL_UNSIGNED_INT);
+			}
+			else if(GetColorComponentType(colorbufferFormat) == GL_FLOAT && GetRedSize(colorbufferFormat) == 16)   // GL_EXT_color_buffer_half_float
+			{
+				internalformat = gl::GetSizedInternalFormat(internalformat, GL_HALF_FLOAT_OES);
+			}
 			else
 			{
 				UNIMPLEMENTED();