-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.odin
948 lines (777 loc) · 29.7 KB
/
generator.odin
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
package openxr_odin
// The following changes were made to xr.xml to ensure encoding/xml could parse it.
// 1. Lines 2-4 inclusive were commented out
// 2. Elements with attribute values multiple lines (typically long descriptions) were collapsed onto single lines
import "core:encoding/xml"
import "core:fmt"
import "core:strconv"
import "core:strings"
import "core:os"
KNOWN_SUFFIXES :: [?]string{"KHR", "EXT", "FB", "MSFT", "HTC", "META", "ULTRALEAP", "EXTX", "ALMALENCE"}
main :: proc() {
doc, err := xml.load_from_file("xr.xml")
if err != .None {
fmt.println("Failed to Parse xr.xml, error", err)
return
}
gen_core_odin(doc)
gen_enums_odin(doc)
gen_structs_odin(doc)
gen_procedures_odin(doc)
gen_loader_odin(doc)
}
el_get_attrib :: proc(el: xml.Element, key: string) -> string {
for attr in el.attribs {
if attr.key != key {continue}
return attr.val
}
panic(fmt.tprintln("attribute not found on element:", key))
}
el_has_attrib :: proc(el: xml.Element, key: string) -> bool {
for attr in el.attribs {
if attr.key != key {continue}
return true
}
return false
}
el_try_get_attrib :: proc(el: xml.Element, key: string) -> (string, bool) {
for attr in el.attribs {
if attr.key != key {continue}
return attr.val, true
}
return "", false
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CORE.ODIN //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CORE_HELPERS :: `
// Version Helpers
CURRENT_API_VERSION :: (1<<48) | (0<<12) | (32)
MAKE_VERSION :: proc(major, minor, patch: u64) -> u64 {
return (major<<48) | (minor<<32) | (patch)
}
// Base Types
Handle :: distinct rawptr
Atom :: distinct u64
Flags64 :: distinct u64
Time :: i64
Duration :: i64
Version :: u64
// Atom Types
Path :: distinct Atom
SystemId :: distinct Atom
ControllerModelKeyMSFT :: distinct Atom
AsyncRequestIdFB :: distinct Atom
RenderModelKeyFB :: distinct Atom
// Helper function for generating fixed buffer strings
make_string :: proc(str: string, $n: int) -> [n]u8 {
result: [n]u8
copy(result[:], str)
return result
}
// Function pointer types
ProcSetProcAddress :: #type proc "c" (p: rawptr, name: cstring)
ProcVoidFunction :: #type proc "c" () -> rawptr
ProcDebugUtilsMessengerCallbackEXT :: #type proc "c" (
messageSeverity: DebugUtilsMessageSeverityFlagsEXT,
messageTypes: DebugUtilsMessageTypeFlagsEXT,
callbackData: ^DebugUtilsMessengerCallbackDataEXT,
userData: rawptr,
) -> rawptr
`
// Generates the full core.odin file and writes it out
gen_core_odin :: proc(doc: ^xml.Document) {
builder := strings.builder_make()
strings.write_string(&builder, "package openxr\n")
strings.write_string(&builder, CORE_HELPERS)
for id in doc.elements[0].children {
el := doc.elements[id]
el_name, ok := el_try_get_attrib(el, "name")
if el.ident != "enums" || el_name != "API Constants" {continue}
gen_constants(&builder, doc, el)
}
for id in doc.elements[0].children {
el := doc.elements[id]
if el.ident != "types" {continue}
gen_core_types(&builder, doc, el)
}
for id in doc.elements[0].children {
el := doc.elements[id]
if el.ident != "extensions" {continue}
gen_extension_constants(&builder, doc, el)
}
os.write_entire_file("openxr/core.odin", builder.buf[:])
}
// Generates odin constants from the "API Constants" <enums> element
gen_constants :: proc(builder: ^strings.Builder, doc: ^xml.Document, el: xml.Element) {
strings.write_string(builder, "// Base constants\n")
for child in el.children {
gen_constant(builder, doc, doc.elements[child])
}
strings.write_string(builder, "\n")
}
// Generate an odin constant from an <enum>
gen_constant :: proc(builder: ^strings.Builder, doc: ^xml.Document, el: xml.Element) {
full_name := el_get_attrib(el, "name")
name := strings.trim_prefix(full_name, "XR_")
value := el_get_attrib(el, "value")
if name == "TRUE" || name == "FALSE" {
return
}
strings.write_string(builder, fmt.aprintln(name, "::", value))
}
// Generates odin constants from the "API Constants" <enums> element
gen_core_types :: proc(builder: ^strings.Builder, doc: ^xml.Document, el: xml.Element) {
strings.write_string(builder, "// Handle Types\n")
for child in el.children {
elem := doc.elements[child]
category, _ := el_try_get_attrib(elem, "category")
if category != "handle" {continue}
full_name := doc.elements[elem.children[1]].value
name := strings.trim_left(full_name, "Xr")
strings.write_string(builder, fmt.aprintf("{} :: distinct Handle\n", name))
}
strings.write_string(builder, "\n")
}
// Generates odin constants from the <extensions> element
gen_extension_constants :: proc(builder: ^strings.Builder, doc: ^xml.Document, el: xml.Element) {
strings.write_string(builder, "// Extension constants\n")
for child in el.children {
gen_extension_constant(builder, doc, doc.elements[child])
}
strings.write_string(builder, "\n")
}
// Generates odin constants from the <extensions> element
gen_extension_constant :: proc(builder: ^strings.Builder, doc: ^xml.Document, el: xml.Element) {
full_name := el_get_attrib(el, "name")
name := strings.trim_prefix(full_name, "XR_")
number := el_get_attrib(el, "number")
spec_element := doc.elements[doc.elements[el.children[0]].children[0]]
spec_version := el_get_attrib(spec_element, "value")
screaming_name := strings.to_screaming_snake_case(name)
strings.write_string(builder, fmt.aprintf("{} :: {}\n", name, number))
strings.write_string(builder, fmt.aprintf("{}_EXTENSION_NAME :: \"{}\"\n", screaming_name, full_name))
// strings.write_string(builder, fmt.aprintf("{}_SPEC_VERSION :: {}\n", screaming_name, spec_version))
for id in doc.elements[el.children[0]].children {
// We're looking for enums with a value field
child_el := doc.elements[id]
if child_el.ident != "enum" || !el_has_attrib(child_el, "value") {continue}
child_full_name := el_get_attrib(child_el, "name")
child_value := el_get_attrib(child_el, "value")
if strings.has_suffix(child_full_name, "_NAME") {continue}
child_name := strings.to_screaming_snake_case(strings.trim_prefix(child_full_name, "XR_"))
strings.write_string(builder, fmt.aprintf("{} :: {}\n", child_name, child_value))
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ENUMS.ODIN //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Extended_Enum :: struct {
name: string,
value: int,
}
// Generates the full enums.odin file and writes it out
gen_enums_odin :: proc(doc: ^xml.Document) {
builder := strings.builder_make()
strings.write_string(&builder, "package openxr\n\n")
strings.write_string(&builder, "import \"core:c\"\n\n")
// Get extension enum values
ext_map: map[string][dynamic]Extended_Enum
for id in doc.elements[0].children {
el := doc.elements[id]
if el.ident != "extensions" {continue}
ext_map = get_enum_extensions(doc, el)
}
// Generate enums using <enums> element
for id in doc.elements[0].children {
el := doc.elements[id]
if el.ident != "enums" {continue}
gen_enums(&builder, doc, el, ext_map)
}
// Now generate our flags types
for id in doc.elements[0].children {
el := doc.elements[id]
if el.ident != "types" {continue}
gen_enums_types(&builder, doc, el)
}
os.write_entire_file("openxr/enums.odin", builder.buf[:])
}
// Builds a map of extended enum values from the <extensions> element
get_enum_extensions :: proc(doc: ^xml.Document, el: xml.Element) -> (ext_map: map[string][dynamic]Extended_Enum) {
for id in el.children {
get_enum_extension(doc, doc.elements[id], &ext_map)
}
return ext_map
}
// Finds extended enum values from a single <extension> element
get_enum_extension :: proc(doc: ^xml.Document, el: xml.Element, ext_map: ^map[string][dynamic]Extended_Enum) {
ext_number, ok := strconv.parse_int(el_get_attrib(el, "number"), 10)
assert(ok)
require_el := doc.elements[el.children[0]]
assert(require_el.ident == "require")
for id in require_el.children {
child_el := doc.elements[id]
if child_el.ident != "enum" {continue}
if !el_has_attrib(child_el, "extends") {continue}
extends := el_get_attrib(child_el, "extends")
if extends == "XrSwapchainUsageFlagBits" {continue} // Special case bitfield extension
if el_has_attrib(child_el, "alias") {continue} // Skip Aliases
name := el_get_attrib(child_el, "name")
offset, ok := strconv.parse_int(el_get_attrib(child_el, "offset"), 10)
assert(ok)
if !(extends in ext_map) {
ext_map[extends] = make([dynamic]Extended_Enum, 0)
}
// The strategy for valuing extended enums is
// 1000000000 Offset for all extensions
// + (ext_number - 1) * 1000, Offset for each extension
// offset, per enum value offset
value := 1000000000 + 1000 * (ext_number - 1) + offset
append(&ext_map[extends], Extended_Enum{name, value})
}
}
// Generates odin code from an <enums> element
gen_enums :: proc(
builder: ^strings.Builder,
doc: ^xml.Document,
el: xml.Element,
ext_map: map[string][dynamic]Extended_Enum,
) {
el_name := el_get_attrib(el, "name")
// Handle API Constants special case
if el_name == "API Constants" {return}
// Handle actual enums
el_type := el_get_attrib(el, "type")
if el_type == "enum" {
gen_enums_enum(builder, doc, el, ext_map)
return
} else if el_type == "bitmask" {
gen_enums_bitfield(builder, doc, el)
return
}
panic(fmt.tprintln("Unknown <enums> of type", el_type))
}
// Generates an odin enum type from an <enums> element of type="enum"
gen_enums_enum :: proc(
builder: ^strings.Builder,
doc: ^xml.Document,
el: xml.Element,
ext_map: map[string][dynamic]Extended_Enum,
) {
full_name := el_get_attrib(el, "name")
name := strings.trim_prefix(full_name, "Xr")
prefix, suffix := enum_name_to_prefix_suffix(full_name)
strings.write_string(builder, fmt.aprintf("{} :: enum c.int {{\n", name))
for child in el.children {
gen_enums_enum_value(builder, doc, doc.elements[child], prefix, suffix)
}
// Now write any extension values
extended_enums: [dynamic]Extended_Enum
if full_name in ext_map {
extended_enums = ext_map[full_name]
}
for ext_enum in extended_enums {
gen_enums_enum_extended(builder, ext_enum, prefix, suffix)
}
strings.write_string(builder, "}\n\n")
}
// Generates code for a single value in an enum declaration
gen_enums_enum_value :: proc(builder: ^strings.Builder, doc: ^xml.Document, el: xml.Element, prefix, suffix: string) {
if el.ident != "enum" {
return
}
name := strings.trim_prefix(strings.trim_suffix(el_get_attrib(el, "name"), suffix), prefix)
value := el_get_attrib(el, "value")
comment, has_comment := el_try_get_attrib(el, "comment")
strings.write_string(builder, fmt.aprintf("\t{} = {},", name, value))
if has_comment {
strings.write_string(builder, fmt.aprintf(" // {}", comment))
}
strings.write_string(builder, "\n")
}
// Generates code for a single extension value in an enum declaration
gen_enums_enum_extended :: proc(builder: ^strings.Builder, ext_enum: Extended_Enum, prefix, suffix: string) {
name := strings.trim_prefix(strings.trim_suffix(ext_enum.name, suffix), prefix)
value := ext_enum.value
strings.write_string(builder, fmt.aprintf("\t{} = {},", name, value))
strings.write_string(builder, "\n")
}
// Generates an odin bitfield type from an <enums> element of type="bitmask"
gen_enums_bitfield :: proc(builder: ^strings.Builder, doc: ^xml.Document, el: xml.Element) {
full_name := el_get_attrib(el, "name")
flags, flag := trim_bitfield_typename(full_name)
prefix, suffix := bitfield_name_to_prefix_suffix(full_name)
strings.write_string(builder, fmt.aprintf("{} :: enum Flags64 {{\n", flag))
for child in el.children {
gen_enums_bitfield_value(builder, doc, doc.elements[child], prefix, suffix)
}
// Handle XrSwapchainUsageFlagBits special case
if full_name == "XrSwapchainUsageFlagBits" {
strings.write_string(builder, "\tINPUT_ATTACHMENT_MND = 7,\n")
}
strings.write_string(builder, "}\n\n")
}
// Generates code for a single value in a bitfield
gen_enums_bitfield_value :: proc(builder: ^strings.Builder, doc: ^xml.Document, el: xml.Element, prefix, suffix: string) {
if el.ident != "enum" {
return
}
name := strings.trim_prefix(strings.trim_suffix(el_get_attrib(el, "name"), suffix), prefix)
value := el_get_attrib(el, "bitpos")
comment, has_comment := el_try_get_attrib(el, "comment")
strings.write_string(builder, fmt.aprintf("\t{} = {},", name, value))
if has_comment {
strings.write_string(builder, fmt.aprintf(" // {}", comment))
}
strings.write_string(builder, "\n")
}
// Converts a full OpenXR enum type name to a prefix and suffix string to trim from its members
enum_name_to_prefix_suffix :: proc(name: string) -> (string, string) {
// Handle weird cases
if name == "XrStructureType" {return "XR_TYPE_", ""}
if name == "XrResult" {return "XR_", ""}
if name == "XrPerfSettingsNotificationLevelEXT" {return "XR_PERF_SETTINGS_NOTIF_LEVEL_", "_EXT"}
// Determine if the type name has a known suffix
suffix := ""
for known in KNOWN_SUFFIXES {
if !strings.has_suffix(name, known) {continue}
suffix = known
}
// Now remove that suffix from the name and convert it to screaming snake case
prefix := strings.trim_suffix(name, suffix)
prefix = fmt.aprintf("{}_", strings.to_screaming_snake_case(prefix))
suffix = fmt.aprintf("_{}", suffix)
return prefix, suffix
}
// Converts a full OpenXR enum type name to a prefix and suffix string to trim from its members
trim_bitfield_typename :: proc(name: string) -> (string, string) {
// Determine vendor suffix
suffix := ""
for known in KNOWN_SUFFIXES {
if !strings.has_suffix(name, known) {continue}
suffix = known
}
// Remove Vendor and "Bits" suffix, and Xr prefix
name := strings.trim_suffix(strings.trim_suffix(name, suffix), "Bits")
name = strings.trim_prefix(name, "Xr")
// Build FooBarFlagsVENDOR and FooBarFlagVENDOR variants
flags := fmt.aprintf("{}s{}", name, suffix)
flag := fmt.aprintf("{}{}", name, suffix)
return flags, flag
}
// Converts a full OpenXR enum type name to a prefix and suffix string to trim from its members
bitfield_name_to_prefix_suffix :: proc(name: string) -> (string, string) {
// Determine if the type name has a known suffix
suffix := ""
for known in KNOWN_SUFFIXES {
if !strings.has_suffix(name, known) {continue}
suffix = known
}
// Now remove that suffix from the name, along with Bits and convert it to screaming snake case
prefix := strings.trim_suffix(strings.trim_suffix(name, suffix), "FlagBits")
prefix = fmt.aprintf("{}_", strings.to_screaming_snake_case(prefix))
suffix = suffix == "" ? "_BIT" : fmt.aprintf("_BIT_{}", suffix)
return prefix, suffix
}
// Generates odin code from an <enums> element
gen_enums_types :: proc(builder: ^strings.Builder, doc: ^xml.Document, el: xml.Element) {
for id in el.children {
gen_enums_type(builder, doc, doc.elements[id])
}
}
// Generates code for an enum bitset type
gen_enums_type :: proc(builder: ^strings.Builder, doc: ^xml.Document, el: xml.Element) {
category, ok := el_try_get_attrib(el, "category")
if category != "bitmask" {return}
bitvalues := el_get_attrib(el, "bitvalues")
flags, flag := trim_bitfield_typename(bitvalues)
strings.write_string(builder, fmt.aprintf("{} :: distinct bit_set[{}; Flags64]\n", flags, flag))
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// STRUCTS.ODIN //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
STRUCT_OS_TYPES :: `
import "core:c"
// Vulkan Types
import vk "vendor:vulkan"
// OpenGL Types
EGLenum :: c.int
// Windows specific OS / API types
when ODIN_OS == .Windows {
import win32 "core:sys/windows"
HDC :: win32.HDC
HGLRC :: win32.HGLRC
LUID :: win32.LUID
IUnknown :: win32.IUnknown
D3D_FEATURE_LEVEL :: c.int
import D3D11 "vendor:directx/d3d11"
ID3D11Device :: D3D11.IDevice
ID3D11Texture2D :: D3D11.ITexture2D
import D3D12 "vendor:directx/d3d12"
ID3D12Device :: D3D12.IDevice
ID3D12CommandQueue :: D3D12.ICommandQueue
ID3D12Resource :: D3D12.IResource
} else {
HDC :: distinct rawptr
HGLRC :: distinct rawptr
LUID :: struct {
LowPart: DWORD,
HighPart: LONG,
}
IUnknown :: distinct rawptr
D3D_FEATURE_LEVEL :: c.int
ID3D11Device :: distinct rawptr
ID3D11Texture2D :: distinct rawptr
ID3D12Device :: distinct rawptr
ID3D12CommandQueue :: distinct rawptr
ID3D12Resource :: distinct rawptr
}
`
// Mapping from C to odin type names
TYPE_ALIASES := map[string]string {
"float" = "f32",
"double" = "f64",
"int8_t" = "i8",
"int16_t" = "i16",
"int32_t" = "i32",
"int64_t" = "i64",
"uint8_t" = "u8",
"uint16_t" = "u16",
"uint32_t" = "u32",
"uint64_t" = "u64",
"XrBool32" = "b32",
"int" = "c.int",
// "char" = "u8",
}
// Mapping from C to odin type names
NAME_ALIASES := map[string]string {
"type" = "sType",
"context" = "ctx",
"dynamic" = "fovDynamic",
}
UNSUPPORTED_STRUCTS :: [?]string{
"XrGraphicsBindingOpenGLXlibKHR",
"XrGraphicsBindingOpenGLXcbKHR",
"XrGraphicsBindingOpenGLWaylandKHR",
"XrGraphicsBindingOpenGLESAndroidKHR",
"XrGraphicsBindingEGLMNDX",
}
// Generates the full enums.odin file and writes it out
gen_structs_odin :: proc(doc: ^xml.Document) {
builder := strings.builder_make()
strings.write_string(&builder, "package openxr\n\n")
strings.write_string(&builder, STRUCT_OS_TYPES)
for id in doc.elements[0].children {
el := doc.elements[id]
if el.ident != "types" {continue}
gen_struct_types(&builder, doc, el)
}
os.write_entire_file("openxr/structs.odin", builder.buf[:])
}
// Generates struct declaration code from the <types> element
gen_struct_types :: proc(builder: ^strings.Builder, doc: ^xml.Document, el: xml.Element) {
for id in el.children {
el := doc.elements[id]
category, _ := el_try_get_attrib(el, "category")
if category != "struct" {continue}
gen_struct_type(builder, doc, el)
}
}
gen_struct_type :: proc(builder: ^strings.Builder, doc: ^xml.Document, el: xml.Element) {
full_name := el_get_attrib(el, "name")
for unsupported in UNSUPPORTED_STRUCTS {
if full_name == unsupported {return}
}
name := strings.trim_left(full_name, "Xr")
strings.write_string(builder, fmt.aprintf("{} :: struct {{\n", name))
for id in el.children {
gen_struct_member(builder, doc, doc.elements[id])
}
strings.write_string(builder, "}\n\n")
}
gen_struct_member :: proc(builder: ^strings.Builder, doc: ^xml.Document, el: xml.Element) {
if el.ident != "member" {return}
type_str := doc.elements[el.children[0]].value
name_str := doc.elements[el.children[1]].value
// Apply alias and trim Xr prefix
if type_str in TYPE_ALIASES {
type_str = TYPE_ALIASES[type_str]
}
type_str = strings.trim_prefix(type_str, "Xr")
if strings.has_prefix(type_str, "PFN_xr") {
type_str = strings.trim_prefix(type_str, "PFN_xr")
type_str = fmt.aprintf("Proc{}", type_str)
}
// Handle Vk prefixes
if strings.has_prefix(type_str, "Vk") {
type_str = strings.trim_prefix(type_str, "Vk")
type_str = fmt.aprintf("vk.{}", type_str)
}
if strings.has_prefix(type_str, "PFN_vk") {
type_str = strings.trim_prefix(type_str, "PFN_vk")
type_str = fmt.aprintf("vk.Proc{}", type_str)
}
// Handle Arrayness
// The XML handles arrayness inconsistently
// So el.value will contain [XR_SOME_CONST]
// Others will just contains ] and we'll need to drill down further
// Handle arrayness where we have the array size in this el
if strings.contains(el.value, "[") && strings.contains(el.value, "]") {
size_str := strings.trim_prefix(strings.trim_suffix(el.value, "]"), "[")
size_str = strings.trim_prefix(size_str, "XR_")
type_str = fmt.aprintf("[{}]{}", size_str, type_str)
}
// Handle arrayness where we need to get array size from another el
if !strings.contains(el.value, "[") && strings.contains(el.value, "]") {
size_str := doc.elements[el.children[2]].value
size_str = strings.trim_prefix(size_str, "XR_")
type_str = fmt.aprintf("[{}]{}", size_str, type_str)
}
// Handle Pointerness / char* ness
ptr_count := strings.count(el.value, "*")
if ptr_count == 1 {
if type_str == "char" {
type_str = "cstring"
} else {
type_str = fmt.aprintf("^{}", type_str)
}
}
if ptr_count == 2 {
if type_str == "char" {
type_str = "[^]cstring"
} else {
type_str = fmt.aprintf("^^{}", type_str)
}
}
if strings.has_suffix(type_str, "char") {
type_str = fmt.aprintf("{}u8", strings.trim_suffix(type_str, "char"))
}
// Now handle void pointers
if type_str == "^void" {
type_str = "rawptr"
}
if type_str == "^^void" {
type_str = "^rawptr"
}
// Alias reserved words
if name_str in NAME_ALIASES {
name_str = NAME_ALIASES[name_str]
}
strings.write_string(builder, fmt.aprintf("\t{}: {},\n", name_str, type_str))
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PROCEDURES.ODIN //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PROC_OS_TYPES :: `
import "core:c"
import "core:c/libc"
// Vulkan Types
import vk "vendor:vulkan"
timespec :: libc.timespec
wchar_t :: libc.wchar_t
jobject :: rawptr
when ODIN_OS == .Windows {
import win32 "core:sys/windows"
LARGE_INTEGER :: win32.LARGE_INTEGER
} else {
LARGE_INTEGER :: distinct distinct c.longlong
}
`
// Generates the full enums.odin file and writes it out
gen_procedures_odin :: proc(doc: ^xml.Document) {
builder := strings.builder_make()
strings.write_string(&builder, "package openxr\n\n")
strings.write_string(&builder, PROC_OS_TYPES)
for id in doc.elements[0].children {
el := doc.elements[id]
if el.ident != "commands" {continue}
gen_procedures(&builder, doc, el)
}
os.write_entire_file("openxr/procedures.odin", builder.buf[:])
}
gen_procedures :: proc(builder: ^strings.Builder, doc: ^xml.Document, el: xml.Element) {
for id in el.children {
child_el := doc.elements[id]
if child_el.ident != "command" {continue}
gen_procedure_type(builder, doc, child_el)
}
strings.write_string(builder, "\n\n")
for id in el.children {
child_el := doc.elements[id]
if child_el.ident != "command" {continue}
gen_procedure(builder, doc, child_el)
}
}
gen_procedure_type :: proc(builder: ^strings.Builder, doc: ^xml.Document, el: xml.Element) {
// Handle aliased commands special case. Just xrGetVulkanGraphicsRequirements2KHR as of writing
if el_has_attrib(el, "alias") {return}
// Function name and return type are in first child
proto_el := doc.elements[el.children[0]]
assert(proto_el.ident == "proto")
full_name := doc.elements[proto_el.children[1]].value
if full_name == "xrGetInstanceProcAddr" {return} // Skip this since we manually link it
full_return_type := doc.elements[proto_el.children[0]].value
name := strings.trim_prefix(full_name, "xr")
return_type := strings.trim_prefix(full_return_type, "Xr")
strings.write_string(builder, fmt.aprintf("Proc{} :: #type proc \"system\" (\n", name))
// The remaining children will be the function arguments
for id in el.children[1:] {
child_el := doc.elements[id]
if child_el.ident != "param" {continue}
gen_procedure_arg(builder, doc, doc.elements[id])
}
strings.write_string(builder, fmt.aprintf(") -> {}\n\n", return_type))
}
gen_procedure_arg :: proc(builder: ^strings.Builder, doc: ^xml.Document, el: xml.Element) {
type_str := doc.elements[el.children[0]].value
name_str := doc.elements[el.children[1]].value
// Apply alias and trim Xr prefix
if type_str in TYPE_ALIASES {
type_str = TYPE_ALIASES[type_str]
}
type_str = strings.trim_prefix(type_str, "Xr")
if strings.has_prefix(type_str, "PFN_xr") {
type_str = strings.trim_prefix(type_str, "PFN_xr")
type_str = fmt.aprintf("Proc{}", type_str)
}
// Handle Vk prefixes
if strings.has_prefix(type_str, "Vk") {
type_str = strings.trim_prefix(type_str, "Vk")
type_str = fmt.aprintf("vk.{}", type_str)
}
if strings.has_prefix(type_str, "PFN_vk") {
type_str = strings.trim_prefix(type_str, "PFN_vk")
type_str = fmt.aprintf("vk.Proc{}", type_str)
}
// Handle Arrayness
if strings.contains(el.value, "[") || strings.contains(el.value, "]") {
type_str = fmt.aprintf("[^]{}", type_str)
}
// Handle Pointerness / char* ness
ptr_count := strings.count(el.value, "*")
if ptr_count == 1 {
if type_str == "char" {
type_str = "cstring"
} else {
type_str = fmt.aprintf("^{}", type_str)
}
}
if ptr_count == 2 {
if type_str == "char" {
type_str = "[^]cstring"
} else {
type_str = fmt.aprintf("^^{}", type_str)
}
}
if strings.has_suffix(type_str, "char") {
type_str = fmt.aprintf("{}u8", strings.trim_suffix(type_str, "char"))
}
// Now handle void pointers
if type_str == "^void" {
type_str = "rawptr"
}
if type_str == "^^void" {
type_str = "^rawptr"
}
strings.write_string(builder, fmt.aprintf("\t{}: {},\n", name_str, type_str))
}
gen_procedure :: proc(builder: ^strings.Builder, doc: ^xml.Document, el: xml.Element) {
// Handle aliased commands special case. Just xrGetVulkanGraphicsRequirements2KHR as of writing
if el_has_attrib(el, "alias") {
full_name := el_get_attrib(el, "name")
full_alias_name := el_get_attrib(el, "alias")
name := strings.trim_prefix(full_name, "xr")
alias_name := strings.trim_prefix(full_alias_name, "xr")
strings.write_string(builder, fmt.aprintf("{}: Proc{}\n", name, alias_name))
return
}
// Function name and return type are in first child
proto_el := doc.elements[el.children[0]]
assert(proto_el.ident == "proto")
full_name := doc.elements[proto_el.children[1]].value
if full_name == "xrGetInstanceProcAddr" {return} // Skip this since we manually link it
name := strings.trim_prefix(full_name, "xr")
strings.write_string(builder, fmt.aprintf("{}: Proc{}\n", name, name))
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LOADER.ODIN //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LOADER_LINKS :: `
// Link just the proc address loader
foreign import openxr_loader "openxr_loader.lib"
foreign openxr_loader {
@(link_name = "xrGetInstanceProcAddr")
GetInstanceProcAddr :: proc "system" (
instance: Instance,
name: cstring,
function: ^ProcVoidFunction,
) -> Result ---
}
`
BASE_PROCS := [?]string{"xrCreateInstance", "xrEnumerateApiLayerProperties", "xrEnumerateInstanceExtensionProperties"}
is_base_proc :: proc(name: string) -> bool {
for proc_name in BASE_PROCS {
if name != proc_name {continue}
return true
}
return false
}
// Generates the full enums.odin file and writes it out
gen_loader_odin :: proc(doc: ^xml.Document) {
builder := strings.builder_make()
strings.write_string(&builder, "package openxr\n\n")
strings.write_string(&builder, LOADER_LINKS)
gen_base_loader(&builder, doc)
gen_instance_loader(&builder, doc)
os.write_entire_file("openxr/loader.odin", builder.buf[:])
}
BASE_LOADER_START :: `
// Loads all the base procedures which don't require an instance
load_base_procs :: proc() {
out_function : ProcVoidFunction
`
gen_base_loader :: proc(builder: ^strings.Builder, doc: ^xml.Document) {
strings.write_string(builder, BASE_LOADER_START)
for full_name in BASE_PROCS {
if full_name == "xrGetInstanceProcAddr" {continue}
name := strings.trim_prefix(full_name, "xr")
strings.write_string(builder, fmt.aprintf("\tGetInstanceProcAddr(nil, \"{}\", &out_function)\n", full_name))
strings.write_string(builder, fmt.aprintf("\t{} = auto_cast out_function\n", name))
}
strings.write_string(builder, "}\n\n")
}
INSTANCE_LOADER_START :: `
// Load all the instance procedures
load_instance_procs :: proc(instance: Instance) {
out_function : ProcVoidFunction
`
gen_instance_loader :: proc(builder: ^strings.Builder, doc: ^xml.Document) {
strings.write_string(builder, INSTANCE_LOADER_START)
commands_el: xml.Element
for id in doc.elements[0].children {
if doc.elements[id].ident != "commands" {continue}
commands_el = doc.elements[id]
}
for id in commands_el.children {
child_el := doc.elements[id]
if child_el.ident != "command" {continue}
// Handle aliased commands special case. Just xrGetVulkanGraphicsRequirements2KHR as of writing
if el_has_attrib(child_el, "alias") {
full_name := el_get_attrib(child_el, "name")
name := strings.trim_prefix(full_name, "xr")
get_str := fmt.aprintf("\tGetInstanceProcAddr(instance, \"{}\", &out_function)\n", full_name)
strings.write_string(builder, get_str)
strings.write_string(builder, fmt.aprintf("\t{} = auto_cast out_function\n", name))
continue
}
proto_el := doc.elements[child_el.children[0]]
full_name := doc.elements[proto_el.children[1]].value
if is_base_proc(full_name) {continue}
if full_name == "xrGetInstanceProcAddr" {continue}
// fmt.println(child_el)
name := strings.trim_prefix(full_name, "xr")
strings.write_string(builder, fmt.aprintf("\tGetInstanceProcAddr(instance, \"{}\", &out_function)\n", full_name))
strings.write_string(builder, fmt.aprintf("\t{} = auto_cast out_function\n", name))
}
strings.write_string(builder, "}\n\n")
}