blob: de386342a78ec9af314359ea9714d04e436aee0d [file] [log] [blame]
Jeremy Gebben610d3a62022-01-01 12:53:17 -07001/* Copyright (c) 2015-2022 The Khronos Group Inc.
2 * Copyright (c) 2015-2022 Valve Corporation
3 * Copyright (c) 2015-2022 LunarG, Inc.
Nadav Geva41c12a22021-05-21 13:14:05 -04004 * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
Camdeneaa86ea2019-07-26 11:00:09 -06005 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Camden Stocker <camden@lunarg.com>
Nadav Geva41c12a22021-05-21 13:14:05 -040019 * Author: Nadav Geva <nadav.geva@amd.com>
Camdeneaa86ea2019-07-26 11:00:09 -060020 */
21
Mark Lobodzinski57b8ae82020-02-20 16:37:14 -070022#include "best_practices_validation.h"
Camden5b184be2019-08-13 07:50:19 -060023#include "layer_chassis_dispatch.h"
Camden Stocker0a660ce2019-08-27 15:30:40 -060024#include "best_practices_error_enums.h"
Sam Wallsd7ab6db2020-06-19 20:41:54 +010025#include "shader_validation.h"
Jeremy Gebbena3705f42021-01-19 16:47:43 -070026#include "sync_utils.h"
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060027#include "cmd_buffer_state.h"
28#include "device_state.h"
29#include "render_pass_state.h"
Camden5b184be2019-08-13 07:50:19 -060030
31#include <string>
Sam Walls8e77e4f2020-03-16 20:47:40 +000032#include <bitset>
Sam Wallsd7ab6db2020-06-19 20:41:54 +010033#include <memory>
Camden5b184be2019-08-13 07:50:19 -060034
Attilio Provenzano19d6a982020-02-27 12:41:41 +000035struct VendorSpecificInfo {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -060036 EnableFlags vendor_id;
Attilio Provenzano19d6a982020-02-27 12:41:41 +000037 std::string name;
38};
39
LawG475463092022-02-22 10:45:54 +000040const std::map<BPVendorFlagBits, VendorSpecificInfo> kVendorInfo = {{kBPVendorArm, {vendor_specific_arm, "Arm"}},
41 {kBPVendorAMD, {vendor_specific_amd, "AMD"}},
42 {kBPVendorIMG, {vendor_specific_img, "IMG"}}};
Attilio Provenzano19d6a982020-02-27 12:41:41 +000043
Hannes Harnisch607d1d92021-07-10 18:44:56 +020044const SpecialUseVUIDs kSpecialUseInstanceVUIDs {
45 kVUID_BestPractices_CreateInstance_SpecialUseExtension_CADSupport,
46 kVUID_BestPractices_CreateInstance_SpecialUseExtension_D3DEmulation,
47 kVUID_BestPractices_CreateInstance_SpecialUseExtension_DevTools,
48 kVUID_BestPractices_CreateInstance_SpecialUseExtension_Debugging,
49 kVUID_BestPractices_CreateInstance_SpecialUseExtension_GLEmulation,
50};
51
52const SpecialUseVUIDs kSpecialUseDeviceVUIDs {
53 kVUID_BestPractices_CreateDevice_SpecialUseExtension_CADSupport,
54 kVUID_BestPractices_CreateDevice_SpecialUseExtension_D3DEmulation,
55 kVUID_BestPractices_CreateDevice_SpecialUseExtension_DevTools,
56 kVUID_BestPractices_CreateDevice_SpecialUseExtension_Debugging,
57 kVUID_BestPractices_CreateDevice_SpecialUseExtension_GLEmulation,
58};
59
Jeremy Gebben20da7a12022-02-25 14:07:46 -070060ReadLockGuard BestPractices::ReadLock() {
61 if (fine_grained_locking) {
62 return ReadLockGuard(validation_object_mutex, std::defer_lock);
63 } else {
64 return ReadLockGuard(validation_object_mutex);
65 }
66}
67
68WriteLockGuard BestPractices::WriteLock() {
69 if (fine_grained_locking) {
70 return WriteLockGuard(validation_object_mutex, std::defer_lock);
71 } else {
72 return WriteLockGuard(validation_object_mutex);
73 }
74}
75
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -060076std::shared_ptr<CMD_BUFFER_STATE> BestPractices::CreateCmdBufferState(VkCommandBuffer cb,
77 const VkCommandBufferAllocateInfo* pCreateInfo,
Jeremy Gebbencd7fa282021-10-27 10:25:32 -060078 const COMMAND_POOL_STATE* pool) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -070079 return std::static_pointer_cast<CMD_BUFFER_STATE>(std::make_shared<bp_state::CommandBuffer>(this, cb, pCreateInfo, pool));
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -060080}
81
Jeremy Gebben20da7a12022-02-25 14:07:46 -070082bp_state::CommandBuffer::CommandBuffer(BestPractices* bp, VkCommandBuffer cb, const VkCommandBufferAllocateInfo* pCreateInfo,
83 const COMMAND_POOL_STATE* pool)
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -060084 : CMD_BUFFER_STATE(bp, cb, pCreateInfo, pool) {}
85
Attilio Provenzano19d6a982020-02-27 12:41:41 +000086bool BestPractices::VendorCheckEnabled(BPVendorFlags vendors) const {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -070087 for (const auto& vendor : kVendorInfo) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -060088 if (vendors & vendor.first && enabled[vendor.second.vendor_id]) {
Attilio Provenzano19d6a982020-02-27 12:41:41 +000089 return true;
90 }
91 }
92 return false;
93}
94
95const char* VendorSpecificTag(BPVendorFlags vendors) {
96 // Cache built vendor tags in a map
Jeremy Gebbencbf22862021-03-03 12:01:22 -070097 static layer_data::unordered_map<BPVendorFlags, std::string> tag_map;
Attilio Provenzano19d6a982020-02-27 12:41:41 +000098
99 auto res = tag_map.find(vendors);
100 if (res == tag_map.end()) {
101 // Build the vendor tag string
102 std::stringstream vendor_tag;
103
104 vendor_tag << "[";
105 bool first_vendor = true;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700106 for (const auto& vendor : kVendorInfo) {
Attilio Provenzano19d6a982020-02-27 12:41:41 +0000107 if (vendors & vendor.first) {
108 if (!first_vendor) {
109 vendor_tag << ", ";
110 }
111 vendor_tag << vendor.second.name;
112 first_vendor = false;
113 }
114 }
115 vendor_tag << "]";
116
117 tag_map[vendors] = vendor_tag.str();
118 res = tag_map.find(vendors);
119 }
120
121 return res->second.c_str();
122}
123
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700124const char* DepReasonToString(ExtDeprecationReason reason) {
125 switch (reason) {
126 case kExtPromoted:
127 return "promoted to";
128 break;
129 case kExtObsoleted:
130 return "obsoleted by";
131 break;
132 case kExtDeprecated:
133 return "deprecated by";
134 break;
135 default:
136 return "";
137 break;
138 }
139}
140
141bool BestPractices::ValidateDeprecatedExtensions(const char* api_name, const char* extension_name, uint32_t version,
142 const char* vuid) const {
143 bool skip = false;
144 auto dep_info_it = deprecated_extensions.find(extension_name);
145 if (dep_info_it != deprecated_extensions.end()) {
146 auto dep_info = dep_info_it->second;
Mark Lobodzinski6a149702020-05-14 12:21:34 -0600147 if (((dep_info.target.compare("VK_VERSION_1_1") == 0) && (version >= VK_API_VERSION_1_1)) ||
Tony-LunarGc30b59f2022-02-15 11:02:36 -0700148 ((dep_info.target.compare("VK_VERSION_1_2") == 0) && (version >= VK_API_VERSION_1_2)) ||
149 ((dep_info.target.compare("VK_VERSION_1_3") == 0) && (version >= VK_API_VERSION_1_3))) {
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700150 skip |=
151 LogWarning(instance, vuid, "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.",
152 api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str());
Mark Lobodzinski6a149702020-05-14 12:21:34 -0600153 } else if (dep_info.target.find("VK_VERSION") == std::string::npos) {
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700154 if (dep_info.target.length() == 0) {
155 skip |= LogWarning(instance, vuid,
156 "%s(): Attempting to enable deprecated extension %s, but this extension has been deprecated "
157 "without replacement.",
158 api_name, extension_name);
159 } else {
160 skip |= LogWarning(instance, vuid,
161 "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.",
162 api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str());
163 }
164 }
165 }
166 return skip;
167}
168
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200169bool BestPractices::ValidateSpecialUseExtensions(const char* api_name, const char* extension_name, const SpecialUseVUIDs& special_use_vuids) const
170{
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700171 bool skip = false;
172 auto dep_info_it = special_use_extensions.find(extension_name);
173
174 if (dep_info_it != special_use_extensions.end()) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200175 const char* const format = "%s(): Attempting to enable extension %s, but this extension is intended to support %s "
176 "and it is strongly recommended that it be otherwise avoided.";
177 auto& special_uses = dep_info_it->second;
sfricke-samsungef15e482022-01-26 11:32:49 -0800178
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700179 if (special_uses.find("cadsupport") != std::string::npos) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800180 skip |= LogWarning(instance, special_use_vuids.cadsupport, format, api_name, extension_name,
181 "specialized functionality used by CAD/CAM applications");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700182 }
183 if (special_uses.find("d3demulation") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200184 skip |= LogWarning(instance, special_use_vuids.d3demulation, format, api_name, extension_name,
185 "D3D emulation layers, and applications ported from D3D, by adding functionality specific to D3D");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700186 }
187 if (special_uses.find("devtools") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200188 skip |= LogWarning(instance, special_use_vuids.devtools, format, api_name, extension_name,
189 "developer tools such as capture-replay libraries");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700190 }
191 if (special_uses.find("debugging") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200192 skip |= LogWarning(instance, special_use_vuids.debugging, format, api_name, extension_name,
193 "use by applications when debugging");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700194 }
195 if (special_uses.find("glemulation") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200196 skip |= LogWarning(instance, special_use_vuids.glemulation, format, api_name, extension_name,
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700197 "OpenGL and/or OpenGL ES emulation layers, and applications ported from those APIs, by adding functionality "
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200198 "specific to those APIs");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700199 }
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700200 }
201 return skip;
202}
203
Camden5b184be2019-08-13 07:50:19 -0600204bool BestPractices::PreCallValidateCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500205 VkInstance* pInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600206 bool skip = false;
207
208 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
209 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeviceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -0800210 skip |= LogWarning(instance, kVUID_BestPractices_CreateInstance_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700211 "vkCreateInstance(): Attempting to enable Device Extension %s at CreateInstance time.",
212 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -0600213 }
Mark Lobodzinski17d8dc62020-06-03 08:48:58 -0600214 uint32_t specified_version =
215 (pCreateInfo->pApplicationInfo ? pCreateInfo->pApplicationInfo->apiVersion : VK_API_VERSION_1_0);
216 skip |= ValidateDeprecatedExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i], specified_version,
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700217 kVUID_BestPractices_CreateInstance_DeprecatedExtension);
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200218 skip |= ValidateSpecialUseExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i], kSpecialUseInstanceVUIDs);
Camden5b184be2019-08-13 07:50:19 -0600219 }
220
221 return skip;
222}
223
Camden5b184be2019-08-13 07:50:19 -0600224bool BestPractices::PreCallValidateCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500225 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) const {
Camden5b184be2019-08-13 07:50:19 -0600226 bool skip = false;
227
228 // get API version of physical device passed when creating device.
229 VkPhysicalDeviceProperties physical_device_properties{};
230 DispatchGetPhysicalDeviceProperties(physicalDevice, &physical_device_properties);
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500231 auto device_api_version = physical_device_properties.apiVersion;
Camden5b184be2019-08-13 07:50:19 -0600232
233 // check api versions and warn if instance api Version is higher than version on device.
Jeremy Gebben404f6ac2021-10-28 12:33:28 -0600234 if (api_version > device_api_version) {
235 std::string inst_api_name = StringAPIVersion(api_version);
Mark Lobodzinski60880782020-08-11 08:02:07 -0600236 std::string dev_api_name = StringAPIVersion(device_api_version);
Camden5b184be2019-08-13 07:50:19 -0600237
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700238 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_API_Mismatch,
239 "vkCreateDevice(): API Version of current instance, %s is higher than API Version on device, %s",
240 inst_api_name.c_str(), dev_api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600241 }
242
243 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
244 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kInstanceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -0800245 skip |= LogWarning(instance, kVUID_BestPractices_CreateDevice_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700246 "vkCreateDevice(): Attempting to enable Instance Extension %s at CreateDevice time.",
247 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -0600248 }
Jeremy Gebben404f6ac2021-10-28 12:33:28 -0600249 skip |= ValidateDeprecatedExtensions("CreateDevice", pCreateInfo->ppEnabledExtensionNames[i], api_version,
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700250 kVUID_BestPractices_CreateDevice_DeprecatedExtension);
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200251 skip |= ValidateSpecialUseExtensions("CreateDevice", pCreateInfo->ppEnabledExtensionNames[i], kSpecialUseDeviceVUIDs);
Camden5b184be2019-08-13 07:50:19 -0600252 }
253
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700254 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600255 if ((bp_pd_state->vkGetPhysicalDeviceFeaturesState == UNCALLED) && (pCreateInfo->pEnabledFeatures != NULL)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700256 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_PDFeaturesNotCalled,
257 "vkCreateDevice() called before getting physical device features from vkGetPhysicalDeviceFeatures().");
Camden83a9c372019-08-14 11:41:38 -0600258 }
259
Nadav Gevaf0808442021-05-21 13:51:25 -0400260 if ((VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorAMD)) && (pCreateInfo->pEnabledFeatures != nullptr) &&
Szilard Papp7d2c7952020-06-22 14:38:13 +0100261 (pCreateInfo->pEnabledFeatures->robustBufferAccess == VK_TRUE)) {
262 skip |= LogPerformanceWarning(
263 device, kVUID_BestPractices_CreateDevice_RobustBufferAccess,
Nadav Gevaf0808442021-05-21 13:51:25 -0400264 "%s %s vkCreateDevice() called with enabled robustBufferAccess. Use robustBufferAccess as a debugging tool during "
Szilard Papp7d2c7952020-06-22 14:38:13 +0100265 "development. Enabling it causes loss in performance for accesses to uniform buffers and shader storage "
266 "buffers. Disable robustBufferAccess in release builds. Only leave it enabled if the application use-case "
267 "requires the additional level of reliability due to the use of unverified user-supplied draw parameters.",
Nadav Gevaf0808442021-05-21 13:51:25 -0400268 VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorAMD));
Szilard Papp7d2c7952020-06-22 14:38:13 +0100269 }
270
Camden5b184be2019-08-13 07:50:19 -0600271 return skip;
272}
273
274bool BestPractices::PreCallValidateCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500275 const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer) const {
Camden5b184be2019-08-13 07:50:19 -0600276 bool skip = false;
277
278 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700279 std::stringstream buffer_hex;
280 buffer_hex << "0x" << std::hex << HandleToUint64(pBuffer);
Camden5b184be2019-08-13 07:50:19 -0600281
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700282 skip |= LogWarning(
283 device, kVUID_BestPractices_SharingModeExclusive,
284 "Warning: Buffer (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
285 "(queueFamilyIndexCount of %" PRIu32 ").",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700286 buffer_hex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600287 }
288
289 return skip;
290}
291
292bool BestPractices::PreCallValidateCreateImage(VkDevice device, const VkImageCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500293 const VkAllocationCallbacks* pAllocator, VkImage* pImage) const {
Camden5b184be2019-08-13 07:50:19 -0600294 bool skip = false;
295
296 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700297 std::stringstream image_hex;
298 image_hex << "0x" << std::hex << HandleToUint64(pImage);
Camden5b184be2019-08-13 07:50:19 -0600299
300 skip |=
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700301 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
302 "Warning: Image (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
303 "(queueFamilyIndexCount of %" PRIu32 ").",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700304 image_hex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600305 }
306
ziga-lunarg6df3d102022-03-18 17:02:14 +0100307 if ((pCreateInfo->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT) && !(pCreateInfo->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) {
308 skip |= LogWarning(device, kVUID_BestPractices_ImageCreateFlags,
309 "vkCreateImage(): pCreateInfo->flags has VK_IMAGE_CREATE_EXTENDED_USAGE_BIT set, but not "
310 "VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, therefore image views created from this image will have to use the "
311 "same format and VK_IMAGE_CREATE_EXTENDED_USAGE_BIT will not have any effect.");
312 }
313
Attilio Provenzano02859b22020-02-27 14:17:28 +0000314 if (VendorCheckEnabled(kBPVendorArm)) {
315 if (pCreateInfo->samples > kMaxEfficientSamplesArm) {
316 skip |= LogPerformanceWarning(
317 device, kVUID_BestPractices_CreateImage_TooLargeSampleCount,
318 "%s vkCreateImage(): Trying to create an image with %u samples. "
319 "The hardware revision may not have full throughput for framebuffers with more than %u samples.",
320 VendorSpecificTag(kBPVendorArm), static_cast<uint32_t>(pCreateInfo->samples), kMaxEfficientSamplesArm);
321 }
322
323 if (pCreateInfo->samples > VK_SAMPLE_COUNT_1_BIT && !(pCreateInfo->usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT)) {
324 skip |= LogPerformanceWarning(
325 device, kVUID_BestPractices_CreateImage_NonTransientMSImage,
326 "%s vkCreateImage(): Trying to create a multisampled image, but createInfo.usage did not have "
327 "VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. Multisampled images may be resolved on-chip, "
328 "and do not need to be backed by physical storage. "
329 "TRANSIENT_ATTACHMENT allows tiled GPUs to not back the multisampled image with physical memory.",
330 VendorSpecificTag(kBPVendorArm));
331 }
332 }
333
Nadav Gevaf0808442021-05-21 13:51:25 -0400334 if (VendorCheckEnabled(kBPVendorAMD)) {
335 std::stringstream image_hex;
336 image_hex << "0x" << std::hex << HandleToUint64(pImage);
337
338 if ((pCreateInfo->usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
339 (pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT)) {
340 skip |= LogPerformanceWarning(device,
341 kVUID_BestPractices_vkImage_AvoidConcurrentRenderTargets,
342 "%s Performance warning: image (%s) is created as a render target with VK_SHARING_MODE_CONCURRENT. "
343 "Using a SHARING_MODE_CONCURRENT "
344 "is not recommended with color and depth targets",
345 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
346 }
347
348 if ((pCreateInfo->usage &
349 (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
350 (pCreateInfo->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) {
351 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_DontUseMutableRenderTargets,
352 "%s Performance warning: image (%s) is created as a render target with VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT. "
353 "Using a MUTABLE_FORMAT is not recommended with color, depth, and storage targets",
354 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
355 }
356
357 if ((pCreateInfo->usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
358 (pCreateInfo->usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
359 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_DontUseStorageRenderTargets,
360 "%s Performance warning: image (%s) is created as a render target with VK_IMAGE_USAGE_STORAGE_BIT. Using a "
361 "VK_IMAGE_USAGE_STORAGE_BIT is not recommended with color and depth targets",
362 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
363 }
364 }
365
Camden5b184be2019-08-13 07:50:19 -0600366 return skip;
367}
368
369bool BestPractices::PreCallValidateCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500370 const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) const {
Camden5b184be2019-08-13 07:50:19 -0600371 bool skip = false;
372
Jeremy Gebben383b9a32021-09-08 16:31:33 -0600373 const auto* bp_pd_state = GetPhysicalDeviceState();
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600374 if (bp_pd_state) {
375 if (bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState == UNCALLED) {
376 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
377 "vkCreateSwapchainKHR() called before getting surface capabilities from "
378 "vkGetPhysicalDeviceSurfaceCapabilitiesKHR().");
379 }
Camden83a9c372019-08-14 11:41:38 -0600380
Shannon McPherson73e58c82021-03-05 17:14:26 -0700381 if ((pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR) &&
382 (bp_pd_state->vkGetPhysicalDeviceSurfacePresentModesKHRState != QUERY_DETAILS)) {
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600383 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
384 "vkCreateSwapchainKHR() called before getting surface present mode(s) from "
385 "vkGetPhysicalDeviceSurfacePresentModesKHR().");
386 }
Camden83a9c372019-08-14 11:41:38 -0600387
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600388 if (bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState != QUERY_DETAILS) {
389 skip |= LogWarning(
390 device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
391 "vkCreateSwapchainKHR() called before getting surface format(s) from vkGetPhysicalDeviceSurfaceFormatsKHR().");
392 }
Camden83a9c372019-08-14 11:41:38 -0600393 }
394
Camden5b184be2019-08-13 07:50:19 -0600395 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700396 skip |=
397 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
Mark Lobodzinski019f4e32020-04-13 11:01:35 -0600398 "Warning: A Swapchain is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while "
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700399 "specifying multiple queues (queueFamilyIndexCount of %" PRIu32 ").",
400 pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600401 }
402
ziga-lunarg79beba62022-03-30 01:17:30 +0200403 const auto present_mode = pCreateInfo->presentMode;
404 if (((present_mode == VK_PRESENT_MODE_MAILBOX_KHR) || (present_mode == VK_PRESENT_MODE_FIFO_KHR)) &&
405 (pCreateInfo->minImageCount == 2)) {
Szilard Papp48a6da32020-06-10 14:41:59 +0100406 skip |= LogPerformanceWarning(
407 device, kVUID_BestPractices_SuboptimalSwapchainImageCount,
408 "Warning: A Swapchain is being created with minImageCount set to %" PRIu32
409 ", which means double buffering is going "
410 "to be used. Using double buffering and vsync locks rendering to an integer fraction of the vsync rate. In turn, "
411 "reducing the performance of the application if rendering is slower than vsync. Consider setting minImageCount to "
412 "3 to use triple buffering to maximize performance in such cases.",
413 pCreateInfo->minImageCount);
414 }
415
Szilard Pappd5f0f812020-06-22 09:01:29 +0100416 if (VendorCheckEnabled(kBPVendorArm) && (pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR)) {
417 skip |= LogWarning(device, kVUID_BestPractices_CreateSwapchain_PresentMode,
418 "%s Warning: Swapchain is not being created with presentation mode \"VK_PRESENT_MODE_FIFO_KHR\". "
419 "Prefer using \"VK_PRESENT_MODE_FIFO_KHR\" to avoid unnecessary CPU and GPU load and save power. "
420 "Presentation modes which are not FIFO will present the latest available frame and discard other "
421 "frame(s) if any.",
422 VendorSpecificTag(kBPVendorArm));
423 }
424
Camden5b184be2019-08-13 07:50:19 -0600425 return skip;
426}
427
428bool BestPractices::PreCallValidateCreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount,
429 const VkSwapchainCreateInfoKHR* pCreateInfos,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500430 const VkAllocationCallbacks* pAllocator,
431 VkSwapchainKHR* pSwapchains) const {
Camden5b184be2019-08-13 07:50:19 -0600432 bool skip = false;
433
434 for (uint32_t i = 0; i < swapchainCount; i++) {
435 if ((pCreateInfos[i].queueFamilyIndexCount > 1) && (pCreateInfos[i].imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700436 skip |= LogWarning(
437 device, kVUID_BestPractices_SharingModeExclusive,
438 "Warning: A shared swapchain (index %" PRIu32
439 ") is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple "
440 "queues (queueFamilyIndexCount of %" PRIu32 ").",
441 i, pCreateInfos[i].queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600442 }
443 }
444
445 return skip;
446}
447
448bool BestPractices::PreCallValidateCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500449 const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass) const {
Camden5b184be2019-08-13 07:50:19 -0600450 bool skip = false;
451
452 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
453 VkFormat format = pCreateInfo->pAttachments[i].format;
454 if (pCreateInfo->pAttachments[i].initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
455 if ((FormatIsColor(format) || FormatHasDepth(format)) &&
456 pCreateInfo->pAttachments[i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700457 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
458 "Render pass has an attachment with loadOp == VK_ATTACHMENT_LOAD_OP_LOAD and "
459 "initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
460 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
461 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600462 }
463 if (FormatHasStencil(format) && pCreateInfo->pAttachments[i].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700464 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
465 "Render pass has an attachment with stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD "
466 "and initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
467 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
468 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600469 }
470 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000471
472 const auto& attachment = pCreateInfo->pAttachments[i];
473 if (attachment.samples > VK_SAMPLE_COUNT_1_BIT) {
474 bool access_requires_memory =
475 attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE;
476
477 if (FormatHasStencil(format)) {
478 access_requires_memory |= attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
479 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE;
480 }
481
482 if (access_requires_memory) {
483 skip |= LogPerformanceWarning(
484 device, kVUID_BestPractices_CreateRenderPass_ImageRequiresMemory,
485 "Attachment %u in the VkRenderPass is a multisampled image with %u samples, but it uses loadOp/storeOp "
486 "which requires accessing data from memory. Multisampled images should always be loadOp = CLEAR or DONT_CARE, "
487 "storeOp = DONT_CARE. This allows the implementation to use lazily allocated memory effectively.",
488 i, static_cast<uint32_t>(attachment.samples));
489 }
490 }
Camden5b184be2019-08-13 07:50:19 -0600491 }
492
493 for (uint32_t dependency = 0; dependency < pCreateInfo->dependencyCount; dependency++) {
494 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].srcStageMask);
495 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].dstStageMask);
496 }
497
498 return skip;
499}
500
Tony-LunarG767180f2020-04-23 14:03:59 -0600501bool BestPractices::ValidateAttachments(const VkRenderPassCreateInfo2* rpci, uint32_t attachmentCount,
502 const VkImageView* image_views) const {
503 bool skip = false;
504
505 // Check for non-transient attachments that should be transient and vice versa
506 for (uint32_t i = 0; i < attachmentCount; ++i) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200507 const auto& attachment = rpci->pAttachments[i];
Tony-LunarG767180f2020-04-23 14:03:59 -0600508 bool attachment_should_be_transient =
509 (attachment.loadOp != VK_ATTACHMENT_LOAD_OP_LOAD && attachment.storeOp != VK_ATTACHMENT_STORE_OP_STORE);
510
511 if (FormatHasStencil(attachment.format)) {
512 attachment_should_be_transient &= (attachment.stencilLoadOp != VK_ATTACHMENT_LOAD_OP_LOAD &&
513 attachment.stencilStoreOp != VK_ATTACHMENT_STORE_OP_STORE);
514 }
515
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600516 auto view_state = Get<IMAGE_VIEW_STATE>(image_views[i]);
Tony-LunarG767180f2020-04-23 14:03:59 -0600517 if (view_state) {
Jeremy Gebben057f9d52021-11-05 14:12:31 -0600518 const auto& ici = view_state->image_state->createInfo;
Tony-LunarG767180f2020-04-23 14:03:59 -0600519
520 bool image_is_transient = (ici.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) != 0;
521
522 // The check for an image that should not be transient applies to all GPUs
523 if (!attachment_should_be_transient && image_is_transient) {
524 skip |= LogPerformanceWarning(
525 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldNotBeTransient,
526 "Attachment %u in VkFramebuffer uses loadOp/storeOps which need to access physical memory, "
527 "but the image backing the image view has VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
528 "Physical memory will need to be backed lazily to this image, potentially causing stalls.",
529 i);
530 }
531
532 bool supports_lazy = false;
533 for (uint32_t j = 0; j < phys_dev_mem_props.memoryTypeCount; j++) {
534 if (phys_dev_mem_props.memoryTypes[j].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
535 supports_lazy = true;
536 }
537 }
538
539 // The check for an image that should be transient only applies to GPUs supporting
540 // lazily allocated memory
541 if (supports_lazy && attachment_should_be_transient && !image_is_transient) {
542 skip |= LogPerformanceWarning(
543 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldBeTransient,
544 "Attachment %u in VkFramebuffer uses loadOp/storeOps which never have to be backed by physical memory, "
545 "but the image backing the image view does not have VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
546 "You can save physical memory by using transient attachment backed by lazily allocated memory here.",
547 i);
548 }
549 }
550 }
551 return skip;
552}
553
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000554bool BestPractices::PreCallValidateCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo* pCreateInfo,
555 const VkAllocationCallbacks* pAllocator, VkFramebuffer* pFramebuffer) const {
556 bool skip = false;
557
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600558 auto rp_state = Get<RENDER_PASS_STATE>(pCreateInfo->renderPass);
Mike Schuchardt2df08912020-12-15 16:28:09 -0800559 if (rp_state && !(pCreateInfo->flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT)) {
Tony-LunarG767180f2020-04-23 14:03:59 -0600560 skip = ValidateAttachments(rp_state->createInfo.ptr(), pCreateInfo->attachmentCount, pCreateInfo->pAttachments);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000561 }
562
563 return skip;
564}
565
Sam Wallse746d522020-03-16 21:20:23 +0000566bool BestPractices::PreCallValidateAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
567 VkDescriptorSet* pDescriptorSets, void* ads_state_data) const {
568 bool skip = false;
569 skip |= ValidationStateTracker::PreCallValidateAllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets, ads_state_data);
570
571 if (!skip) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700572 const auto pool_state = Get<bp_state::DescriptorPool>(pAllocateInfo->descriptorPool);
Sam Wallse746d522020-03-16 21:20:23 +0000573 // if the number of freed sets > 0, it implies they could be recycled instead if desirable
574 // this warning is specific to Arm
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700575 if (VendorCheckEnabled(kBPVendorArm) && pool_state && (pool_state->freed_count > 0)) {
Sam Wallse746d522020-03-16 21:20:23 +0000576 skip |= LogPerformanceWarning(
577 device, kVUID_BestPractices_AllocateDescriptorSets_SuboptimalReuse,
578 "%s Descriptor set memory was allocated via vkAllocateDescriptorSets() for sets which were previously freed in the "
579 "same logical device. On some drivers or architectures it may be most optimal to re-use existing descriptor sets.",
580 VendorSpecificTag(kBPVendorArm));
581 }
582 }
583
584 return skip;
585}
586
Mark Lobodzinski84101d72020-04-24 09:43:48 -0600587void BestPractices::ManualPostCallRecordAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
588 VkDescriptorSet* pDescriptorSets, VkResult result, void* ads_state) {
Sam Wallse746d522020-03-16 21:20:23 +0000589 if (result == VK_SUCCESS) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700590 auto pool_state = Get<bp_state::DescriptorPool>(pAllocateInfo->descriptorPool);
591 if (pool_state) {
Sam Wallse746d522020-03-16 21:20:23 +0000592 // we record successful allocations by subtracting the allocation count from the last recorded free count
593 const auto alloc_count = pAllocateInfo->descriptorSetCount;
594 // clamp the unsigned subtraction to the range [0, last_free_count]
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700595 if (pool_state->freed_count > alloc_count) {
596 pool_state->freed_count -= alloc_count;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700597 } else {
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700598 pool_state->freed_count = 0;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700599 }
Sam Wallse746d522020-03-16 21:20:23 +0000600 }
601 }
602}
603
604void BestPractices::PostCallRecordFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
605 const VkDescriptorSet* pDescriptorSets, VkResult result) {
606 ValidationStateTracker::PostCallRecordFreeDescriptorSets(device, descriptorPool, descriptorSetCount, pDescriptorSets, result);
607 if (result == VK_SUCCESS) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700608 auto pool_state = Get<bp_state::DescriptorPool>(descriptorPool);
Sam Wallse746d522020-03-16 21:20:23 +0000609 // we want to track frees because we're interested in suggesting re-use
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700610 if (pool_state) {
611 pool_state->freed_count += descriptorSetCount;
Sam Wallse746d522020-03-16 21:20:23 +0000612 }
613 }
614}
615
Camden5b184be2019-08-13 07:50:19 -0600616bool BestPractices::PreCallValidateAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500617 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) const {
Camden5b184be2019-08-13 07:50:19 -0600618 bool skip = false;
619
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700620 if ((Count<DEVICE_MEMORY_STATE>() + 1) > kMemoryObjectWarningLimit) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700621 skip |= LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_TooManyObjects,
622 "Performance Warning: This app has > %" PRIu32 " memory objects.", kMemoryObjectWarningLimit);
Camden5b184be2019-08-13 07:50:19 -0600623 }
624
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000625 if (pAllocateInfo->allocationSize < kMinDeviceAllocationSize) {
626 skip |= LogPerformanceWarning(
627 device, kVUID_BestPractices_AllocateMemory_SmallAllocation,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600628 "vkAllocateMemory(): Allocating a VkDeviceMemory of size %" PRIu64 ". This is a very small allocation (current "
629 "threshold is %" PRIu64 " bytes). "
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000630 "You should make large allocations and sub-allocate from one large VkDeviceMemory.",
631 pAllocateInfo->allocationSize, kMinDeviceAllocationSize);
632 }
633
Camden83a9c372019-08-14 11:41:38 -0600634 // TODO: Insert get check for GetPhysicalDeviceMemoryProperties once the state is tracked in the StateTracker
635
636 return skip;
637}
638
Mark Lobodzinski84101d72020-04-24 09:43:48 -0600639void BestPractices::ManualPostCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
640 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory,
641 VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700642 if (result != VK_SUCCESS) {
643 static std::vector<VkResult> error_codes = {VK_ERROR_OUT_OF_HOST_MEMORY, VK_ERROR_OUT_OF_DEVICE_MEMORY,
644 VK_ERROR_TOO_MANY_OBJECTS, VK_ERROR_INVALID_EXTERNAL_HANDLE,
Mike Schuchardt2df08912020-12-15 16:28:09 -0800645 VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS};
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700646 static std::vector<VkResult> success_codes = {};
Nathaniel Cesariodb3f43f2021-05-12 09:08:23 -0600647 ValidateReturnCodes("vkAllocateMemory", result, error_codes, success_codes);
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700648 return;
649 }
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700650}
Camden Stocker9738af92019-10-16 13:54:03 -0700651
Mark Lobodzinskide15e582020-04-29 08:06:00 -0600652void BestPractices::ValidateReturnCodes(const char* api_name, VkResult result, const std::vector<VkResult>& error_codes,
653 const std::vector<VkResult>& success_codes) const {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700654 auto error = std::find(error_codes.begin(), error_codes.end(), result);
655 if (error != error_codes.end()) {
Gareth Webb586c46b2021-01-13 11:17:22 +0000656 static const std::vector<VkResult> common_failure_codes = {VK_ERROR_OUT_OF_DATE_KHR,
657 VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT};
658
659 auto common_failure = std::find(common_failure_codes.begin(), common_failure_codes.end(), result);
660 if (common_failure != common_failure_codes.end()) {
661 LogInfo(instance, kVUID_BestPractices_Failure_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
662 } else {
663 LogWarning(instance, kVUID_BestPractices_Error_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
664 }
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700665 return;
666 }
667 auto success = std::find(success_codes.begin(), success_codes.end(), result);
668 if (success != success_codes.end()) {
Mark Lobodzinskie7215152020-05-11 08:21:23 -0600669 LogInfo(instance, kVUID_BestPractices_NonSuccess_Result, "%s(): Returned non-success return code %s.", api_name,
670 string_VkResult(result));
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500671 }
672}
673
Jeff Bolz5c801d12019-10-09 10:38:45 -0500674bool BestPractices::PreCallValidateFreeMemory(VkDevice device, VkDeviceMemory memory,
675 const VkAllocationCallbacks* pAllocator) const {
Mark Lobodzinski91e50bf2020-01-14 09:55:11 -0700676 if (memory == VK_NULL_HANDLE) return false;
Camden83a9c372019-08-14 11:41:38 -0600677 bool skip = false;
678
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700679 auto mem_info = Get<DEVICE_MEMORY_STATE>(memory);
Camden83a9c372019-08-14 11:41:38 -0600680
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700681 for (const auto& item : mem_info->ObjectBindings()) {
682 const auto& obj = item.first;
Mark Lobodzinski818425a2020-03-16 18:19:03 -0600683 LogObjectList objlist(device);
684 objlist.add(obj);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600685 objlist.add(mem_info->mem());
Mark Lobodzinski818425a2020-03-16 18:19:03 -0600686 skip |= LogWarning(objlist, layer_name.c_str(), "VK Object %s still has a reference to mem obj %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600687 report_data->FormatHandle(obj).c_str(), report_data->FormatHandle(mem_info->mem()).c_str());
Camden83a9c372019-08-14 11:41:38 -0600688 }
689
Camden5b184be2019-08-13 07:50:19 -0600690 return skip;
691}
692
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000693bool BestPractices::ValidateBindBufferMemory(VkBuffer buffer, VkDeviceMemory memory, const char* api_name) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600694 bool skip = false;
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700695 auto buffer_state = Get<BUFFER_STATE>(buffer);
Camden Stockerb603cc82019-09-03 10:09:02 -0600696
sfricke-samsunge2441192019-11-06 14:07:57 -0800697 if (!buffer_state->memory_requirements_checked && !buffer_state->external_memory_handle) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700698 skip |= LogWarning(device, kVUID_BestPractices_BufferMemReqNotCalled,
699 "%s: Binding memory to %s but vkGetBufferMemoryRequirements() has not been called on that buffer.",
700 api_name, report_data->FormatHandle(buffer).c_str());
Camden Stockerb603cc82019-09-03 10:09:02 -0600701 }
702
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700703 auto mem_state = Get<DEVICE_MEMORY_STATE>(memory);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000704
AndreyVK_D3D0416a332021-11-02 23:22:28 +0300705 if (mem_state && mem_state->alloc_info.allocationSize == buffer_state->createInfo.size &&
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000706 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
707 skip |= LogPerformanceWarning(
708 device, kVUID_BestPractices_SmallDedicatedAllocation,
709 "%s: Trying to bind %s to a memory block which is fully consumed by the buffer. "
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600710 "The required size of the allocation is %" PRIu64 ", but smaller buffers like this should be sub-allocated from "
711 "larger memory blocks. (Current threshold is %" PRIu64 " bytes.)",
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000712 api_name, report_data->FormatHandle(buffer).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
713 }
714
Camden Stockerb603cc82019-09-03 10:09:02 -0600715 return skip;
716}
717
718bool BestPractices::PreCallValidateBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500719 VkDeviceSize memoryOffset) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600720 bool skip = false;
721 const char* api_name = "BindBufferMemory()";
722
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000723 skip |= ValidateBindBufferMemory(buffer, memory, api_name);
Camden Stockerb603cc82019-09-03 10:09:02 -0600724
725 return skip;
726}
727
728bool BestPractices::PreCallValidateBindBufferMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500729 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600730 char api_name[64];
731 bool skip = false;
732
733 for (uint32_t i = 0; i < bindInfoCount; i++) {
734 sprintf(api_name, "vkBindBufferMemory2() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000735 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600736 }
737
738 return skip;
739}
Camden Stockerb603cc82019-09-03 10:09:02 -0600740
741bool BestPractices::PreCallValidateBindBufferMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500742 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600743 char api_name[64];
744 bool skip = false;
Camden Stockerb603cc82019-09-03 10:09:02 -0600745
Camden Stocker8b798ab2019-09-03 10:33:28 -0600746 for (uint32_t i = 0; i < bindInfoCount; i++) {
747 sprintf(api_name, "vkBindBufferMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000748 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600749 }
750
751 return skip;
752}
753
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000754bool BestPractices::ValidateBindImageMemory(VkImage image, VkDeviceMemory memory, const char* api_name) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600755 bool skip = false;
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700756 auto image_state = Get<IMAGE_STATE>(image);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600757
sfricke-samsung71bc6572020-04-29 15:49:43 -0700758 if (image_state->disjoint == false) {
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600759 if (!image_state->memory_requirements_checked[0] && !image_state->external_memory_handle) {
sfricke-samsungd7ea5de2020-04-08 09:19:18 -0700760 skip |= LogWarning(device, kVUID_BestPractices_ImageMemReqNotCalled,
761 "%s: Binding memory to %s but vkGetImageMemoryRequirements() has not been called on that image.",
762 api_name, report_data->FormatHandle(image).c_str());
763 }
764 } else {
765 // TODO If binding disjoint image then this needs to check that VkImagePlaneMemoryRequirementsInfo was called for each
766 // plane.
Camden Stocker8b798ab2019-09-03 10:33:28 -0600767 }
768
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700769 auto mem_state = Get<DEVICE_MEMORY_STATE>(memory);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000770
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600771 if (mem_state->alloc_info.allocationSize == image_state->requirements[0].size &&
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000772 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
773 skip |= LogPerformanceWarning(
774 device, kVUID_BestPractices_SmallDedicatedAllocation,
775 "%s: Trying to bind %s to a memory block which is fully consumed by the image. "
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600776 "The required size of the allocation is %" PRIu64 ", but smaller images like this should be sub-allocated from "
777 "larger memory blocks. (Current threshold is %" PRIu64 " bytes.)",
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000778 api_name, report_data->FormatHandle(image).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
779 }
780
781 // If we're binding memory to a image which was created as TRANSIENT and the image supports LAZY allocation,
782 // make sure this type is actually used.
783 // This warning will only trigger if this layer is run on a platform that supports LAZILY_ALLOCATED_BIT
784 // (i.e.most tile - based renderers)
785 if (image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {
786 bool supports_lazy = false;
787 uint32_t suggested_type = 0;
788
789 for (uint32_t i = 0; i < phys_dev_mem_props.memoryTypeCount; i++) {
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600790 if ((1u << i) & image_state->requirements[0].memoryTypeBits) {
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000791 if (phys_dev_mem_props.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
792 supports_lazy = true;
793 suggested_type = i;
794 break;
795 }
796 }
797 }
798
799 uint32_t allocated_properties = phys_dev_mem_props.memoryTypes[mem_state->alloc_info.memoryTypeIndex].propertyFlags;
800
801 if (supports_lazy && (allocated_properties & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) == 0) {
802 skip |= LogPerformanceWarning(
803 device, kVUID_BestPractices_NonLazyTransientImage,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600804 "%s: Attempting to bind memory type %u to VkImage which was created with TRANSIENT_ATTACHMENT_BIT,"
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000805 "but this memory type is not LAZILY_ALLOCATED_BIT. You should use memory type %u here instead to save "
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600806 "%" PRIu64 " bytes of physical memory.",
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600807 api_name, mem_state->alloc_info.memoryTypeIndex, suggested_type, image_state->requirements[0].size);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000808 }
809 }
810
Camden Stocker8b798ab2019-09-03 10:33:28 -0600811 return skip;
812}
813
814bool BestPractices::PreCallValidateBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500815 VkDeviceSize memoryOffset) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600816 bool skip = false;
817 const char* api_name = "vkBindImageMemory()";
818
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000819 skip |= ValidateBindImageMemory(image, memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600820
821 return skip;
822}
823
824bool BestPractices::PreCallValidateBindImageMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500825 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600826 char api_name[64];
827 bool skip = false;
828
829 for (uint32_t i = 0; i < bindInfoCount; i++) {
830 sprintf(api_name, "vkBindImageMemory2() pBindInfos[%u]", i);
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700831 if (!LvlFindInChain<VkBindImageMemorySwapchainInfoKHR>(pBindInfos[i].pNext)) {
Tony-LunarG5e60b852020-04-27 11:27:54 -0600832 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
833 }
Camden Stocker8b798ab2019-09-03 10:33:28 -0600834 }
835
836 return skip;
837}
838
839bool BestPractices::PreCallValidateBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500840 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600841 char api_name[64];
842 bool skip = false;
843
844 for (uint32_t i = 0; i < bindInfoCount; i++) {
845 sprintf(api_name, "vkBindImageMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000846 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600847 }
848
849 return skip;
850}
Camden83a9c372019-08-14 11:41:38 -0600851
Attilio Provenzano02859b22020-02-27 14:17:28 +0000852static inline bool FormatHasFullThroughputBlendingArm(VkFormat format) {
853 switch (format) {
854 case VK_FORMAT_B10G11R11_UFLOAT_PACK32:
855 case VK_FORMAT_R16_SFLOAT:
856 case VK_FORMAT_R16G16_SFLOAT:
857 case VK_FORMAT_R16G16B16_SFLOAT:
858 case VK_FORMAT_R16G16B16A16_SFLOAT:
859 case VK_FORMAT_R32_SFLOAT:
860 case VK_FORMAT_R32G32_SFLOAT:
861 case VK_FORMAT_R32G32B32_SFLOAT:
862 case VK_FORMAT_R32G32B32A32_SFLOAT:
863 return false;
864
865 default:
866 return true;
867 }
868}
869
870bool BestPractices::ValidateMultisampledBlendingArm(uint32_t createInfoCount,
871 const VkGraphicsPipelineCreateInfo* pCreateInfos) const {
872 bool skip = false;
873
874 for (uint32_t i = 0; i < createInfoCount; i++) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700875 auto create_info = &pCreateInfos[i];
Attilio Provenzano02859b22020-02-27 14:17:28 +0000876
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700877 if (!create_info->pColorBlendState || !create_info->pMultisampleState ||
878 create_info->pMultisampleState->rasterizationSamples == VK_SAMPLE_COUNT_1_BIT ||
879 create_info->pMultisampleState->sampleShadingEnable) {
Attilio Provenzano02859b22020-02-27 14:17:28 +0000880 return skip;
881 }
882
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600883 auto rp_state = Get<RENDER_PASS_STATE>(create_info->renderPass);
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200884 const auto& subpass = rp_state->createInfo.pSubpasses[create_info->subpass];
Attilio Provenzano02859b22020-02-27 14:17:28 +0000885
Hans-Kristian Arntzenc2742e72021-07-01 14:31:06 +0200886 // According to spec, pColorBlendState must be ignored if subpass does not have color attachments.
887 uint32_t num_color_attachments = std::min(subpass.colorAttachmentCount, create_info->pColorBlendState->attachmentCount);
888
889 for (uint32_t j = 0; j < num_color_attachments; j++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200890 const auto& blend_att = create_info->pColorBlendState->pAttachments[j];
Attilio Provenzano02859b22020-02-27 14:17:28 +0000891 uint32_t att = subpass.pColorAttachments[j].attachment;
892
893 if (att != VK_ATTACHMENT_UNUSED && blend_att.blendEnable && blend_att.colorWriteMask) {
894 if (!FormatHasFullThroughputBlendingArm(rp_state->createInfo.pAttachments[att].format)) {
895 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultisampledBlending,
896 "%s vkCreateGraphicsPipelines() - createInfo #%u: Pipeline is multisampled and "
897 "color attachment #%u makes use "
898 "of a format which cannot be blended at full throughput when using MSAA.",
899 VendorSpecificTag(kBPVendorArm), i, j);
900 }
901 }
902 }
903 }
904
905 return skip;
906}
907
Nadav Gevaf0808442021-05-21 13:51:25 -0400908void BestPractices::ManualPostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
909 const VkComputePipelineCreateInfo* pCreateInfos,
910 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
911 VkResult result, void* pipe_state) {
912 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700913 pipeline_cache_ = pipelineCache;
Nadav Gevaf0808442021-05-21 13:51:25 -0400914}
915
Camden5b184be2019-08-13 07:50:19 -0600916bool BestPractices::PreCallValidateCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
917 const VkGraphicsPipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600918 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500919 void* cgpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600920 bool skip = StateTracker::PreCallValidateCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos,
921 pAllocator, pPipelines, cgpl_state_data);
ziga-lunarg08c81582022-03-08 17:33:45 +0100922 if (skip) {
923 return skip;
924 }
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600925 create_graphics_pipeline_api_state* cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state*>(cgpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600926
927 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700928 skip |= LogPerformanceWarning(
929 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
930 "Performance Warning: This vkCreateGraphicsPipelines call is creating multiple pipelines but is not using a "
931 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -0600932 }
933
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000934 for (uint32_t i = 0; i < createInfoCount; i++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200935 const auto& create_info = pCreateInfos[i];
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000936
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600937 if (!(cgpl_state->pipe_state[i]->active_shaders & VK_SHADER_STAGE_MESH_BIT_NV)) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200938 const auto& vertex_input = *create_info.pVertexInputState;
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600939 uint32_t count = 0;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700940 for (uint32_t j = 0; j < vertex_input.vertexBindingDescriptionCount; j++) {
941 if (vertex_input.pVertexBindingDescriptions[j].inputRate == VK_VERTEX_INPUT_RATE_INSTANCE) {
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600942 count++;
943 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000944 }
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600945 if (count > kMaxInstancedVertexBuffers) {
946 skip |= LogPerformanceWarning(
947 device, kVUID_BestPractices_CreatePipelines_TooManyInstancedVertexBuffers,
948 "The pipeline is using %u instanced vertex buffers (current limit: %u), but this can be inefficient on the "
949 "GPU. If using instanced vertex attributes prefer interleaving them in a single buffer.",
950 count, kMaxInstancedVertexBuffers);
951 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000952 }
Attilio Provenzano02859b22020-02-27 14:17:28 +0000953
Szilard Pappaaf2da32020-06-22 10:37:35 +0100954 if ((pCreateInfos[i].pRasterizationState->depthBiasEnable) &&
955 (pCreateInfos[i].pRasterizationState->depthBiasConstantFactor == 0.0f) &&
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +0200956 (pCreateInfos[i].pRasterizationState->depthBiasSlopeFactor == 0.0f) &&
957 VendorCheckEnabled(kBPVendorArm)) {
958 skip |= LogPerformanceWarning(
959 device, kVUID_BestPractices_CreatePipelines_DepthBias_Zero,
960 "%s Performance Warning: This vkCreateGraphicsPipelines call is created with depthBiasEnable set to true "
961 "and both depthBiasConstantFactor and depthBiasSlopeFactor are set to 0. This can cause reduced "
962 "efficiency during rasterization. Consider disabling depthBias or increasing either "
963 "depthBiasConstantFactor or depthBiasSlopeFactor.",
964 VendorSpecificTag(kBPVendorArm));
Szilard Pappaaf2da32020-06-22 10:37:35 +0100965 }
966
Attilio Provenzano02859b22020-02-27 14:17:28 +0000967 skip |= VendorCheckEnabled(kBPVendorArm) && ValidateMultisampledBlendingArm(createInfoCount, pCreateInfos);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000968 }
Nadav Gevaf0808442021-05-21 13:51:25 -0400969 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700970 auto prev_pipeline = pipeline_cache_.load();
971 if (pipelineCache && prev_pipeline && pipelineCache != prev_pipeline) {
Nadav Gevaf0808442021-05-21 13:51:25 -0400972 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultiplePipelineCaches,
973 "%s Performance Warning: A second pipeline cache is in use. Consider using only one pipeline cache to "
974 "improve cache hit rate", VendorSpecificTag(kBPVendorAMD));
975 }
976
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700977 if (num_pso_ > kMaxRecommendedNumberOfPSOAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -0400978 skip |=
979 LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_TooManyPipelines,
980 "%s Performance warning: Too many pipelines created, consider consolidation",
981 VendorSpecificTag(kBPVendorAMD));
982 }
983
Nathaniel Cesario1a7e1a92021-08-30 14:34:20 -0600984 if (pCreateInfos->pInputAssemblyState && pCreateInfos->pInputAssemblyState->primitiveRestartEnable) {
Nadav Gevaf0808442021-05-21 13:51:25 -0400985 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_AvoidPrimitiveRestart,
986 "%s Performance warning: Use of primitive restart is not recommended",
987 VendorSpecificTag(kBPVendorAMD));
988 }
989
990 // TODO: this might be too aggressive of a check
991 if (pCreateInfos->pDynamicState && pCreateInfos->pDynamicState->dynamicStateCount > kDynamicStatesWarningLimitAMD) {
992 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MinimizeNumDynamicStates,
993 "%s Performance warning: Dynamic States usage incurs a performance cost. Ensure that they are truly needed",
994 VendorSpecificTag(kBPVendorAMD));
995 }
996 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000997
Camden5b184be2019-08-13 07:50:19 -0600998 return skip;
999}
1000
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001001static std::vector<bp_state::AttachmentInfo> GetAttachmentAccess(const safe_VkGraphicsPipelineCreateInfo& create_info,
1002 std::shared_ptr<const RENDER_PASS_STATE>& rp) {
1003 std::vector<bp_state::AttachmentInfo> result;
Tony-LunarG5e1dbcd2022-03-29 08:19:58 -06001004 if (!rp || rp->use_dynamic_rendering || rp->use_dynamic_rendering_inherited) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001005 return result;
Hans-Kristian Arntzenb033ab12021-06-16 11:16:59 +02001006 }
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001007
1008 const auto& subpass = rp->createInfo.pSubpasses[create_info.subpass];
1009
1010 // NOTE: see PIPELINE_LAYOUT and safe_VkGraphicsPipelineCreateInfo constructors. pColorBlendState and pDepthStencilState
1011 // are only non-null if they are enabled.
1012 if (create_info.pColorBlendState) {
1013 // According to spec, pColorBlendState must be ignored if subpass does not have color attachments.
1014 uint32_t num_color_attachments = std::min(subpass.colorAttachmentCount, create_info.pColorBlendState->attachmentCount);
1015 for (uint32_t j = 0; j < num_color_attachments; j++) {
1016 if (create_info.pColorBlendState->pAttachments[j].colorWriteMask != 0) {
1017 uint32_t attachment = subpass.pColorAttachments[j].attachment;
1018 if (attachment != VK_ATTACHMENT_UNUSED) {
1019 result.push_back({attachment, VK_IMAGE_ASPECT_COLOR_BIT});
1020 }
1021 }
1022 }
1023 }
1024
1025 if (create_info.pDepthStencilState &&
1026 (create_info.pDepthStencilState->depthTestEnable || create_info.pDepthStencilState->depthBoundsTestEnable ||
1027 create_info.pDepthStencilState->stencilTestEnable)) {
1028 uint32_t attachment = subpass.pDepthStencilAttachment ? subpass.pDepthStencilAttachment->attachment : VK_ATTACHMENT_UNUSED;
1029 if (attachment != VK_ATTACHMENT_UNUSED) {
1030 VkImageAspectFlags aspects = 0;
1031 if (create_info.pDepthStencilState->depthTestEnable || create_info.pDepthStencilState->depthBoundsTestEnable) {
1032 aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
1033 }
1034 if (create_info.pDepthStencilState->stencilTestEnable) {
1035 aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
1036 }
1037 result.push_back({attachment, aspects});
1038 }
1039 }
1040 return result;
1041}
1042
1043bp_state::Pipeline::Pipeline(const ValidationStateTracker* state_data, const VkGraphicsPipelineCreateInfo* pCreateInfo,
1044 std::shared_ptr<const RENDER_PASS_STATE>&& rpstate,
1045 std::shared_ptr<const PIPELINE_LAYOUT_STATE>&& layout)
1046 : PIPELINE_STATE(state_data, pCreateInfo, std::move(rpstate), std::move(layout)),
1047 access_framebuffer_attachments(GetAttachmentAccess(create_info.graphics, rp_state)) {}
1048
1049std::shared_ptr<PIPELINE_STATE> BestPractices::CreateGraphicsPipelineState(
1050 const VkGraphicsPipelineCreateInfo* pCreateInfo, std::shared_ptr<const RENDER_PASS_STATE>&& render_pass,
1051 std::shared_ptr<const PIPELINE_LAYOUT_STATE>&& layout) const {
1052 return std::static_pointer_cast<PIPELINE_STATE>(
1053 std::make_shared<bp_state::Pipeline>(this, pCreateInfo, std::move(render_pass), std::move(layout)));
Hans-Kristian Arntzenb033ab12021-06-16 11:16:59 +02001054}
1055
Sam Walls0961ec02020-03-31 16:39:15 +01001056void BestPractices::ManualPostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
1057 const VkGraphicsPipelineCreateInfo* pCreateInfos,
1058 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
1059 VkResult result, void* cgpl_state_data) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001060 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001061 pipeline_cache_ = pipelineCache;
Sam Walls0961ec02020-03-31 16:39:15 +01001062}
1063
Camden5b184be2019-08-13 07:50:19 -06001064bool BestPractices::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
1065 const VkComputePipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -06001066 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001067 void* ccpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -06001068 bool skip = StateTracker::PreCallValidateCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos,
1069 pAllocator, pPipelines, ccpl_state_data);
Camden5b184be2019-08-13 07:50:19 -06001070
1071 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07001072 skip |= LogPerformanceWarning(
1073 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1074 "Performance Warning: This vkCreateComputePipelines call is creating multiple pipelines but is not using a "
1075 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -06001076 }
1077
Nadav Gevaf0808442021-05-21 13:51:25 -04001078 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001079 auto prev_pipeline = pipeline_cache_.load();
1080 if (pipelineCache && prev_pipeline && pipelineCache != prev_pipeline) {
1081 skip |= LogPerformanceWarning(
1082 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1083 "%s Performance Warning: A second pipeline cache is in use. Consider using only one pipeline cache to "
Nadav Gevaf0808442021-05-21 13:51:25 -04001084 "improve cache hit rate",
1085 VendorSpecificTag(kBPVendorAMD));
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001086 }
1087 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001088
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001089 for (uint32_t i = 0; i < createInfoCount; i++) {
1090 const VkComputePipelineCreateInfo& createInfo = pCreateInfos[i];
1091 if (VendorCheckEnabled(kBPVendorArm)) {
1092 skip |= ValidateCreateComputePipelineArm(createInfo);
1093 }
sfricke-samsung86d055a2022-02-11 14:43:50 -08001094
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001095 if (IsExtEnabled(device_extensions.vk_khr_maintenance4)) {
1096 auto module_state = Get<SHADER_MODULE_STATE>(createInfo.stage.module);
1097 for (const auto& builtin : module_state->static_data_.builtin_decoration_list) {
1098 if (builtin.builtin == spv::BuiltInWorkgroupSize) {
1099 skip |= LogWarning(device, kVUID_BestPractices_SpirvDeprecated_WorkgroupSize,
1100 "vkCreateComputePipelines(): pCreateInfos[ %" PRIu32
1101 "] is using the Workgroup built-in which SPIR-V 1.6 deprecated. The VK_KHR_maintenance4 "
1102 "extension exposes a new LocalSizeId execution mode that should be used instead.",
1103 i);
sfricke-samsung86d055a2022-02-11 14:43:50 -08001104 }
1105 }
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001106 }
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001107 }
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001108
1109 return skip;
1110}
1111
1112bool BestPractices::ValidateCreateComputePipelineArm(const VkComputePipelineCreateInfo& createInfo) const {
1113 bool skip = false;
sfricke-samsungef15e482022-01-26 11:32:49 -08001114 auto module_state = Get<SHADER_MODULE_STATE>(createInfo.stage.module);
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001115 // Generate warnings about work group sizes based on active resources.
sfricke-samsungef15e482022-01-26 11:32:49 -08001116 auto entrypoint = module_state->FindEntrypoint(createInfo.stage.pName, createInfo.stage.stage);
1117 if (entrypoint == module_state->end()) return false;
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001118
1119 uint32_t x = 1, y = 1, z = 1;
sfricke-samsungef15e482022-01-26 11:32:49 -08001120 module_state->FindLocalSize(entrypoint, x, y, z);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001121
1122 uint32_t thread_count = x * y * z;
1123
1124 // Generate a priori warnings about work group sizes.
1125 if (thread_count > kMaxEfficientWorkGroupThreadCountArm) {
1126 skip |= LogPerformanceWarning(
1127 device, kVUID_BestPractices_CreateComputePipelines_ComputeWorkGroupSize,
1128 "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, %u, "
1129 "%u) (%u threads total), has more threads than advised in a single work group. It is advised to use work "
1130 "groups with less than %u threads, especially when using barrier() or shared memory.",
1131 VendorSpecificTag(kBPVendorArm), x, y, z, thread_count, kMaxEfficientWorkGroupThreadCountArm);
1132 }
1133
1134 if (thread_count == 1 || ((x > 1) && (x & (kThreadGroupDispatchCountAlignmentArm - 1))) ||
1135 ((y > 1) && (y & (kThreadGroupDispatchCountAlignmentArm - 1))) ||
1136 ((z > 1) && (z & (kThreadGroupDispatchCountAlignmentArm - 1)))) {
1137 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeThreadGroupAlignment,
1138 "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, "
1139 "%u, %u) is not aligned to %u "
1140 "threads. On Arm Mali architectures, not aligning work group sizes to %u may "
1141 "leave threads idle on the shader "
1142 "core.",
1143 VendorSpecificTag(kBPVendorArm), x, y, z, kThreadGroupDispatchCountAlignmentArm,
1144 kThreadGroupDispatchCountAlignmentArm);
1145 }
1146
sfricke-samsungef15e482022-01-26 11:32:49 -08001147 auto accessible_ids = module_state->MarkAccessibleIds(entrypoint);
1148 auto descriptor_uses = module_state->CollectInterfaceByDescriptorSlot(accessible_ids);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001149
1150 unsigned dimensions = 0;
1151 if (x > 1) dimensions++;
1152 if (y > 1) dimensions++;
1153 if (z > 1) dimensions++;
1154 // Here the dimension will really depend on the dispatch grid, but assume it's 1D.
1155 dimensions = std::max(dimensions, 1u);
1156
1157 // If we're accessing images, we almost certainly want to have a 2D workgroup for cache reasons.
1158 // There are some false positives here. We could simply have a shader that does this within a 1D grid,
1159 // or we may have a linearly tiled image, but these cases are quite unlikely in practice.
1160 bool accesses_2d = false;
1161 for (const auto& usage : descriptor_uses) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001162 auto dim = module_state->GetShaderResourceDimensionality(usage.second);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001163 if (dim < 0) continue;
1164 auto spvdim = spv::Dim(dim);
1165 if (spvdim != spv::Dim1D && spvdim != spv::DimBuffer) accesses_2d = true;
1166 }
1167
1168 if (accesses_2d && dimensions < 2) {
1169 LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeSpatialLocality,
1170 "%s vkCreateComputePipelines(): compute shader has work group dimensions (%u, %u, %u), which "
1171 "suggests a 1D dispatch, but the shader is accessing 2D or 3D images. The shader may be "
1172 "exhibiting poor spatial locality with respect to one or more shader resources.",
1173 VendorSpecificTag(kBPVendorArm), x, y, z);
1174 }
1175
Camden5b184be2019-08-13 07:50:19 -06001176 return skip;
1177}
1178
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001179bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags flags) const {
Camden5b184be2019-08-13 07:50:19 -06001180 bool skip = false;
1181
1182 if (flags & VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001183 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1184 "You are using VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -06001185 } else if (flags & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001186 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1187 "You are using VK_PIPELINE_STAGE_ALL_COMMANDS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -06001188 }
1189
1190 return skip;
1191}
1192
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001193bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags2KHR flags) const {
1194 bool skip = false;
1195
1196 if (flags & VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR) {
1197 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1198 "You are using VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR when %s is called\n", api_name.c_str());
1199 } else if (flags & VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR) {
1200 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1201 "You are using VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR when %s is called\n", api_name.c_str());
1202 }
1203
1204 return skip;
1205}
1206
1207bool BestPractices::CheckDependencyInfo(const std::string& api_name, const VkDependencyInfoKHR& dep_info) const {
1208 bool skip = false;
1209 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
1210
1211 skip |= CheckPipelineStageFlags(api_name, stage_masks.src);
1212 skip |= CheckPipelineStageFlags(api_name, stage_masks.dst);
1213
1214 return skip;
1215}
1216
Mark Lobodzinski84101d72020-04-24 09:43:48 -06001217void BestPractices::ManualPostCallRecordQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo, VkResult result) {
Mark Lobodzinski9b133c12020-03-10 10:42:56 -06001218 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; ++i) {
1219 auto swapchains_result = pPresentInfo->pResults ? pPresentInfo->pResults[i] : result;
1220 if (swapchains_result == VK_SUBOPTIMAL_KHR) {
1221 LogPerformanceWarning(
1222 pPresentInfo->pSwapchains[i], kVUID_BestPractices_SuboptimalSwapchain,
1223 "vkQueuePresentKHR: %s :VK_SUBOPTIMAL_KHR was returned. VK_SUBOPTIMAL_KHR - Presentation will still succeed, "
1224 "subject to the window resize behavior, but the swapchain is no longer configured optimally for the surface it "
1225 "targets. Applications should query updated surface information and recreate their swapchain at the next "
1226 "convenient opportunity.",
1227 report_data->FormatHandle(pPresentInfo->pSwapchains[i]).c_str());
1228 }
1229 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001230
1231 // AMD best practice
1232 // end-of-frame cleanup
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001233 num_queue_submissions_ = 0;
1234 num_barriers_objects_ = 0;
1235 ClearPipelinesUsedInFrame();
Mark Lobodzinski9b133c12020-03-10 10:42:56 -06001236}
1237
Jeff Bolz5c801d12019-10-09 10:38:45 -05001238bool BestPractices::PreCallValidateQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
1239 VkFence fence) const {
Camden5b184be2019-08-13 07:50:19 -06001240 bool skip = false;
1241
1242 for (uint32_t submit = 0; submit < submitCount; submit++) {
1243 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreCount; semaphore++) {
1244 skip |= CheckPipelineStageFlags("vkQueueSubmit", pSubmits[submit].pWaitDstStageMask[semaphore]);
1245 }
1246 }
1247
1248 return skip;
1249}
1250
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001251bool BestPractices::PreCallValidateQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR* pSubmits,
1252 VkFence fence) const {
1253 bool skip = false;
1254
1255 for (uint32_t submit = 0; submit < submitCount; submit++) {
1256 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) {
1257 skip |= CheckPipelineStageFlags("vkQueueSubmit2KHR", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask);
1258 }
1259 }
1260
1261 return skip;
1262}
1263
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001264bool BestPractices::PreCallValidateQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2* pSubmits,
1265 VkFence fence) const {
1266 bool skip = false;
1267
1268 for (uint32_t submit = 0; submit < submitCount; submit++) {
1269 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) {
1270 skip |= CheckPipelineStageFlags("vkQueueSubmit2", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask);
1271 }
1272 }
1273
1274 return skip;
1275}
1276
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001277bool BestPractices::PreCallValidateCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo,
1278 const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool) const {
1279 bool skip = false;
1280
1281 if (pCreateInfo->flags & VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT) {
1282 skip |= LogPerformanceWarning(
1283 device, kVUID_BestPractices_CreateCommandPool_CommandBufferReset,
1284 "vkCreateCommandPool(): VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT is set. Consider resetting entire "
1285 "pool instead.");
1286 }
1287
1288 return skip;
1289}
1290
1291bool BestPractices::PreCallValidateBeginCommandBuffer(VkCommandBuffer commandBuffer,
1292 const VkCommandBufferBeginInfo* pBeginInfo) const {
1293 bool skip = false;
1294
1295 if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
1296 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_SimultaneousUse,
1297 "vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT is set.");
1298 }
1299
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001300 if (!(pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT) && VendorCheckEnabled(kBPVendorArm)) {
1301 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_OneTimeSubmit,
Attilio Provenzano02859b22020-02-27 14:17:28 +00001302 "%s vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT is not set. "
1303 "For best performance on Mali GPUs, consider setting ONE_TIME_SUBMIT by default.",
1304 VendorSpecificTag(kBPVendorArm));
1305 }
1306
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001307 return skip;
1308}
1309
Jeff Bolz5c801d12019-10-09 10:38:45 -05001310bool BestPractices::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -06001311 bool skip = false;
1312
1313 skip |= CheckPipelineStageFlags("vkCmdSetEvent", stageMask);
1314
1315 return skip;
1316}
1317
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001318bool BestPractices::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
1319 const VkDependencyInfoKHR* pDependencyInfo) const {
1320 return CheckDependencyInfo("vkCmdSetEvent2KHR", *pDependencyInfo);
1321}
1322
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001323bool BestPractices::PreCallValidateCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
1324 const VkDependencyInfo* pDependencyInfo) const {
1325 return CheckDependencyInfo("vkCmdSetEvent2", *pDependencyInfo);
1326}
1327
Jeff Bolz5c801d12019-10-09 10:38:45 -05001328bool BestPractices::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
1329 VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -06001330 bool skip = false;
1331
1332 skip |= CheckPipelineStageFlags("vkCmdResetEvent", stageMask);
1333
1334 return skip;
1335}
1336
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001337bool BestPractices::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
1338 VkPipelineStageFlags2KHR stageMask) const {
1339 bool skip = false;
1340
1341 skip |= CheckPipelineStageFlags("vkCmdResetEvent2KHR", stageMask);
1342
1343 return skip;
1344}
1345
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001346bool BestPractices::PreCallValidateCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
1347 VkPipelineStageFlags2 stageMask) const {
1348 bool skip = false;
1349
1350 skip |= CheckPipelineStageFlags("vkCmdResetEvent2", stageMask);
1351
1352 return skip;
1353}
1354
Camden5b184be2019-08-13 07:50:19 -06001355bool BestPractices::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1356 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
1357 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
1358 uint32_t bufferMemoryBarrierCount,
1359 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1360 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001361 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -06001362 bool skip = false;
1363
1364 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", srcStageMask);
1365 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", dstStageMask);
1366
1367 return skip;
1368}
1369
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001370bool BestPractices::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1371 const VkDependencyInfoKHR* pDependencyInfos) const {
1372 bool skip = false;
1373 for (uint32_t i = 0; i < eventCount; i++) {
1374 skip = CheckDependencyInfo("vkCmdWaitEvents2KHR", pDependencyInfos[i]);
1375 }
1376
1377 return skip;
1378}
1379
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001380bool BestPractices::PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1381 const VkDependencyInfo* pDependencyInfos) const {
1382 bool skip = false;
1383 for (uint32_t i = 0; i < eventCount; i++) {
1384 skip = CheckDependencyInfo("vkCmdWaitEvents2", pDependencyInfos[i]);
1385 }
1386
1387 return skip;
1388}
1389
Camden5b184be2019-08-13 07:50:19 -06001390bool BestPractices::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1391 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1392 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
1393 uint32_t bufferMemoryBarrierCount,
1394 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1395 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001396 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -06001397 bool skip = false;
1398
1399 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", srcStageMask);
1400 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", dstStageMask);
1401
ziga-lunargb65dbfb2022-03-19 18:45:09 +01001402 for (uint32_t i = 0; i < imageMemoryBarrierCount; ++i) {
1403 if (pImageMemoryBarriers[i].oldLayout == VK_IMAGE_LAYOUT_UNDEFINED &&
1404 IsImageLayoutReadOnly(pImageMemoryBarriers[i].newLayout)) {
1405 skip |= LogWarning(device, kVUID_BestPractices_TransitionUndefinedToReadOnly,
1406 "VkImageMemoryBarrier is being submitted with oldLayout VK_IMAGE_LAYOUT_UNDEFINED and the contents "
1407 "may be discarded, but the newLayout is %s, which is read only.",
1408 string_VkImageLayout(pImageMemoryBarriers[i].newLayout));
1409 }
1410 }
1411
Nadav Gevaf0808442021-05-21 13:51:25 -04001412 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001413 auto num = num_barriers_objects_.load();
1414 if (num + imageMemoryBarrierCount + bufferMemoryBarrierCount > kMaxRecommendedBarriersSizeAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001415 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_highBarrierCount,
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001416 "%s Performance warning: In this frame, %" PRIu32
1417 " barriers were already submitted. Barriers have a high cost and can "
1418 "stall the GPU. "
1419 "Consider consolidating and re-organizing the frame to use fewer barriers.",
1420 VendorSpecificTag(kBPVendorAMD), num);
Nadav Gevaf0808442021-05-21 13:51:25 -04001421 }
1422
1423 std::array<VkImageLayout, 3> read_layouts = {
1424 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
1425 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
1426 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1427 };
1428
1429 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
1430 // read to read barriers
1431 auto found = std::find(read_layouts.begin(), read_layouts.end(), pImageMemoryBarriers[i].oldLayout);
1432 bool old_is_read_layout = found != read_layouts.end();
1433 found = std::find(read_layouts.begin(), read_layouts.end(), pImageMemoryBarriers[i].newLayout);
1434 bool new_is_read_layout = found != read_layouts.end();
1435 if (old_is_read_layout && new_is_read_layout) {
1436 skip |= LogPerformanceWarning(device, kVUID_BestPractices_PipelineBarrier_readToReadBarrier,
1437 "%s Performance warning: Don't issue read-to-read barriers. Get the resource in the right state the first "
1438 "time you use it.",
1439 VendorSpecificTag(kBPVendorAMD));
1440 }
1441
1442 // general with no storage
1443 if (pImageMemoryBarriers[i].newLayout == VK_IMAGE_LAYOUT_GENERAL) {
1444 auto image_state = Get<IMAGE_STATE>(pImageMemoryBarriers[i].image);
1445 if (!(image_state->createInfo.usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1446 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidGeneral,
1447 "%s Performance warning: VK_IMAGE_LAYOUT_GENERAL should only be used with "
1448 "VK_IMAGE_USAGE_STORAGE_BIT images.",
1449 VendorSpecificTag(kBPVendorAMD));
1450 }
1451 }
1452 }
1453 }
1454
Camden5b184be2019-08-13 07:50:19 -06001455 return skip;
1456}
1457
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001458bool BestPractices::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
1459 const VkDependencyInfoKHR* pDependencyInfo) const {
1460 return CheckDependencyInfo("vkCmdPipelineBarrier2KHR", *pDependencyInfo);
1461}
1462
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001463bool BestPractices::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer,
1464 const VkDependencyInfo* pDependencyInfo) const {
1465 return CheckDependencyInfo("vkCmdPipelineBarrier2", *pDependencyInfo);
1466}
1467
Camden5b184be2019-08-13 07:50:19 -06001468bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001469 VkQueryPool queryPool, uint32_t query) const {
Camden5b184be2019-08-13 07:50:19 -06001470 bool skip = false;
1471
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001472 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", static_cast<VkPipelineStageFlags>(pipelineStage));
1473
1474 return skip;
1475}
1476
1477bool BestPractices::PreCallValidateCmdWriteTimestamp2KHR(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
1478 VkQueryPool queryPool, uint32_t query) const {
1479 bool skip = false;
1480
1481 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2KHR", pipelineStage);
Camden5b184be2019-08-13 07:50:19 -06001482
1483 return skip;
1484}
1485
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001486bool BestPractices::PreCallValidateCmdWriteTimestamp2(VkCommandBuffer commandBuffer, VkPipelineStageFlags2 pipelineStage,
1487 VkQueryPool queryPool, uint32_t query) const {
1488 bool skip = false;
1489
1490 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2", pipelineStage);
1491
1492 return skip;
1493}
1494
Sam Walls0961ec02020-03-31 16:39:15 +01001495void BestPractices::PostCallRecordCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
1496 VkPipeline pipeline) {
1497 StateTracker::PostCallRecordCmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline);
1498
Nadav Gevaf0808442021-05-21 13:51:25 -04001499 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001500 PipelineUsedInFrame(pipeline);
Nadav Gevaf0808442021-05-21 13:51:25 -04001501
Sam Walls0961ec02020-03-31 16:39:15 +01001502 if (pipelineBindPoint == VK_PIPELINE_BIND_POINT_GRAPHICS) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001503 auto pipeline_state = Get<bp_state::Pipeline>(pipeline);
Sam Walls0961ec02020-03-31 16:39:15 +01001504 // check for depth/blend state tracking
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001505 if (pipeline_state) {
1506 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001507 assert(cb_node);
1508 auto& render_pass_state = cb_node->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01001509
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001510 render_pass_state.nextDrawTouchesAttachments = pipeline_state->access_framebuffer_attachments;
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001511 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02001512
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001513 const auto* blend_state = pipeline_state->ColorBlendState();
1514 const auto* stencil_state = pipeline_state->DepthStencilState();
Sam Walls0961ec02020-03-31 16:39:15 +01001515
1516 if (blend_state) {
1517 // assume the pipeline is depth-only unless any of the attachments have color writes enabled
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001518 render_pass_state.depthOnly = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001519 for (size_t i = 0; i < blend_state->attachmentCount; i++) {
1520 if (blend_state->pAttachments[i].colorWriteMask != 0) {
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001521 render_pass_state.depthOnly = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001522 }
1523 }
1524 }
1525
1526 // check for depth value usage
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001527 render_pass_state.depthEqualComparison = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001528
1529 if (stencil_state && stencil_state->depthTestEnable) {
1530 switch (stencil_state->depthCompareOp) {
1531 case VK_COMPARE_OP_EQUAL:
1532 case VK_COMPARE_OP_GREATER_OR_EQUAL:
1533 case VK_COMPARE_OP_LESS_OR_EQUAL:
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001534 render_pass_state.depthEqualComparison = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001535 break;
1536 default:
1537 break;
1538 }
1539 }
Sam Walls0961ec02020-03-31 16:39:15 +01001540 }
1541 }
1542}
1543
Hans-Kristian Arntzen237663c2021-07-01 14:36:40 +02001544static inline bool RenderPassUsesAttachmentAsResolve(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1545 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
1546 const auto& subpass_info = createInfo.pSubpasses[subpass];
1547 if (subpass_info.pResolveAttachments) {
1548 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1549 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
1550 }
1551 }
1552 }
1553
1554 return false;
1555}
1556
Attilio Provenzano02859b22020-02-27 14:17:28 +00001557static inline bool RenderPassUsesAttachmentOnTile(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1558 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001559 const auto& subpass_info = createInfo.pSubpasses[subpass];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001560
1561 // If an attachment is ever used as a color attachment,
1562 // resolve attachment or depth stencil attachment,
1563 // it needs to exist on tile at some point.
1564
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001565 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1566 if (subpass_info.pColorAttachments[i].attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001567 }
1568
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001569 if (subpass_info.pResolveAttachments) {
1570 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1571 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
1572 }
1573 }
1574
1575 if (subpass_info.pDepthStencilAttachment && subpass_info.pDepthStencilAttachment->attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001576 }
1577
1578 return false;
1579}
1580
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001581static inline bool RenderPassUsesAttachmentAsImageOnly(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1582 if (RenderPassUsesAttachmentOnTile(createInfo, attachment)) {
1583 return false;
1584 }
1585
1586 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001587 const auto& subpassInfo = createInfo.pSubpasses[subpass];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001588
1589 for (uint32_t i = 0; i < subpassInfo.inputAttachmentCount; i++) {
1590 if (subpassInfo.pInputAttachments[i].attachment == attachment) {
1591 return true;
1592 }
1593 }
1594 }
1595
1596 return false;
1597}
1598
Attilio Provenzano02859b22020-02-27 14:17:28 +00001599bool BestPractices::ValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
1600 const VkRenderPassBeginInfo* pRenderPassBegin) const {
1601 bool skip = false;
1602
1603 if (!pRenderPassBegin) {
1604 return skip;
1605 }
1606
Gareth Webbdc6549a2021-06-16 03:52:24 +01001607 if (pRenderPassBegin->renderArea.extent.width == 0 || pRenderPassBegin->renderArea.extent.height == 0) {
1608 skip |= LogWarning(device, kVUID_BestPractices_BeginRenderPass_ZeroSizeRenderArea,
1609 "This render pass has a zero-size render area. It cannot write to any attachments, "
1610 "and can only be used for side effects such as layout transitions.");
1611 }
1612
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001613 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001614 if (rp_state) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001615 if (rp_state->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07001616 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
Tony-LunarG767180f2020-04-23 14:03:59 -06001617 if (rpabi) {
1618 skip = ValidateAttachments(rp_state->createInfo.ptr(), rpabi->attachmentCount, rpabi->pAttachments);
1619 }
1620 }
Attilio Provenzano02859b22020-02-27 14:17:28 +00001621 // Check if any attachments have LOAD operation on them
1622 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001623 const auto& attachment = rp_state->createInfo.pAttachments[att];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001624
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001625 bool attachment_has_readback = false;
Hans-Kristian Arntzen4afb59b2021-06-18 12:41:36 +02001626 if (!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001627 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001628 }
1629
1630 if (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001631 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001632 }
1633
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001634 bool attachment_needs_readback = false;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001635
1636 // Check if the attachment is actually used in any subpass on-tile
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001637 if (attachment_has_readback && RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1638 attachment_needs_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001639 }
1640
1641 // Using LOAD_OP_LOAD is expensive on tiled GPUs, so flag it as a potential improvement
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001642 if (attachment_needs_readback && VendorCheckEnabled(kBPVendorArm)) {
1643 skip |= LogPerformanceWarning(
1644 device, kVUID_BestPractices_BeginRenderPass_AttachmentNeedsReadback,
1645 "%s Attachment #%u in render pass has begun with VK_ATTACHMENT_LOAD_OP_LOAD.\n"
1646 "Submitting this renderpass will cause the driver to inject a readback of the attachment "
Nadav Gevaf0808442021-05-21 13:51:25 -04001647 "which will copy in total %u pixels (renderArea = "
1648 "{ %" PRId32 ", %" PRId32 ", %" PRIu32", %" PRIu32 " }) to the tile buffer.",
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001649 VendorSpecificTag(kBPVendorArm), att,
1650 pRenderPassBegin->renderArea.extent.width * pRenderPassBegin->renderArea.extent.height,
1651 pRenderPassBegin->renderArea.offset.x, pRenderPassBegin->renderArea.offset.y,
1652 pRenderPassBegin->renderArea.extent.width, pRenderPassBegin->renderArea.extent.height);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001653 }
1654 }
1655 }
1656
1657 return skip;
1658}
1659
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001660void BestPractices::QueueValidateImageView(QueueCallbacks &funcs, const char* function_name,
1661 IMAGE_VIEW_STATE* view, IMAGE_SUBRESOURCE_USAGE_BP usage) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001662 if (view) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001663 auto image_state = std::static_pointer_cast<bp_state::Image>(view->image_state);
1664 QueueValidateImage(funcs, function_name, image_state, usage, view->normalized_subresource_range);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001665 }
1666}
1667
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001668void BestPractices::QueueValidateImage(QueueCallbacks& funcs, const char* function_name, std::shared_ptr<bp_state::Image>& state,
1669 IMAGE_SUBRESOURCE_USAGE_BP usage, const VkImageSubresourceRange& subresource_range) {
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001670 // If we're viewing a 3D slice, ignore base array layer.
1671 // The entire 3D subresource is accessed as one atomic unit.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001672 const uint32_t base_array_layer = state->createInfo.imageType == VK_IMAGE_TYPE_3D ? 0 : subresource_range.baseArrayLayer;
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001673
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001674 const uint32_t max_layers = state->createInfo.arrayLayers - base_array_layer;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001675 const uint32_t array_layers = std::min(subresource_range.layerCount, max_layers);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001676 const uint32_t max_levels = state->createInfo.mipLevels - subresource_range.baseMipLevel;
1677 const uint32_t mip_levels = std::min(state->createInfo.mipLevels, max_levels);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001678
1679 for (uint32_t layer = 0; layer < array_layers; layer++) {
1680 for (uint32_t level = 0; level < mip_levels; level++) {
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001681 QueueValidateImage(funcs, function_name, state, usage, layer + base_array_layer,
1682 level + subresource_range.baseMipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001683 }
1684 }
1685}
1686
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001687void BestPractices::QueueValidateImage(QueueCallbacks& funcs, const char* function_name, std::shared_ptr<bp_state::Image>& state,
1688 IMAGE_SUBRESOURCE_USAGE_BP usage, const VkImageSubresourceLayers& subresource_layers) {
1689 const uint32_t max_layers = state->createInfo.arrayLayers - subresource_layers.baseArrayLayer;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001690 const uint32_t array_layers = std::min(subresource_layers.layerCount, max_layers);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001691
1692 for (uint32_t layer = 0; layer < array_layers; layer++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001693 QueueValidateImage(funcs, function_name, state, usage, layer + subresource_layers.baseArrayLayer, subresource_layers.mipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001694 }
1695}
1696
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001697void BestPractices::QueueValidateImage(QueueCallbacks &funcs, const char* function_name,
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001698 std::shared_ptr<bp_state::Image> &state, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001699 uint32_t array_layer, uint32_t mip_level) {
Jeremy Gebben7fd238f2022-03-04 10:32:24 -07001700 if (VendorCheckEnabled(kBPVendorArm)) {
1701 funcs.push_back([this, function_name, state, usage, array_layer, mip_level](
1702 const ValidationStateTracker&, const QUEUE_STATE&, const CMD_BUFFER_STATE&) -> bool {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001703 ValidateImageInQueue(function_name, *state, usage, array_layer, mip_level);
Jeremy Gebben7fd238f2022-03-04 10:32:24 -07001704 return false;
1705 });
1706 }
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001707}
1708
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001709void BestPractices::ValidateImageInQueueArm(const char* function_name, const bp_state::Image& image,
1710 IMAGE_SUBRESOURCE_USAGE_BP last_usage, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001711 uint32_t array_layer, uint32_t mip_level) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001712 // Swapchain images are implicitly read so clear after store is expected.
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001713 if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED && last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_STORED &&
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001714 !image.IsSwapchainImage()) {
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001715 LogPerformanceWarning(
1716 device, kVUID_BestPractices_RenderPass_RedundantStore,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001717 "%s: %s Subresource (arrayLayer: %u, mipLevel: %u) of image was cleared as part of LOAD_OP_CLEAR, but last time "
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001718 "image was used, it was written to with STORE_OP_STORE. "
1719 "Storing to the image is probably redundant in this case, and wastes bandwidth on tile-based "
1720 "architectures.",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001721 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level);
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001722 } else if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED && last_usage == IMAGE_SUBRESOURCE_USAGE_BP::CLEARED) {
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001723 LogPerformanceWarning(
1724 device, kVUID_BestPractices_RenderPass_RedundantClear,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001725 "%s: %s Subresource (arrayLayer: %u, mipLevel: %u) of image was cleared as part of LOAD_OP_CLEAR, but last time "
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001726 "image was used, it was written to with vkCmdClear*Image(). "
1727 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
1728 "tile-based architectures."
1729 "architectures.",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001730 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level);
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001731 } else if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE &&
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001732 (last_usage == IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE || last_usage == IMAGE_SUBRESOURCE_USAGE_BP::CLEARED ||
1733 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE || last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE)) {
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001734 const char *last_cmd = nullptr;
1735 const char *vuid = nullptr;
1736 const char *suggestion = nullptr;
1737
1738 switch (last_usage) {
1739 case IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE:
1740 vuid = kVUID_BestPractices_RenderPass_BlitImage_LoadOpLoad;
1741 last_cmd = "vkCmdBlitImage";
1742 suggestion =
1743 "The blit is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
1744 "Rather than blitting, just render the source image in a fragment shader in this render pass, "
1745 "which avoids the memory roundtrip.";
1746 break;
1747 case IMAGE_SUBRESOURCE_USAGE_BP::CLEARED:
1748 vuid = kVUID_BestPractices_RenderPass_InefficientClear;
1749 last_cmd = "vkCmdClear*Image";
1750 suggestion =
1751 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
1752 "tile-based architectures. "
1753 "Use LOAD_OP_CLEAR instead to clear the image for free.";
1754 break;
1755 case IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE:
1756 vuid = kVUID_BestPractices_RenderPass_CopyImage_LoadOpLoad;
1757 last_cmd = "vkCmdCopy*Image";
1758 suggestion =
1759 "The copy is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
1760 "Rather than copying, just render the source image in a fragment shader in this render pass, "
1761 "which avoids the memory roundtrip.";
1762 break;
1763 case IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE:
1764 vuid = kVUID_BestPractices_RenderPass_ResolveImage_LoadOpLoad;
1765 last_cmd = "vkCmdResolveImage";
1766 suggestion =
1767 "The resolve is probably redundant in this case, and wastes a lot of bandwidth on tile-based architectures. "
1768 "Rather than resolving, and then loading, try to keep rendering in the same render pass, "
1769 "which avoids the memory roundtrip.";
1770 break;
1771 default:
1772 break;
1773 }
1774
1775 LogPerformanceWarning(
1776 device, vuid,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001777 "%s: %s Subresource (arrayLayer: %u, mipLevel: %u) of image was loaded to tile as part of LOAD_OP_LOAD, but last "
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001778 "time image was used, it was written to with %s. %s",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001779 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level, last_cmd, suggestion);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001780 }
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001781}
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001782
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001783void BestPractices::ValidateImageInQueue(const char* function_name, bp_state::Image& state, IMAGE_SUBRESOURCE_USAGE_BP usage,
1784 uint32_t array_layer, uint32_t mip_level) {
1785 auto last_usage = state.UpdateUsage(array_layer, mip_level, usage);
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001786 if (VendorCheckEnabled(kBPVendorArm)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001787 ValidateImageInQueueArm(function_name, state, last_usage, usage, array_layer, mip_level);
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001788 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001789}
1790
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001791void BestPractices::AddDeferredQueueOperations(bp_state::CommandBuffer& cb) {
1792 cb.queue_submit_functions.insert(cb.queue_submit_functions.end(), cb.queue_submit_functions_after_render_pass.begin(),
1793 cb.queue_submit_functions_after_render_pass.end());
1794 cb.queue_submit_functions_after_render_pass.clear();
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001795}
1796
1797void BestPractices::PreCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
1798 ValidationStateTracker::PreCallRecordCmdEndRenderPass(commandBuffer);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001799 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
1800 if (cb_node) {
1801 AddDeferredQueueOperations(*cb_node);
1802 }
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001803}
1804
1805void BestPractices::PreCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassInfo) {
1806 ValidationStateTracker::PreCallRecordCmdEndRenderPass2(commandBuffer, pSubpassInfo);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001807 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
1808 if (cb_node) {
1809 AddDeferredQueueOperations(*cb_node);
1810 }
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001811}
1812
1813void BestPractices::PreCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassInfo) {
1814 ValidationStateTracker::PreCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassInfo);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001815 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
1816 if (cb_node) {
1817 AddDeferredQueueOperations(*cb_node);
1818 }
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001819}
1820
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02001821void BestPractices::PreCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer,
1822 const VkRenderPassBeginInfo* pRenderPassBegin,
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001823 VkSubpassContents contents) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001824 ValidationStateTracker::PreCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02001825 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1826}
1827
1828void BestPractices::PreCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer,
1829 const VkRenderPassBeginInfo* pRenderPassBegin,
1830 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1831 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1832 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1833}
1834
1835void BestPractices::PreCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1836 const VkRenderPassBeginInfo* pRenderPassBegin,
1837 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1838 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1839 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1840}
1841
1842void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001843
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001844 if (!pRenderPassBegin) {
1845 return;
1846 }
1847
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001848 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001849
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001850 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001851 if (rp_state) {
1852 // Check load ops
1853 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001854 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001855
1856 if (!RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att) &&
1857 !RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1858 continue;
1859 }
1860
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001861 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::UNDEFINED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001862
1863 if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) ||
1864 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001865 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02001866 } else if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) ||
1867 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_CLEAR)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001868 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02001869 } else if (RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att)) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001870 usage = IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001871 }
1872
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001873 auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001874 std::shared_ptr<IMAGE_VIEW_STATE> image_view = nullptr;
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001875
Tony-LunarGb3ab3572021-07-02 09:45:17 -06001876 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001877 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
1878 if (rpabi) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001879 image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001880 }
1881 } else {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001882 image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001883 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001884
Jeremy Gebben9f537102021-10-05 16:37:12 -06001885 QueueValidateImageView(cb->queue_submit_functions, "vkCmdBeginRenderPass()", image_view.get(), usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001886 }
1887
1888 // Check store ops
1889 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001890 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001891
1892 if (!RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1893 continue;
1894 }
1895
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001896 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_DISCARDED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001897
1898 if ((!FormatIsStencilOnly(attachment.format) && attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE) ||
1899 (FormatHasStencil(attachment.format) && attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001900 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_STORED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001901 }
1902
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001903 auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001904
Jeremy Gebben9f537102021-10-05 16:37:12 -06001905 std::shared_ptr<IMAGE_VIEW_STATE> image_view;
Tony-LunarGb3ab3572021-07-02 09:45:17 -06001906 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001907 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
1908 if (rpabi) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001909 image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001910 }
1911 } else {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001912 image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001913 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001914
Jeremy Gebben9f537102021-10-05 16:37:12 -06001915 QueueValidateImageView(cb->queue_submit_functions_after_render_pass, "vkCmdEndRenderPass()", image_view.get(), usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001916 }
1917 }
1918}
1919
Attilio Provenzano02859b22020-02-27 14:17:28 +00001920bool BestPractices::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
1921 VkSubpassContents contents) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001922 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1923 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001924 return skip;
1925}
1926
1927bool BestPractices::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1928 const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001929 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001930 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1931 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001932 return skip;
1933}
1934
1935bool BestPractices::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001936 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001937 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1938 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001939 return skip;
1940}
1941
Sam Walls0961ec02020-03-31 16:39:15 +01001942void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
1943 const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001944 // Reset the renderpass state
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001945 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001946 cb->hasDrawCmd = false;
1947 assert(cb);
1948 auto& render_pass_state = cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02001949 render_pass_state.touchesAttachments.clear();
1950 render_pass_state.earlyClearAttachments.clear();
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001951 render_pass_state.numDrawCallsDepthOnly = 0;
1952 render_pass_state.numDrawCallsDepthEqualCompare = 0;
1953 render_pass_state.colorAttachment = false;
1954 render_pass_state.depthAttachment = false;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02001955 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001956 // Don't reset state related to pipeline state.
Sam Walls0961ec02020-03-31 16:39:15 +01001957
Jeremy Gebbenf4449392022-01-28 10:09:10 -07001958 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Sam Walls0961ec02020-03-31 16:39:15 +01001959
1960 // track depth / color attachment usage within the renderpass
1961 for (size_t i = 0; i < rp_state->createInfo.subpassCount; i++) {
1962 // record if depth/color attachments are in use for this renderpass
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001963 if (rp_state->createInfo.pSubpasses[i].pDepthStencilAttachment != nullptr) render_pass_state.depthAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001964
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001965 if (rp_state->createInfo.pSubpasses[i].colorAttachmentCount > 0) render_pass_state.colorAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001966 }
1967}
1968
1969void BestPractices::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
1970 VkSubpassContents contents) {
1971 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1972 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
1973}
1974
1975void BestPractices::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
1976 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1977 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1978 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
1979}
1980
1981void BestPractices::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1982 const VkRenderPassBeginInfo* pRenderPassBegin,
1983 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1984 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1985 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
1986}
1987
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07001988// Generic function to handle validation for all CmdDraw* type functions
1989bool BestPractices::ValidateCmdDrawType(VkCommandBuffer cmd_buffer, const char* caller) const {
1990 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001991 const auto cb_state = GetRead<bp_state::CommandBuffer>(cmd_buffer);
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07001992 if (cb_state) {
locke-lunargb8d7a7a2020-10-25 16:01:52 -06001993 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
1994 const auto* pipeline_state = cb_state->lastBound[lv_bind_point].pipeline_state;
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07001995 const auto& current_vtx_bfr_binding_info = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings;
locke-lunargb8d7a7a2020-10-25 16:01:52 -06001996
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07001997 // Verify vertex binding
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001998 if (pipeline_state->vertex_input_state && pipeline_state->vertex_input_state->binding_descriptions.size() <= 0) {
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07001999 if ((!current_vtx_bfr_binding_info.empty()) && (!cb_state->vertex_buffer_used)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002000 skip |= LogPerformanceWarning(cb_state->commandBuffer(), kVUID_BestPractices_DrawState_VtxIndexOutOfBounds,
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07002001 "Vertex buffers are bound to %s but no vertex buffers are attached to %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002002 report_data->FormatHandle(cb_state->commandBuffer()).c_str(),
2003 report_data->FormatHandle(pipeline_state->pipeline()).c_str());
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002004 }
2005 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002006
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002007 const auto* pipe = cb_state->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002008 if (pipe) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002009 const auto& rp_state = pipe->RenderPassState();
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002010 if (rp_state) {
2011 for (uint32_t i = 0; i < rp_state->createInfo.subpassCount; ++i) {
2012 const auto& subpass = rp_state->createInfo.pSubpasses[i];
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002013 const auto* ds_state = pipe->DepthStencilState();
Jeremy Gebben11af9792021-08-20 10:20:09 -06002014 const uint32_t depth_stencil_attachment =
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002015 GetSubpassDepthStencilAttachmentIndex(ds_state, subpass.pDepthStencilAttachment);
2016 const auto* raster_state = pipe->RasterizationState();
2017 if ((depth_stencil_attachment == VK_ATTACHMENT_UNUSED) && raster_state &&
2018 raster_state->depthBiasEnable == VK_TRUE) {
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002019 skip |= LogWarning(cb_state->commandBuffer(), kVUID_BestPractices_DepthBiasNoAttachment,
2020 "%s: depthBiasEnable == VK_TRUE without a depth-stencil attachment.", caller);
2021 }
2022 }
2023 }
2024 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002025 }
2026 return skip;
2027}
2028
Sam Walls0961ec02020-03-31 16:39:15 +01002029void BestPractices::RecordCmdDrawType(VkCommandBuffer cmd_buffer, uint32_t draw_count, const char* caller) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002030 auto cb_node = GetWrite<bp_state::CommandBuffer>(cmd_buffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002031 assert(cb_node);
Sam Walls0961ec02020-03-31 16:39:15 +01002032 if (VendorCheckEnabled(kBPVendorArm)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002033 RecordCmdDrawTypeArm(*cb_node, draw_count, caller);
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002034 }
2035
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002036 if (cb_node->render_pass_state.drawTouchAttachments) {
2037 for (auto& touch : cb_node->render_pass_state.nextDrawTouchesAttachments) {
2038 RecordAttachmentAccess(*cb_node, touch.framebufferAttachment, touch.aspects);
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002039 }
2040 // No need to touch the same attachments over and over.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002041 cb_node->render_pass_state.drawTouchAttachments = false;
Sam Walls0961ec02020-03-31 16:39:15 +01002042 }
2043}
2044
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002045void BestPractices::RecordCmdDrawTypeArm(bp_state::CommandBuffer& cb_node, uint32_t draw_count, const char* caller) {
2046 auto& render_pass_state = cb_node.render_pass_state;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002047 if (draw_count >= kDepthPrePassMinDrawCountArm) {
2048 if (render_pass_state.depthOnly) render_pass_state.numDrawCallsDepthOnly++;
2049 if (render_pass_state.depthEqualComparison) render_pass_state.numDrawCallsDepthEqualCompare++;
Sam Walls0961ec02020-03-31 16:39:15 +01002050 }
2051}
2052
Camden5b184be2019-08-13 07:50:19 -06002053bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002054 uint32_t firstVertex, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06002055 bool skip = false;
2056
2057 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002058 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
2059 "Warning: You are calling vkCmdDraw() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002060 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002061 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDraw()");
Camden5b184be2019-08-13 07:50:19 -06002062
2063 return skip;
2064}
2065
Sam Walls0961ec02020-03-31 16:39:15 +01002066void BestPractices::PostCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2067 uint32_t firstVertex, uint32_t firstInstance) {
2068 StateTracker::PostCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
2069 RecordCmdDrawType(commandBuffer, vertexCount * instanceCount, "vkCmdDraw()");
2070}
2071
Camden5b184be2019-08-13 07:50:19 -06002072bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002073 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06002074 bool skip = false;
2075
2076 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002077 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
2078 "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002079 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002080 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexed()");
2081
Attilio Provenzano02859b22020-02-27 14:17:28 +00002082 // Check if we reached the limit for small indexed draw calls.
2083 // Note that we cannot update the draw call count here, so we do it in PreCallRecordCmdDrawIndexed.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002084 const auto cmd_state = GetRead<bp_state::CommandBuffer>(commandBuffer);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002085 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices &&
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02002086 (cmd_state->small_indexed_draw_call_count == kMaxSmallIndexedDrawcalls - 1) &&
2087 VendorCheckEnabled(kBPVendorArm)) {
2088 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_ManySmallIndexedDrawcalls,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -06002089 "%s: The command buffer contains many small indexed drawcalls "
Attilio Provenzano02859b22020-02-27 14:17:28 +00002090 "(at least %u drawcalls with less than %u indices each). This may cause pipeline bubbles. "
2091 "You can try batching drawcalls or instancing when applicable.",
2092 VendorSpecificTag(kBPVendorArm), kMaxSmallIndexedDrawcalls, kSmallIndexedDrawcallIndices);
2093 }
2094
Sam Walls8e77e4f2020-03-16 20:47:40 +00002095 if (VendorCheckEnabled(kBPVendorArm)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002096 ValidateIndexBufferArm(*cmd_state, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002097 }
2098
2099 return skip;
2100}
2101
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002102bool BestPractices::ValidateIndexBufferArm(const bp_state::CommandBuffer& cmd_state, uint32_t indexCount, uint32_t instanceCount,
Sam Walls8e77e4f2020-03-16 20:47:40 +00002103 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
2104 bool skip = false;
2105
2106 // check for sparse/underutilised index buffer, and post-transform cache thrashing
Sam Walls8e77e4f2020-03-16 20:47:40 +00002107
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002108 const auto* ib_state = cmd_state.index_buffer_binding.buffer_state.get();
2109 if (ib_state == nullptr || cmd_state.index_buffer_binding.buffer_state->Destroyed()) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002110
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002111 const VkIndexType ib_type = cmd_state.index_buffer_binding.index_type;
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002112 const auto& ib_mem_state = *ib_state->MemState();
Sam Walls8e77e4f2020-03-16 20:47:40 +00002113 const VkDeviceSize ib_mem_offset = ib_mem_state.mapped_range.offset;
2114 const void* ib_mem = ib_mem_state.p_driver_data;
2115 bool primitive_restart_enable = false;
2116
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002117 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002118 const auto& pipeline_binding_iter = cmd_state.lastBound[lv_bind_point];
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002119 const auto* pipeline_state = pipeline_binding_iter.pipeline_state;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002120
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002121 const auto* ia_state = pipeline_state ? pipeline_state->InputAssemblyState() : nullptr;
2122 if (ia_state) {
2123 primitive_restart_enable = ia_state->primitiveRestartEnable == VK_TRUE;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002124 }
Sam Walls8e77e4f2020-03-16 20:47:40 +00002125
2126 // no point checking index buffer if the memory is nonexistant/unmapped, or if there is no graphics pipeline bound to this CB
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002127 if (ib_mem && pipeline_binding_iter.IsUsing()) {
Sam Walls8e77e4f2020-03-16 20:47:40 +00002128 uint32_t scan_stride;
2129 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2130 scan_stride = sizeof(uint8_t);
2131 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2132 scan_stride = sizeof(uint16_t);
2133 } else {
2134 scan_stride = sizeof(uint32_t);
2135 }
2136
2137 const uint8_t* scan_begin = static_cast<const uint8_t*>(ib_mem) + ib_mem_offset + firstIndex * scan_stride;
2138 const uint8_t* scan_end = scan_begin + indexCount * scan_stride;
2139
2140 // Min and max are important to track for some Mali architectures. In older Mali devices without IDVS, all
2141 // vertices corresponding to indices between the minimum and maximum may be loaded, and possibly shaded,
2142 // irrespective of whether or not they're part of the draw call.
2143
2144 // start with minimum as 0xFFFFFFFF and adjust to indices in the buffer
2145 uint32_t min_index = ~0u;
2146 // start with maximum as 0 and adjust to indices in the buffer
2147 uint32_t max_index = 0u;
2148
2149 // first scan-through, we're looking to simulate a model LRU post-transform cache, estimating the number of vertices shaded
2150 // for the given index buffer
2151 uint32_t vertex_shade_count = 0;
2152
2153 PostTransformLRUCacheModel post_transform_cache;
2154
2155 // The size of the cache being modelled positively correlates with how much behaviour it can capture about
2156 // arbitrary ground-truth hardware/architecture cache behaviour. I.e. it's a good solution when we don't know the
2157 // target architecture.
2158 // However, modelling a post-transform cache with more than 32 elements gives diminishing returns in practice.
2159 // http://eelpi.gotdns.org/papers/fast_vert_cache_opt.html
2160 post_transform_cache.resize(32);
2161
2162 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
2163 uint32_t scan_index;
2164 uint32_t primitive_restart_value;
2165 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2166 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
2167 primitive_restart_value = 0xFF;
2168 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2169 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
2170 primitive_restart_value = 0xFFFF;
2171 } else {
2172 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
2173 primitive_restart_value = 0xFFFFFFFF;
2174 }
2175
2176 max_index = std::max(max_index, scan_index);
2177 min_index = std::min(min_index, scan_index);
2178
2179 if (!primitive_restart_enable || scan_index != primitive_restart_value) {
2180 bool in_cache = post_transform_cache.query_cache(scan_index);
2181 // if the shaded vertex corresponding to the index is not in the PT-cache, we need to shade again
2182 if (!in_cache) vertex_shade_count++;
2183 }
2184 }
2185
2186 // if the max and min values were not set, then we either have no indices, or all primitive restarts, exit...
Sam Walls61b06892020-07-23 16:20:50 +01002187 // if the max and min are the same, then it implies all the indices are the same, then we don't need to do anything
2188 if (max_index < min_index || max_index == min_index) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002189
2190 if (max_index - min_index >= indexCount) {
Mark Young0ec6b062020-11-19 15:32:17 -07002191 skip |=
2192 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
2193 "%s The indices which were specified for the draw call only utilise approximately %.02f%% of "
2194 "index buffer value range. Arm Mali architectures before G71 do not have IDVS (Index-Driven "
2195 "Vertex Shading), meaning all vertices corresponding to indices between the minimum and "
2196 "maximum would be loaded, and possibly shaded, whether or not they are used.",
2197 VendorSpecificTag(kBPVendorArm),
2198 (static_cast<float>(indexCount) / static_cast<float>(max_index - min_index)) * 100.0f);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002199 return skip;
2200 }
2201
2202 // use a dynamic vector of bitsets as a memory-compact representation of which indices are included in the draw call
2203 // each bit of the n-th bucket contains the inclusion information for indices (n*n_buckets) to ((n+1)*n_buckets)
Sam Walls61b06892020-07-23 16:20:50 +01002204 const size_t refs_per_bucket = 64;
2205 std::vector<std::bitset<refs_per_bucket>> vertex_reference_buckets;
2206
2207 const uint32_t n_indices = max_index - min_index + 1;
2208 const uint32_t n_buckets = (n_indices / static_cast<uint32_t>(refs_per_bucket)) +
2209 ((n_indices % static_cast<uint32_t>(refs_per_bucket)) != 0 ? 1 : 0);
2210
2211 // there needs to be at least one bitset to store a set of indices smaller than n_buckets
2212 vertex_reference_buckets.resize(std::max(1u, n_buckets));
Sam Walls8e77e4f2020-03-16 20:47:40 +00002213
2214 // To avoid using too much memory, we run over the indices again.
2215 // Knowing the size from the last scan allows us to record index usage with bitsets
2216 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
2217 uint32_t scan_index;
2218 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2219 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
2220 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2221 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
2222 } else {
2223 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
2224 }
2225 // keep track of the set of all indices used to reference vertices in the draw call
2226 size_t index_offset = scan_index - min_index;
Sam Walls61b06892020-07-23 16:20:50 +01002227 size_t bitset_bucket_index = index_offset / refs_per_bucket;
2228 uint64_t used_indices = 1ull << ((index_offset % refs_per_bucket) & 0xFFFFFFFFu);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002229 vertex_reference_buckets[bitset_bucket_index] |= used_indices;
2230 }
2231
2232 uint32_t vertex_reference_count = 0;
2233 for (const auto& bitset : vertex_reference_buckets) {
2234 vertex_reference_count += static_cast<uint32_t>(bitset.count());
2235 }
2236
2237 // low index buffer utilization implies that: of the vertices available to the draw call, not all are utilized
Mark Young0ec6b062020-11-19 15:32:17 -07002238 float utilization = static_cast<float>(vertex_reference_count) / static_cast<float>(max_index - min_index + 1);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002239 // low hit rate (high miss rate) implies the order of indices in the draw call may be possible to improve
Mark Young0ec6b062020-11-19 15:32:17 -07002240 float cache_hit_rate = static_cast<float>(vertex_reference_count) / static_cast<float>(vertex_shade_count);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002241
2242 if (utilization < 0.5f) {
2243 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
2244 "%s The indices which were specified for the draw call only utilise approximately "
2245 "%.02f%% of the bound vertex buffer.",
2246 VendorSpecificTag(kBPVendorArm), utilization);
2247 }
2248
2249 if (cache_hit_rate <= 0.5f) {
2250 skip |=
2251 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_PostTransformCacheThrashing,
2252 "%s The indices which were specified for the draw call are estimated to cause thrashing of "
2253 "the post-transform vertex cache, with a hit-rate of %.02f%%. "
2254 "I.e. the ordering of the index buffer may not make optimal use of indices associated with "
2255 "recently shaded vertices.",
2256 VendorSpecificTag(kBPVendorArm), cache_hit_rate * 100.0f);
2257 }
2258 }
2259
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002260 return skip;
2261}
2262
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002263bool BestPractices::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
2264 const VkCommandBuffer* pCommandBuffers) const {
2265 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002266 const auto primary = GetRead<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002267 for (uint32_t i = 0; i < commandBufferCount; i++) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002268 const auto secondary_cb = GetRead<bp_state::CommandBuffer>(pCommandBuffers[i]);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002269 if (secondary_cb == nullptr) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002270 continue;
2271 }
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002272 const auto& secondary = secondary_cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002273 for (auto& clear : secondary.earlyClearAttachments) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002274 if (ClearAttachmentsIsFullClear(*primary, uint32_t(clear.rects.size()), clear.rects.data())) {
2275 skip |= ValidateClearAttachment(*primary, clear.framebufferAttachment, clear.colorAttachment, clear.aspects, true);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002276 }
2277 }
2278 }
Nadav Gevaf0808442021-05-21 13:51:25 -04002279
2280 if (VendorCheckEnabled(kBPVendorAMD)) {
2281 if (commandBufferCount > 0) {
2282 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_AvoidSecondaryCmdBuffers,
2283 "%s Performance warning: Use of secondary command buffers is not recommended. ",
2284 VendorSpecificTag(kBPVendorAMD));
2285 }
2286 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002287 return skip;
2288}
2289
2290void BestPractices::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
2291 const VkCommandBuffer* pCommandBuffers) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002292 ValidationStateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
2293
2294 auto primary = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2295 if (!primary) {
2296 return;
2297 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002298
2299 for (uint32_t i = 0; i < commandBufferCount; i++) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002300 auto secondary = GetWrite<bp_state::CommandBuffer>(pCommandBuffers[i]);
2301 if (!secondary) {
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002302 continue;
2303 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002304
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002305 for (auto& early_clear : secondary->render_pass_state.earlyClearAttachments) {
2306 if (ClearAttachmentsIsFullClear(*primary, uint32_t(early_clear.rects.size()), early_clear.rects.data())) {
2307 RecordAttachmentClearAttachments(*primary, early_clear.framebufferAttachment, early_clear.colorAttachment,
2308 early_clear.aspects, uint32_t(early_clear.rects.size()), early_clear.rects.data());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002309 } else {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002310 RecordAttachmentAccess(*primary, early_clear.framebufferAttachment, early_clear.aspects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002311 }
2312 }
2313
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002314 for (auto& touch : secondary->render_pass_state.touchesAttachments) {
2315 RecordAttachmentAccess(*primary, touch.framebufferAttachment, touch.aspects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002316 }
Hans-Kristian Arntzenc7eb82a2021-06-16 13:57:18 +02002317
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002318 primary->render_pass_state.numDrawCallsDepthEqualCompare += secondary->render_pass_state.numDrawCallsDepthEqualCompare;
2319 primary->render_pass_state.numDrawCallsDepthOnly += secondary->render_pass_state.numDrawCallsDepthOnly;
Hans-Kristian Arntzenaf81dca2021-06-18 11:14:28 +02002320
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002321 if (secondary->hasDrawCmd) {
Hans-Kristian Arntzenaf81dca2021-06-18 11:14:28 +02002322 primary->hasDrawCmd = true;
2323 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002324 }
2325
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002326}
2327
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002328void BestPractices::RecordAttachmentAccess(bp_state::CommandBuffer& cb_state, uint32_t fb_attachment, VkImageAspectFlags aspects) {
2329 auto& state = cb_state.render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002330 // Called when we have a partial clear attachment, or a normal draw call which accesses an attachment.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002331 auto itr =
2332 std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
2333 [fb_attachment](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == fb_attachment; });
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002334
2335 if (itr != state.touchesAttachments.end()) {
2336 itr->aspects |= aspects;
2337 } else {
2338 state.touchesAttachments.push_back({ fb_attachment, aspects });
2339 }
2340}
2341
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002342void BestPractices::RecordAttachmentClearAttachments(bp_state::CommandBuffer& cmd_state, uint32_t fb_attachment,
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002343 uint32_t color_attachment, VkImageAspectFlags aspects, uint32_t rectCount,
2344 const VkClearRect* pRects) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002345 auto& state = cmd_state.render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002346 // If we observe a full clear before any other access to a frame buffer attachment,
2347 // we have candidate for redundant clear attachments.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002348 auto itr =
2349 std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
2350 [fb_attachment](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == fb_attachment; });
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002351
2352 uint32_t new_aspects = aspects;
2353 if (itr != state.touchesAttachments.end()) {
2354 new_aspects = aspects & ~itr->aspects;
2355 itr->aspects |= aspects;
2356 } else {
2357 state.touchesAttachments.push_back({ fb_attachment, aspects });
2358 }
2359
2360 if (new_aspects == 0) {
2361 return;
2362 }
2363
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002364 if (cmd_state.createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002365 // The first command might be a clear, but might not be the first in the render pass, defer any checks until
2366 // CmdExecuteCommands.
2367 state.earlyClearAttachments.push_back({ fb_attachment, color_attachment, new_aspects,
2368 std::vector<VkClearRect>{pRects, pRects + rectCount} });
2369 }
2370}
2371
2372void BestPractices::PreCallRecordCmdClearAttachments(VkCommandBuffer commandBuffer,
2373 uint32_t attachmentCount, const VkClearAttachment* pClearAttachments,
2374 uint32_t rectCount, const VkClearRect* pRects) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002375 ValidationStateTracker::PreCallRecordCmdClearAttachments(commandBuffer, attachmentCount, pClearAttachments, rectCount, pRects);
2376
2377 auto cmd_state = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2378 auto* rp_state = cmd_state->activeRenderPass.get();
2379 auto* fb_state = cmd_state->activeFramebuffer.get();
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002380 bool is_secondary = cmd_state->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY;
2381
2382 if (rectCount == 0 || !rp_state) {
2383 return;
2384 }
2385
2386 if (!is_secondary && !fb_state) {
2387 return;
2388 }
2389
2390 // If we have a rect which covers the entire frame buffer, we have a LOAD_OP_CLEAR-like command.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002391 bool full_clear = ClearAttachmentsIsFullClear(*cmd_state, rectCount, pRects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002392
ziga-lunarg885c6542022-03-07 01:08:25 +01002393 if (!rp_state->use_dynamic_rendering && !rp_state->use_dynamic_rendering_inherited) {
2394 auto& subpass = rp_state->createInfo.pSubpasses[cmd_state->activeSubpass];
2395 for (uint32_t i = 0; i < attachmentCount; i++) {
2396 auto& attachment = pClearAttachments[i];
2397 uint32_t fb_attachment = VK_ATTACHMENT_UNUSED;
2398 VkImageAspectFlags aspects = attachment.aspectMask;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002399
ziga-lunarg885c6542022-03-07 01:08:25 +01002400 if (aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
2401 if (subpass.pDepthStencilAttachment) {
2402 fb_attachment = subpass.pDepthStencilAttachment->attachment;
2403 }
2404 } else if (aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
2405 fb_attachment = subpass.pColorAttachments[attachment.colorAttachment].attachment;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002406 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002407
ziga-lunarg885c6542022-03-07 01:08:25 +01002408 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
2409 if (full_clear) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002410 RecordAttachmentClearAttachments(*cmd_state, fb_attachment, attachment.colorAttachment,
ziga-lunarg885c6542022-03-07 01:08:25 +01002411 aspects, rectCount, pRects);
2412 } else {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002413 RecordAttachmentAccess(*cmd_state, fb_attachment, aspects);
ziga-lunarg885c6542022-03-07 01:08:25 +01002414 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002415 }
2416 }
2417 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002418}
2419
Attilio Provenzano02859b22020-02-27 14:17:28 +00002420void BestPractices::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2421 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2422 ValidationStateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset,
2423 firstInstance);
2424
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002425 auto cmd_state = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002426 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices) {
2427 cmd_state->small_indexed_draw_call_count++;
2428 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002429
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002430 ValidateBoundDescriptorSets(*cmd_state, "vkCmdDrawIndexed()");
Attilio Provenzano02859b22020-02-27 14:17:28 +00002431}
2432
Sam Walls0961ec02020-03-31 16:39:15 +01002433void BestPractices::PostCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2434 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2435 StateTracker::PostCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
2436 RecordCmdDrawType(commandBuffer, indexCount * instanceCount, "vkCmdDrawIndexed()");
2437}
2438
sfricke-samsung681ab7b2020-10-29 01:53:35 -07002439bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2440 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2441 uint32_t maxDrawCount, uint32_t stride) const {
2442 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCount()");
2443
2444 return skip;
2445}
2446
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002447bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
2448 VkDeviceSize offset, VkBuffer countBuffer,
2449 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2450 uint32_t stride) const {
2451 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountKHR()");
Camden5b184be2019-08-13 07:50:19 -06002452
2453 return skip;
2454}
2455
2456bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002457 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06002458 bool skip = false;
2459
2460 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002461 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
2462 "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002463 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirect()");
Camden5b184be2019-08-13 07:50:19 -06002464 }
2465
2466 return skip;
2467}
2468
Sam Walls0961ec02020-03-31 16:39:15 +01002469void BestPractices::PostCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2470 uint32_t count, uint32_t stride) {
2471 StateTracker::PostCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, count, stride);
2472 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndirect()");
2473}
2474
Camden5b184be2019-08-13 07:50:19 -06002475bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002476 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06002477 bool skip = false;
2478
2479 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002480 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
2481 "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002482 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirect()");
Camden5b184be2019-08-13 07:50:19 -06002483 }
2484
2485 return skip;
2486}
2487
Sam Walls0961ec02020-03-31 16:39:15 +01002488void BestPractices::PostCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2489 uint32_t count, uint32_t stride) {
2490 StateTracker::PostCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, count, stride);
2491 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndexedIndirect()");
2492}
2493
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002494void BestPractices::ValidateBoundDescriptorSets(bp_state::CommandBuffer& cb_state, const char* function_name) {
2495 for (auto descriptor_set : cb_state.validated_descriptor_sets) {
2496 const auto& layout = *descriptor_set->GetLayout();
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002497
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002498 for (uint32_t index = 0; index < descriptor_set->GetBindingCount(); ++index) {
2499 // For bindless scenarios, we should not attempt to track descriptor set state.
2500 // It is highly uncertain which resources are actually bound.
2501 // Resources which are written to such a descriptor should be marked as indeterminate w.r.t. state.
2502 VkDescriptorBindingFlags flags = layout.GetDescriptorBindingFlagsFromIndex(index);
2503 if (flags & (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT |
2504 VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT)) {
2505 continue;
2506 }
Hans-Kristian Arntzena8199012021-03-22 12:10:07 +01002507
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002508 auto index_range = layout.GetGlobalIndexRangeFromIndex(index);
2509 for (uint32_t i = index_range.start; i < index_range.end; ++i) {
2510 VkImageView image_view{VK_NULL_HANDLE};
2511
2512 auto descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2513 switch (descriptor->GetClass()) {
2514 case cvdescriptorset::DescriptorClass::Image: {
2515 if (const auto image_descriptor = static_cast<const cvdescriptorset::ImageDescriptor*>(descriptor)) {
2516 image_view = image_descriptor->GetImageView();
2517 }
2518 break;
2519 }
2520 case cvdescriptorset::DescriptorClass::ImageSampler: {
2521 if (const auto image_sampler_descriptor =
2522 static_cast<const cvdescriptorset::ImageSamplerDescriptor*>(descriptor)) {
2523 image_view = image_sampler_descriptor->GetImageView();
2524 }
2525 break;
2526 }
2527 default:
2528 break;
Hans-Kristian Arntzena8199012021-03-22 12:10:07 +01002529 }
2530
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002531 if (image_view) {
2532 auto image_view_state = Get<IMAGE_VIEW_STATE>(image_view);
2533 QueueValidateImageView(cb_state.queue_submit_functions, function_name, image_view_state.get(),
2534 IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002535 }
2536 }
2537 }
2538 }
2539}
2540
2541void BestPractices::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2542 uint32_t firstVertex, uint32_t firstInstance) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002543 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2544 ValidateBoundDescriptorSets(*cb_node, "vkCmdDraw()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002545}
2546
2547void BestPractices::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2548 uint32_t drawCount, uint32_t stride) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002549 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2550 ValidateBoundDescriptorSets(*cb_node, "vkCmdDrawIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002551}
2552
2553void BestPractices::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2554 uint32_t drawCount, uint32_t stride) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002555 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2556 ValidateBoundDescriptorSets(*cb_node, "vkCmdDrawIndexedIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002557}
2558
Camden5b184be2019-08-13 07:50:19 -06002559bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002560 uint32_t groupCountZ) const {
Camden5b184be2019-08-13 07:50:19 -06002561 bool skip = false;
2562
2563 if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002564 skip |= LogWarning(device, kVUID_BestPractices_CmdDispatch_GroupCountZero,
2565 "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32
2566 ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").",
2567 groupCountX, groupCountY, groupCountZ);
Camden5b184be2019-08-13 07:50:19 -06002568 }
2569
2570 return skip;
2571}
Camden83a9c372019-08-14 11:41:38 -06002572
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002573bool BestPractices::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
2574 bool skip = false;
2575 skip |= StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2576 skip |= ValidateCmdEndRenderPass(commandBuffer);
2577 return skip;
2578}
2579
2580bool BestPractices::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
2581 bool skip = false;
2582 skip |= StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2583 skip |= ValidateCmdEndRenderPass(commandBuffer);
2584 return skip;
2585}
2586
Sam Walls0961ec02020-03-31 16:39:15 +01002587bool BestPractices::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2588 bool skip = false;
Sam Walls0961ec02020-03-31 16:39:15 +01002589 skip |= StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002590 skip |= ValidateCmdEndRenderPass(commandBuffer);
2591 return skip;
2592}
2593
2594bool BestPractices::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2595 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002596 const auto cmd = GetRead<bp_state::CommandBuffer>(commandBuffer);
Sam Walls0961ec02020-03-31 16:39:15 +01002597
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002598 if (cmd == nullptr) return skip;
2599 auto &render_pass_state = cmd->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01002600
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002601 bool uses_depth = (render_pass_state.depthAttachment || render_pass_state.colorAttachment) &&
2602 render_pass_state.numDrawCallsDepthEqualCompare >= kDepthPrePassNumDrawCallsArm &&
2603 render_pass_state.numDrawCallsDepthOnly >= kDepthPrePassNumDrawCallsArm;
Sam Walls0961ec02020-03-31 16:39:15 +01002604 if (uses_depth) {
2605 skip |= LogPerformanceWarning(
2606 device, kVUID_BestPractices_EndRenderPass_DepthPrePassUsage,
2607 "%s Depth pre-passes may be in use. In general, this is not recommended, as in Arm Mali GPUs since "
2608 "Mali-T620, Forward Pixel Killing (FPK) can already perform automatic hidden surface removal; in which "
2609 "case, using depth pre-passes for hidden surface removal may worsen performance.",
2610 VendorSpecificTag(kBPVendorArm));
2611 }
2612
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002613 RENDER_PASS_STATE* rp = cmd->activeRenderPass.get();
2614
2615 if (VendorCheckEnabled(kBPVendorArm) && rp) {
2616
2617 // If we use an attachment on-tile, we should access it in some way. Otherwise,
2618 // it is redundant to have it be part of the render pass.
2619 // Only consider it redundant if it will actually consume bandwidth, i.e.
2620 // LOAD_OP_LOAD is used or STORE_OP_STORE. CLEAR -> DONT_CARE is benign,
2621 // as is using pure input attachments.
2622 // CLEAR -> STORE might be considered a "useful" thing to do, but
2623 // the optimal thing to do is to defer the clear until you're actually
2624 // going to render to the image.
2625
2626 uint32_t num_attachments = rp->createInfo.attachmentCount;
2627 for (uint32_t i = 0; i < num_attachments; i++) {
Hans-Kristian Arntzen237663c2021-07-01 14:36:40 +02002628 if (!RenderPassUsesAttachmentOnTile(rp->createInfo, i) ||
2629 RenderPassUsesAttachmentAsResolve(rp->createInfo, i)) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002630 continue;
2631 }
2632
2633 auto& attachment = rp->createInfo.pAttachments[i];
2634
2635 VkImageAspectFlags bandwidth_aspects = 0;
2636
2637 if (!FormatIsStencilOnly(attachment.format) &&
2638 (attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
2639 attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE)) {
2640 if (FormatHasDepth(attachment.format)) {
2641 bandwidth_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
2642 } else {
2643 bandwidth_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
2644 }
2645 }
2646
2647 if (FormatHasStencil(attachment.format) &&
2648 (attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
2649 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
2650 bandwidth_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
2651 }
2652
2653 if (!bandwidth_aspects) {
2654 continue;
2655 }
2656
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002657 auto itr = std::find_if(render_pass_state.touchesAttachments.begin(), render_pass_state.touchesAttachments.end(),
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002658 [i](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == i; });
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002659 uint32_t untouched_aspects = bandwidth_aspects;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002660 if (itr != render_pass_state.touchesAttachments.end()) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002661 untouched_aspects &= ~itr->aspects;
2662 }
2663
2664 if (untouched_aspects) {
2665 skip |= LogPerformanceWarning(
2666 device, kVUID_BestPractices_EndRenderPass_RedundantAttachmentOnTile,
2667 "%s Render pass was ended, but attachment #%u (format: %u, untouched aspects 0x%x) "
2668 "was never accessed by a pipeline or clear command. "
2669 "On tile-based architectures, LOAD_OP_LOAD and STORE_OP_STORE consume bandwidth and should not be part of the render pass "
2670 "if the attachments are not intended to be accessed.",
2671 VendorSpecificTag(kBPVendorArm), i, attachment.format, untouched_aspects);
2672 }
2673 }
2674 }
2675
Sam Walls0961ec02020-03-31 16:39:15 +01002676 return skip;
2677}
2678
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002679void BestPractices::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002680 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2681 ValidateBoundDescriptorSets(*cb_node, "vkCmdDispatch()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002682}
2683
2684void BestPractices::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002685 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2686 ValidateBoundDescriptorSets(*cb_node, "vkCmdDispatchIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002687}
2688
Camden Stocker9c051442019-11-06 14:28:43 -08002689bool BestPractices::ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(VkPhysicalDevice physicalDevice,
2690 const char* api_name) const {
2691 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002692 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Camden Stocker9c051442019-11-06 14:28:43 -08002693
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06002694 if (bp_pd_state) {
2695 if (bp_pd_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState == UNCALLED) {
2696 skip |= LogWarning(physicalDevice, kVUID_BestPractices_DisplayPlane_PropertiesNotCalled,
2697 "Potential problem with calling %s() without first retrieving properties from "
2698 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR or vkGetPhysicalDeviceDisplayPlaneProperties2KHR.",
2699 api_name);
2700 }
Camden Stocker9c051442019-11-06 14:28:43 -08002701 }
2702
2703 return skip;
2704}
2705
Camden83a9c372019-08-14 11:41:38 -06002706bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002707 uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const {
Camden83a9c372019-08-14 11:41:38 -06002708 bool skip = false;
2709
Camden Stocker9c051442019-11-06 14:28:43 -08002710 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneSupportedDisplaysKHR");
Camden83a9c372019-08-14 11:41:38 -06002711
Camden Stocker9c051442019-11-06 14:28:43 -08002712 return skip;
2713}
2714
2715bool BestPractices::PreCallValidateGetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
2716 uint32_t planeIndex,
2717 VkDisplayPlaneCapabilitiesKHR* pCapabilities) const {
2718 bool skip = false;
2719
2720 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilitiesKHR");
2721
2722 return skip;
2723}
2724
2725bool BestPractices::PreCallValidateGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice,
2726 const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo,
2727 VkDisplayPlaneCapabilities2KHR* pCapabilities) const {
2728 bool skip = false;
2729
2730 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilities2KHR");
Camden83a9c372019-08-14 11:41:38 -06002731
2732 return skip;
2733}
Camden05de2d42019-08-19 10:23:56 -06002734
2735bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002736 VkImage* pSwapchainImages) const {
Camden05de2d42019-08-19 10:23:56 -06002737 bool skip = false;
2738
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002739 auto swapchain_state = Get<bp_state::Swapchain>(swapchain);
Camden05de2d42019-08-19 10:23:56 -06002740
Nathaniel Cesario39152e62021-07-02 13:04:16 -06002741 if (swapchain_state && pSwapchainImages) {
Camden05de2d42019-08-19 10:23:56 -06002742 // Compare the preliminary value of *pSwapchainImageCount with the value this time:
Nathaniel Cesario39152e62021-07-02 13:04:16 -06002743 if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002744 skip |=
2745 LogWarning(device, kVUID_Core_Swapchain_PriorCount,
2746 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has "
2747 "been seen for pSwapchainImages.");
Camden05de2d42019-08-19 10:23:56 -06002748 }
Camden05de2d42019-08-19 10:23:56 -06002749
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06002750 if (*pSwapchainImageCount > swapchain_state->get_swapchain_image_count) {
2751 skip |= LogWarning(
2752 device, kVUID_BestPractices_Swapchain_InvalidCount,
2753 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImages, and with pSwapchainImageCount set to a "
Nadav Gevaf0808442021-05-21 13:51:25 -04002754 "value (%" PRId32 ") that is greater than the value (%" PRId32 ") that was returned when pSwapchainImages was NULL.",
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06002755 *pSwapchainImageCount, swapchain_state->get_swapchain_image_count);
2756 }
2757 }
2758
Camden05de2d42019-08-19 10:23:56 -06002759 return skip;
2760}
2761
2762// Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002763bool BestPractices::ValidateCommonGetPhysicalDeviceQueueFamilyProperties(const PHYSICAL_DEVICE_STATE* bp_pd_state,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002764 uint32_t requested_queue_family_property_count,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002765 const CALL_STATE call_state,
2766 const char* caller_name) const {
Camden05de2d42019-08-19 10:23:56 -06002767 bool skip = false;
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002768 // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count
2769 if (UNCALLED == call_state) {
2770 skip |= LogWarning(
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002771 bp_pd_state->Handle(), kVUID_Core_DevLimit_MissingQueryCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002772 "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is "
2773 "recommended "
2774 "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.",
2775 caller_name, caller_name);
2776 // Then verify that pCount that is passed in on second call matches what was returned
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002777 } else if (bp_pd_state->queue_family_known_count != requested_queue_family_property_count) {
2778 skip |= LogWarning(bp_pd_state->Handle(), kVUID_Core_DevLimit_CountMismatch,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002779 "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32
2780 ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32
2781 ". It is recommended to instead receive all the properties by calling %s with "
2782 "pQueueFamilyPropertyCount that was "
2783 "previously obtained by calling %s with NULL pQueueFamilyProperties.",
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002784 caller_name, requested_queue_family_property_count, bp_pd_state->queue_family_known_count, caller_name,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002785 caller_name);
Camden05de2d42019-08-19 10:23:56 -06002786 }
2787
2788 return skip;
2789}
2790
Jeff Bolz5c801d12019-10-09 10:38:45 -05002791bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV(
2792 VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const {
Camden Stocker82510582019-09-03 14:00:16 -06002793 bool skip = false;
2794
2795 for (uint32_t i = 0; i < bindInfoCount; i++) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002796 auto as_state = Get<ACCELERATION_STRUCTURE_STATE>(pBindInfos[i].accelerationStructure);
Camden Stocker82510582019-09-03 14:00:16 -06002797 if (!as_state->memory_requirements_checked) {
2798 // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling
2799 // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with
2800 // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002801 skip |= LogWarning(
2802 device, kVUID_BestPractices_BindAccelNV_NoMemReqQuery,
Camden Stocker82510582019-09-03 14:00:16 -06002803 "vkBindAccelerationStructureMemoryNV(): "
2804 "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.",
2805 report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str());
2806 }
2807 }
2808
2809 return skip;
2810}
2811
Camden05de2d42019-08-19 10:23:56 -06002812bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
2813 uint32_t* pQueueFamilyPropertyCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002814 VkQueueFamilyProperties* pQueueFamilyProperties) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002815 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002816 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002817 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002818 bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
2819 "vkGetPhysicalDeviceQueueFamilyProperties()");
2820 }
2821 return false;
Camden05de2d42019-08-19 10:23:56 -06002822}
2823
Mike Schuchardt2df08912020-12-15 16:28:09 -08002824bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
2825 uint32_t* pQueueFamilyPropertyCount,
2826 VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002827 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002828 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002829 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002830 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
2831 "vkGetPhysicalDeviceQueueFamilyProperties2()");
2832 }
2833 return false;
Camden05de2d42019-08-19 10:23:56 -06002834}
2835
Jeff Bolz5c801d12019-10-09 10:38:45 -05002836bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR(
Mike Schuchardt2df08912020-12-15 16:28:09 -08002837 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount, VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002838 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002839 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002840 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002841 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
2842 "vkGetPhysicalDeviceQueueFamilyProperties2KHR()");
2843 }
2844 return false;
Camden05de2d42019-08-19 10:23:56 -06002845}
2846
2847bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
2848 uint32_t* pSurfaceFormatCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002849 VkSurfaceFormatKHR* pSurfaceFormats) const {
Camden05de2d42019-08-19 10:23:56 -06002850 if (!pSurfaceFormats) return false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002851 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06002852 const auto& call_state = bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState;
Camden05de2d42019-08-19 10:23:56 -06002853 bool skip = false;
2854 if (call_state == UNCALLED) {
2855 // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't
2856 // previously call this function with a NULL value of pSurfaceFormats:
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002857 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_MustQueryCount,
2858 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior "
2859 "positive value has been seen for pSurfaceFormats.");
Camden05de2d42019-08-19 10:23:56 -06002860 } else {
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06002861 if (*pSurfaceFormatCount > bp_pd_state->surface_formats_count) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002862 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_CountMismatch,
2863 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with "
2864 "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned "
2865 "when pSurfaceFormatCount was NULL.",
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06002866 *pSurfaceFormatCount, bp_pd_state->surface_formats_count);
Camden05de2d42019-08-19 10:23:56 -06002867 }
2868 }
2869 return skip;
2870}
Camden Stocker23cc47d2019-09-03 14:53:57 -06002871
2872bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002873 VkFence fence) const {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002874 bool skip = false;
2875
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002876 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
2877 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
Camden Stocker23cc47d2019-09-03 14:53:57 -06002878 // Store sparse binding image_state and after binding is complete make sure that any requiring metadata have it bound
Jeremy Gebbencbf22862021-03-03 12:01:22 -07002879 layer_data::unordered_set<const IMAGE_STATE*> sparse_images;
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002880 // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state
2881 // in RecordQueueBindSparse.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07002882 layer_data::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata;
Camden Stocker23cc47d2019-09-03 14:53:57 -06002883 // If we're binding sparse image memory make sure reqs were queried and note if metadata is required and bound
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002884 for (uint32_t i = 0; i < bind_info.imageBindCount; ++i) {
2885 const auto& image_bind = bind_info.pImageBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04002886 auto image_state = Get<IMAGE_STATE>(image_bind.image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002887 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002888 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002889 }
Jeremy Gebben9f537102021-10-05 16:37:12 -06002890 sparse_images.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002891 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
2892 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
2893 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002894 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002895 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
2896 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002897 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002898 }
2899 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002900 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002901 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002902 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002903 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
2904 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002905 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002906 }
2907 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002908 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
2909 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04002910 auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002911 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002912 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002913 }
Jeremy Gebben9f537102021-10-05 16:37:12 -06002914 sparse_images.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002915 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
2916 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
2917 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002918 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002919 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
2920 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002921 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002922 }
2923 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002924 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002925 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002926 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002927 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
2928 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002929 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002930 }
2931 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
2932 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002933 sparse_images_with_metadata.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002934 }
2935 }
2936 }
2937 for (const auto& sparse_image_state : sparse_images) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002938 if (sparse_image_state->sparse_metadata_required && !sparse_image_state->sparse_metadata_bound &&
2939 sparse_images_with_metadata.find(sparse_image_state) == sparse_images_with_metadata.end()) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002940 // Warn if sparse image binding metadata required for image with sparse binding, but metadata not bound
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002941 skip |= LogWarning(sparse_image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002942 "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no "
2943 "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002944 report_data->FormatHandle(sparse_image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002945 }
2946 }
2947 }
2948
2949 return skip;
2950}
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002951
Mark Lobodzinski84101d72020-04-24 09:43:48 -06002952void BestPractices::ManualPostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
2953 VkFence fence, VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07002954 if (result != VK_SUCCESS) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07002955 return;
2956 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002957
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002958 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
2959 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
2960 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
2961 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04002962 auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002963 if (!image_state) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002964 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002965 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002966 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
2967 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
2968 image_state->sparse_metadata_bound = true;
2969 }
2970 }
2971 }
2972 }
2973}
Camden Stocker0e0f89b2019-10-16 12:24:31 -07002974
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002975bool BestPractices::ClearAttachmentsIsFullClear(const bp_state::CommandBuffer& cmd, uint32_t rectCount,
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002976 const VkClearRect* pRects) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002977 if (cmd.createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002978 // We don't know the accurate render area in a secondary,
2979 // so assume we clear the entire frame buffer.
2980 // This is resolved in CmdExecuteCommands where we can check if the clear is a full clear.
2981 return true;
2982 }
2983
2984 // If we have a rect which covers the entire frame buffer, we have a LOAD_OP_CLEAR-like command.
2985 for (uint32_t i = 0; i < rectCount; i++) {
2986 auto& rect = pRects[i];
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002987 auto& render_area = cmd.activeRenderPassBeginInfo.renderArea;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002988 if (rect.rect.extent.width == render_area.extent.width && rect.rect.extent.height == render_area.extent.height) {
2989 return true;
2990 }
2991 }
2992
2993 return false;
2994}
2995
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002996bool BestPractices::ValidateClearAttachment(const bp_state::CommandBuffer& cmd, uint32_t fb_attachment, uint32_t color_attachment,
2997 VkImageAspectFlags aspects, bool secondary) const {
2998 const RENDER_PASS_STATE* rp = cmd.activeRenderPass.get();
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002999 bool skip = false;
3000
3001 if (!rp || fb_attachment == VK_ATTACHMENT_UNUSED) {
3002 return skip;
3003 }
3004
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003005 const auto& rp_state = cmd.render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003006
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003007 auto attachment_itr =
3008 std::find_if(rp_state.touchesAttachments.begin(), rp_state.touchesAttachments.end(),
3009 [fb_attachment](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == fb_attachment; });
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003010
3011 // Only report aspects which haven't been touched yet.
3012 VkImageAspectFlags new_aspects = aspects;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003013 if (attachment_itr != rp_state.touchesAttachments.end()) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003014 new_aspects &= ~attachment_itr->aspects;
3015 }
3016
3017 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003018 if (!cmd.hasDrawCmd) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003019 skip |= LogPerformanceWarning(
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003020 cmd.Handle(), kVUID_BestPractices_DrawState_ClearCmdBeforeDraw,
Hans-Kristian Arntzen4ddd6182021-06-18 12:16:33 +02003021 "vkCmdClearAttachments() issued on %s prior to any Draw Cmds in current render pass. It is recommended you "
3022 "use RenderPass LOAD_OP_CLEAR on attachments instead.",
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003023 report_data->FormatHandle(cmd.Handle()).c_str());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003024 }
3025
3026 if ((new_aspects & VK_IMAGE_ASPECT_COLOR_BIT) &&
3027 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
3028 skip |= LogPerformanceWarning(
3029 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3030 "%svkCmdClearAttachments() issued on %s for color attachment #%u in this subpass, "
3031 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3032 "it is more efficient.",
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003033 secondary ? "vkCmdExecuteCommands(): " : "", report_data->FormatHandle(cmd.Handle()).c_str(), color_attachment);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003034 }
3035
3036 if ((new_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) &&
3037 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003038 skip |=
3039 LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3040 "%svkCmdClearAttachments() issued on %s for the depth attachment in this subpass, "
3041 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3042 "it is more efficient.",
3043 secondary ? "vkCmdExecuteCommands(): " : "", report_data->FormatHandle(cmd.Handle()).c_str());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003044 }
3045
3046 if ((new_aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&
3047 rp->createInfo.pAttachments[fb_attachment].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003048 skip |=
3049 LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3050 "%svkCmdClearAttachments() issued on %s for the stencil attachment in this subpass, "
3051 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3052 "it is more efficient.",
3053 secondary ? "vkCmdExecuteCommands(): " : "", report_data->FormatHandle(cmd.Handle()).c_str());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003054 }
3055
3056 return skip;
3057}
3058
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003059bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
Camden Stockerf55721f2019-09-09 11:04:49 -06003060 const VkClearAttachment* pAttachments, uint32_t rectCount,
3061 const VkClearRect* pRects) const {
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003062 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003063 const auto cb_node = GetRead<bp_state::CommandBuffer>(commandBuffer);
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003064 if (!cb_node) return skip;
3065
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003066 if (cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
3067 // Defer checks to ExecuteCommands.
3068 return skip;
3069 }
3070
3071 // Only care about full clears, partial clears might have legitimate uses.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003072 if (!ClearAttachmentsIsFullClear(*cb_node, rectCount, pRects)) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003073 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003074 }
3075
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003076 // Check for uses of ClearAttachments along with LOAD_OP_LOAD,
3077 // as it can be more efficient to just use LOAD_OP_CLEAR
locke-lunargaecf2152020-05-12 17:15:41 -06003078 const RENDER_PASS_STATE* rp = cb_node->activeRenderPass.get();
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003079 if (rp) {
3080 const auto& subpass = rp->createInfo.pSubpasses[cb_node->activeSubpass];
3081
3082 for (uint32_t i = 0; i < attachmentCount; i++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02003083 const auto& attachment = pAttachments[i];
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003084
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003085 if (attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
3086 uint32_t color_attachment = attachment.colorAttachment;
3087 uint32_t fb_attachment = subpass.pColorAttachments[color_attachment].attachment;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003088 skip |= ValidateClearAttachment(*cb_node, fb_attachment, color_attachment, attachment.aspectMask, false);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003089 }
3090
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003091 if (subpass.pDepthStencilAttachment &&
3092 (attachment.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))) {
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003093 uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003094 skip |= ValidateClearAttachment(*cb_node, fb_attachment, VK_ATTACHMENT_UNUSED, attachment.aspectMask, false);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003095 }
3096 }
3097 }
3098
Nadav Gevaf0808442021-05-21 13:51:25 -04003099 if (VendorCheckEnabled(kBPVendorAMD)) {
3100 for (uint32_t attachment_idx = 0; attachment_idx < attachmentCount; attachment_idx++) {
3101 if (pAttachments[attachment_idx].aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) {
3102 bool black_check = false;
3103 black_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 0.0f;
3104 black_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 0.0f;
3105 black_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 0.0f;
3106 black_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f &&
3107 pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f;
3108
3109 bool white_check = false;
3110 white_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 1.0f;
3111 white_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 1.0f;
3112 white_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 1.0f;
3113 white_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f &&
3114 pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f;
3115
3116 if (black_check && white_check) {
3117 skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues,
3118 "%s Performance warning: vkCmdClearAttachments() clear value for color attachment %" PRId32 " is not a fast clear value."
3119 "Consider changing to one of the following:"
3120 "RGBA(0, 0, 0, 0) "
3121 "RGBA(0, 0, 0, 1) "
3122 "RGBA(1, 1, 1, 0) "
3123 "RGBA(1, 1, 1, 1)",
3124 VendorSpecificTag(kBPVendorAMD), attachment_idx);
3125 }
3126 } else {
3127 if ((pAttachments[attachment_idx].clearValue.depthStencil.depth != 0 &&
3128 pAttachments[attachment_idx].clearValue.depthStencil.depth != 1) &&
3129 pAttachments[attachment_idx].clearValue.depthStencil.stencil != 0) {
3130 skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues,
3131 "%s Performance warning: vkCmdClearAttachments() clear value for depth/stencil "
3132 "attachment %" PRId32 " is not a fast clear value."
3133 "Consider changing to one of the following:"
3134 "D=0.0f, S=0"
3135 "D=1.0f, S=0",
3136 VendorSpecificTag(kBPVendorAMD), attachment_idx);
3137 }
3138 }
3139 }
3140 }
3141
Camden Stockerf55721f2019-09-09 11:04:49 -06003142 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003143}
Attilio Provenzano02859b22020-02-27 14:17:28 +00003144
3145bool BestPractices::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3146 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3147 const VkImageResolve* pRegions) const {
3148 bool skip = false;
3149
3150 skip |= VendorCheckEnabled(kBPVendorArm) &&
3151 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage_ResolvingImage,
3152 "%s Attempting to use vkCmdResolveImage to resolve a multisampled image. "
3153 "This is a very slow and extremely bandwidth intensive path. "
3154 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3155 VendorSpecificTag(kBPVendorArm));
3156
3157 return skip;
3158}
3159
Jeff Leger178b1e52020-10-05 12:22:23 -04003160bool BestPractices::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
3161 const VkResolveImageInfo2KHR* pResolveImageInfo) const {
3162 bool skip = false;
3163
3164 skip |= VendorCheckEnabled(kBPVendorArm) &&
3165 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2KHR_ResolvingImage,
3166 "%s Attempting to use vkCmdResolveImage2KHR to resolve a multisampled image. "
3167 "This is a very slow and extremely bandwidth intensive path. "
3168 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3169 VendorSpecificTag(kBPVendorArm));
3170
3171 return skip;
3172}
3173
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003174bool BestPractices::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer,
3175 const VkResolveImageInfo2* pResolveImageInfo) const {
3176 bool skip = false;
3177
3178 skip |= VendorCheckEnabled(kBPVendorArm) &&
3179 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2_ResolvingImage,
3180 "%s Attempting to use vkCmdResolveImage2 to resolve a multisampled image. "
3181 "This is a very slow and extremely bandwidth intensive path. "
3182 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3183 VendorSpecificTag(kBPVendorArm));
3184
3185 return skip;
3186}
3187
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003188void BestPractices::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3189 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3190 const VkImageResolve* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003191 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003192 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003193 auto src = Get<bp_state::Image>(srcImage);
3194 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003195
3196 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003197 QueueValidateImage(funcs, "vkCmdResolveImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pRegions[i].srcSubresource);
3198 QueueValidateImage(funcs, "vkCmdResolveImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003199 }
3200}
3201
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003202void BestPractices::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
3203 const VkResolveImageInfo2KHR* pResolveImageInfo) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003204 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003205 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003206 auto src = Get<bp_state::Image>(pResolveImageInfo->srcImage);
3207 auto dst = Get<bp_state::Image>(pResolveImageInfo->dstImage);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003208 uint32_t regionCount = pResolveImageInfo->regionCount;
3209
3210 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003211 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pResolveImageInfo->pRegions[i].srcSubresource);
3212 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pResolveImageInfo->pRegions[i].dstSubresource);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003213 }
3214}
3215
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003216void BestPractices::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer,
3217 const VkResolveImageInfo2* pResolveImageInfo) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003218 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003219 auto& funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003220 auto src = Get<bp_state::Image>(pResolveImageInfo->srcImage);
3221 auto dst = Get<bp_state::Image>(pResolveImageInfo->dstImage);
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003222 uint32_t regionCount = pResolveImageInfo->regionCount;
3223
3224 for (uint32_t i = 0; i < regionCount; i++) {
3225 QueueValidateImage(funcs, "vkCmdResolveImage2()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ,
3226 pResolveImageInfo->pRegions[i].srcSubresource);
3227 QueueValidateImage(funcs, "vkCmdResolveImage2()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE,
3228 pResolveImageInfo->pRegions[i].dstSubresource);
3229 }
3230}
3231
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003232void BestPractices::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3233 const VkClearColorValue* pColor, uint32_t rangeCount,
3234 const VkImageSubresourceRange* pRanges) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003235 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003236 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003237 auto dst = Get<bp_state::Image>(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003238
3239 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003240 QueueValidateImage(funcs, "vkCmdClearColorImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003241 }
3242}
3243
3244void BestPractices::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3245 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
3246 const VkImageSubresourceRange* pRanges) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003247 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003248 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003249 auto dst = Get<bp_state::Image>(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003250
3251 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003252 QueueValidateImage(funcs, "vkCmdClearDepthStencilImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003253 }
3254}
3255
3256void BestPractices::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3257 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3258 const VkImageCopy* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003259 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003260 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003261 auto src = Get<bp_state::Image>(srcImage);
3262 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003263
3264 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003265 QueueValidateImage(funcs, "vkCmdCopyImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].srcSubresource);
3266 QueueValidateImage(funcs, "vkCmdCopyImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003267 }
3268}
3269
3270void BestPractices::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3271 VkImageLayout dstImageLayout, uint32_t regionCount,
3272 const VkBufferImageCopy* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003273 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003274 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003275 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003276
3277 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003278 QueueValidateImage(funcs, "vkCmdCopyBufferToImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003279 }
3280}
3281
3282void BestPractices::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3283 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003284 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003285 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003286 auto src = Get<bp_state::Image>(srcImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003287
3288 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003289 QueueValidateImage(funcs, "vkCmdCopyImageToBuffer()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003290 }
3291}
3292
3293void BestPractices::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3294 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3295 const VkImageBlit* pRegions, VkFilter filter) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003296 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003297 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003298 auto src = Get<bp_state::Image>(srcImage);
3299 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003300
3301 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003302 QueueValidateImage(funcs, "vkCmdBlitImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_READ, pRegions[i].srcSubresource);
3303 QueueValidateImage(funcs, "vkCmdBlitImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003304 }
3305}
3306
Attilio Provenzano02859b22020-02-27 14:17:28 +00003307bool BestPractices::PreCallValidateCreateSampler(VkDevice device, const VkSamplerCreateInfo* pCreateInfo,
3308 const VkAllocationCallbacks* pAllocator, VkSampler* pSampler) const {
3309 bool skip = false;
3310
3311 if (VendorCheckEnabled(kBPVendorArm)) {
3312 if ((pCreateInfo->addressModeU != pCreateInfo->addressModeV) || (pCreateInfo->addressModeV != pCreateInfo->addressModeW)) {
3313 skip |= LogPerformanceWarning(
3314 device, kVUID_BestPractices_CreateSampler_DifferentWrappingModes,
3315 "%s Creating a sampler object with wrapping modes which do not match (U = %u, V = %u, W = %u). "
3316 "This may cause reduced performance even if only U (1D image) or U/V wrapping modes (2D "
3317 "image) are actually used. If you need different wrapping modes, disregard this warning.",
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -06003318 VendorSpecificTag(kBPVendorArm), pCreateInfo->addressModeU, pCreateInfo->addressModeV, pCreateInfo->addressModeW);
Attilio Provenzano02859b22020-02-27 14:17:28 +00003319 }
3320
3321 if ((pCreateInfo->minLod != 0.0f) || (pCreateInfo->maxLod < VK_LOD_CLAMP_NONE)) {
3322 skip |= LogPerformanceWarning(
3323 device, kVUID_BestPractices_CreateSampler_LodClamping,
3324 "%s Creating a sampler object with LOD clamping (minLod = %f, maxLod = %f). This may cause reduced performance. "
3325 "Instead of clamping LOD in the sampler, consider using an VkImageView which restricts the mip-levels, set minLod "
3326 "to 0.0, and maxLod to VK_LOD_CLAMP_NONE.",
3327 VendorSpecificTag(kBPVendorArm), pCreateInfo->minLod, pCreateInfo->maxLod);
3328 }
3329
3330 if (pCreateInfo->mipLodBias != 0.0f) {
3331 skip |=
3332 LogPerformanceWarning(device, kVUID_BestPractices_CreateSampler_LodBias,
3333 "%s Creating a sampler object with LOD bias != 0.0 (%f). This will lead to less efficient "
3334 "descriptors being created and may cause reduced performance.",
3335 VendorSpecificTag(kBPVendorArm), pCreateInfo->mipLodBias);
3336 }
3337
3338 if ((pCreateInfo->addressModeU == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
3339 pCreateInfo->addressModeV == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
3340 pCreateInfo->addressModeW == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) &&
3341 (pCreateInfo->borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK)) {
3342 skip |= LogPerformanceWarning(
3343 device, kVUID_BestPractices_CreateSampler_BorderClampColor,
3344 "%s Creating a sampler object with border clamping and borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK. "
3345 "This will lead to less efficient descriptors being created and may cause reduced performance. "
3346 "If possible, use VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK as the border color.",
3347 VendorSpecificTag(kBPVendorArm));
3348 }
3349
3350 if (pCreateInfo->unnormalizedCoordinates) {
3351 skip |= LogPerformanceWarning(
3352 device, kVUID_BestPractices_CreateSampler_UnnormalizedCoordinates,
3353 "%s Creating a sampler object with unnormalized coordinates. This will lead to less efficient "
3354 "descriptors being created and may cause reduced performance.",
3355 VendorSpecificTag(kBPVendorArm));
3356 }
3357
3358 if (pCreateInfo->anisotropyEnable) {
3359 skip |= LogPerformanceWarning(
3360 device, kVUID_BestPractices_CreateSampler_Anisotropy,
3361 "%s Creating a sampler object with anisotropy. This will lead to less efficient descriptors being created "
3362 "and may cause reduced performance.",
3363 VendorSpecificTag(kBPVendorArm));
3364 }
3365 }
3366
3367 return skip;
3368}
Sam Walls8e77e4f2020-03-16 20:47:40 +00003369
Nadav Gevaf0808442021-05-21 13:51:25 -04003370void BestPractices::PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
3371 const VkGraphicsPipelineCreateInfo* pCreateInfos,
3372 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
3373 void* cgpl_state) {
3374 ValidationStateTracker::PreCallRecordCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos, pAllocator,
3375 pPipelines);
3376 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003377 num_pso_ += createInfoCount;
Nadav Gevaf0808442021-05-21 13:51:25 -04003378}
3379
3380bool BestPractices::PreCallValidateUpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount,
3381 const VkWriteDescriptorSet* pDescriptorWrites, uint32_t descriptorCopyCount,
3382 const VkCopyDescriptorSet* pDescriptorCopies) const {
3383 bool skip = false;
3384 if (VendorCheckEnabled(kBPVendorAMD)) {
3385 if (descriptorCopyCount > 0) {
3386 skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_AvoidCopyingDescriptors,
3387 "%s Performance warning: copying descriptor sets is not recommended",
3388 VendorSpecificTag(kBPVendorAMD));
3389 }
3390 }
3391
3392 return skip;
3393}
3394
3395bool BestPractices::PreCallValidateCreateDescriptorUpdateTemplate(VkDevice device,
3396 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
3397 const VkAllocationCallbacks* pAllocator,
3398 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate) const {
3399 bool skip = false;
3400 if (VendorCheckEnabled(kBPVendorAMD)) {
3401 skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_PreferNonTemplate,
3402 "%s Performance warning: using DescriptorSetWithTemplate is not recommended. Prefer using "
3403 "vkUpdateDescriptorSet instead",
3404 VendorSpecificTag(kBPVendorAMD));
3405 }
3406
3407 return skip;
3408}
3409
3410bool BestPractices::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3411 const VkClearColorValue* pColor, uint32_t rangeCount,
3412 const VkImageSubresourceRange* pRanges) const {
3413 bool skip = false;
3414 if (VendorCheckEnabled(kBPVendorAMD)) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003415 skip |= LogPerformanceWarning(
3416 device, kVUID_BestPractices_ClearAttachment_ClearImage,
Nadav Gevaf0808442021-05-21 13:51:25 -04003417 "%s Performance warning: using vkCmdClearColorImage is not recommended. Prefer using LOAD_OP_CLEAR or "
3418 "vkCmdClearAttachments instead",
3419 VendorSpecificTag(kBPVendorAMD));
3420 }
3421
3422 return skip;
3423}
3424
3425bool BestPractices::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3426 VkImageLayout imageLayout,
3427 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
3428 const VkImageSubresourceRange* pRanges) const {
3429 bool skip = false;
3430 if (VendorCheckEnabled(kBPVendorAMD)) {
3431 skip |= LogPerformanceWarning(
3432 device, kVUID_BestPractices_ClearAttachment_ClearImage,
3433 "%s Performance warning: using vkCmdClearDepthStencilImage is not recommended. Prefer using LOAD_OP_CLEAR or "
3434 "vkCmdClearAttachments instead",
3435 VendorSpecificTag(kBPVendorAMD));
3436 }
3437
3438 return skip;
3439}
3440
3441bool BestPractices::PreCallValidateCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo* pCreateInfo,
3442 const VkAllocationCallbacks* pAllocator,
3443 VkPipelineLayout* pPipelineLayout) const {
3444 bool skip = false;
3445 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003446 uint32_t descriptor_size = enabled_features.core.robustBufferAccess ? 4 : 2;
Nadav Gevaf0808442021-05-21 13:51:25 -04003447 // Descriptor sets cost 1 DWORD each.
3448 // Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF.
3449 // Dynamic buffers cost 4 DWORDs each when robust buffer access is ON.
3450 // Push constants cost 1 DWORD per 4 bytes in the Push constant range.
3451 uint32_t pipeline_size = pCreateInfo->setLayoutCount; // in DWORDS
3452 for (uint32_t i = 0; i < pCreateInfo->setLayoutCount; i++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003453 auto descriptor_set_layout_state = Get<cvdescriptorset::DescriptorSetLayout>(pCreateInfo->pSetLayouts[i]);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003454 pipeline_size += descriptor_set_layout_state->GetDynamicDescriptorCount() * descriptor_size;
Nadav Gevaf0808442021-05-21 13:51:25 -04003455 }
3456
3457 for (uint32_t i = 0; i < pCreateInfo->pushConstantRangeCount; i++) {
3458 pipeline_size += pCreateInfo->pPushConstantRanges[i].size / 4;
3459 }
3460
3461 if (pipeline_size > kPipelineLayoutSizeWarningLimitAMD) {
3462 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelinesLayout_KeepLayoutSmall,
3463 "%s Performance warning: pipeline layout size is too large. Prefer smaller pipeline layouts."
3464 "Descriptor sets cost 1 DWORD each. "
3465 "Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF. "
3466 "Dynamic buffers cost 4 DWORDs each when robust buffer access is ON. "
3467 "Push constants cost 1 DWORD per 4 bytes in the Push constant range. ",
3468 VendorSpecificTag(kBPVendorAMD));
3469 }
3470 }
3471
3472 return skip;
3473}
3474
3475bool BestPractices::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3476 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3477 const VkImageCopy* pRegions) const {
3478 bool skip = false;
3479 std::stringstream src_image_hex;
3480 std::stringstream dst_image_hex;
3481 src_image_hex << "0x" << std::hex << HandleToUint64(srcImage);
3482 dst_image_hex << "0x" << std::hex << HandleToUint64(dstImage);
3483
3484 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003485 auto src_state = Get<IMAGE_STATE>(srcImage);
3486 auto dst_state = Get<IMAGE_STATE>(dstImage);
Nadav Gevaf0808442021-05-21 13:51:25 -04003487
3488 if (src_state && dst_state) {
3489 VkImageTiling src_Tiling = src_state->createInfo.tiling;
3490 VkImageTiling dst_Tiling = dst_state->createInfo.tiling;
3491 if (src_Tiling != dst_Tiling && (src_Tiling == VK_IMAGE_TILING_LINEAR || dst_Tiling == VK_IMAGE_TILING_LINEAR)) {
3492 skip |=
3493 LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidImageToImageCopy,
3494 "%s Performance warning: image %s and image %s have differing tilings. Use buffer to "
3495 "image (vkCmdCopyImageToBuffer) "
3496 "and image to buffer (vkCmdCopyBufferToImage) copies instead of image to image "
3497 "copies when converting between linear and optimal images",
3498 VendorSpecificTag(kBPVendorAMD), src_image_hex.str().c_str(), dst_image_hex.str().c_str());
3499 }
3500 }
3501 }
3502
3503 return skip;
3504}
3505
3506bool BestPractices::PreCallValidateCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
3507 VkPipeline pipeline) const {
3508 bool skip = false;
3509
3510 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003511 if (IsPipelineUsedInFrame(pipeline)) {
Nadav Gevaf0808442021-05-21 13:51:25 -04003512 skip |= LogPerformanceWarning(device, kVUID_BestPractices_Pipeline_SortAndBind,
3513 "%s Performance warning: Pipeline %s was bound twice in the frame. Keep pipeline state changes to a minimum,"
3514 "for example, by sorting draw calls by pipeline.",
3515 VendorSpecificTag(kBPVendorAMD), report_data->FormatHandle(pipeline).c_str());
3516 }
3517 }
3518
3519 return skip;
3520}
3521
3522void BestPractices::ManualPostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
3523 VkFence fence, VkResult result) {
3524 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003525 num_queue_submissions_ += submitCount;
Nadav Gevaf0808442021-05-21 13:51:25 -04003526}
3527
3528bool BestPractices::PreCallValidateQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) const {
3529 bool skip = false;
3530
3531 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003532 auto num = num_queue_submissions_.load();
3533 if (num > kNumberOfSubmissionWarningLimitAMD) {
3534 skip |= LogPerformanceWarning(device, kVUID_BestPractices_Submission_ReduceNumberOfSubmissions,
3535 "%s Performance warning: command buffers submitted %" PRId32
3536 " times this frame. Submitting command buffers has a CPU "
3537 "and GPU overhead. Submit fewer times to incur less overhead.",
3538 VendorSpecificTag(kBPVendorAMD), num);
Nadav Gevaf0808442021-05-21 13:51:25 -04003539 }
3540 }
3541
3542 return skip;
3543}
3544
3545void BestPractices::PostCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3546 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3547 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
3548 uint32_t bufferMemoryBarrierCount,
3549 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
3550 uint32_t imageMemoryBarrierCount,
3551 const VkImageMemoryBarrier* pImageMemoryBarriers) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003552 num_barriers_objects_ += (memoryBarrierCount + imageMemoryBarrierCount + bufferMemoryBarrierCount);
Nadav Gevaf0808442021-05-21 13:51:25 -04003553}
3554
3555bool BestPractices::PreCallValidateCreateSemaphore(VkDevice device, const VkSemaphoreCreateInfo* pCreateInfo,
3556 const VkAllocationCallbacks* pAllocator, VkSemaphore* pSemaphore) const {
3557 bool skip = false;
3558 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003559 if (Count<SEMAPHORE_STATE>() > kMaxRecommendedSemaphoreObjectsSizeAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -04003560 skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfSemaphores,
3561 "%s Performance warning: High number of vkSemaphore objects created."
3562 "Minimize the amount of queue synchronization that is used. "
3563 "Semaphores and fences have overhead. Each fence has a CPU and GPU cost with it.",
3564 VendorSpecificTag(kBPVendorAMD));
3565 }
3566 }
3567
3568 return skip;
3569}
3570
3571bool BestPractices::PreCallValidateCreateFence(VkDevice device, const VkFenceCreateInfo* pCreateInfo,
3572 const VkAllocationCallbacks* pAllocator, VkFence* pFence) const {
3573 bool skip = false;
3574 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003575 if (Count<FENCE_STATE>() > kMaxRecommendedFenceObjectsSizeAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -04003576 skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfFences,
3577 "%s Performance warning: High number of VkFence objects created."
3578 "Minimize the amount of CPU-GPU synchronization that is used. "
3579 "Semaphores and fences have overhead.Each fence has a CPU and GPU cost with it.",
3580 VendorSpecificTag(kBPVendorAMD));
3581 }
3582 }
3583
3584 return skip;
3585}
3586
Sam Walls8e77e4f2020-03-16 20:47:40 +00003587void BestPractices::PostTransformLRUCacheModel::resize(size_t size) { _entries.resize(size); }
3588
3589bool BestPractices::PostTransformLRUCacheModel::query_cache(uint32_t value) {
3590 // look for a cache hit
3591 auto hit = std::find_if(_entries.begin(), _entries.end(), [value](const CacheEntry& entry) { return entry.value == value; });
3592 if (hit != _entries.end()) {
3593 // mark the cache hit as being most recently used
3594 hit->age = iteration++;
3595 return true;
3596 }
3597
3598 // if there's no cache hit, we need to model the entry being inserted into the cache
3599 CacheEntry new_entry = {value, iteration};
3600 if (iteration < static_cast<uint32_t>(std::distance(_entries.begin(), _entries.end()))) {
3601 // if there is still space left in the cache, use the next available slot
3602 *(_entries.begin() + iteration) = new_entry;
3603 } else {
3604 // otherwise replace the least recently used cache entry
3605 auto lru = std::min_element(_entries.begin(), hit, [](const CacheEntry& a, const CacheEntry& b) { return a.age < b.age; });
3606 *lru = new_entry;
3607 }
3608 iteration++;
3609 return false;
3610}
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003611
3612bool BestPractices::PreCallValidateAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout,
3613 VkSemaphore semaphore, VkFence fence, uint32_t* pImageIndex) const {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003614 auto swapchain_data = Get<SWAPCHAIN_NODE>(swapchain);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003615 bool skip = false;
3616 if (swapchain_data && swapchain_data->images.size() == 0) {
3617 skip |= LogWarning(swapchain, kVUID_Core_DrawState_SwapchainImagesNotFound,
3618 "vkAcquireNextImageKHR: No images found to acquire from. Application probably did not call "
3619 "vkGetSwapchainImagesKHR after swapchain creation.");
3620 }
3621 return skip;
3622}
3623
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003624void BestPractices::CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(CALL_STATE& call_state, bool no_pointer) {
3625 if (no_pointer) {
3626 if (UNCALLED == call_state) {
3627 call_state = QUERY_COUNT;
3628 }
3629 } else { // Save queue family properties
3630 call_state = QUERY_DETAILS;
3631 }
3632}
3633
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003634void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
3635 uint32_t* pQueueFamilyPropertyCount,
3636 VkQueueFamilyProperties* pQueueFamilyProperties) {
3637 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount,
3638 pQueueFamilyProperties);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003639 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003640 if (bp_pd_state) {
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003641 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
3642 nullptr == pQueueFamilyProperties);
3643 }
3644}
3645
3646void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
3647 uint32_t* pQueueFamilyPropertyCount,
3648 VkQueueFamilyProperties2* pQueueFamilyProperties) {
3649 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(physicalDevice, pQueueFamilyPropertyCount,
3650 pQueueFamilyProperties);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003651 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003652 if (bp_pd_state) {
3653 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
3654 nullptr == pQueueFamilyProperties);
3655 }
3656}
3657
3658void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(VkPhysicalDevice physicalDevice,
3659 uint32_t* pQueueFamilyPropertyCount,
3660 VkQueueFamilyProperties2* pQueueFamilyProperties) {
3661 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(physicalDevice, pQueueFamilyPropertyCount,
3662 pQueueFamilyProperties);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003663 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003664 if (bp_pd_state) {
3665 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
3666 nullptr == pQueueFamilyProperties);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003667 }
3668}
3669
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003670void BestPractices::PostCallRecordGetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice, VkPhysicalDeviceFeatures* pFeatures) {
3671 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures(physicalDevice, pFeatures);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003672 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003673 if (bp_pd_state) {
3674 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3675 }
3676}
3677
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003678void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
3679 VkPhysicalDeviceFeatures2* pFeatures) {
3680 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003681 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003682 if (bp_pd_state) {
3683 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3684 }
3685}
3686
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003687void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2KHR(VkPhysicalDevice physicalDevice,
3688 VkPhysicalDeviceFeatures2* pFeatures) {
3689 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003690 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003691 if (bp_pd_state) {
3692 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3693 }
3694}
3695
3696void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice physicalDevice,
3697 VkSurfaceKHR surface,
3698 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities,
3699 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003700 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003701 if (bp_pd_state) {
3702 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3703 }
3704}
3705
3706void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2KHR(
3707 VkPhysicalDevice physicalDevice, const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
3708 VkSurfaceCapabilities2KHR* pSurfaceCapabilities, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003709 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003710 if (bp_pd_state) {
3711 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3712 }
3713}
3714
3715void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2EXT(VkPhysicalDevice physicalDevice,
3716 VkSurfaceKHR surface,
3717 VkSurfaceCapabilities2EXT* pSurfaceCapabilities,
3718 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003719 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003720 if (bp_pd_state) {
3721 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3722 }
3723}
3724
3725void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice physicalDevice,
3726 VkSurfaceKHR surface, uint32_t* pPresentModeCount,
3727 VkPresentModeKHR* pPresentModes, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003728 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003729 if (bp_pd_data) {
3730 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfacePresentModesKHRState;
3731
3732 if (*pPresentModeCount) {
3733 if (call_state < QUERY_COUNT) {
3734 call_state = QUERY_COUNT;
3735 }
3736 }
3737 if (pPresentModes) {
3738 if (call_state < QUERY_DETAILS) {
3739 call_state = QUERY_DETAILS;
3740 }
3741 }
3742 }
3743}
3744
3745void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
3746 uint32_t* pSurfaceFormatCount,
3747 VkSurfaceFormatKHR* pSurfaceFormats, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003748 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003749 if (bp_pd_data) {
3750 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState;
3751
3752 if (*pSurfaceFormatCount) {
3753 if (call_state < QUERY_COUNT) {
3754 call_state = QUERY_COUNT;
3755 }
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06003756 bp_pd_data->surface_formats_count = *pSurfaceFormatCount;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003757 }
3758 if (pSurfaceFormats) {
3759 if (call_state < QUERY_DETAILS) {
3760 call_state = QUERY_DETAILS;
3761 }
3762 }
3763 }
3764}
3765
3766void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice physicalDevice,
3767 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
3768 uint32_t* pSurfaceFormatCount,
3769 VkSurfaceFormat2KHR* pSurfaceFormats, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003770 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003771 if (bp_pd_data) {
3772 if (*pSurfaceFormatCount) {
3773 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_COUNT) {
3774 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_COUNT;
3775 }
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06003776 bp_pd_data->surface_formats_count = *pSurfaceFormatCount;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003777 }
3778 if (pSurfaceFormats) {
3779 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_DETAILS) {
3780 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_DETAILS;
3781 }
3782 }
3783 }
3784}
3785
3786void BestPractices::ManualPostCallRecordGetPhysicalDeviceDisplayPlanePropertiesKHR(VkPhysicalDevice physicalDevice,
3787 uint32_t* pPropertyCount,
3788 VkDisplayPlanePropertiesKHR* pProperties,
3789 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003790 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003791 if (bp_pd_data) {
3792 if (*pPropertyCount) {
3793 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_COUNT) {
3794 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_COUNT;
3795 }
3796 }
3797 if (pProperties) {
3798 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_DETAILS) {
3799 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_DETAILS;
3800 }
3801 }
3802 }
3803}
3804
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003805void BestPractices::ManualPostCallRecordGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain,
3806 uint32_t* pSwapchainImageCount, VkImage* pSwapchainImages,
3807 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003808 auto swapchain_state = Get<bp_state::Swapchain>(swapchain);
Nathaniel Cesario39152e62021-07-02 13:04:16 -06003809 if (swapchain_state && (pSwapchainImages || *pSwapchainImageCount)) {
3810 if (swapchain_state->vkGetSwapchainImagesKHRState < QUERY_DETAILS) {
3811 swapchain_state->vkGetSwapchainImagesKHRState = QUERY_DETAILS;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003812 }
3813 }
3814}
3815
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003816void BestPractices::PreCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence) {
3817 ValidationStateTracker::PreCallRecordQueueSubmit(queue, submitCount, pSubmits, fence);
3818
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06003819 auto queue_state = Get<QUEUE_STATE>(queue);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003820 for (uint32_t submit = 0; submit < submitCount; submit++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02003821 const auto& submit_info = pSubmits[submit];
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003822 for (uint32_t cb_index = 0; cb_index < submit_info.commandBufferCount; cb_index++) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003823 auto cb = GetWrite<bp_state::CommandBuffer>(submit_info.pCommandBuffers[cb_index]);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003824 for (auto &func : cb->queue_submit_functions) {
Jeremy Gebbene5361dd2021-11-18 14:23:56 -07003825 func(*this, *queue_state, *cb);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003826 }
3827 }
3828 }
3829}