forked from DiligentGraphics/DiligentCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipelineStateVkImpl.cpp
More file actions
1426 lines (1233 loc) · 78.3 KB
/
PipelineStateVkImpl.cpp
File metadata and controls
1426 lines (1233 loc) · 78.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2019-2026 Diligent Graphics LLC
* Copyright 2015-2019 Egor Yusov
*
* 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.
*
* In no event and under no legal theory, whether in tort (including negligence),
* contract, or otherwise, unless required by applicable law (such as deliberate
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
* liable for any damages, including any direct, indirect, special, incidental,
* or consequential damages of any character arising as a result of this License or
* out of the use or inability to use the software (including but not limited to damages
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
* all other commercial damages or losses), even if such Contributor has been advised
* of the possibility of such damages.
*/
#include "pch.h"
#include "PipelineStateVkImpl.hpp"
#include <array>
#include <unordered_map>
#include "RenderDeviceVkImpl.hpp"
#include "DeviceContextVkImpl.hpp"
#include "ShaderVkImpl.hpp"
#include "RenderPassVkImpl.hpp"
#include "ShaderResourceBindingVkImpl.hpp"
#include "PipelineStateCacheVkImpl.hpp"
#include "VulkanTypeConversions.hpp"
#include "EngineMemory.h"
#include "StringTools.hpp"
#if !DILIGENT_NO_HLSL
# include "SPIRVTools.hpp"
#endif
namespace Diligent
{
constexpr INTERFACE_ID PipelineStateVkImpl::IID_InternalImpl;
namespace
{
void InitPipelineShaderStages(const VulkanUtilities::LogicalDevice& LogicalDevice,
const PipelineStateVkImpl::TShaderStages& ShaderStages,
std::vector<VulkanUtilities::ShaderModuleWrapper>& ShaderModules,
std::vector<VkPipelineShaderStageCreateInfo>& Stages)
{
for (const PipelineStateVkImpl::ShaderStageInfo& Stage : ShaderStages)
{
VkPipelineShaderStageCreateInfo StageCI{};
StageCI.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
StageCI.pNext = nullptr;
StageCI.flags = 0; // reserved for future use
StageCI.stage = ShaderTypeToVkShaderStageFlagBit(Stage.Type);
VkShaderModuleCreateInfo ShaderModuleCI{};
ShaderModuleCI.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
ShaderModuleCI.pNext = nullptr;
ShaderModuleCI.flags = 0;
for (const PipelineStateVkImpl::ShaderStageInfo::Item& StageItem : Stage.Items)
{
const ShaderVkImpl* pShader = StageItem.pShader;
const std::vector<uint32_t>& SPIRV = StageItem.SPIRV;
ShaderModuleCI.codeSize = SPIRV.size() * sizeof(uint32_t);
ShaderModuleCI.pCode = SPIRV.data();
ShaderModules.push_back(LogicalDevice.CreateShaderModule(ShaderModuleCI, pShader->GetDesc().Name));
StageCI.module = ShaderModules.back();
StageCI.pName = pShader->GetEntryPoint();
StageCI.pSpecializationInfo = nullptr;
Stages.push_back(StageCI);
}
}
VERIFY_EXPR(ShaderModules.size() == Stages.size());
}
// Per-shader-stage specialization constant data for Vulkan pipeline creation.
// Holds VkSpecializationMapEntry array, contiguous data blob, and the
// VkSpecializationInfo that references them. Lifetime must exceed the
// vkCreate*Pipelines call.
struct ShaderStageSpecializationData
{
std::vector<VkSpecializationMapEntry> MapEntries;
std::vector<Uint8> DataBlob;
VkSpecializationInfo Info{};
};
// Iterates SPIR-V reflected specialization constants and matches them to
// user-provided SpecializationConstant entries by name. If a reflected
// constant has no matching user entry the constant is silently skipped,
// which allows the user to supply a superset of constants shared across
// multiple pipelines / stages.
//
// Parameters:
// ShaderStages - shader stages extracted from the PSO create info
// NumSpecializationConstants - number of user-provided specialization constants
// pSpecializationConstants - user-provided specialization constant array
// PSODesc - pipeline state description (for error messages)
// vkStages [in/out] - VkPipelineShaderStageCreateInfo array to patch
// SpecDataPerStage [out] - per-stage specialization data (must outlive vkCreate*Pipelines)
//
// Throws on size mismatch between user-provided and reflected constants.
void BuildSpecializationData(const PipelineStateVkImpl::TShaderStages& ShaderStages,
Uint32 NumSpecializationConstants,
const SpecializationConstant* pSpecializationConstants,
const PipelineStateDesc& PSODesc,
std::vector<VkPipelineShaderStageCreateInfo>& vkStages,
std::vector<ShaderStageSpecializationData>& SpecDataPerStage)
{
if (NumSpecializationConstants == 0 || pSpecializationConstants == nullptr)
return;
// vkStages has one entry per ShaderStageInfo::Item across all stages.
// We build one ShaderStageSpecializationData per vkStages entry.
SpecDataPerStage.resize(vkStages.size());
Uint32 vkStageIdx = 0;
for (const PipelineStateVkImpl::ShaderStageInfo& Stage : ShaderStages)
{
for (const PipelineStateVkImpl::ShaderStageInfo::Item& StageItem : Stage.Items)
{
VERIFY_EXPR(vkStageIdx < vkStages.size());
const ShaderVkImpl* pShader = StageItem.pShader;
const SPIRVShaderResources* pResources = pShader->GetShaderResources().get();
ShaderStageSpecializationData& StageData = SpecDataPerStage[vkStageIdx];
for (Uint32 r = 0; r < pResources->GetNumSpecConstants(); ++r)
{
const SPIRVSpecializationConstantAttribs& Reflected = pResources->GetSpecConstant(r);
// Search for a matching user-provided constant by name and stage flag.
const SpecializationConstant* pUserConst = nullptr;
for (Uint32 sc = 0; sc < NumSpecializationConstants; ++sc)
{
const SpecializationConstant& Candidate = pSpecializationConstants[sc];
if ((Candidate.ShaderStages & Stage.Type) != 0 &&
strcmp(Candidate.Name, Reflected.Name) == 0)
{
pUserConst = &Candidate;
break;
}
}
// No user constant for this reflected entry -- skip silently.
if (pUserConst == nullptr)
continue;
// The user may provide more data than the shader needs (e.g. when
// sharing a constant array across pipelines with different types).
// Only reject the case where the user provides less data than required.
if (pUserConst->Size < Reflected.Size)
{
LOG_ERROR_AND_THROW("Description of ", GetPipelineTypeString(PSODesc.PipelineType),
" PSO '", (PSODesc.Name != nullptr ? PSODesc.Name : ""),
"' is invalid: specialization constant '", pUserConst->Name,
"' in ", GetShaderTypeLiteralName(Stage.Type),
" shader '", pShader->GetDesc().Name,
"' has insufficient data: user provided ", pUserConst->Size,
" bytes, but the shader declares ",
GetShaderCodeBasicTypeString(Reflected.BasicType),
" (", Reflected.Size, " bytes).");
}
// Use the reflected size -- it is the actual size the shader expects.
const Uint32 ConstSize = Reflected.Size;
// Build the map entry.
VkSpecializationMapEntry Entry{};
Entry.constantID = Reflected.SpecId;
Entry.offset = static_cast<uint32_t>(StageData.DataBlob.size());
Entry.size = ConstSize;
StageData.MapEntries.push_back(Entry);
// Append data to the blob (only the bytes the shader needs).
const Uint8* pSrcData = static_cast<const Uint8*>(pUserConst->pData);
StageData.DataBlob.insert(StageData.DataBlob.end(), pSrcData, pSrcData + ConstSize);
}
// Populate VkSpecializationInfo if any entries were matched.
if (!StageData.MapEntries.empty())
{
StageData.Info.mapEntryCount = static_cast<uint32_t>(StageData.MapEntries.size());
StageData.Info.pMapEntries = StageData.MapEntries.data();
StageData.Info.dataSize = StageData.DataBlob.size();
StageData.Info.pData = StageData.DataBlob.data();
vkStages[vkStageIdx].pSpecializationInfo = &StageData.Info;
}
++vkStageIdx;
}
}
VERIFY_EXPR(vkStageIdx == vkStages.size());
}
void CreateComputePipeline(RenderDeviceVkImpl* pDeviceVk,
std::vector<VkPipelineShaderStageCreateInfo>& Stages,
const PipelineLayoutVk& Layout,
const PipelineStateDesc& PSODesc,
VulkanUtilities::PipelineWrapper& Pipeline,
VkPipelineCache vkPSOCache)
{
const VulkanUtilities::LogicalDevice& LogicalDevice = pDeviceVk->GetLogicalDevice();
VkComputePipelineCreateInfo PipelineCI{};
PipelineCI.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
PipelineCI.pNext = nullptr;
#ifdef DILIGENT_DEBUG
PipelineCI.flags = VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT;
#endif
PipelineCI.basePipelineHandle = VK_NULL_HANDLE; // a pipeline to derive from
PipelineCI.basePipelineIndex = -1; // an index into the pCreateInfos parameter to use as a pipeline to derive from
PipelineCI.stage = Stages[0];
PipelineCI.layout = Layout.GetVkPipelineLayout();
Pipeline = LogicalDevice.CreateComputePipeline(PipelineCI, vkPSOCache, PSODesc.Name);
}
void CreateGraphicsPipeline(RenderDeviceVkImpl* pDeviceVk,
std::vector<VkPipelineShaderStageCreateInfo>& Stages,
const PipelineLayoutVk& Layout,
const PipelineStateDesc& PSODesc,
const GraphicsPipelineDesc& GraphicsPipeline,
VulkanUtilities::PipelineWrapper& Pipeline,
RefCntAutoPtr<IRenderPass>& pRenderPass,
VkPipelineCache vkPSOCache)
{
const VulkanUtilities::LogicalDevice& LogicalDevice = pDeviceVk->GetLogicalDevice();
const VulkanUtilities::PhysicalDevice& PhysicalDevice = pDeviceVk->GetPhysicalDevice();
VkGraphicsPipelineCreateInfo PipelineCI{};
PipelineCI.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
PipelineCI.pNext = nullptr;
#ifdef DILIGENT_DEBUG
PipelineCI.flags = VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT;
#endif
VkPipelineRenderingCreateInfoKHR PipelineRenderingCI{};
std::vector<VkFormat> ColorAttachmentFormats;
if (pRenderPass == nullptr)
{
if (RenderPassCache* RPCache = pDeviceVk->GetImplicitRenderPassCache())
{
RenderPassCache::RenderPassCacheKey Key{
GraphicsPipeline.NumRenderTargets,
GraphicsPipeline.SmplDesc.Count,
GraphicsPipeline.RTVFormats,
GraphicsPipeline.DSVFormat,
(GraphicsPipeline.ShadingRateFlags & PIPELINE_SHADING_RATE_FLAG_TEXTURE_BASED) != 0,
GraphicsPipeline.ReadOnlyDSV};
pRenderPass = RPCache->GetRenderPass(Key);
if (pRenderPass == nullptr)
LOG_ERROR_AND_THROW("Failed to create default render pass.");
}
else
{
// VK_KHR_dynamic_rendering
PipelineRenderingCI = GraphicsPipelineDesc_To_VkPipelineRenderingCreateInfo(GraphicsPipeline, ColorAttachmentFormats);
if ((GraphicsPipeline.ShadingRateFlags & PIPELINE_SHADING_RATE_FLAG_TEXTURE_BASED) != 0)
{
PipelineCI.flags |= VK_PIPELINE_CREATE_RENDERING_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR;
}
}
}
PipelineCI.stageCount = static_cast<Uint32>(Stages.size());
PipelineCI.pStages = Stages.data();
PipelineCI.layout = Layout.GetVkPipelineLayout();
VkPipelineVertexInputStateCreateInfo VertexInputStateCI = {};
VkPipelineVertexInputDivisorStateCreateInfoEXT VertexInputDivisorCI = {};
std::array<VkVertexInputBindingDescription, MAX_LAYOUT_ELEMENTS> BindingDescriptions;
std::array<VkVertexInputAttributeDescription, MAX_LAYOUT_ELEMENTS> AttributeDescription;
std::array<VkVertexInputBindingDivisorDescriptionEXT, MAX_LAYOUT_ELEMENTS> VertexBindingDivisors;
InputLayoutDesc_To_VkVertexInputStateCI(GraphicsPipeline.InputLayout, VertexInputStateCI, VertexInputDivisorCI, BindingDescriptions, AttributeDescription, VertexBindingDivisors);
PipelineCI.pVertexInputState = &VertexInputStateCI;
if (VertexInputDivisorCI.vertexBindingDivisorCount > 0)
{
if (!pDeviceVk->GetFeatures().InstanceDataStepRate)
LOG_ERROR_MESSAGE("InstanceDataStepRate device feature is not enabled");
VertexInputStateCI.pNext = &VertexInputDivisorCI;
}
VkPipelineInputAssemblyStateCreateInfo InputAssemblyCI{};
InputAssemblyCI.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
InputAssemblyCI.pNext = nullptr;
InputAssemblyCI.flags = 0; // reserved for future use
InputAssemblyCI.primitiveRestartEnable =
(GraphicsPipeline.PrimitiveTopology == PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP ||
GraphicsPipeline.PrimitiveTopology == PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_ADJ ||
GraphicsPipeline.PrimitiveTopology == PRIMITIVE_TOPOLOGY_LINE_STRIP ||
GraphicsPipeline.PrimitiveTopology == PRIMITIVE_TOPOLOGY_LINE_STRIP_ADJ) ?
VK_TRUE :
VK_FALSE;
PipelineCI.pInputAssemblyState = &InputAssemblyCI;
VkPipelineTessellationStateCreateInfo TessStateCI{};
TessStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO;
TessStateCI.pNext = nullptr;
TessStateCI.flags = 0; // reserved for future use
PipelineCI.pTessellationState = &TessStateCI;
if (PSODesc.PipelineType == PIPELINE_TYPE_MESH)
{
// Input assembly is not used in the mesh pipeline, so topology may contain any value.
// Validation layers may generate a warning if point_list topology is used, so use MAX_ENUM value.
InputAssemblyCI.topology = VK_PRIMITIVE_TOPOLOGY_MAX_ENUM;
// Vertex input state and tessellation state are ignored in a mesh pipeline and should be null,
// but there is a bug in validation layers that makes them crash.
//PipelineCI.pVertexInputState = nullptr;
PipelineCI.pTessellationState = nullptr;
}
else
{
PrimitiveTopology_To_VkPrimitiveTopologyAndPatchCPCount(GraphicsPipeline.PrimitiveTopology, InputAssemblyCI.topology, TessStateCI.patchControlPoints);
}
VkPipelineViewportStateCreateInfo ViewPortStateCI{};
ViewPortStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
ViewPortStateCI.pNext = nullptr;
ViewPortStateCI.flags = 0; // reserved for future use
ViewPortStateCI.viewportCount =
GraphicsPipeline.NumViewports; // Even though we use dynamic viewports, the number of viewports used
// by the pipeline is still specified by the viewportCount member (23.5)
ViewPortStateCI.pViewports = nullptr; // We will be using dynamic viewport & scissor states
ViewPortStateCI.scissorCount = ViewPortStateCI.viewportCount; // the number of scissors must match the number of viewports (23.5)
// (why the hell it is in the struct then?)
VkRect2D ScissorRect = {};
if (GraphicsPipeline.RasterizerDesc.ScissorEnable)
{
ViewPortStateCI.pScissors = nullptr; // Ignored if the scissor state is dynamic
}
else
{
const VkPhysicalDeviceProperties& Props = PhysicalDevice.GetProperties();
// There are limitations on the viewport width and height (23.5), but
// it is not clear if there are limitations on the scissor rect width and
// height
ScissorRect.extent.width = Props.limits.maxViewportDimensions[0];
ScissorRect.extent.height = Props.limits.maxViewportDimensions[1];
ViewPortStateCI.pScissors = &ScissorRect;
}
PipelineCI.pViewportState = &ViewPortStateCI;
VkPipelineRasterizationStateCreateInfo RasterizerStateCI =
RasterizerStateDesc_To_VkRasterizationStateCI(GraphicsPipeline.RasterizerDesc);
PipelineCI.pRasterizationState = &RasterizerStateCI;
// Multisample state (24)
VkPipelineMultisampleStateCreateInfo MSStateCI{};
MSStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
MSStateCI.pNext = nullptr;
MSStateCI.flags = 0; // reserved for future use
// If subpass uses color and/or depth/stencil attachments, then the rasterizationSamples member of
// pMultisampleState must be the same as the sample count for those subpass attachments
MSStateCI.rasterizationSamples = static_cast<VkSampleCountFlagBits>(GraphicsPipeline.SmplDesc.Count);
MSStateCI.sampleShadingEnable = VK_FALSE;
MSStateCI.minSampleShading = 0; // a minimum fraction of sample shading if sampleShadingEnable is set to VK_TRUE.
uint32_t SampleMask[] = {GraphicsPipeline.SampleMask, 0}; // Vulkan spec allows up to 64 samples
MSStateCI.pSampleMask = SampleMask; // an array of static coverage information that is ANDed with
// the coverage information generated during rasterization (25.3)
MSStateCI.alphaToCoverageEnable = VK_FALSE; // whether a temporary coverage value is generated based on
// the alpha component of the fragment's first color output
MSStateCI.alphaToOneEnable = VK_FALSE; // whether the alpha component of the fragment's first color output is replaced with one
PipelineCI.pMultisampleState = &MSStateCI;
VkPipelineDepthStencilStateCreateInfo DepthStencilStateCI =
DepthStencilStateDesc_To_VkDepthStencilStateCI(GraphicsPipeline.DepthStencilDesc);
PipelineCI.pDepthStencilState = &DepthStencilStateCI;
Uint32 NumRTAttachments = 0;
if (pRenderPass != nullptr)
{
const RenderPassDesc& RPDesc = pRenderPass->GetDesc();
NumRTAttachments = RPDesc.pSubpasses[GraphicsPipeline.SubpassIndex].RenderTargetAttachmentCount;
VERIFY_EXPR(GraphicsPipeline.pRenderPass != nullptr || GraphicsPipeline.NumRenderTargets == NumRTAttachments);
}
else
{
NumRTAttachments = PipelineRenderingCI.colorAttachmentCount;
}
std::vector<VkPipelineColorBlendAttachmentState> ColorBlendAttachmentStates(NumRTAttachments);
VkPipelineColorBlendStateCreateInfo BlendStateCI{};
BlendStateCI.pAttachments = !ColorBlendAttachmentStates.empty() ? ColorBlendAttachmentStates.data() : nullptr;
BlendStateCI.attachmentCount = NumRTAttachments; // must equal the colorAttachmentCount for the subpass
// in which this pipeline is used.
BlendStateDesc_To_VkBlendStateCI(GraphicsPipeline.BlendDesc, BlendStateCI, ColorBlendAttachmentStates);
PipelineCI.pColorBlendState = &BlendStateCI;
VkPipelineDynamicStateCreateInfo DynamicStateCI{};
DynamicStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
DynamicStateCI.pNext = nullptr;
DynamicStateCI.flags = 0; // reserved for future use
std::vector<VkDynamicState> DynamicStates =
{
VK_DYNAMIC_STATE_VIEWPORT, // pViewports state in VkPipelineViewportStateCreateInfo will be ignored and must be
// set dynamically with vkCmdSetViewport before any draw commands. The number of viewports
// used by a pipeline is still specified by the viewportCount member of
// VkPipelineViewportStateCreateInfo.
VK_DYNAMIC_STATE_BLEND_CONSTANTS, // blendConstants state in VkPipelineColorBlendStateCreateInfo will be ignored
// and must be set dynamically with vkCmdSetBlendConstants
VK_DYNAMIC_STATE_STENCIL_REFERENCE // specifies that the reference state in VkPipelineDepthStencilStateCreateInfo
// for both front and back will be ignored and must be set dynamically
// with vkCmdSetStencilReference
};
if (GraphicsPipeline.RasterizerDesc.ScissorEnable)
{
// pScissors state in VkPipelineViewportStateCreateInfo will be ignored and must be set
// dynamically with vkCmdSetScissor before any draw commands. The number of scissor rectangles
// used by a pipeline is still specified by the scissorCount member of
// VkPipelineViewportStateCreateInfo.
DynamicStates.push_back(VK_DYNAMIC_STATE_SCISSOR);
}
if (GraphicsPipeline.ShadingRateFlags != PIPELINE_SHADING_RATE_FLAG_NONE &&
pDeviceVk->GetLogicalDevice().GetEnabledExtFeatures().ShadingRate.attachmentFragmentShadingRate != VK_FALSE)
{
// VkPipelineFragmentShadingRateStateCreateInfoKHR will be ignored
// and must be set dynamically with vkCmdSetFragmentShadingRateKHR before any drawing commands.
DynamicStates.push_back(VK_DYNAMIC_STATE_FRAGMENT_SHADING_RATE_KHR);
}
DynamicStateCI.dynamicStateCount = static_cast<uint32_t>(DynamicStates.size());
DynamicStateCI.pDynamicStates = DynamicStates.data();
PipelineCI.pDynamicState = &DynamicStateCI;
if (pRenderPass)
{
PipelineCI.renderPass = pRenderPass.RawPtr<IRenderPassVk>()->GetVkRenderPass();
}
else
{
PipelineCI.pNext = &PipelineRenderingCI;
}
PipelineCI.subpass = GraphicsPipeline.SubpassIndex;
PipelineCI.basePipelineHandle = VK_NULL_HANDLE; // a pipeline to derive from
PipelineCI.basePipelineIndex = -1; // an index into the pCreateInfos parameter to use as a pipeline to derive from
Pipeline = LogicalDevice.CreateGraphicsPipeline(PipelineCI, vkPSOCache, PSODesc.Name);
}
void CreateRayTracingPipeline(RenderDeviceVkImpl* pDeviceVk,
const std::vector<VkPipelineShaderStageCreateInfo>& vkStages,
const std::vector<VkRayTracingShaderGroupCreateInfoKHR>& vkShaderGroups,
const PipelineLayoutVk& Layout,
const PipelineStateDesc& PSODesc,
const RayTracingPipelineDesc& RayTracingPipeline,
VulkanUtilities::PipelineWrapper& Pipeline,
VkPipelineCache vkPSOCache)
{
const VulkanUtilities::LogicalDevice& LogicalDevice = pDeviceVk->GetLogicalDevice();
VkRayTracingPipelineCreateInfoKHR PipelineCI{};
PipelineCI.sType = VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR;
PipelineCI.pNext = nullptr;
#ifdef DILIGENT_DEBUG
PipelineCI.flags = VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT;
#endif
PipelineCI.stageCount = static_cast<Uint32>(vkStages.size());
PipelineCI.pStages = vkStages.data();
PipelineCI.groupCount = static_cast<Uint32>(vkShaderGroups.size());
PipelineCI.pGroups = vkShaderGroups.data();
PipelineCI.maxPipelineRayRecursionDepth = RayTracingPipeline.MaxRecursionDepth;
PipelineCI.pLibraryInfo = nullptr;
PipelineCI.pLibraryInterface = nullptr;
PipelineCI.pDynamicState = nullptr;
PipelineCI.layout = Layout.GetVkPipelineLayout();
PipelineCI.basePipelineHandle = VK_NULL_HANDLE; // a pipeline to derive from
PipelineCI.basePipelineIndex = -1; // an index into the pCreateInfos parameter to use as a pipeline to derive from
Pipeline = LogicalDevice.CreateRayTracingPipeline(PipelineCI, vkPSOCache, PSODesc.Name);
}
std::vector<VkRayTracingShaderGroupCreateInfoKHR> BuildRTShaderGroupDescription(
const RayTracingPipelineStateCreateInfo& CreateInfo,
const std::unordered_map<HashMapStringKey, Uint32>& NameToGroupIndex,
const PipelineStateVkImpl::TShaderStages& ShaderStages)
{
// Returns the shader module index in the PSO create info
auto GetShaderModuleIndex = [&ShaderStages](const IShader* pShader) {
if (pShader == nullptr)
return VK_SHADER_UNUSED_KHR;
RefCntAutoPtr<ShaderVkImpl> pShaderVk{const_cast<IShader*>(pShader), ShaderVkImpl::IID_InternalImpl};
VERIFY(pShaderVk, "Unexpected shader object implementation");
const SHADER_TYPE ShaderType = pShaderVk->GetDesc().ShaderType;
// Shader modules are initialized in the same order by InitPipelineShaderStages().
uint32_t idx = 0;
for (const PipelineStateVkImpl::ShaderStageInfo& Stage : ShaderStages)
{
if (ShaderType == Stage.Type)
{
for (Uint32 i = 0; i < Stage.Items.size(); ++i, ++idx)
{
if (Stage.Items[i].pShader == pShaderVk)
return idx;
}
UNEXPECTED("Unable to find shader '", pShaderVk->GetDesc().Name, "' in the shader stage. This should never happen and is a bug.");
return VK_SHADER_UNUSED_KHR;
}
else
{
idx += static_cast<Uint32>(Stage.Count());
}
}
UNEXPECTED("Unable to find corresponding shader stage for shader '", pShaderVk->GetDesc().Name, "'. This should never happen and is a bug.");
return VK_SHADER_UNUSED_KHR;
};
std::vector<VkRayTracingShaderGroupCreateInfoKHR> ShaderGroups;
ShaderGroups.reserve(size_t{CreateInfo.GeneralShaderCount} + size_t{CreateInfo.TriangleHitShaderCount} + size_t{CreateInfo.ProceduralHitShaderCount});
for (Uint32 i = 0; i < CreateInfo.GeneralShaderCount; ++i)
{
const RayTracingGeneralShaderGroup& GeneralShader = CreateInfo.pGeneralShaders[i];
VkRayTracingShaderGroupCreateInfoKHR Group = {};
Group.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR;
Group.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR;
Group.generalShader = GetShaderModuleIndex(GeneralShader.pShader);
Group.closestHitShader = VK_SHADER_UNUSED_KHR;
Group.anyHitShader = VK_SHADER_UNUSED_KHR;
Group.intersectionShader = VK_SHADER_UNUSED_KHR;
#ifdef DILIGENT_DEBUG
{
auto Iter = NameToGroupIndex.find(GeneralShader.Name);
VERIFY(Iter != NameToGroupIndex.end(),
"Can't find general shader '", GeneralShader.Name,
"'. This looks to be a bug as NameToGroupIndex is initialized by "
"CopyRTShaderGroupNames() that processes the same general shaders.");
VERIFY(Iter->second == ShaderGroups.size(),
"General shader group '", GeneralShader.Name, "' index mismatch: (", Iter->second, " != ", ShaderGroups.size(),
"). This looks to be a bug as NameToGroupIndex is initialized by "
"CopyRTShaderGroupNames() that processes the same shaders in the same order.");
}
#endif
ShaderGroups.push_back(Group);
}
for (Uint32 i = 0; i < CreateInfo.TriangleHitShaderCount; ++i)
{
const RayTracingTriangleHitShaderGroup& TriHitShader = CreateInfo.pTriangleHitShaders[i];
VkRayTracingShaderGroupCreateInfoKHR Group{};
Group.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR;
Group.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_KHR;
Group.generalShader = VK_SHADER_UNUSED_KHR;
Group.closestHitShader = GetShaderModuleIndex(TriHitShader.pClosestHitShader);
Group.anyHitShader = GetShaderModuleIndex(TriHitShader.pAnyHitShader);
Group.intersectionShader = VK_SHADER_UNUSED_KHR;
#ifdef DILIGENT_DEBUG
{
auto Iter = NameToGroupIndex.find(TriHitShader.Name);
VERIFY(Iter != NameToGroupIndex.end(),
"Can't find triangle hit group '", TriHitShader.Name,
"'. This looks to be a bug as NameToGroupIndex is initialized by "
"CopyRTShaderGroupNames() that processes the same hit groups.");
VERIFY(Iter->second == ShaderGroups.size(),
"Triangle hit group '", TriHitShader.Name, "' index mismatch: (", Iter->second, " != ", ShaderGroups.size(),
"). This looks to be a bug as NameToGroupIndex is initialized by "
"CopyRTShaderGroupNames() that processes the same hit groups in the same order.");
}
#endif
ShaderGroups.push_back(Group);
}
for (Uint32 i = 0; i < CreateInfo.ProceduralHitShaderCount; ++i)
{
const RayTracingProceduralHitShaderGroup& ProcHitShader = CreateInfo.pProceduralHitShaders[i];
VkRayTracingShaderGroupCreateInfoKHR Group{};
Group.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR;
Group.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_KHR;
Group.generalShader = VK_SHADER_UNUSED_KHR;
Group.intersectionShader = GetShaderModuleIndex(ProcHitShader.pIntersectionShader);
Group.closestHitShader = GetShaderModuleIndex(ProcHitShader.pClosestHitShader);
Group.anyHitShader = GetShaderModuleIndex(ProcHitShader.pAnyHitShader);
#ifdef DILIGENT_DEBUG
{
auto Iter = NameToGroupIndex.find(ProcHitShader.Name);
VERIFY(Iter != NameToGroupIndex.end(),
"Can't find procedural hit group '", ProcHitShader.Name,
"'. This looks to be a bug as NameToGroupIndex is initialized by "
"CopyRTShaderGroupNames() that processes the same hit groups.");
VERIFY(Iter->second == ShaderGroups.size(),
"Procedural hit group '", ProcHitShader.Name, "' index mismatch: (", Iter->second, " != ", ShaderGroups.size(),
"). This looks to be a bug as NameToGroupIndex is initialized by "
"CopyRTShaderGroupNames() that processes the same hit groups in the same order.");
}
#endif
ShaderGroups.push_back(Group);
}
return ShaderGroups;
}
void VerifyResourceMerge(const char* PSOName,
const SPIRVShaderResourceAttribs& ExistingRes,
const SPIRVShaderResourceAttribs& NewResAttribs)
{
#define LOG_RESOURCE_MERGE_ERROR_AND_THROW(PropertyName) \
LOG_ERROR_AND_THROW("Shader variable '", NewResAttribs.Name, \
"' is shared between multiple shaders in pipeline '", (PSOName ? PSOName : ""), \
"', but its " PropertyName " varies. A variable shared between multiple shaders " \
"must be defined identically in all shaders. Either use separate variables for " \
"different shader stages, change resource name or make sure that " PropertyName " is consistent.");
if (ExistingRes.Type != NewResAttribs.Type)
LOG_RESOURCE_MERGE_ERROR_AND_THROW("type");
if (ExistingRes.ResourceDim != NewResAttribs.ResourceDim)
LOG_RESOURCE_MERGE_ERROR_AND_THROW("resource dimension");
if (ExistingRes.ArraySize != NewResAttribs.ArraySize)
LOG_RESOURCE_MERGE_ERROR_AND_THROW("array size");
if (ExistingRes.IsMS != NewResAttribs.IsMS)
LOG_RESOURCE_MERGE_ERROR_AND_THROW("multisample state");
#undef LOG_RESOURCE_MERGE_ERROR_AND_THROW
}
struct MergedPushConstantInfo
{
SHADER_TYPE Stages = SHADER_TYPE_UNKNOWN;
Uint32 Size = 0;
};
using MergedPushConstantMapType = std::unordered_map<std::string_view, MergedPushConstantInfo>;
MergedPushConstantMapType MergePushConstants(const PipelineStateVkImpl::TShaderStages& ShaderStages,
const char* PSOName) noexcept(false)
{
MergedPushConstantMapType MergedPushConstants;
for (const PipelineStateVkImpl::ShaderStageInfo& Stage : ShaderStages)
{
for (const PipelineStateVkImpl::ShaderStageInfo::Item& StageItem : Stage.Items)
{
const SPIRVShaderResources& ShaderResources = *StageItem.pShader->GetShaderResources();
for (Uint32 i = 0; i < ShaderResources.GetNumPushConstants(); ++i)
{
const SPIRVShaderResourceAttribs& PCAttribs = ShaderResources.GetPushConstant(i);
MergedPushConstantInfo& MergedPC = MergedPushConstants[PCAttribs.Name];
const Uint32 BufferSize = PCAttribs.GetConstantBufferSize();
// Combine push constants from all stages
MergedPC.Stages |= Stage.Type;
MergedPC.Size = std::max(MergedPC.Size, BufferSize);
}
}
}
return MergedPushConstants;
}
} // namespace
PipelineStateVkImpl::ShaderStageInfo::Item::Item(const ShaderVkImpl* _pShader) :
pShader{_pShader},
SPIRV{_pShader->GetSPIRV()}
{
}
PipelineStateVkImpl::ShaderStageInfo::ShaderStageInfo(const ShaderVkImpl* pShader) :
Type{pShader->GetDesc().ShaderType},
Items{Item{pShader}}
{}
void PipelineStateVkImpl::ShaderStageInfo::Append(const ShaderVkImpl* pShader)
{
VERIFY_EXPR(pShader != nullptr);
VERIFY(std::find_if(Items.begin(), Items.end(), [pShader](const Item& I) { return I.pShader == pShader; }) == Items.end(),
"Shader '", pShader->GetDesc().Name, "' already exists in the stage. Shaders must be deduplicated.");
const SHADER_TYPE NewShaderType = pShader->GetDesc().ShaderType;
if (Type == SHADER_TYPE_UNKNOWN)
{
VERIFY_EXPR(Items.empty());
Type = NewShaderType;
}
else
{
VERIFY(Type == NewShaderType, "The type (", GetShaderTypeLiteralName(NewShaderType),
") of shader '", pShader->GetDesc().Name, "' being added to the stage is inconsistent with the stage type (",
GetShaderTypeLiteralName(Type), ").");
}
Items.emplace_back(pShader);
}
size_t PipelineStateVkImpl::ShaderStageInfo::Count() const
{
return Items.size();
}
PipelineResourceSignatureDescWrapper PipelineStateVkImpl::GetDefaultResourceSignatureDesc(
const TShaderStages& ShaderStages,
const char* PSOName,
const PipelineResourceLayoutDesc& ResourceLayout,
Uint32 SRBAllocationGranularity) noexcept(false)
{
PipelineResourceSignatureDescWrapper SignDesc{PSOName, ResourceLayout, SRBAllocationGranularity};
DefaultSignatureDescBuilder<SPIRVShaderResourceAttribs> Builder{PSOName, ResourceLayout, VerifyResourceMerge, SignDesc};
MergedPushConstantMapType MergedPushConstants;
for (const ShaderStageInfo& Stage : ShaderStages)
{
for (const ShaderStageInfo::Item& StageItem : Stage.Items)
{
const SPIRVShaderResources& ShaderResources = *StageItem.pShader->GetShaderResources();
ShaderResources.ProcessResources(
[&](const SPIRVShaderResourceAttribs& Attribs, Uint32) //
{
// We can't skip immutable samplers because immutable sampler arrays have to be defined
// as both resource and sampler.
//if (Res.Type == SPIRVShaderResourceAttribs::SeparateSampler &&
// FindImmutableSampler(ResourceLayout.ImmutableSamplers, ResourceLayout.NumImmutableSamplers, Stage.Type, Res.Name,
// ShaderResources.GetCombinedSamplerSuffix()) >= 0)
//{
// // Skip separate immutable samplers - they are not resources
// return;
//}
if (Attribs.ArraySize == 0)
{
LOG_ERROR_AND_THROW("Resource '", Attribs.Name, "' in shader '", ShaderResources.GetShaderName(), "' is a runtime-sized array. ",
"You must use explicit resource signature to specify the array size.");
}
const char* const SamplerSuffix =
(ShaderResources.IsUsingCombinedSamplers() && Attribs.Type == SPIRVShaderResourceAttribs::ResourceType::SeparateSampler) ?
ShaderResources.GetCombinedSamplerSuffix() :
nullptr;
ShaderResourceVariableDesc VarDesc = FindPipelineResourceLayoutVariable(ResourceLayout, Attribs.Name, Stage.Type, SamplerSuffix);
if (Attribs.Type == SPIRVShaderResourceAttribs::ResourceType::PushConstant)
{
if (MergedPushConstants.empty())
{
// First time we found a push constant - build the merged map
MergedPushConstants = MergePushConstants(ShaderStages, PSOName);
}
auto it = MergedPushConstants.find(VarDesc.Name);
if (it != MergedPushConstants.end())
{
VERIFY_EXPR(Attribs.GetConstantBufferSize() <= it->second.Size);
VarDesc.ShaderStages = it->second.Stages;
}
else
{
UNEXPECTED("Push constant '", VarDesc.Name, "' not found in merged push constant stages map. This is a bug.");
}
}
const SHADER_RESOURCE_TYPE ResType = SPIRVShaderResourceAttribs::GetShaderResourceType(Attribs.Type);
const PIPELINE_RESOURCE_FLAGS Flags = SPIRVShaderResourceAttribs::GetPipelineResourceFlags(Attribs.Type) | ShaderVariableFlagsToPipelineResourceFlags(VarDesc.Flags);
// For inline constants, ArraySize specifies the number of 32-bit constants,
// not the array dimension. We need to calculate it from the buffer size.
const Uint32 ArraySize = (Flags & PIPELINE_RESOURCE_FLAG_INLINE_CONSTANTS) ?
Attribs.GetInlineConstantCountOrThrow(StageItem.pShader->GetDesc().Name) :
Attribs.ArraySize;
VERIFY((Flags & PIPELINE_RESOURCE_FLAG_INLINE_CONSTANTS) == 0 || (Flags == PIPELINE_RESOURCE_FLAG_INLINE_CONSTANTS),
"INLINE_CONSTANTS flag cannot be combined with other flags.");
// Note that Attribs.Name != VarDesc.Name for combined samplers
Builder.AddResource(Attribs.Name, Attribs, VarDesc, ArraySize, ResType, Flags);
});
// Merge combined sampler suffixes
if (ShaderResources.IsUsingCombinedSamplers() && ShaderResources.GetNumSepSmplrs() > 0)
{
SignDesc.SetCombinedSamplerSuffix(ShaderResources.GetCombinedSamplerSuffix());
}
}
}
return SignDesc;
}
void PipelineStateVkImpl::RemapOrVerifyShaderResources(
TShaderStages& ShaderStages,
const RefCntAutoPtr<PipelineResourceSignatureVkImpl> pSignatures[],
const Uint32 SignatureCount,
const TBindIndexToDescSetIndex& BindIndexToDescSetIndex,
bool bVerifyOnly,
bool bStripReflection,
const char* PipelineName,
TShaderResources* pDvpShaderResources,
TResourceAttibutions* pDvpResourceAttibutions) noexcept(false)
{
if (PipelineName == nullptr)
PipelineName = "<null>";
const PipelineLayoutVk::PushConstantInfo& PushConstant = PipelineLayoutVk::GetPushConstantInfo(pSignatures, SignatureCount);
VERIFY_EXPR(!PushConstant || PushConstant.Name == pSignatures[PushConstant.SignatureIndex]->GetResourceDesc(PushConstant.ResourceIndex).Name);
// Verify that pipeline layout is compatible with shader resources and
// remap resource bindings.
for (ShaderStageInfo& Stage : ShaderStages)
{
const SHADER_TYPE ShaderType = Stage.Type;
for (ShaderStageInfo::Item& StageItem : Stage.Items)
{
const char* const ShaderName = StageItem.pShader->GetDesc().Name;
ShaderResourcesSharedPtr pShaderResources = StageItem.pShader->GetShaderResources();
std::vector<uint32_t>& SPIRV = StageItem.SPIRV;
if (!pShaderResources)
{
UNEXPECTED("Shader resources are not initialized for shader '", ShaderName, "'. This is a bug.");
continue;
}
if (PushConstant && !bVerifyOnly)
{
// Note that the inline constant buffer that is promoted to push constant still
// has the corresponding descriptor in the descriptor set layout, but it will not
// be used by the pipeline.
// Convert the selected uniform buffer to push constant if it is present in the shader
// and is not already a push constant.
if (pShaderResources->GetResourceByName(SPIRVShaderResourceAttribs::ResourceType::PushConstant, PushConstant.Name.c_str()) == nullptr &&
pShaderResources->GetResourceByName(SPIRVShaderResourceAttribs::ResourceType::UniformBuffer, PushConstant.Name.c_str()) != nullptr)
{
if (pShaderResources->GetNumPushConstants() != 0)
{
LOG_ERROR_AND_THROW("Shader '", ShaderName, "' already contains push constant '", pShaderResources->GetPushConstant(0).Name,
"', while pipeline '", PipelineName, "' requires converting uniform buffer '", PushConstant.Name,
"' to push constant. Converting push constants to uniform buffers is not supported. "
"To fix this issue, don't use push constants in the shader - Diligent Engine will convert "
"the required uniform buffer to push constant automatically.");
}
#if !DILIGENT_NO_HLSL
std::vector<uint32_t> PatchedSPIRV = ConvertUBOToPushConstants(SPIRV, PushConstant.Name);
if (PatchedSPIRV.empty())
{
LOG_ERROR_AND_THROW("Failed to convert uniform buffer '", PushConstant.Name,
"' to push constant in shader '", ShaderName, "'");
}
SPIRV = std::move(PatchedSPIRV);
// Recreate shader resources from the patched SPIRV
SPIRVShaderResources::CreateInfo ResCI;
ResCI.ShaderType = ShaderType;
ResCI.Name = ShaderName;
ResCI.CombinedSamplerSuffix = pShaderResources->GetCombinedSamplerSuffix();
ResCI.LoadShaderStageInputs = false; // Inputs have already been remapped
ResCI.LoadUniformBufferReflection = pShaderResources->HasUniformBufferReflection();
pShaderResources = SPIRVShaderResources::Create(GetRawAllocator(), SPIRV, ResCI);
#else
LOG_ERROR_AND_THROW("Cannot patch shader, SPIRV-Tools is not available when DILIGENT_NO_HLSL defined.");
#endif
}
}
if (pDvpShaderResources)
pDvpShaderResources->emplace_back(pShaderResources);
pShaderResources->ProcessResources(
[&](const SPIRVShaderResourceAttribs& SPIRVAttribs, Uint32) //
{
const ResourceAttribution ResAttribution = GetResourceAttribution(SPIRVAttribs.Name, ShaderType, pSignatures, SignatureCount);
if (!ResAttribution)
{
LOG_ERROR_AND_THROW("Shader '", ShaderName, "' contains resource '", SPIRVAttribs.Name,
"' that is not present in any pipeline resource signature used to create pipeline state '",
PipelineName, "'.");
}
const PipelineResourceSignatureDesc& SignDesc = ResAttribution.pSignature->GetDesc();
const SHADER_RESOURCE_TYPE ResType = SPIRVShaderResourceAttribs::GetShaderResourceType(SPIRVAttribs.Type);
const PIPELINE_RESOURCE_FLAGS Flags = SPIRVShaderResourceAttribs::GetPipelineResourceFlags(SPIRVAttribs.Type);
if (PushConstant && PushConstant.Name == SPIRVAttribs.Name)
{
// For push constants, skip descriptor set remapping, but validate the resource.
if (SPIRVAttribs.Type != SPIRVShaderResourceAttribs::ResourceType::PushConstant)
{
LOG_ERROR_AND_THROW("Shader '", ShaderName, "' contains resource with name '", SPIRVAttribs.Name,
"' that is expected to be a push constant in pipeline '", PipelineName,
"', but its type is '", SPIRVShaderResourceAttribs::ResourceTypeToString(SPIRVAttribs.Type), "'.");
}
VERIFY_EXPR(ResAttribution.ResourceIndex != ResourceAttribution::InvalidResourceIndex);
const PipelineResourceDesc& ResDesc = ResAttribution.pSignature->GetResourceDesc(ResAttribution.ResourceIndex);
ValidatePipelineResourceCompatibility(ResDesc, ResType, Flags, SPIRVAttribs.ArraySize,
ShaderName, SignDesc.Name);
}
else
{
Uint32 ResourceBinding = ~0u;
Uint32 DescriptorSet = ~0u;
if (ResAttribution.ResourceIndex != ResourceAttribution::InvalidResourceIndex)
{
if (SPIRVAttribs.Type == SPIRVShaderResourceAttribs::ResourceType::PushConstant)
{
LOG_ERROR_AND_THROW("Shader '", ShaderName, "' contains unexpected push constant resource '", SPIRVAttribs.Name,
"' that is not mapped to push constant in pipeline resource signature '",
SignDesc.Name, "'.");
}
const PipelineResourceDesc& ResDesc = ResAttribution.pSignature->GetResourceDesc(ResAttribution.ResourceIndex);
ValidatePipelineResourceCompatibility(ResDesc, ResType, Flags, SPIRVAttribs.ArraySize,
ShaderName, SignDesc.Name);
const PipelineResourceSignatureVkImpl::ResourceAttribs& ResAttribs{ResAttribution.pSignature->GetResourceAttribs(ResAttribution.ResourceIndex)};
ResourceBinding = ResAttribs.BindingIndex;
DescriptorSet = ResAttribs.DescrSet;
}
else if (ResAttribution.ImmutableSamplerIndex != ResourceAttribution::InvalidResourceIndex)
{
if (ResType != SHADER_RESOURCE_TYPE_SAMPLER)
{
LOG_ERROR_AND_THROW("Shader '", ShaderName, "' contains resource with name '", SPIRVAttribs.Name,
"' and type '", GetShaderResourceTypeLiteralName(ResType),
"' that is not compatible with immutable sampler defined in pipeline resource signature '",
SignDesc.Name, "'.");
}
const ImmutableSamplerAttribsVk& SamAttribs{ResAttribution.pSignature->GetImmutableSamplerAttribs(ResAttribution.ImmutableSamplerIndex)};
ResourceBinding = SamAttribs.BindingIndex;
DescriptorSet = SamAttribs.DescrSet;
}
else
{
UNEXPECTED("Either immutable sampler or resource index should be valid");
}
VERIFY_EXPR(ResourceBinding != ~0u && DescriptorSet != ~0u);
DescriptorSet += BindIndexToDescSetIndex[SignDesc.BindingIndex];
if (bVerifyOnly)
{
const Uint32 SpvBinding = SPIRV[SPIRVAttribs.BindingDecorationOffset];
const Uint32 SpvDescrSet = SPIRV[SPIRVAttribs.DescriptorSetDecorationOffset];
if (SpvBinding != ResourceBinding)
{
LOG_ERROR_AND_THROW("Shader '", ShaderName, "' maps resource '", SPIRVAttribs.Name,
"' to binding ", SpvBinding, ", but the same resource in pipeline resource signature '",
SignDesc.Name, "' is mapped to binding ", ResourceBinding, '.');
}
if (SpvDescrSet != DescriptorSet)
{
LOG_ERROR_AND_THROW("Shader '", ShaderName, "' maps resource '", SPIRVAttribs.Name,
"' to descriptor set ", SpvDescrSet, ", but the same resource in pipeline resource signature '",
SignDesc.Name, "' is mapped to set ", DescriptorSet, '.');
}
}
else
{
SPIRV[SPIRVAttribs.BindingDecorationOffset] = ResourceBinding;