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>
This commit is contained in:
Jamie Madill
2022-10-10 21:00:07 -04:00
committed by Angle LUCI CQ
parent fd45cec30b
commit cd10b6d25a
5 changed files with 24178 additions and 2 deletions

View File

@@ -1,12 +1,12 @@
{
"scripts/gen_gl_enum_utils.py":
"5adc7d4cfc7d17d89d6917015664a060",
"bc90f067f124c1cae63a348e9ae057ce",
"scripts/gl_angle_ext.xml":
"8dd9662655c39ce22fecdc2449594e54",
"scripts/registry_xml.py":
"a532782ba7caa6af314e535b7e467a5b",
"src/common/gl_enum_utils_autogen.cpp":
"109c491a507e139473176a99530308d4",
"05b48bda3e66bbc6b413cf2374899b26",
"src/common/gl_enum_utils_autogen.h":
"4d2876469ba95955182c21c09c466333",
"third_party/OpenGL-Registry/src/xml/gl.xml":

View File

@@ -54,6 +54,7 @@ template_gl_enums_source = """// GENERATED FILE - DO NOT EDIT.
#include "common/gl_enum_utils_autogen.h"
#include "common/debug.h"
#include "common/gl_enum_utils.h"
namespace gl
@@ -88,6 +89,13 @@ const char *GLenumToString(BigGLEnum enumGroup, unsigned int value)
return UnknownEnumToString(value);
}}
}}
unsigned int StringToGLenum(const char *str)
{{
{string_to_enum_table}
UNREACHABLE();
return 0;
}}
}} // namespace gl
"""
@@ -150,6 +158,28 @@ def dump_value_to_string_mapping(enum_groups, api_enum):
])
def dump_string_to_value_mapping(enums_and_values):
templ = """\
if (strcmp(str, "{enum}") == 0)
{{
return {value};
}}
"""
def f(value):
if value < 0:
return str(value)
if value < 0xFFFF:
return "0x%04X" % value
if value <= 0xFFFFFFFF:
return "0x%X" % value
else:
return "0xFFFFFFFF"
items = [templ.format(enum=k, value=f(v)) for (k, v) in sorted(enums_and_values)]
return ''.join(items)
def main(header_output_path, source_output_path):
xml = registry_xml.RegistryXML('gl.xml', 'gl_angle_ext.xml')
@@ -189,11 +219,13 @@ def main(header_output_path, source_output_path):
gles_default_enums = dict()
gl_enum_groups[registry_xml.default_enum_group_name] = gl_default_enums
gles_enum_groups[registry_xml.default_enum_group_name] = gles_default_enums
enums_and_values = []
for enums_node in xml.root.findall('enums'):
for enum in enums_node.findall('enum'):
enum_name = enum.attrib['name']
enum_value = int(enum.attrib['value'], base=16)
enums_and_values.append((enum_name, enum_value))
if enum_name in gles_enums:
gles_default_enums[enum_name] = enum_value
@@ -234,11 +266,13 @@ def main(header_output_path, source_output_path):
# Write mapping to source file
gles_enums_value_to_string_table = dump_value_to_string_mapping(gles_enum_groups, 'GLESEnum')
gl_enums_value_to_string_table = dump_value_to_string_mapping(gl_enum_groups, 'BigGLEnum')
string_to_enum_table = dump_string_to_value_mapping(enums_and_values)
source_content = template_gl_enums_source.format(
script_name=os.path.basename(sys.argv[0]),
data_source_name="gl.xml and gl_angle_ext.xml",
gles_enums_value_to_string_table=gles_enums_value_to_string_table,
gl_enums_value_to_string_table=gl_enums_value_to_string_table,
string_to_enum_table=string_to_enum_table,
)
source_output_path = registry_xml.script_relative(source_output_path)

View File

@@ -9,6 +9,7 @@
#include "common/gl_enum_utils.h"
#include "common/bitset_utils.h"
#include "common/string_utils.h"
#include <iomanip>
#include <sstream>
@@ -121,4 +122,17 @@ 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

View File

@@ -25,6 +25,8 @@ void OutputGLenumString(std::ostream &out, GLESEnum enumGroup, unsigned int valu
void OutputGLenumString(std::ostream &out, BigGLEnum enumGroup, unsigned int value);
void OutputGLbitfieldString(std::ostream &out, GLESEnum enumGroup, unsigned int value);
const char *GLinternalFormatToString(unsigned int format);
unsigned int StringToGLenum(const char *str);
unsigned int StringToGLbitfield(const char *str);
extern const char kUnknownGLenumString[];
} // namespace gl

File diff suppressed because it is too large Load Diff