-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathgenerate.py
1002 lines (857 loc) · 37.2 KB
/
generate.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
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
# This file is part of the "Learn WebGPU for C++" book.
# https://github.com/eliemichel/LearnWebGPU
#
# MIT License
# Copyright (c) 2022-2024 Elie Michel
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# NB: The process is inpired by PpluX' wgpu.hpp generator
# (see https://github.com/pplux/wgpu.hpp )
import re
from dataclasses import dataclass, field
from collections import defaultdict
import os
from os.path import dirname, isfile, join
from typing import Dict, List
import logging
DEFAULT_HEADER_URL = "https://raw.githubusercontent.com/webgpu-native/webgpu-headers/main/webgpu.h"
def makeArgParser():
import argparse
parser = argparse.ArgumentParser(description="""
Generate the webgpu-cpp binding from official webgpu-native headers.
You should not have to change any of the default arguments for a regular use.
This generates a webgpu.hpp file that you can include in your project.
Exactly one of your source files must #define WEBGPU_CPP_IMPLEMENTATION
before including this header.
""")
parser.add_argument("-t", "--template", type=str,
default="webgpu.template.hpp",
help="Template used for generating the output binding file")
parser.add_argument("-o", "--output", type=str,
default="webgpu.hpp",
help="Path where to output the generated webgpu.hpp")
parser.add_argument("-u", "--header-url", action='append',
default=[],
help=f"""
URL of the official webgpu.h from the webgpu-native project. If the URL
does not start with http(s)://, it is considered as a local file. You can
specify this option multiple times to agregate multiple headers (e.g.,
the standard webgpu.h plus backend-specific extensions wgpu.h).
If no URL is specified, the official header from '{DEFAULT_HEADER_URL}'
is used.
""")
parser.add_argument("-d", "--defaults", action='append',
default=[],
help="""
File listing default values for descriptor fields. This argument can
be provided multiple times, the last ones override the previous values.
""")
parser.add_argument("--ext-suffix",
default="",
help="""
Extension number needed for Dawn, which uses them to maintain backward
compatibility of their API (might get removed once version 1 is out).
Set to "2" when using dawn and leave to the empty default with wgpu-native.
""")
# Advanced options
parser.add_argument("--no-scoped-enums", action='store_false', dest="use_scoped_enums",
help="Do not replace WebGPU enums with C++ scoped enums")
parser.add_argument("--no-fake-scoped-enums", action='store_false', dest="use_fake_scoped_enums",
help="Use scoped aliases to original enum values so that no casting is needed")
parser.add_argument("--use-non-member-procedures", action='store_true',
help="Include WebGPU methods that are not members of any WebGPU object")
parser.add_argument("--no-const", action='store_false', dest="use_const",
help="By default, all methods of opaque handle types are const. This option makes them all non-const.")
parser.add_argument("--use-init-macros", action='store_true',
help="Use initialization macros provided by webgpu.h instead of writing custom setDefaults methods.")
parser.add_argument("--use-inline", action='store_true', dest="use_inline",
help="Make all methods inlined (seems to have an effect with clang, but MSVC fails at linking in that case).")
return parser
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
def main(args):
applyDefaultArgs(args)
template, meta = loadTemplate(args.template)
api = WebGpuApi()
for url in args.header_url:
header = downloadHeader(url)
parseHeader(api, header)
loadDefaults(args, api)
binding = produceBinding(args, api, meta)
generateOutput(args.output, template, binding)
def applyDefaultArgs(args):
if not args.header_url:
args.header_url = [DEFAULT_HEADER_URL]
if not args.defaults:
args.defaults = ["defaults.txt", "extra-defaults.txt"]
if hasattr(args, "virtual_fs"):
VfsFile.virtual_fs = args.virtual_fs
# -----------------------------------------------------------------------------
# Virtual File System, to enable using this script as a lib (in an environement
# where there is no file system).
class VfsFile():
virtual_fs = None
def __init__(self, filename):
self.filename = filename
def __enter__(self):
stream = VfsFile.virtual_fs[self.filename]
stream.seek(0)
return stream
def __exit__(self, exc_type, exc_val, exc_tb):
pass
def openVfs(filename, mode='r', **kwargs):
"""
This is a wrapper around the standard 'open' that enables paths starting
with "vfs://" to refer to files in a virtual file system.
"""
if filename.startswith("vfs://"):
return VfsFile(filename[6:])
else:
return open(filename, mode, **kwargs)
def isfileVfs(filename):
if filename.startswith("vfs://"):
return filename[6:] in VfsFile.virtual_fs
else:
return isfile(filename)
# -----------------------------------------------------------------------------
# Parser, for analyzing webgpu.h
# The output of parsing is a WebGpuApi object
@dataclass
class PropertyApi:
name: str
type: str
counter: str|None = None # list properties have an associated counter property
default_value: str|None = None
@dataclass
class HandleApi:
"""WebGPU objects manipulated through blind handles"""
name: str
properties: list[PropertyApi] = field(default_factory=list)
@dataclass
class ClassApi:
"""WebGPU structs whose fields are directly manipulated"""
name: str
parent: str|None = None
properties: list[PropertyApi] = field(default_factory=list)
is_descriptor: bool = False
default_overrides: list[(str,str)] = field(default_factory=list)
@dataclass
class ProcedureArgumentApi:
name: str
type: str
nullable: bool = False
@dataclass
class ProcedureApi:
name: str
return_type: str
parent: str|None = None
arguments: list[ProcedureArgumentApi] = field(default_factory=list)
@dataclass
class EnumerationEntryApi:
key: str
value: str
@dataclass
class EnumerationApi:
name: str
entries: list[EnumerationEntryApi] = field(default_factory=list)
@dataclass
class CallbackApi:
name: str
arguments: list[ProcedureArgumentApi] = field(default_factory=list)
raw_arguments: str = ""
@dataclass
class TypeAliasApi:
aliased_type: str
wgpu_type: str
@dataclass
class WebGpuApi:
handles: list[HandleApi] = field(default_factory=list)
classes: list[ClassApi] = field(default_factory=list)
procedures: list[ProcedureApi] = field(default_factory=list)
enumerations: list[EnumerationApi] = field(default_factory=list)
callbacks: list[CallbackApi] = field(default_factory=list)
type_aliases: list[TypeAliasApi] = field(default_factory=list)
stypes: dict[str,str] = field(default_factory=dict) # Name => SType::Name
init_macros: list[str] = field(default_factory=list)
def parseHeader(api, header):
"""
Add fields to api while reading a header file
"""
it = iter([
line
.replace("WGPU_OBJECT_ATTRIBUTE", "")
.replace("WGPU_ENUM_ATTRIBUTE", "")
.replace("WGPU_STRUCTURE_ATTRIBUTE", "")
.replace("WGPU_FUNCTION_ATTRIBUTE", "")
#.replace("WGPU_NULLABLE", "")
for line in header.split("\n")
])
struct_re = re.compile(r"struct *WGPU(\w+) *{")
handle_re = re.compile(r"typedef struct .*WGPU([^_]\w+)\s*;")
procedure_re = re.compile(r"(?:WGPU_EXPORT\s+)?([\w *]+) wgpu(\w+)\((.*)\)\s*;")
enum_re = re.compile(r"typedef enum WGPU(\w+) {")
flag_enum_re = re.compile(r"typedef WGPUFlags WGPU(\w+)Flags\s*;")
new_flag_enum_re = re.compile(r"typedef WGPUFlags WGPU(\w+)\s*;")
flag_value_re = re.compile(r"static const WGPU(\w+) WGPU(\w+)_(\w+) = (\w+)( /\*(.*)\*/)?;")
typedef_re = re.compile(r"typedef (\w+) WGPU(\w+)\s*;")
callback_re = re.compile(r"typedef void \(\*WGPU(\w+)Callback\)\((.*)\)\s*;")
init_macro_re = re.compile(r"#define (WGPU_[A-Z0-9_]+_INIT)")
while (x := next(it, None)) is not None:
if (match := struct_re.search(x)):
struct_name = match.group(1)
api.classes.append(parseClass(struct_name, it))
continue
if (match := handle_re.search(x)):
struct_name = match.group(1)
api.handles.append(HandleApi(name=struct_name))
continue
if (match := procedure_re.search(x)):
return_type = match.group(1)
if return_type.startswith("WGPU_EXPORT"):
return_type = return_type[len("WGPU_EXPORT"):]
return_type = return_type.strip()
api.procedures.append(ProcedureApi(
name=match.group(2),
return_type=return_type,
arguments=parseProcArgs(match.group(3)),
))
continue
if (match := enum_re.search(x)):
name = match.group(1)
api.enumerations.append(parseEnum(name, it, api.stypes))
continue
if (match := flag_enum_re.search(x)):
api.type_aliases.append(TypeAliasApi(
aliased_type="WGPUFlags",
wgpu_type=match.group(1) + "Flags",
))
continue
if (match := new_flag_enum_re.search(x)):
api.enumerations.append(EnumerationApi(
name=match.group(1),
))
continue
if (match := flag_value_re.search(x)):
enum_name = match.group(1)
enum_name2 = match.group(2)
entry = EnumerationEntryApi(
key=match.group(3),
value=match.group(4),
)
assert(enum_name == enum_name2)
found = False
for enum in api.enumerations:
if enum.name == enum_name:
enum.entries.append(entry)
found = True
break
if not found:
api.enumerations.append(EnumerationApi(
name=enum_name,
entries=[ entry ]
))
continue
if (match := typedef_re.search(x)):
api.type_aliases.append(TypeAliasApi(
aliased_type=match.group(1),
wgpu_type=match.group(2),
))
continue
if (match := callback_re.search(x)):
api.callbacks.append(CallbackApi(
name=match.group(1),
arguments=parseProcArgs(match.group(2)),
raw_arguments=match.group(2),
))
continue
if (match := init_macro_re.search(x)):
api.init_macros.append(match.group(1))
continue
# Post process: find parent of each method
for proc in api.procedures:
maxi = 0
parent_names = (
[ handle.name for handle in api.handles ] +
[ desc.name for desc in api.classes ]
)
for parent in parent_names:
if len(parent) > maxi and proc.name.startswith(parent) and len(parent) < len(proc.name):
proc.parent = parent
maxi = len(parent)
if proc.parent is not None:
proc.name = proc.name[maxi:]
return api
def parseEnum(name, it, stypes):
entry_re = re.compile(r"^\s+WGPU(\w+)_(\w+) = ([^,]+),?")
end_re = re.compile(".*}")
api = EnumerationApi(name=name)
while (x := next(it, None)) is not None:
if (match := entry_re.search(x)):
prefix = match.group(1)
key = match.group(2)
#value = match.group(3)
value = f"WGPU{prefix}_{key}"
api.entries.append(EnumerationEntryApi(key, value))
if "WGPUSType_" in x:
cast = "(WGPUSType)" if name != "SType" else ""
stypes[key] = cast + name + "::" + key
elif (match := end_re.search(x)):
break
return api
def parseClass(name, it):
api = ClassApi(name=name)
end_of_struct_re = re.compile(r".*}")
property_re = re.compile(r"^\s*(.+) (\w+);$")
count_properties = []
x = next(it)
while not end_of_struct_re.search(x):
if (match := property_re.search(x)):
prop = PropertyApi(name=match.group(2), type=match.group(1))
if prop.name == "nextInChain":
api.is_descriptor = True
if prop.name == "chain" and api.parent is not None:
pass
elif prop.name[-5:] == "Count":
count_properties.append(prop)
else:
api.properties.append(prop)
x = next(it)
for counter in count_properties:
# entri|ies -> entr|yCount
# colorFormat|s -> colorFormat|sCount
prefix = counter.name[:-6]
found = False
for r in api.properties:
if r.name.startswith(prefix):
r.counter = counter.name
found = True
break
if not found:
api.properties.append(counter)
return api
def parseProcArgs(line):
args = []
for entry in line.split(","):
entry = entry.strip()
nullable = False
if entry.endswith("/* nullable */"):
nullable = True
entry = entry[:-14].strip()
if entry.startswith("WGPU_NULLABLE"):
nullable = True
entry = entry[13:].strip()
tokens = entry.split()
args.append(ProcedureArgumentApi(
name=tokens[-1],
type=" ".join(tokens[:-1]),
nullable=nullable
))
return args
# -----------------------------------------------------------------------------
def produceBinding(args, api, meta):
"""Produce C++ binding"""
binding = {
"webgpu_includes": [],
"descriptors": [],
"structs": [],
"class_impl": [],
"class_oneliner": [],
"handles_decl": [],
"handles": [],
"handles_impl": [],
"handles_oneliner": [],
"enums": [],
"callbacks": [],
"procedures": [],
"type_aliases": [],
"ext_suffix": args.ext_suffix,
}
for url in args.header_url:
filename = os.path.split(url)[1]
binding["webgpu_includes"].append(f"#include <webgpu/{filename}>")
# Cached variables for format_arg
handle_names = [ h.name for h in api.handles ]
handle_cptr_names = [ f"{h.name} const *" for h in api.handles ]
handle_ptr_names = [ f"{h.name} *" for h in api.handles ]
class_cptr_names = [ f"{d.name} const *" for d in api.classes ]
enum_names = [ e.name for e in api.enumerations ]
enum_ptr_names = [ f"{e.name} *" for e in api.enumerations ]
callbacks = {
f"{cb.name}Callback": cb
for cb in api.callbacks
}
def format_arg(arg):
"""
Given a function argument, return it (i) as an argument *received* from
the C++ API and (ii) as an argument *passed* to the C API and (iii) as
an argument passed to the C++ API.
Also tells (iv) whether the next should be skipped (used only for the
userdata pointer passed to callbacks).
"""
arg_type = arg.type
arg_c = arg.name
arg_cpp = arg.name
skip_next = False
if arg_type.startswith("struct "):
arg_type = arg_type[len("struct "):]
if arg_type.startswith("WGPU"):
arg_type = arg_type[len("WGPU"):]
if arg_type in class_cptr_names:
base_type = arg_type[:-8]
arg_type = f"const {base_type}&"
arg_c = f"&{arg_c}"
arg_cpp = f"*reinterpret_cast<{base_type} const *>({arg.name})"
elif arg_type in callbacks:
arg_type = f"{arg_type}&&"
arg_c = "cCallback"
skip_next = True
elif arg_type in handle_cptr_names:
arg_c = f"reinterpret_cast<WGPU{arg_type}>({arg_c})"
elif arg_type in handle_ptr_names:
arg_c = f"reinterpret_cast<WGPU{arg_type}>({arg_c})"
if args.use_scoped_enums:
if arg_type in enum_names:
arg_c = f"static_cast<WGPU{arg_type}>({arg_c})"
arg_cpp = f"static_cast<{arg_type}>({arg_cpp})"
elif arg_type in enum_ptr_names:
arg_c = f"reinterpret_cast<WGPU{arg_type}>({arg_c})"
sig_cpp = f"{arg_type} {arg.name}"
return sig_cpp, arg_c, arg_cpp, skip_next
maybe_inline = "inline " if args.use_inline else ""
class_names = [f"WGPU{c.name}" for c in api.classes]
classes_and_handles = (
[ ('CLASS', cls_api) for cls_api in api.classes ] +
[ ('HANDLE', handle_api) for handle_api in api.handles ]
)
for entry_type, handle_or_class in classes_and_handles:
entry_name = handle_or_class.name
if entry_type == 'CLASS':
macro = "DESCRIPTOR" if handle_or_class.is_descriptor else "STRUCT"
namespace = "descriptors" if handle_or_class.is_descriptor else "structs"
namespace_impl = "class_impl"
namespace_oneliner = "class_oneliner"
argument_self = "*this"
use_const = False
elif entry_type == 'HANDLE':
binding["handles_decl"].append(f"class {entry_name};")
macro = "HANDLE"
namespace = "handles"
namespace_impl = "handles_impl"
namespace_oneliner = "handles_oneliner"
argument_self = "m_raw"
use_const = args.use_const
if entry_name.startswith("INTERNAL__"):
continue
decls = []
implems = []
# Auto-generate setDefault
if entry_type == 'CLASS':
decls.append(f"\t{maybe_inline}void setDefault();\n")
cls_api = handle_or_class
prop_names = [f"{p.name}" for p in cls_api.properties]
if args.use_init_macros:
init_macro = f"WGPU_{to_constant_case(entry_name)}_INIT"
if init_macro not in api.init_macros:
logging.warning(f"Initialization macro '{init_macro}' was not found, falling back to empty initializer '{{}}'.")
init_macro = "{}"
prop_defaults = [
f"\t*this = WGPU{entry_name} {init_macro};\n",
]
else:
prop_defaults = [
f"\t{prop.name} = {prop.default_value};\n"
for prop in cls_api.properties
if prop.default_value is not None
] + [
f"\t(({prop.type[4:]}*)&{prop.name})->setDefault();\n"
for prop in cls_api.properties
if prop.type in class_names
] + [
f"\t{subprop} = {default_value};\n"
for subprop, default_value in cls_api.default_overrides
]
if "chain" in prop_names:
if entry_name in api.stypes:
prop_defaults.extend([
f"\tchain.sType = {api.stypes[entry_name]};\n",
f"\tchain.next = nullptr;\n",
])
else:
logging.warning(f"Type {entry_name} starts with a 'chain' field but has no apparent associated SType.")
implems.append(
f"{maybe_inline}void {entry_name}::setDefault() " + "{\n"
+ "".join(prop_defaults)
+ "}\n"
)
for proc in api.procedures:
if proc.parent != entry_name:
continue
if "wgpu" + entry_name + proc.name + "\n" in meta["blacklist"]:
logging.debug(f"Skipping wgpu{entry_name}{proc.name} (blacklisted)...")
continue
method_name = proc.name[0].lower() + proc.name[1:]
arguments, argument_names = [], []
skip_next = False
for arg in proc.arguments[1:]:
if skip_next:
skip_next = False
continue
sig, name, _, skip_next = format_arg(arg)
arguments.append(sig)
argument_names.append(name)
return_type = proc.return_type
# Wrap callback into std::function&&
if "userdata" == proc.arguments[-1].name:
cb = callbacks[proc.arguments[-2].type[4:]]
cb_name = proc.arguments[-2].name
cb_arg_names = map(lambda a: format_arg(a)[2], cb.arguments[:-1])
body = (
f"\tauto handle = std::make_unique<{cb.name}Callback>({cb_name});\n"
+ f"\tstatic auto cCallback = []({cb.raw_arguments}) -> void {{\n"
+ f"\t\t{cb.name}Callback& callback = *reinterpret_cast<{cb.name}Callback*>(userdata);\n"
+ f"\t\tcallback({', '.join(cb_arg_names)});\n"
+ "\t};\n"
+ "\t{wrapped_call};\n"
+ "\treturn handle;\n"
)
argument_names.append(f"reinterpret_cast<void*>(handle.get())")
return_type = f"std::unique_ptr<{cb.name}Callback>"
maybe_no_discard = "NO_DISCARD "
else:
body = "\treturn {wrapped_call};\n"
maybe_no_discard = ""
argument_names_str = ', '.join([argument_self] + argument_names)
begin_cast = ""
end_cast = ""
if return_type.startswith("WGPU"):
return_type = return_type[4:]
if args.use_scoped_enums:
if return_type in enum_names:
begin_cast = f"static_cast<{return_type}>("
end_cast = ")"
wrapped_call = f"{begin_cast}wgpu{entry_name}{proc.name}({argument_names_str}){end_cast}"
maybe_const = " const" if use_const else ""
name_and_args = f"{method_name}({', '.join(arguments)}){maybe_const}"
decls.append(f"\t{maybe_inline}{maybe_no_discard}{return_type} {name_and_args};\n")
implems.append(
f"{maybe_inline}{return_type} {entry_name}::{name_and_args} {{\n"
+ body.replace("{wrapped_call}", wrapped_call)
+ "}\n"
)
# Add utility overload for arguments of the form 'uint32_t xxCount, Xx const * xx'
for i in range(len(proc.arguments) - 1):
a, b = proc.arguments[i], proc.arguments[i + 1]
if a.type in {"uint32_t","size_t"} and a.name.endswith("Count"):
name = a.name[:-5]
if b.type.endswith("const *") and b.name.startswith(name):
vec_type = b.type[:-8]
vec_name = name if name.endswith("s") else name + "s"
alternatives = [
(
[f"const std::vector<{vec_type}>& {vec_name}"],
[f"static_cast<{a.type}>({vec_name}.size())", f"{vec_name}.data()"]
),
(
[f"const {vec_type}& {vec_name}"],
[f"1", f"&{vec_name}"]
),
]
for new_args, new_arg_names in alternatives:
alt_arguments = arguments[:i-1] + new_args + arguments[i+2:]
alt_argument_names = argument_names[:i-1] + new_arg_names + argument_names[i+2:]
alt_argument_names_str = ', '.join([argument_self] + alt_argument_names)
wrapped_call = f"wgpu{entry_name}{proc.name}({alt_argument_names_str})"
maybe_const = " const" if use_const else ""
name_and_args = f"{method_name}({', '.join(alt_arguments)}){maybe_const}"
decls.append(f"\t{maybe_inline}{return_type} {name_and_args};\n")
implems.append(
f"{return_type} {entry_name}::{name_and_args} {{\n"
+ body.replace("{wrapped_call}", wrapped_call)
+ "}\n"
)
# Add a variant when the last argument is a nullable descriptor
if len(proc.arguments) > 1:
arg = proc.arguments[-1]
_, arg_c, __, ___ = format_arg(arg)
if arg.nullable and arg_c.startswith("&"):
alt_arguments = arguments[:-1]
alt_argument_names = argument_names[:-1]
alt_argument_names_str = ', '.join([argument_self] + alt_argument_names + ["nullptr"])
wrapped_call = f"{begin_cast}wgpu{entry_name}{proc.name}({alt_argument_names_str}){end_cast}"
maybe_const = " const" if use_const else ""
name_and_args = f"{method_name}({', '.join(alt_arguments)}){maybe_const}"
decls.append(f"\t{maybe_inline}{return_type} {name_and_args};\n")
implems.append(
f"{maybe_inline}{return_type} {entry_name}::{name_and_args} {{\n"
+ body.replace("{wrapped_call}", wrapped_call)
+ "}\n"
)
injected_decls = meta["injected-decls"].members.get(entry_name, [])
macro = meta["injected-decls"].macro_override.get(entry_name, macro)
binding[namespace].append(
f"{macro}({entry_name})\n"
+ "".join(decls + injected_decls)
+ "END\n"
)
binding[namespace_oneliner].append(
f"{macro}({entry_name});"
)
binding[namespace_impl].append(
f"// Methods of {entry_name}\n"
+ "".join(implems)
+ "\n"
)
# Only handles_impl is present in the tamplate
binding["handles_impl"] = (
binding["class_impl"] +
binding["handles_impl"]
)
if args.use_non_member_procedures:
for proc in api.procedures:
if proc.parent is not None:
continue
arg_sig = map(lambda a: f"{a.type} {a.name}", proc.arguments)
arg_names = map(lambda a: a.name, proc.arguments)
proc_name = proc.name[0].lower() + proc.name[1:]
binding["procedures"].append(
f"{proc.return_type} {proc_name}({', '.join(arg_sig)}) {{\n"
+ f"\treturn wgpu{proc.name}({', '.join(arg_names)});\n"
+ "}"
)
for enum in api.enumerations:
if args.use_scoped_enums:
if args.use_fake_scoped_enums:
enum = (
f"ENUM({enum.name})\n"
+ "".join([ f"\tENUM_ENTRY({format_enum_value(e.key)}, {e.value})\n" for e in enum.entries ])
+ "END"
)
else:
enum = (
f"enum class {enum.name}: int {{\n"
+ "".join([ f"\t{format_enum_value(e.key)} = {e.value},\n" for e in enum.entries ])
+ "};"
)
else:
enum = f"typedef WGPU{enum.name} {enum.name};"
binding["enums"].append(enum)
# Use a dict to merge duplicates
cb_dict = {
cb.name: map(lambda a: format_arg(a)[0], cb.arguments[:-1])
for cb in api.callbacks
}
for cb_name, cb_args in cb_dict.items():
binding["callbacks"].append(f"using {cb_name}Callback = std::function<void({', '.join(cb_args)})>;")
for ta in api.type_aliases:
binding["type_aliases"].append(f"using {ta.wgpu_type} = {ta.aliased_type};")
for k, v in binding.items():
binding[k] = "\n".join(v)
return binding
# -----------------------------------------------------------------------------
# Default values
def loadDefaults(args, api):
for default_file in args.defaults:
loadDefaultFile(api, default_file)
postProcessDefaults(api)
def loadDefaultFile(api, filename):
resolved = resolveFilepath(filename)
logging.info(f"Loading default values from {resolved}...")
entry_re = re.compile(r"^WGPU(\w+)::([\w\.]+) = (.+);$")
comment_re = re.compile(r"^\s*(//.*)?$")
name_to_class_idx = { c.name: i for i, c in enumerate(api.classes) }
with openVfs(resolved, encoding="utf-8") as f:
for lineno, line in enumerate(f):
if (match := entry_re.search(line)):
class_name = match.group(1)
prop_name = match.group(2)
default_value = match.group(3)
if class_name not in name_to_class_idx:
logging.warning(f"Unknown class {class_name} (in file {resolved}, line {lineno + 1})")
continue
c = api.classes[name_to_class_idx[class_name]]
# Special case of sub-struct override
if "." in prop_name:
c.default_overrides.append((prop_name, default_value))
continue
name_to_prop_idx = { p.name: i for i, p in enumerate(c.properties) }
if prop_name not in name_to_prop_idx:
logging.warning(f"Unknown property {class_name}::{prop_name} (in file {resolved}, line {lineno + 1})")
continue
prop = c.properties[name_to_prop_idx[prop_name]]
prop.default_value = default_value
elif (match := comment_re.search(line)):
pass
else:
logging.warning(f"Syntax error '{line.strip()}' (in file {resolved}, line {lineno + 1})")
def postProcessDefaults(api):
"""Transform string into enum values in default values"""
name_to_enum = { f"WGPU{e.name}": e for e in api.enumerations }
name_to_class = { f"WGPU{c.name}": c for c in api.classes }
def fixDefaultValue(prop_type, default_value):
enum = name_to_enum.get(prop_type)
if enum is None:
return default_value
name_to_entry = {
re.sub(r"([a-z])([A-Z])", r"\1-\2", e.key).lower(): e for e in enum.entries
}
if default_value is None:
entry = name_to_entry.get("undefined")
if entry is not None:
return f"{enum.name}::{format_enum_value(entry.key)}"
else:
entry = name_to_entry.get(default_value.strip('"'))
if entry is None:
logging.warning(f"Unknown value {default_value} for enum {prop_type}")
return None
else:
return f"{enum.name}::{format_enum_value(entry.key)}"
def getType(path, cls_api):
name_to_prop = { p.name: p for p in cls_api.properties }
if path[0] not in name_to_prop:
return None
prop = name_to_prop[path[0]]
if len(path) == 1:
return prop.type
else:
sub_cls_api = name_to_class[prop.type]
return getType(path[1:], sub_cls_api)
for c in api.classes:
for prop in c.properties:
prop.default_value = fixDefaultValue(prop.type, prop.default_value)
for i, (subprop, default_value) in enumerate(c.default_overrides):
prop_type = getType(subprop.split('.'), c)
if prop_type is not None:
c.default_overrides[i] = (subprop, fixDefaultValue(prop_type, default_value))
# -----------------------------------------------------------------------------
# Utility functions
def loadTemplate(path):
resolved = resolveFilepath(path)
logging.info(f"Loading template from {resolved}...")
with openVfs(resolved, encoding="utf-8") as f:
in_inject = False
in_blacklist = False
injected = ""
blacklist = ""
template = ""
for line in f:
if line.strip() == "{{begin-inject}}":
in_inject = True
continue
if line.strip() == "{{end-inject}}":
in_inject = False
continue
if line.strip() == "{{begin-blacklist}}":
in_blacklist = True
continue
if line.strip() == "{{end-blacklist}}":
in_blacklist = False
continue
if in_inject:
injected += line
elif in_blacklist:
blacklist += line
else:
template += line
template = (
template
.replace('{', '{{') # escape brackets
.replace('}', '}}')
.replace('{{{{', '{') # transform double brackets into format string
.replace('}}}}', '}')
)
return template, {
"injected-decls": parseTemplateInjection(injected),
"blacklist": blacklist,
}
@dataclass
class InjectedData():
# Extra members to insert, for each type
members: Dict[str,List[str]]
# Replacement for the HANDLE/STRUCT/etc macro used for this definition
# (used to replace STRUCT by STRUCT_NO_OSTREAM)
macro_override: Dict[str,str]
def parseTemplateInjection(text):
it = iter(text.split("\n"))
injected_data = InjectedData(
members = defaultdict(list),
macro_override = {},
)
begin_re = re.compile(r"^(HANDLE|DESCRIPTOR|STRUCT|STRUCT_NO_OSTREAM)\((\w+)\)")
end_re = re.compile(r"^END")
current_category = None
while (line := next(it, None)) is not None:
if (match := begin_re.search(line)):
current_category = match.group(2)
injected_data.macro_override[current_category] = match.group(1)
elif (match := end_re.search(line)):
current_category = None
elif current_category is not None:
injected_data.members[current_category].append(line + "\n")
return injected_data
def downloadHeader(url):
if url.startswith("https://") or url.startswith("http://"):
logging.info(f"Downloading webgpu-native header from {url}...")
import urllib.request
response = urllib.request.urlopen(url)
data = response.read()
text = data.decode('utf-8')
return text
else:
resolved = resolveFilepath(url)
logging.info(f"Loading webgpu-native header from {resolved}...")
with openVfs(resolved, encoding="utf-8") as f:
return f.read()
def generateOutput(path, template, fields):
logging.info(f"Writing generated binding to {path}...")
out = template.format(**fields)
with openVfs(path, 'w', encoding="utf-8") as f:
f.write(out)
def resolveFilepath(path):
for p in [ join(dirname(__file__), path), path ]:
if isfileVfs(p):
return p
logging.error(f"Invalid template path: {path}")
raise ValueError("Invalid template path")
def format_enum_value(value):
if value[0] in '0123456789':
return '_' + value
else:
return value
def to_constant_case(caml_case):
naive = ''.join(['_'+c if c.isupper() or c.isnumeric() else c for c in caml_case]).lstrip('_').upper()
# We then regroup isolated characters together (because they correspond to acronyms):
current_acronym = None
tokens = []
for tok in naive.split("_"):
if len(tok) == 1:
if current_acronym is None:
current_acronym = ""
current_acronym += tok
else:
if current_acronym is not None:
tokens.append(current_acronym)
tokens.append(tok)
current_acronym = None
if current_acronym is not None:
tokens.append(current_acronym)
return "_".join(tokens)
# -----------------------------------------------------------------------------
if __name__ == "__main__":
args = makeArgParser().parse_args()