forked from KhronosGroup/Vulkan-ValidationLayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vkrenderframework.cpp
2449 lines (2036 loc) · 103 KB
/
vkrenderframework.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) 2015-2022 The Khronos Group Inc.
* Copyright (c) 2015-2022 Valve Corporation
* Copyright (c) 2015-2022 LunarG, Inc.
* Copyright (c) 2015-2022 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
* Author: Tony Barbour <tony@LunarG.com>
* Author: Dave Houlton <daveh@lunarg.com>
*/
#include "vkrenderframework.h"
#include <algorithm>
#include <cassert>
#include <cstring>
#include <utility>
#include <vector>
#include "vk_format_utils.h"
#include "vk_extension_helper.h"
#include "vk_layer_settings_ext.h"
using std::string;
using std::strncmp;
using std::vector;
template <typename C, typename F>
typename C::iterator RemoveIf(C &container, F &&fn) {
return container.erase(std::remove_if(container.begin(), container.end(), std::forward<F>(fn)), container.end());
}
void DebugReporter::Create(VkInstance instance) noexcept {
assert(instance);
assert(!debug_obj_);
auto DebugCreate = reinterpret_cast<DebugCreateFnType>(vk::GetInstanceProcAddr(instance, debug_create_fn_name_));
if (!DebugCreate) return;
const VkResult err = DebugCreate(instance, &debug_create_info_, nullptr, &debug_obj_);
if (err) debug_obj_ = VK_NULL_HANDLE;
}
void DebugReporter::Destroy(VkInstance instance) noexcept {
assert(instance);
assert(debug_obj_); // valid to call with null object, but probably bug
auto DebugDestroy = reinterpret_cast<DebugDestroyFnType>(vk::GetInstanceProcAddr(instance, debug_destroy_fn_name_));
assert(DebugDestroy);
DebugDestroy(instance, debug_obj_, nullptr);
debug_obj_ = VK_NULL_HANDLE;
}
#ifdef VK_USE_PLATFORM_ANDROID_KHR
VKAPI_ATTR VkBool32 VKAPI_CALL DebugReporter::DebugCallback(VkDebugReportFlagsEXT message_flags, VkDebugReportObjectTypeEXT,
uint64_t, size_t, int32_t, const char *, const char *message,
void *user_data) {
#else
VKAPI_ATTR VkBool32 VKAPI_CALL DebugReporter::DebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
VkDebugUtilsMessageTypeFlagsEXT message_types,
const VkDebugUtilsMessengerCallbackDataEXT *callback_data,
void *user_data) {
const auto message_flags = DebugAnnotFlagsToReportFlags(message_severity, message_types);
const char *message = callback_data->pMessage;
#endif
ErrorMonitor *errMonitor = (ErrorMonitor *)user_data;
if (message_flags & errMonitor->GetMessageFlags()) {
return errMonitor->CheckForDesiredMsg(message);
}
return VK_FALSE;
}
VkRenderFramework::VkRenderFramework()
: instance_(NULL),
m_device(NULL),
m_commandPool(VK_NULL_HANDLE),
m_commandBuffer(NULL),
m_renderPass(VK_NULL_HANDLE),
m_framebuffer(VK_NULL_HANDLE),
m_surface(VK_NULL_HANDLE),
#if defined(VK_USE_PLATFORM_WIN32_KHR)
m_win32Window(nullptr),
#endif
#if defined(VK_USE_PLATFORM_XLIB_KHR)
m_surface_dpy(nullptr),
m_surface_window(None),
#endif
#if defined(VK_USE_PLATFORM_XCB_KHR)
m_surface_xcb_conn(nullptr),
#endif
m_swapchain(VK_NULL_HANDLE),
m_addRenderPassSelfDependency(false),
m_width(256.0), // default window width
m_height(256.0), // default window height
m_render_target_fmt(VK_FORMAT_R8G8B8A8_UNORM),
m_depth_stencil_fmt(VK_FORMAT_UNDEFINED),
m_clear_via_load_op(true),
m_depth_clear_color(1.0),
m_stencil_clear_color(0),
m_depthStencil(NULL) {
m_framebuffer_info = LvlInitStruct<VkFramebufferCreateInfo>();
m_renderPass_info = LvlInitStruct<VkRenderPassCreateInfo>();
m_renderPassBeginInfo = LvlInitStruct<VkRenderPassBeginInfo>();
// clear the back buffer to dark grey
m_clear_color.float32[0] = 0.25f;
m_clear_color.float32[1] = 0.25f;
m_clear_color.float32[2] = 0.25f;
m_clear_color.float32[3] = 0.0f;
}
VkRenderFramework::~VkRenderFramework() {
ShutdownFramework();
debug_reporter_.error_monitor_.Finish();
}
VkPhysicalDevice VkRenderFramework::gpu() {
EXPECT_NE((VkInstance)0, instance_); // Invalid to request gpu before instance exists
return gpu_;
}
const VkPhysicalDeviceProperties &VkRenderFramework::physDevProps() {
EXPECT_NE((VkPhysicalDevice)0, gpu_); // Invalid to request physical device properties before gpu
return physDevProps_;
}
// Return true if layer name is found and spec+implementation values are >= requested values
bool VkRenderFramework::InstanceLayerSupported(const char *const layer_name, const uint32_t spec_version,
const uint32_t impl_version) {
if (available_layers_.empty()) {
available_layers_ = vk_testing::GetGlobalLayers();
}
for (const auto &layer : available_layers_) {
if (0 == strncmp(layer_name, layer.layerName, VK_MAX_EXTENSION_NAME_SIZE)) {
return layer.specVersion >= spec_version && layer.implementationVersion >= impl_version;
}
}
return false;
}
// Return true if extension name is found and spec value is >= requested spec value
// WARNING: for simplicity, does not cover layers' extensions
bool VkRenderFramework::InstanceExtensionSupported(const char *const extension_name, const uint32_t spec_version) {
// WARNING: assume debug and validation feature extensions are always supported, which are usually provided by layers
if (0 == strncmp(extension_name, VK_EXT_DEBUG_UTILS_EXTENSION_NAME, VK_MAX_EXTENSION_NAME_SIZE)) return true;
if (0 == strncmp(extension_name, VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_MAX_EXTENSION_NAME_SIZE)) return true;
if (0 == strncmp(extension_name, VK_EXT_VALIDATION_FEATURES_EXTENSION_NAME, VK_MAX_EXTENSION_NAME_SIZE)) return true;
if (available_extensions_.empty()) {
available_extensions_ = vk_testing::GetGlobalExtensions();
}
const auto IsTheQueriedExtension = [extension_name, spec_version](const VkExtensionProperties &extension) {
return strncmp(extension_name, extension.extensionName, VK_MAX_EXTENSION_NAME_SIZE) == 0 &&
extension.specVersion >= spec_version;
};
return std::any_of(available_extensions_.begin(), available_extensions_.end(), IsTheQueriedExtension);
}
// Return true if instance exists and extension name is in the list
bool VkRenderFramework::InstanceExtensionEnabled(const char *ext_name) {
if (!instance_) return false;
return std::any_of(instance_extensions_.begin(), instance_extensions_.end(),
[ext_name](const char *e) { return 0 == strncmp(ext_name, e, VK_MAX_EXTENSION_NAME_SIZE); });
}
// Return true if extension name is found and spec value is >= requested spec value
bool VkRenderFramework::DeviceExtensionSupported(const char *extension_name, const uint32_t spec_version) const {
if (!instance_ || !gpu_) {
EXPECT_NE((VkInstance)0, instance_); // Complain, not cool without an instance
EXPECT_NE((VkPhysicalDevice)0, gpu_);
return false;
}
const vk_testing::PhysicalDevice device_obj(gpu_);
const auto enabled_layers = instance_layers_; // assumes instance_layers_ contains enabled layers
auto extensions = device_obj.extensions();
for (const auto &layer : enabled_layers) {
const auto layer_extensions = device_obj.extensions(layer);
extensions.insert(extensions.end(), layer_extensions.begin(), layer_extensions.end());
}
const auto IsTheQueriedExtension = [extension_name, spec_version](const VkExtensionProperties &extension) {
return strncmp(extension_name, extension.extensionName, VK_MAX_EXTENSION_NAME_SIZE) == 0 &&
extension.specVersion >= spec_version;
};
return std::any_of(extensions.begin(), extensions.end(), IsTheQueriedExtension);
}
// Return true if device is created and extension name is found in the list
bool VkRenderFramework::DeviceExtensionEnabled(const char *ext_name) {
if (NULL == m_device) return false;
bool ext_found = false;
for (auto ext : m_device_extension_names) {
if (!strncmp(ext, ext_name, VK_MAX_EXTENSION_NAME_SIZE)) {
ext_found = true;
break;
}
}
return ext_found;
}
VkInstanceCreateInfo VkRenderFramework::GetInstanceCreateInfo() const {
#ifdef VK_USE_PLATFORM_METAL_EXT
return {
VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
&debug_reporter_.debug_create_info_,
VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR,
&app_info_,
static_cast<uint32_t>(instance_layers_.size()),
instance_layers_.data(),
static_cast<uint32_t>(instance_extensions_.size()),
instance_extensions_.data(),
};
#else
return {
VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
&debug_reporter_.debug_create_info_,
0,
&app_info_,
static_cast<uint32_t>(instance_layers_.size()),
instance_layers_.data(),
static_cast<uint32_t>(instance_extensions_.size()),
instance_extensions_.data(),
};
#endif
}
inline void CheckDisableCoreValidation(VkValidationFeaturesEXT &features) {
auto disable = GetEnvironment("VK_LAYER_TESTS_DISABLE_CORE_VALIDATION");
std::transform(disable.begin(), disable.end(), disable.begin(), ::tolower);
if (disable == "false" || disable == "0" || disable == "FALSE") { // default is to change nothing, unless flag is correctly specified
features.disabledValidationFeatureCount = 0; // remove all disables to get all validation messages
}
}
void *VkRenderFramework::SetupValidationSettings(void *first_pnext) {
auto validation = GetEnvironment("VK_LAYER_TESTS_VALIDATION_FEATURES");
std::transform(validation.begin(), validation.end(), validation.begin(), ::tolower);
VkValidationFeaturesEXT *features = LvlFindModInChain<VkValidationFeaturesEXT>(first_pnext);
if (features) {
CheckDisableCoreValidation(*features);
}
if (validation == "all" || validation == "core" || validation == "none") {
if (!features) {
features = &validation_features;
features->sType = VK_STRUCTURE_TYPE_VALIDATION_FEATURES_EXT;
features->pNext = first_pnext;
first_pnext = features;
}
if (validation == "all") {
features->enabledValidationFeatureCount = 4;
features->pEnabledValidationFeatures = validation_enable_all;
features->disabledValidationFeatureCount = 0;
} else if (validation == "core") {
features->disabledValidationFeatureCount = 0;
} else if (validation == "none") {
features->disabledValidationFeatureCount = 1;
features->pDisabledValidationFeatures = &validation_disable_all;
features->enabledValidationFeatureCount = 0;
}
}
return first_pnext;
}
void VkRenderFramework::InitFramework(void * /*unused compatibility parameter*/, void *instance_pnext) {
ASSERT_EQ((VkInstance)0, instance_);
const auto LayerNotSupportedWithReporting = [this](const char *layer) {
if (InstanceLayerSupported(layer))
return false;
else {
ADD_FAILURE() << "InitFramework(): Requested layer \"" << layer << "\" is not supported. It will be disabled.";
return true;
}
};
const auto ExtensionNotSupportedWithReporting = [this](const char *extension) {
if (InstanceExtensionSupported(extension))
return false;
else {
ADD_FAILURE() << "InitFramework(): Requested extension \"" << extension << "\" is not supported. It will be disabled.";
return true;
}
};
static bool driver_printed = false;
static bool print_driver_info = GetEnvironment("VK_LAYER_TESTS_PRINT_DRIVER") != "";
if (print_driver_info && !driver_printed &&
InstanceExtensionSupported(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME)) {
instance_extensions_.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
}
#ifdef VK_USE_PLATFORM_METAL_EXT
instance_extensions_.push_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);
#endif
RemoveIf(instance_layers_, LayerNotSupportedWithReporting);
RemoveIf(instance_extensions_, ExtensionNotSupportedWithReporting);
auto ici = GetInstanceCreateInfo();
// If is validation features then check for disabled validation
instance_pnext = SetupValidationSettings(instance_pnext);
// concatenate pNexts
void *last_pnext = nullptr;
if (instance_pnext) {
last_pnext = instance_pnext;
while (reinterpret_cast<const VkBaseOutStructure *>(last_pnext)->pNext)
last_pnext = reinterpret_cast<VkBaseOutStructure *>(last_pnext)->pNext;
void *&link = reinterpret_cast<void *&>(reinterpret_cast<VkBaseOutStructure *>(last_pnext)->pNext);
link = const_cast<void *>(ici.pNext);
ici.pNext = instance_pnext;
}
ASSERT_VK_SUCCESS(vk::CreateInstance(&ici, nullptr, &instance_));
if (instance_pnext) reinterpret_cast<VkBaseOutStructure *>(last_pnext)->pNext = nullptr; // reset back borrowed pNext chain
// Choose a physical device
uint32_t gpu_count = 0;
const VkResult err = vk::EnumeratePhysicalDevices(instance_, &gpu_count, nullptr);
ASSERT_TRUE(err == VK_SUCCESS || err == VK_INCOMPLETE) << vk_result_string(err);
ASSERT_GT(gpu_count, (uint32_t)0) << "No GPU (i.e. VkPhysicalDevice) available";
std::vector<VkPhysicalDevice> phys_devices(gpu_count);
vk::EnumeratePhysicalDevices(instance_, &gpu_count, phys_devices.data());
const int phys_device_index = VkTestFramework::m_phys_device_index;
if ((phys_device_index >= 0) && (phys_device_index < static_cast<int>(gpu_count))) {
gpu_ = phys_devices[phys_device_index];
vk::GetPhysicalDeviceProperties(gpu_, &physDevProps_);
m_gpu_index = phys_device_index;
} else {
// Specify a "physical device priority" with larger values meaning higher priority.
std::array<int, VK_PHYSICAL_DEVICE_TYPE_CPU + 1> device_type_rank;
device_type_rank[VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU] = 4;
device_type_rank[VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU] = 3;
device_type_rank[VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU] = 2;
device_type_rank[VK_PHYSICAL_DEVICE_TYPE_CPU] = 1;
device_type_rank[VK_PHYSICAL_DEVICE_TYPE_OTHER] = 0;
// Initialize physical device and properties with first device found
gpu_ = phys_devices[0];
m_gpu_index = 0;
vk::GetPhysicalDeviceProperties(gpu_, &physDevProps_);
// See if there are any higher priority devices found
for (size_t i = 1; i < phys_devices.size(); ++i) {
VkPhysicalDeviceProperties tmp_props;
vk::GetPhysicalDeviceProperties(phys_devices[i], &tmp_props);
if (device_type_rank[tmp_props.deviceType] > device_type_rank[physDevProps_.deviceType]) {
physDevProps_ = tmp_props;
gpu_ = phys_devices[i];
m_gpu_index = i;
}
}
}
debug_reporter_.Create(instance_);
if (print_driver_info && !driver_printed) {
auto driver_properties = LvlInitStruct<VkPhysicalDeviceDriverProperties>();
auto physical_device_properties2 = LvlInitStruct<VkPhysicalDeviceProperties2>(&driver_properties);
vk::GetPhysicalDeviceProperties2(gpu_, &physical_device_properties2);
printf("Driver Name = %s\n", driver_properties.driverName);
printf("Driver Info = %s\n", driver_properties.driverInfo);
driver_printed = true;
}
for (const auto &ext : m_required_extensions) {
AddRequestedDeviceExtensions(ext);
}
for (const auto &ext : m_optional_extensions) {
AddRequestedDeviceExtensions(ext);
}
}
void VkRenderFramework::AddRequiredExtensions(const char *ext_name) {
m_required_extensions.push_back(ext_name);
AddRequestedInstanceExtensions(ext_name);
}
void VkRenderFramework::AddOptionalExtensions(const char *ext_name) {
m_optional_extensions.push_back(ext_name);
AddRequestedInstanceExtensions(ext_name);
}
bool VkRenderFramework::IsExtensionsEnabled(const char *ext_name) const {
return (CanEnableDeviceExtension(ext_name) || CanEnableInstanceExtension(ext_name));
}
bool VkRenderFramework::AreRequiredExtensionsEnabled() const {
for (const auto &ext : m_required_extensions) {
// `ext` may refer to an instance or device extension
if (!CanEnableDeviceExtension(ext) && !CanEnableInstanceExtension(ext)) {
return false;
}
}
return true;
}
std::string VkRenderFramework::RequiredExtensionsNotSupported() const {
std::stringstream ss;
bool first = true;
for (const auto &ext : m_required_extensions) {
if (!CanEnableDeviceExtension(ext) && !CanEnableInstanceExtension(ext)) {
if (first) {
first = false;
} else {
ss << ", ";
}
ss << ext;
}
}
return ss.str();
}
bool VkRenderFramework::AddRequestedInstanceExtensions(const char *ext_name) {
if (CanEnableInstanceExtension(ext_name)) {
return true;
}
const auto &instance_exts_map = InstanceExtensions::get_info_map();
bool is_instance_ext = false;
if (instance_exts_map.count(ext_name) > 0) {
if (!InstanceExtensionSupported(ext_name)) {
return false;
} else {
is_instance_ext = true;
}
}
// Different tables need to be used for extension dependency lookup depending on whether `ext_name` refers to a device or
// instance extension
if (is_instance_ext) {
const auto &info = InstanceExtensions::get_info(ext_name);
for (const auto &req : info.requirements) {
if (0 == strncmp(req.name, "VK_VERSION", 10)) {
continue;
}
if (!AddRequestedInstanceExtensions(req.name)) {
return false;
}
}
m_instance_extension_names.push_back(ext_name);
} else {
const auto &info = DeviceExtensions::get_info(ext_name);
for (const auto &req : info.requirements) {
if (!AddRequestedInstanceExtensions(req.name)) {
return false;
}
}
}
return true;
}
bool VkRenderFramework::CanEnableInstanceExtension(const std::string &inst_ext_name) const {
return std::any_of(m_instance_extension_names.cbegin(), m_instance_extension_names.cend(),
[&inst_ext_name](const char *ext) { return inst_ext_name == ext; });
}
bool VkRenderFramework::AddRequestedDeviceExtensions(const char *dev_ext_name) {
// Check if the extension has already been added
if (CanEnableDeviceExtension(dev_ext_name)) {
return true;
}
// If this is an instance extension, just return true under the assumption instance extensions do not depend on any device
// extensions.
const auto &instance_exts_map = InstanceExtensions::get_info_map();
if (instance_exts_map.count(dev_ext_name) != 0) {
return true;
}
if (!DeviceExtensionSupported(gpu(), nullptr, dev_ext_name)) {
return false;
}
m_device_extension_names.push_back(dev_ext_name);
const auto &info = DeviceExtensions::get_info(dev_ext_name);
for (const auto &req : info.requirements) {
if (!AddRequestedDeviceExtensions(req.name)) {
return false;
}
}
return true;
}
bool VkRenderFramework::CanEnableDeviceExtension(const std::string &dev_ext_name) const {
return std::any_of(m_device_extension_names.cbegin(), m_device_extension_names.cend(),
[&dev_ext_name](const char *ext) { return dev_ext_name == ext; });
}
void VkRenderFramework::ShutdownFramework() {
// Nothing to shut down without a VkInstance
if (!instance_) return;
if (m_device && m_device->device() != VK_NULL_HANDLE) {
vk::DeviceWaitIdle(device());
}
delete m_commandBuffer;
m_commandBuffer = nullptr;
delete m_commandPool;
m_commandPool = nullptr;
if (m_framebuffer) vk::DestroyFramebuffer(device(), m_framebuffer, NULL);
m_framebuffer = VK_NULL_HANDLE;
if (m_renderPass) vk::DestroyRenderPass(device(), m_renderPass, NULL);
m_renderPass = VK_NULL_HANDLE;
m_renderTargets.clear();
delete m_depthStencil;
m_depthStencil = nullptr;
DestroySwapchain();
// reset the driver
delete m_device;
m_device = nullptr;
debug_reporter_.Destroy(instance_);
if (m_surface != VK_NULL_HANDLE) {
vk::DestroySurfaceKHR(instance_, m_surface, nullptr);
m_surface = VK_NULL_HANDLE;
}
vk::DestroyInstance(instance_, nullptr);
instance_ = NULL; // In case we want to re-initialize
}
ErrorMonitor &VkRenderFramework::Monitor() { return debug_reporter_.error_monitor_; }
void VkRenderFramework::GetPhysicalDeviceFeatures(VkPhysicalDeviceFeatures *features) {
vk::GetPhysicalDeviceFeatures(gpu(), features);
}
// static
bool VkRenderFramework::IgnoreDisableChecks() {
static const bool skip_disable_checks = GetEnvironment("VK_LAYER_TESTS_IGNORE_DISABLE_CHECKS") != "";
return skip_disable_checks;
}
bool VkRenderFramework::IsPlatform(PlatformType platform) {
if (VkRenderFramework::IgnoreDisableChecks()) {
return false;
} else {
const auto search = vk_gpu_table.find(platform);
if (search != vk_gpu_table.end()) {
return 0 == search->second.compare(physDevProps().deviceName);
}
return false;
}
}
void VkRenderFramework::GetPhysicalDeviceProperties(VkPhysicalDeviceProperties *props) { *props = physDevProps_; }
void VkRenderFramework::InitState(VkPhysicalDeviceFeatures *features, void *create_device_pnext,
const VkCommandPoolCreateFlags flags) {
const auto ExtensionNotSupportedWithReporting = [this](const char *extension) {
if (DeviceExtensionSupported(extension))
return false;
else {
ADD_FAILURE() << "InitState(): Requested device extension \"" << extension
<< "\" is not supported. It will be disabled.";
return true;
}
};
RemoveIf(m_device_extension_names, ExtensionNotSupportedWithReporting);
m_device = new VkDeviceObj(0, gpu_, m_device_extension_names, features, create_device_pnext);
m_device->SetDeviceQueue();
m_depthStencil = new VkDepthStencilObj(m_device);
m_render_target_fmt = VkTestFramework::GetFormat(instance_, m_device);
m_lineWidth = 1.0f;
m_depthBiasConstantFactor = 0.0f;
m_depthBiasClamp = 0.0f;
m_depthBiasSlopeFactor = 0.0f;
m_blendConstants[0] = 1.0f;
m_blendConstants[1] = 1.0f;
m_blendConstants[2] = 1.0f;
m_blendConstants[3] = 1.0f;
m_minDepthBounds = 0.f;
m_maxDepthBounds = 1.f;
m_compareMask = 0xff;
m_writeMask = 0xff;
m_reference = 0;
m_commandPool = new VkCommandPoolObj(m_device, m_device->graphics_queue_node_index_, flags);
m_commandBuffer = new VkCommandBufferObj(m_device, m_commandPool);
}
void VkRenderFramework::InitViewport(float width, float height) {
VkViewport viewport;
VkRect2D scissor;
viewport.x = 0;
viewport.y = 0;
viewport.width = 1.f * width;
viewport.height = 1.f * height;
viewport.minDepth = 0.f;
viewport.maxDepth = 1.f;
m_viewports.push_back(viewport);
scissor.extent.width = (int32_t)width;
scissor.extent.height = (int32_t)height;
scissor.offset.x = 0;
scissor.offset.y = 0;
m_scissors.push_back(scissor);
m_width = width;
m_height = height;
}
void VkRenderFramework::InitViewport() { InitViewport(m_width, m_height); }
bool VkRenderFramework::InitSurface() { return InitSurface(m_surface); }
#ifdef VK_USE_PLATFORM_WIN32_KHR
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
#endif // VK_USE_PLATFORM_WIN32_KHR
bool VkRenderFramework::InitSurface(VkSurfaceKHR &surface) {
#if defined(VK_USE_PLATFORM_WIN32_KHR)
HINSTANCE window_instance = GetModuleHandle(nullptr);
const char class_name[] = "test";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = window_instance;
wc.lpszClassName = class_name;
RegisterClass(&wc);
HWND window = CreateWindowEx(0, class_name, 0, 0, 0, 0, (int)m_width, (int)m_height, NULL, NULL, window_instance, NULL);
ShowWindow(window, SW_HIDE);
VkWin32SurfaceCreateInfoKHR surface_create_info = LvlInitStruct<VkWin32SurfaceCreateInfoKHR>();
surface_create_info.hinstance = window_instance;
surface_create_info.hwnd = window;
VkResult err = vk::CreateWin32SurfaceKHR(instance(), &surface_create_info, nullptr, &surface);
// NOTE: Currently InitSurface can leak a WIN32 handle if called multiple times.
// This is intentional. Each swapchain/surface combo needs a unique HWND.
m_win32Window = window;
if (err != VK_SUCCESS) return false;
#endif
#if defined(VK_USE_PLATFORM_ANDROID_KHR) && defined(VALIDATION_APK)
VkAndroidSurfaceCreateInfoKHR surface_create_info = LvlInitStruct<VkAndroidSurfaceCreateInfoKHR>();
surface_create_info.window = VkTestFramework::window;
VkResult err = vk::CreateAndroidSurfaceKHR(instance(), &surface_create_info, nullptr, &surface);
if (err != VK_SUCCESS) return false;
#endif
#if defined(VK_USE_PLATFORM_XLIB_KHR)
m_surface_dpy = XOpenDisplay(NULL);
if (m_surface_dpy) {
int s = DefaultScreen(m_surface_dpy);
m_surface_window = XCreateSimpleWindow(m_surface_dpy, RootWindow(m_surface_dpy, s), 0, 0, (int)m_width, (int)m_height, 1,
BlackPixel(m_surface_dpy, s), WhitePixel(m_surface_dpy, s));
VkXlibSurfaceCreateInfoKHR surface_create_info = LvlInitStruct<VkXlibSurfaceCreateInfoKHR>();
surface_create_info.dpy = m_surface_dpy;
surface_create_info.window = m_surface_window;
VkResult err = vk::CreateXlibSurfaceKHR(instance(), &surface_create_info, nullptr, &surface);
if (err != VK_SUCCESS) return false;
}
#endif
#if defined(VK_USE_PLATFORM_XCB_KHR)
if (surface == VK_NULL_HANDLE) {
m_surface_xcb_conn = xcb_connect(NULL, NULL);
if (m_surface_xcb_conn) {
xcb_window_t window = xcb_generate_id(m_surface_xcb_conn);
VkXcbSurfaceCreateInfoKHR surface_create_info = LvlInitStruct<VkXcbSurfaceCreateInfoKHR>();
surface_create_info.connection = m_surface_xcb_conn;
surface_create_info.window = window;
VkResult err = vk::CreateXcbSurfaceKHR(instance(), &surface_create_info, nullptr, &surface);
if (err != VK_SUCCESS) return false;
}
}
#endif
return (surface != VK_NULL_HANDLE);
}
// Makes query to get information about swapchain needed to create a valid swapchain object each test creating a swapchain will need
void VkRenderFramework::InitSwapchainInfo() {
const VkPhysicalDevice physicalDevice = gpu();
assert(m_surface != VK_NULL_HANDLE);
vk::GetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, m_surface, &m_surface_capabilities);
uint32_t format_count;
vk::GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, m_surface, &format_count, nullptr);
if (format_count != 0) {
m_surface_formats.resize(format_count);
vk::GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, m_surface, &format_count, m_surface_formats.data());
}
uint32_t present_mode_count;
vk::GetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, m_surface, &present_mode_count, nullptr);
if (present_mode_count != 0) {
m_surface_present_modes.resize(present_mode_count);
vk::GetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, m_surface, &present_mode_count, m_surface_present_modes.data());
// Shared Present mode has different requirements most tests won't actually want
// Implementation required to support a non-shared present mode
for (size_t i = 0; i < m_surface_present_modes.size(); i++) {
const VkPresentModeKHR present_mode = m_surface_present_modes[i];
if ((present_mode != VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR) &&
(present_mode != VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR)) {
m_surface_non_shared_present_mode = present_mode;
break;
}
}
}
#ifdef VK_USE_PLATFORM_ANDROID_KHR
m_surface_composite_alpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
#else
m_surface_composite_alpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
#endif
}
bool VkRenderFramework::InitSwapchain(VkImageUsageFlags imageUsage, VkSurfaceTransformFlagBitsKHR preTransform) {
if (InitSurface()) {
return InitSwapchain(m_surface, imageUsage, preTransform);
}
return false;
}
bool VkRenderFramework::InitSwapchain(VkSurfaceKHR &surface, VkImageUsageFlags imageUsage,
VkSurfaceTransformFlagBitsKHR preTransform) {
return InitSwapchain(surface, imageUsage, preTransform, m_swapchain);
}
bool VkRenderFramework::InitSwapchain(VkSurfaceKHR &surface, VkImageUsageFlags imageUsage,
VkSurfaceTransformFlagBitsKHR preTransform, VkSwapchainKHR &swapchain,
VkSwapchainKHR oldSwapchain) {
VkBool32 supported;
vk::GetPhysicalDeviceSurfaceSupportKHR(gpu(), m_device->graphics_queue_node_index_, surface, &supported);
if (!supported) {
// Graphics queue does not support present
return false;
}
InitSwapchainInfo();
VkSwapchainCreateInfoKHR swapchain_create_info = LvlInitStruct<VkSwapchainCreateInfoKHR>();
swapchain_create_info.surface = surface;
swapchain_create_info.minImageCount = m_surface_capabilities.minImageCount;
swapchain_create_info.imageFormat = m_surface_formats[0].format;
swapchain_create_info.imageColorSpace = m_surface_formats[0].colorSpace;
swapchain_create_info.imageExtent = {m_surface_capabilities.minImageExtent.width, m_surface_capabilities.minImageExtent.height};
swapchain_create_info.imageArrayLayers = 1;
swapchain_create_info.imageUsage = imageUsage;
swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_create_info.preTransform = preTransform;
swapchain_create_info.compositeAlpha = m_surface_composite_alpha;
swapchain_create_info.presentMode = m_surface_non_shared_present_mode;
swapchain_create_info.clipped = VK_FALSE;
swapchain_create_info.oldSwapchain = oldSwapchain;
VkResult err = vk::CreateSwapchainKHR(device(), &swapchain_create_info, nullptr, &swapchain);
if (err != VK_SUCCESS) {
return false;
}
uint32_t imageCount = 0;
vk::GetSwapchainImagesKHR(device(), swapchain, &imageCount, nullptr);
vector<VkImage> swapchainImages;
swapchainImages.resize(imageCount);
vk::GetSwapchainImagesKHR(device(), swapchain, &imageCount, swapchainImages.data());
return true;
}
#if defined(VK_USE_PLATFORM_XLIB_KHR)
int IgnoreXErrors(Display *, XErrorEvent *) { return 0; }
#endif
void VkRenderFramework::DestroySwapchain() {
if (m_device && m_device->device() != VK_NULL_HANDLE) {
vk::DeviceWaitIdle(device());
if (m_swapchain != VK_NULL_HANDLE) {
vk::DestroySwapchainKHR(device(), m_swapchain, nullptr);
m_swapchain = VK_NULL_HANDLE;
}
}
#if defined(VK_USE_PLATFORM_WIN32_KHR)
if (m_win32Window != nullptr) {
DestroyWindow(m_win32Window);
}
#endif
#if defined(VK_USE_PLATFORM_XLIB_KHR)
if (m_surface_dpy != nullptr) {
// Ignore BadDrawable errors we seem to get during shutdown.
// The default error handler will exit() and end the test suite.
XSetErrorHandler(IgnoreXErrors);
XDestroyWindow(m_surface_dpy, m_surface_window);
m_surface_window = None;
XCloseDisplay(m_surface_dpy);
m_surface_dpy = nullptr;
XSetErrorHandler(nullptr);
}
#endif
#if defined(VK_USE_PLATFORM_XCB_KHR)
if (m_surface_xcb_conn != nullptr) {
xcb_disconnect(m_surface_xcb_conn);
m_surface_xcb_conn = nullptr;
}
#endif
if (m_surface != VK_NULL_HANDLE) {
vk::DestroySurfaceKHR(instance(), m_surface, nullptr);
m_surface = VK_NULL_HANDLE;
}
}
void VkRenderFramework::InitRenderTarget() { InitRenderTarget(1); }
void VkRenderFramework::InitRenderTarget(uint32_t targets) { InitRenderTarget(targets, NULL); }
void VkRenderFramework::InitRenderTarget(VkImageView *dsBinding) { InitRenderTarget(1, dsBinding); }
void VkRenderFramework::InitRenderTarget(uint32_t targets, VkImageView *dsBinding) {
vector<VkAttachmentDescription> &attachments = m_renderPass_attachments;
vector<VkAttachmentReference> color_references;
vector<VkImageView> &bindings = m_framebuffer_attachments;
attachments.reserve(targets + 1); // +1 for dsBinding
color_references.reserve(targets);
bindings.reserve(targets + 1); // +1 for dsBinding
VkAttachmentDescription att = {};
att.format = m_render_target_fmt;
att.samples = VK_SAMPLE_COUNT_1_BIT;
att.loadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;
att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
att.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
att.initialLayout = (m_clear_via_load_op) ? VK_IMAGE_LAYOUT_UNDEFINED : VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
att.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkAttachmentReference ref = {};
ref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
m_renderPassClearValues.clear();
VkClearValue clear = {};
clear.color = m_clear_color;
for (uint32_t i = 0; i < targets; i++) {
attachments.push_back(att);
ref.attachment = i;
color_references.push_back(ref);
m_renderPassClearValues.push_back(clear);
std::unique_ptr<VkImageObj> img(new VkImageObj(m_device));
VkFormatProperties props;
vk::GetPhysicalDeviceFormatProperties(m_device->phy().handle(), m_render_target_fmt, &props);
if (props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
img->Init((uint32_t)m_width, (uint32_t)m_height, 1, m_render_target_fmt,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
VK_IMAGE_TILING_LINEAR);
} else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
img->Init((uint32_t)m_width, (uint32_t)m_height, 1, m_render_target_fmt,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
VK_IMAGE_TILING_OPTIMAL);
} else {
FAIL() << "Neither Linear nor Optimal allowed for render target";
}
bindings.push_back(img->targetView(m_render_target_fmt));
m_renderTargets.push_back(std::move(img));
}
m_renderPass_subpasses.clear();
m_renderPass_subpasses.resize(1);
VkSubpassDescription &subpass = m_renderPass_subpasses[0];
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.flags = 0;
subpass.inputAttachmentCount = 0;
subpass.pInputAttachments = NULL;
subpass.colorAttachmentCount = targets;
subpass.pColorAttachments = color_references.data();
subpass.pResolveAttachments = NULL;
VkAttachmentReference ds_reference;
if (dsBinding) {
att.format = m_depth_stencil_fmt;
att.loadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;
;
att.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
att.stencilLoadOp = (m_clear_via_load_op) ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;
att.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
att.initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
att.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
attachments.push_back(att);
clear.depthStencil.depth = m_depth_clear_color;
clear.depthStencil.stencil = m_stencil_clear_color;
m_renderPassClearValues.push_back(clear);
bindings.push_back(*dsBinding);
ds_reference.attachment = targets;
ds_reference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
subpass.pDepthStencilAttachment = &ds_reference;
} else {
subpass.pDepthStencilAttachment = NULL;
}
subpass.preserveAttachmentCount = 0;
subpass.pPreserveAttachments = NULL;
VkRenderPassCreateInfo &rp_info = m_renderPass_info;
rp_info = LvlInitStruct<VkRenderPassCreateInfo>();
rp_info.attachmentCount = attachments.size();
rp_info.pAttachments = attachments.data();
rp_info.subpassCount = m_renderPass_subpasses.size();
rp_info.pSubpasses = m_renderPass_subpasses.data();
m_renderPass_dependencies.clear();
if (m_addRenderPassSelfDependency) {
m_renderPass_dependencies.resize(1);
VkSubpassDependency &subpass_dep = m_renderPass_dependencies[0];
// Add a subpass self-dependency to subpass 0 of default renderPass
subpass_dep.srcSubpass = 0;
subpass_dep.dstSubpass = 0;
// Just using all framebuffer-space pipeline stages in order to get a reasonably large
// set of bits that can be used for both src & dst
subpass_dep.srcStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT |
VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
subpass_dep.dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT |
VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
// Add all of the gfx mem access bits that correlate to the fb-space pipeline stages
subpass_dep.srcAccessMask = VK_ACCESS_UNIFORM_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT |
VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
subpass_dep.dstAccessMask = VK_ACCESS_UNIFORM_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT |
VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
// Must include dep_by_region bit when src & dst both include framebuffer-space stages
subpass_dep.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
}
if (m_additionalSubpassDependencies.size()) {
m_renderPass_dependencies.reserve(m_additionalSubpassDependencies.size() + m_renderPass_dependencies.size());
m_renderPass_dependencies.insert(m_renderPass_dependencies.end(), m_additionalSubpassDependencies.begin(),
m_additionalSubpassDependencies.end());
}
if (m_renderPass_dependencies.size()) {
rp_info.dependencyCount = static_cast<uint32_t>(m_renderPass_dependencies.size());
rp_info.pDependencies = m_renderPass_dependencies.data();
} else {
rp_info.dependencyCount = 0;