• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
13 namespace gl
14 {
15 
TextureTargetToType(TextureTarget target)16 TextureType 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::VideoImage:
42             return TextureType::VideoImage;
43         case TextureTarget::InvalidEnum:
44             return TextureType::InvalidEnum;
45         default:
46             UNREACHABLE();
47             return TextureType::InvalidEnum;
48     }
49 }
50 
IsCubeMapFaceTarget(TextureTarget target)51 bool IsCubeMapFaceTarget(TextureTarget target)
52 {
53     return TextureTargetToType(target) == TextureType::CubeMap;
54 }
55 
NonCubeTextureTypeToTarget(TextureType type)56 TextureTarget NonCubeTextureTypeToTarget(TextureType type)
57 {
58     switch (type)
59     {
60         case TextureType::External:
61             return TextureTarget::External;
62         case TextureType::Rectangle:
63             return TextureTarget::Rectangle;
64         case TextureType::_2D:
65             return TextureTarget::_2D;
66         case TextureType::_2DArray:
67             return TextureTarget::_2DArray;
68         case TextureType::_2DMultisample:
69             return TextureTarget::_2DMultisample;
70         case TextureType::_2DMultisampleArray:
71             return TextureTarget::_2DMultisampleArray;
72         case TextureType::_3D:
73             return TextureTarget::_3D;
74         case TextureType::VideoImage:
75             return TextureTarget::VideoImage;
76         default:
77             UNREACHABLE();
78             return TextureTarget::InvalidEnum;
79     }
80 }
81 
82 // Check that we can do arithmetic on TextureTarget to convert from / to cube map faces
83 static_assert(static_cast<uint8_t>(TextureTarget::CubeMapNegativeX) -
84                       static_cast<uint8_t>(TextureTarget::CubeMapPositiveX) ==
85                   1u,
86               "");
87 static_assert(static_cast<uint8_t>(TextureTarget::CubeMapPositiveY) -
88                       static_cast<uint8_t>(TextureTarget::CubeMapPositiveX) ==
89                   2u,
90               "");
91 static_assert(static_cast<uint8_t>(TextureTarget::CubeMapNegativeY) -
92                       static_cast<uint8_t>(TextureTarget::CubeMapPositiveX) ==
93                   3u,
94               "");
95 static_assert(static_cast<uint8_t>(TextureTarget::CubeMapPositiveZ) -
96                       static_cast<uint8_t>(TextureTarget::CubeMapPositiveX) ==
97                   4u,
98               "");
99 static_assert(static_cast<uint8_t>(TextureTarget::CubeMapNegativeZ) -
100                       static_cast<uint8_t>(TextureTarget::CubeMapPositiveX) ==
101                   5u,
102               "");
103 
CubeFaceIndexToTextureTarget(size_t face)104 TextureTarget CubeFaceIndexToTextureTarget(size_t face)
105 {
106     ASSERT(face < 6u);
107     return static_cast<TextureTarget>(static_cast<uint8_t>(TextureTarget::CubeMapPositiveX) + face);
108 }
109 
CubeMapTextureTargetToFaceIndex(TextureTarget target)110 size_t CubeMapTextureTargetToFaceIndex(TextureTarget target)
111 {
112     ASSERT(IsCubeMapFaceTarget(target));
113     return static_cast<uint8_t>(target) - static_cast<uint8_t>(TextureTarget::CubeMapPositiveX);
114 }
115 
SamplerTypeToTextureType(GLenum samplerType)116 TextureType SamplerTypeToTextureType(GLenum samplerType)
117 {
118     switch (samplerType)
119     {
120         case GL_SAMPLER_2D:
121         case GL_INT_SAMPLER_2D:
122         case GL_UNSIGNED_INT_SAMPLER_2D:
123         case GL_SAMPLER_2D_SHADOW:
124             return TextureType::_2D;
125 
126         case GL_SAMPLER_EXTERNAL_OES:
127             return TextureType::External;
128 
129         case GL_SAMPLER_CUBE:
130         case GL_INT_SAMPLER_CUBE:
131         case GL_UNSIGNED_INT_SAMPLER_CUBE:
132         case GL_SAMPLER_CUBE_SHADOW:
133             return TextureType::CubeMap;
134 
135         case GL_SAMPLER_2D_ARRAY:
136         case GL_INT_SAMPLER_2D_ARRAY:
137         case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
138         case GL_SAMPLER_2D_ARRAY_SHADOW:
139             return TextureType::_2DArray;
140 
141         case GL_SAMPLER_3D:
142         case GL_INT_SAMPLER_3D:
143         case GL_UNSIGNED_INT_SAMPLER_3D:
144             return TextureType::_3D;
145 
146         case GL_SAMPLER_2D_MULTISAMPLE:
147         case GL_INT_SAMPLER_2D_MULTISAMPLE:
148         case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
149             return TextureType::_2DMultisample;
150 
151         case GL_SAMPLER_2D_MULTISAMPLE_ARRAY:
152         case GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
153         case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
154             return TextureType::_2DMultisampleArray;
155 
156         case GL_SAMPLER_2D_RECT_ANGLE:
157             return TextureType::Rectangle;
158 
159         case GL_SAMPLER_VIDEO_IMAGE_WEBGL:
160             return TextureType::VideoImage;
161 
162         default:
163             UNREACHABLE();
164             return TextureType::InvalidEnum;
165     }
166 }
167 
IsMultisampled(TextureType type)168 bool IsMultisampled(TextureType type)
169 {
170     switch (type)
171     {
172         case TextureType::_2DMultisample:
173         case TextureType::_2DMultisampleArray:
174             return true;
175         default:
176             return false;
177     }
178 }
179 
IsArrayTextureType(TextureType type)180 bool IsArrayTextureType(TextureType type)
181 {
182     switch (type)
183     {
184         case TextureType::_2DArray:
185         case TextureType::_2DMultisampleArray:
186             return true;
187         default:
188             return false;
189     }
190 }
191 
operator <<(std::ostream & os,PrimitiveMode value)192 std::ostream &operator<<(std::ostream &os, PrimitiveMode value)
193 {
194     switch (value)
195     {
196         case PrimitiveMode::LineLoop:
197             os << "GL_LINE_LOOP";
198             break;
199         case PrimitiveMode::Lines:
200             os << "GL_LINES";
201             break;
202         case PrimitiveMode::LinesAdjacency:
203             os << "GL_LINES_ADJACENCY";
204             break;
205         case PrimitiveMode::LineStrip:
206             os << "GL_LINE_STRIP";
207             break;
208         case PrimitiveMode::LineStripAdjacency:
209             os << "GL_LINE_STRIP_ADJANCENCY";
210             break;
211         case PrimitiveMode::Points:
212             os << "GL_POINTS";
213             break;
214         case PrimitiveMode::TriangleFan:
215             os << "GL_TRIANGLE_FAN";
216             break;
217         case PrimitiveMode::Triangles:
218             os << "GL_TRIANGLES";
219             break;
220         case PrimitiveMode::TrianglesAdjacency:
221             os << "GL_TRIANGLES_ADJANCENCY";
222             break;
223         case PrimitiveMode::TriangleStrip:
224             os << "GL_TRIANGLE_STRIP";
225             break;
226         case PrimitiveMode::TriangleStripAdjacency:
227             os << "GL_TRIANGLE_STRIP_ADJACENCY";
228             break;
229         default:
230             os << "GL_INVALID_ENUM";
231             break;
232     }
233     return os;
234 }
235 
operator <<(std::ostream & os,DrawElementsType value)236 std::ostream &operator<<(std::ostream &os, DrawElementsType value)
237 {
238     switch (value)
239     {
240         case DrawElementsType::UnsignedByte:
241             os << "GL_UNSIGNED_BYTE";
242             break;
243         case DrawElementsType::UnsignedShort:
244             os << "GL_UNSIGNED_SHORT";
245             break;
246         case DrawElementsType::UnsignedInt:
247             os << "GL_UNSIGNED_INT";
248             break;
249         default:
250             os << "GL_INVALID_ENUM";
251             break;
252     }
253 
254     return os;
255 }
256 
operator <<(std::ostream & os,BlendEquationType value)257 std::ostream &operator<<(std::ostream &os, BlendEquationType value)
258 {
259     switch (value)
260     {
261         case BlendEquationType::Add:
262             os << "GL_FUNC_ADD";
263             break;
264         case BlendEquationType::Min:
265             os << "GL_MIN";
266             break;
267         case BlendEquationType::Max:
268             os << "GL_MAX";
269             break;
270         case BlendEquationType::Subtract:
271             os << "GL_FUNC_SUBTRACT";
272             break;
273         case BlendEquationType::ReverseSubtract:
274             os << "GL_FUNC_REVERSE_SUBTRACT";
275             break;
276         default:
277             os << "GL_INVALID_ENUM";
278             break;
279     }
280 
281     return os;
282 }
283 
operator <<(std::ostream & os,BlendFactorType value)284 std::ostream &operator<<(std::ostream &os, BlendFactorType value)
285 {
286     switch (value)
287     {
288         case BlendFactorType::Zero:
289             os << "GL_ZERO";
290             break;
291         case BlendFactorType::One:
292             os << "GL_ONE";
293             break;
294         case BlendFactorType::SrcColor:
295             os << "GL_SRC_COLOR";
296             break;
297         case BlendFactorType::OneMinusSrcColor:
298             os << "GL_ONE_MINUS_SRC_COLOR";
299             break;
300         case BlendFactorType::SrcAlpha:
301             os << "GL_SRC_ALPHA";
302             break;
303         case BlendFactorType::OneMinusSrcAlpha:
304             os << "GL_ONE_MINUS_SRC_ALPHA";
305             break;
306         case BlendFactorType::DstAlpha:
307             os << "GL_DST_ALPHA";
308             break;
309         case BlendFactorType::OneMinusDstAlpha:
310             os << "GL_ONE_MINUS_DST_ALPHA";
311             break;
312         case BlendFactorType::DstColor:
313             os << "GL_DST_COLOR";
314             break;
315         case BlendFactorType::OneMinusDstColor:
316             os << "GL_ONE_MINUS_DST_COLOR";
317             break;
318         case BlendFactorType::SrcAlphaSaturate:
319             os << "GL_SRC_ALPHA_SATURATE";
320             break;
321         case BlendFactorType::ConstantColor:
322             os << "GL_CONSTANT_COLOR";
323             break;
324         case BlendFactorType::OneMinusConstantColor:
325             os << "GL_ONE_MINUS_CONSTANT_COLOR";
326             break;
327         case BlendFactorType::ConstantAlpha:
328             os << "GL_CONSTANT_ALPHA";
329             break;
330         case BlendFactorType::OneMinusConstantAlpha:
331             os << "GL_ONE_MINUS_CONSTANT_ALPHA";
332             break;
333         case BlendFactorType::Src1Alpha:
334             os << "GL_SRC1_ALPHA_EXT";
335             break;
336         case BlendFactorType::Src1Color:
337             os << "GL_SRC1_COLOR_EXT";
338             break;
339         case BlendFactorType::OneMinusSrc1Color:
340             os << "GL_ONE_MINUS_SRC1_COLOR_EXT";
341             break;
342         case BlendFactorType::OneMinusSrc1Alpha:
343             os << "GL_ONE_MINUS_SRC1_ALPHA_EXT";
344             break;
345         default:
346             os << "GL_INVALID_ENUM";
347             break;
348     }
349 
350     return os;
351 }
352 
operator <<(std::ostream & os,VertexAttribType value)353 std::ostream &operator<<(std::ostream &os, VertexAttribType value)
354 {
355     switch (value)
356     {
357         case VertexAttribType::Byte:
358             os << "GL_BYTE";
359             break;
360         case VertexAttribType::Fixed:
361             os << "GL_FIXED";
362             break;
363         case VertexAttribType::Float:
364             os << "GL_FLOAT";
365             break;
366         case VertexAttribType::HalfFloat:
367             os << "GL_HALF_FLOAT";
368             break;
369         case VertexAttribType::Int:
370             os << "GL_INT";
371             break;
372         case VertexAttribType::Int2101010:
373             os << "GL_INT_10_10_10_2";
374             break;
375         case VertexAttribType::Int1010102:
376             os << "GL_INT_10_10_10_2_OES";
377             break;
378         case VertexAttribType::Short:
379             os << "GL_SHORT";
380             break;
381         case VertexAttribType::UnsignedByte:
382             os << "GL_UNSIGNED_BYTE";
383             break;
384         case VertexAttribType::UnsignedInt:
385             os << "GL_UNSIGNED_INT";
386             break;
387         case VertexAttribType::UnsignedInt2101010:
388             os << "GL_UNSIGNED_INT_10_10_10_2";
389             break;
390         case VertexAttribType::UnsignedInt1010102:
391             os << "GL_UNSIGNED_INT_10_10_10_2_OES";
392             break;
393         case VertexAttribType::UnsignedShort:
394             os << "GL_UNSIGNED_SHORT";
395             break;
396         default:
397             os << "GL_INVALID_ENUM";
398             break;
399     }
400     return os;
401 }
402 }  // namespace gl
403 
404 namespace egl
405 {
ErrorCodeToMessageType(EGLint errorCode)406 MessageType ErrorCodeToMessageType(EGLint errorCode)
407 {
408     switch (errorCode)
409     {
410         case EGL_BAD_ALLOC:
411         case EGL_CONTEXT_LOST:
412         case EGL_NOT_INITIALIZED:
413             return MessageType::Critical;
414 
415         case EGL_BAD_ACCESS:
416         case EGL_BAD_ATTRIBUTE:
417         case EGL_BAD_CONFIG:
418         case EGL_BAD_CONTEXT:
419         case EGL_BAD_CURRENT_SURFACE:
420         case EGL_BAD_DISPLAY:
421         case EGL_BAD_MATCH:
422         case EGL_BAD_NATIVE_PIXMAP:
423         case EGL_BAD_NATIVE_WINDOW:
424         case EGL_BAD_PARAMETER:
425         case EGL_BAD_SURFACE:
426         case EGL_BAD_STREAM_KHR:
427         case EGL_BAD_STATE_KHR:
428         case EGL_BAD_DEVICE_EXT:
429             return MessageType::Error;
430 
431         case EGL_SUCCESS:
432         default:
433             UNREACHABLE();
434             return MessageType::InvalidEnum;
435     }
436 }
437 }  // namespace egl
438 
439 namespace egl_gl
440 {
441 
EGLCubeMapTargetToCubeMapTarget(EGLenum eglTarget)442 gl::TextureTarget EGLCubeMapTargetToCubeMapTarget(EGLenum eglTarget)
443 {
444     ASSERT(egl::IsCubeMapTextureTarget(eglTarget));
445     return gl::CubeFaceIndexToTextureTarget(egl::CubeMapTextureTargetToLayerIndex(eglTarget));
446 }
447 
EGLImageTargetToTextureTarget(EGLenum eglTarget)448 gl::TextureTarget EGLImageTargetToTextureTarget(EGLenum eglTarget)
449 {
450     switch (eglTarget)
451     {
452         case EGL_GL_TEXTURE_2D_KHR:
453             return gl::TextureTarget::_2D;
454 
455         case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
456         case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
457         case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
458         case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
459         case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
460         case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
461             return EGLCubeMapTargetToCubeMapTarget(eglTarget);
462 
463         case EGL_GL_TEXTURE_3D_KHR:
464             return gl::TextureTarget::_3D;
465 
466         default:
467             UNREACHABLE();
468             return gl::TextureTarget::InvalidEnum;
469     }
470 }
471 
EGLTextureTargetToTextureType(EGLenum eglTarget)472 gl::TextureType EGLTextureTargetToTextureType(EGLenum eglTarget)
473 {
474     switch (eglTarget)
475     {
476         case EGL_TEXTURE_2D:
477             return gl::TextureType::_2D;
478 
479         case EGL_TEXTURE_RECTANGLE_ANGLE:
480             return gl::TextureType::Rectangle;
481 
482         default:
483             UNREACHABLE();
484             return gl::TextureType::InvalidEnum;
485     }
486 }
487 
488 }  // namespace egl_gl
489