• 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::CubeMapArray:
28             return TextureType::CubeMapArray;
29         case TextureTarget::External:
30             return TextureType::External;
31         case TextureTarget::Rectangle:
32             return TextureType::Rectangle;
33         case TextureTarget::_2D:
34             return TextureType::_2D;
35         case TextureTarget::_2DArray:
36             return TextureType::_2DArray;
37         case TextureTarget::_2DMultisample:
38             return TextureType::_2DMultisample;
39         case TextureTarget::_2DMultisampleArray:
40             return TextureType::_2DMultisampleArray;
41         case TextureTarget::_3D:
42             return TextureType::_3D;
43         case TextureTarget::VideoImage:
44             return TextureType::VideoImage;
45         case TextureTarget::Buffer:
46             return TextureType::Buffer;
47         case TextureTarget::InvalidEnum:
48             return TextureType::InvalidEnum;
49         default:
50             UNREACHABLE();
51             return TextureType::InvalidEnum;
52     }
53 }
54 
IsCubeMapFaceTarget(TextureTarget target)55 bool IsCubeMapFaceTarget(TextureTarget target)
56 {
57     return TextureTargetToType(target) == TextureType::CubeMap;
58 }
59 
NonCubeTextureTypeToTarget(TextureType type)60 TextureTarget NonCubeTextureTypeToTarget(TextureType type)
61 {
62     switch (type)
63     {
64         case TextureType::External:
65             return TextureTarget::External;
66         case TextureType::Rectangle:
67             return TextureTarget::Rectangle;
68         case TextureType::_2D:
69             return TextureTarget::_2D;
70         case TextureType::_2DArray:
71             return TextureTarget::_2DArray;
72         case TextureType::_2DMultisample:
73             return TextureTarget::_2DMultisample;
74         case TextureType::_2DMultisampleArray:
75             return TextureTarget::_2DMultisampleArray;
76         case TextureType::_3D:
77             return TextureTarget::_3D;
78         case TextureType::CubeMapArray:
79             return TextureTarget::CubeMapArray;
80         case TextureType::VideoImage:
81             return TextureTarget::VideoImage;
82         case TextureType::Buffer:
83             return TextureTarget::Buffer;
84         default:
85             UNREACHABLE();
86             return TextureTarget::InvalidEnum;
87     }
88 }
89 
90 // Check that we can do arithmetic on TextureTarget to convert from / to cube map faces
91 static_assert(static_cast<uint8_t>(TextureTarget::CubeMapNegativeX) -
92                       static_cast<uint8_t>(TextureTarget::CubeMapPositiveX) ==
93                   1u,
94               "");
95 static_assert(static_cast<uint8_t>(TextureTarget::CubeMapPositiveY) -
96                       static_cast<uint8_t>(TextureTarget::CubeMapPositiveX) ==
97                   2u,
98               "");
99 static_assert(static_cast<uint8_t>(TextureTarget::CubeMapNegativeY) -
100                       static_cast<uint8_t>(TextureTarget::CubeMapPositiveX) ==
101                   3u,
102               "");
103 static_assert(static_cast<uint8_t>(TextureTarget::CubeMapPositiveZ) -
104                       static_cast<uint8_t>(TextureTarget::CubeMapPositiveX) ==
105                   4u,
106               "");
107 static_assert(static_cast<uint8_t>(TextureTarget::CubeMapNegativeZ) -
108                       static_cast<uint8_t>(TextureTarget::CubeMapPositiveX) ==
109                   5u,
110               "");
111 
CubeFaceIndexToTextureTarget(size_t face)112 TextureTarget CubeFaceIndexToTextureTarget(size_t face)
113 {
114     ASSERT(face < 6u);
115     return static_cast<TextureTarget>(static_cast<uint8_t>(TextureTarget::CubeMapPositiveX) + face);
116 }
117 
CubeMapTextureTargetToFaceIndex(TextureTarget target)118 size_t CubeMapTextureTargetToFaceIndex(TextureTarget target)
119 {
120     ASSERT(IsCubeMapFaceTarget(target));
121     return static_cast<uint8_t>(target) - static_cast<uint8_t>(TextureTarget::CubeMapPositiveX);
122 }
123 
SamplerTypeToTextureType(GLenum samplerType)124 TextureType SamplerTypeToTextureType(GLenum samplerType)
125 {
126     switch (samplerType)
127     {
128         case GL_SAMPLER_2D:
129         case GL_INT_SAMPLER_2D:
130         case GL_UNSIGNED_INT_SAMPLER_2D:
131         case GL_SAMPLER_2D_SHADOW:
132             return TextureType::_2D;
133 
134         case GL_SAMPLER_EXTERNAL_OES:
135         case GL_SAMPLER_EXTERNAL_2D_Y2Y_EXT:
136             return TextureType::External;
137 
138         case GL_SAMPLER_CUBE:
139         case GL_INT_SAMPLER_CUBE:
140         case GL_UNSIGNED_INT_SAMPLER_CUBE:
141         case GL_SAMPLER_CUBE_SHADOW:
142             return TextureType::CubeMap;
143 
144         case GL_SAMPLER_CUBE_MAP_ARRAY:
145         case GL_INT_SAMPLER_CUBE_MAP_ARRAY:
146         case GL_UNSIGNED_INT_SAMPLER_CUBE_MAP_ARRAY:
147         case GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW:
148             return TextureType::CubeMapArray;
149 
150         case GL_SAMPLER_2D_ARRAY:
151         case GL_INT_SAMPLER_2D_ARRAY:
152         case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
153         case GL_SAMPLER_2D_ARRAY_SHADOW:
154             return TextureType::_2DArray;
155 
156         case GL_SAMPLER_3D:
157         case GL_INT_SAMPLER_3D:
158         case GL_UNSIGNED_INT_SAMPLER_3D:
159             return TextureType::_3D;
160 
161         case GL_SAMPLER_2D_MULTISAMPLE:
162         case GL_INT_SAMPLER_2D_MULTISAMPLE:
163         case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
164             return TextureType::_2DMultisample;
165 
166         case GL_SAMPLER_2D_MULTISAMPLE_ARRAY:
167         case GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
168         case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
169             return TextureType::_2DMultisampleArray;
170 
171         case GL_SAMPLER_BUFFER:
172         case GL_INT_SAMPLER_BUFFER:
173         case GL_UNSIGNED_INT_SAMPLER_BUFFER:
174             return TextureType::Buffer;
175 
176         case GL_SAMPLER_2D_RECT_ANGLE:
177             return TextureType::Rectangle;
178 
179         case GL_SAMPLER_VIDEO_IMAGE_WEBGL:
180             return TextureType::VideoImage;
181 
182         default:
183             UNREACHABLE();
184             return TextureType::InvalidEnum;
185     }
186 }
187 
ImageTypeToTextureType(GLenum imageType)188 TextureType ImageTypeToTextureType(GLenum imageType)
189 {
190     switch (imageType)
191     {
192         case GL_IMAGE_2D:
193         case GL_INT_IMAGE_2D:
194         case GL_UNSIGNED_INT_IMAGE_2D:
195             return TextureType::_2D;
196 
197         case GL_IMAGE_CUBE:
198         case GL_INT_IMAGE_CUBE:
199         case GL_UNSIGNED_INT_IMAGE_CUBE:
200             return TextureType::CubeMap;
201 
202         case GL_IMAGE_CUBE_MAP_ARRAY:
203         case GL_INT_IMAGE_CUBE_MAP_ARRAY:
204         case GL_UNSIGNED_INT_IMAGE_CUBE_MAP_ARRAY:
205             return TextureType::CubeMapArray;
206 
207         case GL_IMAGE_2D_ARRAY:
208         case GL_INT_IMAGE_2D_ARRAY:
209         case GL_UNSIGNED_INT_IMAGE_2D_ARRAY:
210             return TextureType::_2DArray;
211 
212         case GL_IMAGE_3D:
213         case GL_INT_IMAGE_3D:
214         case GL_UNSIGNED_INT_IMAGE_3D:
215             return TextureType::_3D;
216 
217         case GL_IMAGE_BUFFER:
218         case GL_INT_IMAGE_BUFFER:
219         case GL_UNSIGNED_INT_IMAGE_BUFFER:
220             return TextureType::Buffer;
221 
222         default:
223             UNREACHABLE();
224             return TextureType::InvalidEnum;
225     }
226 }
227 
IsMultisampled(TextureType type)228 bool IsMultisampled(TextureType type)
229 {
230     switch (type)
231     {
232         case TextureType::_2DMultisample:
233         case TextureType::_2DMultisampleArray:
234             return true;
235         default:
236             return false;
237     }
238 }
239 
IsArrayTextureType(TextureType type)240 bool IsArrayTextureType(TextureType type)
241 {
242     switch (type)
243     {
244         case TextureType::_2DArray:
245         case TextureType::_2DMultisampleArray:
246         case TextureType::CubeMapArray:
247             return true;
248         default:
249             return false;
250     }
251 }
252 
IsStaticBufferUsage(BufferUsage useage)253 bool IsStaticBufferUsage(BufferUsage useage)
254 {
255     switch (useage)
256     {
257         case BufferUsage::StaticCopy:
258         case BufferUsage::StaticDraw:
259         case BufferUsage::StaticRead:
260             return true;
261         default:
262             return false;
263     }
264 }
265 
operator <<(std::ostream & os,PrimitiveMode value)266 std::ostream &operator<<(std::ostream &os, PrimitiveMode value)
267 {
268     switch (value)
269     {
270         case PrimitiveMode::LineLoop:
271             os << "GL_LINE_LOOP";
272             break;
273         case PrimitiveMode::Lines:
274             os << "GL_LINES";
275             break;
276         case PrimitiveMode::LinesAdjacency:
277             os << "GL_LINES_ADJACENCY";
278             break;
279         case PrimitiveMode::LineStrip:
280             os << "GL_LINE_STRIP";
281             break;
282         case PrimitiveMode::LineStripAdjacency:
283             os << "GL_LINE_STRIP_ADJANCENCY";
284             break;
285         case PrimitiveMode::Points:
286             os << "GL_POINTS";
287             break;
288         case PrimitiveMode::TriangleFan:
289             os << "GL_TRIANGLE_FAN";
290             break;
291         case PrimitiveMode::Triangles:
292             os << "GL_TRIANGLES";
293             break;
294         case PrimitiveMode::TrianglesAdjacency:
295             os << "GL_TRIANGLES_ADJANCENCY";
296             break;
297         case PrimitiveMode::TriangleStrip:
298             os << "GL_TRIANGLE_STRIP";
299             break;
300         case PrimitiveMode::TriangleStripAdjacency:
301             os << "GL_TRIANGLE_STRIP_ADJACENCY";
302             break;
303         default:
304             os << "GL_INVALID_ENUM";
305             break;
306     }
307     return os;
308 }
309 
operator <<(std::ostream & os,DrawElementsType value)310 std::ostream &operator<<(std::ostream &os, DrawElementsType value)
311 {
312     switch (value)
313     {
314         case DrawElementsType::UnsignedByte:
315             os << "GL_UNSIGNED_BYTE";
316             break;
317         case DrawElementsType::UnsignedShort:
318             os << "GL_UNSIGNED_SHORT";
319             break;
320         case DrawElementsType::UnsignedInt:
321             os << "GL_UNSIGNED_INT";
322             break;
323         default:
324             os << "GL_INVALID_ENUM";
325             break;
326     }
327 
328     return os;
329 }
330 
operator <<(std::ostream & os,BlendEquationType value)331 std::ostream &operator<<(std::ostream &os, BlendEquationType value)
332 {
333     switch (value)
334     {
335         case BlendEquationType::Add:
336             os << "GL_FUNC_ADD";
337             break;
338         case BlendEquationType::Min:
339             os << "GL_MIN";
340             break;
341         case BlendEquationType::Max:
342             os << "GL_MAX";
343             break;
344         case BlendEquationType::Subtract:
345             os << "GL_FUNC_SUBTRACT";
346             break;
347         case BlendEquationType::ReverseSubtract:
348             os << "GL_FUNC_REVERSE_SUBTRACT";
349             break;
350         default:
351             os << "GL_INVALID_ENUM";
352             break;
353     }
354 
355     return os;
356 }
357 
operator <<(std::ostream & os,BlendFactorType value)358 std::ostream &operator<<(std::ostream &os, BlendFactorType value)
359 {
360     switch (value)
361     {
362         case BlendFactorType::Zero:
363             os << "GL_ZERO";
364             break;
365         case BlendFactorType::One:
366             os << "GL_ONE";
367             break;
368         case BlendFactorType::SrcColor:
369             os << "GL_SRC_COLOR";
370             break;
371         case BlendFactorType::OneMinusSrcColor:
372             os << "GL_ONE_MINUS_SRC_COLOR";
373             break;
374         case BlendFactorType::SrcAlpha:
375             os << "GL_SRC_ALPHA";
376             break;
377         case BlendFactorType::OneMinusSrcAlpha:
378             os << "GL_ONE_MINUS_SRC_ALPHA";
379             break;
380         case BlendFactorType::DstAlpha:
381             os << "GL_DST_ALPHA";
382             break;
383         case BlendFactorType::OneMinusDstAlpha:
384             os << "GL_ONE_MINUS_DST_ALPHA";
385             break;
386         case BlendFactorType::DstColor:
387             os << "GL_DST_COLOR";
388             break;
389         case BlendFactorType::OneMinusDstColor:
390             os << "GL_ONE_MINUS_DST_COLOR";
391             break;
392         case BlendFactorType::SrcAlphaSaturate:
393             os << "GL_SRC_ALPHA_SATURATE";
394             break;
395         case BlendFactorType::ConstantColor:
396             os << "GL_CONSTANT_COLOR";
397             break;
398         case BlendFactorType::OneMinusConstantColor:
399             os << "GL_ONE_MINUS_CONSTANT_COLOR";
400             break;
401         case BlendFactorType::ConstantAlpha:
402             os << "GL_CONSTANT_ALPHA";
403             break;
404         case BlendFactorType::OneMinusConstantAlpha:
405             os << "GL_ONE_MINUS_CONSTANT_ALPHA";
406             break;
407         case BlendFactorType::Src1Alpha:
408             os << "GL_SRC1_ALPHA_EXT";
409             break;
410         case BlendFactorType::Src1Color:
411             os << "GL_SRC1_COLOR_EXT";
412             break;
413         case BlendFactorType::OneMinusSrc1Color:
414             os << "GL_ONE_MINUS_SRC1_COLOR_EXT";
415             break;
416         case BlendFactorType::OneMinusSrc1Alpha:
417             os << "GL_ONE_MINUS_SRC1_ALPHA_EXT";
418             break;
419         default:
420             os << "GL_INVALID_ENUM";
421             break;
422     }
423 
424     return os;
425 }
426 
operator <<(std::ostream & os,VertexAttribType value)427 std::ostream &operator<<(std::ostream &os, VertexAttribType value)
428 {
429     switch (value)
430     {
431         case VertexAttribType::Byte:
432             os << "GL_BYTE";
433             break;
434         case VertexAttribType::Fixed:
435             os << "GL_FIXED";
436             break;
437         case VertexAttribType::Float:
438             os << "GL_FLOAT";
439             break;
440         case VertexAttribType::HalfFloat:
441             os << "GL_HALF_FLOAT";
442             break;
443         case VertexAttribType::HalfFloatOES:
444             os << "GL_HALF_FLOAT_OES";
445             break;
446         case VertexAttribType::Int:
447             os << "GL_INT";
448             break;
449         case VertexAttribType::Int2101010:
450             os << "GL_INT_2_10_10_10_REV";
451             break;
452         case VertexAttribType::Int1010102:
453             os << "GL_INT_10_10_10_2_OES";
454             break;
455         case VertexAttribType::Short:
456             os << "GL_SHORT";
457             break;
458         case VertexAttribType::UnsignedByte:
459             os << "GL_UNSIGNED_BYTE";
460             break;
461         case VertexAttribType::UnsignedInt:
462             os << "GL_UNSIGNED_INT";
463             break;
464         case VertexAttribType::UnsignedInt2101010:
465             os << "GL_UNSIGNED_INT_2_10_10_10_REV";
466             break;
467         case VertexAttribType::UnsignedInt1010102:
468             os << "GL_UNSIGNED_INT_10_10_10_2_OES";
469             break;
470         case VertexAttribType::UnsignedShort:
471             os << "GL_UNSIGNED_SHORT";
472             break;
473         default:
474             os << "GL_INVALID_ENUM";
475             break;
476     }
477     return os;
478 }
479 
operator <<(std::ostream & os,TessEvaluationType value)480 std::ostream &operator<<(std::ostream &os, TessEvaluationType value)
481 {
482     switch (value)
483     {
484         case TessEvaluationType::Triangles:
485             os << "GL_TRIANGLES";
486             break;
487         case TessEvaluationType::Quads:
488             os << "GL_QUADS";
489             break;
490         case TessEvaluationType::Isolines:
491             os << "GL_ISOLINES";
492             break;
493         case TessEvaluationType::EqualSpacing:
494             os << "GL_EQUAL";
495             break;
496         case TessEvaluationType::FractionalEvenSpacing:
497             os << "GL_FRACTIONAL_EVEN";
498             break;
499         case TessEvaluationType::FractionalOddSpacing:
500             os << "GL_FRACTIONAL_ODD";
501             break;
502         case TessEvaluationType::Cw:
503             os << "GL_CW";
504             break;
505         case TessEvaluationType::Ccw:
506             os << "GL_CCW";
507             break;
508         case TessEvaluationType::PointMode:
509             os << "GL_TESS_GEN_POINT_MODE";
510             break;
511         default:
512             os << "GL_INVALID_ENUM";
513             break;
514     }
515     return os;
516 }
517 
ShaderTypeToString(ShaderType shaderType)518 const char *ShaderTypeToString(ShaderType shaderType)
519 {
520     constexpr ShaderMap<const char *> kShaderTypeNameMap = {
521         {ShaderType::Vertex, "Vertex"},
522         {ShaderType::TessControl, "Tessellation control"},
523         {ShaderType::TessEvaluation, "Tessellation evaluation"},
524         {ShaderType::Geometry, "Geometry"},
525         {ShaderType::Fragment, "Fragment"},
526         {ShaderType::Compute, "Compute"}};
527     return kShaderTypeNameMap[shaderType];
528 }
529 }  // namespace gl
530 
531 namespace egl
532 {
ErrorCodeToMessageType(EGLint errorCode)533 MessageType ErrorCodeToMessageType(EGLint errorCode)
534 {
535     switch (errorCode)
536     {
537         case EGL_BAD_ALLOC:
538         case EGL_CONTEXT_LOST:
539         case EGL_NOT_INITIALIZED:
540             return MessageType::Critical;
541 
542         case EGL_BAD_ACCESS:
543         case EGL_BAD_ATTRIBUTE:
544         case EGL_BAD_CONFIG:
545         case EGL_BAD_CONTEXT:
546         case EGL_BAD_CURRENT_SURFACE:
547         case EGL_BAD_DISPLAY:
548         case EGL_BAD_MATCH:
549         case EGL_BAD_NATIVE_PIXMAP:
550         case EGL_BAD_NATIVE_WINDOW:
551         case EGL_BAD_PARAMETER:
552         case EGL_BAD_SURFACE:
553         case EGL_BAD_STREAM_KHR:
554         case EGL_BAD_STATE_KHR:
555         case EGL_BAD_DEVICE_EXT:
556             return MessageType::Error;
557 
558         case EGL_SUCCESS:
559         default:
560             UNREACHABLE();
561             return MessageType::InvalidEnum;
562     }
563 }
564 }  // namespace egl
565 
566 namespace egl_gl
567 {
568 
EGLCubeMapTargetToCubeMapTarget(EGLenum eglTarget)569 gl::TextureTarget EGLCubeMapTargetToCubeMapTarget(EGLenum eglTarget)
570 {
571     ASSERT(egl::IsCubeMapTextureTarget(eglTarget));
572     return gl::CubeFaceIndexToTextureTarget(egl::CubeMapTextureTargetToLayerIndex(eglTarget));
573 }
574 
EGLImageTargetToTextureTarget(EGLenum eglTarget)575 gl::TextureTarget EGLImageTargetToTextureTarget(EGLenum eglTarget)
576 {
577     switch (eglTarget)
578     {
579         case EGL_GL_TEXTURE_2D_KHR:
580             return gl::TextureTarget::_2D;
581 
582         case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
583         case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
584         case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
585         case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
586         case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
587         case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
588             return EGLCubeMapTargetToCubeMapTarget(eglTarget);
589 
590         case EGL_GL_TEXTURE_3D_KHR:
591             return gl::TextureTarget::_3D;
592 
593         default:
594             UNREACHABLE();
595             return gl::TextureTarget::InvalidEnum;
596     }
597 }
598 
EGLTextureTargetToTextureType(EGLenum eglTarget)599 gl::TextureType EGLTextureTargetToTextureType(EGLenum eglTarget)
600 {
601     switch (eglTarget)
602     {
603         case EGL_TEXTURE_2D:
604             return gl::TextureType::_2D;
605 
606         case EGL_TEXTURE_RECTANGLE_ANGLE:
607             return gl::TextureType::Rectangle;
608 
609         default:
610             UNREACHABLE();
611             return gl::TextureType::InvalidEnum;
612     }
613 }
614 }  // namespace egl_gl
615