-
Notifications
You must be signed in to change notification settings - Fork 352
Expand file tree
/
Copy pathVulkanHppGenerator.cpp
More file actions
16978 lines (15669 loc) · 739 KB
/
VulkanHppGenerator.cpp
File metadata and controls
16978 lines (15669 loc) · 739 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
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
// Copyright(c) 2015-2020, NVIDIA CORPORATION. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "VulkanHppGenerator.hpp"
#include "XMLHelper.hpp"
#include <algorithm>
#include <array>
#include <cassert>
#include <fstream>
#include <future>
#include <numeric>
#include <regex>
using namespace std::literals;
namespace
{
template <typename T>
bool containsByName( std::vector<T> const & values, std::string const & name );
template <typename T>
bool containsByNameOrAlias( std::vector<T> const & values, std::string const & name );
template <typename T>
bool containsByNameOrAlias( std::map<std::string, T> const & values, std::string const & name );
std::vector<std::pair<std::string, size_t>> filterNumbers( std::vector<std::string> const & names );
template <typename T>
typename std::vector<T>::const_iterator findByName( std::vector<T> const & values, std::string const & name );
template <typename T>
typename std::vector<T>::iterator findByName( std::vector<T> & values, std::string const & name );
template <typename T>
typename std::map<std::string, T>::const_iterator findByNameOrAlias( std::map<std::string, T> const & values, std::string const & name );
template <typename T>
typename std::map<std::string, T>::iterator findByNameOrAlias( std::map<std::string, T> & values, std::string const & name );
template <typename T>
typename std::vector<T>::const_iterator findByNameOrAlias( std::vector<T> const & values, std::string const & name );
template <typename T>
typename std::vector<T>::const_iterator findByType( std::vector<T> const & values, std::string const & type );
std::string generateCArraySizes( std::vector<std::string> const & sizes );
std::string generateList( std::vector<std::string> const & elements, std::string const & prefix, std::string const & separator );
std::string generateNoDiscard( bool returnsSomething, bool multiSuccessCodes, bool multiErrorCodes );
std::string generateStandardArray( std::string const & type, std::vector<std::string> const & sizes );
bool isUpperCase( std::string const & name );
VulkanHppGenerator::MacroData parseMacro( std::vector<std::string> const & completeMacro );
std::string startLowerCase( std::string const & input );
std::string startUpperCase( std::string const & input );
std::vector<std::string> tokenizeAny( std::string const & tokenString, std::string const & separators );
} // namespace
// Some types come in as non-const pointers, but are meant as const-pointers
std::set<std::string> const specialPointerTypes = { "Display", "IDirectFB", "wl_display", "xcb_connection_t", "_screen_window", "VkExportMetalObjectsInfoEXT" };
//
// VulkanHppGenerator public interface
//
VulkanHppGenerator::VulkanHppGenerator( tinyxml2::XMLDocument const & document, std::string const & api ) : m_api( api )
{
// insert the default "handle" without class (for createInstance, and such)
m_handles.insert( { "", {} } );
// read the document and check its correctness
int const line = document.GetLineNum();
std::vector<tinyxml2::XMLElement const *> elements = getChildElements( &document );
checkElements( line, elements, { { "registry", MultipleAllowed::No } } );
readRegistry( elements[0] );
filterLenMembers();
checkCorrectness();
handleRemovals();
// add the commands to the respective handles
// some "FlagBits" enums are not specified, but needed for our "Flags" handling -> add them here
for ( auto & feature : m_features )
{
forEachRequiredCommand( feature.requireData,
[this]( NameLine const & command, auto const & commandData ) { addCommandToHandle( { command.name, commandData.second } ); } );
addMissingFlagBits( feature.requireData, feature.name );
}
for ( auto & extension : m_extensions )
{
forEachRequiredCommand( extension.requireData,
[this]( NameLine const & command, auto const & commandData ) { addCommandToHandle( { command.name, commandData.second } ); } );
addMissingFlagBits( extension.requireData, extension.name );
}
m_definesPartition = partitionDefines( m_defines );
}
void VulkanHppGenerator::distributeSecondLevelCommands()
{
// distribute commands from instance/device to second-level handles, like Queue, Event,... for RAII handles
assert( !m_handles.empty() && m_handles.begin()->first.empty() ); // the first "handle" is the empty one
for ( auto handleIt = std::next( m_handles.begin() ); handleIt != m_handles.end(); ++handleIt )
{
for ( auto command = handleIt->second.commands.begin(); command != handleIt->second.commands.end(); )
{
bool foundCommand = false;
if ( !m_RAIISpecialFunctions.contains( *command ) )
{
auto commandIt = findByNameOrAlias( m_commands, *command );
assert( commandIt != m_commands.end() );
assert( commandIt->second.params.front().type.type == handleIt->first );
if ( ( 1 < commandIt->second.params.size() ) && ( isHandleType( commandIt->second.params[1].type.type ) ) && !commandIt->second.params[1].optional )
{
auto secondLevelHandleIt = m_handles.find( commandIt->second.params[1].type.type );
assert( secondLevelHandleIt != m_handles.end() );
// filter out functions that seem to fit due to taking handles as first and second argument, but the first argument is not the
// type to create the second one, and so it's unknown to the raii handle!
assert( !secondLevelHandleIt->second.constructorIts.empty() );
if ( ( *secondLevelHandleIt->second.constructorIts.begin() )->second.handle == handleIt->first )
{
assert( std::ranges::none_of( secondLevelHandleIt->second.constructorIts,
[handleIt]( auto const & constructorIt ) { return constructorIt->second.handle != handleIt->first; } ) );
secondLevelHandleIt->second.secondLevelCommands.insert( *command );
command = handleIt->second.commands.erase( command );
foundCommand = true;
}
}
}
if ( !foundCommand )
{
++command;
}
}
}
}
void VulkanHppGenerator::generateCppmFile() const
{
generateFileFromTemplate( m_api + ".cppm",
"CppmTemplate.hpp",
{ { "api", m_api },
{ "vulkan_h", ( m_api == "vulkansc" ) ? "vulkan_sc_core.h" : ( m_api + ".h" ) },
{ "licenseHeader", m_vulkanLicenseHeader },
{ "headerVersion", m_version },
{ "pfnCommands", generateCppModuleCommands() } } );
}
void VulkanHppGenerator::generateEnumsHppFile() const
{
generateFileFromTemplate( m_api + "_enums.hpp",
"EnumsHppTemplate.hpp",
{ { "enums", generateEnums() },
{ "Flags", readSnippet( "Flags.hpp" ) },
{ "licenseHeader", m_vulkanLicenseHeader },
{ "objectTypeToDebugReportObjectType", generateObjectTypeToDebugReportObjectType() } } );
}
void VulkanHppGenerator::generateExtensionInspectionFile() const
{
generateFileFromTemplate(
m_api + "_extension_inspection.hpp",
"ExtensionInspectionHppTemplate.hpp",
{ { "api", m_api },
{ "deprecatedExtensions",
generateReplacedExtensionsList( []( ExtensionData const & extension ) { return extension.isDeprecated; },
[]( ExtensionData const & extension ) { return extension.deprecatedBy; } ) },
{ "deviceExtensions", generateExtensionsList( "device" ) },
{ "deviceTest", generateExtensionTypeTest( "device" ) },
{ "deprecatedBy",
generateExtensionReplacedBy( []( ExtensionData const & extension ) { return extension.isDeprecated; },
[]( ExtensionData const & extension ) { return extension.deprecatedBy; } ) },
{ "deprecatedTest", generateExtensionReplacedTest( []( ExtensionData const & extension ) { return extension.isDeprecated; } ) },
{ "extensionDependencies", generateExtensionDependencies() },
{ "instanceExtensions", generateExtensionsList( "instance" ) },
{ "instanceTest", generateExtensionTypeTest( "instance" ) },
{ "licenseHeader", m_vulkanLicenseHeader },
{ "obsoletedBy",
generateExtensionReplacedBy( []( ExtensionData const & extension ) { return !extension.obsoletedBy.empty(); },
[]( ExtensionData const & extension ) { return extension.obsoletedBy; } ) },
{ "obsoletedExtensions",
generateReplacedExtensionsList( []( ExtensionData const & extension ) { return !extension.obsoletedBy.empty(); },
[]( ExtensionData const & extension ) { return extension.obsoletedBy; } ) },
{ "obsoletedTest", generateExtensionReplacedTest( []( ExtensionData const & extension ) { return !extension.obsoletedBy.empty(); } ) },
{ "promotedExtensions",
generateReplacedExtensionsList( []( ExtensionData const & extension ) { return !extension.promotedTo.empty(); },
[]( ExtensionData const & extension ) { return extension.promotedTo; } ) },
{ "promotedTest", generateExtensionReplacedTest( []( ExtensionData const & extension ) { return !extension.promotedTo.empty(); } ) },
{ "promotedTo",
generateExtensionReplacedBy( []( ExtensionData const & extension ) { return !extension.promotedTo.empty(); },
[]( ExtensionData const & extension ) { return extension.promotedTo; } ) },
{ "versions",
std::accumulate( std::next( m_features.begin() ),
m_features.end(),
"\"" + m_features[0].name + "\"",
[]( std::string const & acc, auto const & feature ) { return acc + ", \"" + feature.name + "\""; } ) },
{ "voidExtension", ( m_api == "vulkan" ) ? "" : "(void)extension;" } } );
}
void VulkanHppGenerator::generateFormatTraitsHppFile() const
{
generateFileFromTemplate( m_api + "_format_traits.hpp",
"FormatTraitsHppTemplate.hpp",
{ { "api", m_api }, { "formatTraits", generateFormatTraits() }, { "licenseHeader", m_vulkanLicenseHeader } } );
}
void VulkanHppGenerator::generateFuncsHppFile() const
{
generateFileFromTemplate(
m_api + "_funcs.hpp", "FuncsHppTemplate.hpp", { { "commandDefinitions", generateCommandDefinitions() }, { "licenseHeader", m_vulkanLicenseHeader } } );
}
void VulkanHppGenerator::generateHandlesHppFile() const
{
generateFileFromTemplate( m_api + "_handles.hpp",
"HandlesHppTemplate.hpp",
{ { "funcPointerReturns", generateFuncPointerReturns() },
{ "handles", generateHandles() },
{ "handleForwardDeclarations", generateHandleForwardDeclarations() },
{ "licenseHeader", m_vulkanLicenseHeader },
{ "structForwardDeclarations", generateStructForwardDeclarations() },
{ "uniqueHandles", generateUniqueHandles() } } );
}
void VulkanHppGenerator::generateHashHppFile() const
{
generateFileFromTemplate( m_api + "_hash.hpp",
"HashHppTemplate.hpp",
{ { "api", m_api },
{ "handleHashStructures", generateHandleHashStructures() },
{ "licenseHeader", m_vulkanLicenseHeader },
{ "structHashStructures", generateStructHashStructures() } } );
}
void VulkanHppGenerator::generateHppFile() const
{
generateFileFromTemplate(
m_api + ".hpp",
"HppTemplate.hpp",
{ { "api", m_api },
{ "ArrayProxy", readSnippet( "ArrayProxy.hpp" ) },
{ "ArrayProxyNoTemporaries", readSnippet( "ArrayProxyNoTemporaries.hpp" ) },
{ "ArrayWrapper1D", readSnippet( "ArrayWrapper1D.hpp" ) },
{ "ArrayWrapper2D", readSnippet( "ArrayWrapper2D.hpp" ) },
{ "baseTypes", generateBaseTypes() },
{ "constexprDefines", generateConstexprDefines() },
{ "defines", readSnippet( "defines.hpp" ) },
{ "DispatchLoaderBase", readSnippet( "DispatchLoaderBase.hpp" ) },
{ "DispatchLoaderDynamic", generateDispatchLoaderDynamic() },
{ "DispatchLoaderStatic", generateDispatchLoaderStatic() },
{ "DynamicLoader", readSnippet( "DynamicLoader.hpp" ) },
{ "Exceptions", readSnippet( "Exceptions.hpp" ) },
{ "Exchange", readSnippet( "Exchange.hpp" ) },
{ "headerVersion", m_version },
{ "includes", replaceWithMap( readSnippet( "includes.hpp" ), { { "vulkan_h", ( m_api == "vulkansc" ) ? "vulkan_sc_core.h" : ( m_api + ".h" ) } } ) },
{ "IsDispatchedList", generateIsDispatchedList() },
{ "licenseHeader", m_vulkanLicenseHeader },
{ "ObjectDestroy", readSnippet( "ObjectDestroy.hpp" ) },
{ "ObjectFree", readSnippet( "ObjectFree.hpp" ) },
{ "ObjectRelease", readSnippet( "ObjectRelease.hpp" ) },
{ "Optional", readSnippet( "Optional.hpp" ) },
{ "PoolFree", readSnippet( "PoolFree.hpp" ) },
{ "resultChecks", readSnippet( "resultChecks.hpp" ) },
{ "resultExceptions", generateResultExceptions() },
{ "structExtendsStructs", generateStructExtendsStructs() },
{ "ResultValue", readSnippet( "ResultValue.hpp" ) },
{ "StridedArrayProxy", readSnippet( "StridedArrayProxy.hpp" ) },
{ "StructureChain", readSnippet( "StructureChain.hpp" ) },
{ "throwResultException", generateThrowResultException() },
{ "UniqueHandle", readSnippet( "UniqueHandle.hpp" ) } } );
}
void VulkanHppGenerator::generateMacrosFile() const
{
generateFileFromTemplate( m_api + "_hpp_macros.hpp",
"MacrosHppTemplate.hpp",
{ { "licenseHeader", m_vulkanLicenseHeader },
{ "vulkan_hpp", m_api + ".hpp" },
{ "vulkan_64_bit_ptr_defines", m_defines.at( "VK_USE_64_BIT_PTR_DEFINES" ).possibleDefinition } } );
}
void VulkanHppGenerator::generateRAIIHppFile() const
{
generateFileFromTemplate( m_api + "_raii.hpp",
"RAIIHppTemplate.hpp",
{ { "api", m_api },
{ "licenseHeader", m_vulkanLicenseHeader },
{ "RAIICommandDefinitions", generateRAIICommandDefinitions() },
{ "RAIIDispatchers", generateRAIIDispatchers() },
{ "RAIIHandles", generateRAIIHandles() } } );
}
void VulkanHppGenerator::generateSharedHppFile() const
{
generateFileFromTemplate( m_api + "_shared.hpp",
"SharedHppTemplate.hpp",
{ { "api", m_api },
{ "licenseHeader", m_vulkanLicenseHeader },
{ "sharedHandles", generateSharedHandles() },
{ "sharedHandlesNoDestroy", generateSharedHandlesNoDestroy() } } );
}
void VulkanHppGenerator::generateStaticAssertionsHppFile() const
{
generateFileFromTemplate( m_api + "_static_assertions.hpp",
"StaticAssertionsHppTemplate.hpp",
{ { "api", m_api }, { "licenseHeader", m_vulkanLicenseHeader }, { "staticAssertions", generateStaticAssertions() } } );
}
void VulkanHppGenerator::generateStructsHppFile() const
{
generateFileFromTemplate(
m_api + "_structs.hpp", "StructsHppTemplate.hpp", { { "licenseHeader", m_vulkanLicenseHeader }, { "structs", generateStructs() } } );
}
void VulkanHppGenerator::generateToStringHppFile() const
{
generateFileFromTemplate( m_api + "_to_string.hpp",
"ToStringHppTemplate.hpp",
{ { "api", m_api },
{ "bitmasksToString", generateBitmasksToString() },
{ "enumsToString", generateEnumsToString() },
{ "licenseHeader", m_vulkanLicenseHeader } } );
}
void VulkanHppGenerator::prepareRAIIHandles()
{
// filter out functions that are not usefull on this level of abstraction (like vkGetInstanceProcAddr)
// and all the destruction functions, as they are used differently
assert( m_handles.begin()->first.empty() );
for ( auto handleIt = std::next( m_handles.begin() ); handleIt != m_handles.end(); ++handleIt )
{
handleIt->second.destructorIt = determineRAIIHandleDestructor( handleIt->first );
if ( handleIt->second.destructorIt != m_commands.end() )
{
m_RAIISpecialFunctions.insert( handleIt->second.destructorIt->first );
}
handleIt->second.constructorIts = determineRAIIHandleConstructors( handleIt->first, handleIt->second.destructorIt );
}
}
//
// VulkanHppGenerator private interface
//
void VulkanHppGenerator::addCommand( std::string const & name, CommandData && commandData )
{
checkForError( !commandData.params.empty(), commandData.xmlLine, "command <" + name + "> with no params" );
auto [commandIt, inserted] = m_commands.insert( { name, std::move( commandData ) } );
checkForError( inserted, commandData.xmlLine, "already encountered command <" + name + ">" );
// find the handle this command is going to be associated to
auto handleIt = m_handles.find( commandIt->second.params[0].type.type );
if ( handleIt == m_handles.end() )
{
handleIt = m_handles.begin();
assert( handleIt->first == "" );
}
commandIt->second.handle = handleIt->first;
}
void VulkanHppGenerator::addCommandToHandle( std::pair<std::string, CommandData> const & commandData )
{
auto handleIt = m_handles.find( commandData.second.handle );
assert( handleIt != m_handles.end() );
if ( !handleIt->second.commands.contains( commandData.first ) )
{
handleIt->second.commands.insert( commandData.first );
registerDeleter( commandData.first, commandData.second );
}
}
void VulkanHppGenerator::addMissingFlagBits( std::vector<RequireData> & requireData, std::string const & requiredBy )
{
for ( auto & require : requireData )
{
std::vector<NameLine> newTypes;
for ( auto const & type : require.types )
{
auto bitmaskIt = m_bitmasks.find( type.name );
if ( bitmaskIt != m_bitmasks.end() )
{
if ( bitmaskIt->second.require.empty() )
{
// generate the flagBits enum name out of the bitmask name: VkFooFlagsXXX -> VkFooFlagBitsXXX
size_t const pos = bitmaskIt->first.find( "Flags" );
assert( pos != std::string::npos );
std::string flagBits = bitmaskIt->first.substr( 0, pos + 4 ) + "Bit" + bitmaskIt->first.substr( pos + 4 );
assert( flagBits.find( "Flags" ) == std::string::npos );
bitmaskIt->second.require = flagBits;
// some flagsBits are specified but never listed as required for any flags!
// so, even if this bitmask has no enum listed as required, it might still already exist in the enums list
auto enumIt = m_enums.find( flagBits );
if ( enumIt == m_enums.end() )
{
m_enums.insert( { flagBits, EnumData{ .isBitmask = true, .xmlLine = 0 } } );
assert( !m_types.contains( flagBits ) );
m_types.insert( { flagBits, TypeData{ TypeCategory::Bitmask, { requiredBy }, 0 } } );
}
else
{
assert( m_types.contains( flagBits ) );
enumIt->second.isBitmask = true;
}
}
if ( std::ranges::none_of( require.types, [bitmaskIt]( auto const & requireType ) { return requireType.name == bitmaskIt->second.require; } ) )
{
// this bitmask requires a flags type that is not listed in here, so add it
newTypes.push_back( { bitmaskIt->second.require, bitmaskIt->second.xmlLine } );
}
}
}
// add all the newly created flagBits types to the require list as if they had been part of the vk.xml!
require.types.insert( require.types.end(), newTypes.begin(), newTypes.end() );
}
}
std::string VulkanHppGenerator::addTitleAndProtection( std::string const & title, std::string const & strIf, std::string const & strElse ) const
{
std::string str;
if ( !strIf.empty() )
{
auto const [enter, leave] = generateProtection( getProtectFromTitle( title ) );
str = "\n" + enter + " //=== " + title + " ===\n" + strIf;
if ( !enter.empty() && !strElse.empty() )
{
str += "#else \n" + strElse;
}
str += leave;
}
return str;
}
bool VulkanHppGenerator::allVectorSizesSupported( std::vector<ParamData> const & params, std::map<size_t, VectorParamData> const & vectorParams ) const
{
return std::ranges::all_of( vectorParams,
[¶ms]( auto const & vpi )
{
static std::set<std::string> const sizeTypes = { "size_t", "uint32_t", "VkDeviceSize", "VkSampleCountFlagBits" };
return sizeTypes.contains( params[vpi.second.lenParam].type.type );
} );
}
void VulkanHppGenerator::appendCppModuleCommands( std::vector<RequireData> const & requireData,
std::set<std::string> & listedCommands,
std::string const & title,
std::string & commandMembers ) const
{
std::string members;
forEachRequiredCommand( requireData,
[&]( NameLine const & command, auto const & )
{
if ( listedCommands.insert( command.name ).second )
{
members += "using ::PFN_" + command.name + ";\n";
}
} );
commandMembers += addTitleAndProtection( title, members );
}
void VulkanHppGenerator::checkAttributes( int line,
std::map<std::string, std::string> const & attributes,
std::map<std::string, std::set<std::string>> const & required,
std::map<std::string, std::set<std::string>> const & optional ) const
{
::checkAttributes( "VulkanHppGenerator", line, attributes, required, optional );
}
void VulkanHppGenerator::checkBitmaskCorrectness() const
{
for ( auto const & bitmask : m_bitmasks )
{
// check that a bitmask is required somewhere
// I think, it's not forbidden to not reference a bitmask, but it would probably be not intended?
auto typeIt = m_types.find( bitmask.first );
assert( typeIt != m_types.end() );
checkForError( !typeIt->second.requiredBy.empty(), bitmask.second.xmlLine, "bitmask <" + bitmask.first + "> not required in any feature or extension" );
// check that the requirement is an enum
if ( !bitmask.second.require.empty() )
{
auto requireTypeIt = m_types.find( bitmask.second.require );
checkForError(
requireTypeIt != m_types.end(), bitmask.second.xmlLine, "bitmask <" + bitmask.first + "> requires unknown type <" + bitmask.second.require + ">" );
checkForError( requireTypeIt->second.category == TypeCategory::Enum,
bitmask.second.xmlLine,
"bitmask <" + bitmask.first + "> requires non-enum type <" + bitmask.second.require + ">" );
assert( m_enums.contains( bitmask.second.require ) );
checkForError( !requireTypeIt->second.requiredBy.empty(),
bitmask.second.xmlLine,
"bitmask <" + bitmask.first + "> requires <" + bitmask.second.require + "> which is not required by any feature or extension!" );
}
}
}
void VulkanHppGenerator::checkCommandCorrectness() const
{
// prepare command checks by gathering all result codes (including aliases and not supported ones!) into one set of resultCodes
std::set<std::string> resultCodes = gatherResultCodes();
// command checks
for ( auto const & command : m_commands )
{
// check that a command is referenced somewhere
// I think, it's not forbidden to not reference a function, but it would probably be not intended?
checkForError( !command.second.requiredBy.empty(), command.second.xmlLine, "command <" + command.first + "> not required in any feature or extension" );
// check for unknown error or succes codes
for ( auto const & ec : command.second.errorCodes )
{
checkForError( resultCodes.contains( ec ), command.second.xmlLine, "command uses unknown error code <" + ec + ">" );
}
for ( auto const & sc : command.second.successCodes )
{
checkForError( resultCodes.contains( sc ), command.second.xmlLine, "command uses unknown success code <" + sc + ">" );
}
// check that functions returning a VkResult specify successcodes
if ( ( command.second.returnType.type == "VkResult" ) && command.second.successCodes.empty() )
{
// emit an error if this function is required in at least one supported feature or extension
// disabled or not supported features/extensions are still listed in requiredBy, but not in m_features/m_extensions
bool functionUsed = false;
for ( auto const & require : command.second.requiredBy )
{
functionUsed |= containsByName( m_features, require ) || containsByName( m_extensions, require );
}
if ( functionUsed )
{
checkForError( false, command.second.xmlLine, "missing successcodes on command <" + command.first + "> returning VkResult!" );
}
}
// check that all parameter types as well as the return type are known types
for ( auto const & p : command.second.params )
{
checkForError( m_types.contains( p.type.type ), p.xmlLine, "comand uses parameter of unknown type <" + p.type.type + ">" );
}
checkForError(
m_types.contains( command.second.returnType.type ), command.second.xmlLine, "command uses unknown return type <" + command.second.returnType.type + ">" );
}
}
void VulkanHppGenerator::checkCorrectness() const
{
checkForError( !m_vulkanLicenseHeader.empty(), -1, "missing license header" );
checkBitmaskCorrectness();
checkCommandCorrectness();
checkDefineCorrectness();
checkEnumCorrectness();
checkExtensionCorrectness();
checkFuncPointerCorrectness();
checkHandleCorrectness();
checkRequireCorrectness();
checkSpirVCapabilityCorrectness();
checkStructCorrectness();
checkSyncAccessCorrectness();
checkSyncStageCorrectness();
}
void VulkanHppGenerator::checkDefineCorrectness() const
{
// check that any requirements of a define is known
for ( auto const & d : m_defines )
{
checkForError( d.second.require.empty() || m_types.contains( d.second.require ),
d.second.xmlLine,
"define <" + d.first + "> uses unknown require <" + d.second.require + ">" );
}
}
void VulkanHppGenerator::checkElements( int line,
std::vector<tinyxml2::XMLElement const *> const & elements,
std::map<std::string, MultipleAllowed> const & required,
std::map<std::string, MultipleAllowed> const & optional ) const
{
::checkElements( "VulkanHppGenerator", line, elements, required, optional );
}
void VulkanHppGenerator::checkEnumCorrectness() const
{
for ( auto const & e : m_enums )
{
// check that a bitmask is required somewhere
// some bitmasks are never required, so make this a warning only
auto typeIt = m_types.find( e.first );
assert( typeIt != m_types.end() );
checkForWarning( !typeIt->second.requiredBy.empty(), e.second.xmlLine, "enum <" + e.first + "> not required in any feature or extension" );
if ( e.second.isBitmask )
{
if ( std::ranges::any_of( e.second.values, []( auto const & v ) { return v.supported; } ) )
{
// check that any enum of a bitmask with supported values is listed as "require" or "bitvalues" for a bitmask
auto bitmaskIt = std::ranges::find_if( m_bitmasks, [&e]( auto const & bitmask ) { return bitmask.second.require == e.first; } );
checkForError( bitmaskIt != m_bitmasks.end(),
e.second.xmlLine,
"enum <" + e.first + "> is not listed as an requires or bitvalues for any bitmask in the types section" );
// check that bitwidth of the enum and type of the corresponding bitmask are equal
checkForError( ( e.second.bitwidth != "64" ) || ( bitmaskIt->second.type == "VkFlags64" ),
e.second.xmlLine,
"enum <" + e.first + "> is marked with bitwidth <64> but corresponding bitmask <" + bitmaskIt->first + "> is not of type <VkFlags64>" );
}
}
}
// special check for VkFormat
if ( !m_formats.empty() )
{
auto enumIt = m_enums.find( "VkFormat" );
assert( enumIt != m_enums.end() );
assert( enumIt->second.values.front().name == "VK_FORMAT_UNDEFINED" );
for ( auto enumValueIt = std::next( enumIt->second.values.begin() ); enumValueIt != enumIt->second.values.end(); ++enumValueIt )
{
checkForError( !enumValueIt->supported || m_formats.contains( enumValueIt->name ),
enumValueIt->xmlLine,
"missing format specification for <" + enumValueIt->name + ">" );
}
}
}
bool VulkanHppGenerator::checkEquivalentSingularConstructor( std::vector<std::map<std::string, CommandData>::const_iterator> const & constructorIts,
std::map<std::string, CommandData>::const_iterator constructorIt,
std::vector<ParamData>::const_iterator lenIt ) const
{
// check, if there is no singular constructor with the very same arguments as this array constructor
// (besides the size, of course)
auto isEquivalentSingularConstructor = [constructorIt, lenIt]( std::map<std::string, CommandData>::const_iterator it )
{
if ( it->second.params.size() + 1 != constructorIt->second.params.size() )
{
return false;
}
size_t const lenIdx = std::distance( constructorIt->second.params.begin(), lenIt );
for ( size_t i = 0, j = 0; i < it->second.params.size(); ++i, ++j )
{
assert( j < constructorIt->second.params.size() );
if ( j == lenIdx )
{
++j;
}
if ( it->second.params[i].type.type != constructorIt->second.params[j].type.type )
{
return false;
}
}
return true;
};
return ( std::ranges::any_of( constructorIts, isEquivalentSingularConstructor ) );
}
void VulkanHppGenerator::checkExtensionCorrectness() const
{
for ( auto const & extension : m_extensions )
{
// check for existence of any depends, deprecation, obsoletion, or promotion
for ( auto const & dep : extension.depends )
{
checkForError( isFeature( dep.first ), extension.xmlLine, "extension <" + extension.name + "> lists an unknown feature dependency <" + dep.first + ">" );
for ( auto depsIt = dep.second.begin(); depsIt != dep.second.end(); ++depsIt )
{
for ( auto const & d : *depsIt )
{
checkForError( isExtension( d ), extension.xmlLine, "extension <" + extension.name + "> lists an unknown extension dependency <" + d + ">" );
}
checkForError( std::none_of( std::next( depsIt ), dep.second.end(), [&depsIt]( auto const & d ) { return *depsIt == d; } ),
extension.xmlLine,
"extension <" + extension.name + "> lists multiple identical dependencies" );
}
}
checkForError( extension.deprecatedBy.empty() || isFeature( extension.deprecatedBy ) || isExtension( extension.deprecatedBy ),
extension.xmlLine,
"extension <" + extension.name + "> is deprecated by unknown extension/version <" + extension.deprecatedBy + ">" );
checkForError( extension.obsoletedBy.empty() || isFeature( extension.obsoletedBy ) || isExtension( extension.obsoletedBy ),
extension.xmlLine,
"extension <" + extension.name + "> is obsoleted by unknown extension/version <" + extension.obsoletedBy + ">" );
checkForError( extension.promotedTo.empty() || isFeature( extension.promotedTo ) || isExtension( extension.promotedTo ),
extension.xmlLine,
"extension <" + extension.name + "> is promoted to unknown extension/version <" + extension.promotedTo + ">" );
// check for existence of any requirement
for ( auto const & require : extension.requireData )
{
std::vector<std::string> requireDepends = tokenizeAny( require.depends, ",+()" );
for ( auto const & dep : requireDepends )
{
checkForError( isFeature( dep ) || isExtension( dep ), require.xmlLine, "extension <" + extension.name + "> lists an unknown depends <" + dep + ">" );
}
}
}
}
inline void VulkanHppGenerator::checkForError( bool condition, int line, std::string const & message ) const
{
::checkForError( "VulkanHppGenerator", condition, line, message );
}
void VulkanHppGenerator::checkForWarning( bool condition, int line, std::string const & message ) const
{
::checkForWarning( "VulkanHppGenerator", condition, line, message );
}
void VulkanHppGenerator::checkFuncPointerCorrectness() const
{
for ( auto const & funcPointer : m_funcPointers )
{
checkForError( funcPointer.second.require.empty() || m_types.contains( funcPointer.second.require ),
funcPointer.second.xmlLine,
"funcpointer requires unknown <" + funcPointer.second.require + ">" );
for ( auto const & param : funcPointer.second.params )
{
checkForError( m_types.contains( param.type.type ), param.xmlLine, "funcpointer param of unknown type <" + param.type.type + ">" );
}
}
}
void VulkanHppGenerator::checkHandleCorrectness() const
{
// prepare handle checks by getting the VkObjectType enum
auto objectTypeIt = m_enums.find( "VkObjectType" );
assert( objectTypeIt != m_enums.end() );
// handle checks
assert( m_handles.begin()->first.empty() );
for ( auto handleIt = std::next( m_handles.begin() ); handleIt != m_handles.end(); ++handleIt )
{
assert( !handleIt->first.empty() );
// check the existence of the parent
checkForError( m_handles.contains( handleIt->second.parent ),
handleIt->second.xmlLine,
"handle <" + handleIt->first + "> with unknown parent <" + handleIt->second.parent + ">" );
// check existence of objTypeEnum used with this handle type
checkForError(
!handleIt->second.objTypeEnum.empty(), handleIt->second.xmlLine, "handle <" + handleIt->first + "> missing required \"objtypeenum\" attribute" );
checkForError( !isTypeUsed( handleIt->first ) || containsByNameOrAlias( objectTypeIt->second.values, handleIt->second.objTypeEnum ),
handleIt->second.xmlLine,
"handle <" + handleIt->first + "> specifies unknown \"objtypeenum\" <" + handleIt->second.objTypeEnum + ">" );
}
// check that all specified objectType values are used with a handle type
for ( auto const & objectTypeValue : objectTypeIt->second.values )
{
checkForError( ( objectTypeValue.name == "VK_OBJECT_TYPE_UNKNOWN" ) ||
std::ranges::any_of( m_handles,
[&objectTypeValue]( std::pair<std::string, HandleData> const & hd )
{ return hd.second.objTypeEnum == objectTypeValue.name; } ),
objectTypeValue.xmlLine,
"VkObjectType value <" + objectTypeValue.name + "> not specified as \"objtypeenum\" for any handle" );
}
}
void VulkanHppGenerator::checkRequireCorrectness() const
{
// checks by features and extensions
for ( auto & feature : m_features )
{
checkRequireCorrectness( feature.requireData, "feature", feature.name );
}
for ( auto & extension : m_extensions )
{
checkRequireCorrectness( extension.requireData, "extension", extension.name );
}
}
void VulkanHppGenerator::checkRequireCorrectness( std::vector<RequireData> const & requireData, std::string const & section, std::string const & name ) const
{
for ( auto const & require : requireData )
{
checkRequireDependenciesCorrectness( require, section, name );
checkRequireTypesCorrectness( require );
}
}
void VulkanHppGenerator::checkRequireDependenciesCorrectness( RequireData const & require, std::string const & section, std::string const & name ) const
{
std::vector<std::string> dependencies = tokenizeAny( require.depends, ",+()" );
for ( auto const & depends : dependencies )
{
size_t separatorPos = depends.find( "::" );
if ( separatorPos == std::string::npos )
{
checkForError( isFeature( depends ) || isExtension( depends ),
require.xmlLine,
section + " <" + name + "> depends on unknown extension or feature <" + depends + ">" );
}
else
{
std::string structure = depends.substr( 0, separatorPos );
std::string member = depends.substr( separatorPos + 2 );
auto structIt = m_structs.find( structure );
checkForError( structIt != m_structs.end(), require.xmlLine, section + " <" + name + "> requires member of an unknown struct <" + structure + ">" );
checkForError( std::ranges::any_of( structIt->second.members, [&member]( auto const & md ) { return md.name == member; } ),
require.xmlLine,
section + " <" + name + "> requires unknown member <" + member + "> as part of the struct <" + structure + ">" );
}
}
}
void VulkanHppGenerator::checkRequireTypesCorrectness( RequireData const & require ) const
{
for ( auto const & type : require.types )
{
auto typeIt = m_types.find( type.name );
assert( typeIt != m_types.end() );
// every required type should be listed in the corresponding map
switch ( typeIt->second.category )
{
case TypeCategory::Bitmask:
checkForError( findByNameOrAlias( m_bitmasks, type.name ) != m_bitmasks.end(),
typeIt->second.xmlLine,
"required bitmask type <" + type.name + "> is not listed as bitmask" );
break;
case TypeCategory::BaseType:
checkForError( m_baseTypes.contains( type.name ), typeIt->second.xmlLine, "required base type <" + type.name + "> is not listed as a base type" );
break;
case TypeCategory::Constant:
checkForError( m_constants.contains( type.name ), typeIt->second.xmlLine, "required constant <" + type.name + "> is not listed as a constant" );
break;
case TypeCategory::Define:
checkForError( m_defines.contains( type.name ), typeIt->second.xmlLine, "required define <" + type.name + "> is not listed as a define" );
break;
case TypeCategory::Enum:
checkForError(
findByNameOrAlias( m_enums, type.name ) != m_enums.end(), typeIt->second.xmlLine, "required enum type <" + type.name + "> is not listed as an enum" );
break;
case TypeCategory::ExternalType:
checkForError(
m_externalTypes.contains( type.name ), typeIt->second.xmlLine, "required external type <" + type.name + "> is not listed as an external type" );
break;
case TypeCategory::FuncPointer:
checkForError(
m_funcPointers.contains( type.name ), typeIt->second.xmlLine, "required funcpointer <" + type.name + "> is not listed as a funcpointer" );
break;
case TypeCategory::Handle:
checkForError( findByNameOrAlias( m_handles, type.name ) != m_handles.end(),
typeIt->second.xmlLine,
"required handle type <" + type.name + "> is not listed as a handle" );
break;
case TypeCategory::Include:
checkForError( m_includes.contains( type.name ), typeIt->second.xmlLine, "required include <" + type.name + "> is not listed as an include" );
break;
case TypeCategory::Struct:
case TypeCategory::Union:
checkForError( findByNameOrAlias( m_structs, type.name ) != m_structs.end(),
typeIt->second.xmlLine,
"required struct type <" + type.name + "> is not listed as a struct" );
break;
case TypeCategory::Unknown: break;
default : assert( false ); break;
}
}
}
void VulkanHppGenerator::checkSpirVCapabilityCorrectness() const
{
for ( auto const & capability : m_spirVCapabilities )
{
for ( auto const & enable : capability.second.structs )
{
assert( !enable.second.empty() );
auto structIt = findByNameOrAlias( m_structs, enable.first );
checkForError( structIt != m_structs.end(),
enable.second.begin()->second,
"unknown structure <" + enable.first + "> specified for SPIR-V capability <" + capability.first + ">" );
for ( auto const & member : enable.second )
{
checkForError( std::ranges::any_of( structIt->second.members, [&member]( auto const & md ) { return md.name == member.first; } ),
member.second,
"unknown member <" + member.first + "> in struct <" + enable.first + "> specified for SPIR-V capability <" + capability.first + ">" );
}
}
}
}
void VulkanHppGenerator::checkStructCorrectness() const
{
std::set<std::string> sTypeValues;
for ( auto const & structure : m_structs )
{
// check that a struct is referenced somewhere
// I think, it's not forbidden to not reference a struct, but it would probably be not intended?
auto typeIt = m_types.find( structure.first );
assert( typeIt != m_types.end() );
checkForError(
!typeIt->second.requiredBy.empty(), structure.second.xmlLine, "structure <" + structure.first + "> not required by any feature or extension" );
// check for existence of all structs that are extended by this struct
for ( auto const & extend : structure.second.structExtends )
{
checkForError( findByNameOrAlias( m_structs, extend ) != m_structs.end(),
structure.second.xmlLine,
"struct <" + structure.first + "> extends unknown <" + extend + ">" );
}
// checks on the members of a struct
checkStructMemberCorrectness( structure.first, structure.second.members, sTypeValues );
}
// enum VkStructureType checks (need to be after structure checks because of sTypeValues gathered there)
auto structureTypeIt = m_enums.find( "VkStructureType" );
assert( structureTypeIt != m_enums.end() );
static std::set<std::string> reservedValues = { "VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO",
"VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO",
"VK_STRUCTURE_TYPE_PRIVATE_VENDOR_INFO_PLACEHOLDER_OFFSET_0_NV" };
for ( auto const & enumValue : structureTypeIt->second.values )
{
if ( reservedValues.contains( enumValue.name ) )
{
checkForError( !sTypeValues.contains( enumValue.name ), enumValue.xmlLine, "Reserved VkStructureType enum value <" + enumValue.name + "> is used" );
}
else
{
checkForError( !enumValue.supported || ( sTypeValues.erase( enumValue.name ) == 1 ),
enumValue.xmlLine,
"VkStructureType enum value <" + enumValue.name + "> never used" );
}
}
assert( sTypeValues.empty() );
}
void VulkanHppGenerator::checkStructMemberArraySizesAreValid( std::vector<std::string> const & arraySizes, int line ) const
{
// check that any array size is either a number or a known constant
for ( auto const & arraySize : arraySizes )
{
if ( !isNumber( arraySize ) && !m_constants.contains( arraySize ) )
{
auto typeIt = m_types.find( arraySize );
checkForError( ( typeIt != m_types.end() ) && isUpperCase( arraySize ) && ( typeIt->second.category == TypeCategory::ExternalType ),
line,
"struct member array size uses unknown constant <" + arraySize + ">" );
}
}
}
void VulkanHppGenerator::checkStructMemberCorrectness( std::string const & structureName,
std::vector<MemberData> const & members,
std::set<std::string> & sTypeValues ) const
{
// determine if this struct is requird/used
bool const structUsed = isTypeUsed( structureName );
for ( auto const & member : members )
{
checkStructMemberArraySizesAreValid( member.arraySizes, member.xmlLine );
checkStructMemberSelectorConnection( member.selector, members, member.type.type );
checkStructMemberTypeIsKnown( member.type.type, member.xmlLine );
checkStructMemberTypeIsRequired( member.type.type, member.xmlLine, structureName );
checkStructMemberValueIsValid( member.value, member.type.type, member.name, member.xmlLine, structUsed, structureName, sTypeValues );
}
}
void VulkanHppGenerator::checkStructMemberSelectorConnection( std::string const & selector,
std::vector<MemberData> const & members,
std::string const & memberType ) const
{
// if a member specifies a selector, that member is a union and the selector is an enum
// check that there's a 1-1 connection between the specified selections and the values of that enum
if ( !selector.empty() )
{
auto selectorIt = findByName( members, selector );
assert( selectorIt != members.end() );
auto selectorEnumIt = findByNameOrAlias( m_enums, selectorIt->type.type );
assert( selectorEnumIt != m_enums.end() );
auto unionIt = m_structs.find( memberType );
assert( ( unionIt != m_structs.end() ) && unionIt->second.isUnion );
for ( auto const & unionMember : unionIt->second.members )
{
// check that each union member has a selection, that is a value of the seleting enum
assert( !unionMember.selection.empty() );
for ( auto const & selection : unionMember.selection )
{
checkForError(
containsByNameOrAlias( selectorEnumIt->second.values, selection ),
unionMember.xmlLine,
"union member <" + unionMember.name + "> uses selection <" + selection + "> that is not part of the selector type <" + selectorIt->type.type + ">" );
}
}
}
}
void VulkanHppGenerator::checkStructMemberTypeIsKnown( std::string const & memberType, int line ) const
{
// check that the member type is known
checkForError( m_types.contains( memberType ), line, "struct member uses unknown type <" + memberType + ">" );
}
void VulkanHppGenerator::checkStructMemberTypeIsRequired( std::string const & memberType, int line, std::string const & structureName ) const
{
// check that the member type is required in some feature or extension
if ( memberType.starts_with( "Vk" ) )