-
Notifications
You must be signed in to change notification settings - Fork 309
/
VulkanHppGenerator.cpp
17402 lines (15903 loc) · 727 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 "XMLHelper.hpp"
#include <algorithm>
#include <array>
#include <cassert>
#include <fstream>
#include <regex>
#include <sstream>
using namespace std::literals;
namespace
{
std::vector<std::pair<std::string, size_t>> filterNumbers( std::vector<std::string> const & names );
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>::iterator findByNameOrAlias( std::vector<T> & values, std::string const & name );
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 generateNamespacedType( std::string const & type );
std::string generateNoDiscard( bool returnsSomething, bool multiSuccessCodes, bool multiErrorCodes );
std::string generateStandardArray( std::string const & type, std::vector<std::string> const & sizes );
bool isAllUpper( std::string const & name );
VulkanHppGenerator::MacroData parseMacro( std::vector<std::string> const & completeMacro );
std::string readSnippet( std::string const & snippetFile );
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
void writeToFile( std::string const & str, std::string const & fileName );
const std::set<std::string> specialPointerTypes = { "Display", "IDirectFB", "wl_display", "xcb_connection_t", "_screen_window" };
//
// 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( std::make_pair( "", HandleData() ) );
// read the document and check its correctness
const int line = document.GetLineNum();
std::vector<tinyxml2::XMLElement const *> elements = getChildElements( &document );
checkElements( line, elements, { { "registry", true } } );
checkForError( elements.size() == 1, line, "encountered " + std::to_string( elements.size() ) + " elements named <registry> but only one is allowed" );
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 )
{
addCommandsToHandle( feature.requireData );
addMissingFlagBits( feature.requireData, feature.name );
}
for ( auto & extension : m_extensions )
{
addCommandsToHandle( extension.requireData );
addMissingFlagBits( extension.requireData, extension.name );
}
m_definesPartition = partitionDefines( m_defines );
}
void VulkanHppGenerator::generateEnumsHppFile() const
{
std::string const vulkan_enums_hpp = std::string( BASE_PATH ) + "/vulkan/" + m_api + "_enums.hpp";
std::cout << "VulkanHppGenerator: Generating " << vulkan_enums_hpp << " ..." << std::endl;
std::string const vulkanEnumsHppTemplate = R"(${licenseHeader}
#ifndef VULKAN_ENUMS_HPP
# define VULKAN_ENUMS_HPP
// include-what-you-use: make sure, vulkan.hpp is used by code-completers
// IWYU pragma: private; include "vulkan.hpp"
namespace VULKAN_HPP_NAMESPACE
{
${Flags}
${enums}
${objectTypeToDebugReportObjectType}
} // namespace VULKAN_HPP_NAMESPACE
#endif
)";
std::string str = replaceWithMap( vulkanEnumsHppTemplate,
{ { "enums", generateEnums() },
{ "Flags", readSnippet( "Flags.hpp" ) },
{ "licenseHeader", m_vulkanLicenseHeader },
{ "objectTypeToDebugReportObjectType", generateObjectTypeToDebugReportObjectType() } } );
writeToFile( str, vulkan_enums_hpp );
}
void VulkanHppGenerator::generateExtensionInspectionFile() const
{
std::string const vulkan_extension_inspection_hpp = std::string( BASE_PATH ) + "/vulkan/" + m_api + "_extension_inspection.hpp";
std::cout << "VulkanHppGenerator: Generating " << vulkan_extension_inspection_hpp << " ..." << std::endl;
std::string const vulkanExtensionInspectionHppTemplate = readSnippet( "ExtensionInspection.hpp" );
std::string const deprecatedExtensions = generateReplacedExtensionsList( []( ExtensionData const & extension ) { return extension.isDeprecated; },
[]( ExtensionData const & extension ) { return extension.deprecatedBy; } );
std::string const deprecatedBy = generateExtensionReplacedBy( []( ExtensionData const & extension ) { return extension.isDeprecated; },
[]( ExtensionData const & extension ) { return extension.deprecatedBy; } );
std::string const obsoletedBy = generateExtensionReplacedBy( []( ExtensionData const & extension ) { return !extension.obsoletedBy.empty(); },
[]( ExtensionData const & extension ) { return extension.obsoletedBy; } );
std::string const obsoletedExtensions = generateReplacedExtensionsList( []( ExtensionData const & extension ) { return !extension.obsoletedBy.empty(); },
[]( ExtensionData const & extension ) { return extension.obsoletedBy; } );
std::string const promotedExtensions = generateReplacedExtensionsList( []( ExtensionData const & extension ) { return !extension.promotedTo.empty(); },
[]( ExtensionData const & extension ) { return extension.promotedTo; } );
std::string const promotedTo = generateExtensionReplacedBy( []( ExtensionData const & extension ) { return !extension.promotedTo.empty(); },
[]( ExtensionData const & extension ) { return extension.promotedTo; } );
std::string str =
replaceWithMap( vulkanExtensionInspectionHppTemplate,
{ { "api", m_api },
{ "deprecatedExtensions", deprecatedExtensions },
{ "deviceExtensions", generateExtensionsList( "device" ) },
{ "deviceTest", generateExtensionTypeTest( "device" ) },
{ "deprecatedBy", deprecatedBy },
{ "deprecatedTest", generateExtensionReplacedTest( []( ExtensionData const & extension ) { return extension.isDeprecated; } ) },
{ "extensionDependencies", generateExtensionDependencies() },
{ "getExtensionDependsByVersionDeclaration", generateExtensionDependsByVersion( false ) },
{ "getExtensionDependsByVersionDefinition", generateExtensionDependsByVersion( true ) },
{ "instanceExtensions", generateExtensionsList( "instance" ) },
{ "instanceTest", generateExtensionTypeTest( "instance" ) },
{ "licenseHeader", m_vulkanLicenseHeader },
{ "obsoletedBy", obsoletedBy },
{ "obsoletedExtensions", obsoletedExtensions },
{ "obsoletedTest", generateExtensionReplacedTest( []( ExtensionData const & extension ) { return !extension.obsoletedBy.empty(); } ) },
{ "promotedExtensions", promotedExtensions },
{ "promotedTest", generateExtensionReplacedTest( []( ExtensionData const & extension ) { return !extension.promotedTo.empty(); } ) },
{ "promotedTo", promotedTo },
{ "voidExtension", ( m_api == "vulkan" ) ? "" : "(void)extension;" } } );
writeToFile( str, vulkan_extension_inspection_hpp );
}
void VulkanHppGenerator::generateFormatTraitsHppFile() const
{
std::string const vulkan_format_traits_hpp = std::string( BASE_PATH ) + "/vulkan/" + m_api + "_format_traits.hpp";
std::cout << "VulkanHppGenerator: Generating " << vulkan_format_traits_hpp << " ..." << std::endl;
std::string const vulkanFormatTraitsHppTemplate = R"(${licenseHeader}
#ifndef VULKAN_FORMAT_TRAITS_HPP
# define VULKAN_FORMAT_TRAITS_HPP
#include <vulkan/${api}.hpp>
namespace VULKAN_HPP_NAMESPACE
{
${formatTraits}
} // namespace VULKAN_HPP_NAMESPACE
#endif
)";
std::string str = replaceWithMap( vulkanFormatTraitsHppTemplate,
{ { "api", m_api }, { "formatTraits", generateFormatTraits() }, { "licenseHeader", m_vulkanLicenseHeader } } );
writeToFile( str, vulkan_format_traits_hpp );
}
void VulkanHppGenerator::generateFuncsHppFile() const
{
std::string const vulkan_funcs_hpp = std::string( BASE_PATH ) + "/vulkan/" + m_api + "_funcs.hpp";
std::cout << "VulkanHppGenerator: Generating " << vulkan_funcs_hpp << " ..." << std::endl;
std::string const vulkanFuncsHppTemplate = R"(${licenseHeader}
#ifndef VULKAN_FUNCS_HPP
# define VULKAN_FUNCS_HPP
// include-what-you-use: make sure, vulkan.hpp is used by code-completers
// IWYU pragma: private; include "vulkan.hpp"
namespace VULKAN_HPP_NAMESPACE
{
${commandDefinitions}
} // namespace VULKAN_HPP_NAMESPACE
#endif
)";
std::string str =
replaceWithMap( vulkanFuncsHppTemplate, { { "commandDefinitions", generateCommandDefinitions() }, { "licenseHeader", m_vulkanLicenseHeader } } );
writeToFile( str, vulkan_funcs_hpp );
}
void VulkanHppGenerator::generateHandlesHppFile() const
{
std::string const vulkan_handles_hpp = std::string( BASE_PATH ) + "/vulkan/" + m_api + "_handles.hpp";
std::cout << "VulkanHppGenerator: Generating " << vulkan_handles_hpp << " ..." << std::endl;
std::string const vulkanHandlesHppTemplate = R"(${licenseHeader}
#ifndef VULKAN_HANDLES_HPP
# define VULKAN_HANDLES_HPP
// include-what-you-use: make sure, vulkan.hpp is used by code-completers
// IWYU pragma: private; include "vulkan.hpp"
namespace VULKAN_HPP_NAMESPACE
{
${structForwardDeclarations}
${handleForwardDeclarations}
${uniqueHandles}
${handles}
// operators to compare vk::-handles
# if defined( VULKAN_HPP_HAS_SPACESHIP_OPERATOR )
template <typename T, typename std::enable_if<VULKAN_HPP_NAMESPACE::isVulkanHandleType<T>::value, int>::type = 0>
auto operator<=>( T const & lhs, T const & rhs )
{
return static_cast<typename T::NativeType>( lhs ) <=> static_cast<typename T::NativeType>( rhs );
}
#else
template <typename T, typename std::enable_if<VULKAN_HPP_NAMESPACE::isVulkanHandleType<T>::value, int>::type = 0>
bool operator==( T const & lhs, T const & rhs )
{
return static_cast<typename T::NativeType>( lhs ) == static_cast<typename T::NativeType>( rhs );
}
template <typename T, typename std::enable_if<VULKAN_HPP_NAMESPACE::isVulkanHandleType<T>::value, int>::type = 0>
bool operator!=( T const & lhs, T const & rhs )
{
return static_cast<typename T::NativeType>( lhs ) != static_cast<typename T::NativeType>( rhs );
}
template <typename T, typename std::enable_if<VULKAN_HPP_NAMESPACE::isVulkanHandleType<T>::value, int>::type = 0>
bool operator<( T const & lhs, T const & rhs )
{
return static_cast<typename T::NativeType>( lhs ) < static_cast<typename T::NativeType>( rhs );
}
#endif
template <typename T, typename std::enable_if<VULKAN_HPP_NAMESPACE::isVulkanHandleType<T>::value, int>::type = 0>
bool operator==( T const & v, std::nullptr_t )
{
return !v;
}
template <typename T, typename std::enable_if<VULKAN_HPP_NAMESPACE::isVulkanHandleType<T>::value, int>::type = 0>
bool operator==( std::nullptr_t, T const & v )
{
return !v;
}
template <typename T, typename std::enable_if<VULKAN_HPP_NAMESPACE::isVulkanHandleType<T>::value, int>::type = 0>
bool operator!=( T const & v, std::nullptr_t )
{
return !!v;
}
template <typename T, typename std::enable_if<VULKAN_HPP_NAMESPACE::isVulkanHandleType<T>::value, int>::type = 0>
bool operator!=( std::nullptr_t, T const & v )
{
return !!v;
}
} // namespace VULKAN_HPP_NAMESPACE
#endif
)";
std::string str = replaceWithMap( vulkanHandlesHppTemplate,
{
{ "handles", generateHandles() },
{ "handleForwardDeclarations", generateHandleForwardDeclarations() },
{ "licenseHeader", m_vulkanLicenseHeader },
{ "structForwardDeclarations", generateStructForwardDeclarations() },
{ "uniqueHandles", generateUniqueHandles() },
} );
writeToFile( str, vulkan_handles_hpp );
}
void VulkanHppGenerator::generateHashHppFile() const
{
std::string const vulkan_hash_hpp = std::string( BASE_PATH ) + "/vulkan/" + m_api + "_hash.hpp";
std::cout << "VulkanHppGenerator: Generating " << vulkan_hash_hpp << " ..." << std::endl;
std::string const vulkanHandlesHppTemplate = R"(${licenseHeader}
#ifndef VULKAN_HASH_HPP
# define VULKAN_HASH_HPP
#include <vulkan/${api}.hpp>
namespace std
{
//=======================================
//=== HASH structures for Flags types ===
//=======================================
template <typename BitType>
struct hash<VULKAN_HPP_NAMESPACE::Flags<BitType>>
{
std::size_t operator()( VULKAN_HPP_NAMESPACE::Flags<BitType> const & flags ) const VULKAN_HPP_NOEXCEPT
{
return std::hash<typename std::underlying_type<BitType>::type>{}(
static_cast<typename std::underlying_type<BitType>::type>( flags ) );
}
};
${handleHashStructures}
${structHashStructures}
} // namespace std
#endif
)";
std::string str = replaceWithMap( vulkanHandlesHppTemplate,
{ { "api", m_api },
{ "handleHashStructures", generateHandleHashStructures() },
{ "licenseHeader", m_vulkanLicenseHeader },
{ "structHashStructures", generateStructHashStructures() } } );
writeToFile( str, vulkan_hash_hpp );
}
void VulkanHppGenerator::generateHppFile() const
{
std::string const vulkan_hpp = std::string( BASE_PATH ) + "/vulkan/" + m_api + ".hpp";
std::cout << "VulkanHppGenerator: Generating " << vulkan_hpp << " ... " << std::endl;
std::string const vulkanHppTemplate = R"(${licenseHeader}
#ifndef VULKAN_HPP
# define VULKAN_HPP
${includes}
static_assert( VK_HEADER_VERSION == ${headerVersion}, "Wrong VK_HEADER_VERSION!" );
${defines}
namespace VULKAN_HPP_NAMESPACE
{
${ArrayWrapper1D}
${ArrayWrapper2D}
#if !defined( VULKAN_HPP_DISABLE_ENHANCED_MODE )
${ArrayProxy}
${ArrayProxyNoTemporaries}
${StridedArrayProxy}
${Optional}
${StructureChain}
${UniqueHandle}
#endif // VULKAN_HPP_DISABLE_ENHANCED_MODE
namespace detail
{
${DispatchLoaderBase}
${DispatchLoaderStatic}
}
${Exchange}
#if !defined( VULKAN_HPP_NO_SMART_HANDLE )
struct AllocationCallbacks;
namespace detail
{
${ObjectDestroy}
${ObjectFree}
${ObjectRelease}
${PoolFree}
}
#endif // !VULKAN_HPP_NO_SMART_HANDLE
${baseTypes}
template <typename Type, Type value = Type{}>
struct CppType
{};
} // namespace VULKAN_HPP_NAMESPACE
#include <vulkan/${api}_enums.hpp>
#if !defined( VULKAN_HPP_NO_TO_STRING )
#include <vulkan/${api}_to_string.hpp>
#endif
#ifndef VULKAN_HPP_NO_EXCEPTIONS
namespace std
{
template <>
struct is_error_code_enum<VULKAN_HPP_NAMESPACE::Result> : public true_type
{};
} // namespace std
#endif
namespace VULKAN_HPP_NAMESPACE
{
#ifndef VULKAN_HPP_NO_EXCEPTIONS
${Exceptions}
${resultExceptions}
${throwResultException}
#endif
${ResultValue}
${resultChecks}
${constexprDefines}
} // namespace VULKAN_HPP_NAMESPACE
// clang-format off
#include <vulkan/${api}_handles.hpp>
#include <vulkan/${api}_structs.hpp>
#include <vulkan/${api}_funcs.hpp>
// clang-format on
namespace VULKAN_HPP_NAMESPACE
{
#if !defined( VULKAN_HPP_DISABLE_ENHANCED_MODE )
${structExtendsStructs}
#endif // VULKAN_HPP_DISABLE_ENHANCED_MODE
namespace detail
{
${DynamicLoader}
${DispatchLoaderDynamic}
}
} // namespace VULKAN_HPP_NAMESPACE
#endif
)";
std::string str = replaceWithMap(
vulkanHppTemplate,
{ { "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 == "vulkan" ) ? "vulkan.h" : "vulkan_sc_core.h" } } ) },
{ "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" ) } } );
writeToFile( str, vulkan_hpp );
}
void VulkanHppGenerator::generateMacrosFile() const
{
std::string const macros_hpp = std::string( BASE_PATH ) + "/vulkan/" + m_api + "_hpp_macros.hpp";
std::cout << "VulkanHppGenerator: Generating " << macros_hpp << " ..." << std::endl;
std::string const macrosTemplate = R"(${licenseHeader}
#ifndef VULKAN_HPP_MACROS_HPP
#define VULKAN_HPP_MACROS_HPP
${macros}
#endif)";
std::string str =
replaceWithMap( macrosTemplate,
{ { "licenseHeader", m_vulkanLicenseHeader },
{ "macros",
replaceWithMap( readSnippet( "macros.hpp" ),
{ { "vulkan_hpp", m_api + ".hpp" },
{ "vulkan_64_bit_ptr_defines", m_defines.at( "VK_USE_64_BIT_PTR_DEFINES" ).possibleDefinition } } ) } } );
writeToFile( str, macros_hpp );
}
void VulkanHppGenerator::generateRAIIHppFile() const
{
std::string const vulkan_raii_hpp = std::string( BASE_PATH ) + "/vulkan/" + m_api + "_raii.hpp";
std::cout << "VulkanHppGenerator: Generating " << vulkan_raii_hpp << " ..." << std::endl;
std::string const vulkanHandlesHppTemplate = R"(${licenseHeader}
#ifndef VULKAN_RAII_HPP
#define VULKAN_RAII_HPP
#include <vulkan/${api}.hpp>
#if !( defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE ) )
# include <memory> // std::unique_ptr
# include <utility> // std::forward
#endif
#if !defined( VULKAN_HPP_DISABLE_ENHANCED_MODE )
namespace VULKAN_HPP_NAMESPACE
{
namespace VULKAN_HPP_RAII_NAMESPACE
{
namespace detail
{
template <class T>
class CreateReturnType
{
public:
#if defined( VULKAN_HPP_RAII_NO_EXCEPTIONS )
using Type = VULKAN_HPP_EXPECTED<T, VULKAN_HPP_NAMESPACE::Result>;
#else
using Type = T;
#endif
};
using PFN_dummy = void ( * )();
${RAIIDispatchers}
}
${RAIIHandles}
${RAIICommandDefinitions}
//====================
//=== RAII Helpers ===
//====================
template <typename RAIIType>
std::vector<typename RAIIType::CppType> filterCppTypes( std::vector<RAIIType> const & raiiTypes )
{
std::vector<typename RAIIType::CppType> cppTypes( raiiTypes.size() );
std::transform( raiiTypes.begin(), raiiTypes.end(), cppTypes.begin(), []( RAIIType const & d ) { return *d; } );
return cppTypes;
}
template <typename RAIIType, class UnaryPredicate>
std::vector<typename RAIIType::CppType> filterCppTypes( std::vector<RAIIType> const & raiiTypes, UnaryPredicate p)
{
std::vector<typename RAIIType::CppType> cppTypes;
for (auto const& t : raiiTypes)
{
if (p(t))
{
cppTypes.push_back( *t );
}
}
return cppTypes;
}
} // namespace VULKAN_HPP_RAII_NAMESPACE
} // namespace VULKAN_HPP_NAMESPACE
#endif
#endif
)";
std::string str = replaceWithMap( vulkanHandlesHppTemplate,
{ { "api", m_api },
{ "licenseHeader", m_vulkanLicenseHeader },
{ "RAIICommandDefinitions", generateRAIICommandDefinitions() },
{ "RAIIDispatchers", generateRAIIDispatchers() },
{ "RAIIHandles", generateRAIIHandles() } } );
writeToFile( str, vulkan_raii_hpp );
}
void VulkanHppGenerator::generateSharedHppFile() const
{
std::string const vulkan_shared_hpp = std::string( BASE_PATH ) + "/vulkan/" + m_api + "_shared.hpp";
std::cout << "VulkanHppGenerator: Generating " << vulkan_shared_hpp << " ..." << std::endl;
std::string const vulkanHandlesHppTemplate = R"(${licenseHeader}
#ifndef VULKAN_SHARED_HPP
#define VULKAN_SHARED_HPP
#include <vulkan/${api}.hpp>
#if !( defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE ) )
#include <atomic> // std::atomic_size_t
#endif
namespace VULKAN_HPP_NAMESPACE
{
#if !defined( VULKAN_HPP_NO_SMART_HANDLE )
${sharedHandle}
namespace detail
{
${sharedDestroy}
}
${sharedHandles}
${sharedHandleSpecializations}
${sharedHandlesNoDestroy}
#endif // !VULKAN_HPP_NO_SMART_HANDLE
} // namespace VULKAN_HPP_NAMESPACE
#endif // VULKAN_SHARED_HPP
)";
std::string str = replaceWithMap( vulkanHandlesHppTemplate,
{
{ "api", m_api },
{ "licenseHeader", m_vulkanLicenseHeader },
{ "sharedDestroy", readSnippet( "SharedDestroy.hpp" ) },
{ "sharedHandle", readSnippet( "SharedHandle.hpp" ) },
{ "sharedHandles", generateSharedHandles() },
{ "sharedHandlesNoDestroy", generateSharedHandlesNoDestroy() },
{ "sharedHandleSpecializations", readSnippet( "SharedHandleSpecializations.hpp" ) },
} );
writeToFile( str, vulkan_shared_hpp );
}
void VulkanHppGenerator::generateStaticAssertionsHppFile() const
{
std::string const static_assertions_hpp = std::string( BASE_PATH ) + "/vulkan/" + m_api + "_static_assertions.hpp";
std::cout << "VulkanHppGenerator: Generating " << static_assertions_hpp << " ..." << std::endl;
std::string const vulkanHandlesHppTemplate = R"(${licenseHeader}
#ifndef VULKAN_STATIC_ASSERTIONS_HPP
# define VULKAN_STATIC_ASSERTIONS_HPP
#include <vulkan/${api}.hpp>
//=========================
//=== static_assertions ===
//=========================
${staticAssertions}
#endif
)";
std::string str = replaceWithMap( vulkanHandlesHppTemplate,
{ { "api", m_api }, { "licenseHeader", m_vulkanLicenseHeader }, { "staticAssertions", generateStaticAssertions() } } );
writeToFile( str, static_assertions_hpp );
}
void VulkanHppGenerator::generateStructsHppFile() const
{
std::string const vulkan_structs_hpp = std::string( BASE_PATH ) + "/vulkan/" + m_api + "_structs.hpp";
std::cout << "VulkanHppGenerator: Generating " << vulkan_structs_hpp << " ..." << std::endl;
std::string const vulkanHandlesHppTemplate = R"(${licenseHeader}
#ifndef VULKAN_STRUCTS_HPP
# define VULKAN_STRUCTS_HPP
// include-what-you-use: make sure, vulkan.hpp is used by code-completers
// IWYU pragma: private; include "vulkan.hpp"
#include <cstring> // strcmp
namespace VULKAN_HPP_NAMESPACE
{
${structs}
} // namespace VULKAN_HPP_NAMESPACE
#endif
)";
std::string str = replaceWithMap( vulkanHandlesHppTemplate, { { "licenseHeader", m_vulkanLicenseHeader }, { "structs", generateStructs() } } );
writeToFile( str, vulkan_structs_hpp );
}
void VulkanHppGenerator::generateToStringHppFile() const
{
std::string const vulkan_to_string_hpp = std::string( BASE_PATH ) + "/vulkan/" + m_api + "_to_string.hpp";
std::cout << "VulkanHppGenerator: Generating " << vulkan_to_string_hpp << "..." << std::endl;
std::string const vulkanHandlesHppTemplate = R"(${licenseHeader}
#ifndef VULKAN_TO_STRING_HPP
# define VULKAN_TO_STRING_HPP
#include <vulkan/${api}.hpp>
// ignore warnings on using deprecated enum values in this header
#if defined( __clang__ ) || defined( __GNUC__ )
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined( _MSC_VER )
# pragma warning( push )
# pragma warning( disable : 4996 )
#endif
#if defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE )
import VULKAN_HPP_STD_MODULE;
#else
# if __cpp_lib_format
# include <format> // std::format
# else
# include <sstream> // std::stringstream
# endif
#endif
namespace VULKAN_HPP_NAMESPACE
{
${bitmasksToString}
${enumsToString}
} // namespace VULKAN_HPP_NAMESPACE
#if defined( __clang__ ) || defined( __GNUC__ )
# pragma GCC diagnostic pop
#elif defined( _MSC_VER )
# pragma warning( pop )
#endif
#endif
)";
std::string str = replaceWithMap( vulkanHandlesHppTemplate,
{ { "api", m_api },
{ "bitmasksToString", generateBitmasksToString() },
{ "enumsToString", generateEnumsToString() },
{ "licenseHeader", m_vulkanLicenseHeader } } );
writeToFile( str, vulkan_to_string_hpp );
}
void VulkanHppGenerator::generateCppModuleFile() const
{
std::string const vulkan_cppm = std::string( BASE_PATH ) + "/vulkan/" + m_api + ".cppm";
std::cout << "VulkanHppGenerator: Generating " << vulkan_cppm << "..." << std::endl;
std::string const vulkanCppmTemplate = R"(${licenseHeader}
// Note: This module is still in an experimental state.
// Any feedback is welcome on https://github.com/KhronosGroup/Vulkan-Hpp/issues.
module;
#include <vulkan/vulkan_hpp_macros.hpp>
#if defined( __cpp_lib_modules ) && !defined( VULKAN_HPP_ENABLE_STD_MODULE )
#define VULKAN_HPP_ENABLE_STD_MODULE
#endif
#include <vulkan/${api}.hpp>
#include <vulkan/${api}_extension_inspection.hpp>
#include <vulkan/${api}_format_traits.hpp>
#include <vulkan/${api}_hash.hpp>
#include <vulkan/${api}_raii.hpp>
#include <vulkan/${api}_shared.hpp>
#include <vulkan/${api}_to_string.hpp>
export module ${api}_hpp;
export namespace VULKAN_HPP_NAMESPACE
{
${usings}
#if !defined( VULKAN_HPP_DISABLE_ENHANCED_MODE )
namespace VULKAN_HPP_RAII_NAMESPACE
{
${raiiUsings}
} // namespace VULKAN_HPP_RAII_NAMESPACE
#endif
} // namespace VULKAN_HPP_NAMESPACE
export namespace std
{
${hashSpecializations}
}
)";
auto const str = replaceWithMap( vulkanCppmTemplate,
{ { "api", m_api },
{ "hashSpecializations", generateCppModuleHashSpecializations() },
{ "licenseHeader", m_vulkanLicenseHeader },
{ "raiiUsings", generateCppModuleRaiiUsings() },
{ "usings", generateCppModuleUsings() } } );
writeToFile( str, vulkan_cppm );
}
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 );
}
distributeSecondLevelCommands( m_RAIISpecialFunctions );
}
void VulkanHppGenerator::prepareVulkanFuncs()
{
// rename a couple of function parameters to prevent this warning, treated as an error:
// warning C4458: declaration of 'objectType' hides class member
for ( auto & command : m_commands )
{
for ( auto & param : command.second.params )
{
if ( param.name == "objectType" )
{
param.name += "_";
}
}
}
}
//
// VulkanHppGenerator private interface
//
void VulkanHppGenerator::addCommand( std::string const & name, CommandData & commandData )
{
// find the handle this command is going to be associated to
checkForError( !commandData.params.empty(), commandData.xmlLine, "command <" + name + "> with no params" );
auto handleIt = m_handles.find( commandData.params[0].type.type );
if ( handleIt == m_handles.end() )
{
handleIt = m_handles.begin();
assert( handleIt->first == "" );
}
commandData.handle = handleIt->first;
// add this command to the list of commands
checkForError( m_commands.insert( std::make_pair( name, commandData ) ).second, commandData.xmlLine, "already encountered command <" + name + ">" );
}
void VulkanHppGenerator::addCommandsToHandle( std::vector<RequireData> const & requireData )
{
for ( auto const & require : requireData )
{
for ( auto const & command : require.commands )
{
auto commandIt = findByNameOrAlias( m_commands, command.name );
assert( commandIt != m_commands.end() );
auto handleIt = m_handles.find( commandIt->second.handle );
assert( handleIt != m_handles.end() );
if ( !handleIt->second.commands.contains( command.name ) )
{
handleIt->second.commands.insert( command.name );
registerDeleter( command.name, commandIt->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
const size_t 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 );
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( std::make_pair( flagBits, EnumData{ .isBitmask = true, .xmlLine = 0 } ) );
assert( !m_types.contains( flagBits ) );
m_types.insert( std::make_pair( flagBits, TypeData{ TypeCategory::Bitmask, { requiredBy }, 0 } ) );
}
else
{
assert( m_types.contains( flagBits ) );
enumIt->second.isBitmask = true;
}
}
if ( std::none_of(
require.types.begin(), require.types.end(), [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() )
{
const auto [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
{
// check if all vector sizes are by value and their type is one of "uint32_t", "VkDeviceSize", or "VkSampleCountFlagBits"
return std::all_of( vectorParams.begin(),
vectorParams.end(),
[¶ms]( auto const & vpi )
{
return params[vpi.second.lenParam].type.isValue() &&
( ( params[vpi.second.lenParam].type.type == "uint32_t" ) || ( params[vpi.second.lenParam].type.type == "VkDeviceSize" ) ||
( params[vpi.second.lenParam].type.type == "VkSampleCountFlagBits" ) );
} );
}
void VulkanHppGenerator::appendDispatchLoaderDynamicCommands( std::vector<RequireData> const & requireData,
std::set<std::string> & listedCommands,
std::string const & title,
std::string & commandMembers,
std::string & initialCommandAssignments,
std::string & instanceCommandAssignments,
std::string & deviceCommandAssignments ) const
{
std::string members, initial, instance, device, placeholders;
for ( auto const & require : requireData )
{
for ( auto const & command : require.commands )
{
if ( listedCommands.insert( command.name ).second )
{
auto commandIt = findByNameOrAlias( m_commands, command.name );
assert( commandIt != m_commands.end() );
members += " PFN_" + command.name + " " + command.name + " = 0;\n";
placeholders += " PFN_dummy " + command.name + "_placeholder = 0;\n";
if ( commandIt->second.handle.empty() )
{
initial += generateDispatchLoaderDynamicCommandAssignment( command.name, commandIt->first, "NULL" );
}
else
{
instance += generateDispatchLoaderDynamicCommandAssignment( command.name, commandIt->first, "instance" );
if ( isDeviceCommand( commandIt->second ) )
{
device += generateDispatchLoaderDynamicCommandAssignment( command.name, commandIt->first, "device" );
}
}
}
}
}
const auto [enter, leave] = generateProtection( getProtectFromTitle( title ) );
std::string header = "\n" + enter + " //=== " + title + " ===\n";
if ( !members.empty() )
{
commandMembers += header + members;
if ( !enter.empty() )
{
commandMembers += "#else\n" + placeholders;
}
commandMembers += leave;
}
if ( !initial.empty() )
{
initialCommandAssignments += header + initial + leave;
}
if ( !instance.empty() )
{
instanceCommandAssignments += header + instance + leave;
}
if ( !device.empty() )