forked from KhronosGroup/Vulkan-ValidationLayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpu_validation.cpp
2055 lines (1851 loc) · 120 KB
/
gpu_validation.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-2021 The Khronos Group Inc.
* Copyright (c) 2018-2021 Valve Corporation
* Copyright (c) 2018-2021 LunarG, Inc.
*
* 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.
*
* Author: Karl Schultz <[email protected]>
* Author: Tony Barbour <[email protected]>
*/
#include <climits>
#include "gpu_validation.h"
#include "spirv-tools/optimizer.hpp"
#include "spirv-tools/instrument.hpp"
#include "layer_chassis_dispatch.h"
#include "gpu_vuids.h"
#include "sync_utils.h"
static const VkShaderStageFlags kShaderStageAllRayTracing =
VK_SHADER_STAGE_ANY_HIT_BIT_NV | VK_SHADER_STAGE_CALLABLE_BIT_NV | VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV |
VK_SHADER_STAGE_INTERSECTION_BIT_NV | VK_SHADER_STAGE_MISS_BIT_NV | VK_SHADER_STAGE_RAYGEN_BIT_NV;
// Keep in sync with the GLSL shader below.
struct GpuAccelerationStructureBuildValidationBuffer {
uint32_t instances_to_validate;
uint32_t replacement_handle_bits_0;
uint32_t replacement_handle_bits_1;
uint32_t invalid_handle_found;
uint32_t invalid_handle_bits_0;
uint32_t invalid_handle_bits_1;
uint32_t valid_handles_count;
};
// This is the GLSL source for the compute shader that is used during ray tracing acceleration structure
// building validation which inspects instance buffers for top level acceleration structure builds and
// reports and replaces invalid bottom level acceleration structure handles with good bottom level
// acceleration structure handle so that applications can continue without undefined behavior long enough
// to report errors.
//
// #version 450
// layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
// struct VkGeometryInstanceNV {
// uint unused[14];
// uint handle_bits_0;
// uint handle_bits_1;
// };
// layout(set=0, binding=0, std430) buffer InstanceBuffer {
// VkGeometryInstanceNV instances[];
// };
// layout(set=0, binding=1, std430) buffer ValidationBuffer {
// uint instances_to_validate;
// uint replacement_handle_bits_0;
// uint replacement_handle_bits_1;
// uint invalid_handle_found;
// uint invalid_handle_bits_0;
// uint invalid_handle_bits_1;
// uint valid_handles_count;
// uint valid_handles[];
// };
// void main() {
// for (uint instance_index = 0; instance_index < instances_to_validate; instance_index++) {
// uint instance_handle_bits_0 = instances[instance_index].handle_bits_0;
// uint instance_handle_bits_1 = instances[instance_index].handle_bits_1;
// bool valid = false;
// for (uint valid_handle_index = 0; valid_handle_index < valid_handles_count; valid_handle_index++) {
// if (instance_handle_bits_0 == valid_handles[2*valid_handle_index+0] &&
// instance_handle_bits_1 == valid_handles[2*valid_handle_index+1]) {
// valid = true;
// break;
// }
// }
// if (!valid) {
// invalid_handle_found += 1;
// invalid_handle_bits_0 = instance_handle_bits_0;
// invalid_handle_bits_1 = instance_handle_bits_1;
// instances[instance_index].handle_bits_0 = replacement_handle_bits_0;
// instances[instance_index].handle_bits_1 = replacement_handle_bits_1;
// }
// }
// }
//
// To regenerate the spirv below:
// 1. Save the above GLSL source to a file called validation_shader.comp.
// 2. Run in terminal
//
// glslangValidator.exe -x -V validation_shader.comp -o validation_shader.comp.spv
//
// 4. Copy-paste the contents of validation_shader.comp.spv here (clang-format will fix up the alignment).
static const uint32_t kComputeShaderSpirv[] = {
0x07230203, 0x00010000, 0x00080007, 0x0000006d, 0x00000000, 0x00020011, 0x00000001, 0x0006000b, 0x00000001, 0x4c534c47,
0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0005000f, 0x00000005, 0x00000004, 0x6e69616d,
0x00000000, 0x00060010, 0x00000004, 0x00000011, 0x00000001, 0x00000001, 0x00000001, 0x00030003, 0x00000002, 0x000001c2,
0x00040005, 0x00000004, 0x6e69616d, 0x00000000, 0x00060005, 0x00000008, 0x74736e69, 0x65636e61, 0x646e695f, 0x00007865,
0x00070005, 0x00000011, 0x696c6156, 0x69746164, 0x75426e6f, 0x72656666, 0x00000000, 0x00090006, 0x00000011, 0x00000000,
0x74736e69, 0x65636e61, 0x6f745f73, 0x6c61765f, 0x74616469, 0x00000065, 0x000a0006, 0x00000011, 0x00000001, 0x6c706572,
0x6d656361, 0x5f746e65, 0x646e6168, 0x625f656c, 0x5f737469, 0x00000030, 0x000a0006, 0x00000011, 0x00000002, 0x6c706572,
0x6d656361, 0x5f746e65, 0x646e6168, 0x625f656c, 0x5f737469, 0x00000031, 0x00090006, 0x00000011, 0x00000003, 0x61766e69,
0x5f64696c, 0x646e6168, 0x665f656c, 0x646e756f, 0x00000000, 0x00090006, 0x00000011, 0x00000004, 0x61766e69, 0x5f64696c,
0x646e6168, 0x625f656c, 0x5f737469, 0x00000030, 0x00090006, 0x00000011, 0x00000005, 0x61766e69, 0x5f64696c, 0x646e6168,
0x625f656c, 0x5f737469, 0x00000031, 0x00080006, 0x00000011, 0x00000006, 0x696c6176, 0x61685f64, 0x656c646e, 0x6f635f73,
0x00746e75, 0x00070006, 0x00000011, 0x00000007, 0x696c6176, 0x61685f64, 0x656c646e, 0x00000073, 0x00030005, 0x00000013,
0x00000000, 0x00080005, 0x0000001b, 0x74736e69, 0x65636e61, 0x6e61685f, 0x5f656c64, 0x73746962, 0x0000305f, 0x00080005,
0x0000001e, 0x65476b56, 0x74656d6f, 0x6e497972, 0x6e617473, 0x564e6563, 0x00000000, 0x00050006, 0x0000001e, 0x00000000,
0x73756e75, 0x00006465, 0x00070006, 0x0000001e, 0x00000001, 0x646e6168, 0x625f656c, 0x5f737469, 0x00000030, 0x00070006,
0x0000001e, 0x00000002, 0x646e6168, 0x625f656c, 0x5f737469, 0x00000031, 0x00060005, 0x00000020, 0x74736e49, 0x65636e61,
0x66667542, 0x00007265, 0x00060006, 0x00000020, 0x00000000, 0x74736e69, 0x65636e61, 0x00000073, 0x00030005, 0x00000022,
0x00000000, 0x00080005, 0x00000027, 0x74736e69, 0x65636e61, 0x6e61685f, 0x5f656c64, 0x73746962, 0x0000315f, 0x00040005,
0x0000002d, 0x696c6176, 0x00000064, 0x00070005, 0x0000002f, 0x696c6176, 0x61685f64, 0x656c646e, 0x646e695f, 0x00007865,
0x00040047, 0x00000010, 0x00000006, 0x00000004, 0x00050048, 0x00000011, 0x00000000, 0x00000023, 0x00000000, 0x00050048,
0x00000011, 0x00000001, 0x00000023, 0x00000004, 0x00050048, 0x00000011, 0x00000002, 0x00000023, 0x00000008, 0x00050048,
0x00000011, 0x00000003, 0x00000023, 0x0000000c, 0x00050048, 0x00000011, 0x00000004, 0x00000023, 0x00000010, 0x00050048,
0x00000011, 0x00000005, 0x00000023, 0x00000014, 0x00050048, 0x00000011, 0x00000006, 0x00000023, 0x00000018, 0x00050048,
0x00000011, 0x00000007, 0x00000023, 0x0000001c, 0x00030047, 0x00000011, 0x00000003, 0x00040047, 0x00000013, 0x00000022,
0x00000000, 0x00040047, 0x00000013, 0x00000021, 0x00000001, 0x00040047, 0x0000001d, 0x00000006, 0x00000004, 0x00050048,
0x0000001e, 0x00000000, 0x00000023, 0x00000000, 0x00050048, 0x0000001e, 0x00000001, 0x00000023, 0x00000038, 0x00050048,
0x0000001e, 0x00000002, 0x00000023, 0x0000003c, 0x00040047, 0x0000001f, 0x00000006, 0x00000040, 0x00050048, 0x00000020,
0x00000000, 0x00000023, 0x00000000, 0x00030047, 0x00000020, 0x00000003, 0x00040047, 0x00000022, 0x00000022, 0x00000000,
0x00040047, 0x00000022, 0x00000021, 0x00000000, 0x00020013, 0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00040015,
0x00000006, 0x00000020, 0x00000000, 0x00040020, 0x00000007, 0x00000007, 0x00000006, 0x0004002b, 0x00000006, 0x00000009,
0x00000000, 0x0003001d, 0x00000010, 0x00000006, 0x000a001e, 0x00000011, 0x00000006, 0x00000006, 0x00000006, 0x00000006,
0x00000006, 0x00000006, 0x00000006, 0x00000010, 0x00040020, 0x00000012, 0x00000002, 0x00000011, 0x0004003b, 0x00000012,
0x00000013, 0x00000002, 0x00040015, 0x00000014, 0x00000020, 0x00000001, 0x0004002b, 0x00000014, 0x00000015, 0x00000000,
0x00040020, 0x00000016, 0x00000002, 0x00000006, 0x00020014, 0x00000019, 0x0004002b, 0x00000006, 0x0000001c, 0x0000000e,
0x0004001c, 0x0000001d, 0x00000006, 0x0000001c, 0x0005001e, 0x0000001e, 0x0000001d, 0x00000006, 0x00000006, 0x0003001d,
0x0000001f, 0x0000001e, 0x0003001e, 0x00000020, 0x0000001f, 0x00040020, 0x00000021, 0x00000002, 0x00000020, 0x0004003b,
0x00000021, 0x00000022, 0x00000002, 0x0004002b, 0x00000014, 0x00000024, 0x00000001, 0x0004002b, 0x00000014, 0x00000029,
0x00000002, 0x00040020, 0x0000002c, 0x00000007, 0x00000019, 0x0003002a, 0x00000019, 0x0000002e, 0x0004002b, 0x00000014,
0x00000036, 0x00000006, 0x0004002b, 0x00000014, 0x0000003b, 0x00000007, 0x0004002b, 0x00000006, 0x0000003c, 0x00000002,
0x0004002b, 0x00000006, 0x00000048, 0x00000001, 0x00030029, 0x00000019, 0x00000050, 0x0004002b, 0x00000014, 0x00000058,
0x00000003, 0x0004002b, 0x00000014, 0x0000005d, 0x00000004, 0x0004002b, 0x00000014, 0x00000060, 0x00000005, 0x00050036,
0x00000002, 0x00000004, 0x00000000, 0x00000003, 0x000200f8, 0x00000005, 0x0004003b, 0x00000007, 0x00000008, 0x00000007,
0x0004003b, 0x00000007, 0x0000001b, 0x00000007, 0x0004003b, 0x00000007, 0x00000027, 0x00000007, 0x0004003b, 0x0000002c,
0x0000002d, 0x00000007, 0x0004003b, 0x00000007, 0x0000002f, 0x00000007, 0x0003003e, 0x00000008, 0x00000009, 0x000200f9,
0x0000000a, 0x000200f8, 0x0000000a, 0x000400f6, 0x0000000c, 0x0000000d, 0x00000000, 0x000200f9, 0x0000000e, 0x000200f8,
0x0000000e, 0x0004003d, 0x00000006, 0x0000000f, 0x00000008, 0x00050041, 0x00000016, 0x00000017, 0x00000013, 0x00000015,
0x0004003d, 0x00000006, 0x00000018, 0x00000017, 0x000500b0, 0x00000019, 0x0000001a, 0x0000000f, 0x00000018, 0x000400fa,
0x0000001a, 0x0000000b, 0x0000000c, 0x000200f8, 0x0000000b, 0x0004003d, 0x00000006, 0x00000023, 0x00000008, 0x00070041,
0x00000016, 0x00000025, 0x00000022, 0x00000015, 0x00000023, 0x00000024, 0x0004003d, 0x00000006, 0x00000026, 0x00000025,
0x0003003e, 0x0000001b, 0x00000026, 0x0004003d, 0x00000006, 0x00000028, 0x00000008, 0x00070041, 0x00000016, 0x0000002a,
0x00000022, 0x00000015, 0x00000028, 0x00000029, 0x0004003d, 0x00000006, 0x0000002b, 0x0000002a, 0x0003003e, 0x00000027,
0x0000002b, 0x0003003e, 0x0000002d, 0x0000002e, 0x0003003e, 0x0000002f, 0x00000009, 0x000200f9, 0x00000030, 0x000200f8,
0x00000030, 0x000400f6, 0x00000032, 0x00000033, 0x00000000, 0x000200f9, 0x00000034, 0x000200f8, 0x00000034, 0x0004003d,
0x00000006, 0x00000035, 0x0000002f, 0x00050041, 0x00000016, 0x00000037, 0x00000013, 0x00000036, 0x0004003d, 0x00000006,
0x00000038, 0x00000037, 0x000500b0, 0x00000019, 0x00000039, 0x00000035, 0x00000038, 0x000400fa, 0x00000039, 0x00000031,
0x00000032, 0x000200f8, 0x00000031, 0x0004003d, 0x00000006, 0x0000003a, 0x0000001b, 0x0004003d, 0x00000006, 0x0000003d,
0x0000002f, 0x00050084, 0x00000006, 0x0000003e, 0x0000003c, 0x0000003d, 0x00050080, 0x00000006, 0x0000003f, 0x0000003e,
0x00000009, 0x00060041, 0x00000016, 0x00000040, 0x00000013, 0x0000003b, 0x0000003f, 0x0004003d, 0x00000006, 0x00000041,
0x00000040, 0x000500aa, 0x00000019, 0x00000042, 0x0000003a, 0x00000041, 0x000300f7, 0x00000044, 0x00000000, 0x000400fa,
0x00000042, 0x00000043, 0x00000044, 0x000200f8, 0x00000043, 0x0004003d, 0x00000006, 0x00000045, 0x00000027, 0x0004003d,
0x00000006, 0x00000046, 0x0000002f, 0x00050084, 0x00000006, 0x00000047, 0x0000003c, 0x00000046, 0x00050080, 0x00000006,
0x00000049, 0x00000047, 0x00000048, 0x00060041, 0x00000016, 0x0000004a, 0x00000013, 0x0000003b, 0x00000049, 0x0004003d,
0x00000006, 0x0000004b, 0x0000004a, 0x000500aa, 0x00000019, 0x0000004c, 0x00000045, 0x0000004b, 0x000200f9, 0x00000044,
0x000200f8, 0x00000044, 0x000700f5, 0x00000019, 0x0000004d, 0x00000042, 0x00000031, 0x0000004c, 0x00000043, 0x000300f7,
0x0000004f, 0x00000000, 0x000400fa, 0x0000004d, 0x0000004e, 0x0000004f, 0x000200f8, 0x0000004e, 0x0003003e, 0x0000002d,
0x00000050, 0x000200f9, 0x00000032, 0x000200f8, 0x0000004f, 0x000200f9, 0x00000033, 0x000200f8, 0x00000033, 0x0004003d,
0x00000006, 0x00000052, 0x0000002f, 0x00050080, 0x00000006, 0x00000053, 0x00000052, 0x00000024, 0x0003003e, 0x0000002f,
0x00000053, 0x000200f9, 0x00000030, 0x000200f8, 0x00000032, 0x0004003d, 0x00000019, 0x00000054, 0x0000002d, 0x000400a8,
0x00000019, 0x00000055, 0x00000054, 0x000300f7, 0x00000057, 0x00000000, 0x000400fa, 0x00000055, 0x00000056, 0x00000057,
0x000200f8, 0x00000056, 0x00050041, 0x00000016, 0x00000059, 0x00000013, 0x00000058, 0x0004003d, 0x00000006, 0x0000005a,
0x00000059, 0x00050080, 0x00000006, 0x0000005b, 0x0000005a, 0x00000048, 0x00050041, 0x00000016, 0x0000005c, 0x00000013,
0x00000058, 0x0003003e, 0x0000005c, 0x0000005b, 0x0004003d, 0x00000006, 0x0000005e, 0x0000001b, 0x00050041, 0x00000016,
0x0000005f, 0x00000013, 0x0000005d, 0x0003003e, 0x0000005f, 0x0000005e, 0x0004003d, 0x00000006, 0x00000061, 0x00000027,
0x00050041, 0x00000016, 0x00000062, 0x00000013, 0x00000060, 0x0003003e, 0x00000062, 0x00000061, 0x0004003d, 0x00000006,
0x00000063, 0x00000008, 0x00050041, 0x00000016, 0x00000064, 0x00000013, 0x00000024, 0x0004003d, 0x00000006, 0x00000065,
0x00000064, 0x00070041, 0x00000016, 0x00000066, 0x00000022, 0x00000015, 0x00000063, 0x00000024, 0x0003003e, 0x00000066,
0x00000065, 0x0004003d, 0x00000006, 0x00000067, 0x00000008, 0x00050041, 0x00000016, 0x00000068, 0x00000013, 0x00000029,
0x0004003d, 0x00000006, 0x00000069, 0x00000068, 0x00070041, 0x00000016, 0x0000006a, 0x00000022, 0x00000015, 0x00000067,
0x00000029, 0x0003003e, 0x0000006a, 0x00000069, 0x000200f9, 0x00000057, 0x000200f8, 0x00000057, 0x000200f9, 0x0000000d,
0x000200f8, 0x0000000d, 0x0004003d, 0x00000006, 0x0000006b, 0x00000008, 0x00050080, 0x00000006, 0x0000006c, 0x0000006b,
0x00000024, 0x0003003e, 0x00000008, 0x0000006c, 0x000200f9, 0x0000000a, 0x000200f8, 0x0000000c, 0x000100fd, 0x00010038};
// Convenience function for reporting problems with setting up GPU Validation.
template <typename T>
void GpuAssisted::ReportSetupProblem(T object, const char *const specific_message) const {
LogError(object, "UNASSIGNED-GPU-Assisted Validation Error. ", "Detail: (%s)", specific_message);
}
bool GpuAssisted::CheckForDescriptorIndexing(DeviceFeatures enabled_features) const {
bool result =
(IsExtEnabled(device_extensions.vk_ext_descriptor_indexing) &&
(enabled_features.core12.descriptorIndexing || enabled_features.core12.shaderInputAttachmentArrayDynamicIndexing ||
enabled_features.core12.shaderUniformTexelBufferArrayDynamicIndexing ||
enabled_features.core12.shaderStorageTexelBufferArrayDynamicIndexing ||
enabled_features.core12.shaderUniformBufferArrayNonUniformIndexing ||
enabled_features.core12.shaderSampledImageArrayNonUniformIndexing ||
enabled_features.core12.shaderStorageBufferArrayNonUniformIndexing ||
enabled_features.core12.shaderStorageImageArrayNonUniformIndexing ||
enabled_features.core12.shaderInputAttachmentArrayNonUniformIndexing ||
enabled_features.core12.shaderUniformTexelBufferArrayNonUniformIndexing ||
enabled_features.core12.shaderStorageTexelBufferArrayNonUniformIndexing ||
enabled_features.core12.descriptorBindingUniformBufferUpdateAfterBind ||
enabled_features.core12.descriptorBindingSampledImageUpdateAfterBind ||
enabled_features.core12.descriptorBindingStorageImageUpdateAfterBind ||
enabled_features.core12.descriptorBindingStorageBufferUpdateAfterBind ||
enabled_features.core12.descriptorBindingUniformTexelBufferUpdateAfterBind ||
enabled_features.core12.descriptorBindingStorageTexelBufferUpdateAfterBind ||
enabled_features.core12.descriptorBindingUpdateUnusedWhilePending ||
enabled_features.core12.descriptorBindingPartiallyBound ||
enabled_features.core12.descriptorBindingVariableDescriptorCount || enabled_features.core12.runtimeDescriptorArray));
return result;
}
void GpuAssisted::PreCallRecordCreateBuffer(VkDevice device, const VkBufferCreateInfo *pCreateInfo,
const VkAllocationCallbacks *pAllocator, VkBuffer *pBuffer, void *cb_state_data) {
// Ray tracing acceleration structure instance buffers also need the storage buffer usage as
// acceleration structure build validation will find and replace invalid acceleration structure
// handles inside of a compute shader.
create_buffer_api_state *cb_state = reinterpret_cast<create_buffer_api_state *>(cb_state_data);
if (cb_state && cb_state->modified_create_info.usage & VK_BUFFER_USAGE_RAY_TRACING_BIT_NV) {
cb_state->modified_create_info.usage |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
}
}
// Turn on necessary device features.
void GpuAssisted::PreCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *create_info,
const VkAllocationCallbacks *pAllocator, VkDevice *pDevice,
void *modified_create_info) {
DispatchGetPhysicalDeviceFeatures(gpu, &supported_features);
VkPhysicalDeviceFeatures features = {};
features.vertexPipelineStoresAndAtomics = true;
features.fragmentStoresAndAtomics = true;
features.shaderInt64 = true;
UtilPreCallRecordCreateDevice(gpu, reinterpret_cast<safe_VkDeviceCreateInfo *>(modified_create_info), supported_features,
features);
}
// Perform initializations that can be done at Create Device time.
void GpuAssisted::PostCallRecordCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
// The state tracker sets up the device state
ValidationStateTracker::PostCallRecordCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice, result);
ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, this->container_type);
GpuAssisted *device_gpu_assisted = static_cast<GpuAssisted *>(validation_data);
const char *bufferoob_string = getLayerOption("khronos_validation.gpuav_buffer_oob");
if (device_gpu_assisted->enabled_features.core.robustBufferAccess ||
device_gpu_assisted->enabled_features.robustness2_features.robustBufferAccess2) {
device_gpu_assisted->buffer_oob_enabled = false;
} else {
device_gpu_assisted->buffer_oob_enabled = *bufferoob_string ? !strcmp(bufferoob_string, "true") : true;
}
if (device_gpu_assisted->phys_dev_props.apiVersion < VK_API_VERSION_1_1) {
ReportSetupProblem(device, "GPU-Assisted validation requires Vulkan 1.1 or later. GPU-Assisted Validation disabled.");
device_gpu_assisted->aborted = true;
return;
}
if (!supported_features.fragmentStoresAndAtomics || !supported_features.vertexPipelineStoresAndAtomics) {
ReportSetupProblem(device,
"GPU-Assisted validation requires fragmentStoresAndAtomics and vertexPipelineStoresAndAtomics. "
"GPU-Assisted Validation disabled.");
device_gpu_assisted->aborted = true;
return;
}
if ((device_extensions.vk_ext_buffer_device_address || device_extensions.vk_khr_buffer_device_address) &&
!supported_features.shaderInt64) {
LogWarning(device, "UNASSIGNED-GPU-Assisted Validation Warning",
"shaderInt64 feature is not available. No buffer device address checking will be attempted");
}
device_gpu_assisted->shaderInt64 = supported_features.shaderInt64;
device_gpu_assisted->physicalDevice = physicalDevice;
device_gpu_assisted->device = *pDevice;
device_gpu_assisted->output_buffer_size = sizeof(uint32_t) * (spvtools::kInstMaxOutCnt + 1);
device_gpu_assisted->descriptor_indexing = CheckForDescriptorIndexing(device_gpu_assisted->enabled_features);
std::vector<VkDescriptorSetLayoutBinding> bindings;
VkDescriptorSetLayoutBinding binding = {0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT |
VK_SHADER_STAGE_MESH_BIT_NV | VK_SHADER_STAGE_TASK_BIT_NV |
kShaderStageAllRayTracing,
NULL};
bindings.push_back(binding);
for (auto i = 1; i < 3; i++) {
binding.binding = i;
bindings.push_back(binding);
}
UtilPostCallRecordCreateDevice(pCreateInfo, bindings, device_gpu_assisted, device_gpu_assisted->phys_dev_props);
CreateAccelerationStructureBuildValidationState(device_gpu_assisted);
}
void GpuAssisted::PostCallRecordGetBufferDeviceAddress(VkDevice device, const VkBufferDeviceAddressInfo *pInfo,
VkDeviceAddress address) {
BUFFER_STATE *buffer_state = GetBufferState(pInfo->buffer);
// Validate against the size requested when the buffer was created
if (buffer_state) {
buffer_map[address] = buffer_state->createInfo.size;
buffer_state->deviceAddress = address;
}
}
void GpuAssisted::PostCallRecordGetBufferDeviceAddressEXT(VkDevice device, const VkBufferDeviceAddressInfo *pInfo,
VkDeviceAddress address) {
PostCallRecordGetBufferDeviceAddress(device, pInfo, address);
}
void GpuAssisted::PostCallRecordGetBufferDeviceAddressKHR(VkDevice device, const VkBufferDeviceAddressInfo *pInfo,
VkDeviceAddress address) {
PostCallRecordGetBufferDeviceAddress(device, pInfo, address);
}
void GpuAssisted::PreCallRecordDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator) {
BUFFER_STATE *buffer_state = GetBufferState(buffer);
if (buffer_state) buffer_map.erase(buffer_state->deviceAddress);
ValidationStateTracker::PreCallRecordDestroyBuffer(device, buffer, pAllocator);
}
// Clean up device-related resources
void GpuAssisted::PreCallRecordDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) {
DestroyAccelerationStructureBuildValidationState();
UtilPreCallRecordDestroyDevice(this);
ValidationStateTracker::PreCallRecordDestroyDevice(device, pAllocator);
// State Tracker can end up making vma calls through callbacks - don't destroy allocator until ST is done
if (vmaAllocator) {
vmaDestroyAllocator(vmaAllocator);
}
desc_set_manager.reset();
}
void GpuAssisted::CreateAccelerationStructureBuildValidationState(GpuAssisted *device_gpuav) {
if (device_gpuav->aborted) {
return;
}
auto &as_validation_state = device_gpuav->acceleration_structure_validation_state;
if (as_validation_state.initialized) {
return;
}
if (!device_extensions.vk_nv_ray_tracing) {
return;
}
// Outline:
// - Create valid bottom level acceleration structure which acts as replacement
// - Create and load vertex buffer
// - Create and load index buffer
// - Create, allocate memory for, and bind memory for acceleration structure
// - Query acceleration structure handle
// - Create command pool and command buffer
// - Record build acceleration structure command
// - Submit command buffer and wait for completion
// - Cleanup
// - Create compute pipeline for validating instance buffers
// - Create descriptor set layout
// - Create pipeline layout
// - Create pipeline
// - Cleanup
VkResult result = VK_SUCCESS;
VkBuffer vbo = VK_NULL_HANDLE;
VmaAllocation vbo_allocation = VK_NULL_HANDLE;
if (result == VK_SUCCESS) {
VkBufferCreateInfo vbo_ci = {};
vbo_ci.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
vbo_ci.size = sizeof(float) * 9;
vbo_ci.usage = VK_BUFFER_USAGE_RAY_TRACING_BIT_NV;
VmaAllocationCreateInfo vbo_ai = {};
vbo_ai.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
vbo_ai.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
result = vmaCreateBuffer(device_gpuav->vmaAllocator, &vbo_ci, &vbo_ai, &vbo, &vbo_allocation, nullptr);
if (result != VK_SUCCESS) {
ReportSetupProblem(device, "Failed to create vertex buffer for acceleration structure build validation.");
}
}
if (result == VK_SUCCESS) {
uint8_t *mapped_vbo_buffer = nullptr;
result = vmaMapMemory(device_gpuav->vmaAllocator, vbo_allocation, reinterpret_cast<void **>(&mapped_vbo_buffer));
if (result != VK_SUCCESS) {
ReportSetupProblem(device, "Failed to map vertex buffer for acceleration structure build validation.");
} else {
const std::vector<float> vertices = {1.0f, 0.0f, 0.0f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f};
std::memcpy(mapped_vbo_buffer, (uint8_t *)vertices.data(), sizeof(float) * vertices.size());
vmaUnmapMemory(device_gpuav->vmaAllocator, vbo_allocation);
}
}
VkBuffer ibo = VK_NULL_HANDLE;
VmaAllocation ibo_allocation = VK_NULL_HANDLE;
if (result == VK_SUCCESS) {
VkBufferCreateInfo ibo_ci = {};
ibo_ci.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
ibo_ci.size = sizeof(uint32_t) * 3;
ibo_ci.usage = VK_BUFFER_USAGE_RAY_TRACING_BIT_NV;
VmaAllocationCreateInfo ibo_ai = {};
ibo_ai.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
ibo_ai.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
result = vmaCreateBuffer(device_gpuav->vmaAllocator, &ibo_ci, &ibo_ai, &ibo, &ibo_allocation, nullptr);
if (result != VK_SUCCESS) {
ReportSetupProblem(device, "Failed to create index buffer for acceleration structure build validation.");
}
}
if (result == VK_SUCCESS) {
uint8_t *mapped_ibo_buffer = nullptr;
result = vmaMapMemory(device_gpuav->vmaAllocator, ibo_allocation, reinterpret_cast<void **>(&mapped_ibo_buffer));
if (result != VK_SUCCESS) {
ReportSetupProblem(device, "Failed to map index buffer for acceleration structure build validation.");
} else {
const std::vector<uint32_t> indicies = {0, 1, 2};
std::memcpy(mapped_ibo_buffer, (uint8_t *)indicies.data(), sizeof(uint32_t) * indicies.size());
vmaUnmapMemory(device_gpuav->vmaAllocator, ibo_allocation);
}
}
VkGeometryNV geometry = {};
geometry.sType = VK_STRUCTURE_TYPE_GEOMETRY_NV;
geometry.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_NV;
geometry.geometry.triangles.sType = VK_STRUCTURE_TYPE_GEOMETRY_TRIANGLES_NV;
geometry.geometry.triangles.vertexData = vbo;
geometry.geometry.triangles.vertexOffset = 0;
geometry.geometry.triangles.vertexCount = 3;
geometry.geometry.triangles.vertexStride = 12;
geometry.geometry.triangles.vertexFormat = VK_FORMAT_R32G32B32_SFLOAT;
geometry.geometry.triangles.indexData = ibo;
geometry.geometry.triangles.indexOffset = 0;
geometry.geometry.triangles.indexCount = 3;
geometry.geometry.triangles.indexType = VK_INDEX_TYPE_UINT32;
geometry.geometry.triangles.transformData = VK_NULL_HANDLE;
geometry.geometry.triangles.transformOffset = 0;
geometry.geometry.aabbs = {};
geometry.geometry.aabbs.sType = VK_STRUCTURE_TYPE_GEOMETRY_AABB_NV;
VkAccelerationStructureCreateInfoNV as_ci = {};
as_ci.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_INFO_NV;
as_ci.info.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_INFO_NV;
as_ci.info.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_NV;
as_ci.info.instanceCount = 0;
as_ci.info.geometryCount = 1;
as_ci.info.pGeometries = &geometry;
if (result == VK_SUCCESS) {
result = DispatchCreateAccelerationStructureNV(device_gpuav->device, &as_ci, nullptr, &as_validation_state.replacement_as);
if (result != VK_SUCCESS) {
ReportSetupProblem(device_gpuav->device,
"Failed to create acceleration structure for acceleration structure build validation.");
}
}
VkMemoryRequirements2 as_mem_requirements = {};
if (result == VK_SUCCESS) {
VkAccelerationStructureMemoryRequirementsInfoNV as_mem_requirements_info = {};
as_mem_requirements_info.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_INFO_NV;
as_mem_requirements_info.type = VK_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_TYPE_OBJECT_NV;
as_mem_requirements_info.accelerationStructure = as_validation_state.replacement_as;
DispatchGetAccelerationStructureMemoryRequirementsNV(device_gpuav->device, &as_mem_requirements_info, &as_mem_requirements);
}
VmaAllocationInfo as_memory_ai = {};
if (result == VK_SUCCESS) {
VmaAllocationCreateInfo as_memory_aci = {};
as_memory_aci.usage = VMA_MEMORY_USAGE_GPU_ONLY;
result = vmaAllocateMemory(device_gpuav->vmaAllocator, &as_mem_requirements.memoryRequirements, &as_memory_aci,
&as_validation_state.replacement_as_allocation, &as_memory_ai);
if (result != VK_SUCCESS) {
ReportSetupProblem(device_gpuav->device,
"Failed to alloc acceleration structure memory for acceleration structure build validation.");
}
}
if (result == VK_SUCCESS) {
VkBindAccelerationStructureMemoryInfoNV as_bind_info = {};
as_bind_info.sType = VK_STRUCTURE_TYPE_BIND_ACCELERATION_STRUCTURE_MEMORY_INFO_NV;
as_bind_info.accelerationStructure = as_validation_state.replacement_as;
as_bind_info.memory = as_memory_ai.deviceMemory;
as_bind_info.memoryOffset = as_memory_ai.offset;
result = DispatchBindAccelerationStructureMemoryNV(device_gpuav->device, 1, &as_bind_info);
if (result != VK_SUCCESS) {
ReportSetupProblem(device_gpuav->device,
"Failed to bind acceleration structure memory for acceleration structure build validation.");
}
}
if (result == VK_SUCCESS) {
result = DispatchGetAccelerationStructureHandleNV(device_gpuav->device, as_validation_state.replacement_as,
sizeof(uint64_t), &as_validation_state.replacement_as_handle);
if (result != VK_SUCCESS) {
ReportSetupProblem(device_gpuav->device,
"Failed to get acceleration structure handle for acceleration structure build validation.");
}
}
VkMemoryRequirements2 scratch_mem_requirements = {};
if (result == VK_SUCCESS) {
VkAccelerationStructureMemoryRequirementsInfoNV scratch_mem_requirements_info = {};
scratch_mem_requirements_info.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_INFO_NV;
scratch_mem_requirements_info.type = VK_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_TYPE_BUILD_SCRATCH_NV;
scratch_mem_requirements_info.accelerationStructure = as_validation_state.replacement_as;
DispatchGetAccelerationStructureMemoryRequirementsNV(device_gpuav->device, &scratch_mem_requirements_info,
&scratch_mem_requirements);
}
VkBuffer scratch = VK_NULL_HANDLE;
VmaAllocation scratch_allocation = {};
if (result == VK_SUCCESS) {
VkBufferCreateInfo scratch_ci = {};
scratch_ci.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
scratch_ci.size = scratch_mem_requirements.memoryRequirements.size;
scratch_ci.usage = VK_BUFFER_USAGE_RAY_TRACING_BIT_NV;
VmaAllocationCreateInfo scratch_aci = {};
scratch_aci.usage = VMA_MEMORY_USAGE_GPU_ONLY;
result = vmaCreateBuffer(device_gpuav->vmaAllocator, &scratch_ci, &scratch_aci, &scratch, &scratch_allocation, nullptr);
if (result != VK_SUCCESS) {
ReportSetupProblem(device_gpuav->device,
"Failed to create scratch buffer for acceleration structure build validation.");
}
}
VkCommandPool command_pool = VK_NULL_HANDLE;
if (result == VK_SUCCESS) {
VkCommandPoolCreateInfo command_pool_ci = {};
command_pool_ci.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
command_pool_ci.queueFamilyIndex = 0;
result = DispatchCreateCommandPool(device_gpuav->device, &command_pool_ci, nullptr, &command_pool);
if (result != VK_SUCCESS) {
ReportSetupProblem(device_gpuav->device, "Failed to create command pool for acceleration structure build validation.");
}
}
VkCommandBuffer command_buffer = VK_NULL_HANDLE;
if (result == VK_SUCCESS) {
VkCommandBufferAllocateInfo command_buffer_ai = {};
command_buffer_ai.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
command_buffer_ai.commandPool = command_pool;
command_buffer_ai.commandBufferCount = 1;
command_buffer_ai.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
result = DispatchAllocateCommandBuffers(device_gpuav->device, &command_buffer_ai, &command_buffer);
if (result != VK_SUCCESS) {
ReportSetupProblem(device_gpuav->device,
"Failed to create command buffer for acceleration structure build validation.");
}
// Hook up command buffer dispatch
device_gpuav->vkSetDeviceLoaderData(device_gpuav->device, command_buffer);
}
if (result == VK_SUCCESS) {
VkCommandBufferBeginInfo command_buffer_bi = {};
command_buffer_bi.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
result = DispatchBeginCommandBuffer(command_buffer, &command_buffer_bi);
if (result != VK_SUCCESS) {
ReportSetupProblem(device_gpuav->device, "Failed to begin command buffer for acceleration structure build validation.");
}
}
if (result == VK_SUCCESS) {
DispatchCmdBuildAccelerationStructureNV(command_buffer, &as_ci.info, VK_NULL_HANDLE, 0, VK_FALSE,
as_validation_state.replacement_as, VK_NULL_HANDLE, scratch, 0);
DispatchEndCommandBuffer(command_buffer);
}
VkQueue queue = VK_NULL_HANDLE;
if (result == VK_SUCCESS) {
DispatchGetDeviceQueue(device_gpuav->device, 0, 0, &queue);
// Hook up queue dispatch
device_gpuav->vkSetDeviceLoaderData(device_gpuav->device, queue);
VkSubmitInfo submit_info = {};
submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submit_info.commandBufferCount = 1;
submit_info.pCommandBuffers = &command_buffer;
result = DispatchQueueSubmit(queue, 1, &submit_info, VK_NULL_HANDLE);
if (result != VK_SUCCESS) {
ReportSetupProblem(device_gpuav->device,
"Failed to submit command buffer for acceleration structure build validation.");
}
}
if (result == VK_SUCCESS) {
result = DispatchQueueWaitIdle(queue);
if (result != VK_SUCCESS) {
ReportSetupProblem(device_gpuav->device, "Failed to wait for queue idle for acceleration structure build validation.");
}
}
if (vbo != VK_NULL_HANDLE) {
vmaDestroyBuffer(device_gpuav->vmaAllocator, vbo, vbo_allocation);
}
if (ibo != VK_NULL_HANDLE) {
vmaDestroyBuffer(device_gpuav->vmaAllocator, ibo, ibo_allocation);
}
if (scratch != VK_NULL_HANDLE) {
vmaDestroyBuffer(device_gpuav->vmaAllocator, scratch, scratch_allocation);
}
if (command_pool != VK_NULL_HANDLE) {
DispatchDestroyCommandPool(device_gpuav->device, command_pool, nullptr);
}
if (device_gpuav->debug_desc_layout == VK_NULL_HANDLE) {
ReportSetupProblem(device_gpuav->device,
"Failed to find descriptor set layout for acceleration structure build validation.");
result = VK_INCOMPLETE;
}
if (result == VK_SUCCESS) {
VkPipelineLayoutCreateInfo pipeline_layout_ci = {};
pipeline_layout_ci.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipeline_layout_ci.setLayoutCount = 1;
pipeline_layout_ci.pSetLayouts = &device_gpuav->debug_desc_layout;
result = DispatchCreatePipelineLayout(device_gpuav->device, &pipeline_layout_ci, 0, &as_validation_state.pipeline_layout);
if (result != VK_SUCCESS) {
ReportSetupProblem(device_gpuav->device,
"Failed to create pipeline layout for acceleration structure build validation.");
}
}
VkShaderModule shader_module = VK_NULL_HANDLE;
if (result == VK_SUCCESS) {
VkShaderModuleCreateInfo shader_module_ci = {};
shader_module_ci.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
shader_module_ci.codeSize = sizeof(kComputeShaderSpirv);
shader_module_ci.pCode = (uint32_t *)kComputeShaderSpirv;
result = DispatchCreateShaderModule(device_gpuav->device, &shader_module_ci, nullptr, &shader_module);
if (result != VK_SUCCESS) {
ReportSetupProblem(device_gpuav->device,
"Failed to create compute shader module for acceleration structure build validation.");
}
}
if (result == VK_SUCCESS) {
VkPipelineShaderStageCreateInfo pipeline_stage_ci = {};
pipeline_stage_ci.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
pipeline_stage_ci.stage = VK_SHADER_STAGE_COMPUTE_BIT;
pipeline_stage_ci.module = shader_module;
pipeline_stage_ci.pName = "main";
VkComputePipelineCreateInfo pipeline_ci = {};
pipeline_ci.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
pipeline_ci.stage = pipeline_stage_ci;
pipeline_ci.layout = as_validation_state.pipeline_layout;
result = DispatchCreateComputePipelines(device_gpuav->device, VK_NULL_HANDLE, 1, &pipeline_ci, nullptr,
&as_validation_state.pipeline);
if (result != VK_SUCCESS) {
ReportSetupProblem(device_gpuav->device,
"Failed to create compute pipeline for acceleration structure build validation.");
}
}
if (shader_module != VK_NULL_HANDLE) {
DispatchDestroyShaderModule(device_gpuav->device, shader_module, nullptr);
}
if (result == VK_SUCCESS) {
as_validation_state.initialized = true;
LogInfo(device_gpuav->device, "UNASSIGNED-GPU-Assisted Validation.",
"Acceleration Structure Building GPU Validation Enabled.");
} else {
device_gpuav->aborted = true;
}
}
void GpuAssisted::DestroyAccelerationStructureBuildValidationState() {
auto &as_validation_state = acceleration_structure_validation_state;
if (as_validation_state.pipeline != VK_NULL_HANDLE) {
DispatchDestroyPipeline(device, as_validation_state.pipeline, nullptr);
}
if (as_validation_state.pipeline_layout != VK_NULL_HANDLE) {
DispatchDestroyPipelineLayout(device, as_validation_state.pipeline_layout, nullptr);
}
if (as_validation_state.replacement_as != VK_NULL_HANDLE) {
DispatchDestroyAccelerationStructureNV(device, as_validation_state.replacement_as, nullptr);
}
if (as_validation_state.replacement_as_allocation != VK_NULL_HANDLE) {
vmaFreeMemory(vmaAllocator, as_validation_state.replacement_as_allocation);
}
}
struct GPUAV_RESTORABLE_PIPELINE_STATE {
VkPipelineBindPoint pipeline_bind_point = VK_PIPELINE_BIND_POINT_MAX_ENUM;
VkPipeline pipeline = VK_NULL_HANDLE;
VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
std::vector<VkDescriptorSet> descriptor_sets;
std::vector<std::vector<uint32_t>> dynamic_offsets;
uint32_t push_descriptor_set_index = 0;
std::vector<safe_VkWriteDescriptorSet> push_descriptor_set_writes;
std::vector<uint8_t> push_constants_data;
PushConstantRangesId push_constants_ranges;
void Create(CMD_BUFFER_STATE *cb_state, VkPipelineBindPoint bind_point) {
pipeline_bind_point = bind_point;
const auto lv_bind_point = ConvertToLvlBindPoint(bind_point);
LAST_BOUND_STATE &last_bound = cb_state->lastBound[lv_bind_point];
if (last_bound.pipeline_state) {
pipeline = last_bound.pipeline_state->pipeline;
pipeline_layout = last_bound.pipeline_layout;
descriptor_sets.reserve(last_bound.per_set.size());
for (std::size_t i = 0; i < last_bound.per_set.size(); i++) {
const auto *bound_descriptor_set = last_bound.per_set[i].bound_descriptor_set;
descriptor_sets.push_back(bound_descriptor_set->GetSet());
if (bound_descriptor_set->IsPushDescriptor()) {
push_descriptor_set_index = static_cast<uint32_t>(i);
}
dynamic_offsets.push_back(last_bound.per_set[i].dynamicOffsets);
}
if (last_bound.push_descriptor_set) {
push_descriptor_set_writes = last_bound.push_descriptor_set->GetWrites();
}
if (last_bound.pipeline_state->pipeline_layout->push_constant_ranges == cb_state->push_constant_data_ranges) {
push_constants_data = cb_state->push_constant_data;
push_constants_ranges = last_bound.pipeline_state->pipeline_layout->push_constant_ranges;
}
}
}
void Restore(VkCommandBuffer command_buffer) const {
if (pipeline != VK_NULL_HANDLE) {
DispatchCmdBindPipeline(command_buffer, pipeline_bind_point, pipeline);
if (!descriptor_sets.empty()) {
for (std::size_t i = 0; i < descriptor_sets.size(); i++) {
VkDescriptorSet descriptor_set = descriptor_sets[i];
if (descriptor_set != VK_NULL_HANDLE) {
DispatchCmdBindDescriptorSets(command_buffer, pipeline_bind_point, pipeline_layout,
static_cast<uint32_t>(i), 1, &descriptor_set,
static_cast<uint32_t>(dynamic_offsets[i].size()), dynamic_offsets[i].data());
}
}
}
if (!push_descriptor_set_writes.empty()) {
DispatchCmdPushDescriptorSetKHR(command_buffer, pipeline_bind_point, pipeline_layout, push_descriptor_set_index,
static_cast<uint32_t>(push_descriptor_set_writes.size()),
reinterpret_cast<const VkWriteDescriptorSet *>(push_descriptor_set_writes.data()));
}
for (const auto &push_constant_range : *push_constants_ranges) {
if (push_constant_range.size == 0) continue;
DispatchCmdPushConstants(command_buffer, pipeline_layout, push_constant_range.stageFlags,
push_constant_range.offset, push_constant_range.size, push_constants_data.data());
}
}
}
};
void GpuAssisted::PreCallRecordCmdBuildAccelerationStructureNV(VkCommandBuffer commandBuffer,
const VkAccelerationStructureInfoNV *pInfo, VkBuffer instanceData,
VkDeviceSize instanceOffset, VkBool32 update,
VkAccelerationStructureNV dst, VkAccelerationStructureNV src,
VkBuffer scratch, VkDeviceSize scratchOffset) {
if (pInfo == nullptr || pInfo->type != VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_NV) {
return;
}
auto &as_validation_state = acceleration_structure_validation_state;
if (!as_validation_state.initialized) {
return;
}
// Empty acceleration structure is valid according to the spec.
if (pInfo->instanceCount == 0 || instanceData == VK_NULL_HANDLE) {
return;
}
CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
assert(cb_state != nullptr);
std::vector<uint64_t> current_valid_handles;
for (const auto &as_state_kv : accelerationStructureMap) {
const ACCELERATION_STRUCTURE_STATE &as_state = *as_state_kv.second;
if (as_state.built && as_state.create_infoNV.info.type == VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_NV) {
current_valid_handles.push_back(as_state.opaque_handle);
}
}
GpuAssistedAccelerationStructureBuildValidationBufferInfo as_validation_buffer_info = {};
as_validation_buffer_info.acceleration_structure = dst;
const VkDeviceSize validation_buffer_size =
// One uint for number of instances to validate
4 +
// Two uint for the replacement acceleration structure handle
8 +
// One uint for number of invalid handles found
4 +
// Two uint for the first invalid handle found
8 +
// One uint for the number of current valid handles
4 +
// Two uint for each current valid handle
(8 * current_valid_handles.size());
VkBufferCreateInfo validation_buffer_create_info = {};
validation_buffer_create_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
validation_buffer_create_info.size = validation_buffer_size;
validation_buffer_create_info.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
VmaAllocationCreateInfo validation_buffer_alloc_info = {};
validation_buffer_alloc_info.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
VkResult result = vmaCreateBuffer(vmaAllocator, &validation_buffer_create_info, &validation_buffer_alloc_info,
&as_validation_buffer_info.validation_buffer,
&as_validation_buffer_info.validation_buffer_allocation, nullptr);
if (result != VK_SUCCESS) {
ReportSetupProblem(device, "Unable to allocate device memory. Device could become unstable.");
aborted = true;
return;
}
GpuAccelerationStructureBuildValidationBuffer *mapped_validation_buffer = nullptr;
result = vmaMapMemory(vmaAllocator, as_validation_buffer_info.validation_buffer_allocation,
reinterpret_cast<void **>(&mapped_validation_buffer));
if (result != VK_SUCCESS) {
ReportSetupProblem(device, "Unable to allocate device memory for acceleration structure build val buffer.");
aborted = true;
return;
}
mapped_validation_buffer->instances_to_validate = pInfo->instanceCount;
mapped_validation_buffer->replacement_handle_bits_0 =
reinterpret_cast<const uint32_t *>(&as_validation_state.replacement_as_handle)[0];
mapped_validation_buffer->replacement_handle_bits_1 =
reinterpret_cast<const uint32_t *>(&as_validation_state.replacement_as_handle)[1];
mapped_validation_buffer->invalid_handle_found = 0;
mapped_validation_buffer->invalid_handle_bits_0 = 0;
mapped_validation_buffer->invalid_handle_bits_1 = 0;
mapped_validation_buffer->valid_handles_count = static_cast<uint32_t>(current_valid_handles.size());
uint32_t *mapped_valid_handles = reinterpret_cast<uint32_t *>(&mapped_validation_buffer[1]);
for (std::size_t i = 0; i < current_valid_handles.size(); i++) {
const uint64_t current_valid_handle = current_valid_handles[i];
*mapped_valid_handles = reinterpret_cast<const uint32_t *>(¤t_valid_handle)[0];
++mapped_valid_handles;
*mapped_valid_handles = reinterpret_cast<const uint32_t *>(¤t_valid_handle)[1];
++mapped_valid_handles;
}
vmaUnmapMemory(vmaAllocator, as_validation_buffer_info.validation_buffer_allocation);
static constexpr const VkDeviceSize k_instance_size = 64;
const VkDeviceSize instance_buffer_size = k_instance_size * pInfo->instanceCount;
result = desc_set_manager->GetDescriptorSet(&as_validation_buffer_info.descriptor_pool, debug_desc_layout,
&as_validation_buffer_info.descriptor_set);
if (result != VK_SUCCESS) {
ReportSetupProblem(device, "Unable to get descriptor set for acceleration structure build.");
aborted = true;
return;
}
VkDescriptorBufferInfo descriptor_buffer_infos[2] = {};
descriptor_buffer_infos[0].buffer = instanceData;
descriptor_buffer_infos[0].offset = instanceOffset;
descriptor_buffer_infos[0].range = instance_buffer_size;
descriptor_buffer_infos[1].buffer = as_validation_buffer_info.validation_buffer;
descriptor_buffer_infos[1].offset = 0;
descriptor_buffer_infos[1].range = validation_buffer_size;
VkWriteDescriptorSet descriptor_set_writes[2] = {};
descriptor_set_writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptor_set_writes[0].dstSet = as_validation_buffer_info.descriptor_set;
descriptor_set_writes[0].dstBinding = 0;
descriptor_set_writes[0].descriptorCount = 1;
descriptor_set_writes[0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
descriptor_set_writes[0].pBufferInfo = &descriptor_buffer_infos[0];
descriptor_set_writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptor_set_writes[1].dstSet = as_validation_buffer_info.descriptor_set;
descriptor_set_writes[1].dstBinding = 1;
descriptor_set_writes[1].descriptorCount = 1;
descriptor_set_writes[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
descriptor_set_writes[1].pBufferInfo = &descriptor_buffer_infos[1];
DispatchUpdateDescriptorSets(device, 2, descriptor_set_writes, 0, nullptr);
// Issue a memory barrier to make sure anything writing to the instance buffer has finished.
VkMemoryBarrier memory_barrier = {};
memory_barrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
memory_barrier.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT;
memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
DispatchCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 1,
&memory_barrier, 0, nullptr, 0, nullptr);
// Save a copy of the compute pipeline state that needs to be restored.
GPUAV_RESTORABLE_PIPELINE_STATE restorable_state;
restorable_state.Create(cb_state, VK_PIPELINE_BIND_POINT_COMPUTE);
// Switch to and launch the validation compute shader to find, replace, and report invalid acceleration structure handles.
DispatchCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, as_validation_state.pipeline);
DispatchCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, as_validation_state.pipeline_layout, 0, 1,
&as_validation_buffer_info.descriptor_set, 0, nullptr);
DispatchCmdDispatch(commandBuffer, 1, 1, 1);
// Issue a buffer memory barrier to make sure that any invalid bottom level acceleration structure handles
// have been replaced by the validation compute shader before any builds take place.
VkBufferMemoryBarrier instance_buffer_barrier = {};
instance_buffer_barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
instance_buffer_barrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
instance_buffer_barrier.dstAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_NV;
instance_buffer_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
instance_buffer_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
instance_buffer_barrier.buffer = instanceData;
instance_buffer_barrier.offset = instanceOffset;
instance_buffer_barrier.size = instance_buffer_size;
DispatchCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_NV, 0, 0, nullptr, 1, &instance_buffer_barrier, 0,
nullptr);
// Restore the previous compute pipeline state.
restorable_state.Restore(commandBuffer);
as_validation_state.validation_buffers[commandBuffer].push_back(std::move(as_validation_buffer_info));
}
void GpuAssisted::ProcessAccelerationStructureBuildValidationBuffer(VkQueue queue, CMD_BUFFER_STATE *cb_node) {
if (cb_node == nullptr || !cb_node->hasBuildAccelerationStructureCmd) {
return;
}
auto &as_validation_info = acceleration_structure_validation_state;
auto &as_validation_buffer_infos = as_validation_info.validation_buffers[cb_node->commandBuffer];
for (const auto &as_validation_buffer_info : as_validation_buffer_infos) {
GpuAccelerationStructureBuildValidationBuffer *mapped_validation_buffer = nullptr;
VkResult result = vmaMapMemory(vmaAllocator, as_validation_buffer_info.validation_buffer_allocation,
reinterpret_cast<void **>(&mapped_validation_buffer));
if (result == VK_SUCCESS) {
if (mapped_validation_buffer->invalid_handle_found > 0) {
uint64_t invalid_handle = 0;
reinterpret_cast<uint32_t *>(&invalid_handle)[0] = mapped_validation_buffer->invalid_handle_bits_0;
reinterpret_cast<uint32_t *>(&invalid_handle)[1] = mapped_validation_buffer->invalid_handle_bits_1;
LogError(as_validation_buffer_info.acceleration_structure, "UNASSIGNED-AccelerationStructure",
"Attempted to build top level acceleration structure using invalid bottom level acceleration structure "
"handle (%" PRIu64 ")",
invalid_handle);
}
vmaUnmapMemory(vmaAllocator, as_validation_buffer_info.validation_buffer_allocation);
}
}
}
void GpuAssisted::PostCallRecordBindAccelerationStructureMemoryNV(VkDevice device, uint32_t bindInfoCount,
const VkBindAccelerationStructureMemoryInfoNV *pBindInfos,
VkResult result) {
if (VK_SUCCESS != result) return;
ValidationStateTracker::PostCallRecordBindAccelerationStructureMemoryNV(device, bindInfoCount, pBindInfos, result);
for (uint32_t i = 0; i < bindInfoCount; i++) {
const VkBindAccelerationStructureMemoryInfoNV &info = pBindInfos[i];
ACCELERATION_STRUCTURE_STATE *as_state = GetAccelerationStructureStateNV(info.accelerationStructure);
if (as_state) {
DispatchGetAccelerationStructureHandleNV(device, info.accelerationStructure, 8, &as_state->opaque_handle);
}
}
}
// Modify the pipeline layout to include our debug descriptor set and any needed padding with the dummy descriptor set.
void GpuAssisted::PreCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo,
const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout,
void *cpl_state_data) {
if (aborted) {
return;
}
create_pipeline_layout_api_state *cpl_state = reinterpret_cast<create_pipeline_layout_api_state *>(cpl_state_data);
if (cpl_state->modified_create_info.setLayoutCount >= adjusted_max_desc_sets) {
std::ostringstream strm;
strm << "Pipeline Layout conflict with validation's descriptor set at slot " << desc_set_bind_index << ". "
<< "Application has too many descriptor sets in the pipeline layout to continue with gpu validation. "
<< "Validation is not modifying the pipeline layout. "
<< "Instrumented shaders are replaced with non-instrumented shaders.";
ReportSetupProblem(device, strm.str().c_str());
} else {
UtilPreCallRecordCreatePipelineLayout(cpl_state, this, pCreateInfo);
}
}
void GpuAssisted::PostCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo,
const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout,
VkResult result) {
ValidationStateTracker::PostCallRecordCreatePipelineLayout(device, pCreateInfo, pAllocator, pPipelineLayout, result);