-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvkxml.py
More file actions
971 lines (786 loc) · 30 KB
/
vkxml.py
File metadata and controls
971 lines (786 loc) · 30 KB
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
# Copyright 2020 Google LLC
# SPDX-License-Identifier: MIT
import xml.etree.ElementTree as ET
class VkDecl:
"""Parse a C-declaration like 'int* const* const blah[4]' into
name := 'blah'
type_name := 'int'
type_decor.qual = 'const'
type_decor.dim = '4'
type_decor.ref_quals = ['', 'const']
"""
class Decor:
def __init__(self, qual, dim, bit_size, ref_quals):
self.qual = qual
self.dim = dim
self.bit_size = bit_size
self.ref_quals = ref_quals
def __init__(self, name, type_name, type_decor=None):
if not type_decor:
type_decor = self.Decor(None, None, None, [])
self.name = name
self.type_name = type_name
self.type_decor = type_decor
def to_c(self, type_only):
c_decl = self.type_name
quals = self.type_decor.ref_quals[:]
quals.append(self.type_decor.qual)
for i, qual in enumerate(quals):
is_first = i == 0
is_last = i == len(quals) - 1
if qual:
# first qualifier is prepended
if is_first:
c_decl = qual + ' ' + c_decl
else:
c_decl = c_decl + ' ' + qual
if is_last:
if not type_only:
c_decl = c_decl + ' ' + self.name
if self.type_decor.dim:
if c_decl[-1] == '*':
c_decl = c_decl + ' '
c_decl = c_decl + '[' + self.type_decor.dim + ']'
if self.type_decor.bit_size:
c_decl = c_decl + ':' + self.type_decor.bit_size
else:
c_decl += '*'
return c_decl
@staticmethod
def from_c(c_decl):
"""This is very limited."""
# extract bit size
bit_size = None
index = c_decl.find(':')
if index != -1:
bit_size = c_decl[index + 1:].strip()
c_decl = c_decl[:index]
# extract array size
array_size = None
index = c_decl.find('[')
if index != -1:
array_size = c_decl[index + 1:c_decl.rfind(']')].strip()
c_decl = c_decl[:index]
# extract name
end = len(c_decl)
while not c_decl[end - 1].isalnum():
end -= 1
index = c_decl.rfind(' ', 0, end)
name = c_decl[index + 1:end]
c_decl = c_decl[:index]
# extract base type which is always before the first '*'
quals = c_decl.split('*')
qualified_type_name = quals[0].split()
type_name = qualified_type_name.pop()
quals[0] = ' '.join(qualified_type_name)
ref_quals = [qual.strip() for qual in quals]
qual = ref_quals.pop()
type_decor = VkDecl.Decor(qual, array_size, bit_size, ref_quals)
return VkDecl(name, type_name, type_decor)
class VkVariable:
def __init__(self, ty, name='unnamed', attrs={}):
self.ty = ty
self.name = name
self.attrs = attrs
def can_validate(self):
if 'noautovalidity' in self.attrs and \
self.attrs['noautovalidity'] == 'true':
return False
return True
def maybe_null(self):
return self.ty.is_pointer() and self.is_optional()
def is_optional(self):
return 'optional' in self.attrs and \
self.attrs['optional'][0] == 'true'
def is_blob(self):
return self.ty.indirection_depth() == 1 and \
not self.ty.is_static_array() and \
self.ty.base.name == 'void' and \
'len_exprs' in self.attrs
def is_dynamic_array(self):
return self.ty.is_pointer() and 'len_exprs' in self.attrs
def has_c_string(self):
"""Return True for C-strings and arrays of C-strings."""
if not self.is_dynamic_array():
return False
for len_expr in self.attrs['len_exprs']:
if len_expr == 'null-terminated':
return True
return False
def is_p_next(self):
return self.name == 'pNext'
def to_c(self):
return VkDecl(self.name, self.ty.base.name, self.ty.decor).to_c(False)
class VkType:
INCLUDE = 0
DEFINE = 1
DEFAULT = 2
BASETYPE = 3
HANDLE = 4
ENUM = 5
BITMASK = 6
STRUCT = 7
UNION = 8
FUNCPOINTER = 9
COMMAND = 10
DERIVED = 11
CATEGORY_COUNT = 12
def __init__(self):
self.name = None
self.category = None
self.base = None
self.aliases = []
self.ext_aliases = {}
self.attrs = {}
# True if defined by private XMLs
self.is_private = None
# for DEFINE
self.define = None
# for BASETYPE/BITMASK
self.typedef = None
# for HANDLE
self.dispatchable = None
# for ENUM
self.enums = None
# for BITMASK (optional)
self.requires = None
# for STRUCT (optional)
self.s_type = None
self._struct_extends = []
self.p_next = []
# for FUNCPOINTER/COMMAND
self.ret = None
self.can_device_lost = False
# for STRUCT/UNION/FUNCPOINTER/COMMAND
self.variables = []
# for DERIVED
self.decor = None
def init(self, name, category):
assert name and self.name is None
assert category < self.CATEGORY_COUNT and self.category is None
self.name = name
self.category = category
if category != self.DERIVED:
self.base = self
def validate(self):
assert self.name is not None
assert self.category is not None
assert self.base == self.base.base
if self.category != self.INCLUDE:
assert self.base.name.isidentifier()
if self.category == self.DERIVED:
assert self.base != self
assert self.decor
else:
assert self.base == self
assert self.decor is None
if self.category == self.BASETYPE and self.typedef:
assert self.typedef == self.typedef.base
if self.s_type:
assert self.variables[0].name == 'sType'
assert self.variables[1].name == 'pNext'
def is_static_array(self):
return bool(self.decor.dim) if self.decor else False
def static_array_size(self):
return self.decor.dim if self.decor else None
def is_pointer(self):
return bool(self.decor.ref_quals) if self.decor else False
def indirection_depth(self):
return len(self.decor.ref_quals) if self.decor else 0
def is_const_static_array(self):
return self.is_static_array() and 'const' in self.decor.qual
def is_const_pointer(self):
if self.is_pointer():
for qual in self.decor.ref_quals:
if 'const' in qual:
return True
return False
def is_c_string(self):
"""Return True if the type is a C-string.
This does not include char arrays or arrays of C-strings.
"""
return self.base.name == 'char' and self.indirection_depth() == 1
def find_variables(self, len_name):
names = len_name.split('->')
var_list = []
for name in names:
if var_list:
candidates = var_list[-1].ty.base.variables
else:
candidates = self.base.variables
found = None
for var in candidates:
if var.name == name:
found = var
break
if not found:
return []
var_list.append(found)
return var_list
def _add_deps(self, deps):
ty = self.base
if ty.typedef:
ty.typedef._add_deps(deps)
if ty.requires:
ty.requires._add_deps(deps)
# ty.p_next is not a dependency
if ty.ret:
ty.ret.ty._add_deps(deps)
for var in ty.variables:
if var.ty.base != ty:
var.ty._add_deps(deps)
if ty not in deps:
deps.append(ty)
def get_dependencies(self):
deps = []
self._add_deps(deps)
return deps
def set_attribute(self, key, val):
ty = self.base
# Avoid circular reference.
# VkBaseOutStructure refers to itself in
# VkBaseOutStructure* VkBaseOutStructure::pNext
if ty.name == 'VkBaseOutStructure':
return
ty.attrs[key] = val
for var in ty.variables:
var.ty.set_attribute(key, val)
for next_ty in ty.p_next:
next_ty.set_attribute(key, val)
def c_func_ret(self):
return self.ret.ty.name if self.ret else 'void'
def c_func_params(self, separator=', '):
c_params = [var.to_c() for var in self.variables]
return separator.join(c_params)
def c_func_args(self, separator=', '):
c_args = [var.name for var in self.variables]
return separator.join(c_args)
@staticmethod
def _get_inner_text(elem):
text = []
if elem.text:
text.append(elem.text)
for child in elem:
if child.tag == 'comment':
continue
if child.text:
text.append(child.text)
if child.tail:
text.append(child.tail)
return ' '.join(text)
@staticmethod
def _get_type(key, type_table):
if isinstance(key, VkDecl):
base_name = key.type_name
name = key.to_c(True)
else:
base_name = key
name = key
# return the type if exists
if name in type_table:
return type_table[name]
# get (or create) the base type first
if base_name in type_table:
base_ty = type_table[base_name]
else:
base_ty = VkType()
type_table[base_name] = base_ty
# this is a base type
if name == base_name:
return base_ty
# create the derived type
ty = VkType()
ty.init(name, ty.DERIVED)
ty.base = base_ty
ty.decor = key.type_decor
type_table[name] = ty
return ty
@staticmethod
def _parse_alias(type_elem, type_table):
name = type_elem.attrib['name']
alias = type_elem.attrib['alias']
assert name not in type_table
ty = VkType._get_type(alias, type_table)
ty.aliases.append(name)
type_table[name] = ty
@staticmethod
def _parse_variable(elem, type_table):
c_decl = VkType._get_inner_text(elem)
decl = VkDecl.from_c(c_decl)
# sanity check
assert decl.name == elem.find('name').text
assert decl.type_name == elem.find('type').text
enum_elem = elem.find('enum')
if enum_elem is not None:
assert decl.type_decor.dim == enum_elem.text
ty = VkType._get_type(decl, type_table)
attrs = {}
if 'values' in elem.attrib:
attrs['values'] = elem.attrib['values'].split(',')
if 'len' in elem.attrib:
lens = []
if 'altlen' in elem.attrib:
lens = elem.attrib['altlen'].split(',')
else:
lens = elem.attrib['len'].split(',')
len_exprs = []
len_names = []
for l in lens:
if l == 'null-terminated':
len_exprs.append(l)
len_names.append('')
continue
len_exprs.append(l)
# extract "thisFoo->thatBar"
begin = 0
while begin < len(l):
if l[begin].islower():
break
begin += 1
end = begin + 1
while end < len(l):
if l[end].isidentifier() or l[end].isdigit():
end += 1
elif l[end:end + 2] == '->':
end += 2
else:
break
len_names.append(l[begin:end])
if len_exprs:
attrs['len_exprs'] = len_exprs
attrs['len_names'] = len_names
if 'optional' in elem.attrib:
attrs['optional'] = elem.attrib['optional'].split(',')
if 'noautovalidity' in elem.attrib:
attrs['noautovalidity'] = elem.attrib['noautovalidity']
if 'stride' in elem.attrib:
attrs['stride'] = elem.attrib['stride']
return VkVariable(ty, decl.name, attrs)
@staticmethod
def _parse_type_define(ty, type_elem, type_table):
# get and save the #define macro
ty.define = VkType._get_inner_text(type_elem)
@staticmethod
def _parse_type_basetype(ty, type_elem, type_table):
typedef_elem = type_elem.find('type')
if typedef_elem is not None:
ty.typedef = VkType._get_type(typedef_elem.text, type_table)
@staticmethod
def _parse_type_handle(ty, type_elem, type_table):
if type_elem.find('type').text == 'VK_DEFINE_HANDLE':
ty.dispatchable = True
@staticmethod
def _parse_type_enum(ty, type_elem, type_table):
# enum values will be filled in when <enums> is parsed
ty.enums = VkEnums()
@staticmethod
def _parse_type_bitmask(ty, type_elem, type_table):
to = type_elem.find('type').text
assert to in ['VkFlags', 'VkFlags64']
requires_ty = None
if 'requires' in type_elem.attrib:
requires = type_elem.attrib['requires']
requires_ty = VkType._get_type(requires, type_table)
elif 'bitvalues' in type_elem.attrib:
requires = type_elem.attrib['bitvalues']
requires_ty = VkType._get_type(requires, type_table)
ty.typedef = VkType._get_type(to, type_table)
ty.requires = requires_ty
@staticmethod
def _parse_type_struct(ty, type_elem, type_table):
members = []
for member_elem in type_elem.iterfind('member'):
var = VkType._parse_variable(member_elem, type_table)
members.append(var)
s_type = None
if members[0].name == 'sType' and 'values' in members[0].attrs:
s_type = members[0].attrs['values'][0]
struct_extends = []
if 'structextends' in type_elem.attrib:
struct_extends = type_elem.attrib['structextends'].split(',')
returnedonly = type_elem.attrib.get(
'returnedonly', 'false') != 'false'
ty.variables = members
ty.s_type = s_type
ty._struct_extends = struct_extends
if returnedonly:
ty.attrs['returnedonly'] = True
@staticmethod
def _parse_type_union(ty, type_elem, type_table):
VkType._parse_type_struct(ty, type_elem, type_table)
@staticmethod
def _parse_type_funcpointer(ty, type_elem, type_table):
c_decls = VkType._get_inner_text(type_elem).splitlines()
# clean up the first line to abuse VkDecl
c_decl = c_decls.pop(0)
assert c_decl.startswith('typedef ')
c_decl = c_decl[8:]
c_decl = c_decl.replace('(VKAPI_PTR * ', '', 1)
index = c_decl.rfind('(')
c_decl = c_decl[:index]
decl = VkDecl.from_c(c_decl)
assert ty.name == decl.name
ret_ty = VkType._get_type(decl, type_table)
if ret_ty.name == 'void':
ret_ty = None
params = []
for c_decl in c_decls:
decl = VkDecl.from_c(c_decl)
param_ty = VkType._get_type(decl, type_table)
params.append(VkVariable(param_ty, decl.name))
ty.variables = params
if ret_ty:
ty.ret = VkVariable(ret_ty, 'ret')
@staticmethod
def parse_type(type_elem, type_table):
"""Parse <type> into a VkType."""
if 'alias' in type_elem.attrib:
VkType._parse_alias(type_elem, type_table)
return
category, parse_func = {
'include': (VkType.INCLUDE, None),
'define': (VkType.DEFINE, VkType._parse_type_define),
None: (VkType.DEFAULT, None),
'basetype': (VkType.BASETYPE, VkType._parse_type_basetype),
'handle': (VkType.HANDLE, VkType._parse_type_handle),
'enum': (VkType.ENUM, VkType._parse_type_enum),
'bitmask': (VkType.BITMASK, VkType._parse_type_bitmask),
'struct': (VkType.STRUCT, VkType._parse_type_struct),
'union': (VkType.UNION, VkType._parse_type_union),
'funcpointer': (VkType.FUNCPOINTER, VkType._parse_type_funcpointer),
}[type_elem.attrib.get('category')]
if 'name' in type_elem.attrib:
name = type_elem.attrib['name']
else:
name = type_elem.find('name').text
ty = VkType._get_type(name, type_table)
ty.init(name, category)
if parse_func:
parse_func(ty, type_elem, type_table)
@staticmethod
def parse_command(command_elem, type_table):
"""Parse <command> into a VkType."""
if 'alias' in command_elem.attrib:
VkType._parse_alias(command_elem, type_table)
return
name = None
params = []
ret_ty = None
errorcodes = []
for child in command_elem:
if child.tag == 'proto':
c_decl = VkType._get_inner_text(child)
decl = VkDecl.from_c(c_decl)
name = decl.name
ret_ty = VkType._get_type(decl, type_table)
if ret_ty.name == 'void':
ret_ty = None
elif child.tag == 'param':
var = VkType._parse_variable(child, type_table)
params.append(var)
assert name == command_elem.find('proto').find('name').text
ty = VkType._get_type(name, type_table)
ty.init(name, ty.COMMAND)
ty.variables = params
if ret_ty:
ty.ret = VkVariable(ret_ty, 'ret')
if 'errorcodes' in command_elem.attrib:
errorcodes = command_elem.attrib['errorcodes'].split(',')
ty.can_device_lost = 'VK_ERROR_DEVICE_LOST' in errorcodes
class VkEnums:
"""Represent a <enums>."""
def __init__(self):
self.bitwidth = 32
self.values = {}
self.vk_xml_values = None
def init_values(self, bitwidth, values):
assert not self.values
self.bitwidth = bitwidth
self.values = values
def extend_value(self, enum_elem, ext_number):
key, val = self._parse_enum(enum_elem, ext_number)
if key in self.values:
assert self.values[key] == val
else:
self.values[key] = val
@staticmethod
def _parse_enum(enum_elem, ext_number=None):
"""Parse <enum> into a (key, val) pair."""
key = enum_elem.attrib['name']
if 'alias' in enum_elem.attrib:
val = enum_elem.attrib['alias']
elif 'value' in enum_elem.attrib:
val = enum_elem.attrib['value']
elif 'bitpos' in enum_elem.attrib:
bit = int(enum_elem.attrib['bitpos'])
val = '0x%08x' % (1 << bit)
elif 'offset' in enum_elem.attrib:
offset = int(enum_elem.attrib['offset'])
extnumber = int(enum_elem.attrib.get('extnumber', ext_number))
assert extnumber > 0
val = str(1000000000 + (extnumber - 1) * 1000 + offset)
else:
assert False
if 'dir' in enum_elem.attrib:
val = enum_elem.attrib['dir'] + val
return key, val
@staticmethod
def parse_enums(enums_elem, type_table):
"""Parse <enums> and update the corresponding VkType."""
name = enums_elem.attrib['name']
bitwidth = 32
if 'bitwidth' in enums_elem.attrib:
bitwidth = int(enums_elem.attrib['bitwidth'])
values = {}
for enum_elem in enums_elem.iterfind('enum'):
key, val = VkEnums._parse_enum(enum_elem, values)
assert key not in values
values[key] = val
# look up and update VkType
ty = type_table[name]
ty.enums.init_values(bitwidth, values)
class VkFeature:
"""Represent a <feature>."""
def __init__(self, api, name, number, types):
self.api = api
self.name = name
self.number = number
self.types = types
@staticmethod
def parse_require(require_elem, type_table, ext_number=None):
"""Parse <require> into a list of VkType."""
types = []
names = []
for child in require_elem:
if child.tag == 'enum':
if 'extends' not in child.attrib:
continue
ty = type_table[child.attrib['extends']]
ty.enums.extend_value(child, ext_number)
elif child.tag in ['type', 'command']:
name = child.attrib['name']
ty = type_table[name]
types.append(ty)
names.append(name)
return (types, names)
@staticmethod
def parse_feature(feature_elem, type_table):
"""Parse <feature> into a VkFeature."""
api = feature_elem.attrib['api']
name = feature_elem.attrib['name']
number = feature_elem.attrib['number']
types = []
for require_elem in feature_elem.iterfind('require'):
require_types, _ = VkFeature.parse_require(require_elem,
type_table)
for ty in require_types:
if ty not in types:
types.append(ty)
return VkFeature(api, name, number, types)
class VkExtension:
"""Represent a <extension>."""
def __init__(self, name, number, supported):
self.name = name
self.number = int(number)
self.supported = supported
self.platform = None
self.promoted = None
self.requires = []
self.version = 0
self.types = []
self.optional_types = {}
@staticmethod
def parse_extension(elem, type_table):
"""Parse <extension> into a VkExtension."""
name = elem.attrib['name']
number = elem.attrib['number']
supported = elem.attrib['supported'].split(',')
ext = VkExtension(name, number, supported)
ext.platform = elem.attrib.get('platform')
ext.promoted = elem.attrib.get('promotedto')
reqs = elem.attrib.get('requires')
if reqs:
ext.requires = reqs.split(',')
for require_elem in elem.iterfind('require'):
# parse SPEC_VERSION to get the extension version
for enum_elem in require_elem.iterfind('enum'):
if enum_elem.attrib['name'].endswith('SPEC_VERSION'):
ext.version = int(enum_elem.attrib['value'])
break
if 'vulkan' not in ext.supported:
continue
require_types, require_names = VkFeature.parse_require(
require_elem, type_table, number)
# check if this <require> depends on another extension
require_dep = require_elem.attrib.get('depends')
if not require_dep:
# fallback to check extension for legacy vk registry
require_dep = require_elem.attrib.get('extension')
if require_dep and not require_dep.startswith('VK_VERSION'):
if require_dep not in ext.optional_types:
ext.optional_types[require_dep] = []
types = ext.optional_types[require_dep]
else:
types = ext.types
for ty, alias in zip(require_types, require_names):
ty.ext_aliases[name] = alias
if ty not in types:
types.append(ty)
return ext
class VkRegistry:
"""Represent a <registry>."""
def __init__(self):
self.platform_guards = {}
self.tags = []
self.type_table = {}
self.features = []
self.extensions = []
self.vk_xml_extension_count = 0
self.vk_xml_version = None
self.max_vk_command_type_value = None
@staticmethod
def parse(vk_xml, private_xmls=[]):
"""Parse vk.xml and optional private XMLs into a VkRegistry."""
reg = VkRegistry()
reg._parse_xml(vk_xml)
reg.vk_xml_extension_count = len(reg.extensions)
vk_xml_types = set(reg.type_table.values())
for ty in vk_xml_types:
if ty.category == ty.ENUM:
ty.enums.vk_xml_values = set(ty.enums.values.keys())
for xml in private_xmls:
reg._parse_xml(xml)
reg._resolve(vk_xml_types)
reg._validate()
return reg
def upper_name(self, name):
"""Convert FooBarEXT to FOO_BAR_EXT."""
suffix = ''
for tag in self.tags:
if name.endswith(tag):
name = name[:-len(tag)]
suffix = '_' + tag
break
underscore = "".join([c if c.islower() else '_' + c for c in name])
return underscore.lstrip('_').upper() + suffix
@staticmethod
def is_vulkansc(child):
return 'api' in child.attrib and child.attrib['api'] == 'vulkansc'
@staticmethod
def filter_vulkansc(root):
root[:] = [child for child in root if not VkRegistry.is_vulkansc(child)]
for child in root:
VkRegistry.filter_vulkansc(child)
def _parse_xml(self, xml):
tree = ET.parse(xml)
root = tree.getroot()
self.filter_vulkansc(root)
for child in root:
if child.tag == 'platforms':
self._parse_platforms(child)
elif child.tag == 'tags':
self._parse_tags(child)
elif child.tag == 'types':
self._parse_types(child)
elif child.tag == 'enums':
self._parse_enums(child)
elif child.tag == 'commands':
self._parse_commands(child)
elif child.tag == 'feature':
self._parse_feature(child)
elif child.tag == 'extensions':
self._parse_extensions(child)
def _parse_platforms(self, platforms_elem):
"""Parse <platforms>."""
for plat_elem in platforms_elem.iterfind('platform'):
self.platform_guards[plat_elem.attrib['name']] = \
plat_elem.attrib['protect']
def _parse_tags(self, tags_elem):
"""Parse <tags>."""
for tag_elem in tags_elem.iterfind('tag'):
self.tags.append(tag_elem.attrib['name'])
def _parse_types(self, types_elem):
"""Parse <types>."""
for type_elem in types_elem.iterfind('type'):
VkType.parse_type(type_elem, self.type_table)
def _parse_enums(self, enums_elem):
"""Parse <enums>. There is one for each enumerated type."""
if 'type' in enums_elem.attrib:
VkEnums.parse_enums(enums_elem, self.type_table)
def _parse_commands(self, commands_elem):
"""Parse <commands>."""
for command_elem in commands_elem.iterfind('command'):
VkType.parse_command(command_elem, self.type_table)
def _parse_feature(self, feature_elem):
"""Parse <feature>. There is one for each Vulkan version."""
feat = VkFeature.parse_feature(feature_elem, self.type_table)
self.features.append(feat)
def _parse_extensions(self, extensions_elem):
"""Parse <extensions>."""
for extension_elem in extensions_elem.iterfind('extension'):
ext = VkExtension.parse_extension(extension_elem, self.type_table)
self.extensions.append(ext)
def _get_xml_version(self, ver_ty, complete_ver_ty):
ver = ver_ty.define
ver = ver[(ver.rindex(' ') + 1):]
assert ver.isdigit()
complete_ver = complete_ver_ty.define
complete_ver = complete_ver[(complete_ver.rindex('(') + 1):-1]
complete_ver = complete_ver.replace(ver_ty.name, ver)
if complete_ver.count(',') == 3:
return 'VK_MAKE_API_VERSION(%s)' % complete_ver
else:
return 'VK_MAKE_VERSION(%s)' % complete_ver
def _resolve(self, vk_xml_types):
"""Resolve after all XMLs are parsed."""
for name, ty in self.type_table.items():
# skip aliases
if name in ty.aliases:
continue
ty.is_private = ty not in vk_xml_types
# add ty to p_next of the type it extends
if ty._struct_extends:
extended_tys = [VkType._get_type(name, self.type_table) for name in ty._struct_extends]
for extended_ty in extended_tys:
assert ty not in extended_ty.p_next
extended_ty.p_next.append(ty)
if ty.category != ty.ENUM or not ty.enums.values:
continue
# resolve enum value aliases
for key, val in ty.enums.values.items():
if val not in ty.enums.values:
continue
# resolve alias
while val in ty.enums.values:
val = ty.enums.values[val]
ty.enums.values[key] = val
self.vk_xml_version = self._get_xml_version(
self.type_table['VK_HEADER_VERSION'],
self.type_table['VK_HEADER_VERSION_COMPLETE'])
max_val = 0
command_type_enums = self.type_table['VkCommandTypeEXT'].enums.values
for val in command_type_enums.values():
max_val = max(max_val, int(val))
self.max_vk_command_type_value = max_val
def _validate(self):
"""Sanity check."""
for ty in self.type_table.values():
ty.validate()
def test():
C_DECLS = [
'int a',
'int* a',
'const int a',
'const int* a',
'int* const a',
'int a[3]',
'int* a[3]',
'const int a[3]',
'const int* a[3]',
'int* const a[3]',
]
for c_decl in C_DECLS:
decl = VkDecl.from_c(c_decl)
assert decl.to_c(False) == c_decl
if __name__ == '__main__':
test()