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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
gpu / ipc / common / generate_vulkan_types.py [blame]
#!/usr/bin/env python3
# Copyright 2019 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import argparse
import filecmp
import os
import platform
import re
import subprocess
import sys
import typing
from xml.etree import ElementTree
_VK_XML_FILE = "third_party/vulkan-headers/src/registry/vk.xml"
_STRUCTS = [
"VkExtensionProperties",
"VkLayerProperties",
"VkPhysicalDeviceProperties",
"VkPhysicalDeviceFeatures",
"VkQueueFamilyProperties",
]
_SELF_LOCATION = os.path.dirname(os.path.abspath(__file__))
_MOJO_TYPES = set([
"uint8",
"uint16",
"uint32",
"int8",
"int16",
"int32",
"float",
"string",
])
_VULKAN_BASIC_TYPE_MAP = set([
"uint8_t",
"uint16_t",
"uint32_t",
"uint64_t",
"int8_t",
"int16_t",
"int32_t",
"int64_t",
"size_t",
"VkBool32",
"float",
"char",
])
# types to mojo type
_type_map = {
"uint8_t" : "uint8",
"uint16_t" : "uint16",
"uint32_t" : "uint32",
"uint64_t" : "uint64",
"int8_t" : "int8",
"int16_t" : "int16",
"int32_t" : "int32",
"int64_t" : "int64",
"size_t" : "uint64",
"VkBool32" : "bool",
"float" : "float",
"char" : "char",
}
_structs = {}
_enums = {}
_defines = {}
_handles = set([])
_generated_types = []
def ValueNameToVALUE_NAME(name: str) -> str:
return re.sub(
r'(?<=[a-z])[A-Z]|(?<!^)[A-Z](?=[a-z])', r"_\g<0>", name).upper()
def ParseEnums(reg: re.Pattern) -> None:
for type_elm in reg.findall("enums"):
name = type_elm.get("name")
if name == "API Constants":
for enum in type_elm.findall("enum"):
enum_name = enum.get("name")
enum_value = enum.get("value")
enum_alias = enum.get("alias")
if enum_alias:
continue
_defines[enum_name] = enum_value
continue
# Skip VkResult and NameBits
if name == "VkResult":
value_name_prefix = "VK"
elif name.endswith("FlagBits"):
value_name_prefix = ValueNameToVALUE_NAME(name[:-len("FlagBits")])
elif name.endswith("FlagBitsKHR"):
value_name_prefix = ValueNameToVALUE_NAME(name[:-len("FlagBitsKHR")])
else:
value_name_prefix = ValueNameToVALUE_NAME(name)
values = []
for enum in type_elm.findall("enum"):
enum_name = enum.get("name")
enum_value = enum.get("value")
mojom_name = enum_name[len(value_name_prefix) + 1:]
values.append((enum_name, enum_value, mojom_name))
_enums[name] = values
def ParseHandleElement(element: ElementTree.Element) -> None:
name = element.get("name") or element.find("name").text
_handles.add(name)
def ParseBaseTypeElement(element: ElementTree.Element) -> None:
name = element.find("name").text
_type = None if element.find("type") is None else element.find("type").text
if name not in _type_map:
_type_map[name] = _type
def ParseBitmaskElement(element: ElementTree.Element) -> None:
name = element.find("name")
if name is not None:
name = name.text
_type = None if element.find("type") is None else element.find("type").text
_type_map[name] = _type
def ParseStructElement(element: ElementTree.Element) -> None:
name = element.get("name") or element.find("name").text
members = []
for member in element.findall("member"):
member_type = member.find("type").text
member_name = member.find("name").text
member_array_len = None
for text in member.itertext():
if text.startswith("["):
member_array_len = text[1:-1]
if not member_array_len:
member_array_len = member.find("enum").text
break
members.append((member_name, member_type, member_array_len))
_structs[name] = members
def ParseTypes(reg: re.Pattern) -> None:
for type_elm in reg.findall("types/type"):
category = type_elm.get("category")
if category == "handle":
ParseHandleElement(type_elm)
elif category == "basetype":
ParseBaseTypeElement(type_elm)
elif category == "bitmask":
ParseBitmaskElement(type_elm)
elif category == "struct":
ParseStructElement(type_elm)
def ParseVkXMLFile(path: str) -> None:
tree = ElementTree.parse(path)
reg = tree.getroot()
ParseEnums(reg)
ParseTypes(reg)
def WriteMojomEnum(name: str, mojom_file: typing.IO) -> None:
if name in _generated_types:
return
_generated_types.append(name)
values = _enums[name]
mojom_file.write("\n")
mojom_file.write("enum %s {\n" % name)
for _, value, mojom_value_name in values:
mojom_file.write(" %s = %s,\n" % (mojom_value_name, value))
mojom_file.write(" INVALID_VALUE = -1,\n")
mojom_file.write("};\n")
def WriteMojomStruct(name: str, mojom_file: typing.IO) -> None:
if name in _generated_types:
return
_generated_types.append(name)
fields = _structs[name]
deps = []
for field_name, field_type, array_len in fields:
if field_type in _structs or field_type in _enums:
deps.append(field_type)
WriteMojomTypes(deps, mojom_file)
mojom_file.write("\n")
mojom_file.write("struct %s {\n" % name)
for field_name, field_type, array_len in fields:
if field_type in _type_map:
while field_type in _type_map and field_type != _type_map[field_type]:
field_type = _type_map[field_type]
else:
assert field_type in _structs or field_type in _enums or \
field_type in _handles, "Undefine type: '%s'" % field_type
if field_type == "char":
assert array_len
array_len = _defines[array_len]
mojom_file.write(" string %s;\n" % field_name)
elif not array_len:
mojom_file.write(" %s %s;\n" % (field_type, field_name))
else:
if not array_len.isdigit():
array_len = _defines[array_len]
assert array_len.isdigit(), "%s is not a digit." % array_len
mojom_file.write(
" array<%s, %s> %s;\n" % (field_type, array_len, field_name))
mojom_file.write("};\n")
def WriteMojomTypes(types: typing.Iterable[str], mojom_file: typing.IO) -> None:
for t in types:
if t in _structs:
WriteMojomStruct(t, mojom_file)
elif t in _enums:
WriteMojomEnum(t, mojom_file)
else:
pass
def GenerateMojom(mojom_file: typing.IO) -> None:
mojom_file.write(
'''// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file is auto-generated from
// gpu/ipc/common/generate_vulkan_types.py
// It's formatted by clang-format using chromium coding style:
// clang-format -i -style=chromium filename
// DO NOT EDIT!
module gpu.mojom;
''')
WriteMojomTypes(_STRUCTS, mojom_file)
def NormalizedCamelCase(identifier: str) -> None:
result = identifier[0].upper()
lowercase_next = True
for i in range(1, len(identifier)):
if identifier[i].isupper():
if lowercase_next:
result += identifier[i].lower()
else:
result += identifier[i]
lowercase_next = True
else:
lowercase_next = False
result += identifier[i]
return result
def WriteStructTraits(name: str,
traits_header_file: typing.IO,
traits_source_file: typing.IO) -> None:
traits_header_file.write(
"""
template <>
struct StructTraits<gpu::mojom::%sDataView, %s> {
""" % (name, name)
)
fields = _structs[name]
for field_name, field_type, array_len in fields:
if field_type == "VkBool32":
field_type = "bool"
elif field_type == "VkDeviceSize":
field_type = "bool"
if field_type == "char":
assert array_len
traits_header_file.write(
"""
static std::string_view %s(const %s& input) {
return input.%s;
}
""" % (field_name, name, field_name))
elif array_len:
traits_header_file.write(
"""
static base::span<const %s> %s(const %s& input) {
return input.%s;
}
""" % (field_type, field_name, name, field_name))
elif field_type in _structs:
traits_header_file.write(
"""
static const %s& %s(const %s& input) {
return input.%s;
}
""" % (field_type, field_name, name, field_name))
else:
traits_header_file.write(
"""
static %s %s(const %s& input) {
return input.%s;
}
""" % (field_type, field_name, name, field_name))
traits_header_file.write(
"""
static bool Read(gpu::mojom::%sDataView data, %s* out);
""" % (name, name))
traits_source_file.write(
"""
// static
bool StructTraits<gpu::mojom::%sDataView, %s>::Read(
gpu::mojom::%sDataView data, %s* out) {
""" % (name, name, name, name))
fields = _structs[name]
for field_name, field_type, array_len in fields:
if field_type == "VkBool32":
field_type = "bool"
elif field_type == "VkDeviceSize":
field_type = "bool"
if field_type == "char":
assert array_len
read_method = "Read%s" % (NormalizedCamelCase(field_name))
traits_source_file.write(
"""
std::string_view %s;
if (!data.%s(&%s))
return false;
%s.copy(out->%s, sizeof(out->%s));
""" % (field_name, read_method, field_name, field_name, field_name, field_name))
elif array_len:
read_method = "Read%s" % (NormalizedCamelCase(field_name))
traits_source_file.write(
"""
base::span<%s> %s(out->%s);
if (!data.%s(&%s))
return false;
""" % (field_type, field_name, field_name, read_method, field_name))
elif field_type in _structs or field_type in _enums:
traits_source_file.write(
"""
if (!data.Read%s(&out->%s))
return false;
""" % (NormalizedCamelCase(field_name), field_name))
else:
traits_source_file.write(
"""
out->%s = data.%s();
""" % (field_name, field_name))
traits_source_file.write(
"""
return true;
}
""")
traits_header_file.write("};\n")
def WriteEnumTraits(name: str, traits_header_file: typing.IO) -> None:
traits_header_file.write(
"""
template <>
struct EnumTraits<gpu::mojom::%s, %s> {
static gpu::mojom::%s ToMojom(%s input) {
switch (input) {
""" % (name, name, name, name))
for value_name, _, mojom_value_name in _enums[name]:
traits_header_file.write(
"""
case %s::%s:
return gpu::mojom::%s::%s;"""
% (name, value_name, name, mojom_value_name))
traits_header_file.write(
"""
default:
NOTREACHED();
}
}
static bool FromMojom(gpu::mojom::%s input, %s* out) {
switch (input) {
""" % (name, name))
for value_name, _, mojom_value_name in _enums[name]:
traits_header_file.write(
"""
case gpu::mojom::%s::%s:
*out = %s::%s;
return true;""" % (name, mojom_value_name, name, value_name))
traits_header_file.write(
"""
case gpu::mojom::%s::INVALID_VALUE:
NOTREACHED();
}
NOTREACHED();
}
};""" % name)
def GenerateTraitsFile(traits_header_file: typing.IO,
traits_source_file: typing.IO) -> None:
traits_header_file.write(
"""// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file is auto-generated from
// gpu/ipc/common/generate_vulkan_types.py
// It's formatted by clang-format using chromium coding style:
// clang-format -i -style=chromium filename
// DO NOT EDIT!
#ifndef GPU_IPC_COMMON_VULKAN_TYPES_MOJOM_TRAITS_H_
#define GPU_IPC_COMMON_VULKAN_TYPES_MOJOM_TRAITS_H_
#include <string_view>
#include "base/containers/span.h"
#include "gpu/ipc/common/vulkan_types.h"
#include "gpu/ipc/common/vulkan_types.mojom-shared.h"
namespace mojo {
""")
traits_source_file.write(
"""// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file is auto-generated from
// gpu/ipc/common/generate_vulkan_types.py
// It's formatted by clang-format using chromium coding style:
// clang-format -i -style=chromium filename
// DO NOT EDIT!
#include "gpu/ipc/common/vulkan_info_mojom_traits.h"
namespace mojo {
""")
for t in _generated_types:
if t in _structs:
WriteStructTraits(t, traits_header_file, traits_source_file)
elif t in _enums:
WriteEnumTraits(t, traits_header_file)
traits_header_file.write(
"""
} // namespace mojo
#endif // GPU_IPC_COMMON_VULKAN_TYPES_MOJOM_TRAITS_H_""")
traits_source_file.write(
"""
} // namespace mojo""")
def GenerateTypemapFile(typemap_file: typing.IO) -> None:
typemap_file.write(
"""# Copyright 2019 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# This file is auto-generated from
# gpu/ipc/common/generate_vulkan_types.py
# DO NOT EDIT!
generated_vulkan_type_mappings = [""")
for t in _generated_types:
typemap_file.write(
"""
{
mojom = "gpu.mojom.%s"
cpp = "::%s"
},""" % (t, t))
typemap_file.write("\n]\n")
def main() -> int:
"""This is the main function."""
parser = argparse.ArgumentParser()
parser.add_argument(
"--output-dir",
help="Output directory for generated files. Defaults to this script's "
"directory.")
parser.add_argument(
"-c", "--check", action="store_true",
help="Check if output files match generated files in chromium root "
"directory. Use this in PRESUBMIT scripts with --output-dir.")
args = parser.parse_args()
# Support generating files for PRESUBMIT.
if args.output_dir:
output_dir = args.output_dir
else:
output_dir = _SELF_LOCATION
def ClangFormat(filename):
formatter = "clang-format"
if platform.system() == "Windows":
formatter += ".bat"
subprocess.call([formatter, "-i", "-style=chromium", filename])
vk_xml_file_path = os.path.join(
_SELF_LOCATION, "../../..", _VK_XML_FILE)
ParseVkXMLFile(vk_xml_file_path)
mojom_file_name = "vulkan_types.mojom"
mojom_file = open(
os.path.join(output_dir, mojom_file_name), 'w', newline='')
GenerateMojom(mojom_file)
mojom_file.close()
ClangFormat(mojom_file.name)
traits_header_file_name = "vulkan_types_mojom_traits.h"
traits_header_file = \
open(os.path.join(output_dir, traits_header_file_name), 'w', newline='')
traits_source_file_name = "vulkan_types_mojom_traits.cc"
traits_source_file = \
open(os.path.join(output_dir, traits_source_file_name), 'w', newline='')
GenerateTraitsFile(traits_header_file, traits_source_file)
traits_header_file.close()
ClangFormat(traits_header_file.name)
traits_source_file.close()
ClangFormat(traits_source_file.name)
typemap_file_name = "generated_vulkan_type_mappings.gni"
typemap_file = open(
os.path.join(output_dir, typemap_file_name), 'w', newline='')
GenerateTypemapFile(typemap_file)
typemap_file.close()
check_failed_filenames = []
if args.check:
for filename in [mojom_file_name, traits_header_file_name,
traits_source_file_name, typemap_file_name]:
if not filecmp.cmp(os.path.join(output_dir, filename),
os.path.join(_SELF_LOCATION, filename)):
check_failed_filenames.append(filename)
if len(check_failed_filenames) > 0:
print('Please run gpu/ipc/common/generate_vulkan_types.py')
print('Failed check on generated files:')
for filename in check_failed_filenames:
print(filename)
return 1
return 0
if __name__ == '__main__':
sys.exit(main())