This repository was archived by the owner on Apr 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathVPipelineCompiler.cpp
1292 lines (1067 loc) · 36.7 KB
/
VPipelineCompiler.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) 2018-2020, Zhirnov Andrey. For more information see 'LICENSE'
#include "VPipelineCompiler.h"
#include "SpirvCompiler.h"
#include "PrivateDefines.h"
#include "framegraph/Shared/EnumUtils.h"
#include "framegraph/Shared/EnumToString.h"
#include "VCachedDebuggableShaderData.h"
#ifdef FG_ENABLE_VULKAN
# include "extensions/vulkan_loader/VulkanLoader.h"
# include "extensions/vulkan_loader/VulkanCheckError.h"
#endif
namespace FG
{
/*
=================================================
constructor
=================================================
*/
VPipelineCompiler::VPipelineCompiler () :
_spirvCompiler{ new SpirvCompiler{ _directories }}
{
EXLOCK( _lock );
// enable all features
_spirvCompiler->SetShaderClockFeatures( true, true );
_spirvCompiler->SetShaderFeatures( true, true );
}
/*
=================================================
constructor
=================================================
*/
VPipelineCompiler::VPipelineCompiler (InstanceVk_t instance, PhysicalDeviceVk_t physicalDevice, DeviceVk_t device) :
VPipelineCompiler()
{
EXLOCK( _lock );
#ifdef FG_ENABLE_VULKAN
_vkInstance = instance;
_vkPhysicalDevice = physicalDevice;
_vkLogicalDevice = device;
if ( _vkInstance and _vkPhysicalDevice )
{
_fpCreateShaderModule = BitCast<void*>( vkGetDeviceProcAddr( BitCast<VkDevice>(_vkLogicalDevice), "vkCreateShaderModule" ));
_fpDestroyShaderModule = BitCast<void*>( vkGetDeviceProcAddr( BitCast<VkDevice>(_vkLogicalDevice), "vkDestroyShaderModule" ));
#ifdef VK_KHR_shader_clock
auto fpGetPhysicalDeviceFeatures2 = BitCast<PFN_vkGetPhysicalDeviceFeatures2KHR>( vkGetInstanceProcAddr( BitCast<VkInstance>(_vkInstance), "vkGetPhysicalDeviceFeatures2KHR" ));
if (fpGetPhysicalDeviceFeatures2 == null)
fpGetPhysicalDeviceFeatures2 = BitCast<PFN_vkGetPhysicalDeviceFeatures2KHR>( vkGetInstanceProcAddr( BitCast<VkInstance>(_vkInstance), "vkGetPhysicalDeviceFeatures2" ));
if ( fpGetPhysicalDeviceFeatures2 )
{
VkPhysicalDeviceShaderClockFeaturesKHR clock_feat = {};
clock_feat.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_CLOCK_FEATURES_KHR;
VkPhysicalDeviceFeatures2 feats = {};
feats.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
feats.pNext = &clock_feat;
fpGetPhysicalDeviceFeatures2( BitCast<VkPhysicalDevice>(_vkPhysicalDevice), OUT &feats );
_spirvCompiler->SetShaderClockFeatures( clock_feat.shaderSubgroupClock, clock_feat.shaderDeviceClock );
_spirvCompiler->SetShaderFeatures( feats.features.vertexPipelineStoresAndAtomics, feats.features.fragmentStoresAndAtomics );
}
else
#endif // VK_KHR_shader_clock
{
auto fpGetPhysicalDeviceFeatures = BitCast<PFN_vkGetPhysicalDeviceFeatures>( vkGetInstanceProcAddr( BitCast<VkInstance>(_vkInstance), "vkGetPhysicalDeviceFeatures" ));
if ( fpGetPhysicalDeviceFeatures )
{
VkPhysicalDeviceFeatures feats = {};
fpGetPhysicalDeviceFeatures( BitCast<VkPhysicalDevice>(_vkPhysicalDevice), OUT &feats );
_spirvCompiler->SetShaderFeatures( feats.vertexPipelineStoresAndAtomics, feats.fragmentStoresAndAtomics );
}
_spirvCompiler->SetShaderClockFeatures( false, false );
}
}
#else
Unused( instance, physicalDevice, device );
#endif
}
/*
=================================================
destructor
=================================================
*/
VPipelineCompiler::~VPipelineCompiler ()
{
ReleaseShaderCache();
}
/*
=================================================
SetCompilationFlags
=================================================
*/
bool VPipelineCompiler::SetCompilationFlags (EShaderCompilationFlags flags)
{
EXLOCK( _lock );
_compilerFlags = flags;
#ifdef FG_ENABLE_VULKAN
if ( AllBits( flags, EShaderCompilationFlags::UseCurrentDeviceLimits ) and _vkPhysicalDevice != Zero )
CHECK_ERR( _spirvCompiler->SetCurrentResourceLimits( _vkPhysicalDevice ))
else
#endif
CHECK_ERR( _spirvCompiler->SetDefaultResourceLimits() );
_spirvCompiler->SetCompilationFlags( flags );
return true;
}
/*
=================================================
SetDebugFlags
=================================================
*/
void VPipelineCompiler::SetDebugFlags (EShaderLangFormat flags)
{
EXLOCK( _lock );
_spirvCompiler->SetDebugFlags( flags & EShaderLangFormat::_DebugModeMask );
}
/*
=================================================
AddDirectory
=================================================
*/
void VPipelineCompiler::AddDirectory (StringView path)
{
EXLOCK( _lock );
String file_path;
# ifdef FS_HAS_FILESYSTEM
FS::path fpath{ path };
if ( not fpath.is_absolute() )
fpath = FS::absolute( fpath );
fpath.make_preferred();
CHECK_ERRV( FS::exists( fpath ));
file_path = fpath.string();
# else
file_path = path;
# endif
for (const auto& dir : _directories) {
if ( dir == file_path )
return; // already exists
}
_directories.push_back( std::move(file_path) );
}
/*
=================================================
ReleaseUnusedShaders
=================================================
*/
void VPipelineCompiler::ReleaseUnusedShaders ()
{
#ifdef FG_ENABLE_VULKAN
EXLOCK( _lock );
if ( _vkLogicalDevice == VK_NULL_HANDLE )
return;
VkDevice dev = BitCast<VkDevice>( _vkLogicalDevice );
auto DestroyShaderModule = BitCast<PFN_vkDestroyShaderModule>(_fpDestroyShaderModule);
for (auto iter = _shaderCache.begin(); iter != _shaderCache.end();)
{
if ( not (iter->second.use_count() == 1) )
{
++iter;
continue;
}
Cast<VCachedDebuggableShaderModule>( iter->second )->Destroy( DestroyShaderModule, dev );
iter = _shaderCache.erase( iter );
}
#endif
}
/*
=================================================
ReleaseShaderCache
=================================================
*/
void VPipelineCompiler::ReleaseShaderCache ()
{
#ifdef FG_ENABLE_VULKAN
EXLOCK( _lock );
if ( _vkLogicalDevice == VK_NULL_HANDLE )
return;
VkDevice dev = BitCast<VkDevice>( _vkLogicalDevice );
auto DestroyShaderModule = BitCast<PFN_vkDestroyShaderModule>(_fpDestroyShaderModule);
for (auto& sh : _shaderCache)
{
// pipeline may keep reference to shader module, so you need to release pipeline before
ASSERT( sh.second.use_count() == 1 );
Cast<VCachedDebuggableShaderModule>( sh.second )->Destroy( DestroyShaderModule, dev );
}
_shaderCache.clear();
#endif
}
/*
=================================================
IsSrcFormatSupported
=================================================
*/
ND_ static bool IsSrcFormatSupported (EShaderLangFormat srcFormat)
{
switch ( srcFormat & EShaderLangFormat::_ApiStorageFormatMask )
{
case EShaderLangFormat::OpenGL | EShaderLangFormat::HighLevel :
case EShaderLangFormat::Vulkan | EShaderLangFormat::HighLevel :
return true;
}
return false;
}
/*
=================================================
IsDstFormatSupported
=================================================
*/
ND_ static bool IsDstFormatSupported (EShaderLangFormat dstFormat, bool hasDevice)
{
switch ( dstFormat & EShaderLangFormat::_ApiStorageFormatMask )
{
case EShaderLangFormat::Vulkan | EShaderLangFormat::SPIRV : return true;
case EShaderLangFormat::Vulkan | EShaderLangFormat::ShaderModule : return hasDevice;
}
return false;
}
/*
=================================================
IsSupported
=================================================
*/
bool VPipelineCompiler::IsSupported (const MeshPipelineDesc &ppln, EShaderLangFormat dstFormat) const
{
// lock is not needed because only '_vkLogicalDevice' read access is used
if ( ppln._shaders.empty() )
return false;
if ( not IsDstFormatSupported( dstFormat, (_vkLogicalDevice != Zero) ))
return false;
bool is_supported = true;
for (auto& sh : ppln._shaders)
{
is_supported &= _IsSupported( sh.second.data );
}
return is_supported;
}
/*
=================================================
IsSupported
=================================================
*/
bool VPipelineCompiler::IsSupported (const RayTracingPipelineDesc &ppln, EShaderLangFormat dstFormat) const
{
// lock is not needed because only '_vkLogicalDevice' read access is used
if ( ppln._shaders.empty() )
return false;
if ( not IsDstFormatSupported( dstFormat, (_vkLogicalDevice != Zero) ))
return false;
bool is_supported = true;
for (auto& sh : ppln._shaders)
{
is_supported &= _IsSupported( sh.second.data );
}
return is_supported;
}
/*
=================================================
IsSupported
=================================================
*/
bool VPipelineCompiler::IsSupported (const GraphicsPipelineDesc &ppln, EShaderLangFormat dstFormat) const
{
// lock is not needed because only '_vkLogicalDevice' read access is used
if ( ppln._shaders.empty() )
return false;
if ( not IsDstFormatSupported( dstFormat, (_vkLogicalDevice != Zero) ))
return false;
bool is_supported = true;
for (auto& sh : ppln._shaders)
{
is_supported &= _IsSupported( sh.second.data );
}
return is_supported;
}
/*
=================================================
IsSupported
=================================================
*/
bool VPipelineCompiler::IsSupported (const ComputePipelineDesc &ppln, EShaderLangFormat dstFormat) const
{
// lock is not needed because only '_vkLogicalDevice' read access is used
if ( not IsDstFormatSupported( dstFormat, (_vkLogicalDevice != Zero) ))
return false;
return _IsSupported( ppln._shader.data );
}
/*
=================================================
_IsSupported
=================================================
*/
bool VPipelineCompiler::_IsSupported (const ShaderDataMap_t &shaderDataMap)
{
ASSERT( not shaderDataMap.empty() );
bool is_supported = false;
for (auto& data : shaderDataMap)
{
if ( data.second.index() and IsSrcFormatSupported( data.first ))
{
is_supported = true;
break;
}
}
return is_supported;
}
/*
=================================================
MergeShaderAccess
=================================================
*/
static void MergeShaderAccess (const EResourceState srcAccess, INOUT EResourceState &dstAccess)
{
if ( srcAccess == dstAccess )
return;
dstAccess |= srcAccess;
if ( AllBits( dstAccess, EResourceState::InvalidateBefore ) and
AllBits( dstAccess, EResourceState::ShaderRead ))
{
dstAccess &= ~EResourceState::InvalidateBefore;
}
}
/*
=================================================
MergeUniformData
=================================================
*/
ND_ static bool MergeUniformData (const PipelineDescription::Uniform &src, INOUT PipelineDescription::Uniform &dst)
{
return Visit( src.data,
[&] (const PipelineDescription::Texture &lhs)
{
if ( auto* rhs = UnionGetIf<PipelineDescription::Texture>( &dst.data ))
{
ASSERT( lhs.textureType == rhs->textureType );
ASSERT( src.index == dst.index );
if ( lhs.textureType == rhs->textureType and
src.index == dst.index )
{
dst.stageFlags |= src.stageFlags;
rhs->state |= EResourceState_FromShaders( dst.stageFlags );
return true;
}
}
return false;
},
[&] (const PipelineDescription::Sampler &)
{
if ( auto* rhs = UnionGetIf<PipelineDescription::Sampler>( &dst.data ))
{
ASSERT( src.index == dst.index );
if ( src.index == dst.index )
{
dst.stageFlags |= src.stageFlags;
return true;
}
}
return false;
},
[&] (const PipelineDescription::SubpassInput &lhs)
{
if ( auto* rhs = UnionGetIf<PipelineDescription::SubpassInput>( &dst.data ))
{
ASSERT( lhs.attachmentIndex == rhs->attachmentIndex );
ASSERT( lhs.isMultisample == rhs->isMultisample );
ASSERT( src.index == dst.index );
if ( lhs.attachmentIndex == rhs->attachmentIndex and
lhs.isMultisample == rhs->isMultisample and
src.index == dst.index )
{
dst.stageFlags |= src.stageFlags;
rhs->state |= EResourceState_FromShaders( dst.stageFlags );
return true;
}
}
return false;
},
[&] (const PipelineDescription::Image &lhs)
{
if ( auto* rhs = UnionGetIf<PipelineDescription::Image>( &dst.data ))
{
ASSERT( lhs.imageType == rhs->imageType );
ASSERT( src.index == dst.index );
if ( lhs.imageType == rhs->imageType and
src.index == dst.index )
{
MergeShaderAccess( lhs.state, INOUT rhs->state );
dst.stageFlags |= src.stageFlags;
rhs->state |= EResourceState_FromShaders( dst.stageFlags );
return true;
}
}
return false;
},
[&] (const PipelineDescription::UniformBuffer &lhs)
{
if ( auto* rhs = UnionGetIf<PipelineDescription::UniformBuffer>( &dst.data ))
{
ASSERT( lhs.size == rhs->size );
ASSERT( src.index == dst.index );
if ( lhs.size == rhs->size and
src.index == dst.index )
{
dst.stageFlags |= src.stageFlags;
rhs->state |= EResourceState_FromShaders( dst.stageFlags );
return true;
}
}
return false;
},
[&] (const PipelineDescription::StorageBuffer &lhs)
{
if ( auto* rhs = UnionGetIf<PipelineDescription::StorageBuffer>( &dst.data ))
{
ASSERT( lhs.staticSize == rhs->staticSize );
ASSERT( lhs.arrayStride == rhs->arrayStride );
ASSERT( src.index == dst.index );
if ( lhs.staticSize == rhs->staticSize and
lhs.arrayStride == rhs->arrayStride and
src.index == dst.index )
{
MergeShaderAccess( lhs.state, INOUT rhs->state );
dst.stageFlags |= src.stageFlags;
rhs->state |= EResourceState_FromShaders( dst.stageFlags );
return true;
}
}
return false;
},
[&] (const PipelineDescription::RayTracingScene &lhs)
{
if ( auto* rhs = UnionGetIf<PipelineDescription::RayTracingScene>( &dst.data ))
{
ASSERT( lhs.state == rhs->state );
if ( lhs.state == rhs->state )
{
dst.stageFlags |= src.stageFlags;
return true;
}
}
return false;
},
[] (const NullUnion &) { return false; }
);
}
/*
=================================================
_MergeUniforms
=================================================
*/
bool VPipelineCompiler::_MergeUniforms (const PipelineDescription::UniformMap_t &srcUniforms,
INOUT PipelineDescription::UniformMap_t &dstUniforms) const
{
for (auto& un : srcUniforms)
{
auto iter = dstUniforms.find( un.first );
// add new uniform
if ( iter == dstUniforms.end() )
{
dstUniforms.insert( un );
continue;
}
if ( un.second.index.VKBinding() != iter->second.index.VKBinding() )
{
for (uint i = 0; i < 100; ++i)
{
# ifdef FG_OPTIMIZE_IDS
UniformID id { "un" + ToString(i) }; // TODO
# else
UniformID id { String(un.first.GetName()) + "_" + ToString(i) };
# endif
if ( dstUniforms.count( id ) == 0 and srcUniforms.count( id ) == 0 )
{
dstUniforms.insert_or_assign( id, un.second );
break;
}
}
}
else
{
COMP_CHECK_ERR( MergeUniformData( un.second, INOUT iter->second ));
}
}
return true;
}
/*
=================================================
_MergePipelineResources
=================================================
*/
bool VPipelineCompiler::_MergePipelineResources (const PipelineDescription::PipelineLayout &srcLayout,
INOUT PipelineDescription::PipelineLayout &dstLayout) const
{
// merge descriptor sets
for (auto& src_ds : srcLayout.descriptorSets)
{
bool found = false;
for (auto& dst_ds : dstLayout.descriptorSets)
{
// merge
if ( src_ds.id == dst_ds.id )
{
COMP_CHECK_ERR( src_ds.bindingIndex == dst_ds.bindingIndex );
COMP_CHECK_ERR( _MergeUniforms( *src_ds.uniforms, INOUT const_cast<PipelineDescription::UniformMap_t &>(*dst_ds.uniforms) ));
found = true;
break;
}
}
// add new descriptor set
if ( not found )
dstLayout.descriptorSets.push_back( src_ds );
}
// merge push constants
for (auto& src_pc : srcLayout.pushConstants)
{
// Vulkan valid usage:
// * offset must be a multiple of 4
// * size must be a multiple of 4
// https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#VUID-vkCmdPushConstants-offset-00368
COMP_CHECK_ERR( src_pc.second.offset % 4 == 0 );
COMP_CHECK_ERR( src_pc.second.size % 4 == 0 );
auto iter = dstLayout.pushConstants.find( src_pc.first );
if ( iter == dstLayout.pushConstants.end() )
{
// check intersections
for (auto& dst_pc : dstLayout.pushConstants)
{
// Vulkan valid usage:
// * Any two elements of pPushConstantRanges must not include the same stage in stageFlags
// https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#VUID-VkPipelineLayoutCreateInfo-pPushConstantRanges-00292
if ( IsIntersects( src_pc.second.offset, src_pc.second.offset + src_pc.second.size,
dst_pc.second.offset, dst_pc.second.offset + dst_pc.second.size ))
{
// It is forbidden because FG can't handle this case.
COMP_RETURN_ERR( "Push constants with different names uses same memory range:\n"s <<
" First (" << ToString( src_pc.first ) << "): " << ToString(src_pc.second.offset) << " .. " << ToString(src_pc.second.offset + src_pc.second.size) << '\n' <<
" Second (" << ToString( dst_pc.first ) << "): " << ToString(dst_pc.second.offset) << " .. " << ToString(dst_pc.second.offset + dst_pc.second.size) );
}
}
// add new push constant
dstLayout.pushConstants.insert( src_pc );
continue;
}
// merge
iter->second.size = Max( src_pc.second.offset + src_pc.second.size, iter->second.offset + iter->second.size );
iter->second.offset = Min( src_pc.second.offset, iter->second.offset );
iter->second.size -= iter->second.offset;
iter->second.stageFlags |= src_pc.second.stageFlags;
// same as above
COMP_CHECK_ERR( iter->second.offset % 4 == 0 );
COMP_CHECK_ERR( iter->second.size % 4 == 0 );
}
return true;
}
/*
=================================================
MergePrimitiveTopology
=================================================
*/
static void MergePrimitiveTopology (const GraphicsPipelineDesc::TopologyBits_t &src, INOUT GraphicsPipelineDesc::TopologyBits_t &dst)
{
for (size_t i = 0; i < src.size(); ++i)
{
if ( src.test( i ))
dst.set( i );
}
}
/*
=================================================
ValidatePrimitiveTopology
=================================================
*/
static void ValidatePrimitiveTopology (INOUT GraphicsPipelineDesc::TopologyBits_t &topology)
{
if ( topology.test(uint(EPrimitive::Patch)) )
{
topology.reset().set(uint(EPrimitive::Patch));
return;
}
if ( topology.test(uint(EPrimitive::TriangleListAdjacency)) or
topology.test(uint(EPrimitive::TriangleStripAdjacency)) )
{
topology.reset().set(uint(EPrimitive::TriangleListAdjacency))
.set(uint(EPrimitive::TriangleStripAdjacency));
return;
}
if ( topology.test(uint(EPrimitive::LineListAdjacency)) or
topology.test(uint(EPrimitive::LineStripAdjacency)) )
{
topology.reset().set(uint(EPrimitive::LineListAdjacency))
.set(uint(EPrimitive::LineStripAdjacency));
return;
}
if ( topology.none() )
{
topology.reset().set(uint(EPrimitive::Point))
.set(uint(EPrimitive::LineList))
.set(uint(EPrimitive::LineStrip))
.set(uint(EPrimitive::TriangleList))
.set(uint(EPrimitive::TriangleStrip))
.set(uint(EPrimitive::TriangleFan));
return;
}
}
/*
=================================================
UpdateBufferDynamicOffsets
=================================================
*/
static void UpdateBufferDynamicOffsets (INOUT PipelineDescription::DescriptorSets_t &descriptorSets)
{
FixedArray< PipelineDescription::Uniform *, FG_MaxBufferDynamicOffsets > sorted;
for (auto& ds_layout : descriptorSets)
{
for (auto& un : *ds_layout.uniforms)
{
if ( auto* ubuf = UnionGetIf< PipelineDescription::UniformBuffer >( &un.second.data );
ubuf and AllBits( ubuf->state, EResourceState::_BufferDynamicOffset ))
{
sorted.push_back( const_cast< PipelineDescription::Uniform *>( &un.second ));
}
else
if ( auto* sbuf = UnionGetIf< PipelineDescription::StorageBuffer >( &un.second.data );
sbuf and AllBits( sbuf->state, EResourceState::_BufferDynamicOffset ))
{
sorted.push_back( const_cast< PipelineDescription::Uniform *>( &un.second ));
}
}
}
std::sort( sorted.begin(), sorted.end(), [](auto& lhs, auto& rhs) { return lhs->index.VKBinding() < rhs->index.VKBinding(); });
uint index = 0;
for (auto* un : sorted)
{
if ( auto* ubuf = UnionGetIf< PipelineDescription::UniformBuffer >( &un->data ))
ubuf->dynamicOffsetIndex = index++;
else
if ( auto* sbuf = UnionGetIf< PipelineDescription::StorageBuffer >( &un->data ))
sbuf->dynamicOffsetIndex = index++;
}
CHECK( index <= FG_MaxBufferDynamicOffsets );
}
/*
=================================================
FindHighPriorityShaderFormat
=================================================
*/
ND_ static auto FindHighPriorityShaderFormat (const PipelineDescription::ShaderDataMap_t &shaderData, EShaderLangFormat dstFormat)
{
auto result = shaderData.find( dstFormat );
if ( result != shaderData.end() )
return result;
// limit max vulkan version with considering of 'dstFormat'
const EShaderLangFormat dst_vulkan_ver = (dstFormat & EShaderLangFormat::_ApiMask) == EShaderLangFormat::Vulkan ?
(dstFormat & EShaderLangFormat::_VersionMask) :
EShaderLangFormat::_VersionMask;
// search nearest shader format
for (auto iter = shaderData.begin(); iter != shaderData.end(); ++iter)
{
if ( not IsSrcFormatSupported( iter->first ))
continue;
// vulkan has most priority than opengl
const bool current_is_vulkan = (result != shaderData.end()) and (result->first & EShaderLangFormat::_ApiMask) == EShaderLangFormat::Vulkan;
const bool pending_is_vulkan = (iter->first & EShaderLangFormat::_ApiMask) == EShaderLangFormat::Vulkan;
const EShaderLangFormat current_ver = (result != shaderData.end()) ? (result->first & EShaderLangFormat::_VersionMask) : EShaderLangFormat::Unknown;
const EShaderLangFormat pending_ver = (iter->first & EShaderLangFormat::_VersionMask);
if ( current_is_vulkan )
{
if ( not pending_is_vulkan )
continue;
// compare vulkan versions
if ( pending_ver > current_ver and pending_ver <= dst_vulkan_ver )
result = iter;
continue;
}
if ( pending_is_vulkan )
{
if ( pending_ver <= dst_vulkan_ver )
result = iter;
continue;
}
// compare opengl versions
if ( pending_ver > current_ver )
result = iter;
}
return result;
}
/*
=================================================
Compile
=================================================
*/
bool VPipelineCompiler::Compile (INOUT MeshPipelineDesc &ppln, EShaderLangFormat dstFormat)
{
EXLOCK( _lock );
ASSERT( IsSupported( ppln, dstFormat ));
const bool create_module = ((dstFormat & EShaderLangFormat::_StorageFormatMask) == EShaderLangFormat::ShaderModule);
const EShaderLangFormat spirv_format = not create_module ? dstFormat :
((dstFormat & ~EShaderLangFormat::_StorageFormatMask) | EShaderLangFormat::SPIRV);
MeshPipelineDesc new_ppln;
for (const auto& shader : ppln._shaders)
{
ASSERT( not shader.second.data.empty() );
auto iter = FindHighPriorityShaderFormat( shader.second.data, spirv_format );
if ( iter == shader.second.data.end() )
RETURN_ERR( "no suitable shader format found!" );
// compile glsl
if ( auto* shader_data = UnionGetIf< StringShaderData >( &iter->second ))
{
SpirvCompiler::ShaderReflection reflection;
String log;
PipelineDescription::Shader new_shader;
if ( not _spirvCompiler->Compile( shader.first, iter->first, spirv_format, (*shader_data)->GetEntry(),
(*shader_data)->GetData(), (*shader_data)->GetDebugName(),
OUT new_shader, OUT reflection, OUT log ))
{
COMP_RETURN_ERR( log );
}
if ( create_module )
COMP_CHECK_ERR( _CreateVulkanShader( INOUT new_shader ));
COMP_CHECK_ERR( _MergePipelineResources( reflection.layout, INOUT new_ppln._pipelineLayout ));
switch ( shader.first )
{
case EShader::MeshTask :
new_ppln._defaultTaskGroupSize = reflection.mesh.taskGroupSize;
new_ppln._taskSizeSpec = reflection.mesh.taskGroupSpecialization;
break;
case EShader::Mesh :
new_ppln._maxIndices = reflection.mesh.maxIndices;
new_ppln._maxVertices = reflection.mesh.maxVertices;
new_ppln._topology = reflection.mesh.topology;
new_ppln._defaultMeshGroupSize = reflection.mesh.meshGroupSize;
new_ppln._meshSizeSpec = reflection.mesh.meshGroupSpecialization;
break;
case EShader::Fragment :
new_ppln._fragmentOutput = reflection.fragment.fragmentOutput;
new_ppln._earlyFragmentTests = reflection.fragment.earlyFragmentTests;
break;
default :
RETURN_ERR( "unknown shader type!" );
}
new_ppln._shaders.insert_or_assign( shader.first, std::move(new_shader) );
}
else
{
COMP_RETURN_ERR( "invalid shader data type!" );
}
}
UpdateBufferDynamicOffsets( new_ppln._pipelineLayout.descriptorSets );
std::swap( ppln, new_ppln );
_CheckHashCollision( ppln );
ASSERT( _CheckDescriptorBindings( ppln ));
return true;
}
/*
=================================================
Compile
=================================================
*/
bool VPipelineCompiler::Compile (INOUT RayTracingPipelineDesc &ppln, EShaderLangFormat dstFormat)
{
EXLOCK( _lock );
ASSERT( IsSupported( ppln, dstFormat ));
const bool create_module = ((dstFormat & EShaderLangFormat::_StorageFormatMask) == EShaderLangFormat::ShaderModule);
const EShaderLangFormat spirv_format = not create_module ? dstFormat :
((dstFormat & ~EShaderLangFormat::_StorageFormatMask) | EShaderLangFormat::SPIRV);
RayTracingPipelineDesc new_ppln;
for (const auto& shader : ppln._shaders)
{
ASSERT( not shader.second.data.empty() );
auto iter = FindHighPriorityShaderFormat( shader.second.data, spirv_format );
if ( iter == shader.second.data.end() )
RETURN_ERR( "no suitable shader format found!" );
// compile glsl
if ( auto* shader_data = UnionGetIf< StringShaderData >( &iter->second ))
{
SpirvCompiler::ShaderReflection reflection;
String log;
RayTracingPipelineDesc::RTShader new_shader;
if ( not _spirvCompiler->Compile( shader.second.shaderType, iter->first, spirv_format, (*shader_data)->GetEntry(),
(*shader_data)->GetData(), (*shader_data)->GetDebugName(),
OUT new_shader, OUT reflection, OUT log ))
{
COMP_RETURN_ERR( log );
}
if ( create_module )
COMP_CHECK_ERR( _CreateVulkanShader( INOUT new_shader ));
COMP_CHECK_ERR( _MergePipelineResources( reflection.layout, INOUT new_ppln._pipelineLayout ));
switch ( shader.second.shaderType )
{
case EShader::RayGen :
case EShader::RayAnyHit :
case EShader::RayClosestHit :
case EShader::RayMiss :
case EShader::RayIntersection :
case EShader::RayCallable :
break; // TODO
default :
RETURN_ERR( "unknown shader type!" );
}
new_shader.shaderType = shader.second.shaderType;
new_ppln._shaders.insert_or_assign( shader.first, std::move(new_shader) );
}
else
{
COMP_RETURN_ERR( "invalid shader data type!" );
}
}
UpdateBufferDynamicOffsets( new_ppln._pipelineLayout.descriptorSets );
std::swap( ppln, new_ppln );
_CheckHashCollision( ppln );
ASSERT( _CheckDescriptorBindings( ppln ));
return true;
}
/*
=================================================
Compile
=================================================
*/
bool VPipelineCompiler::Compile (INOUT GraphicsPipelineDesc &ppln, EShaderLangFormat dstFormat)
{
EXLOCK( _lock );
ASSERT( IsSupported( ppln, dstFormat ));
const bool create_module = ((dstFormat & EShaderLangFormat::_StorageFormatMask) == EShaderLangFormat::ShaderModule);
const EShaderLangFormat spirv_format = not create_module ? dstFormat :
((dstFormat & ~EShaderLangFormat::_StorageFormatMask) | EShaderLangFormat::SPIRV);
GraphicsPipelineDesc new_ppln;
for (const auto& shader : ppln._shaders)
{
ASSERT( not shader.second.data.empty() );
auto iter = FindHighPriorityShaderFormat( shader.second.data, spirv_format );
if ( iter == shader.second.data.end() )
RETURN_ERR( "no suitable shader format found!" );
// compile glsl
if ( auto* shader_data = UnionGetIf< StringShaderData >( &iter->second ))
{
SpirvCompiler::ShaderReflection reflection;