Skip to content

Commit 9eefa84

Browse files
Improved Flags enums usability (#4)
1 parent f7c56ff commit 9eefa84

File tree

135 files changed

+612
-266
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+612
-266
lines changed

modules/vulkan/generator/source/app.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,10 @@ struct App {
251251

252252
private void listBitmasks(ref VkRegistry registry) {
253253
foreach (ref bitmask; registry.bitmasks) {
254-
if (bitmask.requires.empty) {
254+
if (bitmask.bitvalues.empty) {
255255
logger.info("<lgreen>%s</lgreen> : <lblue>%s</lblue>", bitmask.name, bitmask.backing);
256256
} else {
257-
logger.info("<lgreen>%s</lgreen> : <lgreen>%s</lgreen>", bitmask.name, bitmask.requires);
257+
logger.info("<lgreen>%s</lgreen> : <lgreen>%s</lgreen>", bitmask.name, bitmask.bitvalues);
258258
}
259259
}
260260

modules/vulkan/generator/source/emitter.d

Lines changed: 95 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,6 @@ class VkRegistryEmitter {
161161
file.writefln!"import vulkan.%s.%s;"(deprecated_.prefix, deprecated_.shortName);
162162
}
163163

164-
if (auto deps = ext.name in dependencies) {
165-
foreach (ref dep; (*deps).map!(d => registry.extensions[d])) {
166-
file.writefln!"import vulkan.%s.%s;"(dep.prefix, dep.shortName);
167-
}
168-
}
169-
170164
if (auto video = ext.name in videos) {
171165
foreach (v; *video) {
172166
file.writefln!"import vulkan.video.%s;"(v);
@@ -340,7 +334,7 @@ class VkRegistryEmitter {
340334
* Emit a section from core Vulkan.
341335
*/
342336
private void emitCoreSection(const ref VkSection section, const ref VkFeature feature, ref VkTypeCategory prevcategory) {
343-
emitSection(section, prevcategory);
337+
emitSection(section, prevcategory, null);
344338

345339
if (!section.commands.empty) {
346340
if (prevcategory) {
@@ -369,11 +363,11 @@ class VkRegistryEmitter {
369363
* Emit a section from a Vulkan extension.
370364
*/
371365
private void emitExtSection(const ref VkSection section, const ref VkExtension ext, ref VkTypeCategory prevcategory) {
372-
emitSection(section, prevcategory);
366+
emitSection(section, prevcategory, &ext);
373367

374368
foreach (ref command; section.commands.map!(c => registry.commands[c])) {
375369
file.writeln();
376-
emitFuncPtr(command);
370+
emitFuncPtr(command, &ext);
377371
}
378372
}
379373

@@ -432,7 +426,7 @@ class VkRegistryEmitter {
432426
/**
433427
* Common functionality between all functions that emit Vulkan sections.
434428
*/
435-
private VkTypeCategory emitSection(const ref VkSection section, ref VkTypeCategory prevcategory) {
429+
private VkTypeCategory emitSection(const ref VkSection section, ref VkTypeCategory prevcategory, const VkExtension* current) {
436430
// Section marker.
437431
if (!section.name.empty) {
438432
file.writeln();
@@ -476,23 +470,23 @@ class VkRegistryEmitter {
476470
break;
477471

478472
case VkTypeCategory.Bitmask:
479-
emitBitmask(registry.bitmasks[type.name]);
473+
emitBitmask(registry.bitmasks[type.name], current);
480474
break;
481475

482476
case VkTypeCategory.Handle:
483477
emitHandle(registry.handles[type.name]);
484478
break;
485479

486480
case VkTypeCategory.Enum:
487-
emitEnum(registry.enums[type.name]);
481+
emitEnum(registry.enums[type.name], current);
488482
break;
489483

490484
case VkTypeCategory.FuncPtr:
491-
emitFuncPtr(registry.funcptrs[type.name]);
485+
emitFuncPtr(registry.funcptrs[type.name], current);
492486
break;
493487

494488
case VkTypeCategory.Struct:
495-
emitStruct(registry.structs[type.name]);
489+
emitStruct(registry.structs[type.name], current);
496490
break;
497491

498492
case VkTypeCategory.Union:
@@ -522,7 +516,7 @@ class VkRegistryEmitter {
522516
(typeof(null)) {},
523517
(string name) {
524518
if (name in registry.features) {
525-
file.writefln!"version (%s):"(name);
519+
// file.writefln!"version (%s):"(name);
526520
} else if (auto dep = name in registry.extensions) {
527521
file.writefln!"public import vulkan.%s.%s;"(dep.prefix, dep.shortName);
528522
} else {
@@ -572,8 +566,11 @@ class VkRegistryEmitter {
572566
}
573567
}
574568

575-
private void emitBitmask(const ref VkBitmaskType bitmask) {
576-
if (bitmask.alias_.empty) {
569+
private void emitBitmask(const ref VkBitmaskType bitmask, const VkExtension* current) {
570+
if (bitmask.bitvalues) {
571+
emitProvenance(bitmask.bitvalues, current);
572+
file.writefln!"alias %s = VkBitFlagsBase!(%s, %s);"(bitmask.name, bitmask.backing, bitmask.bitvalues);
573+
} else if (bitmask.alias_.empty) {
577574
file.writefln!"alias %s = %s;"(bitmask.name, bitmask.backing);
578575
} else {
579576
file.writefln!"alias %s = %s;"(bitmask.name, bitmask.alias_);
@@ -589,7 +586,8 @@ class VkRegistryEmitter {
589586
}
590587
}
591588

592-
private void emitEnum(const ref VkEnumType enum_) {
589+
private void emitEnum(const ref VkEnumType enum_, const(VkExtension)* current) {
590+
emitProvenance(enum_, current);
593591
if (enum_.alias_) {
594592
emitAlias(enum_.name, enum_.alias_);
595593
} else if (enum_.bitmask) {
@@ -649,20 +647,21 @@ class VkRegistryEmitter {
649647
}
650648
}
651649

652-
private void emitFuncPtr(const ref VkCommand command) {
650+
private void emitFuncPtr(const ref VkCommand command, const(VkExtension)* current) {
653651
if (command.alias_) {
654652
auto funcptr = registry.commands[command.alias_].funcptr;
655-
emitFuncPtr(funcptr);
653+
emitFuncPtr(funcptr, current);
656654
} else {
657655
auto funcptr = command.funcptr;
658-
emitFuncPtr(funcptr);
656+
emitFuncPtr(funcptr, current);
659657
}
660658
}
661659

662-
private void emitFuncPtr(const ref VkFuncPtrType funcptr) {
660+
private void emitFuncPtr(const ref VkFuncPtrType funcptr, const(VkExtension)* current) {
663661
if (funcptr.params.empty) {
664662
file.writefln!"alias %s = %s function();"(funcptr.name, funcptr.type);
665663
} else {
664+
emitProvenance(funcptr, current);
666665
file.openf!"alias %s = %s function("(funcptr.name, funcptr.type);
667666
foreach (ref param; funcptr.params) {
668667
file.writefln!"%s %s,"(param.type, param.safename);
@@ -671,11 +670,12 @@ class VkRegistryEmitter {
671670
}
672671
}
673672

674-
private void emitStruct(const ref VkStructType struct_) {
673+
private void emitStruct(const ref VkStructType struct_, const(VkExtension)* current) {
675674
if (struct_.name.isBespoke) {
676675
return;
677676
}
678677

678+
emitProvenance(struct_, current);
679679
if (!struct_.alias_.empty) {
680680
emitAlias(struct_.name, struct_.alias_);
681681
} else if (struct_.members.empty) {
@@ -735,6 +735,7 @@ class VkRegistryEmitter {
735735
if (command.params.empty) {
736736
file.writefln!"extern %s %s();"(command.type, command.name);
737737
} else {
738+
emitProvenance(command, null);
738739
file.openf!"extern %s %s("(command.type, command.name);
739740
foreach (param; command.params) {
740741
file.writefln!"%s %s,"(param.type, param.safename);
@@ -749,6 +750,77 @@ class VkRegistryEmitter {
749750
}
750751
}
751752

753+
private void emitProvenance(const ref VkEnumType enum_, const(VkExtension)* current) {
754+
if (enum_.alias_) {
755+
emitProvenance(enum_.alias_, current);
756+
return;
757+
}
758+
}
759+
760+
private void emitProvenance(const ref VkStructType struct_, const(VkExtension)* current) {
761+
if (struct_.alias_) {
762+
emitProvenance(struct_.alias_, current);
763+
return;
764+
}
765+
766+
string[][string] provs;
767+
foreach (ref member; struct_.members) {
768+
if (auto ext = registry.provenance(member.utype)) {
769+
provs[ext.name] ~= member.utype;
770+
}
771+
}
772+
773+
foreach (ext, deps; provs) {
774+
emitImport(registry.extensions[ext], deps.filter!(d => d !in *current).array);
775+
}
776+
}
777+
778+
private void emitProvenance(const ref VkFuncPtrType funcptr, const(VkExtension)* current) {
779+
string[][string] provs;
780+
foreach (ref param; funcptr.params) {
781+
if (auto ext = registry.provenance(param.utype)) {
782+
provs[ext.name] ~= param.utype;
783+
}
784+
}
785+
786+
foreach (ext, deps; provs) {
787+
emitImport(registry.extensions[ext], deps.filter!(d => d !in *current).array);
788+
}
789+
}
790+
791+
private void emitProvenance(const ref VkCommand command, const(VkExtension)* current) {
792+
string[][string] provs;
793+
foreach (ref param; command.params) {
794+
if (auto ext = registry.provenance(param.utype)) {
795+
provs[ext.name] ~= param.utype;
796+
}
797+
}
798+
799+
foreach (ext, deps; provs) {
800+
emitImport(registry.extensions[ext], deps.filter!(d => d !in *current).array);
801+
}
802+
}
803+
804+
private void emitProvenance(string name, const(VkExtension)* current) {
805+
if (!current || name !in *current) {
806+
if (auto ext = registry.provenance(name)) {
807+
if (!current || ext.name != current.name) {
808+
emitImport(*ext, [name]);
809+
}
810+
}
811+
}
812+
}
813+
814+
private void emitImport(const ref VkExtension ext) {
815+
file.writefln!"import vulkan.%s.%s;"(ext.prefix, ext.shortName);
816+
}
817+
818+
private void emitImport(const ref VkExtension ext, string[] deps) {
819+
if (!deps.empty) {
820+
file.writefln!"import vulkan.%s.%s : %s;"(ext.prefix, ext.shortName, deps.join(", "));
821+
}
822+
}
823+
752824
private void emitAlias(string lhs, string rhs) {
753825
file.writefln!"alias %s = %s;"(lhs, rhs);
754826
}
@@ -877,35 +949,6 @@ private const string[string] platforms = [
877949
"macos": "OSX",
878950
];
879951

880-
/**
881-
* Dependencies between extensions that have to be manually added because
882-
* vk.xml is a mess.
883-
*/
884-
private string[][string] dependencies = [
885-
"VK_EXT_pipeline_properties": [
886-
"VK_KHR_pipeline_executable_properties",
887-
],
888-
"VK_KHR_ray_tracing_pipeline": [
889-
"VK_KHR_pipeline_library",
890-
],
891-
"VK_NV_framebuffer_mixed_samples": [
892-
"VK_AMD_mixed_attachment_samples",
893-
],
894-
"VK_NV_cooperative_matrix": [
895-
"VK_KHR_shader_bfloat16",
896-
"VK_KHR_cooperative_matrix",
897-
],
898-
"VK_NV_partitioned_acceleration_structure": [
899-
"VK_NV_cluster_acceleration_structure",
900-
],
901-
"VK_QCOM_render_pass_transform": [
902-
"VK_KHR_surface",
903-
],
904-
"VK_QCOM_rotated_copy_commands": [
905-
"VK_KHR_surface",
906-
],
907-
];
908-
909952
/**
910953
* Map Vulkan extensions to Vulkan Video modules.
911954
*/

modules/vulkan/generator/source/parser.d

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,10 @@ class VkRegistryParser {
263263
VkBitmaskType result;
264264
result.base = base;
265265

266-
if (auto requires = element.getAttribute("requires")) {
267-
result.requires = requires.idup;
266+
if (auto bitvalues = element.getAttribute("bitvalues")) {
267+
result.bitvalues = bitvalues.idup;
268+
} else if (auto requires = element.getAttribute("requires")) {
269+
result.bitvalues = requires.idup;
268270
}
269271

270272
if (auto alias_ = element.getAttribute("alias")) {
@@ -398,6 +400,7 @@ class VkRegistryParser {
398400
auto postfix = child.textContent[$ - postNameLength + member.name.length .. $ - commentLength];
399401
auto whole = child.textContent[0 .. $ - postNameLength] ~ postfix;
400402
member.type = parseTypeString(type.textContent, whole, width: &member.width);
403+
member.utype = type.textContent.idup;
401404
}
402405
}
403406

@@ -758,6 +761,7 @@ class VkRegistryParser {
758761
auto postfix = child.textContent[$ - postNameLength + param.name.length .. $];
759762
auto whole = child.textContent[0 .. $ - postNameLength] ~ postfix;
760763
param.type = parseTypeString(type.textContent, whole, refify: !param.optional);
764+
param.utype = type.textContent.idup;
761765
}
762766
}
763767

@@ -924,15 +928,21 @@ class VkRegistryParser {
924928
foreach (child; sectiontag.childElements) {
925929
switch (child.tagName) {
926930
case "enum":
927-
parseSectionEnum(section, child, result.number);
931+
if (auto name = parseSectionEnum(section, child, result.number)) {
932+
registry.sources[name][result.name] = true;
933+
}
928934
break;
929935

930936
case "type":
931-
parseSectionType(section, child);
937+
if (auto name = parseSectionType(section, child)) {
938+
registry.sources[name][result.name] = true;
939+
}
932940
break;
933941

934942
case "command":
935-
parseSectionCommand(section, child);
943+
if (auto name = parseSectionCommand(section, child)) {
944+
registry.sources[name][result.name] = true;
945+
}
936946
break;
937947

938948
case "feature":
@@ -963,7 +973,7 @@ class VkRegistryParser {
963973
* <enum extends="enumname" ...> is for extending existing enums.
964974
* <enum alias="somename" ...> is for aliasing some other enum member.
965975
*/
966-
private void parseSectionEnum(ref VkSection section, XmlElement element, int ext = 0) {
976+
private string parseSectionEnum(ref VkSection section, XmlElement element, int ext = 0) {
967977
if (auto name = element.getAttribute("name")) {
968978
VkEnumMember member;
969979
member.name = name.idup;
@@ -1009,15 +1019,23 @@ class VkRegistryParser {
10091019
section.mconsts ~= member;
10101020
}
10111021
}
1022+
1023+
return member.name;
10121024
}
1025+
1026+
return null;
10131027
}
10141028

1015-
private void parseSectionType(ref VkSection section, XmlElement element) {
1016-
section.types ~= element.getAttribute("name").idup;
1029+
private string parseSectionType(ref VkSection section, XmlElement element) {
1030+
auto name = element.getAttribute("name").idup;
1031+
section.types ~= name;
1032+
return name;
10171033
}
10181034

1019-
private void parseSectionCommand(ref VkSection section, XmlElement element) {
1020-
section.commands ~= element.getAttribute("name").idup;
1035+
private string parseSectionCommand(ref VkSection section, XmlElement element) {
1036+
auto name = element.getAttribute("name").idup;
1037+
section.commands ~= name;
1038+
return name;
10211039
}
10221040

10231041
/**

0 commit comments

Comments
 (0)