forked from KhronosGroup/Vulkan-Hpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VulkanHppGenerator.cpp
11490 lines (10410 loc) · 456 KB
/
VulkanHppGenerator.cpp
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 <algorithm>
#include <cassert>
#include <exception>
#include <fstream>
#include <functional>
#include <iterator>
#include <regex>
void appendArgumentCount( std::string & str,
size_t vectorIndex,
std::string const & vectorName,
size_t templateParamIndex );
void appendReinterpretCast( std::string & str, bool leadingConst, std::string const & type );
void appendTypesafeStuff( std::string & str, std::string const & typesafeCheck );
void appendVersionCheck( std::string & str, std::string const & version );
bool beginsWith( std::string const & text, std::string const & prefix );
bool endsWith( std::string const & text, std::string const & postfix );
void check( bool condition, int line, std::string const & message );
void 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 );
void checkElements( int line,
std::vector<tinyxml2::XMLElement const *> const & elements,
std::map<std::string, bool> const & required,
std::set<std::string> const & optional = {} );
std::string constructStandardArray( std::string const & type, std::vector<std::string> const & sizes );
std::string createEnumValueName( std::string const & name,
std::string const & prefix,
std::string const & postfix,
bool bitmask,
std::string const & tag );
std::string createSuccessCode( std::string const & code, std::set<std::string> const & tags );
std::string determineCommandName( std::string const & vulkanCommandName, std::string const & firstArgumentType );
std::string determineNoDiscard( bool multiSuccessCodes, bool multiErrorCodes );
std::set<size_t> determineSkippedParams( size_t returnParamIndex, std::map<size_t, size_t> const & vectorParamIndices );
std::string extractTag( int line, std::string const & name, std::set<std::string> const & tags );
std::string findTag( std::set<std::string> const & tags, std::string const & name, std::string const & postfix = "" );
std::map<std::string, std::string> getAttributes( tinyxml2::XMLElement const * element );
template <typename ElementContainer>
std::vector<tinyxml2::XMLElement const *> getChildElements( ElementContainer const * element );
std::string getEnumPostfix( std::string const & name, std::set<std::string> const & tags, std::string & prefix );
std::string readTypePostfix( tinyxml2::XMLNode const * node );
std::string readTypePrefix( tinyxml2::XMLNode const * node );
std::string replaceWithMap( std::string const & input, std::map<std::string, std::string> replacements );
std::string startLowerCase( std::string const & input );
std::string startUpperCase( std::string const & input );
std::string stripPostfix( std::string const & value, std::string const & postfix );
std::string stripPluralS( std::string const & name );
std::string stripPrefix( std::string const & value, std::string const & prefix );
std::string toCamelCase( std::string const & value );
std::string toString( std::vector<std::string> const & strings );
std::string toUpperCase( std::string const & name );
std::vector<std::string> tokenize( std::string const & tokenString, std::string const & separator );
std::string trim( std::string const & input );
std::string trimEnd( std::string const & input );
std::string trimStars( std::string const & input );
void warn( bool condition, int line, std::string const & message );
const std::set<std::string> ignoreLens = { "null-terminated",
R"(latexmath:[\lceil{\mathit{rasterizationSamples} \over 32}\rceil])",
"2*VK_UUID_SIZE",
"2*ename:VK_UUID_SIZE" };
const std::set<std::string> specialPointerTypes = { "Display", "IDirectFB", "wl_display", "xcb_connection_t" };
void appendArgumentCount( std::string & str,
size_t vectorIndex,
std::string const & vectorName,
size_t templateParamIndex )
{
// this parameter is a count parameter for a vector parameter
// the corresponding vector parameter is not the return parameter, or it's not a two-step algorithm
// for the non-singular version, the count is the size of the vector parameter
// -> use the vector parameter name without leading 'p' to get the size (in number of elements, not in bytes)
assert( vectorName[0] == 'p' );
str += startLowerCase( stripPrefix( vectorName, "p" ) ) + ".size() ";
if ( templateParamIndex == vectorIndex )
{
// if the vector parameter is templatized -> multiply by the size of that type to get the size in bytes
str += "* sizeof( T ) ";
}
}
void appendReinterpretCast( std::string & str, bool leadingConst, std::string const & type )
{
str += "reinterpret_cast<";
if ( leadingConst )
{
str += "const ";
}
str += type + "*>";
}
void appendTypesafeStuff( std::string & str, std::string const & typesafeCheck )
{
str +=
"// 32-bit vulkan is not typesafe for handles, so don't allow copy constructors on this platform by default.\n"
"// To enable this feature on 32-bit platforms please define VULKAN_HPP_TYPESAFE_CONVERSION\n" +
typesafeCheck +
"\n"
"# if !defined( VULKAN_HPP_TYPESAFE_CONVERSION )\n"
"# define VULKAN_HPP_TYPESAFE_CONVERSION\n"
"# endif\n"
"#endif\n";
}
void appendVersionCheck( std::string & str, std::string const & version )
{
str += "static_assert( VK_HEADER_VERSION == " + version +
" , \"Wrong VK_HEADER_VERSION!\" );\n"
"\n";
}
bool beginsWith( std::string const & text, std::string const & prefix )
{
return prefix.empty() || text.substr( 0, prefix.length() ) == prefix;
}
bool endsWith( std::string const & text, std::string const & postfix )
{
return postfix.empty() ||
( ( postfix.length() <= text.length() ) && ( text.substr( text.length() - postfix.length() ) == postfix ) );
}
void check( bool condition, int line, std::string const & message )
{
if ( !condition )
{
throw std::runtime_error( "Spec error on line " + std::to_string( line ) + ": " + message );
}
}
// check the validity of an attributes map
// line : the line in the xml file where the attributes are listed
// attributes : the map of name/value pairs of the encountered attributes
// required : the required attributes, with a set of allowed values per attribute
// optional : the optional attributes, with a set of allowed values per attribute
void 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 )
{
// check if all required attributes are included and if there is a set of allowed values, check if the actual value is
// part of that set
for ( auto const & r : required )
{
auto attributesIt = attributes.find( r.first );
check( attributesIt != attributes.end(), line, "missing attribute <" + r.first + ">" );
check( r.second.empty() || ( r.second.find( attributesIt->second ) != r.second.end() ),
line,
"unexpected attribute value <" + attributesIt->second + "> in attribute <" + r.first + ">" );
}
// check if all not required attributes or optional, and if there is a set of allowed values, check if the actual
// value is part of that set
for ( auto const & a : attributes )
{
if ( required.find( a.first ) == required.end() )
{
auto optionalIt = optional.find( a.first );
if ( optionalIt == optional.end() )
{
warn( false, line, "unknown attribute <" + a.first + ">" );
continue;
}
if ( !optionalIt->second.empty() )
{
std::vector<std::string> values = tokenize( a.second, "," );
for ( auto const & v : values )
{
warn( optionalIt->second.find( v ) != optionalIt->second.end(),
line,
"unexpected attribute value <" + v + "> in attribute <" + a.first + ">" );
}
}
}
}
}
void checkedAssignment( std::string & str,
std::string const & leave,
std::string const & commandName,
std::string aliasName )
{
str.erase( str.length() - leave.length() );
str += " if ( !" + commandName + " ) " + commandName + " = " + aliasName + ";\n" + leave;
}
void checkElements( int line,
std::vector<tinyxml2::XMLElement const *> const & elements,
std::map<std::string, bool> const & required,
std::set<std::string> const & optional )
{
std::map<std::string, size_t> encountered;
for ( auto const & e : elements )
{
std::string value = e->Value();
encountered[value]++;
warn( ( required.find( value ) != required.end() ) || ( optional.find( value ) != optional.end() ),
e->GetLineNum(),
"unknown element <" + value + ">" );
}
for ( auto const & r : required )
{
auto encounteredIt = encountered.find( r.first );
check( encounteredIt != encountered.end(), line, "missing required element <" + r.first + ">" );
// check: r.second (means: required excactly once) => (encouteredIt->second == 1)
check( !r.second || ( encounteredIt->second == 1 ),
line,
"required element <" + r.first + "> is supposed to be listed exactly once, but is listed " +
std::to_string( encounteredIt->second ) );
}
}
std::string constructCArraySizes( std::vector<std::string> const & sizes )
{
std::string arraySizes;
for ( auto const & s : sizes )
{
arraySizes += "[" + s + "]";
}
return arraySizes;
}
std::string constructStandardArray( std::string const & type, std::vector<std::string> const & sizes )
{
std::string arrayString = "std::array<" + type + "," + sizes.back() + ">";
for ( size_t i = sizes.size() - 2; i < sizes.size(); i-- )
{
arrayString = "std::array<" + arrayString + "," + sizes[i] + ">";
}
return arrayString;
}
std::string constructStandardArrayWrapper( std::string const & type, std::vector<std::string> const & sizes )
{
std::string arrayString = "VULKAN_HPP_NAMESPACE::ArrayWrapper" + std::to_string( sizes.size() ) + "D<" + type;
for ( auto const & size : sizes )
{
arrayString += ", " + size;
}
arrayString += ">";
return arrayString;
}
std::string createEnumValueName( std::string const & name,
std::string const & prefix,
std::string const & postfix,
bool bitmask,
std::string const & tag )
{
std::string result = "e" + toCamelCase( stripPostfix( stripPrefix( name, prefix ), postfix ) );
if ( bitmask )
{
size_t pos = result.find( "Bit" );
if ( pos != std::string::npos )
{
result.erase( pos, 3 );
}
}
if ( !tag.empty() && ( result.substr( result.length() - tag.length() ) == toCamelCase( tag ) ) )
{
result = result.substr( 0, result.length() - tag.length() ) + tag;
}
return result;
}
std::string createSuccessCode( std::string const & code, std::set<std::string> const & tags )
{
std::string tag = findTag( tags, code );
// on each success code: prepend 'e', strip "VK_" and a tag, convert it to camel case, and add the tag again
return "e" + toCamelCase( stripPostfix( stripPrefix( code, "VK_" ), tag ) ) + tag;
}
std::string determineCommandName( std::string const & vulkanCommandName, std::string const & firstArgumentType )
{
std::string commandName( startLowerCase( stripPrefix( vulkanCommandName, "vk" ) ) );
std::string searchName = stripPrefix( firstArgumentType, "Vk" );
size_t pos = commandName.find( searchName );
if ( ( pos == std::string::npos ) && isupper( searchName[0] ) )
{
searchName[0] = static_cast<char>( tolower( searchName[0] ) );
pos = commandName.find( searchName );
}
if ( pos != std::string::npos )
{
commandName.erase( pos, searchName.length() );
}
else if ( ( searchName == "commandBuffer" ) && beginsWith( commandName, "cmd" ) )
{
commandName.erase( 0, 3 );
pos = 0;
}
if ( ( pos == 0 ) && isupper( commandName[0] ) )
{
commandName[0] = static_cast<char>( tolower( commandName[0] ) );
}
return commandName;
}
std::string determineNoDiscard( bool multiSuccessCodes, bool multiErrorCodes )
{
return ( multiSuccessCodes ) ? "VULKAN_HPP_NODISCARD "
: ( multiErrorCodes ? "VULKAN_HPP_NODISCARD_WHEN_NO_EXCEPTIONS " : "" );
}
std::set<size_t> determineSkippedParams( size_t returnParamIndex, std::map<size_t, size_t> const & vectorParamIndices )
{
std::set<size_t> skippedParams;
// the size-parameters of vector parameters are not explicitly used in the enhanced API
std::for_each(
vectorParamIndices.begin(), vectorParamIndices.end(), [&skippedParams]( std::pair<size_t, size_t> const & vp ) {
if ( vp.second != INVALID_INDEX )
{
skippedParams.insert( vp.second );
}
} );
// and the return parameter is also skipped
if ( returnParamIndex != INVALID_INDEX )
{
skippedParams.insert( returnParamIndex );
}
return skippedParams;
}
std::string findTag( std::set<std::string> const & tags, std::string const & name, std::string const & postfix )
{
auto tagIt = std::find_if(
tags.begin(), tags.end(), [&name, &postfix]( std::string const & t ) { return endsWith( name, t + postfix ); } );
return ( tagIt != tags.end() ) ? *tagIt : "";
}
std::map<std::string, std::string> getAttributes( tinyxml2::XMLElement const * element )
{
std::map<std::string, std::string> attributes;
for ( auto attribute = element->FirstAttribute(); attribute; attribute = attribute->Next() )
{
assert( attributes.find( attribute->Name() ) == attributes.end() );
attributes[attribute->Name()] = attribute->Value();
}
return attributes;
}
template <typename ElementContainer>
std::vector<tinyxml2::XMLElement const *> getChildElements( ElementContainer const * element )
{
std::vector<tinyxml2::XMLElement const *> childElements;
for ( tinyxml2::XMLElement const * childElement = element->FirstChildElement(); childElement;
childElement = childElement->NextSiblingElement() )
{
childElements.push_back( childElement );
}
return childElements;
}
std::string getEnumPostfix( std::string const & name, std::set<std::string> const & tags, std::string & prefix )
{
std::string postfix;
if ( name != "VkResult" )
{
// if the enum name contains a tag move it from the prefix to the postfix to generate correct enum value names.
for ( auto const & tag : tags )
{
if ( endsWith( prefix, tag + "_" ) )
{
prefix.erase( prefix.length() - tag.length() - 1 );
postfix = "_" + tag;
break;
}
else if ( endsWith( name, tag ) )
{
postfix = "_" + tag;
break;
}
}
}
return postfix;
}
std::string getEnumPrefix( int line, std::string const & name, bool bitmask )
{
std::string prefix;
if ( name == "VkResult" )
{
prefix = "VK_";
}
else if ( bitmask )
{
// for a bitmask enum, start with "VK", cut off the trailing "FlagBits", and convert that name to upper case
// end that with "Bit"
size_t pos = name.find( "FlagBits" );
check( pos != std::string::npos, line, "bitmask <" + name + "> does not contain <FlagBits>" );
prefix = toUpperCase( name.substr( 0, pos ) ) + "_";
}
else
{
// for a non-bitmask enum, convert the name to upper case
prefix = toUpperCase( name ) + "_";
}
return prefix;
}
std::string extractTag( int line, std::string const & name, std::set<std::string> const & tags )
{
// extract the tag from the name, which is supposed to look like VK_<tag>_<other>
size_t tagStart = name.find( '_' );
check( tagStart != std::string::npos, line, "name <" + name + "> is missing an underscore '_'" );
size_t tagEnd = name.find( '_', tagStart + 1 );
check( tagEnd != std::string::npos, line, "name <" + name + "> is missing an underscore '_'" );
std::string tag = name.substr( tagStart + 1, tagEnd - tagStart - 1 );
check( tags.find( tag ) != tags.end(), line, "name <" + name + "> is using an unknown tag <" + tag + ">" );
return tag;
}
std::pair<std::vector<std::string>, std::string> readModifiers( tinyxml2::XMLNode const * node )
{
std::vector<std::string> arraySizes;
std::string bitCount;
if ( node && node->ToText() )
{
// following the name there might be some array size
std::string value = node->Value();
assert( !value.empty() );
if ( value[0] == '[' )
{
std::string::size_type endPos = 0;
while ( endPos + 1 != value.length() )
{
std::string::size_type startPos = value.find( '[', endPos );
check( startPos != std::string::npos, node->GetLineNum(), "could not find '[' in <" + value + ">" );
endPos = value.find( ']', startPos );
check( endPos != std::string::npos, node->GetLineNum(), "could not find ']' in <" + value + ">" );
check( startPos + 2 <= endPos, node->GetLineNum(), "missing content between '[' and ']' in <" + value + ">" );
arraySizes.push_back( value.substr( startPos + 1, endPos - startPos - 1 ) );
}
}
else if ( value[0] == ':' )
{
bitCount = value.substr( 1 );
}
else
{
check( ( value[0] == ';' ) || ( value[0] == ')' ), node->GetLineNum(), "unknown modifier <" + value + ">" );
}
}
return std::make_pair( arraySizes, bitCount );
;
}
std::string readTypePostfix( tinyxml2::XMLNode const * node )
{
std::string postfix;
if ( node && node->ToText() )
{
postfix = trimStars( trimEnd( node->Value() ) );
}
return postfix;
}
std::string readTypePrefix( tinyxml2::XMLNode const * node )
{
std::string prefix;
if ( node && node->ToText() )
{
prefix = trim( node->Value() );
}
return prefix;
}
std::string replaceWithMap( std::string const & input, std::map<std::string, std::string> replacements )
{
// This will match ${someVariable} and contain someVariable in match group 1
std::regex re( R"(\$\{([^\}]+)\})" );
auto it = std::sregex_iterator( input.begin(), input.end(), re );
auto end = std::sregex_iterator();
// No match, just return the original string
if ( it == end )
{
assert( replacements.empty() );
return input;
}
#if !defined( NDEBUG )
std::set<std::string> matchedReplacements;
#endif
std::string result = "";
while ( it != end )
{
std::smatch match = *it;
auto itReplacement = replacements.find( match[1].str() );
assert( itReplacement != replacements.end() );
#if !defined( NDEBUG )
matchedReplacements.insert( match[1].str() );
#endif
result +=
match.prefix().str() + ( ( itReplacement != replacements.end() ) ? itReplacement->second : match[0].str() );
++it;
// we've passed the last match. Append the rest of the orignal string
if ( it == end )
{
result += match.suffix().str();
}
}
#if !defined( NDEBUG )
std::set<std::string> missedReplacements;
for ( auto r : replacements )
{
if ( matchedReplacements.find( r.first ) == matchedReplacements.end() )
{
missedReplacements.insert( r.first );
}
}
assert( missedReplacements.empty() );
#endif
return result;
}
std::string startLowerCase( std::string const & input )
{
return input.empty() ? "" : static_cast<char>( tolower( input[0] ) ) + input.substr( 1 );
}
std::string startUpperCase( std::string const & input )
{
return input.empty() ? "" : static_cast<char>( toupper( input[0] ) ) + input.substr( 1 );
}
std::string stripPostfix( std::string const & value, std::string const & postfix )
{
std::string strippedValue = value;
if ( endsWith( strippedValue, postfix ) )
{
strippedValue.erase( strippedValue.length() - postfix.length() );
}
return strippedValue;
}
std::string stripPluralS( std::string const & name )
{
std::string strippedName( name );
size_t pos = strippedName.rfind( 's' );
if ( pos != std::string::npos )
{
if ( ( 2 <= pos ) && ( strippedName.substr( pos - 2, 3 ) == "ies" ) )
{
strippedName.replace( pos - 2, 3, "y" );
}
else
{
strippedName.erase( pos, 1 );
}
}
return strippedName;
}
std::string stripPrefix( std::string const & value, std::string const & prefix )
{
std::string strippedValue = value;
if ( beginsWith( strippedValue, prefix ) )
{
strippedValue.erase( 0, prefix.length() );
}
return strippedValue;
}
std::string toCamelCase( std::string const & value )
{
assert( !value.empty() && ( isupper( value[0] ) || isdigit( value[0] ) ) );
std::string result;
result.reserve( value.size() );
bool keepUpper = true;
for ( auto c : value )
{
if ( c == '_' )
{
keepUpper = true;
}
else if ( isdigit( c ) )
{
keepUpper = true;
result.push_back( c );
}
else if ( keepUpper )
{
result.push_back( c );
keepUpper = false;
}
else
{
result.push_back( static_cast<char>( tolower( c ) ) );
}
}
return result;
}
std::string toString( std::vector<std::string> const & strings )
{
std::string str;
if ( !strings.empty() )
{
str = strings[0];
for ( size_t i = 1; i < strings.size(); i++ )
{
str += ", " + strings[i];
}
}
return str;
}
std::string toUpperCase( std::string const & name )
{
std::string convertedName;
convertedName.reserve( name.size() );
bool lowerOrDigit = false;
for ( auto c : name )
{
if ( islower( c ) || isdigit( c ) )
{
lowerOrDigit = true;
}
else if ( lowerOrDigit )
{
convertedName.push_back( '_' );
lowerOrDigit = false;
}
convertedName.push_back( static_cast<char>( toupper( c ) ) );
}
return convertedName;
}
std::vector<std::string> tokenize( std::string const & tokenString, std::string const & separator )
{
std::vector<std::string> tokens;
size_t start = 0, end;
do
{
end = tokenString.find( separator, start );
if ( start != end )
{
tokens.push_back( trim( tokenString.substr( start, end - start ) ) );
}
start = end + separator.length();
} while ( end != std::string::npos );
return tokens;
}
std::string trim( std::string const & input )
{
std::string result = input;
result.erase( result.begin(),
std::find_if( result.begin(), result.end(), []( char c ) { return !std::isspace( c ); } ) );
result.erase( std::find_if( result.rbegin(), result.rend(), []( char c ) { return !std::isspace( c ); } ).base(),
result.end() );
return result;
}
std::string trimEnd( std::string const & input )
{
std::string result = input;
result.erase( std::find_if( result.rbegin(), result.rend(), []( char c ) { return !std::isspace( c ); } ).base(),
result.end() );
return result;
}
std::string trimStars( std::string const & input )
{
std::string result = input;
size_t pos = result.find( '*' );
while ( pos != std::string::npos )
{
if ( ( 0 < pos ) && ( result[pos - 1] != ' ' ) && ( result[pos - 1] != '*' ) )
{
result.insert( pos, 1, ' ' );
++pos;
}
else if ( ( pos < result.length() - 1 ) && ( result[pos + 1] != ' ' ) && ( result[pos + 1] != '*' ) )
{
result.insert( pos + 1, 1, ' ' );
}
pos = result.find( '*', pos + 1 );
}
return result;
}
void warn( bool condition, int line, std::string const & message )
{
if ( !condition )
{
std::cerr << "Spec warning on line " << std::to_string( line ) << " " << message << "!" << std::endl;
}
}
VulkanHppGenerator::VulkanHppGenerator( tinyxml2::XMLDocument const & document )
{
m_handles.insert( std::make_pair(
"", HandleData( {}, "", 0 ) ) ); // insert the default "handle" without class (for createInstance, and such)
int line = document.GetLineNum();
std::vector<tinyxml2::XMLElement const *> elements = getChildElements( &document );
checkElements( line, elements, { { "registry", true } } );
check( elements.size() == 1,
line,
"encountered " + std::to_string( elements.size() ) + " elements named <registry> but only one is allowed" );
readRegistry( elements[0] );
checkCorrectness();
}
void VulkanHppGenerator::appendArgumentPlainType( std::string & str, ParamData const & paramData ) const
{
// this parameter is just a plain type
if ( !paramData.type.postfix.empty() )
{
assert( paramData.type.postfix.back() == '*' );
// it's a pointer
std::string parameterName = startLowerCase( stripPrefix( paramData.name, "p" ) );
// it's a non-const pointer, and char is the only type that occurs -> use the address of the parameter
assert( paramData.type.type.find( "char" ) == std::string::npos );
str += "&" + parameterName;
}
else
{
// it's a plain parameter -> just use its name
str += paramData.name;
}
}
void VulkanHppGenerator::appendArguments( std::string & str,
CommandData const & commandData,
size_t returnParamIndex,
size_t templateParamIndex,
std::map<size_t, size_t> const & vectorParamIndices,
bool twoStep,
bool firstCall,
size_t from,
size_t to ) const
{
assert( from <= to );
bool encounteredArgument = false;
for ( size_t i = from; i < to; i++ )
{
if ( encounteredArgument )
{
str += ", ";
}
auto it = vectorParamIndices.find( i );
if ( it != vectorParamIndices.end() )
{
appendArgumentVector(
str, it->first, commandData.params[it->first], returnParamIndex, templateParamIndex, twoStep, firstCall );
}
else
{
it = find_if( vectorParamIndices.begin(), vectorParamIndices.end(), [i]( std::pair<size_t, size_t> const & vpi ) {
return vpi.second == i;
} );
if ( it != vectorParamIndices.end() )
{
appendArgumentCount( str, it->first, commandData.params[it->first].name, templateParamIndex );
}
else if ( beginsWith( commandData.params[i].type.type, "Vk" ) )
{
str += "static_cast<" + commandData.params[i].type.type + ">( " + commandData.params[i].name + " )";
}
else
{
appendArgumentPlainType( str, commandData.params[i] );
}
}
encounteredArgument = true;
}
}
void VulkanHppGenerator::appendArgumentVector( std::string & str,
size_t paramIndex,
ParamData const & paramData,
size_t returnParamIndex,
size_t templateParamIndex,
bool twoStep,
bool firstCall ) const
{
// this parameter is a vector parameter
assert( paramData.type.postfix.back() == '*' );
if ( ( returnParamIndex == paramIndex ) && twoStep && firstCall )
{
// this parameter is the return parameter, and it's the first call of a two-step algorithm -> just just nullptr
str += "nullptr";
}
else
{
std::string parameterName = startLowerCase( stripPrefix( paramData.name, "p" ) );
if ( beginsWith( paramData.type.type, "Vk" ) || ( paramIndex == templateParamIndex ) )
{
// CHECK for !commandData.params[it->first].optional
// this parameter is a vulkan type or a templated type -> need to reinterpret cast
appendReinterpretCast( str, paramData.type.prefix.find( "const" ) == 0, paramData.type.type );
str += "( " + parameterName + ".data() )";
}
else
{
// this parameter is just a vetor -> get the pointer to its data
str += parameterName + ".data()";
}
}
}
void VulkanHppGenerator::appendBaseTypes( std::string & str ) const
{
assert( !m_baseTypes.empty() );
for ( auto const & baseType : m_baseTypes )
{
if ( ( baseType.first != "VkFlags" ) &&
( baseType.first !=
"VkFlags64" ) ) // filter out VkFlags and VkFlags64, as they are mapped to our own Flags class
{
str += " using " + stripPrefix( baseType.first, "Vk" ) + " = " + baseType.second.type + ";\n";
}
}
}
void VulkanHppGenerator::appendBitmasks( std::string & str ) const
{
for ( auto const & bitmask : m_bitmasks )
{
auto bitmaskBits = m_enums.find( bitmask.second.requirements );
bool hasBits = ( bitmaskBits != m_enums.end() );
check( bitmask.second.requirements.empty() || hasBits,
bitmask.second.xmlLine,
"bitmask <" + bitmask.first + "> references the undefined requires <" + bitmask.second.requirements + ">" );
std::string strippedBitmaskName = stripPrefix( bitmask.first, "Vk" );
std::string strippedEnumName = hasBits ? stripPrefix( bitmaskBits->first, "Vk" ) : "";
std::string enter, leave;
std::tie( enter, leave ) = generateProtection( bitmask.first, !bitmask.second.alias.empty() );
str += "\n" + enter;
appendBitmask( str,
strippedBitmaskName,
bitmask.second.type,
bitmask.second.alias,
strippedEnumName,
hasBits ? bitmaskBits->second.values : std::vector<EnumValueData>() );
appendBitmaskToStringFunction(
str, strippedBitmaskName, strippedEnumName, hasBits ? bitmaskBits->second.values : std::vector<EnumValueData>() );
str += leave;
}
}
void VulkanHppGenerator::appendBitmask( std::string & str,
std::string const & bitmaskName,
std::string const & bitmaskType,
std::string const & bitmaskAlias,
std::string const & enumName,
std::vector<EnumValueData> const & enumValues ) const
{
// each Flags class is using the class 'Flags' with the corresponding FlagBits enum as the template parameter
// if there's no enum for the FlagBits, introduce an artificial empty one
std::string emptyEnumName;
if ( enumName.empty() )
{
emptyEnumName = bitmaskName;
size_t pos = emptyEnumName.rfind( "Flags" );
assert( pos != std::string::npos );
emptyEnumName.replace( pos, 5, "FlagBits" );
// if this emptyEnumName is not in the list of enums, list it here
if ( m_enums.find( "Vk" + emptyEnumName ) == m_enums.end() )
{
const std::string templateString = R"x( enum class ${enumName} : ${bitmaskType}
{};
VULKAN_HPP_INLINE std::string to_string( ${enumName} )
{
return "(void)";
}
)x";
str += replaceWithMap( templateString, { { "enumName", emptyEnumName }, { "bitmaskType", bitmaskType } } );
}
}
std::string name = ( enumName.empty() ? emptyEnumName : enumName );
str +=
"\n"
" using " +
bitmaskName + " = Flags<" + name + ">;\n";
if ( !enumValues.empty() )
{
std::string allFlags;
for ( auto const & value : enumValues )
{
if ( !allFlags.empty() )
{
allFlags += " | ";
}
allFlags += bitmaskType + "(" + enumName + "::" + value.vkValue + ")";
}
static const std::string bitmaskOperatorsTemplate = R"(
template <> struct FlagTraits<${enumName}>
{
enum : ${bitmaskType}
{
allFlags = ${allFlags}
};
};
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR ${bitmaskName} operator|( ${enumName} bit0, ${enumName} bit1 ) VULKAN_HPP_NOEXCEPT
{
return ${bitmaskName}( bit0 ) | bit1;
}
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR ${bitmaskName} operator&( ${enumName} bit0, ${enumName} bit1 ) VULKAN_HPP_NOEXCEPT
{
return ${bitmaskName}( bit0 ) & bit1;
}
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR ${bitmaskName} operator^( ${enumName} bit0, ${enumName} bit1 ) VULKAN_HPP_NOEXCEPT
{
return ${bitmaskName}( bit0 ) ^ bit1;
}
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR ${bitmaskName} operator~( ${enumName} bits ) VULKAN_HPP_NOEXCEPT
{
return ~( ${bitmaskName}( bits ) );
}
)";
str += replaceWithMap( bitmaskOperatorsTemplate,
{ { "bitmaskName", bitmaskName },
{ "bitmaskType", bitmaskType },
{ "enumName", enumName },
{ "allFlags", allFlags } } );
}
if ( !bitmaskAlias.empty() )
{
str +=
"\n"
" using " +
stripPrefix( bitmaskAlias, "Vk" ) + " = " + bitmaskName + ";\n";
}
}
void VulkanHppGenerator::appendBitmaskToStringFunction( std::string & str,
std::string const & bitmaskName,
std::string const & enumName,
std::vector<EnumValueData> const & enumValues ) const
{
str +=
"\n"
" VULKAN_HPP_INLINE std::string to_string( " +
bitmaskName + ( enumValues.empty() ? " " : " value " ) +
" )\n"
" {\n";
if ( enumValues.empty() )
{
str += "\n return \"{}\";\n";
}
else
{
// 'or' together all the bits in the value
str +=
"\n"
" if ( !value ) return \"{}\";\n"
" std::string result;\n";
for ( auto const & evd : enumValues )
{
if ( evd.singleBit )
{
str +=
"\n"
" if ( value & " +