-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_protocols.jai
1587 lines (1352 loc) · 53.7 KB
/
generate_protocols.jai
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
/*
TODOS:
- Implement kinds
- When printing outputting types, consider the target platform. For example, don't print EGLsizeiANDROID bullshit unconditionally.
- Vendor khrplatfrom.h and eglplatform.h instead of sourcing them from /usr/include
- Explore an idea of just trying to load every EGL extension instead of checking if they are available, because the API surface is small.
NOTES:
- We can't use namespaces attribute of <enums> tag, because it's not mentioned in <command> for all extensions.
At least EGL doesn't use these namespaces anywhere. Sadge. But maybe we can still define them as enums but then alias to flat identifiers.
We'll see...
*/
Raw_Enum_Value :: struct {
api: string;
value: string;
name: string;
alias: string;
type: string;
groups: []string;
comment: string;
}
Raw_Enum_Namespace :: struct {
namespace: string;
start: string;
end: string;
vendor: string;
group: string;
type: string;
comment: string;
values: [..]Raw_Enum_Value;
}
Raw_Command :: struct {
// This comment should be printed before the command. There's only one
// in gl.xml actually
comment: string;
// <ptype> goes here
proto: Raw_Command_Param;
alias: string;
vecequiv: string;
params: [..]Raw_Command_Param;
}
Raw_Command_Param :: struct {
name: string;
class: string;
// Array of combined cdatas of the <param> child nodes, excluding <name>
type: []string;
group: string;
len: string;
kind: string;
}
Raw_Require_Remove :: struct {
// Seems like "api" attribute on <require> tag happens only once
// in GL_ARB_framebuffer_object extension?
api: string;
profile: string;
comment: string;
enums: [..]string;
commands: [..]string;
}
Raw_Feature :: struct {
api: string;
name: string;
number: string;
requires: [..]Raw_Require_Remove;
removes: [..]Raw_Require_Remove;
}
Raw_Extension :: struct {
name: string;
comment: string;
// I assume this is like a condition for the extension to be supported, but
// that's just my guess. I need to ask someone who actually knows.
protect: string;
// Separated by | - e.g. something like "gl|clcore|gles"
supported: []string;
// Test if maybe extensions have only a single require block
requires: [..]Raw_Require_Remove;
}
Raw_OpenGL :: struct {
// name of the xml file. So if the file is gl.xml, name will be "gl".
name: string;
platform_header: string;
// Raw unstripped C code that we're gonna pass to the Bindingds_Generator
types: string;
enum_namespaces: [..]Raw_Enum_Namespace;
commands_namespace: [..]Raw_Command;
features: [..]Raw_Feature;
extensions: [..]Raw_Extension;
}
Preprocessed_Namespace :: struct {
comment: string;
vendor: string; // Change to enum later
// Names of enums that are mentioned in the <enums /> tag, aka groups attribute
enums: [..]string;
// flat defines without an attached enum and <unused /> members that get turned into
// comments
flat_entries: [..]string;
}
// TODO: Implement
Preprocessed_Enum :: struct {
name: string;
type: string;
is_bitmask: bool;
variants: [..]Preprocessed_Enum_Value;
}
Preprocessed_Enum_Value :: struct {
name: string;
alias: string;
value: string;
comment: string;
api: GL_API;
}
Preprocessed_Command :: struct {
comment: string;
name: string;
alias: string;
vecequiv: string;
return_type: string;
arguments: [..]string;
}
Preprocessed_Feature :: struct {
// todo: alias for flags or some kind of making sure that there's only one set here
// at a time;
api: GL_API;
// Previous versions of the protocol that this one should support;
prev_features: GL_API;
// TODO: what to do with profile="compatibility" on <require> ?
// I thought that <remove> should handle that, but apparently not???
enums: []string;
commands: []string;
removes: []string;
}
Preprocessed_Extension :: struct {
// TODO: implement "protect"
name: string;
disabled: bool;
comment: string;
api: GL_API;
enums_core: [..]string;
enums_compat: [..]string;
commands_core: [..]string;
commands_compat: [..]string;
}
// TODO: Key doesn't have to be a string here
GL_Prev_Features :: Table(string, GL_API);
gl_prev_features: GL_Prev_Features;
GL_API :: enum_flags u64 {
GL_VERSION_1_0;
GL_VERSION_1_1;
GL_VERSION_1_2;
GL_VERSION_1_3;
GL_VERSION_1_4;
GL_VERSION_1_5;
GL_VERSION_2_0;
GL_VERSION_2_1;
GL_VERSION_3_0;
GL_VERSION_3_1;
GL_VERSION_3_2;
GL_VERSION_3_3;
GL_VERSION_4_0;
GL_VERSION_4_1;
GL_VERSION_4_2;
GL_VERSION_4_3;
GL_VERSION_4_4;
GL_VERSION_4_5;
GL_VERSION_4_6;
// gles1
GL_VERSION_ES_CM_1_0;
// gles2
GL_ES_VERSION_2_0;
GL_ES_VERSION_3_0;
GL_ES_VERSION_3_1;
GL_ES_VERSION_3_2;
// glsc2
GL_SC_VERSION_2_0;
EGL_VERSION_1_0;
EGL_VERSION_1_1;
EGL_VERSION_1_2;
EGL_VERSION_1_3;
EGL_VERSION_1_4;
EGL_VERSION_1_5;
// Unsupported for now (or forever)
GLX_VERSION_1_0;
GLX_VERSION_1_1;
GLX_VERSION_1_2;
GLX_VERSION_1_3;
GLX_VERSION_1_4;
WGL_VERSION_1_0;
}
GL_Extension :: string;
GL_Profile :: enum {
Core;
Compatibility;
}
Feature_Table :: Table(string, Preprocessed_Feature);
Extension_Table :: Table(string, Preprocessed_Extension);
Enum_Value_Table :: Table(string, Preprocessed_Enum_Value);
Command_Table :: Table(string, Preprocessed_Command);
// A table of types from xml file name to the generated string;
// So if the file was gl.xml, key will be "gl"
Types_Table :: Table(string, string);
types: Types_Table;
enum_values: Enum_Value_Table;
commands: Command_Table;
features: Feature_Table;
extensions: Extension_Table;
// TODO: somehow configure output files?
GL_Generation_Options :: struct {
api: GL_API;
profile: GL_Profile;
extensions: []GL_Extension;
output_directory: string;
}
main :: () {
gl_protocols_path := "./protocols/gl/gl.xml";
egl_protocols_path := "./protocols/gl/egl.xml";
raw_gl_xml_string, ok := read_entire_file(gl_protocols_path);
if !ok {
log_error("Coulnd't open gl.xml\n");
return;
}
raw_egl_xml_string:, ok= read_entire_file(egl_protocols_path);
if !ok {
log_error("Coulnd't open egl.xml\n");
return;
}
raw_opengl: Raw_OpenGL;
raw_opengl.platform_header = "/usr/include/KHR/khrplatform.h";
raw_opengl.name = "gl";
raw_egl: Raw_OpenGL;
raw_egl.platform_header = "/usr/include/EGL/eglplatform.h";
raw_egl.name = "egl";
success: bool;
success = parse_raw_opengl(*raw_opengl, raw_gl_xml_string);
if !success {
log_error("Unable to parse gl.xml\n");
}
success = parse_raw_opengl(*raw_egl, raw_egl_xml_string);
if !success {
log_error("Unable to parse egl.xml\n");
}
preprocess_raw_opengl(*raw_opengl);
preprocess_raw_opengl(*raw_egl);
gl_gen_options: GL_Generation_Options;
gl_gen_options.api = .GL_VERSION_3_3 | .EGL_VERSION_1_5;
gl_gen_options.extensions = .[
"GL_ARB_direct_state_access",
"GL_ARB_separate_shader_objects",
"GL_KHR_debug",
"EGL_EXT_platform_xcb",
"EGL_EXT_platform_base",
"GL_EXT_texture_filter_anisotropic",
"GL_EXT_texture_sRGB",
"GL_EXT_texture_compression_s3tc",
"GL_ARB_texture_compression_bptc",
"GL_ARB_internalformat_query2",
"GL_ARB_debug_output",
];
gl_gen_options.output_directory = "./protocols/gl/";
generate_jai_bindings(gl_gen_options);
}
types_builder: String_Builder;
types_done: bool;
parse_raw_opengl :: (raw: *Raw_OpenGL, raw_xml_string: string) -> bool {
root, result := xml_parse(raw_xml_string);
if result.status != .ok {
log_error("Problem with XML file: %\n", result.status);
return false;
}
// TODO: Maybe submit a patch to jai-xml that does this cdata collection
// And that accepts user data to the callback, and returns something that indicates a walk
// iterator that it's time to stop.
cb :: (node: *XMLNode) {
if types_done return;
// kinds for gl.xml, enums for egl.xml
if node.name == "kinds" || node.name == "enums" {
types_done = true;
return;
}
if node.type == .pcdata {
// urlencode 101 boysssss
value := replace(node.value, "<", "<");
value = replace(value, ">", ">");
if node.prev_sibling && node.prev_sibling.name == "name" {
// Massive hack for jai-xml eating a whitespace at the beginning
// of cdata. I need to report this
print_to_builder(*types_builder, " %", value);
} else {
append(*types_builder, value);
}
}
if node.name == "type" && node.first_child {
append(*types_builder, "\n");
}
}
assert(root.type == .document, "Unexpected top level root");
registry := root.first_child.next_sibling;
assert(registry.name == "registry", tprint("Expected registry, got: %\n", registry.name));
registry_item := registry.first_child;
// jai-xml has a bug in its for_expansion that makes continue statement unusable.
// So I'm not using it until the bug is fixed
while registry_item {
defer registry_item = registry_item.next_sibling;
if registry_item.name == "types" {
xml_walk_depthfirst(registry_item, cb);
types_string := builder_to_string(*types_builder);
types_done = false;
raw.types = types_string;
} else if registry_item.name == "enums" {
raw_enum_node := registry_item;
raw_enum_namespace: Raw_Enum_Namespace;
for raw_enum_node.attributes {
if it.name == {
case "namespace"; raw_enum_namespace.namespace = it.value;
case "start"; raw_enum_namespace.start = it.value;
case "end"; raw_enum_namespace.end = it.value;
case "vendor"; raw_enum_namespace.vendor = it.value;
case "group"; raw_enum_namespace.group = it.value;
case "type"; raw_enum_namespace.type = it.value;
case "comment"; raw_enum_namespace.comment = it.value;
case; log_error("Unexpected enums attribute: %\n", it.*);
}
}
first_child := raw_enum_node.first_child;
while first_child {
defer first_child = first_child.next_sibling;
raw_enum_value: Raw_Enum_Value;
if first_child.name == "enum" {
for attr: first_child.attributes {
if attr.name == {
case "value"; raw_enum_value.value = attr.value;
case "name"; raw_enum_value.name = attr.value;
case "alias"; raw_enum_value.alias = attr.value;
case "group"; {
groups := split(attr.value, ",");
raw_enum_value.groups = groups;
}
case "comment"; raw_enum_value.comment = attr.value;
case "api"; raw_enum_value.api = attr.value;
case "type"; raw_enum_value.type = attr.value;
case; assert(false, tprint("Unexpected <enum> attribute: %", attr.name));
}
}
} else if first_child.name == "unused" {
b: String_Builder;
for attr: first_child.attributes {
if attr.name == {
case "start"; print_to_builder(*b, "% ", attr.value);
case "end"; print_to_builder(*b, "to % - ", attr.value);
case "vendor"; print_to_builder(*b, "% - ", attr.value);
case "comment"; print_to_builder(*b, attr.value);
}
}
raw_enum_value.comment = builder_to_string(*b);
}
array_add(*raw_enum_namespace.values, raw_enum_value);
}
array_add(*raw.enum_namespaces, raw_enum_namespace);
} else if registry_item.name == "commands" {
command_node := registry_item.first_child;
// Iterating over all of the commands
comment_to_remember: string;
while command_node {
defer command_node = command_node.next_sibling;
raw_command: Raw_Command;
if comment_to_remember {
raw_command.comment = comment_to_remember;
comment_to_remember = "";
}
if command_node.type == .comment {
comment_to_remember = command_node.value;
continue;
}
assert(
command_node.name == "command",
tprint("Expect first child of <commands> to be <command>, got %", command_node.name),
);
proto_node := command_node.first_child;
assert(
proto_node.name == "proto",
tprint("Expect first child of the <command /> node to be <proto>, got %", proto_node.name),
);
// Parse proto
raw_command.proto = parse_raw_command_param(proto_node);
// Parse params and other shit
next_node := proto_node.next_sibling;
while next_node {
defer next_node = next_node.next_sibling;
ifx next_node.name == {
case "param"; {
param := parse_raw_command_param(next_node);
array_add(*raw_command.params, param);
}
case "alias";
case "vecequiv"; {
raw_command.alias = attribute_get_value(next_node, "name");
}
case "glx"; // Fuck GLX, all my homies hate GLX
case; {
assert(false, tprint("Unexpected command child node: %\n", next_node.name));
}
}
}
array_add(*raw.commands_namespace, raw_command);
}
} else if registry_item.name == "feature" {
raw_feature: Raw_Feature;
for attr: registry_item.attributes {
if attr.name == {
case "api"; raw_feature.api = attr.value;
case "name"; raw_feature.name = attr.value;
case "number"; raw_feature.number = attr.value;
case; assert(false, tprint("unexpected feature attribute: %", attr.name));
}
}
require_remove_node := registry_item.first_child;
// Iterating over all of the requires or removes of the feature.
while require_remove_node {
defer require_remove_node = require_remove_node.next_sibling;
if require_remove_node.type != .element {
// There are comments that we just skip for now. Doesn't look like
// they're that much useful in an actual generated code.
continue;
}
raw_require_remove := parse_raw_require_remove(require_remove_node);
if require_remove_node.name == "require" {
array_add(*raw_feature.requires, raw_require_remove);
} else if require_remove_node.name == "remove" {
array_add(*raw_feature.removes, raw_require_remove);
} else {
assert(false, tprint("Unexpected <feature> child node: %\n", require_remove_node.name));
}
}
array_add(*raw.features, raw_feature);
} else if registry_item.name == "extensions" {
extension_node := registry_item.first_child;
while extension_node {
defer extension_node = extension_node.next_sibling;
if extension_node.name != "extension" {
assert(false, tprint("Unexpected <extensions> child: %", extension_node.name));
}
raw_extension: Raw_Extension;
for attr: extension_node.attributes {
if attr.name == {
case "name"; raw_extension.name = attr.value;
case "protect"; raw_extension.protect = attr.value;
case "supported"; {
splitted := split(attr.value, "|");
raw_extension.supported = splitted;
}
case "comment"; raw_extension.comment = attr.value;
case; assert(false, tprint("unexpected extension attribute: %", attr.name));
}
}
require_node := extension_node.first_child;
// Iterating over all of the requires of the extension;
while require_node {
defer require_node = require_node.next_sibling;
if require_node.name != "require" {
assert(false, tprint("Unexpected <extension> child: %", require_node.name));
}
require := parse_raw_require_remove(require_node);
array_add(*raw_extension.requires, require);
}
array_add(*raw.extensions, raw_extension);
}
} else {
// print("%\n", formatStruct(registry_item, 1, true));
}
}
return true;
}
// Temp shit because we don't have capturing lambdas, and the callback doesn't accept user data lmfao
cdata: [..]string;
param_cdata_done: bool;
parse_raw_command_param :: (param_node: *XMLNode) -> Raw_Command_Param {
param_child := param_node.first_child;
result: Raw_Command_Param;
defer {
array_reset_keeping_memory(*cdata);
param_cdata_done = false;
reset_temporary_storage();
}
for param_node.attributes {
if it.name == {
case "kind"; result.kind = it.value;
case "class"; result.class = it.value;
case "group"; result.group = it.value;
case "len"; result.len = it.value;
case; assert(false, "unexpected attr % on node: %", it.name, xml_write(param_node));
}
}
// TODO: Maybe submit a patch to jai-xml that does this cdata collection
// And that accepts user data to the callback, and returns something that indicates a walk
// iterator that it's time to stop.
cb :: (node: *XMLNode) {
if param_cdata_done return;
if node.name == "name" {
param_cdata_done = true;
return;
}
if node.type == .pcdata {
splitted := split(node.value, " ",, temp);
for splitted {
value := trim(it);
if value array_add(*cdata, value);
}
}
}
xml_walk_depthfirst(param_child, cb);
assert(cdata.count > 0, "expected cdata to be populated. Tag is: %", xml_write(param_node));
result.type = array_copy(cdata);
while param_child && param_child.name != "name" {
param_child = param_child.next_sibling;
}
assert(param_child && param_child.name == "name", "Unexpected command proto, couldn't find name: %", xml_write(param_node));
assert(param_child.next_sibling == null, "Expected name to be the last child of the param: %", xml_write(param_node));
result.name = trim(param_child.first_child.value);
return result;
}
parse_raw_require_remove :: (require_remove_node: *XMLNode) -> Raw_Require_Remove {
raw_require_remove: Raw_Require_Remove;
for attr: require_remove_node.attributes {
if attr.name == {
case "comment"; raw_require_remove.comment = attr.value;
case "profile"; raw_require_remove.profile = attr.value;
case "api"; raw_require_remove.api = attr.value;
case; assert(false, tprint("Unexpected <%> attribute: %=\"%\"", require_remove_node.name, attr.name, attr.value));
}
}
// Stands for require_remove_child, but I couldn't be bothered to spell it every time.
rr_child := require_remove_node.first_child;
while rr_child {
defer rr_child = rr_child.next_sibling;
if rr_child.type != .element {
// There are comments that we just skip for now. Doesn't look like
// they're that much useful in an actual generated code.
continue;
}
if rr_child.name == "type" {
// We always provide all typedefs, so no need to pick those individually.
// I'm like 83% confident that it's the right way to go.
continue;
}
if rr_child.name == {
case "enum"; #through;
case "command"; {
for attr: rr_child.attributes {
if attr.name == {
case "name"; {
if rr_child.name == "enum" {
array_add(*raw_require_remove.enums, attr.value);
} else {
array_add(*raw_require_remove.commands, attr.value);
}
}
case "comment"; // Skip comments, at least for now
case; assert(false, tprint("Unexpected % attribute: %", rr_child.name, attr.name));
}
}
}
case; assert(false, tprint("Unexpected % inside of %", rr_child.name, require_remove_node.name));
}
}
return raw_require_remove;
}
preprocess_raw_opengl :: (raw: *Raw_OpenGL) -> bool {
table_set(*gl_prev_features, "GL_VERSION_1_0", 0);
table_set(*gl_prev_features, "GL_VERSION_1_1", .GL_VERSION_1_0);
table_set(*gl_prev_features, "GL_VERSION_1_2", .GL_VERSION_1_0 | .GL_VERSION_1_1);
table_set(*gl_prev_features, "GL_VERSION_1_3", .GL_VERSION_1_0 | .GL_VERSION_1_1 | .GL_VERSION_1_2);
table_set(
*gl_prev_features,
"GL_VERSION_3_1",
.GL_VERSION_1_0 | .GL_VERSION_1_1 | .GL_VERSION_1_2 | .GL_VERSION_1_3 |
.GL_VERSION_1_4 | .GL_VERSION_1_5 | .GL_VERSION_2_0 | .GL_VERSION_2_1 |
.GL_VERSION_3_0,
);
table_set(
*gl_prev_features,
"GL_VERSION_3_2",
.GL_VERSION_1_0 | .GL_VERSION_1_1 | .GL_VERSION_1_2 | .GL_VERSION_1_3 |
.GL_VERSION_1_4 | .GL_VERSION_1_5 | .GL_VERSION_2_0 | .GL_VERSION_2_1 |
.GL_VERSION_3_0 | .GL_VERSION_3_1,
);
table_set(
*gl_prev_features,
"GL_VERSION_3_3",
.GL_VERSION_1_0 | .GL_VERSION_1_1 | .GL_VERSION_1_2 | .GL_VERSION_1_3 |
.GL_VERSION_1_4 | .GL_VERSION_1_5 | .GL_VERSION_2_0 | .GL_VERSION_2_1 |
.GL_VERSION_3_0 | .GL_VERSION_3_1 | .GL_VERSION_3_2,
);
table_set(
*gl_prev_features,
"GL_VERSION_4_0",
.GL_VERSION_1_0 | .GL_VERSION_1_1 | .GL_VERSION_1_2 | .GL_VERSION_1_3 |
.GL_VERSION_1_4 | .GL_VERSION_1_5 | .GL_VERSION_2_0 | .GL_VERSION_2_1 |
.GL_VERSION_3_0 | .GL_VERSION_3_1 | .GL_VERSION_3_2 | .GL_VERSION_3_3,
);
table_set(
*gl_prev_features,
"GL_VERSION_4_6",
.GL_VERSION_1_0 | .GL_VERSION_1_1 | .GL_VERSION_1_2 | .GL_VERSION_1_3 |
.GL_VERSION_1_4 | .GL_VERSION_1_5 | .GL_VERSION_2_0 | .GL_VERSION_2_1 |
.GL_VERSION_3_0 | .GL_VERSION_3_1 | .GL_VERSION_3_2 | .GL_VERSION_3_3 |
.GL_VERSION_4_0 | .GL_VERSION_4_1 | .GL_VERSION_4_2,
);
table_set(
*gl_prev_features,
"GL_VERSION_4_6",
.GL_VERSION_1_0 | .GL_VERSION_1_1 | .GL_VERSION_1_2 | .GL_VERSION_1_3 |
.GL_VERSION_1_4 | .GL_VERSION_1_5 | .GL_VERSION_2_0 | .GL_VERSION_2_1 |
.GL_VERSION_3_0 | .GL_VERSION_3_1 | .GL_VERSION_3_2 | .GL_VERSION_3_3 |
.GL_VERSION_4_0 | .GL_VERSION_4_1 | .GL_VERSION_4_2 | .GL_VERSION_4_3 |
.GL_VERSION_4_4 | .GL_VERSION_4_5,
);
table_set(
*gl_prev_features,
"EGL_VERSION_1_5",
.EGL_VERSION_1_0 | .EGL_VERSION_1_1 | .EGL_VERSION_1_2 | .EGL_VERSION_1_3 |
.EGL_VERSION_1_4,
);
gb: String_Builder;
types_builder: String_Builder;
// Generate types
{
// Writing types code into a temporary file because Bindings_Generator
// doesn't work with in-memory code.
// Even though it seems like it would be very easy to add this funcionality
temp_types_file := "./temp.h";
write_entire_file(temp_types_file, raw.types);
defer file_delete(temp_types_file);
types_opts: Generate_Bindings_Options;
types_opts.add_generator_command = false;
types_opts.strip_enum_prefixes = false;
array_add(*types_opts.source_files, raw.platform_header);
array_add(*types_opts.source_files, temp_types_file);
array_add(*types_opts.typedef_prefixes_to_unwrap, "khronos_");
types_opts.generate_compile_time_struct_checks = false;
types_opts.try_to_preserve_comments = true;
types_opts.mimic_spacing_flags = .STRUCT | .GLOBAL;
types_opts.visitor = gl_types_visitor;
types_builder, success := generate_bindings(types_opts, log_results=false);
if !success return success;
cut_scope_file(*types_builder);
// Inserting ifdef for GLhalf and Apple
if raw.name == "gl" {
print_to_builder(
*types_builder,
replace(
builder_to_string(*types_builder,, temp),
"GLcharARB :: u8;",
tprint("GLcharARB :: u8;\n\n%", GLhandleARB_MACOS_Hack),
),
);
} else if raw.name == "egl" {
print_to_builder(*types_builder, "%", EGL_CAST_Hack);
}
table_set(*types, raw.name, builder_to_string(*types_builder));
}
// Preprocess enums
{
for raw_namespace: raw.enum_namespaces {
using raw_namespace;
b: String_Builder;
// TODO: Think about how and at what point it is better to print the comment
// TODO: Check it different namespaces have conflicting info about some enum being a bitmask?
is_bitmask := type == "bitmask";
for values {
raw_value := it.value;
pe: Preprocessed_Enum_Value;
pe.name = it.name;
pe.value = it.value;
pe.alias = it.alias;
pe.comment = it.comment;
if !it.api {
table_set(*enum_values, pe.name, pe);
} else {
api := api_from_string(it.api);
pe.api = api;
}
// TODO: groups
// if !it.groups && it.name {
// array_add(*preprocessed.flat_entries, tprint("% :: %;%\n", it.name, it.value, ifx it.comment tprint(" // %", it.comment)));
// } else if it.groups {
// for g: it.groups {
// // Specialcasing SpecialNumbers - this is not a real enum, just a bunch of "magic numbers" bungled together in the XML
// // They even have different types - a couple of them are u64, and others are u32.
// if g == "SpecialNumbers" {
// array_add(*preprocessed.flat_entries, tprint("% :: %;%\n", it.name, it.value, ifx it.comment tprint(" // %", it.comment)));
// continue;
// }
// array_add(*preprocessed.enums, g);
// raw_enum, newly_added := table_find_or_add(*enums, g);
// if newly_added {
// raw_enum.name = g;
// raw_enum.is_bitmask = is_bitmask;
// raw_enum.type = ifx starts_with(it.value, "-") "GLint" else "GLenum";
// }
// array_add(*raw_enum.variants, it);
// }
// }
}
}
}
// Preprocess commands
{
format_param :: (raw: Raw_Command_Param, store_name: bool = true) -> string {
// pb stands for param_builder
pb: String_Builder;
pointer_level: string;
type: string;
for raw.type {
value := trim(replace(it, "const", "",, temp));
if !value {
continue;
}
if contains(value, "*") {
pointer_level = value;
} else {
// TODO: handle groups
// if raw.group {
// type = raw.group;
// } else {
// char leaked in egl.xml
if value == "char" {
type = "u8";
} else {
type = value;
}
// }
}
}
if store_name {
name := raw.name;
if name == "context" name = "context_";
if name == "string" name = "string_";
print_to_builder(*pb, "%1: %2%3", name, pointer_level, type);
} else {
print_to_builder(*pb, "%1%2", pointer_level, type);
}
return builder_to_string(*pb);
}
// cb stands for command_builder
cb: String_Builder;
for raw_command: raw.commands_namespace {
pc: Preprocessed_Command;
// defer append(*gb, builder_to_string(*cb));
comment_builder: String_Builder;
if raw_command.comment {
comment_lines := split(raw_command.comment, "\n");
append(*comment_builder, "/*\n");
for comment_lines {
print_to_builder(*comment_builder, "%\n", it);
}
append(*comment_builder, "*/\n");
pc.comment = builder_to_string(*comment_builder);
}
pc.name = raw_command.proto.name;
pc.return_type = format_param(raw_command.proto, store_name=false);
// print_to_builder(*cb, "% :: (", raw_command.proto.name);
for raw_param: raw_command.params {
formatted := format_param(raw_param);
array_add(*pc.arguments, formatted);
}
pc.alias = raw_command.alias;
pc.vecequiv = raw_command.vecequiv;
table_set(*commands, pc.name, pc);
// append(*cb, join(..params, ", "));
// if type == "void" {
// append(*cb, ") #foreign;\n\n");
// } else {
// print_to_builder(*cb, ") -> % #foreign;\n\n", type);
// }
}
}
// Preprocess features
{
for feature: raw.features {
pf: Preprocessed_Feature;
pf.api = enum_name_to_value(GL_API, feature.name);
prev_features, found := table_find(*gl_prev_features, feature.name);
if found {
pf.prev_features = prev_features;
}
enums: [..]string;
commands: [..]string;
for req: feature.requires {
for req.enums {
array_add(*enums, it);
}
for req.commands {
array_add(*commands, it);
}
}
pf.enums = enums;
pf.commands = commands;
removes: [..]string;
for req: feature.removes {
for req.enums {
array_add(*removes, it);
}
for req.commands {
array_add(*removes, it);
}
}
pf.removes = removes;
table_set(*features, feature.name, pf);
// print("%\n", formatStruct(pf, 1, true));
}
}
// Preprocess extensions
{
for ex: raw.extensions {
pe: Preprocessed_Extension;
pe.name = ex.name;
pe.comment = ex.comment;
api: GL_API;
for ex.supported {
if it == "disabled" {
pe.disabled = true;
continue;
}
api |= api_from_string(it);
}
pe.api = api;
for require: ex.requires {
if require.profile {
if require.profile == "compatibility" {
for require.enums {
array_add(*pe.enums_compat, it);
}
for require.commands {
array_add(*pe.commands_compat, it);
}
} else {
// Should never happen
assert(false, "this should never happen, but we found unexpected extension profile: %\n", require.profile);
}
} else {
for require.enums {
array_add(*pe.enums_core, it);
}
for require.commands {
array_add(*pe.commands_core, it);
}
}
}
table_set(*extensions, pe.name, pe);
}
}
return true;