ToTextureFormat and ToTextureInternalFormat clarified for DataType::UINT8

PiperOrigin-RevId: 449160732
diff --git a/tensorflow/lite/delegates/gpu/gl/gl_texture.cc b/tensorflow/lite/delegates/gpu/gl/gl_texture.cc
index 3fb230b..7edc840 100644
--- a/tensorflow/lite/delegates/gpu/gl/gl_texture.cc
+++ b/tensorflow/lite/delegates/gpu/gl/gl_texture.cc
@@ -129,8 +129,9 @@
         "expected dimensions.");
   }
   const GLenum kTarget = GL_TEXTURE_2D;
-  GLenum internal_format = ToTextureInternalFormat(data_type);
-  GLenum format = ToTextureFormat(data_type);
+  const bool normalized = data_type == DataType::UINT8;
+  GLenum internal_format = ToTextureInternalFormat(data_type, normalized);
+  GLenum format = ToTextureFormat(data_type, normalized);
   GLenum type = ToTextureDataType(data_type);
   gl_texture_internal::TextureId id;
   gl_texture_internal::TextureBinder binder(kTarget, id.id());
@@ -156,8 +157,9 @@
         "product.");
   }
   const GLenum kTarget = GL_TEXTURE_2D_ARRAY;
-  GLenum internal_format = ToTextureInternalFormat(data_type);
-  GLenum format = ToTextureFormat(data_type);
+  const bool normalized = data_type == DataType::UINT8;
+  GLenum internal_format = ToTextureInternalFormat(data_type, normalized);
+  GLenum format = ToTextureFormat(data_type, normalized);
   GLenum type = ToTextureDataType(data_type);
   gl_texture_internal::TextureId id;
   gl_texture_internal::TextureBinder binder(kTarget, id.id());
@@ -219,7 +221,8 @@
                                              const uint2& size,
                                              GlTexture* gl_texture) {
   const GLenum kTarget = GL_TEXTURE_2D;
-  const GLenum internal_format = ToTextureInternalFormat(data_type);
+  const bool normalized = data_type == DataType::UINT8;
+  const GLenum internal_format = ToTextureInternalFormat(data_type, normalized);
   gl_texture_internal::TextureId id;
   gl_texture_internal::TextureBinder binder(kTarget, id.id());
   RETURN_IF_ERROR(SetTextureWrapAndFilter(kTarget, internal_format));
@@ -237,7 +240,8 @@
                                              const uint3& size,
                                              GlTexture* gl_texture) {
   const GLenum kTarget = GL_TEXTURE_2D_ARRAY;
-  GLenum internal_format = ToTextureInternalFormat(data_type);
+  const bool normalized = data_type == DataType::UINT8;
+  GLenum internal_format = ToTextureInternalFormat(data_type, normalized);
   gl_texture_internal::TextureId id;
   gl_texture_internal::TextureBinder binder(kTarget, id.id());
   RETURN_IF_ERROR(SetTextureWrapAndFilter(kTarget, internal_format));
diff --git a/tensorflow/lite/delegates/gpu/gl/gl_texture_helper.cc b/tensorflow/lite/delegates/gpu/gl/gl_texture_helper.cc
index 0a173de..48ec5c2 100644
--- a/tensorflow/lite/delegates/gpu/gl/gl_texture_helper.cc
+++ b/tensorflow/lite/delegates/gpu/gl/gl_texture_helper.cc
@@ -22,9 +22,11 @@
 namespace gpu {
 namespace gl {
 
-GLenum ToTextureFormat(DataType type) {
+GLenum ToTextureFormat(DataType type, bool normalized) {
   switch (type) {
     case DataType::INT8:
+    case DataType::UINT8:
+      return normalized ? GL_RGBA : GL_RGBA_INTEGER;
     case DataType::UINT16:
     case DataType::UINT32:
     case DataType::INT16:
@@ -32,19 +34,18 @@
       return GL_RGBA_INTEGER;
     case DataType::FLOAT16:
     case DataType::FLOAT32:
-    case DataType::UINT8:  // this requires GL_RGBA8 internal format
       return GL_RGBA;
     default:
       return 0;
   }
 }
 
-GLenum ToTextureInternalFormat(DataType type) {
+GLenum ToTextureInternalFormat(DataType type, bool normalized) {
   switch (type) {
     case DataType::UINT8:
-      return GL_RGBA8;  // this requires GL_RGBA format
+      return normalized ? GL_RGBA8 : GL_RGBA8UI;
     case DataType::INT8:
-      return GL_RGBA8I;
+      return normalized ? GL_RGBA8_SNORM : GL_RGBA8I;
     case DataType::UINT16:
       return GL_RGBA16UI;
     case DataType::UINT32:
diff --git a/tensorflow/lite/delegates/gpu/gl/gl_texture_helper.h b/tensorflow/lite/delegates/gpu/gl/gl_texture_helper.h
index 6de4341..e520de3 100644
--- a/tensorflow/lite/delegates/gpu/gl/gl_texture_helper.h
+++ b/tensorflow/lite/delegates/gpu/gl/gl_texture_helper.h
@@ -23,9 +23,15 @@
 namespace gpu {
 namespace gl {
 
-GLenum ToTextureFormat(DataType type);
+// From https://www.khronos.org/opengl/wiki/Normalized_Integer
+// A Normalized Integer is an integer which is used to store a decimal floating
+// point number. When formats use such an integer, OpenGL will automatically
+// convert them to/from floating point values as needed. This allows normalized
+// integers to be treated equivalently with floating-point values, acting as a
+// form of compression.
+GLenum ToTextureFormat(DataType type, bool normalized = false);
 
-GLenum ToTextureInternalFormat(DataType type);
+GLenum ToTextureInternalFormat(DataType type, bool normalized = false);
 
 GLenum ToTextureDataType(DataType type);