1 //
2 // Copyright 2019 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // gl_enum_utils.cpp:
7 // Utility functions for converting GLenums to string.
8
9 #include "libANGLE/capture/gl_enum_utils.h"
10
11 #include "common/bitset_utils.h"
12
13 #include <iomanip>
14 #include <sstream>
15
16 namespace gl
17 {
18 const char kUnknownGLenumString[] = "EnumUnknown";
19
OutputGLenumString(std::ostream & out,GLenumGroup enumGroup,unsigned int value)20 void OutputGLenumString(std::ostream &out, GLenumGroup enumGroup, unsigned int value)
21 {
22 const char *enumStr = GLenumToString(enumGroup, value);
23 if (enumStr != kUnknownGLenumString)
24 {
25 out << enumStr;
26 return;
27 }
28
29 if (enumGroup == GLenumGroup::Boolean)
30 {
31 // If an unknown enum was submitted as GLboolean, just write out the value.
32 if (enumStr == kUnknownGLenumString)
33 {
34 out << value;
35 }
36 else
37 {
38 out << enumStr;
39 }
40
41 return;
42 }
43
44 if (enumGroup != GLenumGroup::DefaultGroup)
45 {
46 // Retry with the "Default" group
47 enumStr = GLenumToString(GLenumGroup::DefaultGroup, value);
48 if (enumStr != kUnknownGLenumString)
49 {
50 out << enumStr;
51 return;
52 }
53 }
54
55 out << std::hex << "0x" << std::setfill('0') << std::setw(4) << value << std::dec;
56 }
57
OutputGLbitfieldString(std::ostream & out,GLenumGroup enumGroup,unsigned int value)58 void OutputGLbitfieldString(std::ostream &out, GLenumGroup enumGroup, unsigned int value)
59 {
60 out << GLbitfieldToString(enumGroup, value);
61 }
62
GLbooleanToString(unsigned int value)63 const char *GLbooleanToString(unsigned int value)
64 {
65 return GLenumToString(GLenumGroup::Boolean, value);
66 }
67
GLbitfieldToString(GLenumGroup enumGroup,unsigned int value)68 std::string GLbitfieldToString(GLenumGroup enumGroup, unsigned int value)
69 {
70 std::stringstream st;
71
72 if (value == 0)
73 {
74 return "0";
75 }
76
77 const angle::BitSet<32> bitSet(value);
78 bool first = true;
79 for (const auto index : bitSet)
80 {
81 if (!first)
82 {
83 st << " | ";
84 }
85 first = false;
86
87 unsigned int mask = 1u << index;
88 OutputGLenumString(st, enumGroup, mask);
89 }
90
91 return st.str();
92 }
93 } // namespace gl
94