forked from KhronosGroup/Vulkan-ValidationLayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvklayertests_gpu.cpp
2631 lines (2356 loc) · 130 KB
/
vklayertests_gpu.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) 2020-2023 The Khronos Group Inc.
* Copyright (c) 2020-2023 Valve Corporation
* Copyright (c) 2020-2023 LunarG, Inc.
* Copyright (c) 2020-2023 Google, 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
*/
#include "layer_validation_tests.h"
static VkValidationFeatureEnableEXT gpu_av_enables[] = {VK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_EXT};
static VkValidationFeatureDisableEXT gpu_av_disables[] = {
VK_VALIDATION_FEATURE_DISABLE_THREAD_SAFETY_EXT, VK_VALIDATION_FEATURE_DISABLE_API_PARAMETERS_EXT,
VK_VALIDATION_FEATURE_DISABLE_OBJECT_LIFETIMES_EXT, VK_VALIDATION_FEATURE_DISABLE_CORE_CHECKS_EXT};
// All VkGpuAssistedLayerTest should use this for setup as a single access point to more easily toggle which validation features are
// enabled/disabled
VkValidationFeaturesEXT VkGpuAssistedLayerTest::GetValidationFeatures() {
VkValidationFeaturesEXT features = LvlInitStruct<VkValidationFeaturesEXT>();
features.enabledValidationFeatureCount = 1;
// TODO - Add command line flag or env var or another system for setting this to 'zero' to allow for someone writting a new
// GPU-AV test to easily check the test is valid
features.disabledValidationFeatureCount = 4;
features.pEnabledValidationFeatures = gpu_av_enables;
features.pDisabledValidationFeatures = gpu_av_disables;
return features;
}
// This checks any requirements needed for GPU-AV are met otherwise devices not meeting them will "fail" the tests
bool VkGpuAssistedLayerTest::CanEnableGpuAV() {
// Check version first before trying to call GetPhysicalDeviceFeatures2
if (DeviceValidationVersion() < VK_API_VERSION_1_1) {
printf("At least Vulkan version 1.1 is required for GPU-AV\n");
return false;
}
auto features2 = LvlInitStruct<VkPhysicalDeviceFeatures2>();
GetPhysicalDeviceFeatures2(features2);
if (!features2.features.fragmentStoresAndAtomics || !features2.features.vertexPipelineStoresAndAtomics) {
printf("fragmentStoresAndAtomics and vertexPipelineStoresAndAtomics are required for GPU-AV\n");
return false;
} else if (IsPlatform(kMockICD)) {
printf("Test not supported by MockICD, GPU-Assisted validation test requires a driver that can draw\n");
return false;
}
return true;
}
TEST_F(VkGpuAssistedLayerTest, GpuValidationArrayOOBGraphicsShaders) {
TEST_DESCRIPTION(
"GPU validation: Verify detection of out-of-bounds descriptor array indexing and use of uninitialized descriptors.");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredExtensions(VK_KHR_MAINTENANCE_4_EXTENSION_NAME);
AddOptionalExtensions(VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME);
VkValidationFeaturesEXT validation_features = GetValidationFeatures();
ASSERT_NO_FATAL_FAILURE(InitFramework(m_errorMonitor, &validation_features));
bool descriptor_indexing = IsExtensionsEnabled(VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME);
if (!AreRequiredExtensionsEnabled()) {
GTEST_SKIP() << RequiredExtensionsNotSupported() << " not supported";
}
if (!CanEnableGpuAV()) {
GTEST_SKIP() << "Requirements for GPU-AV are not met";
}
if (IsPlatform(kGalaxyS10)) {
GTEST_SKIP() << "This test should not run on Galaxy S10";
}
if (IsPlatform(kNexusPlayer)) {
GTEST_SKIP() << "This test should not run on Nexus Player";
}
auto maintenance4_features = LvlInitStruct<VkPhysicalDeviceMaintenance4Features>();
maintenance4_features.maintenance4 = true;
auto features2 = LvlInitStruct<VkPhysicalDeviceFeatures2KHR>(&maintenance4_features);
auto indexing_features = LvlInitStruct<VkPhysicalDeviceDescriptorIndexingFeaturesEXT>();
if (descriptor_indexing) {
maintenance4_features.pNext = &indexing_features;
GetPhysicalDeviceFeatures2(features2);
if (!indexing_features.runtimeDescriptorArray || !indexing_features.descriptorBindingSampledImageUpdateAfterBind ||
!indexing_features.descriptorBindingPartiallyBound || !indexing_features.descriptorBindingVariableDescriptorCount ||
!indexing_features.shaderSampledImageArrayNonUniformIndexing ||
!indexing_features.shaderStorageBufferArrayNonUniformIndexing) {
GTEST_SKIP() << "Not all descriptor indexing features supported, skipping descriptor indexing tests";
descriptor_indexing = false;
}
}
VkCommandPoolCreateFlags pool_flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
ASSERT_NO_FATAL_FAILURE(InitState(nullptr, &features2, pool_flags));
ASSERT_NO_FATAL_FAILURE(InitViewport());
ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
// Make a uniform buffer to be passed to the shader that contains the invalid array index.
uint32_t qfi = 0;
VkBufferCreateInfo bci = LvlInitStruct<VkBufferCreateInfo>();
bci.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
bci.size = 1024;
bci.queueFamilyIndexCount = 1;
bci.pQueueFamilyIndices = &qfi;
VkBufferObj buffer0;
VkMemoryPropertyFlags mem_props = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
buffer0.init(*m_device, bci, mem_props);
bci.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
// Make another buffer to populate the buffer array to be indexed
VkBufferObj buffer1;
buffer1.init(*m_device, bci, mem_props);
void *layout_pnext = nullptr;
void *allocate_pnext = nullptr;
auto pool_create_flags = 0;
auto layout_create_flags = 0;
VkDescriptorBindingFlagsEXT ds_binding_flags[2] = {};
VkDescriptorSetLayoutBindingFlagsCreateInfoEXT layout_createinfo_binding_flags[1] = {};
if (descriptor_indexing) {
ds_binding_flags[0] = 0;
ds_binding_flags[1] = VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT;
layout_createinfo_binding_flags[0] = LvlInitStruct<VkDescriptorSetLayoutBindingFlagsCreateInfo>();
layout_createinfo_binding_flags[0].bindingCount = 2;
layout_createinfo_binding_flags[0].pBindingFlags = ds_binding_flags;
layout_create_flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT;
pool_create_flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT;
layout_pnext = layout_createinfo_binding_flags;
}
// Prepare descriptors
OneOffDescriptorSet descriptor_set(m_device,
{
{0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_ALL, nullptr},
{1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 6, VK_SHADER_STAGE_ALL, nullptr},
},
layout_create_flags, layout_pnext, pool_create_flags);
VkDescriptorSetVariableDescriptorCountAllocateInfoEXT variable_count = {};
uint32_t desc_counts;
if (descriptor_indexing) {
layout_create_flags = 0;
pool_create_flags = 0;
ds_binding_flags[1] =
VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT | VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT;
desc_counts = 6; // We'll reserve 8 spaces in the layout, but the descriptor will only use 6
variable_count = LvlInitStruct<VkDescriptorSetVariableDescriptorCountAllocateInfo>();
variable_count.descriptorSetCount = 1;
variable_count.pDescriptorCounts = &desc_counts;
allocate_pnext = &variable_count;
}
OneOffDescriptorSet descriptor_set_variable(m_device,
{
{0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_ALL, nullptr},
{1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 8, VK_SHADER_STAGE_ALL, nullptr},
},
layout_create_flags, layout_pnext, pool_create_flags, allocate_pnext);
const VkPipelineLayoutObj pipeline_layout(m_device, {&descriptor_set.layout_});
const VkPipelineLayoutObj pipeline_layout_variable(m_device, {&descriptor_set_variable.layout_});
VkTextureObj texture(m_device, nullptr);
VkSamplerObj sampler(m_device);
VkDescriptorBufferInfo buffer_info[1] = {};
buffer_info[0].buffer = buffer0.handle();
buffer_info[0].offset = 0;
buffer_info[0].range = sizeof(uint32_t);
VkDescriptorImageInfo image_info[6] = {};
for (int i = 0; i < 6; i++) {
image_info[i] = texture.DescriptorImageInfo();
image_info[i].sampler = sampler.handle();
image_info[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
}
VkWriteDescriptorSet descriptor_writes[2] = {};
descriptor_writes[0] = LvlInitStruct<VkWriteDescriptorSet>();
descriptor_writes[0].dstSet = descriptor_set.set_; // descriptor_set;
descriptor_writes[0].dstBinding = 0;
descriptor_writes[0].descriptorCount = 1;
descriptor_writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptor_writes[0].pBufferInfo = buffer_info;
descriptor_writes[1] = LvlInitStruct<VkWriteDescriptorSet>();
descriptor_writes[1].dstSet = descriptor_set.set_; // descriptor_set;
descriptor_writes[1].dstBinding = 1;
if (descriptor_indexing)
descriptor_writes[1].descriptorCount = 5; // Intentionally don't write index 5
else
descriptor_writes[1].descriptorCount = 6;
descriptor_writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptor_writes[1].pImageInfo = image_info;
vk::UpdateDescriptorSets(m_device->device(), 2, descriptor_writes, 0, NULL);
if (descriptor_indexing) {
descriptor_writes[0].dstSet = descriptor_set_variable.set_;
descriptor_writes[1].dstSet = descriptor_set_variable.set_;
vk::UpdateDescriptorSets(m_device->device(), 2, descriptor_writes, 0, NULL);
}
ds_binding_flags[0] = 0;
ds_binding_flags[1] = VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT;
// Resources for buffer tests
OneOffDescriptorSet descriptor_set_buffer(m_device,
{
{0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_ALL, nullptr},
{1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 6, VK_SHADER_STAGE_ALL, nullptr},
},
0, layout_pnext, 0);
const VkPipelineLayoutObj pipeline_layout_buffer(m_device, {&descriptor_set_buffer.layout_});
VkDescriptorBufferInfo buffer_test_buffer_info[7] = {};
buffer_test_buffer_info[0].buffer = buffer0.handle();
buffer_test_buffer_info[0].offset = 0;
buffer_test_buffer_info[0].range = sizeof(uint32_t);
for (int i = 1; i < 7; i++) {
buffer_test_buffer_info[i].buffer = buffer1.handle();
buffer_test_buffer_info[i].offset = 0;
buffer_test_buffer_info[i].range = 4 * sizeof(float);
}
if (descriptor_indexing) {
VkWriteDescriptorSet buffer_descriptor_writes[2] = {};
buffer_descriptor_writes[0] = LvlInitStruct<VkWriteDescriptorSet>();
buffer_descriptor_writes[0].dstSet = descriptor_set_buffer.set_; // descriptor_set;
buffer_descriptor_writes[0].dstBinding = 0;
buffer_descriptor_writes[0].descriptorCount = 1;
buffer_descriptor_writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
buffer_descriptor_writes[0].pBufferInfo = buffer_test_buffer_info;
buffer_descriptor_writes[1] = LvlInitStruct<VkWriteDescriptorSet>();
buffer_descriptor_writes[1].dstSet = descriptor_set_buffer.set_; // descriptor_set;
buffer_descriptor_writes[1].dstBinding = 1;
buffer_descriptor_writes[1].descriptorCount = 5; // Intentionally don't write index 5
buffer_descriptor_writes[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
buffer_descriptor_writes[1].pBufferInfo = &buffer_test_buffer_info[1];
vk::UpdateDescriptorSets(m_device->device(), 2, buffer_descriptor_writes, 0, NULL);
}
// Shader programs for array OOB test in vertex stage:
// - The vertex shader fetches the invalid index from the uniform buffer and uses it to make an invalid index into another
// array.
char const *vsSource_vert =
"#version 450\n"
"\n"
"layout(std140, set = 0, binding = 0) uniform foo { uint tex_index[1]; } uniform_index_buffer;\n"
"layout(set = 0, binding = 1) uniform sampler2D tex[6];\n"
"vec2 vertices[3];\n"
"void main(){\n"
" vertices[0] = vec2(-1.0, -1.0);\n"
" vertices[1] = vec2( 1.0, -1.0);\n"
" vertices[2] = vec2( 0.0, 1.0);\n"
" gl_Position = vec4(vertices[gl_VertexIndex % 3], 0.0, 1.0);\n"
" gl_Position += 1e-30 * texture(tex[uniform_index_buffer.tex_index[0]], vec2(0, 0));\n"
"}\n";
char const *fsSource_vert =
"#version 450\n"
"\n"
"layout(set = 0, binding = 1) uniform sampler2D tex[6];\n"
"layout(location = 0) out vec4 uFragColor;\n"
"void main(){\n"
" uFragColor = texture(tex[0], vec2(0, 0));\n"
"}\n";
// Shader programs for array OOB test in fragment stage:
// - The vertex shader fetches the invalid index from the uniform buffer and passes it to the fragment shader.
// - The fragment shader makes the invalid array access.
char const *vsSource_frag =
"#version 450\n"
"\n"
"layout(std140, binding = 0) uniform foo { uint tex_index[1]; } uniform_index_buffer;\n"
"layout(location = 0) out flat uint index;\n"
"vec2 vertices[3];\n"
"void main(){\n"
" vertices[0] = vec2(-1.0, -1.0);\n"
" vertices[1] = vec2( 1.0, -1.0);\n"
" vertices[2] = vec2( 0.0, 1.0);\n"
" gl_Position = vec4(vertices[gl_VertexIndex % 3], 0.0, 1.0);\n"
" index = uniform_index_buffer.tex_index[0];\n"
"}\n";
char const *fsSource_frag =
"#version 450\n"
"\n"
"layout(set = 0, binding = 1) uniform sampler2D tex[6];\n"
"layout(location = 0) out vec4 uFragColor;\n"
"layout(location = 0) in flat uint index;\n"
"void main(){\n"
" uFragColor = texture(tex[index], vec2(0, 0));\n"
"}\n";
char const *fsSource_frag_runtime =
"#version 450\n"
"#extension GL_EXT_nonuniform_qualifier : enable\n"
"\n"
"layout(set = 0, binding = 1) uniform sampler2D tex[];\n"
"layout(location = 0) out vec4 uFragColor;\n"
"layout(location = 0) in flat uint index;\n"
"void main(){\n"
" uFragColor = texture(tex[index], vec2(0, 0));\n"
"}\n";
char const *fsSource_buffer =
"#version 450\n"
"#extension GL_EXT_nonuniform_qualifier : enable\n "
"\n"
"layout(set = 0, binding = 1) buffer foo { vec4 val; } colors[];\n"
"layout(location = 0) out vec4 uFragColor;\n"
"layout(location = 0) in flat uint index;\n"
"void main(){\n"
" uFragColor = colors[index].val;\n"
"}\n";
char const *gsSource =
"#version 450\n"
"#extension GL_EXT_nonuniform_qualifier : enable\n "
"layout(triangles) in;\n"
"layout(triangle_strip, max_vertices=3) out;\n"
"layout(location=0) in VertexData { vec4 x; } gs_in[];\n"
"layout(std140, set = 0, binding = 0) uniform ufoo { uint index; } uniform_index_buffer;\n"
"layout(set = 0, binding = 1) buffer bfoo { vec4 val; } adds[];\n"
"void main() {\n"
" gl_Position = gs_in[0].x + adds[uniform_index_buffer.index].val.x;\n"
" EmitVertex();\n"
"}\n";
static const char vsSourceForGS[] = R"glsl(
#version 450
layout(location=0) out foo {vec4 val;} gs_out[3];
void main() {
gs_out[0].val = vec4(0);
gl_Position = vec4(1);
})glsl";
static const char *tesSource =
"#version 450\n"
"#extension GL_EXT_nonuniform_qualifier : enable\n "
"layout(std140, set = 0, binding = 0) uniform ufoo { uint index; } uniform_index_buffer;\n"
"layout(set = 0, binding = 1) buffer bfoo { vec4 val; } adds[];\n"
"layout(triangles, equal_spacing, cw) in;\n"
"void main() {\n"
" gl_Position = adds[uniform_index_buffer.index].val;\n"
"}\n";
struct TestCase {
char const *vertex_source;
char const *fragment_source;
char const *geometry_source;
char const *tess_ctrl_source;
char const *tess_eval_source;
bool debug;
const VkPipelineLayoutObj *pipeline_layout;
const OneOffDescriptorSet *descriptor_set;
uint32_t index;
char const *expected_error;
};
std::vector<TestCase> tests;
tests.push_back({vsSource_vert, fsSource_vert, nullptr, nullptr, nullptr, false, &pipeline_layout, &descriptor_set, 25,
"Index of 25 used to index descriptor array of length 6."});
tests.push_back({vsSource_frag, fsSource_frag, nullptr, nullptr, nullptr, false, &pipeline_layout, &descriptor_set, 25,
"Index of 25 used to index descriptor array of length 6."});
#if !defined(ANDROID)
// The Android test framework uses shaderc for online compilations. Even when configured to compile with debug info,
// shaderc seems to drop the OpLine instructions from the shader binary. This causes the following two tests to fail
// on Android platforms. Skip these tests until the shaderc issue is understood/resolved.
tests.push_back({vsSource_vert, fsSource_vert, nullptr, nullptr, nullptr, true, &pipeline_layout, &descriptor_set, 25,
"gl_Position += 1e-30 * texture(tex[uniform_index_buffer.tex_index[0]], vec2(0, 0));"});
tests.push_back({vsSource_frag, fsSource_frag, nullptr, nullptr, nullptr, true, &pipeline_layout, &descriptor_set, 25,
"uFragColor = texture(tex[index], vec2(0, 0));"});
#endif
if (descriptor_indexing) {
tests.push_back({vsSource_frag, fsSource_frag_runtime, nullptr, nullptr, nullptr, false, &pipeline_layout, &descriptor_set,
25, "Index of 25 used to index descriptor array of length 6."});
tests.push_back({vsSource_frag, fsSource_frag_runtime, nullptr, nullptr, nullptr, false, &pipeline_layout, &descriptor_set,
5, "Descriptor index 5 is uninitialized"});
// Pick 6 below because it is less than the maximum specified, but more than the actual specified
tests.push_back({vsSource_frag, fsSource_frag_runtime, nullptr, nullptr, nullptr, false, &pipeline_layout_variable,
&descriptor_set_variable, 6, "Index of 6 used to index descriptor array of length 6."});
tests.push_back({vsSource_frag, fsSource_frag_runtime, nullptr, nullptr, nullptr, false, &pipeline_layout_variable,
&descriptor_set_variable, 5, "Descriptor index 5 is uninitialized"});
tests.push_back({vsSource_frag, fsSource_buffer, nullptr, nullptr, nullptr, false, &pipeline_layout_buffer,
&descriptor_set_buffer, 25, "Index of 25 used to index descriptor array of length 6."});
tests.push_back({vsSource_frag, fsSource_buffer, nullptr, nullptr, nullptr, false, &pipeline_layout_buffer,
&descriptor_set_buffer, 5, "Descriptor index 5 is uninitialized"});
if (m_device->phy().features().geometryShader) {
// OOB Geometry
tests.push_back({vsSourceForGS, bindStateFragShaderText, gsSource, nullptr, nullptr, false,
&pipeline_layout_buffer, &descriptor_set_buffer, 25, "Stage = Geometry"});
// Uninitialized Geometry
tests.push_back({vsSourceForGS, bindStateFragShaderText, gsSource, nullptr, nullptr, false,
&pipeline_layout_buffer, &descriptor_set_buffer, 5, "Stage = Geometry"});
}
if (m_device->phy().features().tessellationShader) {
tests.push_back({bindStateVertShaderText, bindStateFragShaderText, nullptr, bindStateTscShaderText, tesSource, false,
&pipeline_layout_buffer, &descriptor_set_buffer, 25, "Stage = Tessellation Eval"});
tests.push_back({bindStateVertShaderText, bindStateFragShaderText, nullptr, bindStateTscShaderText, tesSource, false,
&pipeline_layout_buffer, &descriptor_set_buffer, 5, "Stage = Tessellation Eval"});
}
}
VkViewport viewport = m_viewports[0];
VkRect2D scissors = m_scissors[0];
VkSubmitInfo submit_info = LvlInitStruct<VkSubmitInfo>();
submit_info.commandBufferCount = 1;
submit_info.pCommandBuffers = &m_commandBuffer->handle();
for (const auto &iter : tests) {
VkResult err;
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, iter.expected_error);
VkShaderObj vs(this, iter.vertex_source, VK_SHADER_STAGE_VERTEX_BIT, SPV_ENV_VULKAN_1_0, SPV_SOURCE_GLSL, nullptr, "main",
iter.debug);
VkShaderObj fs(this, iter.fragment_source, VK_SHADER_STAGE_FRAGMENT_BIT, SPV_ENV_VULKAN_1_0, SPV_SOURCE_GLSL, nullptr,
"main", iter.debug);
VkShaderObj *gs = nullptr;
VkShaderObj *tcs = nullptr;
VkShaderObj *tes = nullptr;
VkPipelineObj pipe(m_device);
pipe.AddShader(&vs);
pipe.AddShader(&fs);
if (iter.geometry_source) {
gs = new VkShaderObj(this, iter.geometry_source, VK_SHADER_STAGE_GEOMETRY_BIT, SPV_ENV_VULKAN_1_0, SPV_SOURCE_GLSL,
nullptr, "main", iter.debug);
pipe.AddShader(gs);
}
VkPipelineInputAssemblyStateCreateInfo iasci{VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, nullptr, 0,
VK_PRIMITIVE_TOPOLOGY_PATCH_LIST, VK_FALSE};
VkPipelineTessellationDomainOriginStateCreateInfo tessellationDomainOriginStateInfo = {
VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_DOMAIN_ORIGIN_STATE_CREATE_INFO, VK_NULL_HANDLE,
VK_TESSELLATION_DOMAIN_ORIGIN_UPPER_LEFT};
VkPipelineTessellationStateCreateInfo tsci{VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO,
&tessellationDomainOriginStateInfo, 0, 3};
if (iter.tess_ctrl_source && iter.tess_eval_source) {
tcs = new VkShaderObj(this, iter.tess_ctrl_source, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT, SPV_ENV_VULKAN_1_0,
SPV_SOURCE_GLSL, nullptr, "main", iter.debug);
tes = new VkShaderObj(this, iter.tess_eval_source, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT, SPV_ENV_VULKAN_1_0,
SPV_SOURCE_GLSL, nullptr, "main", iter.debug);
pipe.AddShader(tcs);
pipe.AddShader(tes);
pipe.SetTessellation(&tsci);
pipe.SetInputAssembly(&iasci);
}
pipe.AddDefaultColorAttachment();
err = pipe.CreateVKPipeline(iter.pipeline_layout->handle(), renderPass());
ASSERT_VK_SUCCESS(err);
m_commandBuffer->begin();
m_commandBuffer->BeginRenderPass(m_renderPassBeginInfo);
vk::CmdBindPipeline(m_commandBuffer->handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipe.handle());
vk::CmdBindDescriptorSets(m_commandBuffer->handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, iter.pipeline_layout->handle(), 0, 1,
&iter.descriptor_set->set_, 0, nullptr);
vk::CmdSetViewport(m_commandBuffer->handle(), 0, 1, &viewport);
vk::CmdSetScissor(m_commandBuffer->handle(), 0, 1, &scissors);
vk::CmdDraw(m_commandBuffer->handle(), 3, 1, 0, 0);
vk::CmdEndRenderPass(m_commandBuffer->handle());
m_commandBuffer->end();
uint32_t *data = (uint32_t *)buffer0.memory().map();
data[0] = iter.index;
buffer0.memory().unmap();
vk::QueueSubmit(m_device->m_queue, 1, &submit_info, VK_NULL_HANDLE);
vk::QueueWaitIdle(m_device->m_queue);
m_errorMonitor->VerifyFound();
delete gs;
delete tcs;
delete tes;
}
auto c_queue = m_device->GetDefaultComputeQueue();
if (c_queue && descriptor_indexing) {
char const *csSource =
"#version 450\n"
"#extension GL_EXT_nonuniform_qualifier : enable\n "
"layout(set = 0, binding = 0) uniform ufoo { uint index; } u_index;"
"layout(set = 0, binding = 1) buffer StorageBuffer {\n"
" uint data;\n"
"} Data[];\n"
"void main() {\n"
" Data[(u_index.index - 1)].data = Data[u_index.index].data;\n"
"}\n";
VkShaderObj shader_module(this, csSource, VK_SHADER_STAGE_COMPUTE_BIT);
VkPipelineShaderStageCreateInfo stage = LvlInitStruct<VkPipelineShaderStageCreateInfo>();
stage.flags = 0;
stage.stage = VK_SHADER_STAGE_COMPUTE_BIT;
stage.module = shader_module.handle();
stage.pName = "main";
stage.pSpecializationInfo = nullptr;
// CreateComputePipelines
VkComputePipelineCreateInfo pipeline_info = LvlInitStruct<VkComputePipelineCreateInfo>();
pipeline_info.flags = 0;
pipeline_info.layout = pipeline_layout_buffer.handle();
pipeline_info.basePipelineHandle = VK_NULL_HANDLE;
pipeline_info.basePipelineIndex = -1;
pipeline_info.stage = stage;
VkPipeline c_pipeline;
vk::CreateComputePipelines(device(), VK_NULL_HANDLE, 1, &pipeline_info, nullptr, &c_pipeline);
VkCommandBufferBeginInfo begin_info = LvlInitStruct<VkCommandBufferBeginInfo>();
VkCommandBufferInheritanceInfo hinfo = LvlInitStruct<VkCommandBufferInheritanceInfo>();
begin_info.pInheritanceInfo = &hinfo;
m_commandBuffer->begin(&begin_info);
vk::CmdBindPipeline(m_commandBuffer->handle(), VK_PIPELINE_BIND_POINT_COMPUTE, c_pipeline);
vk::CmdBindDescriptorSets(m_commandBuffer->handle(), VK_PIPELINE_BIND_POINT_COMPUTE, pipeline_layout_buffer.handle(), 0, 1,
&descriptor_set_buffer.set_, 0, nullptr);
vk::CmdDispatch(m_commandBuffer->handle(), 1, 1, 1);
m_commandBuffer->end();
// Uninitialized
uint32_t *data = (uint32_t *)buffer0.memory().map();
data[0] = 5;
buffer0.memory().unmap();
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "Stage = Compute");
vk::QueueSubmit(c_queue->handle(), 1, &submit_info, VK_NULL_HANDLE);
vk::QueueWaitIdle(m_device->m_queue);
m_errorMonitor->VerifyFound();
// Out of Bounds
data = (uint32_t *)buffer0.memory().map();
data[0] = 25;
buffer0.memory().unmap();
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "Stage = Compute");
vk::QueueSubmit(c_queue->handle(), 1, &submit_info, VK_NULL_HANDLE);
vk::QueueWaitIdle(m_device->m_queue);
m_errorMonitor->VerifyFound();
vk::DestroyPipeline(m_device->handle(), c_pipeline, NULL);
}
return;
}
TEST_F(VkGpuAssistedLayerTest, GpuRobustBufferOOB) {
TEST_DESCRIPTION("Check buffer oob validation when per pipeline robustness is enabled");
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredExtensions(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
AddRequiredExtensions(VK_EXT_PIPELINE_ROBUSTNESS_EXTENSION_NAME);
VkValidationFeaturesEXT validation_features = GetValidationFeatures();
ASSERT_NO_FATAL_FAILURE(InitFramework(m_errorMonitor, &validation_features));
if (!AreRequiredExtensionsEnabled()) {
GTEST_SKIP() << RequiredExtensionsNotSupported() << " not supported";
}
if (!CanEnableGpuAV()) {
GTEST_SKIP() << "Requirements for GPU-AV are not met";
}
auto pipeline_robustness_features = LvlInitStruct<VkPhysicalDevicePipelineRobustnessFeaturesEXT>();
auto features2 = GetPhysicalDeviceFeatures2(pipeline_robustness_features);
features2.features.robustBufferAccess = VK_FALSE;
if (!pipeline_robustness_features.pipelineRobustness) {
GTEST_SKIP() << "pipelineRobustness feature not supported";
}
pipeline_robustness_features.pipelineRobustness = VK_FALSE;
ASSERT_NO_FATAL_FAILURE(InitState(nullptr, &features2));
ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
VkBufferObj uniform_buffer;
VkBufferObj storage_buffer;
VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
uniform_buffer.init(*m_device, 4, reqs);
storage_buffer.init_as_storage(*m_device, 16, reqs);
OneOffDescriptorSet descriptor_set(m_device, {
{0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_ALL, nullptr},
{1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_ALL, nullptr}});
const VkPipelineLayoutObj pipeline_layout(m_device, {&descriptor_set.layout_});
descriptor_set.WriteDescriptorBufferInfo(0, uniform_buffer.handle(), 0, 4);
descriptor_set.WriteDescriptorBufferInfo(1, storage_buffer.handle(), 0, 16, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
descriptor_set.UpdateDescriptorSets();
const char *vertshader = R"glsl(
#version 450
layout(set = 0, binding = 0) uniform foo { uint index[]; } u_index;
layout(set = 0, binding = 1) buffer StorageBuffer { uint data[]; } Data;
void main() {
vec4 x;
if (u_index.index[0] == 0)
x[0] = u_index.index[1]; // Uniform read OOB
else
Data.data[8] = 0xdeadca71; // Storage write OOB
}
)glsl";
VkShaderObj vs(this, vertshader, VK_SHADER_STAGE_VERTEX_BIT);
auto pipeline_robustness_ci = LvlInitStruct<VkPipelineRobustnessCreateInfoEXT>();
VkPipelineObj robust_pipe(m_device);
robust_pipe.AddShader(&vs);
robust_pipe.AddDefaultColorAttachment();
VkGraphicsPipelineCreateInfo gp_ci;
robust_pipe.InitGraphicsPipelineCreateInfo(&gp_ci);
gp_ci.pNext = &pipeline_robustness_ci;
pipeline_robustness_ci.uniformBuffers = VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_ROBUST_BUFFER_ACCESS_EXT;
pipeline_robustness_ci.storageBuffers = VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_ROBUST_BUFFER_ACCESS_EXT;
robust_pipe.CreateVKPipeline(pipeline_layout.handle(), m_renderPass, &gp_ci);
VkCommandBufferBeginInfo begin_info = LvlInitStruct<VkCommandBufferBeginInfo>();
m_commandBuffer->begin(&begin_info);
vk::CmdBindPipeline(m_commandBuffer->handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, robust_pipe.handle());
m_commandBuffer->BeginRenderPass(m_renderPassBeginInfo);
vk::CmdBindDescriptorSets(m_commandBuffer->handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_layout.handle(), 0, 1,
&descriptor_set.set_, 0, nullptr);
VkViewport viewport = {0, 0, 16, 16, 0, 1};
vk::CmdSetViewport(m_commandBuffer->handle(), 0, 1, &viewport);
VkRect2D scissor = {{0, 0}, {16, 16}};
vk::CmdSetScissor(m_commandBuffer->handle(), 0, 1, &scissor);
vk::CmdDraw(m_commandBuffer->handle(), 3, 1, 0, 0);
vk::CmdEndRenderPass(m_commandBuffer->handle());
m_commandBuffer->end();
uint32_t *data = (uint32_t *)uniform_buffer.memory().map();
*data = 0;
uniform_buffer.memory().unmap();
m_errorMonitor->SetDesiredFailureMsg(kWarningBit, "Descriptor index 0 access out of bounds. Descriptor size is 4 and highest byte accessed was 19");
m_commandBuffer->QueueCommandBuffer();
m_errorMonitor->VerifyFound();
data = (uint32_t *)uniform_buffer.memory().map();
*data = 1;
uniform_buffer.memory().unmap();
m_errorMonitor->SetDesiredFailureMsg(kWarningBit, "Descriptor index 0 access out of bounds. Descriptor size is 16 and highest byte accessed was 35");
m_commandBuffer->QueueCommandBuffer();
m_errorMonitor->VerifyFound();
}
TEST_F(VkGpuAssistedLayerTest, GpuBufferOOB) {
SetTargetApiVersion(VK_API_VERSION_1_1);
AddRequiredExtensions(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
AddRequiredExtensions(VK_EXT_ROBUSTNESS_2_EXTENSION_NAME);
AddOptionalExtensions(VK_EXT_MULTI_DRAW_EXTENSION_NAME);
VkValidationFeaturesEXT validation_features = GetValidationFeatures();
ASSERT_NO_FATAL_FAILURE(InitFramework(m_errorMonitor, &validation_features));
if (!AreRequiredExtensionsEnabled()) {
GTEST_SKIP() << RequiredExtensionsNotSupported() << " not supported";
}
if (!CanEnableGpuAV()) {
GTEST_SKIP() << "Requirements for GPU-AV are not met";
}
auto multi_draw_features = LvlInitStruct<VkPhysicalDeviceMultiDrawFeaturesEXT>();
const bool multi_draw = IsExtensionsEnabled(VK_EXT_MULTI_DRAW_EXTENSION_NAME);
auto robustness2_features = LvlInitStruct<VkPhysicalDeviceRobustness2FeaturesEXT>(multi_draw ? &multi_draw_features : nullptr);
auto features2 = GetPhysicalDeviceFeatures2(robustness2_features);
if (!robustness2_features.nullDescriptor) {
GTEST_SKIP() << "nullDescriptor feature not supported";
}
features2.features.robustBufferAccess = VK_FALSE;
robustness2_features.robustBufferAccess2 = VK_FALSE;
VkCommandPoolCreateFlags pool_flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
ASSERT_NO_FATAL_FAILURE(InitState(nullptr, &features2, pool_flags));
ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
VkBufferObj offset_buffer;
VkBufferObj write_buffer;
VkBufferObj uniform_texel_buffer;
VkBufferObj storage_texel_buffer;
VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
offset_buffer.init(*m_device, 4, reqs);
write_buffer.init_as_storage(*m_device, 16, reqs);
VkBufferCreateInfo buffer_create_info = LvlInitStruct<VkBufferCreateInfo>();
uint32_t queue_family_index = 0;
buffer_create_info.size = 16;
buffer_create_info.usage = VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT;
buffer_create_info.queueFamilyIndexCount = 1;
buffer_create_info.pQueueFamilyIndices = &queue_family_index;
uniform_texel_buffer.init(*m_device, buffer_create_info, reqs);
buffer_create_info.usage = VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT;
storage_texel_buffer.init(*m_device, buffer_create_info, reqs);
VkBufferView uniform_buffer_view;
VkBufferView storage_buffer_view;
VkBufferViewCreateInfo bvci = LvlInitStruct<VkBufferViewCreateInfo>();
bvci.buffer = uniform_texel_buffer.handle();
bvci.format = VK_FORMAT_R32_SFLOAT;
bvci.range = VK_WHOLE_SIZE;
vk::CreateBufferView(m_device->device(), &bvci, NULL, &uniform_buffer_view);
bvci.buffer = storage_texel_buffer.handle();
vk::CreateBufferView(m_device->device(), &bvci, NULL, &storage_buffer_view);
OneOffDescriptorSet descriptor_set(m_device, {{0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_ALL, nullptr},
{1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_ALL, nullptr},
{2, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_ALL, nullptr},
{3, VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1, VK_SHADER_STAGE_ALL, nullptr},
{4, VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1, VK_SHADER_STAGE_ALL, nullptr}});
const VkPipelineLayoutObj pipeline_layout(m_device, {&descriptor_set.layout_});
descriptor_set.WriteDescriptorBufferInfo(0, offset_buffer.handle(), 0, 4);
descriptor_set.WriteDescriptorBufferInfo(1, write_buffer.handle(), 0, 16, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
descriptor_set.WriteDescriptorBufferInfo(2, VK_NULL_HANDLE, 0, VK_WHOLE_SIZE, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
descriptor_set.WriteDescriptorBufferView(3, uniform_buffer_view, VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
descriptor_set.WriteDescriptorBufferView(4, storage_buffer_view, VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
descriptor_set.UpdateDescriptorSets();
static const char vertshader[] =
"#version 450\n"
"layout(set = 0, binding = 0) uniform ufoo { uint index[]; } u_index;\n" // index[1]
"layout(set = 0, binding = 1) buffer StorageBuffer { uint data[]; } Data;\n" // data[4]
"layout(set = 0, binding = 2) buffer NullBuffer { uint data[]; } Null;\n" // VK_NULL_HANDLE
"layout(set = 0, binding = 3) uniform samplerBuffer u_buffer;\n" // texel_buffer[4]
"layout(set = 0, binding = 4, r32f) uniform imageBuffer s_buffer;\n" // texel_buffer[4]
"void main() {\n"
" vec4 x;\n"
" if (u_index.index[0] == 8)\n"
" Data.data[u_index.index[0]] = 0xdeadca71;\n"
" else if (u_index.index[0] == 0)\n"
" Data.data[0] = u_index.index[4];\n"
" else if (u_index.index[0] == 1)\n"
" Data.data[0] = Null.data[40];\n" // No error
" else if (u_index.index[0] == 2)\n"
" x = texelFetch(u_buffer, 5);\n"
" else if (u_index.index[0] == 3)\n"
" x = imageLoad(s_buffer, 5);\n"
" else if (u_index.index[0] == 4)\n"
" imageStore(s_buffer, 5, x);\n"
" else if (u_index.index[0] == 5)\n" // No Error
" imageStore(s_buffer, 0, x);\n"
" else if (u_index.index[0] == 6)\n" // No Error
" x = imageLoad(s_buffer, 0);\n"
"}\n";
VkShaderObj vs(this, vertshader, VK_SHADER_STAGE_VERTEX_BIT);
VkPipelineObj pipe(m_device);
pipe.AddShader(&vs);
pipe.AddDefaultColorAttachment();
pipe.CreateVKPipeline(pipeline_layout.handle(), m_renderPass);
VkCommandBufferBeginInfo begin_info = LvlInitStruct<VkCommandBufferBeginInfo>();
m_commandBuffer->begin(&begin_info);
vk::CmdBindPipeline(m_commandBuffer->handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipe.handle());
m_commandBuffer->BeginRenderPass(m_renderPassBeginInfo);
vk::CmdBindDescriptorSets(m_commandBuffer->handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_layout.handle(), 0, 1,
&descriptor_set.set_, 0, nullptr);
VkViewport viewport = {0, 0, 16, 16, 0, 1};
vk::CmdSetViewport(m_commandBuffer->handle(), 0, 1, &viewport);
VkRect2D scissor = {{0, 0}, {16, 16}};
vk::CmdSetScissor(m_commandBuffer->handle(), 0, 1, &scissor);
vk::CmdDraw(m_commandBuffer->handle(), 3, 1, 0, 0);
vk::CmdEndRenderPass(m_commandBuffer->handle());
m_commandBuffer->end();
struct TestCase {
bool positive;
uint32_t index;
char const *expected_error;
};
std::vector<TestCase> tests;
// "VUID-vkCmdDispatchBase-None-02706" Storage
tests.push_back({false, 8, "Descriptor size is 16 and highest byte accessed was 35"});
// Uniform buffer stride rounded up to the alignment of a vec4 (16 bytes)
// so u_index.index[4] accesses bytes 64, 65, 66, and 67
// "VUID-vkCmdDispatchBase-None-02705" Uniform
tests.push_back({false, 0, "Descriptor size is 4 and highest byte accessed was 67"});
tests.push_back({true, 1, ""});
// "VUID-vkCmdDispatchBase-None-02705" Uniform
tests.push_back({false, 2, "Descriptor size is 4 texels and highest texel accessed was 5"});
// "VUID-vkCmdDispatchBase-None-02706" Storage
tests.push_back({false, 3, "Descriptor size is 4 texels and highest texel accessed was 5"});
// "VUID-vkCmdDispatchBase-None-02706" Storage
tests.push_back({false, 4, "Descriptor size is 4 texels and highest texel accessed was 5"});
for (const auto &test : tests) {
uint32_t *data = (uint32_t *)offset_buffer.memory().map();
*data = test.index;
offset_buffer.memory().unmap();
if (test.positive) {
} else {
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, test.expected_error);
}
m_commandBuffer->QueueCommandBuffer();
if (test.positive) {
} else {
m_errorMonitor->VerifyFound();
}
vk::QueueWaitIdle(m_device->m_queue);
}
if (multi_draw && multi_draw_features.multiDraw) {
auto vkCmdDrawMultiEXT = (PFN_vkCmdDrawMultiEXT)vk::GetDeviceProcAddr(m_device->device(), "vkCmdDrawMultiEXT");
auto vkCmdDrawMultiIndexedEXT =
(PFN_vkCmdDrawMultiIndexedEXT)vk::GetDeviceProcAddr(m_device->device(), "vkCmdDrawMultiIndexedEXT");
assert(vkCmdDrawMultiEXT != nullptr && vkCmdDrawMultiIndexedEXT != nullptr);
VkMultiDrawInfoEXT multi_draws[3] = {};
multi_draws[0].vertexCount = multi_draws[1].vertexCount = multi_draws[2].vertexCount = 3;
VkMultiDrawIndexedInfoEXT multi_draw_indices[3] = {};
multi_draw_indices[0].indexCount = multi_draw_indices[1].indexCount = multi_draw_indices[2].indexCount = 3;
VkBufferObj buffer;
buffer.init(*m_device, 1024, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_BUFFER_USAGE_INDEX_BUFFER_BIT);
m_commandBuffer->begin(&begin_info);
vk::CmdBindPipeline(m_commandBuffer->handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipe.handle());
m_commandBuffer->BeginRenderPass(m_renderPassBeginInfo);
vk::CmdBindDescriptorSets(m_commandBuffer->handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_layout.handle(), 0, 1,
&descriptor_set.set_, 0, nullptr);
vk::CmdSetViewport(m_commandBuffer->handle(), 0, 1, &viewport);
vk::CmdSetScissor(m_commandBuffer->handle(), 0, 1, &scissor);
m_commandBuffer->BindIndexBuffer(&buffer, 0, VK_INDEX_TYPE_UINT16);
vkCmdDrawMultiIndexedEXT(m_commandBuffer->handle(), 3, multi_draw_indices, 1, 0, sizeof(VkMultiDrawIndexedInfoEXT), 0);
vk::CmdEndRenderPass(m_commandBuffer->handle());
m_commandBuffer->end();
uint32_t *data = (uint32_t *)offset_buffer.memory().map();
*data = 8;
offset_buffer.memory().unmap();
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDrawMultiIndexedEXT-None-02706");
m_commandBuffer->QueueCommandBuffer();
m_errorMonitor->VerifyFound();
m_commandBuffer->begin(&begin_info);
vk::CmdBindPipeline(m_commandBuffer->handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipe.handle());
m_commandBuffer->BeginRenderPass(m_renderPassBeginInfo);
vk::CmdBindDescriptorSets(m_commandBuffer->handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_layout.handle(), 0, 1,
&descriptor_set.set_, 0, nullptr);
vk::CmdSetViewport(m_commandBuffer->handle(), 0, 1, &viewport);
vk::CmdSetScissor(m_commandBuffer->handle(), 0, 1, &scissor);
vkCmdDrawMultiEXT(m_commandBuffer->handle(), 3, multi_draws, 1, 0, sizeof(VkMultiDrawInfoEXT));
vk::CmdEndRenderPass(m_commandBuffer->handle());
m_commandBuffer->end();
data = (uint32_t *)offset_buffer.memory().map();
*data = 0;
offset_buffer.memory().unmap();
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDrawMultiEXT-None-02705");
m_commandBuffer->QueueCommandBuffer();
m_errorMonitor->VerifyFound();
}
vk::DestroyBufferView(m_device->handle(), uniform_buffer_view, nullptr);
vk::DestroyBufferView(m_device->handle(), storage_buffer_view, nullptr);
}
void VkGpuAssistedLayerTest::ShaderBufferSizeTest(VkDeviceSize buffer_size, VkDeviceSize binding_offset, VkDeviceSize binding_range,
VkDescriptorType descriptor_type, const char *fragment_shader,
const char *expected_error) {
SetTargetApiVersion(VK_API_VERSION_1_1);
VkValidationFeaturesEXT validation_features = GetValidationFeatures();
ASSERT_NO_FATAL_FAILURE(InitFramework(m_errorMonitor, &validation_features));
if (!CanEnableGpuAV()) {
GTEST_SKIP() << "Requirements for GPU-AV are not met";
}
if (IsPlatform(kGalaxyS10)) {
GTEST_SKIP() << "This test should not run on Galaxy S10";
}
VkPhysicalDeviceFeatures features = {}; // Make sure robust buffer access is not enabled
ASSERT_NO_FATAL_FAILURE(InitState(&features));
ASSERT_NO_FATAL_FAILURE(InitViewport());
ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
m_errorMonitor->SetDesiredFailureMsg(VK_DEBUG_REPORT_ERROR_BIT_EXT, expected_error);
OneOffDescriptorSet ds(m_device, {{0, descriptor_type, 1, VK_SHADER_STAGE_ALL, nullptr}});
const VkPipelineLayoutObj pipeline_layout(m_device, {&ds.layout_});
uint32_t qfi = 0;
VkBufferCreateInfo bci = LvlInitStruct<VkBufferCreateInfo>();
bci.usage = descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER ? VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
: VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
bci.size = buffer_size;
bci.queueFamilyIndexCount = 1;
bci.pQueueFamilyIndices = &qfi;
VkBufferObj buffer;
buffer.init(*m_device, bci);
VkPipelineObj pipe(m_device);
VkDescriptorBufferInfo buffer_info;
buffer_info.buffer = buffer.handle();
buffer_info.offset = binding_offset;
buffer_info.range = binding_range;
VkWriteDescriptorSet descriptor_write = LvlInitStruct<VkWriteDescriptorSet>();
descriptor_write.dstSet = ds.set_;
descriptor_write.dstBinding = 0;
descriptor_write.descriptorCount = 1;
descriptor_write.descriptorType = descriptor_type;
descriptor_write.pBufferInfo = &buffer_info;
vk::UpdateDescriptorSets(m_device->device(), 1, &descriptor_write, 0, NULL);
char const *vsSource =
"#version 450\n"
"vec2 vertices[3];\n"
"void main(){\n"
" vertices[0] = vec2(-1.0, -1.0);\n"
" vertices[1] = vec2( 1.0, -1.0);\n"
" vertices[2] = vec2( 0.0, 1.0);\n"
" gl_Position = vec4(vertices[gl_VertexIndex % 3], 0.0, 1.0);\n"
"}\n";
VkShaderObj vs(this, vsSource, VK_SHADER_STAGE_VERTEX_BIT);
VkShaderObj fs(this, fragment_shader, VK_SHADER_STAGE_FRAGMENT_BIT);
pipe.AddShader(&vs);
pipe.AddShader(&fs);
pipe.AddDefaultColorAttachment();
VkResult err = pipe.CreateVKPipeline(pipeline_layout.handle(), renderPass());
ASSERT_VK_SUCCESS(err);
m_commandBuffer->begin();
m_commandBuffer->BeginRenderPass(m_renderPassBeginInfo);
vk::CmdBindPipeline(m_commandBuffer->handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipe.handle());
vk::CmdBindDescriptorSets(m_commandBuffer->handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_layout.handle(), 0, 1, &ds.set_,
0, nullptr);
VkViewport viewport = {0, 0, 16, 16, 0, 1};
vk::CmdSetViewport(m_commandBuffer->handle(), 0, 1, &viewport);
VkRect2D scissor = {{0, 0}, {16, 16}};
vk::CmdSetScissor(m_commandBuffer->handle(), 0, 1, &scissor);
vk::CmdDraw(m_commandBuffer->handle(), 3, 1, 0, 0);
m_commandBuffer->EndRenderPass();
m_commandBuffer->end();
m_commandBuffer->QueueCommandBuffer(true);
m_errorMonitor->VerifyFound();
DestroyRenderTarget();
}
TEST_F(VkGpuAssistedLayerTest, DrawTimeShaderUniformBufferTooSmall) {
TEST_DESCRIPTION("Test that an error is produced when trying to access uniform buffer outside the bound region.");
char const *fsSource =
"#version 450\n"
"\n"
"layout(location=0) out vec4 x;\n"
"layout(set=0, binding=0) uniform readonly foo { int x; int y; } bar;\n"
"void main(){\n"
" x = vec4(bar.x, bar.y, 0, 1);\n"
"}\n";
ShaderBufferSizeTest(4, // buffer size
0, // binding offset
4, // binding range
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, fsSource,
"Descriptor size is 4 and highest byte accessed was 7");
}
TEST_F(VkGpuAssistedLayerTest, DrawTimeShaderStorageBufferTooSmall) {
TEST_DESCRIPTION("Test that an error is produced when trying to access storage buffer outside the bound region.");
char const *fsSource =
"#version 450\n"
"\n"
"layout(location=0) out vec4 x;\n"
"layout(set=0, binding=0) buffer readonly foo { int x; int y; } bar;\n"
"void main(){\n"
" x = vec4(bar.x, bar.y, 0, 1);\n"
"}\n";
ShaderBufferSizeTest(4, // buffer size
0, // binding offset
4, // binding range
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, fsSource,
"Descriptor size is 4 and highest byte accessed was 7");
}
TEST_F(VkGpuAssistedLayerTest, DrawTimeShaderUniformBufferTooSmallArray) {
TEST_DESCRIPTION(
"Test that an error is produced when trying to access uniform buffer outside the bound region. Uses array in block "
"definition.");
char const *fsSource =
"#version 450\n"
"\n"
"layout(location=0) out vec4 x;\n"
"layout(set=0, binding=0) uniform readonly foo { int x[17]; } bar;\n"
"void main(){\n"
" int y = 0;\n"
" for (int i = 0; i < 17; i++)\n"
" y += bar.x[i];\n"
" x = vec4(y, 0, 0, 1);\n"
"}\n";
ShaderBufferSizeTest(64, // buffer size
0, // binding offset
64, // binding range
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, fsSource,
"Descriptor size is 64 and highest byte accessed was 67");
}
TEST_F(VkGpuAssistedLayerTest, DrawTimeShaderUniformBufferTooSmallNestedStruct) {
TEST_DESCRIPTION(
"Test that an error is produced when trying to access uniform buffer outside the bound region. Uses nested struct in block "
"definition.");
char const *fsSource =
"#version 450\n"
"\n"
"struct S {\n"
" int x;\n"
" int y;\n"
"};\n"
"layout(location=0) out vec4 x;\n"
"layout(set=0, binding=0) uniform readonly foo { int a; S b; } bar;\n"
"void main(){\n"
" x = vec4(bar.a, bar.b.x, bar.b.y, 1);\n"
"}\n";
ShaderBufferSizeTest(8, // buffer size
0, // binding offset
8, // binding range
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, fsSource,
"Descriptor size is 8 and highest byte accessed was 19");
}
TEST_F(VkGpuAssistedLayerTest, GpuBufferDeviceAddressOOB) {
SetTargetApiVersion(VK_API_VERSION_1_2);