This repository was archived by the owner on Dec 29, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathDxilRootSignature.cpp
More file actions
1682 lines (1479 loc) · 63.2 KB
/
Copy pathDxilRootSignature.cpp
File metadata and controls
1682 lines (1479 loc) · 63.2 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
///////////////////////////////////////////////////////////////////////////////
// //
// DxilRootSignature.cpp //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
// Provides support for manipulating root signature structures. //
// //
///////////////////////////////////////////////////////////////////////////////
#include "dxc/HLSL/DxilConstants.h"
#include "dxc/HLSL/DxilRootSignature.h"
#include "dxc/HLSL/DxilPipelineStateValidation.h"
#include "dxc/Support/Global.h"
#include "dxc/Support/WinIncludes.h"
#include "dxc/Support/WinFunctions.h"
#include "dxc/Support/FileIOHelper.h"
#include "dxc/dxcapi.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/DiagnosticPrinter.h"
#include <string>
#include <algorithm>
#include <utility>
#include <vector>
#include <set>
using namespace llvm;
using std::string;
namespace hlsl {
DEFINE_ENUM_FLAG_OPERATORS(DxilRootSignatureFlags)
DEFINE_ENUM_FLAG_OPERATORS(DxilRootDescriptorFlags)
DEFINE_ENUM_FLAG_OPERATORS(DxilDescriptorRangeType)
DEFINE_ENUM_FLAG_OPERATORS(DxilDescriptorRangeFlags)
// Execute (error) and throw.
#define EAT(x) { (x); throw ::hlsl::Exception(E_FAIL); }
//////////////////////////////////////////////////////////////////////////////
// Root signature handler.
RootSignatureHandle::RootSignatureHandle(RootSignatureHandle&& other) {
m_pDesc = nullptr;
m_pSerialized = nullptr;
std::swap(m_pDesc, other.m_pDesc);
std::swap(m_pSerialized, other.m_pSerialized);
}
void RootSignatureHandle::Assign(const DxilVersionedRootSignatureDesc *pDesc,
IDxcBlob *pSerialized) {
Clear();
m_pDesc = pDesc;
m_pSerialized = pSerialized;
if (m_pSerialized)
m_pSerialized->AddRef();
}
void RootSignatureHandle::Clear() {
hlsl::DeleteRootSignature(m_pDesc);
m_pDesc = nullptr;
if (m_pSerialized != nullptr) {
m_pSerialized->Release();
m_pSerialized = nullptr;
}
}
const uint8_t *RootSignatureHandle::GetSerializedBytes() const {
DXASSERT_NOMSG(m_pSerialized != nullptr);
return (uint8_t *)m_pSerialized->GetBufferPointer();
}
unsigned RootSignatureHandle::GetSerializedSize() const {
DXASSERT_NOMSG(m_pSerialized != nullptr);
return m_pSerialized->GetBufferSize();
}
void RootSignatureHandle::EnsureSerializedAvailable() {
DXASSERT_NOMSG(!IsEmpty());
if (m_pSerialized == nullptr) {
CComPtr<IDxcBlob> pResult;
hlsl::SerializeRootSignature(m_pDesc, &pResult, nullptr, false);
IFTBOOL(pResult != nullptr, E_FAIL);
m_pSerialized = pResult.Detach();
}
}
void RootSignatureHandle::Deserialize() {
DXASSERT_NOMSG(m_pSerialized && !m_pDesc);
DeserializeRootSignature((uint8_t*)m_pSerialized->GetBufferPointer(), (uint32_t)m_pSerialized->GetBufferSize(), &m_pDesc);
}
void RootSignatureHandle::LoadSerialized(const uint8_t *pData,
unsigned length) {
DXASSERT_NOMSG(IsEmpty());
IDxcBlobEncoding *pCreated;
IFT(DxcCreateBlobWithEncodingOnHeapCopy(pData, length, CP_UTF8, &pCreated));
m_pSerialized = pCreated;
}
//////////////////////////////////////////////////////////////////////////////
// Simple serializer.
class SimpleSerializer {
struct Segment {
void *pData;
unsigned cbSize;
bool bOwner;
unsigned Offset;
Segment *pNext;
};
public:
SimpleSerializer();
~SimpleSerializer();
HRESULT AddBlock(void *pData, unsigned cbSize, unsigned *pOffset);
HRESULT ReserveBlock(void **ppData, unsigned cbSize, unsigned *pOffset);
HRESULT Compact(_Out_writes_bytes_(cbSize) char *pData, unsigned cbSize);
unsigned GetSize();
protected:
unsigned m_cbSegments;
Segment *m_pSegment;
Segment **m_ppSegment;
};
SimpleSerializer::SimpleSerializer() {
m_cbSegments = 0;
m_pSegment = nullptr;
m_ppSegment = &m_pSegment;
}
SimpleSerializer::~SimpleSerializer() {
while (m_pSegment) {
Segment *pSegment = m_pSegment;
m_pSegment = pSegment->pNext;
if (pSegment->bOwner) {
delete[] (char*)pSegment->pData;
}
delete pSegment;
}
}
HRESULT SimpleSerializer::AddBlock(void *pData, unsigned cbSize,
unsigned *pOffset) {
Segment *pSegment = nullptr;
IFRBOOL(!(cbSize != 0 && pData == nullptr), E_FAIL);
IFROOM(pSegment = new (std::nothrow) Segment);
pSegment->pData = pData;
m_cbSegments = (m_cbSegments + 3) & ~3;
pSegment->Offset = m_cbSegments;
pSegment->cbSize = cbSize;
pSegment->bOwner = false;
pSegment->pNext = nullptr;
m_cbSegments += pSegment->cbSize;
*m_ppSegment = pSegment;
m_ppSegment = &pSegment->pNext;
if (pOffset != nullptr) {
*pOffset = pSegment->Offset;
}
return S_OK;
}
HRESULT SimpleSerializer::ReserveBlock(void **ppData, unsigned cbSize,
unsigned *pOffset) {
HRESULT hr = S_OK;
Segment *pSegment = nullptr;
void *pClonedData = nullptr;
IFCOOM(pSegment = new (std::nothrow) Segment);
pSegment->pData = nullptr;
IFCOOM(pClonedData = new (std::nothrow) char[cbSize]);
pSegment->pData = pClonedData;
m_cbSegments = (m_cbSegments + 3) & ~3;
pSegment->Offset = m_cbSegments;
pSegment->cbSize = cbSize;
pSegment->bOwner = true;
pSegment->pNext = nullptr;
m_cbSegments += pSegment->cbSize;
*m_ppSegment = pSegment;
m_ppSegment = &pSegment->pNext;
*ppData = pClonedData;
if (pOffset) {
*pOffset = pSegment->Offset;
}
Cleanup:
if (FAILED(hr)) {
delete[] (char*)pClonedData;
delete pSegment;
}
return hr;
}
HRESULT SimpleSerializer::Compact(_Out_writes_bytes_(cbSize) char *pData,
unsigned cbSize) {
unsigned cb = GetSize();
IFRBOOL(cb <= cbSize, E_FAIL);
DXASSERT_NOMSG(cb <= UINT32_MAX / 2);
char *p = (char *)pData;
cb = 0;
for (Segment *pSegment = m_pSegment; pSegment; pSegment = pSegment->pNext) {
unsigned cbAlign = ((cb + 3) & ~3) - cb;
_Analysis_assume_(p + cbAlign <= pData + cbSize);
memset(p, 0xab, cbAlign);
p += cbAlign;
cb += cbAlign;
_Analysis_assume_(p + pSegment->cbSize <= pData + cbSize);
memcpy(p, pSegment->pData, pSegment->cbSize);
p += pSegment->cbSize;
cb += pSegment->cbSize;
}
// Trailing zeros
_Analysis_assume_(p + cbSize - cb <= pData + cbSize);
memset(p, 0xab, cbSize - cb);
return S_OK;
}
unsigned SimpleSerializer::GetSize() {
// Round up to 4==sizeof(unsigned).
return ((m_cbSegments + 3) >> 2) * 4;
}
//////////////////////////////////////////////////////////////////////////////
// Interval helper.
template <typename T>
class CIntervalCollection {
private:
std::set<T> m_set;
public:
const T* FindIntersectingInterval(const T &I) {
auto it = m_set.find(I);
if (it != m_set.end())
return &*it;
return nullptr;
}
void Insert(const T& value) {
auto result = m_set.insert(value);
UNREFERENCED_PARAMETER(result);
#if DBG
DXASSERT(result.second, "otherwise interval collides with existing in collection");
#endif
}
};
//////////////////////////////////////////////////////////////////////////////
// Verifier classes.
class DescriptorTableVerifier {
public:
void Verify(const DxilDescriptorRange1 *pRanges, unsigned NumRanges,
unsigned iRTS, DiagnosticPrinter &DiagPrinter);
};
class StaticSamplerVerifier {
public:
void Verify(const DxilStaticSamplerDesc *pDesc, DiagnosticPrinter &DiagPrinter);
};
class RootSignatureVerifier {
public:
RootSignatureVerifier();
~RootSignatureVerifier();
void AllowReservedRegisterSpace(bool bAllow);
// Call this before calling VerifyShader, as it accumulates root signature state.
void VerifyRootSignature(const DxilVersionedRootSignatureDesc *pRootSignature,
DiagnosticPrinter &DiagPrinter);
void VerifyShader(DxilShaderVisibility VisType,
const void *pPSVData,
uint32_t PSVSize,
DiagnosticPrinter &DiagPrinter);
typedef enum NODE_TYPE {
DESCRIPTOR_TABLE_ENTRY,
ROOT_DESCRIPTOR,
ROOT_CONSTANT,
STATIC_SAMPLER
} NODE_TYPE;
private:
static const unsigned kMinVisType = (unsigned)DxilShaderVisibility::All;
static const unsigned kMaxVisType = (unsigned)DxilShaderVisibility::Pixel;
static const unsigned kMinDescType = (unsigned)DxilDescriptorRangeType::SRV;
static const unsigned kMaxDescType = (unsigned)DxilDescriptorRangeType::Sampler;
struct RegisterRange {
NODE_TYPE nt;
unsigned space;
unsigned lb; // inclusive lower bound
unsigned ub; // inclusive upper bound
unsigned iRP;
unsigned iDTS;
// Sort by space, then lower bound.
bool operator<(const RegisterRange& other) const {
return space < other.space ||
(space == other.space && ub < other.lb);
}
// Like a regular -1,0,1 comparison, but 0 indicates overlap.
int overlap(const RegisterRange& other) const {
if (space < other.space) return -1;
if (space > other.space) return 1;
if (ub < other.lb) return -1;
if (lb > other.ub) return 1;
return 0;
}
// Check containment.
bool contains(const RegisterRange& other) const {
return (space == other.space) && (lb <= other.lb && other.ub <= ub);
}
};
typedef CIntervalCollection<RegisterRange> RegisterRanges;
void AddRegisterRange(unsigned iRTS, NODE_TYPE nt, unsigned iDTS,
DxilDescriptorRangeType DescType,
DxilShaderVisibility VisType,
unsigned NumRegisters, unsigned BaseRegister,
unsigned RegisterSpace, DiagnosticPrinter &DiagPrinter);
const RegisterRange *FindCoveringInterval(DxilDescriptorRangeType RangeType,
DxilShaderVisibility VisType,
unsigned Num,
unsigned LB,
unsigned Space);
RegisterRanges &
GetRanges(DxilShaderVisibility VisType, DxilDescriptorRangeType DescType) {
return RangeKinds[(unsigned)VisType][(unsigned)DescType];
}
RegisterRanges RangeKinds[kMaxVisType + 1][kMaxDescType + 1];
bool m_bAllowReservedRegisterSpace;
DxilRootSignatureFlags m_RootSignatureFlags;
};
void DescriptorTableVerifier::Verify(const DxilDescriptorRange1 *pRanges,
uint32_t NumRanges, uint32_t iRP,
DiagnosticPrinter &DiagPrinter) {
bool bHasSamplers = false;
bool bHasResources = false;
uint64_t iAppendStartSlot = 0;
for (unsigned iDTS = 0; iDTS < NumRanges; iDTS++) {
const DxilDescriptorRange1 *pRange = &pRanges[iDTS];
switch (pRange->RangeType) {
case DxilDescriptorRangeType::SRV:
case DxilDescriptorRangeType::UAV:
case DxilDescriptorRangeType::CBV:
bHasResources = true;
break;
case DxilDescriptorRangeType::Sampler:
bHasSamplers = true;
break;
default:
EAT(DiagPrinter << "Unsupported RangeType value " << (uint32_t)pRange->RangeType
<< " (descriptor table slot [" << iDTS << "], root parameter [" << iRP << "]).\n");
}
// Samplers cannot be mixed with other resources.
if (bHasResources && bHasSamplers) {
EAT(DiagPrinter << "Samplers cannot be mixed with other "
<< "resource types in a descriptor table (root "
<< "parameter [" << iRP << "]).\n");
}
// NumDescriptors is not 0.
if (pRange->NumDescriptors == 0) {
EAT(DiagPrinter << "NumDescriptors cannot be 0 (descriptor "
<< "table slot [" << iDTS << "], root parameter [" << iRP << "]).\n");
}
// Range start.
uint64_t iStartSlot = iAppendStartSlot;
if (pRange->OffsetInDescriptorsFromTableStart != DxilDescriptorRangeOffsetAppend) {
iStartSlot = pRange->OffsetInDescriptorsFromTableStart;
}
if (iStartSlot > UINT_MAX) {
EAT(DiagPrinter << "Cannot append range with implicit lower "
<< "bound after an unbounded range (descriptor "
<< "table slot [" << iDTS << "], root parameter [" << iRP << "]).\n");
}
// Descriptor range and shader register range overlow.
if (pRange->NumDescriptors != UINT_MAX) {
// Bounded range.
uint64_t ub1 = (uint64_t)pRange->BaseShaderRegister +
(uint64_t)pRange->NumDescriptors - 1ull;
if (ub1 > UINT_MAX) {
EAT(DiagPrinter << "Overflow for shader register range: "
<< "BaseShaderRegister=" << pRange->BaseShaderRegister
<< ", NumDescriptor=" << pRange->NumDescriptors
<< "; (descriptor table slot [" << iDTS
<< "], root parameter [" << iRP << "]).\n");
}
uint64_t ub2 = (uint64_t)iStartSlot + (uint64_t)pRange->NumDescriptors - 1ull;
if (ub2 > UINT_MAX) {
EAT(DiagPrinter << "Overflow for descriptor range (descriptor "
<< "table slot [" << iDTS << "], root parameter [" << iRP << "])\n");
}
iAppendStartSlot = iStartSlot + (uint64_t)pRange->NumDescriptors;
} else {
// Unbounded range.
iAppendStartSlot = 1ull + (uint64_t)UINT_MAX;
}
}
}
RootSignatureVerifier::RootSignatureVerifier() {
m_RootSignatureFlags = DxilRootSignatureFlags::None;
m_bAllowReservedRegisterSpace = false;
}
RootSignatureVerifier::~RootSignatureVerifier() {}
void RootSignatureVerifier::AllowReservedRegisterSpace(bool bAllow) {
m_bAllowReservedRegisterSpace = bAllow;
}
const char* RangeTypeString(DxilDescriptorRangeType rt)
{
static const char *RangeType[] = {"SRV", "UAV", "CBV", "SAMPLER"};
return (rt <= DxilDescriptorRangeType::Sampler) ? RangeType[(unsigned)rt]
: "unknown";
}
const char *VisTypeString(DxilShaderVisibility vis) {
static const char *Vis[] = {"ALL", "VERTEX", "HULL",
"DOMAIN", "GEOMETRY", "PIXEL"};
unsigned idx = (unsigned)vis;
return vis <= DxilShaderVisibility::Pixel ? Vis[idx] : "unknown";
}
static bool IsDxilShaderVisibility(DxilShaderVisibility v) {
return v <= DxilShaderVisibility::Pixel;
}
void RootSignatureVerifier::AddRegisterRange(unsigned iRP,
NODE_TYPE nt,
unsigned iDTS,
DxilDescriptorRangeType DescType,
DxilShaderVisibility VisType,
unsigned NumRegisters,
unsigned BaseRegister,
unsigned RegisterSpace,
DiagnosticPrinter &DiagPrinter) {
RegisterRange interval;
interval.space = RegisterSpace;
interval.lb = BaseRegister;
interval.ub = (NumRegisters != UINT_MAX) ? BaseRegister + NumRegisters - 1 : UINT_MAX;
interval.nt = nt;
interval.iDTS = iDTS;
interval.iRP = iRP;
if (!m_bAllowReservedRegisterSpace &&
(RegisterSpace >= DxilSystemReservedRegisterSpaceValuesStart) &&
(RegisterSpace <= DxilSystemReservedRegisterSpaceValuesEnd)) {
if (nt == DESCRIPTOR_TABLE_ENTRY) {
EAT(DiagPrinter << "Root parameter [" << iRP << "] descriptor table entry [" << iDTS
<< "] specifies RegisterSpace=" << std::hex << RegisterSpace
<< ", which is invalid since RegisterSpace values in the range "
<< "[" << std::hex << DxilSystemReservedRegisterSpaceValuesStart
<< "," << std::hex << DxilSystemReservedRegisterSpaceValuesEnd
<< "] are reserved for system use.\n");
}
else {
EAT(DiagPrinter << "Root parameter [" << iRP
<< "] specifies RegisterSpace=" << std::hex << RegisterSpace
<< ", which is invalid since RegisterSpace values in the range "
<< "[" << std::hex << DxilSystemReservedRegisterSpaceValuesStart
<< "," << std::hex << DxilSystemReservedRegisterSpaceValuesEnd
<< "] are reserved for system use.\n");
}
}
const RegisterRange *pNode = nullptr;
DxilShaderVisibility NodeVis = VisType;
if (VisType == DxilShaderVisibility::All) {
// Check for overlap with each visibility type.
for (unsigned iVT = kMinVisType; iVT <= kMaxVisType; iVT++) {
pNode = GetRanges((DxilShaderVisibility)iVT, DescType).FindIntersectingInterval(interval);
if (pNode != nullptr)
break;
}
} else {
// Check for overlap with the same visibility.
pNode = GetRanges(VisType, DescType).FindIntersectingInterval(interval);
// Check for overlap with ALL visibility.
if (pNode == nullptr) {
pNode = GetRanges(DxilShaderVisibility::All, DescType).FindIntersectingInterval(interval);
NodeVis = DxilShaderVisibility::All;
}
}
if (pNode != nullptr) {
const int strSize = 132;
char testString[strSize];
char nodeString[strSize];
switch (nt) {
case DESCRIPTOR_TABLE_ENTRY:
StringCchPrintfA(testString, strSize, "(root parameter [%u], visibility %s, descriptor table slot [%u])",
iRP, VisTypeString(VisType), iDTS);
break;
case ROOT_DESCRIPTOR:
case ROOT_CONSTANT:
StringCchPrintfA(testString, strSize, "(root parameter [%u], visibility %s)",
iRP, VisTypeString(VisType));
break;
case STATIC_SAMPLER:
StringCchPrintfA(testString, strSize, "(static sampler [%u], visibility %s)",
iRP, VisTypeString(VisType));
break;
default:
DXASSERT_NOMSG(false);
break;
}
switch (pNode->nt)
{
case DESCRIPTOR_TABLE_ENTRY:
StringCchPrintfA(nodeString, strSize, "(root parameter[%u], visibility %s, descriptor table slot [%u])",
pNode->iRP, VisTypeString(NodeVis), pNode->iDTS);
break;
case ROOT_DESCRIPTOR:
case ROOT_CONSTANT:
StringCchPrintfA(nodeString, strSize, "(root parameter [%u], visibility %s)",
pNode->iRP, VisTypeString(NodeVis));
break;
case STATIC_SAMPLER:
StringCchPrintfA(nodeString, strSize, "(static sampler [%u], visibility %s)",
pNode->iRP, VisTypeString(NodeVis));
break;
default:
DXASSERT_NOMSG(false);
break;
}
EAT(DiagPrinter << "Shader register range of type " << RangeTypeString(DescType)
<< " " << testString << " overlaps with another "
<< "shader register range " << nodeString << ".\n");
}
// Insert node.
GetRanges(VisType, DescType).Insert(interval);
}
const RootSignatureVerifier::RegisterRange *
RootSignatureVerifier::FindCoveringInterval(DxilDescriptorRangeType RangeType,
DxilShaderVisibility VisType,
unsigned Num,
unsigned LB,
unsigned Space) {
RegisterRange RR;
RR.space = Space;
RR.lb = LB;
RR.ub = LB + Num - 1;
const RootSignatureVerifier::RegisterRange *pRange = GetRanges(DxilShaderVisibility::All, RangeType).FindIntersectingInterval(RR);
if (!pRange && VisType != DxilShaderVisibility::All) {
pRange = GetRanges(VisType, RangeType).FindIntersectingInterval(RR);
}
if (pRange && !pRange->contains(RR)) {
pRange = nullptr;
}
return pRange;
}
static DxilDescriptorRangeType GetRangeType(DxilRootParameterType RPT) {
switch (RPT) {
case DxilRootParameterType::CBV: return DxilDescriptorRangeType::CBV;
case DxilRootParameterType::SRV: return DxilDescriptorRangeType::SRV;
case DxilRootParameterType::UAV: return DxilDescriptorRangeType::UAV;
default:
break;
}
DXASSERT_NOMSG(false);
return DxilDescriptorRangeType::SRV;
}
void RootSignatureVerifier::VerifyRootSignature(
const DxilVersionedRootSignatureDesc *pVersionedRootSignature,
DiagnosticPrinter &DiagPrinter) {
const DxilVersionedRootSignatureDesc *pUpconvertedRS = nullptr;
// Up-convert root signature to the latest RS version.
ConvertRootSignature(pVersionedRootSignature, DxilRootSignatureVersion::Version_1_1, &pUpconvertedRS);
DXASSERT_NOMSG(pUpconvertedRS->Version == DxilRootSignatureVersion::Version_1_1);
// Ensure this gets deleted as necessary.
struct SigGuard {
const DxilVersionedRootSignatureDesc *Orig, *Guard;
SigGuard(const DxilVersionedRootSignatureDesc *pOrig, const DxilVersionedRootSignatureDesc *pGuard)
: Orig(pOrig), Guard(pGuard) { }
~SigGuard() {
if (Orig != Guard) {
DeleteRootSignature(Guard);
}
}
};
SigGuard S(pVersionedRootSignature, pUpconvertedRS);
const DxilRootSignatureDesc1 *pRootSignature = &pUpconvertedRS->Desc_1_1;
// Flags (assume they are bits that can be combined with OR).
if ((pRootSignature->Flags & ~DxilRootSignatureFlags::ValidFlags) != DxilRootSignatureFlags::None) {
EAT(DiagPrinter << "Unsupported bit-flag set (root signature flags "
<< std::hex << (uint32_t)pRootSignature->Flags << ").\n");
}
m_RootSignatureFlags = pRootSignature->Flags;
for (unsigned iRP = 0; iRP < pRootSignature->NumParameters; iRP++) {
const DxilRootParameter1 *pSlot = &pRootSignature->pParameters[iRP];
// Shader visibility.
DxilShaderVisibility Visibility = pSlot->ShaderVisibility;
if (!IsDxilShaderVisibility(Visibility)) {
EAT(DiagPrinter << "Unsupported ShaderVisibility value " << (uint32_t)Visibility
<< " (root parameter [" << iRP << "]).\n");
}
DxilRootParameterType ParameterType = pSlot->ParameterType;
switch (ParameterType) {
case DxilRootParameterType::DescriptorTable: {
DescriptorTableVerifier DTV;
DTV.Verify(pSlot->DescriptorTable.pDescriptorRanges,
pSlot->DescriptorTable.NumDescriptorRanges, iRP, DiagPrinter);
for (unsigned iDTS = 0; iDTS < pSlot->DescriptorTable.NumDescriptorRanges; iDTS++) {
const DxilDescriptorRange1 *pRange = &pSlot->DescriptorTable.pDescriptorRanges[iDTS];
unsigned RangeFlags = (unsigned)pRange->Flags;
// Verify range flags.
if (RangeFlags & ~(unsigned)DxilDescriptorRangeFlags::ValidFlags) {
EAT(DiagPrinter << "Unsupported bit-flag set (descriptor range flags "
<< (uint32_t)pRange->Flags << ").\n");
}
switch (pRange->RangeType) {
case DxilDescriptorRangeType::Sampler: {
if (RangeFlags & (unsigned)(DxilDescriptorRangeFlags::DataVolatile |
DxilDescriptorRangeFlags::DataStatic |
DxilDescriptorRangeFlags::DataStaticWhileSetAtExecute)) {
EAT(DiagPrinter << "Sampler descriptor ranges can't specify DATA_* flags "
<< "since there is no data pointed to by samplers "
<< "(descriptor range flags " << (uint32_t)pRange->Flags << ").\n");
}
break;
}
default: {
unsigned NumDataFlags = 0;
if (RangeFlags & (unsigned)DxilDescriptorRangeFlags::DataVolatile) { NumDataFlags++; }
if (RangeFlags & (unsigned)DxilDescriptorRangeFlags::DataStatic) { NumDataFlags++; }
if (RangeFlags & (unsigned)DxilDescriptorRangeFlags::DataStaticWhileSetAtExecute) { NumDataFlags++; }
if (NumDataFlags > 1) {
EAT(DiagPrinter << "Descriptor range flags cannot specify more than one DATA_* flag "
<< "at a time (descriptor range flags " << (uint32_t)pRange->Flags << ").\n");
}
if ((RangeFlags & (unsigned)DxilDescriptorRangeFlags::DataStatic) &&
(RangeFlags & (unsigned)DxilDescriptorRangeFlags::DescriptorsVolatile)) {
EAT(DiagPrinter << "Descriptor range flags cannot specify DESCRIPTORS_VOLATILE with the DATA_STATIC flag at the same time (descriptor range flags " << (uint32_t)pRange->Flags << "). "
<< "DATA_STATIC_WHILE_SET_AT_EXECUTE is fine to combine with DESCRIPTORS_VOLATILE, since DESCRIPTORS_VOLATILE still requires descriptors don't change during execution. \n");
}
break;
}
}
AddRegisterRange(iRP,
DESCRIPTOR_TABLE_ENTRY,
iDTS,
pRange->RangeType,
Visibility,
pRange->NumDescriptors,
pRange->BaseShaderRegister,
pRange->RegisterSpace,
DiagPrinter);
}
break;
}
case DxilRootParameterType::Constants32Bit:
AddRegisterRange(iRP,
ROOT_CONSTANT,
(unsigned)-1,
DxilDescriptorRangeType::CBV,
Visibility,
1,
pSlot->Constants.ShaderRegister,
pSlot->Constants.RegisterSpace,
DiagPrinter);
break;
case DxilRootParameterType::CBV:
case DxilRootParameterType::SRV:
case DxilRootParameterType::UAV: {
// Verify root descriptor flags.
unsigned Flags = (unsigned)pSlot->Descriptor.Flags;
if (Flags & ~(unsigned)DxilRootDescriptorFlags::ValidFlags) {
EAT(DiagPrinter << "Unsupported bit-flag set (root descriptor flags " << std::hex << Flags << ").\n");
}
unsigned NumDataFlags = 0;
if (Flags & (unsigned)DxilRootDescriptorFlags::DataVolatile) { NumDataFlags++; }
if (Flags & (unsigned)DxilRootDescriptorFlags::DataStatic) { NumDataFlags++; }
if (Flags & (unsigned)DxilRootDescriptorFlags::DataStaticWhileSetAtExecute) { NumDataFlags++; }
if (NumDataFlags > 1) {
EAT(DiagPrinter << "Root descriptor flags cannot specify more "
<< "than one DATA_* flag at a time (root "
<< "descriptor flags " << NumDataFlags << ").\n");
}
AddRegisterRange(iRP, ROOT_DESCRIPTOR, (unsigned)-1,
GetRangeType(ParameterType), Visibility, 1,
pSlot->Descriptor.ShaderRegister,
pSlot->Descriptor.RegisterSpace, DiagPrinter);
break;
}
default:
EAT(DiagPrinter << "Unsupported ParameterType value " << (uint32_t)ParameterType
<< " (root parameter " << iRP << ")\n");
}
}
for (unsigned iSS = 0; iSS < pRootSignature->NumStaticSamplers; iSS++) {
const DxilStaticSamplerDesc *pSS = &pRootSignature->pStaticSamplers[iSS];
// Shader visibility.
DxilShaderVisibility Visibility = pSS->ShaderVisibility;
if (!IsDxilShaderVisibility(Visibility)) {
EAT(DiagPrinter << "Unsupported ShaderVisibility value " << (uint32_t)Visibility
<< " (static sampler [" << iSS << "]).\n");
}
StaticSamplerVerifier SSV;
SSV.Verify(pSS, DiagPrinter);
AddRegisterRange(iSS, STATIC_SAMPLER, (unsigned)-1,
DxilDescriptorRangeType::Sampler, Visibility, 1,
pSS->ShaderRegister, pSS->RegisterSpace, DiagPrinter);
}
}
void RootSignatureVerifier::VerifyShader(DxilShaderVisibility VisType,
const void *pPSVData,
uint32_t PSVSize,
DiagnosticPrinter &DiagPrinter) {
DxilPipelineStateValidation PSV;
IFTBOOL(PSV.InitFromPSV0(pPSVData, PSVSize), E_INVALIDARG);
bool bShaderDeniedByRootSig = false;
switch (VisType) {
case DxilShaderVisibility::Vertex:
if ((m_RootSignatureFlags & DxilRootSignatureFlags::DenyVertexShaderRootAccess) != DxilRootSignatureFlags::None) {
bShaderDeniedByRootSig = true;
}
break;
case DxilShaderVisibility::Hull:
if ((m_RootSignatureFlags & DxilRootSignatureFlags::DenyHullShaderRootAccess) != DxilRootSignatureFlags::None) {
bShaderDeniedByRootSig = true;
}
break;
case DxilShaderVisibility::Domain:
if ((m_RootSignatureFlags & DxilRootSignatureFlags::DenyDomainShaderRootAccess) != DxilRootSignatureFlags::None) {
bShaderDeniedByRootSig = true;
}
break;
case DxilShaderVisibility::Geometry:
if ((m_RootSignatureFlags & DxilRootSignatureFlags::DenyGeometryShaderRootAccess) != DxilRootSignatureFlags::None) {
bShaderDeniedByRootSig = true;
}
break;
case DxilShaderVisibility::Pixel:
if ((m_RootSignatureFlags & DxilRootSignatureFlags::DenyPixelShaderRootAccess) != DxilRootSignatureFlags::None) {
bShaderDeniedByRootSig = true;
}
break;
default:
break;
}
bool bShaderHasRootBindings = false;
for (unsigned iResource = 0; iResource < PSV.GetBindCount(); iResource++) {
const PSVResourceBindInfo0 *pBindInfo0 = PSV.GetPSVResourceBindInfo0(iResource);
DXASSERT_NOMSG(pBindInfo0);
unsigned Space = pBindInfo0->Space;
unsigned LB = pBindInfo0->LowerBound;
unsigned UB = pBindInfo0->UpperBound;
unsigned Num = (UB != UINT_MAX) ? (UB - LB + 1) : 1;
PSVResourceType ResType = (PSVResourceType)pBindInfo0->ResType;
switch(ResType) {
case PSVResourceType::Sampler: {
bShaderHasRootBindings = true;
auto pCoveringRange = FindCoveringInterval(DxilDescriptorRangeType::Sampler, VisType, Num, LB, Space);
if(!pCoveringRange) {
EAT(DiagPrinter << "Shader sampler descriptor range (RegisterSpace=" << Space
<< ", NumDescriptors=" << Num << ", BaseShaderRegister=" << LB
<< ") is not fully bound in root signature.\n");
}
break;
}
case PSVResourceType::SRVTyped:
case PSVResourceType::SRVRaw:
case PSVResourceType::SRVStructured: {
bShaderHasRootBindings = true;
auto pCoveringRange = FindCoveringInterval(DxilDescriptorRangeType::SRV, VisType, Num, LB, Space);
if (pCoveringRange) {
if(pCoveringRange->nt == ROOT_DESCRIPTOR && ResType == PSVResourceType::SRVTyped) {
EAT(DiagPrinter << "A Shader is declaring a resource object as a texture using "
<< "a register mapped to a root descriptor SRV (RegisterSpace=" << Space
<< ", ShaderRegister=" << LB << "). "
<< "SRV or UAV root descriptors can only be Raw or Structured buffers.\n");
}
}
else {
EAT(DiagPrinter << "Shader SRV descriptor range (RegisterSpace=" << Space
<< ", NumDescriptors=" << Num << ", BaseShaderRegister=" << LB
<< ") is not fully bound in root signature.\n");
}
break;
}
case PSVResourceType::UAVTyped:
case PSVResourceType::UAVRaw:
case PSVResourceType::UAVStructured:
case PSVResourceType::UAVStructuredWithCounter: {
bShaderHasRootBindings = true;
auto pCoveringRange = FindCoveringInterval(DxilDescriptorRangeType::UAV, VisType, Num, LB, Space);
if (pCoveringRange) {
if (pCoveringRange->nt == ROOT_DESCRIPTOR) {
if (ResType == PSVResourceType::UAVTyped) {
EAT(DiagPrinter << "A shader is declaring a typed UAV using a register mapped "
<< "to a root descriptor UAV (RegisterSpace=" << Space
<< ", ShaderRegister=" << LB << "). "
<< "SRV or UAV root descriptors can only be Raw or Structured buffers.\n");
}
if (ResType == PSVResourceType::UAVStructuredWithCounter) {
EAT(DiagPrinter << "A Shader is declaring a structured UAV with counter using "
<< "a register mapped to a root descriptor UAV (RegisterSpace=" << Space
<< ", ShaderRegister=" << LB << "). "
<< "SRV or UAV root descriptors can only be Raw or Structured buffers.\n");
}
}
}
else {
EAT(DiagPrinter << "Shader UAV descriptor range (RegisterSpace=" << Space
<< ", NumDescriptors=" << Num << ", BaseShaderRegister=" << LB
<< ") is not fully bound in root signature.\n");
}
break;
}
case PSVResourceType::CBV: {
bShaderHasRootBindings = true;
auto pCoveringRange = FindCoveringInterval(DxilDescriptorRangeType::CBV, VisType, Num, LB, Space);
if (!pCoveringRange) {
EAT(DiagPrinter << "Shader CBV descriptor range (RegisterSpace=" << Space
<< ", NumDescriptors=" << Num << ", BaseShaderRegister=" << LB
<< ") is not fully bound in root signature.\n");
}
break;
}
default:
break;
}
}
if (bShaderHasRootBindings && bShaderDeniedByRootSig) {
EAT(DiagPrinter << "Shader has root bindings but root signature uses a DENY flag "
<< "to disallow root binding access to the shader stage.\n");
}
}
BOOL isNaN(const float &a) {
static const unsigned exponentMask = 0x7f800000;
static const unsigned mantissaMask = 0x007fffff;
unsigned u = *(const unsigned *)&a;
return (((u & exponentMask) == exponentMask) && (u & mantissaMask)); // NaN
}
static bool IsDxilTextureAddressMode(DxilTextureAddressMode v) {
return DxilTextureAddressMode::Wrap <= v &&
v <= DxilTextureAddressMode::MirrorOnce;
}
static bool IsDxilComparisonFunc(DxilComparisonFunc v) {
return DxilComparisonFunc::Never <= v && v <= DxilComparisonFunc::Always;
}
// This validation closely mirrors CCreateSamplerStateValidator's checks
void StaticSamplerVerifier::Verify(const DxilStaticSamplerDesc* pDesc,
DiagnosticPrinter &DiagPrinter) {
if (!pDesc) {
EAT(DiagPrinter << "Static sampler: A nullptr pSamplerDesc was specified.\n");
}
bool bIsComparison = false;
switch (pDesc->Filter) {
case DxilFilter::MINIMUM_MIN_MAG_MIP_POINT:
case DxilFilter::MINIMUM_MIN_MAG_POINT_MIP_LINEAR:
case DxilFilter::MINIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT:
case DxilFilter::MINIMUM_MIN_POINT_MAG_MIP_LINEAR:
case DxilFilter::MINIMUM_MIN_LINEAR_MAG_MIP_POINT:
case DxilFilter::MINIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR:
case DxilFilter::MINIMUM_MIN_MAG_LINEAR_MIP_POINT:
case DxilFilter::MINIMUM_MIN_MAG_MIP_LINEAR:
case DxilFilter::MINIMUM_ANISOTROPIC:
case DxilFilter::MAXIMUM_MIN_MAG_MIP_POINT:
case DxilFilter::MAXIMUM_MIN_MAG_POINT_MIP_LINEAR:
case DxilFilter::MAXIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT:
case DxilFilter::MAXIMUM_MIN_POINT_MAG_MIP_LINEAR:
case DxilFilter::MAXIMUM_MIN_LINEAR_MAG_MIP_POINT:
case DxilFilter::MAXIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR:
case DxilFilter::MAXIMUM_MIN_MAG_LINEAR_MIP_POINT:
case DxilFilter::MAXIMUM_MIN_MAG_MIP_LINEAR:
case DxilFilter::MAXIMUM_ANISOTROPIC:
break;
case DxilFilter::MIN_MAG_MIP_POINT:
case DxilFilter::MIN_MAG_POINT_MIP_LINEAR:
case DxilFilter::MIN_POINT_MAG_LINEAR_MIP_POINT:
case DxilFilter::MIN_POINT_MAG_MIP_LINEAR:
case DxilFilter::MIN_LINEAR_MAG_MIP_POINT:
case DxilFilter::MIN_LINEAR_MAG_POINT_MIP_LINEAR:
case DxilFilter::MIN_MAG_LINEAR_MIP_POINT:
case DxilFilter::MIN_MAG_MIP_LINEAR:
case DxilFilter::ANISOTROPIC:
break;
case DxilFilter::COMPARISON_MIN_MAG_MIP_POINT:
case DxilFilter::COMPARISON_MIN_MAG_POINT_MIP_LINEAR:
case DxilFilter::COMPARISON_MIN_POINT_MAG_LINEAR_MIP_POINT:
case DxilFilter::COMPARISON_MIN_POINT_MAG_MIP_LINEAR:
case DxilFilter::COMPARISON_MIN_LINEAR_MAG_MIP_POINT:
case DxilFilter::COMPARISON_MIN_LINEAR_MAG_POINT_MIP_LINEAR:
case DxilFilter::COMPARISON_MIN_MAG_LINEAR_MIP_POINT:
case DxilFilter::COMPARISON_MIN_MAG_MIP_LINEAR:
case DxilFilter::COMPARISON_ANISOTROPIC:
bIsComparison = true;
break;
default:
EAT(DiagPrinter << "Static sampler: Filter unrecognized.\n");
}
if (!IsDxilTextureAddressMode(pDesc->AddressU)) {
EAT(DiagPrinter << "Static sampler: AddressU unrecognized.\n");
}
if (!IsDxilTextureAddressMode(pDesc->AddressV)) {
EAT(DiagPrinter << "Static sampler: AddressV unrecognized.\n");
}
if (!IsDxilTextureAddressMode(pDesc->AddressW)) {
EAT(DiagPrinter << "Static sampler: AddressW unrecognized.\n");
}
if (isNaN(pDesc->MipLODBias) || (pDesc->MipLODBias < DxilMipLodBiaxMin) ||
(pDesc->MipLODBias > DxilMipLodBiaxMax)) {
EAT(DiagPrinter << "Static sampler: MipLODBias must be in the "
<< "range [" << DxilMipLodBiaxMin << " to " << DxilMipLodBiaxMax
<<"]. " << pDesc->MipLODBias << "specified.\n");
}
if (pDesc->MaxAnisotropy > DxilMapAnisotropy) {
EAT(DiagPrinter << "Static sampler: MaxAnisotropy must be in "
<< "the range [0 to " << DxilMapAnisotropy << "]. "
<< pDesc->MaxAnisotropy << " specified.\n");
}
if (bIsComparison && !IsDxilComparisonFunc(pDesc->ComparisonFunc)) {
EAT(DiagPrinter << "Static sampler: ComparisonFunc unrecognized.");
}
if (isNaN(pDesc->MinLOD)) {
EAT(DiagPrinter << "Static sampler: MinLOD be in the range [-INF to +INF]. "