forked from KhronosGroup/Vulkan-ValidationLayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
best_practices_utils.cpp
3802 lines (3246 loc) · 200 KB
/
best_practices_utils.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-2021 The Khronos Group Inc.
* Copyright (c) 2015-2021 Valve Corporation
* Copyright (c) 2015-2021 LunarG, Inc.
* Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
*
* 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: Camden Stocker <[email protected]>
* Author: Nadav Geva <[email protected]>
*/
#include "best_practices_validation.h"
#include "layer_chassis_dispatch.h"
#include "best_practices_error_enums.h"
#include "shader_validation.h"
#include "sync_utils.h"
#include "cmd_buffer_state.h"
#include "device_state.h"
#include "render_pass_state.h"
#include <string>
#include <bitset>
#include <memory>
struct VendorSpecificInfo {
EnableFlags vendor_id;
std::string name;
};
const std::map<BPVendorFlagBits, VendorSpecificInfo> kVendorInfo = {
{kBPVendorArm, {vendor_specific_arm, "Arm"}},
{kBPVendorAMD, {vendor_specific_amd, "AMD"}},
};
const SpecialUseVUIDs kSpecialUseInstanceVUIDs {
kVUID_BestPractices_CreateInstance_SpecialUseExtension_CADSupport,
kVUID_BestPractices_CreateInstance_SpecialUseExtension_D3DEmulation,
kVUID_BestPractices_CreateInstance_SpecialUseExtension_DevTools,
kVUID_BestPractices_CreateInstance_SpecialUseExtension_Debugging,
kVUID_BestPractices_CreateInstance_SpecialUseExtension_GLEmulation,
};
const SpecialUseVUIDs kSpecialUseDeviceVUIDs {
kVUID_BestPractices_CreateDevice_SpecialUseExtension_CADSupport,
kVUID_BestPractices_CreateDevice_SpecialUseExtension_D3DEmulation,
kVUID_BestPractices_CreateDevice_SpecialUseExtension_DevTools,
kVUID_BestPractices_CreateDevice_SpecialUseExtension_Debugging,
kVUID_BestPractices_CreateDevice_SpecialUseExtension_GLEmulation,
};
std::shared_ptr<CMD_BUFFER_STATE> BestPractices::CreateCmdBufferState(VkCommandBuffer cb,
const VkCommandBufferAllocateInfo* pCreateInfo,
const COMMAND_POOL_STATE* pool) {
return std::static_pointer_cast<CMD_BUFFER_STATE>(std::make_shared<CMD_BUFFER_STATE_BP>(this, cb, pCreateInfo, pool));
}
CMD_BUFFER_STATE_BP::CMD_BUFFER_STATE_BP(BestPractices* bp, VkCommandBuffer cb, const VkCommandBufferAllocateInfo* pCreateInfo,
const COMMAND_POOL_STATE* pool)
: CMD_BUFFER_STATE(bp, cb, pCreateInfo, pool) {}
bool BestPractices::VendorCheckEnabled(BPVendorFlags vendors) const {
for (const auto& vendor : kVendorInfo) {
if (vendors & vendor.first && enabled[vendor.second.vendor_id]) {
return true;
}
}
return false;
}
const char* VendorSpecificTag(BPVendorFlags vendors) {
// Cache built vendor tags in a map
static layer_data::unordered_map<BPVendorFlags, std::string> tag_map;
auto res = tag_map.find(vendors);
if (res == tag_map.end()) {
// Build the vendor tag string
std::stringstream vendor_tag;
vendor_tag << "[";
bool first_vendor = true;
for (const auto& vendor : kVendorInfo) {
if (vendors & vendor.first) {
if (!first_vendor) {
vendor_tag << ", ";
}
vendor_tag << vendor.second.name;
first_vendor = false;
}
}
vendor_tag << "]";
tag_map[vendors] = vendor_tag.str();
res = tag_map.find(vendors);
}
return res->second.c_str();
}
const char* DepReasonToString(ExtDeprecationReason reason) {
switch (reason) {
case kExtPromoted:
return "promoted to";
break;
case kExtObsoleted:
return "obsoleted by";
break;
case kExtDeprecated:
return "deprecated by";
break;
default:
return "";
break;
}
}
bool BestPractices::ValidateDeprecatedExtensions(const char* api_name, const char* extension_name, uint32_t version,
const char* vuid) const {
bool skip = false;
auto dep_info_it = deprecated_extensions.find(extension_name);
if (dep_info_it != deprecated_extensions.end()) {
auto dep_info = dep_info_it->second;
if (((dep_info.target.compare("VK_VERSION_1_1") == 0) && (version >= VK_API_VERSION_1_1)) ||
((dep_info.target.compare("VK_VERSION_1_2") == 0) && (version >= VK_API_VERSION_1_2))) {
skip |=
LogWarning(instance, vuid, "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.",
api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str());
} else if (dep_info.target.find("VK_VERSION") == std::string::npos) {
if (dep_info.target.length() == 0) {
skip |= LogWarning(instance, vuid,
"%s(): Attempting to enable deprecated extension %s, but this extension has been deprecated "
"without replacement.",
api_name, extension_name);
} else {
skip |= LogWarning(instance, vuid,
"%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.",
api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str());
}
}
}
return skip;
}
bool BestPractices::ValidateSpecialUseExtensions(const char* api_name, const char* extension_name, const SpecialUseVUIDs& special_use_vuids) const
{
bool skip = false;
auto dep_info_it = special_use_extensions.find(extension_name);
if (dep_info_it != special_use_extensions.end()) {
const char* const format = "%s(): Attempting to enable extension %s, but this extension is intended to support %s "
"and it is strongly recommended that it be otherwise avoided.";
auto& special_uses = dep_info_it->second;
if (special_uses.find("cadsupport") != std::string::npos) {
skip |= LogWarning(instance, special_use_vuids.cadsupport, format, api_name, extension_name,
"specialized functionality used by CAD/CAM applications");
}
if (special_uses.find("d3demulation") != std::string::npos) {
skip |= LogWarning(instance, special_use_vuids.d3demulation, format, api_name, extension_name,
"D3D emulation layers, and applications ported from D3D, by adding functionality specific to D3D");
}
if (special_uses.find("devtools") != std::string::npos) {
skip |= LogWarning(instance, special_use_vuids.devtools, format, api_name, extension_name,
"developer tools such as capture-replay libraries");
}
if (special_uses.find("debugging") != std::string::npos) {
skip |= LogWarning(instance, special_use_vuids.debugging, format, api_name, extension_name,
"use by applications when debugging");
}
if (special_uses.find("glemulation") != std::string::npos) {
skip |= LogWarning(instance, special_use_vuids.glemulation, format, api_name, extension_name,
"OpenGL and/or OpenGL ES emulation layers, and applications ported from those APIs, by adding functionality "
"specific to those APIs");
}
}
return skip;
}
void BestPractices::InitDeviceValidationObject(bool add_obj, ValidationObject* inst_obj, ValidationObject* dev_obj) {
if (add_obj) {
ValidationStateTracker::InitDeviceValidationObject(add_obj, inst_obj, dev_obj);
}
}
bool BestPractices::PreCallValidateCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
VkInstance* pInstance) const {
bool skip = false;
for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeviceExtensionNames)) {
skip |= LogWarning(instance, kVUID_BestPractices_CreateInstance_ExtensionMismatch,
"vkCreateInstance(): Attempting to enable Device Extension %s at CreateInstance time.",
pCreateInfo->ppEnabledExtensionNames[i]);
}
uint32_t specified_version =
(pCreateInfo->pApplicationInfo ? pCreateInfo->pApplicationInfo->apiVersion : VK_API_VERSION_1_0);
skip |= ValidateDeprecatedExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i], specified_version,
kVUID_BestPractices_CreateInstance_DeprecatedExtension);
skip |= ValidateSpecialUseExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i], kSpecialUseInstanceVUIDs);
}
return skip;
}
bool BestPractices::PreCallValidateCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) const {
bool skip = false;
// get API version of physical device passed when creating device.
VkPhysicalDeviceProperties physical_device_properties{};
DispatchGetPhysicalDeviceProperties(physicalDevice, &physical_device_properties);
auto device_api_version = physical_device_properties.apiVersion;
// check api versions and warn if instance api Version is higher than version on device.
if (api_version > device_api_version) {
std::string inst_api_name = StringAPIVersion(api_version);
std::string dev_api_name = StringAPIVersion(device_api_version);
skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_API_Mismatch,
"vkCreateDevice(): API Version of current instance, %s is higher than API Version on device, %s",
inst_api_name.c_str(), dev_api_name.c_str());
}
for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kInstanceExtensionNames)) {
skip |= LogWarning(instance, kVUID_BestPractices_CreateDevice_ExtensionMismatch,
"vkCreateDevice(): Attempting to enable Instance Extension %s at CreateDevice time.",
pCreateInfo->ppEnabledExtensionNames[i]);
}
skip |= ValidateDeprecatedExtensions("CreateDevice", pCreateInfo->ppEnabledExtensionNames[i], api_version,
kVUID_BestPractices_CreateDevice_DeprecatedExtension);
skip |= ValidateSpecialUseExtensions("CreateDevice", pCreateInfo->ppEnabledExtensionNames[i], kSpecialUseDeviceVUIDs);
}
const auto bp_pd_state = GetPhysicalDeviceState(physicalDevice);
if ((bp_pd_state->vkGetPhysicalDeviceFeaturesState == UNCALLED) && (pCreateInfo->pEnabledFeatures != NULL)) {
skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_PDFeaturesNotCalled,
"vkCreateDevice() called before getting physical device features from vkGetPhysicalDeviceFeatures().");
}
if ((VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorAMD)) && (pCreateInfo->pEnabledFeatures != nullptr) &&
(pCreateInfo->pEnabledFeatures->robustBufferAccess == VK_TRUE)) {
skip |= LogPerformanceWarning(
device, kVUID_BestPractices_CreateDevice_RobustBufferAccess,
"%s %s vkCreateDevice() called with enabled robustBufferAccess. Use robustBufferAccess as a debugging tool during "
"development. Enabling it causes loss in performance for accesses to uniform buffers and shader storage "
"buffers. Disable robustBufferAccess in release builds. Only leave it enabled if the application use-case "
"requires the additional level of reliability due to the use of unverified user-supplied draw parameters.",
VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorAMD));
}
return skip;
}
bool BestPractices::PreCallValidateCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer) const {
bool skip = false;
if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
std::stringstream buffer_hex;
buffer_hex << "0x" << std::hex << HandleToUint64(pBuffer);
skip |= LogWarning(
device, kVUID_BestPractices_SharingModeExclusive,
"Warning: Buffer (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
"(queueFamilyIndexCount of %" PRIu32 ").",
buffer_hex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
}
return skip;
}
bool BestPractices::PreCallValidateCreateImage(VkDevice device, const VkImageCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator, VkImage* pImage) const {
bool skip = false;
if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
std::stringstream image_hex;
image_hex << "0x" << std::hex << HandleToUint64(pImage);
skip |=
LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
"Warning: Image (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
"(queueFamilyIndexCount of %" PRIu32 ").",
image_hex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
}
if (VendorCheckEnabled(kBPVendorArm)) {
if (pCreateInfo->samples > kMaxEfficientSamplesArm) {
skip |= LogPerformanceWarning(
device, kVUID_BestPractices_CreateImage_TooLargeSampleCount,
"%s vkCreateImage(): Trying to create an image with %u samples. "
"The hardware revision may not have full throughput for framebuffers with more than %u samples.",
VendorSpecificTag(kBPVendorArm), static_cast<uint32_t>(pCreateInfo->samples), kMaxEfficientSamplesArm);
}
if (pCreateInfo->samples > VK_SAMPLE_COUNT_1_BIT && !(pCreateInfo->usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT)) {
skip |= LogPerformanceWarning(
device, kVUID_BestPractices_CreateImage_NonTransientMSImage,
"%s vkCreateImage(): Trying to create a multisampled image, but createInfo.usage did not have "
"VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. Multisampled images may be resolved on-chip, "
"and do not need to be backed by physical storage. "
"TRANSIENT_ATTACHMENT allows tiled GPUs to not back the multisampled image with physical memory.",
VendorSpecificTag(kBPVendorArm));
}
}
if (VendorCheckEnabled(kBPVendorAMD)) {
std::stringstream image_hex;
image_hex << "0x" << std::hex << HandleToUint64(pImage);
if ((pCreateInfo->usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
(pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT)) {
skip |= LogPerformanceWarning(device,
kVUID_BestPractices_vkImage_AvoidConcurrentRenderTargets,
"%s Performance warning: image (%s) is created as a render target with VK_SHARING_MODE_CONCURRENT. "
"Using a SHARING_MODE_CONCURRENT "
"is not recommended with color and depth targets",
VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
}
if ((pCreateInfo->usage &
(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
(pCreateInfo->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) {
skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_DontUseMutableRenderTargets,
"%s Performance warning: image (%s) is created as a render target with VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT. "
"Using a MUTABLE_FORMAT is not recommended with color, depth, and storage targets",
VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
}
if ((pCreateInfo->usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
(pCreateInfo->usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_DontUseStorageRenderTargets,
"%s Performance warning: image (%s) is created as a render target with VK_IMAGE_USAGE_STORAGE_BIT. Using a "
"VK_IMAGE_USAGE_STORAGE_BIT is not recommended with color and depth targets",
VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
}
}
return skip;
}
void BestPractices::PreCallRecordDestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator) {
ValidationStateTracker::PreCallRecordDestroyImage(device, image, pAllocator);
ReleaseImageUsageState(image);
}
void BestPractices::PreCallRecordDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks* pAllocator) {
if (VK_NULL_HANDLE != swapchain) {
auto chain = Get<SWAPCHAIN_NODE>(swapchain);
for (auto& image : chain->images) {
if (image.image_state) {
ReleaseImageUsageState(image.image_state->image());
}
}
}
ValidationStateTracker::PreCallRecordDestroySwapchainKHR(device, swapchain, pAllocator);
}
IMAGE_STATE_BP* BestPractices::GetImageUsageState(VkImage vk_image) {
auto itr = imageUsageMap.find(vk_image);
if (itr != imageUsageMap.end()) {
return &itr->second;
} else {
auto& state = imageUsageMap[vk_image];
auto image = Get<IMAGE_STATE>(vk_image);
state.image = image.get();
state.usages.resize(image->createInfo.arrayLayers);
for (auto& mips : state.usages) {
mips.resize(image->createInfo.mipLevels, IMAGE_SUBRESOURCE_USAGE_BP::UNDEFINED);
}
return &state;
}
}
void BestPractices::ReleaseImageUsageState(VkImage image) {
auto itr = imageUsageMap.find(image);
if (itr != imageUsageMap.end()) {
imageUsageMap.erase(itr);
}
}
bool BestPractices::PreCallValidateCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo,
const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) const {
bool skip = false;
const auto* bp_pd_state = GetPhysicalDeviceState();
if (bp_pd_state) {
if (bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState == UNCALLED) {
skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
"vkCreateSwapchainKHR() called before getting surface capabilities from "
"vkGetPhysicalDeviceSurfaceCapabilitiesKHR().");
}
if ((pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR) &&
(bp_pd_state->vkGetPhysicalDeviceSurfacePresentModesKHRState != QUERY_DETAILS)) {
skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
"vkCreateSwapchainKHR() called before getting surface present mode(s) from "
"vkGetPhysicalDeviceSurfacePresentModesKHR().");
}
if (bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState != QUERY_DETAILS) {
skip |= LogWarning(
device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
"vkCreateSwapchainKHR() called before getting surface format(s) from vkGetPhysicalDeviceSurfaceFormatsKHR().");
}
}
if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
skip |=
LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
"Warning: A Swapchain is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while "
"specifying multiple queues (queueFamilyIndexCount of %" PRIu32 ").",
pCreateInfo->queueFamilyIndexCount);
}
if (pCreateInfo->minImageCount == 2) {
skip |= LogPerformanceWarning(
device, kVUID_BestPractices_SuboptimalSwapchainImageCount,
"Warning: A Swapchain is being created with minImageCount set to %" PRIu32
", which means double buffering is going "
"to be used. Using double buffering and vsync locks rendering to an integer fraction of the vsync rate. In turn, "
"reducing the performance of the application if rendering is slower than vsync. Consider setting minImageCount to "
"3 to use triple buffering to maximize performance in such cases.",
pCreateInfo->minImageCount);
}
if (VendorCheckEnabled(kBPVendorArm) && (pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR)) {
skip |= LogWarning(device, kVUID_BestPractices_CreateSwapchain_PresentMode,
"%s Warning: Swapchain is not being created with presentation mode \"VK_PRESENT_MODE_FIFO_KHR\". "
"Prefer using \"VK_PRESENT_MODE_FIFO_KHR\" to avoid unnecessary CPU and GPU load and save power. "
"Presentation modes which are not FIFO will present the latest available frame and discard other "
"frame(s) if any.",
VendorSpecificTag(kBPVendorArm));
}
return skip;
}
bool BestPractices::PreCallValidateCreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount,
const VkSwapchainCreateInfoKHR* pCreateInfos,
const VkAllocationCallbacks* pAllocator,
VkSwapchainKHR* pSwapchains) const {
bool skip = false;
for (uint32_t i = 0; i < swapchainCount; i++) {
if ((pCreateInfos[i].queueFamilyIndexCount > 1) && (pCreateInfos[i].imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
skip |= LogWarning(
device, kVUID_BestPractices_SharingModeExclusive,
"Warning: A shared swapchain (index %" PRIu32
") is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple "
"queues (queueFamilyIndexCount of %" PRIu32 ").",
i, pCreateInfos[i].queueFamilyIndexCount);
}
}
return skip;
}
bool BestPractices::PreCallValidateCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass) const {
bool skip = false;
for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
VkFormat format = pCreateInfo->pAttachments[i].format;
if (pCreateInfo->pAttachments[i].initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
if ((FormatIsColor(format) || FormatHasDepth(format)) &&
pCreateInfo->pAttachments[i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
"Render pass has an attachment with loadOp == VK_ATTACHMENT_LOAD_OP_LOAD and "
"initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
"intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
"image truely is undefined at the start of the render pass.");
}
if (FormatHasStencil(format) && pCreateInfo->pAttachments[i].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
"Render pass has an attachment with stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD "
"and initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
"intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
"image truely is undefined at the start of the render pass.");
}
}
const auto& attachment = pCreateInfo->pAttachments[i];
if (attachment.samples > VK_SAMPLE_COUNT_1_BIT) {
bool access_requires_memory =
attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE;
if (FormatHasStencil(format)) {
access_requires_memory |= attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE;
}
if (access_requires_memory) {
skip |= LogPerformanceWarning(
device, kVUID_BestPractices_CreateRenderPass_ImageRequiresMemory,
"Attachment %u in the VkRenderPass is a multisampled image with %u samples, but it uses loadOp/storeOp "
"which requires accessing data from memory. Multisampled images should always be loadOp = CLEAR or DONT_CARE, "
"storeOp = DONT_CARE. This allows the implementation to use lazily allocated memory effectively.",
i, static_cast<uint32_t>(attachment.samples));
}
}
}
for (uint32_t dependency = 0; dependency < pCreateInfo->dependencyCount; dependency++) {
skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].srcStageMask);
skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].dstStageMask);
}
return skip;
}
bool BestPractices::ValidateAttachments(const VkRenderPassCreateInfo2* rpci, uint32_t attachmentCount,
const VkImageView* image_views) const {
bool skip = false;
// Check for non-transient attachments that should be transient and vice versa
for (uint32_t i = 0; i < attachmentCount; ++i) {
const auto& attachment = rpci->pAttachments[i];
bool attachment_should_be_transient =
(attachment.loadOp != VK_ATTACHMENT_LOAD_OP_LOAD && attachment.storeOp != VK_ATTACHMENT_STORE_OP_STORE);
if (FormatHasStencil(attachment.format)) {
attachment_should_be_transient &= (attachment.stencilLoadOp != VK_ATTACHMENT_LOAD_OP_LOAD &&
attachment.stencilStoreOp != VK_ATTACHMENT_STORE_OP_STORE);
}
auto view_state = Get<IMAGE_VIEW_STATE>(image_views[i]);
if (view_state) {
const auto& ici = view_state->image_state->createInfo;
bool image_is_transient = (ici.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) != 0;
// The check for an image that should not be transient applies to all GPUs
if (!attachment_should_be_transient && image_is_transient) {
skip |= LogPerformanceWarning(
device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldNotBeTransient,
"Attachment %u in VkFramebuffer uses loadOp/storeOps which need to access physical memory, "
"but the image backing the image view has VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
"Physical memory will need to be backed lazily to this image, potentially causing stalls.",
i);
}
bool supports_lazy = false;
for (uint32_t j = 0; j < phys_dev_mem_props.memoryTypeCount; j++) {
if (phys_dev_mem_props.memoryTypes[j].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
supports_lazy = true;
}
}
// The check for an image that should be transient only applies to GPUs supporting
// lazily allocated memory
if (supports_lazy && attachment_should_be_transient && !image_is_transient) {
skip |= LogPerformanceWarning(
device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldBeTransient,
"Attachment %u in VkFramebuffer uses loadOp/storeOps which never have to be backed by physical memory, "
"but the image backing the image view does not have VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
"You can save physical memory by using transient attachment backed by lazily allocated memory here.",
i);
}
}
}
return skip;
}
bool BestPractices::PreCallValidateCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator, VkFramebuffer* pFramebuffer) const {
bool skip = false;
auto rp_state = Get<RENDER_PASS_STATE>(pCreateInfo->renderPass);
if (rp_state && !(pCreateInfo->flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT)) {
skip = ValidateAttachments(rp_state->createInfo.ptr(), pCreateInfo->attachmentCount, pCreateInfo->pAttachments);
}
return skip;
}
bool BestPractices::PreCallValidateAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
VkDescriptorSet* pDescriptorSets, void* ads_state_data) const {
bool skip = false;
skip |= ValidationStateTracker::PreCallValidateAllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets, ads_state_data);
if (!skip) {
const auto& pool_handle = pAllocateInfo->descriptorPool;
auto iter = descriptor_pool_freed_count.find(pool_handle);
// if the number of freed sets > 0, it implies they could be recycled instead if desirable
// this warning is specific to Arm
if (VendorCheckEnabled(kBPVendorArm) && iter != descriptor_pool_freed_count.end() && iter->second > 0) {
skip |= LogPerformanceWarning(
device, kVUID_BestPractices_AllocateDescriptorSets_SuboptimalReuse,
"%s Descriptor set memory was allocated via vkAllocateDescriptorSets() for sets which were previously freed in the "
"same logical device. On some drivers or architectures it may be most optimal to re-use existing descriptor sets.",
VendorSpecificTag(kBPVendorArm));
}
}
return skip;
}
void BestPractices::ManualPostCallRecordAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
VkDescriptorSet* pDescriptorSets, VkResult result, void* ads_state) {
if (result == VK_SUCCESS) {
// find the free count for the pool we allocated into
auto iter = descriptor_pool_freed_count.find(pAllocateInfo->descriptorPool);
if (iter != descriptor_pool_freed_count.end()) {
// we record successful allocations by subtracting the allocation count from the last recorded free count
const auto alloc_count = pAllocateInfo->descriptorSetCount;
// clamp the unsigned subtraction to the range [0, last_free_count]
if (iter->second > alloc_count) {
iter->second -= alloc_count;
} else {
iter->second = 0;
}
}
}
}
void BestPractices::PostCallRecordFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
const VkDescriptorSet* pDescriptorSets, VkResult result) {
ValidationStateTracker::PostCallRecordFreeDescriptorSets(device, descriptorPool, descriptorSetCount, pDescriptorSets, result);
if (result == VK_SUCCESS) {
// we want to track frees because we're interested in suggesting re-use
auto iter = descriptor_pool_freed_count.find(descriptorPool);
if (iter == descriptor_pool_freed_count.end()) {
descriptor_pool_freed_count.emplace(descriptorPool, descriptorSetCount);
} else {
iter->second += descriptorSetCount;
}
}
}
bool BestPractices::PreCallValidateAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) const {
bool skip = false;
if (num_mem_objects + 1 > kMemoryObjectWarningLimit) {
skip |= LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_TooManyObjects,
"Performance Warning: This app has > %" PRIu32 " memory objects.", kMemoryObjectWarningLimit);
}
if (pAllocateInfo->allocationSize < kMinDeviceAllocationSize) {
skip |= LogPerformanceWarning(
device, kVUID_BestPractices_AllocateMemory_SmallAllocation,
"vkAllocateMemory(): Allocating a VkDeviceMemory of size %" PRIu64 ". This is a very small allocation (current "
"threshold is %" PRIu64 " bytes). "
"You should make large allocations and sub-allocate from one large VkDeviceMemory.",
pAllocateInfo->allocationSize, kMinDeviceAllocationSize);
}
// TODO: Insert get check for GetPhysicalDeviceMemoryProperties once the state is tracked in the StateTracker
return skip;
}
void BestPractices::ManualPostCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory,
VkResult result) {
if (result != VK_SUCCESS) {
static std::vector<VkResult> error_codes = {VK_ERROR_OUT_OF_HOST_MEMORY, VK_ERROR_OUT_OF_DEVICE_MEMORY,
VK_ERROR_TOO_MANY_OBJECTS, VK_ERROR_INVALID_EXTERNAL_HANDLE,
VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS};
static std::vector<VkResult> success_codes = {};
ValidateReturnCodes("vkAllocateMemory", result, error_codes, success_codes);
return;
}
num_mem_objects++;
}
void BestPractices::ValidateReturnCodes(const char* api_name, VkResult result, const std::vector<VkResult>& error_codes,
const std::vector<VkResult>& success_codes) const {
auto error = std::find(error_codes.begin(), error_codes.end(), result);
if (error != error_codes.end()) {
static const std::vector<VkResult> common_failure_codes = {VK_ERROR_OUT_OF_DATE_KHR,
VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT};
auto common_failure = std::find(common_failure_codes.begin(), common_failure_codes.end(), result);
if (common_failure != common_failure_codes.end()) {
LogInfo(instance, kVUID_BestPractices_Failure_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
} else {
LogWarning(instance, kVUID_BestPractices_Error_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
}
return;
}
auto success = std::find(success_codes.begin(), success_codes.end(), result);
if (success != success_codes.end()) {
LogInfo(instance, kVUID_BestPractices_NonSuccess_Result, "%s(): Returned non-success return code %s.", api_name,
string_VkResult(result));
}
}
bool BestPractices::PreCallValidateFreeMemory(VkDevice device, VkDeviceMemory memory,
const VkAllocationCallbacks* pAllocator) const {
if (memory == VK_NULL_HANDLE) return false;
bool skip = false;
const auto mem_info = Get<DEVICE_MEMORY_STATE>(memory);
for (const auto& node: mem_info->ObjectBindings()) {
const auto& obj = node->Handle();
LogObjectList objlist(device);
objlist.add(obj);
objlist.add(mem_info->mem());
skip |= LogWarning(objlist, layer_name.c_str(), "VK Object %s still has a reference to mem obj %s.",
report_data->FormatHandle(obj).c_str(), report_data->FormatHandle(mem_info->mem()).c_str());
}
return skip;
}
void BestPractices::PreCallRecordFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* pAllocator) {
ValidationStateTracker::PreCallRecordFreeMemory(device, memory, pAllocator);
if (memory != VK_NULL_HANDLE) {
num_mem_objects--;
}
}
bool BestPractices::ValidateBindBufferMemory(VkBuffer buffer, VkDeviceMemory memory, const char* api_name) const {
bool skip = false;
const auto buffer_state = Get<BUFFER_STATE>(buffer);
if (!buffer_state->memory_requirements_checked && !buffer_state->external_memory_handle) {
skip |= LogWarning(device, kVUID_BestPractices_BufferMemReqNotCalled,
"%s: Binding memory to %s but vkGetBufferMemoryRequirements() has not been called on that buffer.",
api_name, report_data->FormatHandle(buffer).c_str());
}
const auto mem_state = Get<DEVICE_MEMORY_STATE>(memory);
if (mem_state && mem_state->alloc_info.allocationSize == buffer_state->createInfo.size &&
mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
skip |= LogPerformanceWarning(
device, kVUID_BestPractices_SmallDedicatedAllocation,
"%s: Trying to bind %s to a memory block which is fully consumed by the buffer. "
"The required size of the allocation is %" PRIu64 ", but smaller buffers like this should be sub-allocated from "
"larger memory blocks. (Current threshold is %" PRIu64 " bytes.)",
api_name, report_data->FormatHandle(buffer).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
}
return skip;
}
bool BestPractices::PreCallValidateBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory,
VkDeviceSize memoryOffset) const {
bool skip = false;
const char* api_name = "BindBufferMemory()";
skip |= ValidateBindBufferMemory(buffer, memory, api_name);
return skip;
}
bool BestPractices::PreCallValidateBindBufferMemory2(VkDevice device, uint32_t bindInfoCount,
const VkBindBufferMemoryInfo* pBindInfos) const {
char api_name[64];
bool skip = false;
for (uint32_t i = 0; i < bindInfoCount; i++) {
sprintf(api_name, "vkBindBufferMemory2() pBindInfos[%u]", i);
skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
}
return skip;
}
bool BestPractices::PreCallValidateBindBufferMemory2KHR(VkDevice device, uint32_t bindInfoCount,
const VkBindBufferMemoryInfo* pBindInfos) const {
char api_name[64];
bool skip = false;
for (uint32_t i = 0; i < bindInfoCount; i++) {
sprintf(api_name, "vkBindBufferMemory2KHR() pBindInfos[%u]", i);
skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
}
return skip;
}
bool BestPractices::ValidateBindImageMemory(VkImage image, VkDeviceMemory memory, const char* api_name) const {
bool skip = false;
const auto image_state = Get<IMAGE_STATE>(image);
if (image_state->disjoint == false) {
if (!image_state->memory_requirements_checked[0] && !image_state->external_memory_handle) {
skip |= LogWarning(device, kVUID_BestPractices_ImageMemReqNotCalled,
"%s: Binding memory to %s but vkGetImageMemoryRequirements() has not been called on that image.",
api_name, report_data->FormatHandle(image).c_str());
}
} else {
// TODO If binding disjoint image then this needs to check that VkImagePlaneMemoryRequirementsInfo was called for each
// plane.
}
const auto mem_state = Get<DEVICE_MEMORY_STATE>(memory);
if (mem_state->alloc_info.allocationSize == image_state->requirements[0].size &&
mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
skip |= LogPerformanceWarning(
device, kVUID_BestPractices_SmallDedicatedAllocation,
"%s: Trying to bind %s to a memory block which is fully consumed by the image. "
"The required size of the allocation is %" PRIu64 ", but smaller images like this should be sub-allocated from "
"larger memory blocks. (Current threshold is %" PRIu64 " bytes.)",
api_name, report_data->FormatHandle(image).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
}
// If we're binding memory to a image which was created as TRANSIENT and the image supports LAZY allocation,
// make sure this type is actually used.
// This warning will only trigger if this layer is run on a platform that supports LAZILY_ALLOCATED_BIT
// (i.e.most tile - based renderers)
if (image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {
bool supports_lazy = false;
uint32_t suggested_type = 0;
for (uint32_t i = 0; i < phys_dev_mem_props.memoryTypeCount; i++) {
if ((1u << i) & image_state->requirements[0].memoryTypeBits) {
if (phys_dev_mem_props.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
supports_lazy = true;
suggested_type = i;
break;
}
}
}
uint32_t allocated_properties = phys_dev_mem_props.memoryTypes[mem_state->alloc_info.memoryTypeIndex].propertyFlags;
if (supports_lazy && (allocated_properties & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) == 0) {
skip |= LogPerformanceWarning(
device, kVUID_BestPractices_NonLazyTransientImage,
"%s: Attempting to bind memory type %u to VkImage which was created with TRANSIENT_ATTACHMENT_BIT,"
"but this memory type is not LAZILY_ALLOCATED_BIT. You should use memory type %u here instead to save "
"%" PRIu64 " bytes of physical memory.",
api_name, mem_state->alloc_info.memoryTypeIndex, suggested_type, image_state->requirements[0].size);
}
}
return skip;
}
bool BestPractices::PreCallValidateBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory,
VkDeviceSize memoryOffset) const {
bool skip = false;
const char* api_name = "vkBindImageMemory()";
skip |= ValidateBindImageMemory(image, memory, api_name);
return skip;
}
bool BestPractices::PreCallValidateBindImageMemory2(VkDevice device, uint32_t bindInfoCount,
const VkBindImageMemoryInfo* pBindInfos) const {
char api_name[64];
bool skip = false;
for (uint32_t i = 0; i < bindInfoCount; i++) {
sprintf(api_name, "vkBindImageMemory2() pBindInfos[%u]", i);
if (!LvlFindInChain<VkBindImageMemorySwapchainInfoKHR>(pBindInfos[i].pNext)) {
skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
}
}
return skip;
}
bool BestPractices::PreCallValidateBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount,
const VkBindImageMemoryInfo* pBindInfos) const {
char api_name[64];
bool skip = false;
for (uint32_t i = 0; i < bindInfoCount; i++) {
sprintf(api_name, "vkBindImageMemory2KHR() pBindInfos[%u]", i);
skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
}
return skip;
}
static inline bool FormatHasFullThroughputBlendingArm(VkFormat format) {
switch (format) {
case VK_FORMAT_B10G11R11_UFLOAT_PACK32:
case VK_FORMAT_R16_SFLOAT:
case VK_FORMAT_R16G16_SFLOAT:
case VK_FORMAT_R16G16B16_SFLOAT:
case VK_FORMAT_R16G16B16A16_SFLOAT:
case VK_FORMAT_R32_SFLOAT:
case VK_FORMAT_R32G32_SFLOAT:
case VK_FORMAT_R32G32B32_SFLOAT:
case VK_FORMAT_R32G32B32A32_SFLOAT:
return false;
default:
return true;
}
}
bool BestPractices::ValidateMultisampledBlendingArm(uint32_t createInfoCount,
const VkGraphicsPipelineCreateInfo* pCreateInfos) const {
bool skip = false;
for (uint32_t i = 0; i < createInfoCount; i++) {
auto create_info = &pCreateInfos[i];
if (!create_info->pColorBlendState || !create_info->pMultisampleState ||
create_info->pMultisampleState->rasterizationSamples == VK_SAMPLE_COUNT_1_BIT ||
create_info->pMultisampleState->sampleShadingEnable) {
return skip;
}
auto rp_state = Get<RENDER_PASS_STATE>(create_info->renderPass);
const auto& subpass = rp_state->createInfo.pSubpasses[create_info->subpass];
// According to spec, pColorBlendState must be ignored if subpass does not have color attachments.
uint32_t num_color_attachments = std::min(subpass.colorAttachmentCount, create_info->pColorBlendState->attachmentCount);
for (uint32_t j = 0; j < num_color_attachments; j++) {
const auto& blend_att = create_info->pColorBlendState->pAttachments[j];
uint32_t att = subpass.pColorAttachments[j].attachment;
if (att != VK_ATTACHMENT_UNUSED && blend_att.blendEnable && blend_att.colorWriteMask) {
if (!FormatHasFullThroughputBlendingArm(rp_state->createInfo.pAttachments[att].format)) {
skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultisampledBlending,
"%s vkCreateGraphicsPipelines() - createInfo #%u: Pipeline is multisampled and "
"color attachment #%u makes use "
"of a format which cannot be blended at full throughput when using MSAA.",
VendorSpecificTag(kBPVendorArm), i, j);
}
}
}
}
return skip;
}
void BestPractices::ManualPostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
const VkComputePipelineCreateInfo* pCreateInfos,
const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
VkResult result, void* pipe_state) {
// AMD best practice
pipeline_cache = pipelineCache;
}
bool BestPractices::PreCallValidateCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
const VkGraphicsPipelineCreateInfo* pCreateInfos,
const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
void* cgpl_state_data) const {
bool skip = StateTracker::PreCallValidateCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos,
pAllocator, pPipelines, cgpl_state_data);
create_graphics_pipeline_api_state* cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state*>(cgpl_state_data);
if ((createInfoCount > 1) && (!pipelineCache)) {
skip |= LogPerformanceWarning(
device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
"Performance Warning: This vkCreateGraphicsPipelines call is creating multiple pipelines but is not using a "
"pipeline cache, which may help with performance");
}
for (uint32_t i = 0; i < createInfoCount; i++) {
const auto& create_info = pCreateInfos[i];
if (!(cgpl_state->pipe_state[i]->active_shaders & VK_SHADER_STAGE_MESH_BIT_NV)) {
const auto& vertex_input = *create_info.pVertexInputState;
uint32_t count = 0;
for (uint32_t j = 0; j < vertex_input.vertexBindingDescriptionCount; j++) {
if (vertex_input.pVertexBindingDescriptions[j].inputRate == VK_VERTEX_INPUT_RATE_INSTANCE) {
count++;
}
}
if (count > kMaxInstancedVertexBuffers) {
skip |= LogPerformanceWarning(
device, kVUID_BestPractices_CreatePipelines_TooManyInstancedVertexBuffers,
"The pipeline is using %u instanced vertex buffers (current limit: %u), but this can be inefficient on the "
"GPU. If using instanced vertex attributes prefer interleaving them in a single buffer.",
count, kMaxInstancedVertexBuffers);
}
}
if ((pCreateInfos[i].pRasterizationState->depthBiasEnable) &&
(pCreateInfos[i].pRasterizationState->depthBiasConstantFactor == 0.0f) &&
(pCreateInfos[i].pRasterizationState->depthBiasSlopeFactor == 0.0f) &&
VendorCheckEnabled(kBPVendorArm)) {
skip |= LogPerformanceWarning(
device, kVUID_BestPractices_CreatePipelines_DepthBias_Zero,
"%s Performance Warning: This vkCreateGraphicsPipelines call is created with depthBiasEnable set to true "
"and both depthBiasConstantFactor and depthBiasSlopeFactor are set to 0. This can cause reduced "
"efficiency during rasterization. Consider disabling depthBias or increasing either "
"depthBiasConstantFactor or depthBiasSlopeFactor.",
VendorSpecificTag(kBPVendorArm));
}
skip |= VendorCheckEnabled(kBPVendorArm) && ValidateMultisampledBlendingArm(createInfoCount, pCreateInfos);
}
if (VendorCheckEnabled(kBPVendorAMD)) {