forked from KhronosGroup/Vulkan-ValidationLayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvklayertests_viewport_inheritance.cpp
1162 lines (1018 loc) · 60.8 KB
/
vklayertests_viewport_inheritance.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) 2021-2023 The Khronos Group Inc.
* Copyright (c) 2023 LunarG, Inc.
* Copyright (c) 2021 NVIDIA Corporation
*
* 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 <array>
#include <cassert>
#include <stdio.h>
#include <vector>
#include "layer_validation_tests.h"
// Common data structures needed for tests.
class ViewportInheritanceTestData {
// Borrowed owner device.
VkDevice m_device{};
// Renderpass, draw to one attachment of format m_colorFormat.
VkFormat m_colorFormat{};
VkRenderPass m_renderPass{};
// Framebuffer data.
VkImageObj m_colorImageObj;
VkImageView m_colorImageView{};
VkFramebuffer m_framebuffer{};
// Do-nothing vertex and fragment programs.
static const uint32_t kVertexSpirV[166];
static const uint32_t kFragmentSpirV[83];
std::array<VkPipelineShaderStageCreateInfo, 2> m_shaderStages{};
// Do-nothing graphics pipelines.
// dynamic state pipelines have viewport/scissor as sole dynamic state; static state pipelines have no dynamic state.
// the i-th pipeline needs i viewports/scissors (0th uses EXT dynamic viewport/scissor count).
// m_staticStatePipelines[0] is unused.
VkPipelineLayout m_pipelineLayout{};
std::array<VkPipeline, 33> m_dynamicStatePipelines{}, m_staticStatePipelines{};
// Various premade state structs for graphics pipeline.
static const VkPipelineVertexInputStateCreateInfo kVertexInputState;
static const VkPipelineInputAssemblyStateCreateInfo kInputAssemblyState;
static const VkPipelineRasterizationStateCreateInfo kRasterizationState;
static const VkPipelineMultisampleStateCreateInfo kMultisampleState;
static const VkPipelineDepthStencilStateCreateInfo kDepthStencilState;
static const VkPipelineColorBlendAttachmentState kBlendAttachmentState;
static const VkPipelineColorBlendStateCreateInfo kBlendState;
static const VkPipelineDynamicStateCreateInfo kStaticState;
static const VkPipelineDynamicStateCreateInfo kDynamicState;
static const VkPipelineDynamicStateCreateInfo kDynamicStateWithCount;
public:
// Premade viewport and scissor arrays for testing.
static const VkViewport kViewportArray[32];
static const VkViewport kViewportDepthOnlyArray[32];
static const VkViewport kViewportAlternateDepthArray[32];
static const VkRect2D kScissorArray[32];
private:
// Set to a failure message if initialization failed.
const char* m_failureReason = nullptr;
void PickColorFormat(VkPhysicalDevice physical_device) noexcept {
std::array<VkFormat, 7> formats = {VK_FORMAT_R8G8B8A8_SRGB, VK_FORMAT_B8G8R8A8_SRGB, VK_FORMAT_R8G8B8A8_UNORM,
VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_R8G8B8A8_SNORM, VK_FORMAT_B8G8R8A8_SNORM,
VK_FORMAT_R32G32B32A32_SFLOAT};
for (VkFormat candidate : formats) {
VkImageFormatProperties properties;
VkResult result =
vk::GetPhysicalDeviceImageFormatProperties(physical_device, candidate, VK_IMAGE_TYPE_2D, VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, 0, &properties);
if (result == VK_SUCCESS) {
m_colorFormat = candidate;
return;
}
}
m_failureReason = "No color attachment format found";
}
void CreateRenderPass() noexcept {
assert(m_colorFormat != VK_FORMAT_UNDEFINED);
VkAttachmentDescription color_attachment = {0,
m_colorFormat,
VK_SAMPLE_COUNT_1_BIT,
VK_ATTACHMENT_LOAD_OP_DONT_CARE,
VK_ATTACHMENT_STORE_OP_STORE,
VK_ATTACHMENT_LOAD_OP_DONT_CARE,
VK_ATTACHMENT_STORE_OP_DONT_CARE,
VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL};
VkAttachmentReference color_reference = {0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL};
VkSubpassDescription subpass = {
0, VK_PIPELINE_BIND_POINT_GRAPHICS, 0, nullptr, 1, &color_reference, nullptr, nullptr, 0, nullptr};
VkRenderPassCreateInfo info = {
VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, nullptr, 0, 1, &color_attachment, 1, &subpass, 0, nullptr};
VkResult result = vk::CreateRenderPass(m_device, &info, nullptr, &m_renderPass);
if (result != VK_SUCCESS) m_failureReason = "Could not create render pass.";
}
void CreateColorImageObj() noexcept {
assert(m_colorFormat != VK_FORMAT_UNDEFINED);
m_colorImageObj.Init(128, 128, 1, m_colorFormat, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_TILING_OPTIMAL, 0);
if (!m_colorImageObj.initialized()) {
m_failureReason = "Image not initialized";
}
}
void CreateColorView() noexcept {
assert(!m_colorImageView);
VkImageViewCreateInfo info = {VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
nullptr,
0,
m_colorImageObj.handle(),
VK_IMAGE_VIEW_TYPE_2D,
m_colorFormat,
{},
{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
VkResult result = vk::CreateImageView(m_device, &info, nullptr, &m_colorImageView);
if (result != VK_SUCCESS) m_failureReason = "Could not create image view";
}
void CreateFramebuffer() noexcept {
assert(!m_framebuffer);
VkFramebufferCreateInfo info = {
VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, nullptr, 0, m_renderPass, 1, &m_colorImageView, 128, 128, 1};
VkResult result = vk::CreateFramebuffer(m_device, &info, nullptr, &m_framebuffer);
if (result != VK_SUCCESS) m_failureReason = "Could not create framebuffer";
}
void CreatePipelineLayout() noexcept {
assert(!m_pipelineLayout);
VkPipelineLayoutCreateInfo info = LvlInitStruct<VkPipelineLayoutCreateInfo>();
VkResult result = vk::CreatePipelineLayout(m_device, &info, nullptr, &m_pipelineLayout);
if (result != VK_SUCCESS) m_failureReason = "Could not create pipeline layout";
}
void CreateShaderStages() noexcept {
VkShaderModuleCreateInfo vertex_info = {VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, nullptr, 0, sizeof kVertexSpirV,
kVertexSpirV};
m_shaderStages[0] = LvlInitStruct<VkPipelineShaderStageCreateInfo>();
m_shaderStages[0].flags = 0;
m_shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
VkResult result = vk::CreateShaderModule(m_device, &vertex_info, nullptr, &m_shaderStages[0].module);
m_shaderStages[0].pName = "main";
m_shaderStages[0].pSpecializationInfo = nullptr;
if (result != VK_SUCCESS) {
m_failureReason = "Could not create vertex program";
return;
}
VkShaderModuleCreateInfo fragment_info = {VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, nullptr, 0, sizeof kFragmentSpirV,
kFragmentSpirV};
m_shaderStages[1] = LvlInitStruct<VkPipelineShaderStageCreateInfo>();
m_shaderStages[1].flags = 0;
m_shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
result = vk::CreateShaderModule(m_device, &fragment_info, nullptr, &m_shaderStages[1].module);
m_shaderStages[1].pName = "main";
m_shaderStages[1].pSpecializationInfo = nullptr;
if (result != VK_SUCCESS) {
m_failureReason = "Could not create fragment program";
return;
}
}
void Cleanup() {
for (VkPipeline& pipeline : m_dynamicStatePipelines) {
vk::DestroyPipeline(m_device, pipeline, nullptr);
pipeline = VK_NULL_HANDLE;
}
for (VkPipeline& pipeline : m_staticStatePipelines) {
vk::DestroyPipeline(m_device, pipeline, nullptr);
pipeline = VK_NULL_HANDLE;
}
for (auto& stage : m_shaderStages) {
vk::DestroyShaderModule(m_device, stage.module, nullptr);
stage.module = VK_NULL_HANDLE;
}
vk::DestroyPipelineLayout(m_device, m_pipelineLayout, nullptr);
m_pipelineLayout = VK_NULL_HANDLE;
vk::DestroyFramebuffer(m_device, m_framebuffer, nullptr);
m_framebuffer = VK_NULL_HANDLE;
vk::DestroyImageView(m_device, m_colorImageView, nullptr);
m_colorImageView = VK_NULL_HANDLE;
vk::DestroyRenderPass(m_device, m_renderPass, nullptr);
m_renderPass = VK_NULL_HANDLE;
}
public:
// Check if the gpu has the needed features, and call InitState requesting the needed features.
// Return whether the needed features were found or not.
template <typename AddDeviceExtension>
static bool InitState(VkRenderFramework* p_framework, AddDeviceExtension add_device_extension, const char** pp_reason,
bool inheritedViewportScissor2D, bool extended_dynamic_state_multi_viewport,
bool disable_multi_viewport = false) {
VkPhysicalDeviceExtendedDynamicStateFeaturesEXT ext = LvlInitStruct<VkPhysicalDeviceExtendedDynamicStateFeaturesEXT>();
VkPhysicalDeviceInheritedViewportScissorFeaturesNV nv =
LvlInitStruct<VkPhysicalDeviceInheritedViewportScissorFeaturesNV>(&ext);
VkPhysicalDeviceFeatures2 features2 = LvlInitStruct<VkPhysicalDeviceFeatures2>(&nv);
VkPhysicalDevice gpu = p_framework->gpu();
// Enable extended dynamic state if requested.
if (extended_dynamic_state_multi_viewport) {
if (!p_framework->DeviceExtensionSupported(VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME)) {
*pp_reason = "missing " VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME;
return false;
}
add_device_extension(VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME);
}
// Always try to enable the tested extension (but maybe won't actually enable relevant feature.)
if (p_framework->DeviceExtensionSupported(VK_NV_INHERITED_VIEWPORT_SCISSOR_EXTENSION_NAME)) {
add_device_extension(VK_NV_INHERITED_VIEWPORT_SCISSOR_EXTENSION_NAME);
} else {
*pp_reason = "missing " VK_NV_INHERITED_VIEWPORT_SCISSOR_EXTENSION_NAME;
return false;
}
vk::GetPhysicalDeviceFeatures2(gpu, &features2);
if (extended_dynamic_state_multi_viewport) {
if (!features2.features.multiViewport) {
*pp_reason = "missing multiViewport feature";
return false;
}
if (!ext.extendedDynamicState) {
*pp_reason = "missing extendedDynamicState feature";
return false;
}
}
if (!nv.inheritedViewportScissor2D && !inheritedViewportScissor2D) {
*pp_reason = "missing inheritedViewportScissor2D feature";
return false;
}
nv.inheritedViewportScissor2D = inheritedViewportScissor2D;
if (disable_multi_viewport) {
features2.features.multiViewport = VK_FALSE;
}
p_framework->InitState(nullptr, &features2, VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT);
return true;
}
ViewportInheritanceTestData(VkDeviceObj* p_device_obj, VkPhysicalDevice physical_device) noexcept
: m_colorImageObj(p_device_obj) {
m_device = p_device_obj->handle();
PickColorFormat(physical_device);
if (m_failureReason) return;
CreateRenderPass();
if (m_failureReason) return;
CreateColorImageObj();
if (m_failureReason) return;
CreateColorView();
if (m_failureReason) return;
CreateFramebuffer();
if (m_failureReason) return;
CreatePipelineLayout();
if (m_failureReason) return;
CreateShaderStages();
if (m_failureReason) return;
}
~ViewportInheritanceTestData() { Cleanup(); }
// nullptr indicates successful construction.
const char* FailureReason() const { return m_failureReason; };
// Get the graphics pipeline with the specified viewport/scissor state configuration, creating it if needed.
// viewport_scissor_count == 0 and dynamic_viewport_scissor == true indicates EXT viewport/scissor with count dynamic state.
// All pipelines are destroyed when the class is destroyed.
VkPipeline GetGraphicsPipeline(bool dynamic_viewport_scissor, uint32_t viewport_scissor_count) {
assert(dynamic_viewport_scissor || viewport_scissor_count != 0);
assert(size_t(viewport_scissor_count) < m_dynamicStatePipelines.size());
VkPipeline* p_pipeline =
&(dynamic_viewport_scissor ? m_dynamicStatePipelines : m_staticStatePipelines)[viewport_scissor_count];
if (*p_pipeline) {
return *p_pipeline;
}
// Need some static viewport/scissors if no dynamic state. Their values don't really matter; the only purpose
// of static viewport/scissor pipelines is to test messing up the dynamic state.
std::vector<VkViewport> static_viewports;
std::vector<VkRect2D> static_scissors;
if (!dynamic_viewport_scissor) {
VkViewport viewport { 0, 0, 128, 128, 0, 1 };
static_viewports = std::vector<VkViewport>(viewport_scissor_count, viewport);
static_scissors.resize(viewport_scissor_count);
}
VkPipelineViewportStateCreateInfo viewport_state = {VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
nullptr,
0,
viewport_scissor_count,
static_viewports.data(),
viewport_scissor_count,
static_scissors.data()};
const VkPipelineDynamicStateCreateInfo& dynamic_state =
dynamic_viewport_scissor == false ? kStaticState : viewport_scissor_count == 0 ? kDynamicStateWithCount : kDynamicState;
VkGraphicsPipelineCreateInfo info = {VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
nullptr,
0,
uint32_t(m_shaderStages.size()),
m_shaderStages.data(),
&kVertexInputState,
&kInputAssemblyState,
nullptr, // tess
&viewport_state,
&kRasterizationState,
&kMultisampleState,
&kDepthStencilState,
&kBlendState,
&dynamic_state,
m_pipelineLayout,
m_renderPass,
0};
VkResult result = vk::CreateGraphicsPipelines(m_device, VK_NULL_HANDLE, 1, &info, nullptr, p_pipeline);
if (result < 0) m_failureReason = "Failed to create graphics pipeline";
return result >= 0 ? *p_pipeline : VK_NULL_HANDLE;
}
// Bind the graphics pipeline with the specified viewport/scissor state configuration.
void BindGraphicsPipeline(VkCommandBuffer cmd, bool dynamic_viewport_scissor, uint32_t viewport_scissor_count) {
VkPipeline pipeline = GetGraphicsPipeline(dynamic_viewport_scissor, viewport_scissor_count);
if (pipeline) vk::CmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
}
// Make a primary command buffer and begin recording.
VkCommandBuffer MakeBeginPrimaryCommandBuffer(VkCommandPool pool) const {
VkCommandBufferAllocateInfo info = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, nullptr, pool,
VK_COMMAND_BUFFER_LEVEL_PRIMARY, 1};
VkCommandBuffer cmd;
vk::AllocateCommandBuffers(m_device, &info, &cmd);
BeginPrimaryCommandBuffer(cmd);
return cmd;
}
// Begin recording the primary command buffer.
VkResult BeginPrimaryCommandBuffer(VkCommandBuffer cmd) const {
VkCommandBufferBeginInfo info = LvlInitStruct<VkCommandBufferBeginInfo>();
return vk::BeginCommandBuffer(cmd, &info);
}
// Begin the render pass, with subpass contents provided by secondary command buffers.
void BeginRenderPass(VkCommandBuffer cmd) const {
VkRenderPassBeginInfo info =
LvlInitStruct<VkRenderPassBeginInfo>(nullptr, m_renderPass, m_framebuffer, VkRect2D{{0, 0}, {128u, 128u}}, 0u, nullptr);
vk::CmdBeginRenderPass(cmd, &info, VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS);
}
// Make a secondary (non-subpass) command buffer and begin recording.
VkCommandBuffer MakeBeginSecondaryCommandBuffer(VkCommandPool pool, VkFlags usage = 0,
const void* inheritance_pNext = nullptr) const {
VkCommandBufferAllocateInfo info = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, nullptr, pool,
VK_COMMAND_BUFFER_LEVEL_SECONDARY, 1};
VkCommandBuffer cmd;
vk::AllocateCommandBuffers(m_device, &info, &cmd);
BeginSecondaryCommandBuffer(cmd, usage, inheritance_pNext);
return cmd;
}
// Begin recording the (non-subpass) secondary command buffer.
VkResult BeginSecondaryCommandBuffer(VkCommandBuffer cmd, VkFlags usage = 0, const void* inheritance_pNext = nullptr) const {
VkCommandBufferInheritanceInfo inheritance = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO, inheritance_pNext,
m_renderPass};
VkCommandBufferBeginInfo info = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, nullptr, usage, &inheritance};
return vk::BeginCommandBuffer(cmd, &info);
}
// Make a subpass secondary command buffer (for the class's render pass) and begin recording.
// If a nonzero array of viewports is given, this enabled viewport/scissor inheritance and
// passes the list of expected viewport depths.
VkCommandBuffer MakeBeginSubpassCommandBuffer(VkCommandPool pool, uint32_t inherited_viewport_count,
const VkViewport* p_viewport_depths) const {
VkCommandBufferAllocateInfo info = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, nullptr, pool,
VK_COMMAND_BUFFER_LEVEL_SECONDARY, 1};
VkCommandBuffer cmd;
vk::AllocateCommandBuffers(m_device, &info, &cmd);
BeginSubpassCommandBuffer(cmd, inherited_viewport_count, p_viewport_depths);
return cmd;
}
// Same as above, but recycle the given secondary command buffer.
VkResult BeginSubpassCommandBuffer(VkCommandBuffer cmd, uint32_t inherited_viewport_count,
const VkViewport* p_viewport_depths) const {
VkCommandBufferInheritanceViewportScissorInfoNV viewport_scissor = {
VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_VIEWPORT_SCISSOR_INFO_NV, nullptr,
inherited_viewport_count != 0, inherited_viewport_count, p_viewport_depths };
VkCommandBufferInheritanceInfo inheritance = {
VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO, &viewport_scissor, m_renderPass, 0, m_framebuffer };
VkCommandBufferBeginInfo info = {
VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, nullptr,
VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT | VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT, &inheritance};
return vk::BeginCommandBuffer(cmd, &info);
}
};
TEST_F(VkLayerTest, ViewportInheritance) {
TEST_DESCRIPTION("Simple correct and incorrect usage of VK_NV_inherited_viewport_scissor");
m_instance_extension_names.push_back("VK_KHR_get_physical_device_properties2");
ASSERT_NO_FATAL_FAILURE(InitFramework(m_errorMonitor));
bool has_features = false;
const char* missing_feature_string = nullptr;
auto self = this;
ASSERT_NO_FATAL_FAILURE(has_features = ViewportInheritanceTestData::InitState(
this, [self](const char* extension) { self->m_device_extension_names.push_back(extension); },
&missing_feature_string, true, false));
if (!has_features) {
GTEST_SKIP() << missing_feature_string;
}
ViewportInheritanceTestData test_data(m_device, gpu());
if (test_data.FailureReason()) {
GTEST_SKIP() << "Test internal failure: " << test_data.FailureReason();
}
VkCommandPool pool = m_commandPool->handle();
VkCommandBuffer subpass_cmd = test_data.MakeBeginSubpassCommandBuffer(pool, 1, test_data.kViewportDepthOnlyArray);
test_data.BindGraphicsPipeline(subpass_cmd, true, 1);
vk::CmdDraw(subpass_cmd, 3, 1, 0, 0);
vk::EndCommandBuffer(subpass_cmd);
// Basic correct usage, provide viewport in primary that has the correct depth.
VkCommandBuffer primary_cmd = test_data.MakeBeginPrimaryCommandBuffer(pool);
vk::CmdSetViewport(primary_cmd, 0, 1, test_data.kViewportArray);
vk::CmdSetScissor(primary_cmd, 0, 1, test_data.kScissorArray);
test_data.BeginRenderPass(primary_cmd);
vk::CmdExecuteCommands(primary_cmd, 1, &subpass_cmd);
vk::CmdEndRenderPass(primary_cmd);
vk::EndCommandBuffer(primary_cmd);
// Viewport with incorrect depth range.
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07850");
test_data.BeginPrimaryCommandBuffer(primary_cmd);
vk::CmdSetViewport(primary_cmd, 0, 1, test_data.kViewportAlternateDepthArray);
vk::CmdSetScissor(primary_cmd, 0, 1, test_data.kScissorArray);
test_data.BeginRenderPass(primary_cmd);
vk::CmdExecuteCommands(primary_cmd, 1, &subpass_cmd);
vk::CmdEndRenderPass(primary_cmd);
vk::EndCommandBuffer(primary_cmd);
m_errorMonitor->VerifyFound();
// Viewport not provided.
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07850");
test_data.BeginPrimaryCommandBuffer(primary_cmd);
vk::CmdSetScissor(primary_cmd, 0, 1, test_data.kScissorArray);
test_data.BeginRenderPass(primary_cmd);
vk::CmdExecuteCommands(primary_cmd, 1, &subpass_cmd);
vk::CmdEndRenderPass(primary_cmd);
vk::EndCommandBuffer(primary_cmd);
m_errorMonitor->VerifyFound();
// Scissor not provided.
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07850");
test_data.BeginPrimaryCommandBuffer(primary_cmd);
vk::CmdSetViewport(primary_cmd, 0, 1, test_data.kViewportArray);
test_data.BeginRenderPass(primary_cmd);
vk::CmdExecuteCommands(primary_cmd, 1, &subpass_cmd);
vk::CmdEndRenderPass(primary_cmd);
vk::EndCommandBuffer(primary_cmd);
m_errorMonitor->VerifyFound();
// again (i.e. no stale state left over when resetting a secondary command buffer).
// Don't swap the loop order or you'll mess up subpass_cmd for upcoming tests.
for (int should_fail = 1; should_fail >= 0; --should_fail) {
if (should_fail) {
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07832"); // viewport
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07831"); // scissor
test_data.BeginSubpassCommandBuffer(subpass_cmd, 0, nullptr);
} else {
test_data.BeginSubpassCommandBuffer(subpass_cmd, 1, test_data.kViewportDepthOnlyArray);
}
test_data.BindGraphicsPipeline(subpass_cmd, true, 1);
vk::CmdDraw(subpass_cmd, 3, 1, 0, 0);
vk::EndCommandBuffer(subpass_cmd);
test_data.BeginPrimaryCommandBuffer(primary_cmd);
vk::CmdSetViewport(primary_cmd, 0, 1, test_data.kViewportArray);
vk::CmdSetScissor(primary_cmd, 0, 1, test_data.kScissorArray);
test_data.BeginRenderPass(primary_cmd);
vk::CmdExecuteCommands(primary_cmd, 1, &subpass_cmd);
vk::CmdEndRenderPass(primary_cmd);
vk::EndCommandBuffer(primary_cmd);
if (should_fail) m_errorMonitor->VerifyFound();
}
// Secondary that binds a static viewport/scissor pipeline.
VkCommandBuffer static_state_cmd = test_data.MakeBeginSubpassCommandBuffer(pool, 0, nullptr);
test_data.BindGraphicsPipeline(static_state_cmd, false, 1);
vk::EndCommandBuffer(static_state_cmd);
// Test that the validation layers still flag missing state when inheritance is disabled, then stops flagging it when enabled
// Test that inheritance fails if a static viewport/scissor pipeline
// trashes the state before it is inherited (but it's okay if it's
// trashed afterwards).
for (int should_fail = 0; should_fail < 2; ++should_fail) {
if (should_fail) {
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07850"); // viewport
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07850"); // scissor
} else {
}
std::array<VkCommandBuffer, 2> secondaries = {should_fail ? static_state_cmd : subpass_cmd,
should_fail ? subpass_cmd : static_state_cmd};
test_data.BeginPrimaryCommandBuffer(primary_cmd);
vk::CmdSetViewport(primary_cmd, 0, 1, test_data.kViewportArray);
vk::CmdSetScissor(primary_cmd, 0, 1, test_data.kScissorArray);
test_data.BeginRenderPass(primary_cmd);
vk::CmdExecuteCommands(primary_cmd, secondaries.size(), secondaries.data());
vk::CmdEndRenderPass(primary_cmd);
vk::EndCommandBuffer(primary_cmd);
if (should_fail) m_errorMonitor->VerifyFound();
}
// Check that the validation layers don't count the primary
// command buffer state when overwritten by static
// viewport/scissor pipeline.
for (int should_fail = 0; should_fail < 2; ++should_fail) {
if (should_fail) {
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07850"); // viewport
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07850"); // scissor
} else {
}
test_data.BeginPrimaryCommandBuffer(primary_cmd);
vk::CmdSetViewport(primary_cmd, 0, 1, test_data.kViewportArray);
vk::CmdSetScissor(primary_cmd, 0, 1, test_data.kScissorArray);
if (should_fail) test_data.BindGraphicsPipeline(primary_cmd, false, 1);
test_data.BeginRenderPass(primary_cmd);
vk::CmdExecuteCommands(primary_cmd, 1, &subpass_cmd);
vk::CmdEndRenderPass(primary_cmd);
vk::EndCommandBuffer(primary_cmd);
if (should_fail) m_errorMonitor->VerifyFound();
}
// Check that the validation layers DON'T report mismatched viewport depth when the secondary command buffer does not actually
// consume the viewport in drawing commands (weird corner case).
VkCommandBuffer no_draw_cmd = test_data.MakeBeginSubpassCommandBuffer(pool, 1, test_data.kViewportDepthOnlyArray);
test_data.BindGraphicsPipeline(no_draw_cmd, true, 1); // but no subsequent draw call.
vk::EndCommandBuffer(no_draw_cmd);
for (int should_fail = 0; should_fail < 2; ++should_fail) {
if (should_fail) {
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07850"); // viewport
} else {
}
test_data.BeginPrimaryCommandBuffer(primary_cmd);
vk::CmdSetViewport(primary_cmd, 0, 1, test_data.kViewportAlternateDepthArray);
vk::CmdSetScissor(primary_cmd, 0, 1, test_data.kScissorArray);
test_data.BeginRenderPass(primary_cmd);
vk::CmdExecuteCommands(primary_cmd, 1, should_fail ? &subpass_cmd : &no_draw_cmd);
vk::CmdEndRenderPass(primary_cmd);
vk::EndCommandBuffer(primary_cmd);
if (should_fail) m_errorMonitor->VerifyFound();
}
// Check that the validation layers are not okay with binding static viewport/scissor pipelines when inheritance enabled, or
// setting viewport/scissor explicitly, but are okay if inheritance is not enabled (no regression).
for (int should_fail = 0; should_fail < 2; ++should_fail) {
if (should_fail) m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdBindPipeline-commandBuffer-04808");
test_data.BeginSubpassCommandBuffer(no_draw_cmd, should_fail, test_data.kViewportDepthOnlyArray);
test_data.BindGraphicsPipeline(no_draw_cmd, false, 1);
if (should_fail) m_errorMonitor->VerifyFound();
// Check that the validation layers flag setting viewport/scissor with inheritance.
if (should_fail) m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdSetViewport-commandBuffer-04821");
vk::CmdSetViewport(no_draw_cmd, 0, 1, test_data.kViewportArray);
if (should_fail) m_errorMonitor->VerifyFound();
if (should_fail) m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdSetScissor-viewportScissor2D-04789");
vk::CmdSetScissor(no_draw_cmd, 0, 1, test_data.kScissorArray);
if (should_fail) m_errorMonitor->VerifyFound();
vk::EndCommandBuffer(no_draw_cmd);
}
// Check for at least 1 viewport depth given when enabling inheritance.
for (int should_fail = 0; should_fail < 2; ++should_fail) {
if (should_fail) {
m_errorMonitor->SetDesiredFailureMsg(kErrorBit,
"VUID-VkCommandBufferInheritanceViewportScissorInfoNV-viewportScissor2D-04784");
} else {
}
VkCommandBufferInheritanceViewportScissorInfoNV viewport_scissor = {
VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_VIEWPORT_SCISSOR_INFO_NV, nullptr, should_fail ? VK_TRUE : VK_FALSE,
0, test_data.kViewportArray /* avoid null pointer crash still */ };
VkCommandBuffer cmd =
test_data.MakeBeginSecondaryCommandBuffer(pool, VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT, &viewport_scissor);
// vk::EndCommandBuffer(cmd); // seg faults.
(void)cmd;
if (should_fail) m_errorMonitor->VerifyFound();
}
// Check for VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-VkCommandBufferInheritanceViewportScissorInfoNV-viewportScissor2D-04786");
VkCommandBufferInheritanceViewportScissorInfoNV viewport_scissor = {
VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_VIEWPORT_SCISSOR_INFO_NV, nullptr, VK_TRUE, 1, test_data.kViewportArray};
test_data.MakeBeginSecondaryCommandBuffer(pool, 0, &viewport_scissor);
m_errorMonitor->VerifyFound();
// Check that validation layers allow getting inherited viewport/scissor state from earlier secondary command buffer, but not
// from a different vkCmdExecuteCommands.
VkCommandBuffer set_viewport_cmd = test_data.MakeBeginSubpassCommandBuffer(pool, 0, nullptr);
vk::CmdSetViewport(set_viewport_cmd, 0, 1, test_data.kViewportArray);
vk::EndCommandBuffer(set_viewport_cmd);
VkCommandBuffer set_scissor_cmd = test_data.MakeBeginSubpassCommandBuffer(pool, 0, nullptr);
vk::CmdSetScissor(set_scissor_cmd, 0, 1, test_data.kScissorArray);
vk::EndCommandBuffer(set_scissor_cmd);
for (int should_fail = 0; should_fail < 2; ++should_fail) {
test_data.BeginPrimaryCommandBuffer(primary_cmd);
test_data.BeginRenderPass(primary_cmd);
if (should_fail) {
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07850"); // viewport
vk::CmdExecuteCommands(primary_cmd, 1, &set_viewport_cmd);
VkCommandBuffer secondaries[2] = {set_scissor_cmd, subpass_cmd};
vk::CmdExecuteCommands(primary_cmd, 2, secondaries);
} else {
VkCommandBuffer secondaries[3] = {set_viewport_cmd, set_scissor_cmd, subpass_cmd};
vk::CmdExecuteCommands(primary_cmd, 3, secondaries);
}
vk::CmdEndRenderPass(primary_cmd);
vk::EndCommandBuffer(primary_cmd);
if (should_fail) m_errorMonitor->VerifyFound();
}
}
TEST_F(VkLayerTest, ViewportInheritanceMissingFeature) {
TEST_DESCRIPTION("Error using VK_NV_inherited_viewport_scissor without enabling feature.");
m_instance_extension_names.push_back("VK_KHR_get_physical_device_properties2");
ASSERT_NO_FATAL_FAILURE(InitFramework(m_errorMonitor));
bool has_features = false;
const char* missing_feature_string = nullptr;
auto self = this;
ASSERT_NO_FATAL_FAILURE(has_features = ViewportInheritanceTestData::InitState(
this, [self](const char* extension) { self->m_device_extension_names.push_back(extension); },
&missing_feature_string, false, false));
if (!has_features) {
GTEST_SKIP() << missing_feature_string;
}
ViewportInheritanceTestData test_data(m_device, gpu());
if (test_data.FailureReason()) {
GTEST_SKIP() << "Test internal failure: " << test_data.FailureReason();
}
VkCommandPool pool = m_commandPool->handle();
test_data.MakeBeginSubpassCommandBuffer(pool, 0, nullptr);
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-VkCommandBufferInheritanceViewportScissorInfoNV-viewportScissor2D-04782");
test_data.MakeBeginSubpassCommandBuffer(pool, 1, test_data.kViewportArray);
m_errorMonitor->VerifyFound();
}
TEST_F(VkLayerTest, ViewportInheritanceMultiViewport) {
TEST_DESCRIPTION("VK_NV_inherited_viewport_scissor tests with multiple viewports/scissors");
m_instance_extension_names.push_back("VK_KHR_get_physical_device_properties2");
ASSERT_NO_FATAL_FAILURE(InitFramework(m_errorMonitor));
bool has_features = false;
const char* missing_feature_string = nullptr;
auto self = this;
ASSERT_NO_FATAL_FAILURE(has_features = ViewportInheritanceTestData::InitState(
this, [self](const char* extension) { self->m_device_extension_names.push_back(extension); },
&missing_feature_string, true, true));
if (!has_features) {
GTEST_SKIP() << missing_feature_string;
}
ViewportInheritanceTestData test_data(m_device, gpu());
if (test_data.FailureReason()) {
GTEST_SKIP() << "Test internal failure: " << test_data.FailureReason();
}
VkCommandPool pool = m_commandPool->handle();
// Test using viewport/scissor with count state without providing it to be inherited.
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07850"); // viewport
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07850"); // scissor
VkCommandBuffer draw_cmd = test_data.MakeBeginSubpassCommandBuffer(pool, 1, test_data.kViewportArray);
test_data.BindGraphicsPipeline(draw_cmd, true, 0 /* dynamic viewport and scissor count */);
vk::CmdDraw(draw_cmd, 3, 1, 0, 0);
vk::EndCommandBuffer(draw_cmd);
VkCommandBuffer primary_cmd = test_data.MakeBeginPrimaryCommandBuffer(pool);
vk::CmdSetViewport(primary_cmd, 0, 1, test_data.kViewportArray); // inadequate, needs with count
vk::CmdSetScissor(primary_cmd, 0, 1, test_data.kScissorArray);
test_data.BeginRenderPass(primary_cmd);
vk::CmdExecuteCommands(primary_cmd, 1, &draw_cmd);
vk::CmdEndRenderPass(primary_cmd);
vk::EndCommandBuffer(primary_cmd);
m_errorMonitor->VerifyFound();
// Test drawing with pipeline that uses more viewports than have been inherited.
for (int i = 0; i < 4; ++i) {
auto should_fail = i & 1;
if (should_fail) m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07850"); // viewport
test_data.BeginSubpassCommandBuffer(draw_cmd, should_fail ? 1 : 2, test_data.kViewportDepthOnlyArray);
test_data.BindGraphicsPipeline(draw_cmd, true, 2); // Uses 2 viewports
vk::CmdDraw(draw_cmd, 3, 1, 0, 0);
if (i & 2) {
// Uses only 1 viewport, should not cause us to "forget" the earlier need for 2 viewports.
test_data.BindGraphicsPipeline(draw_cmd, true, 1);
vk::CmdDraw(draw_cmd, 3, 1, 0, 0);
}
vk::EndCommandBuffer(draw_cmd);
test_data.BeginPrimaryCommandBuffer(primary_cmd);
vk::CmdSetViewport(primary_cmd, 0, 2, test_data.kViewportArray);
vk::CmdSetScissor(primary_cmd, 0, 2, test_data.kScissorArray);
test_data.BeginRenderPass(primary_cmd);
vk::CmdExecuteCommands(primary_cmd, 1, &draw_cmd);
vk::CmdEndRenderPass(primary_cmd);
vk::EndCommandBuffer(primary_cmd);
if (should_fail) m_errorMonitor->VerifyFound();
}
// Test providing needed viewports in secondary command buffer, and trashing it by binding static state pipeline in another
// secondary command buffer.
VkCommandBuffer set_state_fixed_count_cmd = test_data.MakeBeginSubpassCommandBuffer(pool, 0, nullptr);
vk::CmdSetViewport(set_state_fixed_count_cmd, 0, 2, test_data.kViewportArray);
vk::CmdSetScissor(set_state_fixed_count_cmd, 0, 2, test_data.kScissorArray);
vk::EndCommandBuffer(set_state_fixed_count_cmd);
auto vkCmdSetViewportWithCountEXT =
PFN_vkCmdSetViewportWithCountEXT(vk::GetDeviceProcAddr(m_device->handle(), "vkCmdSetViewportWithCountEXT"));
assert(vkCmdSetViewportWithCountEXT);
auto vkCmdSetScissorWithCountEXT =
PFN_vkCmdSetScissorWithCountEXT(vk::GetDeviceProcAddr(m_device->handle(), "vkCmdSetScissorWithCountEXT"));
assert(vkCmdSetScissorWithCountEXT);
VkCommandBuffer set_state_with_count_cmd = test_data.MakeBeginSubpassCommandBuffer(pool, 0, nullptr);
vkCmdSetViewportWithCountEXT(set_state_with_count_cmd, 2, test_data.kViewportArray);
vkCmdSetScissorWithCountEXT(set_state_with_count_cmd, 2, test_data.kScissorArray);
vk::EndCommandBuffer(set_state_with_count_cmd);
VkCommandBuffer static_state_cmd = test_data.MakeBeginSubpassCommandBuffer(pool, 0, nullptr);
test_data.BindGraphicsPipeline(static_state_cmd, false, 2);
test_data.BindGraphicsPipeline(static_state_cmd, true, 1); // Should not "forgive" eariler static state pipeline.
vk::EndCommandBuffer(static_state_cmd);
for (int i = 0; i < 8; ++i) {
auto should_fail = i & 1;
auto use_with_count = i & 4;
if (should_fail) {
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07850"); // viewport 0 (or with count)
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07850"); // scissor 0 (or with count)
if (!use_with_count) {
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07850"); // viewport 1
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07850"); // scissor 1
}
} else {
}
test_data.BeginSubpassCommandBuffer(draw_cmd, 2, test_data.kViewportDepthOnlyArray);
test_data.BindGraphicsPipeline(draw_cmd, true, use_with_count ? 0 : 2);
vk::CmdDraw(draw_cmd, 3, 1, 0, 0);
vk::EndCommandBuffer(draw_cmd);
VkCommandBuffer set_state_cmd = use_with_count ? set_state_with_count_cmd : set_state_fixed_count_cmd;
VkCommandBuffer secondaries[3];
uint32_t secondaries_count;
switch (i % 4) {
case 0:
secondaries[0] = static_state_cmd;
secondaries[1] = set_state_cmd;
secondaries[2] = draw_cmd;
secondaries_count = 3;
break;
case 1:
secondaries[0] = draw_cmd;
secondaries_count = 1;
break;
case 2:
secondaries[0] = set_state_cmd;
secondaries[1] = draw_cmd;
secondaries[2] = static_state_cmd; // Okay as it's after the drawing commands.
secondaries_count = 3;
break;
case 3: default:
secondaries[0] = set_state_cmd;
secondaries[1] = static_state_cmd; // Trashes the dynamic state
secondaries[2] = draw_cmd;
secondaries_count = 3;
break;
}
test_data.BeginPrimaryCommandBuffer(primary_cmd);
test_data.BeginRenderPass(primary_cmd);
vk::CmdExecuteCommands(primary_cmd, secondaries_count, secondaries);
vk::CmdEndRenderPass(primary_cmd);
vk::EndCommandBuffer(primary_cmd);
if (should_fail) m_errorMonitor->VerifyFound();
}
// Test mismatched depth count detection, but allow it if the mismatched viewport is not actually used.
// 0: viewport 1 mismatch, passes as draw only consumes correct 0th viewport.
// 1: fail, mismatched viewport 1.
// 2: pass, 2 correct viewports used. (also tests vkCmdSetViewport split between primary and secondary).
for (int i = 0; i < 3; ++i) {
auto should_fail = i & 1;
if (should_fail) {
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07850"); // viewport 1
} else {
}
test_data.BeginSubpassCommandBuffer(draw_cmd, 2, test_data.kViewportDepthOnlyArray);
test_data.BindGraphicsPipeline(draw_cmd, true, i == 0 ? 1 : 2);
vk::CmdDraw(draw_cmd, 3, 1, 0, 0);
vk::EndCommandBuffer(draw_cmd);
test_data.BeginSubpassCommandBuffer(set_state_fixed_count_cmd, 0, nullptr);
vk::CmdSetViewport(set_state_fixed_count_cmd, 1, 1, i == 2 ? &test_data.kViewportArray[1] : &test_data.kViewportAlternateDepthArray[1]);
vk::CmdSetScissor(set_state_fixed_count_cmd, 1, 1, &test_data.kScissorArray[1]);
vk::EndCommandBuffer(set_state_fixed_count_cmd);
test_data.BeginPrimaryCommandBuffer(primary_cmd);
vk::CmdSetViewport(primary_cmd, 0, 1, &test_data.kViewportArray[0]);
vk::CmdSetScissor(primary_cmd, 0, 1, &test_data.kScissorArray[0]);
test_data.BeginRenderPass(primary_cmd);
VkCommandBuffer secondaries[] = {set_state_fixed_count_cmd, draw_cmd};
vk::CmdExecuteCommands(primary_cmd, 2, secondaries);
vk::CmdEndRenderPass(primary_cmd);
vk::EndCommandBuffer(primary_cmd);
if (should_fail) m_errorMonitor->VerifyFound();
}
// Test dynamic viewport count failing due to either exceeding the viewport inheritance limit, or inheriting a viewport with
// incorrect depth.
for (int i = 0; i < 8; ++i) {
auto state_in_secondary = i & 1;
auto should_fail = i & 2;
auto inherited_incorrect_depth = i & 4;
if (should_fail) m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdDraw-None-07850");
VkViewport viewports[3];
viewports[0] = test_data.kViewportArray[0];
viewports[1] = test_data.kViewportArray[1];
if (inherited_incorrect_depth) {
viewports[2] = test_data.kViewportAlternateDepthArray[2]; // Will be a problem only when using all 3 viewports.
}
else {
viewports[2] = test_data.kViewportArray[2];
}
uint32_t inherited_count = should_fail && !inherited_incorrect_depth ? 2 : 3;
test_data.BeginSubpassCommandBuffer(draw_cmd, inherited_count, test_data.kViewportDepthOnlyArray);
test_data.BindGraphicsPipeline(draw_cmd, true, 0);
vk::CmdDraw(draw_cmd, 3, 1, 0, 0);
vk::EndCommandBuffer(draw_cmd);
test_data.BeginPrimaryCommandBuffer(primary_cmd);
// Decoy command, should be overwritten later.
vkCmdSetViewportWithCountEXT(primary_cmd, 3, test_data.kViewportAlternateDepthArray);
// Record state setting commands in either the primary, or auxilliary secondary, command buffer.
VkCommandBuffer set_state_cmd;
if (state_in_secondary) {
set_state_cmd = set_state_with_count_cmd;
test_data.BeginSubpassCommandBuffer(set_state_cmd, 0, nullptr);
}
else {
set_state_cmd = primary_cmd;
}
uint32_t count = should_fail ? 3 : 2;
vkCmdSetViewportWithCountEXT(set_state_cmd, count, viewports);
vkCmdSetScissorWithCountEXT(set_state_cmd, count, test_data.kScissorArray);
if (state_in_secondary) {
vk::EndCommandBuffer(set_state_cmd);
}
test_data.BeginRenderPass(primary_cmd);
if (state_in_secondary) {
VkCommandBuffer secondaries[] = {set_state_cmd, draw_cmd};
vk::CmdExecuteCommands(primary_cmd, 2, secondaries);
}
else {
vk::CmdExecuteCommands(primary_cmd, 1, &draw_cmd);
}
vk::CmdEndRenderPass(primary_cmd);
vk::EndCommandBuffer(primary_cmd);
if (should_fail) m_errorMonitor->VerifyFound();
}
}
TEST_F(VkLayerTest, ViewportInheritanceScissorMissingFeature) {
TEST_DESCRIPTION("Error using VK_NV_inherited_viewport_scissor without enabling multiViewport feature.");
m_instance_extension_names.push_back("VK_KHR_get_physical_device_properties2");
ASSERT_NO_FATAL_FAILURE(InitFramework(m_errorMonitor));
bool has_features = false;
const char* missing_feature_string = nullptr;
auto self = this;
ASSERT_NO_FATAL_FAILURE(has_features = ViewportInheritanceTestData::InitState(
this, [self](const char* extension) { self->m_device_extension_names.push_back(extension); },
&missing_feature_string, true, false, true));
if (!has_features) {
GTEST_SKIP() << missing_feature_string;
}
ViewportInheritanceTestData test_data(m_device, gpu());
if (test_data.FailureReason()) {
GTEST_SKIP() << "Test internal failure: " << test_data.FailureReason();
}
VkCommandPool pool = m_commandPool->handle();
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-VkCommandBufferInheritanceViewportScissorInfoNV-viewportScissor2D-04783");
test_data.MakeBeginSubpassCommandBuffer(pool, 2, test_data.kViewportArray);
m_errorMonitor->VerifyFound();
}
TEST_F(VkLayerTest, PipelineMissingDynamicStateDiscardRectangle) {
TEST_DESCRIPTION("Bind pipeline with missing dynamic state discard rectangle.");
AddRequiredExtensions(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
AddRequiredExtensions(VK_EXT_DISCARD_RECTANGLES_EXTENSION_NAME);
ASSERT_NO_FATAL_FAILURE(InitFramework(m_errorMonitor));
if (!AreRequiredExtensionsEnabled()) {
GTEST_SKIP() << RequiredExtensionsNotSupported() << " not supported";
}
auto discard_rect_props = LvlInitStruct<VkPhysicalDeviceDiscardRectanglePropertiesEXT>();
GetPhysicalDeviceProperties2(discard_rect_props);
if (discard_rect_props.maxDiscardRectangles < 1) {
GTEST_SKIP() << "maxDiscardRectangles property is less than 1.";
}
bool has_features = false;
const char* missing_feature_string = nullptr;
auto self = this;
ASSERT_NO_FATAL_FAILURE(has_features = ViewportInheritanceTestData::InitState(
this, [self](const char* extension) { self->m_device_extension_names.push_back(extension); },
&missing_feature_string, true, false, true));
if (!has_features) {
GTEST_SKIP() << missing_feature_string;
}
ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
VkCommandPoolObj pool(m_device, m_device->graphics_queue_node_index_, VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT);
VkCommandBufferObj secondary(m_device, &pool, VK_COMMAND_BUFFER_LEVEL_SECONDARY);
ViewportInheritanceTestData test_data(m_device, gpu());
if (test_data.FailureReason()) {
GTEST_SKIP() << "Test internal failure: " << test_data.FailureReason();
}
VkCommandBufferInheritanceViewportScissorInfoNV viewport_scissor =
LvlInitStruct<VkCommandBufferInheritanceViewportScissorInfoNV>();
viewport_scissor.viewportScissor2D = VK_TRUE;
viewport_scissor.viewportDepthCount = 1;
viewport_scissor.pViewportDepths = test_data.kViewportArray;
VkCommandBufferInheritanceInfo cbii = LvlInitStruct<VkCommandBufferInheritanceInfo>(&viewport_scissor);
cbii.renderPass = m_renderPass;
VkCommandBufferBeginInfo cbbi = LvlInitStruct<VkCommandBufferBeginInfo>();
cbbi.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT | VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT;
cbbi.pInheritanceInfo = &cbii;
VkRect2D discard_rectangle = {};
VkPipelineDiscardRectangleStateCreateInfoEXT discard_rectangle_state =
LvlInitStruct<VkPipelineDiscardRectangleStateCreateInfoEXT>();
discard_rectangle_state.discardRectangleCount = 1;
discard_rectangle_state.pDiscardRectangles = &discard_rectangle;
const VkDynamicState dyn_states[] = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR};
VkPipelineDynamicStateCreateInfo dyn_state_ci = LvlInitStruct<VkPipelineDynamicStateCreateInfo>();
dyn_state_ci.dynamicStateCount = 2;
dyn_state_ci.pDynamicStates = dyn_states;
CreatePipelineHelper pipe(*this);
pipe.InitInfo();
pipe.gp_ci_.pNext = &discard_rectangle_state;
pipe.gp_ci_.pDynamicState = &dyn_state_ci;
pipe.InitState();
pipe.CreateGraphicsPipeline();
vk::BeginCommandBuffer(secondary.handle(), &cbbi);
m_errorMonitor->SetDesiredFailureMsg(kErrorBit, "VUID-vkCmdBindPipeline-commandBuffer-04809");
vk::CmdBindPipeline(secondary.handle(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipe.pipeline_);
m_errorMonitor->VerifyFound();