1// Copyright 2018 The ANGLE Project Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4//
5// PackedGLEnums.cpp:
6// Declares ANGLE-specific enums classes for GLEnum and functions operating
7// on them.
8
9#include "common/PackedEnums.h"
10
11#include "common/utilities.h"
12
13namespace gl
14{
15
16TextureType TextureTargetToType(TextureTarget target)
17{
18 switch (target)
19 {
20 case TextureTarget::CubeMapNegativeX:
21 case TextureTarget::CubeMapNegativeY:
22 case TextureTarget::CubeMapNegativeZ:
23 case TextureTarget::CubeMapPositiveX:
24 case TextureTarget::CubeMapPositiveY:
25 case TextureTarget::CubeMapPositiveZ:
26 return TextureType::CubeMap;
27 case TextureTarget::External:
28 return TextureType::External;
29 case TextureTarget::Rectangle:
30 return TextureType::Rectangle;
31 case TextureTarget::_2D:
32 return TextureType::_2D;
33 case TextureTarget::_2DArray:
34 return TextureType::_2DArray;
35 case TextureTarget::_2DMultisample:
36 return TextureType::_2DMultisample;
37 case TextureTarget::_2DMultisampleArray:
38 return TextureType::_2DMultisampleArray;
39 case TextureTarget::_3D:
40 return TextureType::_3D;
41 case TextureTarget::InvalidEnum:
42 return TextureType::InvalidEnum;
43 default:
44 UNREACHABLE();
45 return TextureType::InvalidEnum;
46 }
47}
48
49bool IsCubeMapFaceTarget(TextureTarget target)
50{
51 return TextureTargetToType(target) == TextureType::CubeMap;
52}
53
54TextureTarget NonCubeTextureTypeToTarget(TextureType type)
55{
56 switch (type)
57 {
58 case TextureType::External:
59 return TextureTarget::External;
60 case TextureType::Rectangle:
61 return TextureTarget::Rectangle;
62 case TextureType::_2D:
63 return TextureTarget::_2D;
64 case TextureType::_2DArray:
65 return TextureTarget::_2DArray;
66 case TextureType::_2DMultisample:
67 return TextureTarget::_2DMultisample;
68 case TextureType::_2DMultisampleArray:
69 return TextureTarget::_2DMultisampleArray;
70 case TextureType::_3D:
71 return TextureTarget::_3D;
72 default:
73 UNREACHABLE();
74 return TextureTarget::InvalidEnum;
75 }
76}
77
78// Check that we can do arithmetic on TextureTarget to convert from / to cube map faces
79static_assert(static_cast<uint8_t>(TextureTarget::CubeMapNegativeX) -
80 static_cast<uint8_t>(TextureTarget::CubeMapPositiveX) ==
81 1u,
82 "");
83static_assert(static_cast<uint8_t>(TextureTarget::CubeMapPositiveY) -
84 static_cast<uint8_t>(TextureTarget::CubeMapPositiveX) ==
85 2u,
86 "");
87static_assert(static_cast<uint8_t>(TextureTarget::CubeMapNegativeY) -
88 static_cast<uint8_t>(TextureTarget::CubeMapPositiveX) ==
89 3u,
90 "");
91static_assert(static_cast<uint8_t>(TextureTarget::CubeMapPositiveZ) -
92 static_cast<uint8_t>(TextureTarget::CubeMapPositiveX) ==
93 4u,
94 "");
95static_assert(static_cast<uint8_t>(TextureTarget::CubeMapNegativeZ) -
96 static_cast<uint8_t>(TextureTarget::CubeMapPositiveX) ==
97 5u,
98 "");
99
100TextureTarget CubeFaceIndexToTextureTarget(size_t face)
101{
102 ASSERT(face < 6u);
103 return static_cast<TextureTarget>(static_cast<uint8_t>(TextureTarget::CubeMapPositiveX) + face);
104}
105
106size_t CubeMapTextureTargetToFaceIndex(TextureTarget target)
107{
108 ASSERT(IsCubeMapFaceTarget(target));
109 return static_cast<uint8_t>(target) - static_cast<uint8_t>(TextureTarget::CubeMapPositiveX);
110}
111
112TextureType SamplerTypeToTextureType(GLenum samplerType)
113{
114 switch (samplerType)
115 {
116 case GL_SAMPLER_2D:
117 case GL_INT_SAMPLER_2D:
118 case GL_UNSIGNED_INT_SAMPLER_2D:
119 case GL_SAMPLER_2D_SHADOW:
120 return TextureType::_2D;
121
122 case GL_SAMPLER_EXTERNAL_OES:
123 return TextureType::External;
124
125 case GL_SAMPLER_CUBE:
126 case GL_INT_SAMPLER_CUBE:
127 case GL_UNSIGNED_INT_SAMPLER_CUBE:
128 case GL_SAMPLER_CUBE_SHADOW:
129 return TextureType::CubeMap;
130
131 case GL_SAMPLER_2D_ARRAY:
132 case GL_INT_SAMPLER_2D_ARRAY:
133 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
134 case GL_SAMPLER_2D_ARRAY_SHADOW:
135 return TextureType::_2DArray;
136
137 case GL_SAMPLER_3D:
138 case GL_INT_SAMPLER_3D:
139 case GL_UNSIGNED_INT_SAMPLER_3D:
140 return TextureType::_3D;
141
142 case GL_SAMPLER_2D_MULTISAMPLE:
143 case GL_INT_SAMPLER_2D_MULTISAMPLE:
144 case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
145 return TextureType::_2DMultisample;
146
147 case GL_SAMPLER_2D_MULTISAMPLE_ARRAY:
148 case GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
149 case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
150 return TextureType::_2DMultisampleArray;
151
152 case GL_SAMPLER_2D_RECT_ANGLE:
153 return TextureType::Rectangle;
154
155 default:
156 UNREACHABLE();
157 return TextureType::InvalidEnum;
158 }
159}
160
161bool IsMultisampled(gl::TextureType type)
162{
163 switch (type)
164 {
165 case gl::TextureType::_2DMultisample:
166 case gl::TextureType::_2DMultisampleArray:
167 return true;
168 default:
169 return false;
170 }
171}
172
173} // namespace gl
174
175namespace egl
176{
177MessageType ErrorCodeToMessageType(EGLint errorCode)
178{
179 switch (errorCode)
180 {
181 case EGL_BAD_ALLOC:
182 case EGL_CONTEXT_LOST:
183 case EGL_NOT_INITIALIZED:
184 return MessageType::Critical;
185
186 case EGL_BAD_ACCESS:
187 case EGL_BAD_ATTRIBUTE:
188 case EGL_BAD_CONFIG:
189 case EGL_BAD_CONTEXT:
190 case EGL_BAD_CURRENT_SURFACE:
191 case EGL_BAD_DISPLAY:
192 case EGL_BAD_MATCH:
193 case EGL_BAD_NATIVE_PIXMAP:
194 case EGL_BAD_NATIVE_WINDOW:
195 case EGL_BAD_PARAMETER:
196 case EGL_BAD_SURFACE:
197 case EGL_BAD_STREAM_KHR:
198 case EGL_BAD_STATE_KHR:
199 case EGL_BAD_DEVICE_EXT:
200 return MessageType::Error;
201
202 case EGL_SUCCESS:
203 default:
204 UNREACHABLE();
205 return MessageType::InvalidEnum;
206 }
207}
208} // namespace egl
209
210namespace egl_gl
211{
212
213gl::TextureTarget EGLCubeMapTargetToCubeMapTarget(EGLenum eglTarget)
214{
215 ASSERT(egl::IsCubeMapTextureTarget(eglTarget));
216 return gl::CubeFaceIndexToTextureTarget(egl::CubeMapTextureTargetToLayerIndex(eglTarget));
217}
218
219gl::TextureTarget EGLImageTargetToTextureTarget(EGLenum eglTarget)
220{
221 switch (eglTarget)
222 {
223 case EGL_GL_TEXTURE_2D_KHR:
224 return gl::TextureTarget::_2D;
225
226 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
227 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
228 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
229 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
230 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
231 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
232 return EGLCubeMapTargetToCubeMapTarget(eglTarget);
233
234 case EGL_GL_TEXTURE_3D_KHR:
235 return gl::TextureTarget::_3D;
236
237 default:
238 UNREACHABLE();
239 return gl::TextureTarget::InvalidEnum;
240 }
241}
242
243gl::TextureType EGLTextureTargetToTextureType(EGLenum eglTarget)
244{
245 switch (eglTarget)
246 {
247 case EGL_TEXTURE_2D:
248 return gl::TextureType::_2D;
249
250 case EGL_TEXTURE_RECTANGLE_ANGLE:
251 return gl::TextureType::Rectangle;
252
253 default:
254 UNREACHABLE();
255 return gl::TextureType::InvalidEnum;
256 }
257}
258
259} // namespace egl_gl
260