Hash :
cd10b6d2
Author :
Date :
2022-10-10T21:00:07
Enum Utils: Add method to return enum value from string. We'll use this in the trace interpreter to parse enum values. Bug: angleproject:7752 Change-Id: I232a00baac2f74c9618029929bbb3e5822654046 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3963366 Reviewed-by: Cody Northrop <cnorthrop@google.com> Reviewed-by: Yuxin Hu <yuxinhu@google.com> Commit-Queue: Jamie Madill <jmadill@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
//
// Copyright 2019 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// gl_enum_utils.cpp:
// Utility functions for converting GLenums to string.
#include "common/gl_enum_utils.h"
#include "common/bitset_utils.h"
#include "common/string_utils.h"
#include <iomanip>
#include <sstream>
namespace gl
{
namespace
{
template <typename EnumType>
void OutputGLenumStringImpl(std::ostream &out, EnumType enumGroup, unsigned int value)
{
const char *enumStr = GLenumToString(enumGroup, value);
if (enumStr != kUnknownGLenumString)
{
out << enumStr;
return;
}
if (enumGroup == EnumType::Boolean)
{
// If an unknown enum was submitted as GLboolean, just write out the value.
if (enumStr == kUnknownGLenumString)
{
out << value;
}
else
{
out << enumStr;
}
return;
}
if (enumGroup != EnumType::AllEnums)
{
// Retry with the "Default" group
enumStr = GLenumToString(EnumType::AllEnums, value);
if (enumStr != kUnknownGLenumString)
{
out << enumStr;
return;
}
}
out << std::hex << "0x" << std::setfill('0') << std::setw(4) << value << std::dec;
}
template <typename EnumType>
std::string GLbitfieldToStringImpl(EnumType enumGroup, unsigned int value)
{
std::stringstream st;
if (value == 0)
{
return "0";
}
const angle::BitSet<32> bitSet(value);
bool first = true;
for (const auto index : bitSet)
{
if (!first)
{
st << " | ";
}
first = false;
unsigned int mask = 1u << index;
OutputGLenumString(st, enumGroup, mask);
}
return st.str();
}
} // namespace
const char kUnknownGLenumString[] = "EnumUnknown";
void OutputGLenumString(std::ostream &out, GLESEnum enumGroup, unsigned int value)
{
return OutputGLenumStringImpl(out, enumGroup, value);
}
void OutputGLenumString(std::ostream &out, BigGLEnum enumGroup, unsigned int value)
{
return OutputGLenumStringImpl(out, enumGroup, value);
}
void OutputGLbitfieldString(std::ostream &out, GLESEnum enumGroup, unsigned int value)
{
out << GLbitfieldToString(enumGroup, value);
}
const char *GLbooleanToString(unsigned int value)
{
return GLenumToString(GLESEnum::Boolean, value);
}
std::string GLbitfieldToString(GLESEnum enumGroup, unsigned int value)
{
return GLbitfieldToStringImpl(enumGroup, value);
}
std::string GLbitfieldToString(BigGLEnum enumGroup, unsigned int value)
{
return GLbitfieldToStringImpl(enumGroup, value);
}
const char *GLinternalFormatToString(unsigned int format)
{
return GLenumToString(gl::GLESEnum::InternalFormat, format);
}
unsigned int StringToGLbitfield(const char *str)
{
unsigned int value = 0;
std::vector<std::string> strings =
angle::SplitString(str, " |", angle::WhitespaceHandling::TRIM_WHITESPACE,
angle::SplitResult::SPLIT_WANT_NONEMPTY);
for (const std::string &enumString : strings)
{
value |= StringToGLenum(enumString.c_str());
}
return value;
}
} // namespace gl