xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/gl/gl_texture_helper.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/lite/delegates/gpu/gl/gl_texture_helper.h"
17 
18 #include "tensorflow/lite/delegates/gpu/common/data_type.h"
19 #include "tensorflow/lite/delegates/gpu/gl/portable_gl31.h"
20 
21 namespace tflite {
22 namespace gpu {
23 namespace gl {
24 
ToTextureFormat(DataType type,bool normalized)25 GLenum ToTextureFormat(DataType type, bool normalized) {
26   switch (type) {
27     case DataType::INT8:
28     case DataType::UINT8:
29       return normalized ? GL_RGBA : GL_RGBA_INTEGER;
30     case DataType::BOOL:
31       return GL_RGBA_INTEGER;
32     case DataType::UINT16:
33     case DataType::UINT32:
34     case DataType::INT16:
35     case DataType::INT32:
36       return GL_RGBA_INTEGER;
37     case DataType::FLOAT16:
38     case DataType::FLOAT32:
39       return GL_RGBA;
40     default:
41       return 0;
42   }
43 }
44 
ToTextureInternalFormat(DataType type,bool normalized)45 GLenum ToTextureInternalFormat(DataType type, bool normalized) {
46   switch (type) {
47     case DataType::UINT8:
48       return normalized ? GL_RGBA8 : GL_RGBA8UI;
49     case DataType::BOOL:
50       return GL_RGBA8UI;
51     case DataType::INT8:
52       return normalized ? GL_RGBA8_SNORM : GL_RGBA8I;
53     case DataType::UINT16:
54       return GL_RGBA16UI;
55     case DataType::UINT32:
56       return GL_RGBA32UI;
57     case DataType::INT16:
58       return GL_RGBA16I;
59     case DataType::INT32:
60       return GL_RGBA32I;
61     case DataType::FLOAT16:
62       return GL_RGBA16F;
63     case DataType::FLOAT32:
64       return GL_RGBA32F;
65     default:
66       return 0;
67   }
68 }
69 
ToTextureDataType(DataType type)70 GLenum ToTextureDataType(DataType type) {
71   switch (type) {
72     case DataType::UINT8:
73       return GL_UNSIGNED_BYTE;
74     case DataType::BOOL:
75       return GL_UNSIGNED_BYTE;
76     case DataType::INT8:
77       return GL_BYTE;
78     case DataType::UINT16:
79       return GL_UNSIGNED_SHORT;
80     case DataType::UINT32:
81       return GL_UNSIGNED_INT;
82     case DataType::INT16:
83       return GL_SHORT;
84     case DataType::INT32:
85       return GL_INT;
86     case DataType::FLOAT16:
87       return GL_HALF_FLOAT;
88     case DataType::FLOAT32:
89       return GL_FLOAT;
90     default:
91       return 0;
92   }
93 }
94 
95 }  // namespace gl
96 }  // namespace gpu
97 }  // namespace tflite
98