mirrored from https://chromium.googlesource.com/angle/angle
- Notifications
You must be signed in to change notification settings - Fork 646
/
Copy pathgen_gl_enum_utils.py
executable file
·321 lines (268 loc) · 10.7 KB
/
gen_gl_enum_utils.py
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/python3
#
# 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.
#
# gen_gl_enum_utils.py:
# Generates GLenum value to string mapping for ANGLE
# NOTE: don't run this script directly. Run scripts/run_code_generation.py.
importsys
importos
importregistry_xml
template_gl_enums_header="""// GENERATED FILE - DO NOT EDIT.
// Generated by {script_name} using data from {data_source_name}.
//
// 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_autogen.h:
// mapping of GLenum value to string.
# ifndef COMMON_GL_ENUM_UTILS_AUTOGEN_H_
# define COMMON_GL_ENUM_UTILS_AUTOGEN_H_
namespace gl
{{
enum class GLESEnum
{{
{gles_enum_groups}
}};
enum class BigGLEnum
{{
{gl_enum_groups}
}};
}} // namespace gl
# endif // COMMON_GL_ENUM_UTILS_AUTOGEN_H_
"""
template_gl_enums_source="""// GENERATED FILE - DO NOT EDIT.
// Generated by {script_name} using data from {data_source_name}.
//
// 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_autogen.cpp:
// mapping of GLenum value to string.
#include "common/gl_enum_utils_autogen.h"
#include "common/debug.h"
#include "common/gl_enum_utils.h"
#include <algorithm>
#include <cstring>
namespace gl
{{
namespace
{{
const char *UnknownEnumToString(unsigned int value)
{{
constexpr size_t kBufferSize = 64;
static thread_local char sBuffer[kBufferSize];
snprintf(sBuffer, kBufferSize, "0x%04X", value);
return sBuffer;
}}
}} // anonymous namespace
const char *GLenumToString(GLESEnum enumGroup, unsigned int value)
{{
switch (enumGroup)
{{
{gles_enums_value_to_string_table}
default:
return UnknownEnumToString(value);
}}
}}
const char *GLenumToString(BigGLEnum enumGroup, unsigned int value)
{{
switch (enumGroup)
{{
{gl_enums_value_to_string_table}
default:
return UnknownEnumToString(value);
}}
}}
namespace
{{
using StringEnumEntry = std::pair<const char*, unsigned int>;
static StringEnumEntry g_stringEnumTable[] = {{
{string_to_enum_table}
}};
const size_t g_numStringEnums = std::size(g_stringEnumTable);
}} // anonymous namespace
unsigned int StringToGLenum(const char *str)
{{
auto it = std::lower_bound(
&g_stringEnumTable[0], &g_stringEnumTable[g_numStringEnums], str,
[](const StringEnumEntry& a, const char* b) {{ return strcmp(a.first, b) < 0; }});
if (strcmp(it->first, str) == 0)
{{
return it->second;
}}
UNREACHABLE();
return 0;
}}
}} // namespace gl
"""
template_enum_group_case="""case {api_enum}::{group_name}: {{
switch (value) {{
{inner_group_cases}
default:
return UnknownEnumToString(value);
}}
}}
"""
template_enum_value_to_string_case="""case {value}: return {name};"""
exclude_enum_groups= {'SpecialNumbers'}
# Special enum groups that don't have any enum values.
# Add groups here if you get missing group compile errors.
empty_enum_groups= ['SemaphoreParameterName', 'ShaderBinaryFormat']
defdump_value_to_string_mapping(enum_groups, api_enum):
exporting_groups=list()
forgroup_name, inner_mappinginenum_groups.items():
# Convert to pairs and strip out-of-range values.
string_value_pairs=list(
filter(lambdax: x[1] >=0andx[1] <=0xFFFFFFFFF, inner_mapping.items()))
ifnotstring_value_pairs:
continue
# sort according values
string_value_pairs.sort(key=lambdax: (x[1], len(x[0]), x[0]))
# remove all duplicate values from the pairs list
# some value may have more than one GLenum mapped to them, such as:
# GL_DRAW_FRAMEBUFFER_BINDING and GL_FRAMEBUFFER_BINDING
# GL_BLEND_EQUATION_RGB and GL_BLEND_EQUATION
# it is safe to output either one of them, for simplity here just
# choose the shorter one which comes first in the sorted list
exporting_string_value_pairs=list()
forindex, pairinenumerate(string_value_pairs):
ifindex==0orpair[1] !=string_value_pairs[index-1][1]:
exporting_string_value_pairs.append(pair)
inner_code_block="\n".join([
template_enum_value_to_string_case.format(
value='0x%X'%value,
name='"%s"'%name,
) forname, valueinexporting_string_value_pairs
])
exporting_groups.append((group_name, inner_code_block))
return"\n".join([
template_enum_group_case.format(
api_enum=api_enum,
group_name=group_name,
inner_group_cases=inner_code_block,
) forgroup_name, inner_code_blockinsorted(exporting_groups, key=lambdax: x[0])
])
defdump_string_to_value_mapping(enums_and_values):
deff(value):
ifvalue<0:
returnstr(value)
ifvalue<0xFFFF:
return"0x%04X"%value
ifvalue<=0xFFFFFFFF:
return"0x%X"%value
else:
return"0xFFFFFFFF"
return'\n'.join('{"%s", %s},'% (k, f(v)) fork, vinsorted(enums_and_values))
defmain(header_output_path, source_output_path):
xml=registry_xml.RegistryXML('gl.xml', 'gl_angle_ext.xml')
# Compute a list of all GLES enums.
gles_enums=set()
bigl_enums=set()
forfeatureinxml.root.findall('feature'):
forrequireinfeature.findall('require'):
assert'api'notinrequire.attrib
if'gles'infeature.attrib['api']:
forenuminrequire.findall('enum'):
gles_enums.add(enum.attrib['name'])
iffeature.attrib['api'] =='gl':
forenuminrequire.findall('enum'):
bigl_enums.add(enum.attrib['name'])
forextensionsinxml.root.findall('extensions'):
forextensioninextensions.findall('extension'):
ifextension.attrib['name'] inregistry_xml.supported_extensions:
forrequireinextension.findall('require'):
ext_apis=extension.attrib['supported'].split('|')
if ('api'notinrequire.attribor'gles'inrequire.attrib['api']) and (
'gles'inextension.attrib['supported']):
forenuminrequire.findall('enum'):
gles_enums.add(enum.attrib['name'])
if ('api'notinrequire.attribor
feature.attrib['api'] =='gl') and ('gl'inext_apis):
forenuminrequire.findall('enum'):
bigl_enums.add(enum.attrib['name'])
# Build a map from GLenum name to its value
gl_enum_groups=dict()
gles_enum_groups=dict()
# Add all enums to default groups
gl_default_enums=dict()
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= []
forenums_nodeinxml.root.findall('enums'):
forenuminenums_node.findall('enum'):
enum_name=enum.attrib['name']
enum_value=int(enum.attrib['value'], base=16)
enums_and_values.append((enum_name, enum_value))
ifenum_nameingles_enums:
gles_default_enums[enum_name] =enum_value
ifenum_nameinbigl_enums:
gl_default_enums[enum_name] =enum_value
if'group'inenum.attrib:
forenum_groupinenum.attrib['group'].split(','):
ifenum_groupinexclude_enum_groups:
continue
ifenum_nameingles_enums:
ifenum_groupnotingles_enum_groups:
gles_enum_groups[enum_group] =dict()
gles_enum_groups[enum_group][enum_name] =enum_value
ifenum_nameinbigl_enums:
ifenum_groupnotingl_enum_groups:
gl_enum_groups[enum_group] =dict()
gl_enum_groups[enum_group][enum_name] =enum_value
forempty_groupinempty_enum_groups:
assertnotempty_groupingles_enum_groupsornotempty_groupingl_enum_groups, 'Remove %s from the empty groups list, it has enums now.'%empty_group
ifempty_groupnotingles_enum_groups:
gles_enum_groups[empty_group] =dict()
ifempty_groupnotingl_enum_groups:
gl_enum_groups[empty_group] =dict()
# Write GLenum groups into the header file.
header_content=template_gl_enums_header.format(
script_name=os.path.basename(sys.argv[0]),
data_source_name="gl.xml and gl_angle_ext.xml",
gles_enum_groups=',\n'.join(sorted(gles_enum_groups.keys())),
gl_enum_groups=',\n'.join(sorted(gl_enum_groups.keys())))
header_output_path=registry_xml.script_relative(header_output_path)
withopen(header_output_path, 'w') asf:
f.write(header_content)
# 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)
withopen(source_output_path, 'w') asf:
f.write(source_content)
return0
if__name__=='__main__':
inputs= [
'../third_party/OpenGL-Registry/src/xml/gl.xml',
'gl_angle_ext.xml',
'registry_xml.py',
]
gl_enum_utils_autogen_base_path='../src/common/gl_enum_utils_autogen'
outputs= [
gl_enum_utils_autogen_base_path+'.h',
gl_enum_utils_autogen_base_path+'.cpp',
]
iflen(sys.argv) >1:
ifsys.argv[1] =='inputs':
print(','.join(inputs))
elifsys.argv[1] =='outputs':
print(','.join(outputs))
else:
sys.exit(
main(
registry_xml.script_relative(outputs[0]),
registry_xml.script_relative(outputs[1])))