blob: c814afb37afe2f7e0f6395c74388614ea4ad4665 [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
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -070040const std::map<BPVendorFlagBits, VendorSpecificInfo> kVendorInfo = {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -060041 {kBPVendorArm, {vendor_specific_arm, "Arm"}},
Nadav Geva41c12a22021-05-21 13:14:05 -040042 {kBPVendorAMD, {vendor_specific_amd, "AMD"}},
Attilio Provenzano19d6a982020-02-27 12:41:41 +000043};
44
Hannes Harnisch607d1d92021-07-10 18:44:56 +020045const SpecialUseVUIDs kSpecialUseInstanceVUIDs {
46 kVUID_BestPractices_CreateInstance_SpecialUseExtension_CADSupport,
47 kVUID_BestPractices_CreateInstance_SpecialUseExtension_D3DEmulation,
48 kVUID_BestPractices_CreateInstance_SpecialUseExtension_DevTools,
49 kVUID_BestPractices_CreateInstance_SpecialUseExtension_Debugging,
50 kVUID_BestPractices_CreateInstance_SpecialUseExtension_GLEmulation,
51};
52
53const SpecialUseVUIDs kSpecialUseDeviceVUIDs {
54 kVUID_BestPractices_CreateDevice_SpecialUseExtension_CADSupport,
55 kVUID_BestPractices_CreateDevice_SpecialUseExtension_D3DEmulation,
56 kVUID_BestPractices_CreateDevice_SpecialUseExtension_DevTools,
57 kVUID_BestPractices_CreateDevice_SpecialUseExtension_Debugging,
58 kVUID_BestPractices_CreateDevice_SpecialUseExtension_GLEmulation,
59};
60
Jeremy Gebben20da7a12022-02-25 14:07:46 -070061ReadLockGuard BestPractices::ReadLock() {
62 if (fine_grained_locking) {
63 return ReadLockGuard(validation_object_mutex, std::defer_lock);
64 } else {
65 return ReadLockGuard(validation_object_mutex);
66 }
67}
68
69WriteLockGuard BestPractices::WriteLock() {
70 if (fine_grained_locking) {
71 return WriteLockGuard(validation_object_mutex, std::defer_lock);
72 } else {
73 return WriteLockGuard(validation_object_mutex);
74 }
75}
76
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -060077std::shared_ptr<CMD_BUFFER_STATE> BestPractices::CreateCmdBufferState(VkCommandBuffer cb,
78 const VkCommandBufferAllocateInfo* pCreateInfo,
Jeremy Gebbencd7fa282021-10-27 10:25:32 -060079 const COMMAND_POOL_STATE* pool) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -070080 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 -060081}
82
Jeremy Gebben20da7a12022-02-25 14:07:46 -070083bp_state::CommandBuffer::CommandBuffer(BestPractices* bp, VkCommandBuffer cb, const VkCommandBufferAllocateInfo* pCreateInfo,
84 const COMMAND_POOL_STATE* pool)
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -060085 : CMD_BUFFER_STATE(bp, cb, pCreateInfo, pool) {}
86
Attilio Provenzano19d6a982020-02-27 12:41:41 +000087bool BestPractices::VendorCheckEnabled(BPVendorFlags vendors) const {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -070088 for (const auto& vendor : kVendorInfo) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -060089 if (vendors & vendor.first && enabled[vendor.second.vendor_id]) {
Attilio Provenzano19d6a982020-02-27 12:41:41 +000090 return true;
91 }
92 }
93 return false;
94}
95
96const char* VendorSpecificTag(BPVendorFlags vendors) {
97 // Cache built vendor tags in a map
Jeremy Gebbencbf22862021-03-03 12:01:22 -070098 static layer_data::unordered_map<BPVendorFlags, std::string> tag_map;
Attilio Provenzano19d6a982020-02-27 12:41:41 +000099
100 auto res = tag_map.find(vendors);
101 if (res == tag_map.end()) {
102 // Build the vendor tag string
103 std::stringstream vendor_tag;
104
105 vendor_tag << "[";
106 bool first_vendor = true;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700107 for (const auto& vendor : kVendorInfo) {
Attilio Provenzano19d6a982020-02-27 12:41:41 +0000108 if (vendors & vendor.first) {
109 if (!first_vendor) {
110 vendor_tag << ", ";
111 }
112 vendor_tag << vendor.second.name;
113 first_vendor = false;
114 }
115 }
116 vendor_tag << "]";
117
118 tag_map[vendors] = vendor_tag.str();
119 res = tag_map.find(vendors);
120 }
121
122 return res->second.c_str();
123}
124
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700125const char* DepReasonToString(ExtDeprecationReason reason) {
126 switch (reason) {
127 case kExtPromoted:
128 return "promoted to";
129 break;
130 case kExtObsoleted:
131 return "obsoleted by";
132 break;
133 case kExtDeprecated:
134 return "deprecated by";
135 break;
136 default:
137 return "";
138 break;
139 }
140}
141
142bool BestPractices::ValidateDeprecatedExtensions(const char* api_name, const char* extension_name, uint32_t version,
143 const char* vuid) const {
144 bool skip = false;
145 auto dep_info_it = deprecated_extensions.find(extension_name);
146 if (dep_info_it != deprecated_extensions.end()) {
147 auto dep_info = dep_info_it->second;
Mark Lobodzinski6a149702020-05-14 12:21:34 -0600148 if (((dep_info.target.compare("VK_VERSION_1_1") == 0) && (version >= VK_API_VERSION_1_1)) ||
Tony-LunarGc30b59f2022-02-15 11:02:36 -0700149 ((dep_info.target.compare("VK_VERSION_1_2") == 0) && (version >= VK_API_VERSION_1_2)) ||
150 ((dep_info.target.compare("VK_VERSION_1_3") == 0) && (version >= VK_API_VERSION_1_3))) {
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700151 skip |=
152 LogWarning(instance, vuid, "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.",
153 api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str());
Mark Lobodzinski6a149702020-05-14 12:21:34 -0600154 } else if (dep_info.target.find("VK_VERSION") == std::string::npos) {
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700155 if (dep_info.target.length() == 0) {
156 skip |= LogWarning(instance, vuid,
157 "%s(): Attempting to enable deprecated extension %s, but this extension has been deprecated "
158 "without replacement.",
159 api_name, extension_name);
160 } else {
161 skip |= LogWarning(instance, vuid,
162 "%s(): Attempting to enable deprecated extension %s, but this extension has been %s %s.",
163 api_name, extension_name, DepReasonToString(dep_info.reason), (dep_info.target).c_str());
164 }
165 }
166 }
167 return skip;
168}
169
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200170bool BestPractices::ValidateSpecialUseExtensions(const char* api_name, const char* extension_name, const SpecialUseVUIDs& special_use_vuids) const
171{
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700172 bool skip = false;
173 auto dep_info_it = special_use_extensions.find(extension_name);
174
175 if (dep_info_it != special_use_extensions.end()) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200176 const char* const format = "%s(): Attempting to enable extension %s, but this extension is intended to support %s "
177 "and it is strongly recommended that it be otherwise avoided.";
178 auto& special_uses = dep_info_it->second;
sfricke-samsungef15e482022-01-26 11:32:49 -0800179
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700180 if (special_uses.find("cadsupport") != std::string::npos) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800181 skip |= LogWarning(instance, special_use_vuids.cadsupport, format, api_name, extension_name,
182 "specialized functionality used by CAD/CAM applications");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700183 }
184 if (special_uses.find("d3demulation") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200185 skip |= LogWarning(instance, special_use_vuids.d3demulation, format, api_name, extension_name,
186 "D3D emulation layers, and applications ported from D3D, by adding functionality specific to D3D");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700187 }
188 if (special_uses.find("devtools") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200189 skip |= LogWarning(instance, special_use_vuids.devtools, format, api_name, extension_name,
190 "developer tools such as capture-replay libraries");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700191 }
192 if (special_uses.find("debugging") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200193 skip |= LogWarning(instance, special_use_vuids.debugging, format, api_name, extension_name,
194 "use by applications when debugging");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700195 }
196 if (special_uses.find("glemulation") != std::string::npos) {
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200197 skip |= LogWarning(instance, special_use_vuids.glemulation, format, api_name, extension_name,
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700198 "OpenGL and/or OpenGL ES emulation layers, and applications ported from those APIs, by adding functionality "
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200199 "specific to those APIs");
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700200 }
Mark Lobodzinski057724a2020-11-09 17:13:18 -0700201 }
202 return skip;
203}
204
Nadav Gevaf0808442021-05-21 13:51:25 -0400205void BestPractices::InitDeviceValidationObject(bool add_obj, ValidationObject* inst_obj, ValidationObject* dev_obj) {
206 if (add_obj) {
207 ValidationStateTracker::InitDeviceValidationObject(add_obj, inst_obj, dev_obj);
208 }
209}
210
211
Camden5b184be2019-08-13 07:50:19 -0600212bool BestPractices::PreCallValidateCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500213 VkInstance* pInstance) const {
Camden5b184be2019-08-13 07:50:19 -0600214 bool skip = false;
215
216 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
217 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kDeviceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -0800218 skip |= LogWarning(instance, kVUID_BestPractices_CreateInstance_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700219 "vkCreateInstance(): Attempting to enable Device Extension %s at CreateInstance time.",
220 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -0600221 }
Mark Lobodzinski17d8dc62020-06-03 08:48:58 -0600222 uint32_t specified_version =
223 (pCreateInfo->pApplicationInfo ? pCreateInfo->pApplicationInfo->apiVersion : VK_API_VERSION_1_0);
224 skip |= ValidateDeprecatedExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i], specified_version,
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700225 kVUID_BestPractices_CreateInstance_DeprecatedExtension);
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200226 skip |= ValidateSpecialUseExtensions("CreateInstance", pCreateInfo->ppEnabledExtensionNames[i], kSpecialUseInstanceVUIDs);
Camden5b184be2019-08-13 07:50:19 -0600227 }
228
229 return skip;
230}
231
Camden5b184be2019-08-13 07:50:19 -0600232bool BestPractices::PreCallValidateCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500233 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) const {
Camden5b184be2019-08-13 07:50:19 -0600234 bool skip = false;
235
236 // get API version of physical device passed when creating device.
237 VkPhysicalDeviceProperties physical_device_properties{};
238 DispatchGetPhysicalDeviceProperties(physicalDevice, &physical_device_properties);
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500239 auto device_api_version = physical_device_properties.apiVersion;
Camden5b184be2019-08-13 07:50:19 -0600240
241 // check api versions and warn if instance api Version is higher than version on device.
Jeremy Gebben404f6ac2021-10-28 12:33:28 -0600242 if (api_version > device_api_version) {
243 std::string inst_api_name = StringAPIVersion(api_version);
Mark Lobodzinski60880782020-08-11 08:02:07 -0600244 std::string dev_api_name = StringAPIVersion(device_api_version);
Camden5b184be2019-08-13 07:50:19 -0600245
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700246 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_API_Mismatch,
247 "vkCreateDevice(): API Version of current instance, %s is higher than API Version on device, %s",
248 inst_api_name.c_str(), dev_api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -0600249 }
250
251 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
252 if (white_list(pCreateInfo->ppEnabledExtensionNames[i], kInstanceExtensionNames)) {
Camden Stocker11ecf512020-01-21 16:06:49 -0800253 skip |= LogWarning(instance, kVUID_BestPractices_CreateDevice_ExtensionMismatch,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700254 "vkCreateDevice(): Attempting to enable Instance Extension %s at CreateDevice time.",
255 pCreateInfo->ppEnabledExtensionNames[i]);
Camden5b184be2019-08-13 07:50:19 -0600256 }
Jeremy Gebben404f6ac2021-10-28 12:33:28 -0600257 skip |= ValidateDeprecatedExtensions("CreateDevice", pCreateInfo->ppEnabledExtensionNames[i], api_version,
Mark Lobodzinski6167e102020-02-24 17:03:55 -0700258 kVUID_BestPractices_CreateDevice_DeprecatedExtension);
Hannes Harnisch607d1d92021-07-10 18:44:56 +0200259 skip |= ValidateSpecialUseExtensions("CreateDevice", pCreateInfo->ppEnabledExtensionNames[i], kSpecialUseDeviceVUIDs);
Camden5b184be2019-08-13 07:50:19 -0600260 }
261
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700262 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600263 if ((bp_pd_state->vkGetPhysicalDeviceFeaturesState == UNCALLED) && (pCreateInfo->pEnabledFeatures != NULL)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700264 skip |= LogWarning(device, kVUID_BestPractices_CreateDevice_PDFeaturesNotCalled,
265 "vkCreateDevice() called before getting physical device features from vkGetPhysicalDeviceFeatures().");
Camden83a9c372019-08-14 11:41:38 -0600266 }
267
Nadav Gevaf0808442021-05-21 13:51:25 -0400268 if ((VendorCheckEnabled(kBPVendorArm) || VendorCheckEnabled(kBPVendorAMD)) && (pCreateInfo->pEnabledFeatures != nullptr) &&
Szilard Papp7d2c7952020-06-22 14:38:13 +0100269 (pCreateInfo->pEnabledFeatures->robustBufferAccess == VK_TRUE)) {
270 skip |= LogPerformanceWarning(
271 device, kVUID_BestPractices_CreateDevice_RobustBufferAccess,
Nadav Gevaf0808442021-05-21 13:51:25 -0400272 "%s %s vkCreateDevice() called with enabled robustBufferAccess. Use robustBufferAccess as a debugging tool during "
Szilard Papp7d2c7952020-06-22 14:38:13 +0100273 "development. Enabling it causes loss in performance for accesses to uniform buffers and shader storage "
274 "buffers. Disable robustBufferAccess in release builds. Only leave it enabled if the application use-case "
275 "requires the additional level of reliability due to the use of unverified user-supplied draw parameters.",
Nadav Gevaf0808442021-05-21 13:51:25 -0400276 VendorSpecificTag(kBPVendorArm), VendorSpecificTag(kBPVendorAMD));
Szilard Papp7d2c7952020-06-22 14:38:13 +0100277 }
278
Camden5b184be2019-08-13 07:50:19 -0600279 return skip;
280}
281
282bool BestPractices::PreCallValidateCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500283 const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer) const {
Camden5b184be2019-08-13 07:50:19 -0600284 bool skip = false;
285
286 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700287 std::stringstream buffer_hex;
288 buffer_hex << "0x" << std::hex << HandleToUint64(pBuffer);
Camden5b184be2019-08-13 07:50:19 -0600289
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700290 skip |= LogWarning(
291 device, kVUID_BestPractices_SharingModeExclusive,
292 "Warning: Buffer (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
293 "(queueFamilyIndexCount of %" PRIu32 ").",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700294 buffer_hex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600295 }
296
297 return skip;
298}
299
300bool BestPractices::PreCallValidateCreateImage(VkDevice device, const VkImageCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500301 const VkAllocationCallbacks* pAllocator, VkImage* pImage) const {
Camden5b184be2019-08-13 07:50:19 -0600302 bool skip = false;
303
304 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700305 std::stringstream image_hex;
306 image_hex << "0x" << std::hex << HandleToUint64(pImage);
Camden5b184be2019-08-13 07:50:19 -0600307
308 skip |=
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700309 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
310 "Warning: Image (%s) specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple queues "
311 "(queueFamilyIndexCount of %" PRIu32 ").",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700312 image_hex.str().c_str(), pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600313 }
314
ziga-lunarg6df3d102022-03-18 17:02:14 +0100315 if ((pCreateInfo->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT) && !(pCreateInfo->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) {
316 skip |= LogWarning(device, kVUID_BestPractices_ImageCreateFlags,
317 "vkCreateImage(): pCreateInfo->flags has VK_IMAGE_CREATE_EXTENDED_USAGE_BIT set, but not "
318 "VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, therefore image views created from this image will have to use the "
319 "same format and VK_IMAGE_CREATE_EXTENDED_USAGE_BIT will not have any effect.");
320 }
321
Attilio Provenzano02859b22020-02-27 14:17:28 +0000322 if (VendorCheckEnabled(kBPVendorArm)) {
323 if (pCreateInfo->samples > kMaxEfficientSamplesArm) {
324 skip |= LogPerformanceWarning(
325 device, kVUID_BestPractices_CreateImage_TooLargeSampleCount,
326 "%s vkCreateImage(): Trying to create an image with %u samples. "
327 "The hardware revision may not have full throughput for framebuffers with more than %u samples.",
328 VendorSpecificTag(kBPVendorArm), static_cast<uint32_t>(pCreateInfo->samples), kMaxEfficientSamplesArm);
329 }
330
331 if (pCreateInfo->samples > VK_SAMPLE_COUNT_1_BIT && !(pCreateInfo->usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT)) {
332 skip |= LogPerformanceWarning(
333 device, kVUID_BestPractices_CreateImage_NonTransientMSImage,
334 "%s vkCreateImage(): Trying to create a multisampled image, but createInfo.usage did not have "
335 "VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. Multisampled images may be resolved on-chip, "
336 "and do not need to be backed by physical storage. "
337 "TRANSIENT_ATTACHMENT allows tiled GPUs to not back the multisampled image with physical memory.",
338 VendorSpecificTag(kBPVendorArm));
339 }
340 }
341
Nadav Gevaf0808442021-05-21 13:51:25 -0400342 if (VendorCheckEnabled(kBPVendorAMD)) {
343 std::stringstream image_hex;
344 image_hex << "0x" << std::hex << HandleToUint64(pImage);
345
346 if ((pCreateInfo->usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
347 (pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT)) {
348 skip |= LogPerformanceWarning(device,
349 kVUID_BestPractices_vkImage_AvoidConcurrentRenderTargets,
350 "%s Performance warning: image (%s) is created as a render target with VK_SHARING_MODE_CONCURRENT. "
351 "Using a SHARING_MODE_CONCURRENT "
352 "is not recommended with color and depth targets",
353 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
354 }
355
356 if ((pCreateInfo->usage &
357 (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
358 (pCreateInfo->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) {
359 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_DontUseMutableRenderTargets,
360 "%s Performance warning: image (%s) is created as a render target with VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT. "
361 "Using a MUTABLE_FORMAT is not recommended with color, depth, and storage targets",
362 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
363 }
364
365 if ((pCreateInfo->usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
366 (pCreateInfo->usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
367 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_DontUseStorageRenderTargets,
368 "%s Performance warning: image (%s) is created as a render target with VK_IMAGE_USAGE_STORAGE_BIT. Using a "
369 "VK_IMAGE_USAGE_STORAGE_BIT is not recommended with color and depth targets",
370 VendorSpecificTag(kBPVendorAMD), image_hex.str().c_str());
371 }
372 }
373
Camden5b184be2019-08-13 07:50:19 -0600374 return skip;
375}
376
377bool BestPractices::PreCallValidateCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500378 const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) const {
Camden5b184be2019-08-13 07:50:19 -0600379 bool skip = false;
380
Jeremy Gebben383b9a32021-09-08 16:31:33 -0600381 const auto* bp_pd_state = GetPhysicalDeviceState();
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600382 if (bp_pd_state) {
383 if (bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState == UNCALLED) {
384 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
385 "vkCreateSwapchainKHR() called before getting surface capabilities from "
386 "vkGetPhysicalDeviceSurfaceCapabilitiesKHR().");
387 }
Camden83a9c372019-08-14 11:41:38 -0600388
Shannon McPherson73e58c82021-03-05 17:14:26 -0700389 if ((pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR) &&
390 (bp_pd_state->vkGetPhysicalDeviceSurfacePresentModesKHRState != QUERY_DETAILS)) {
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600391 skip |= LogWarning(device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
392 "vkCreateSwapchainKHR() called before getting surface present mode(s) from "
393 "vkGetPhysicalDeviceSurfacePresentModesKHR().");
394 }
Camden83a9c372019-08-14 11:41:38 -0600395
Nathaniel Cesario24184fe2020-10-06 12:46:12 -0600396 if (bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState != QUERY_DETAILS) {
397 skip |= LogWarning(
398 device, kVUID_BestPractices_Swapchain_GetSurfaceNotCalled,
399 "vkCreateSwapchainKHR() called before getting surface format(s) from vkGetPhysicalDeviceSurfaceFormatsKHR().");
400 }
Camden83a9c372019-08-14 11:41:38 -0600401 }
402
Camden5b184be2019-08-13 07:50:19 -0600403 if ((pCreateInfo->queueFamilyIndexCount > 1) && (pCreateInfo->imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700404 skip |=
405 LogWarning(device, kVUID_BestPractices_SharingModeExclusive,
Mark Lobodzinski019f4e32020-04-13 11:01:35 -0600406 "Warning: A Swapchain is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while "
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700407 "specifying multiple queues (queueFamilyIndexCount of %" PRIu32 ").",
408 pCreateInfo->queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600409 }
410
Szilard Papp48a6da32020-06-10 14:41:59 +0100411 if (pCreateInfo->minImageCount == 2) {
412 skip |= LogPerformanceWarning(
413 device, kVUID_BestPractices_SuboptimalSwapchainImageCount,
414 "Warning: A Swapchain is being created with minImageCount set to %" PRIu32
415 ", which means double buffering is going "
416 "to be used. Using double buffering and vsync locks rendering to an integer fraction of the vsync rate. In turn, "
417 "reducing the performance of the application if rendering is slower than vsync. Consider setting minImageCount to "
418 "3 to use triple buffering to maximize performance in such cases.",
419 pCreateInfo->minImageCount);
420 }
421
Szilard Pappd5f0f812020-06-22 09:01:29 +0100422 if (VendorCheckEnabled(kBPVendorArm) && (pCreateInfo->presentMode != VK_PRESENT_MODE_FIFO_KHR)) {
423 skip |= LogWarning(device, kVUID_BestPractices_CreateSwapchain_PresentMode,
424 "%s Warning: Swapchain is not being created with presentation mode \"VK_PRESENT_MODE_FIFO_KHR\". "
425 "Prefer using \"VK_PRESENT_MODE_FIFO_KHR\" to avoid unnecessary CPU and GPU load and save power. "
426 "Presentation modes which are not FIFO will present the latest available frame and discard other "
427 "frame(s) if any.",
428 VendorSpecificTag(kBPVendorArm));
429 }
430
Camden5b184be2019-08-13 07:50:19 -0600431 return skip;
432}
433
434bool BestPractices::PreCallValidateCreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount,
435 const VkSwapchainCreateInfoKHR* pCreateInfos,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500436 const VkAllocationCallbacks* pAllocator,
437 VkSwapchainKHR* pSwapchains) const {
Camden5b184be2019-08-13 07:50:19 -0600438 bool skip = false;
439
440 for (uint32_t i = 0; i < swapchainCount; i++) {
441 if ((pCreateInfos[i].queueFamilyIndexCount > 1) && (pCreateInfos[i].imageSharingMode == VK_SHARING_MODE_EXCLUSIVE)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700442 skip |= LogWarning(
443 device, kVUID_BestPractices_SharingModeExclusive,
444 "Warning: A shared swapchain (index %" PRIu32
445 ") is being created which specifies a sharing mode of VK_SHARING_MODE_EXCLUSIVE while specifying multiple "
446 "queues (queueFamilyIndexCount of %" PRIu32 ").",
447 i, pCreateInfos[i].queueFamilyIndexCount);
Camden5b184be2019-08-13 07:50:19 -0600448 }
449 }
450
451 return skip;
452}
453
454bool BestPractices::PreCallValidateCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500455 const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass) const {
Camden5b184be2019-08-13 07:50:19 -0600456 bool skip = false;
457
458 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
459 VkFormat format = pCreateInfo->pAttachments[i].format;
460 if (pCreateInfo->pAttachments[i].initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
461 if ((FormatIsColor(format) || FormatHasDepth(format)) &&
462 pCreateInfo->pAttachments[i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700463 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
464 "Render pass has an attachment with loadOp == VK_ATTACHMENT_LOAD_OP_LOAD and "
465 "initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
466 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
467 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600468 }
469 if (FormatHasStencil(format) && pCreateInfo->pAttachments[i].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700470 skip |= LogWarning(device, kVUID_BestPractices_RenderPass_Attatchment,
471 "Render pass has an attachment with stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD "
472 "and initialLayout == VK_IMAGE_LAYOUT_UNDEFINED. This is probably not what you "
473 "intended. Consider using VK_ATTACHMENT_LOAD_OP_DONT_CARE instead if the "
474 "image truely is undefined at the start of the render pass.");
Camden5b184be2019-08-13 07:50:19 -0600475 }
476 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000477
478 const auto& attachment = pCreateInfo->pAttachments[i];
479 if (attachment.samples > VK_SAMPLE_COUNT_1_BIT) {
480 bool access_requires_memory =
481 attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE;
482
483 if (FormatHasStencil(format)) {
484 access_requires_memory |= attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
485 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE;
486 }
487
488 if (access_requires_memory) {
489 skip |= LogPerformanceWarning(
490 device, kVUID_BestPractices_CreateRenderPass_ImageRequiresMemory,
491 "Attachment %u in the VkRenderPass is a multisampled image with %u samples, but it uses loadOp/storeOp "
492 "which requires accessing data from memory. Multisampled images should always be loadOp = CLEAR or DONT_CARE, "
493 "storeOp = DONT_CARE. This allows the implementation to use lazily allocated memory effectively.",
494 i, static_cast<uint32_t>(attachment.samples));
495 }
496 }
Camden5b184be2019-08-13 07:50:19 -0600497 }
498
499 for (uint32_t dependency = 0; dependency < pCreateInfo->dependencyCount; dependency++) {
500 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].srcStageMask);
501 skip |= CheckPipelineStageFlags("vkCreateRenderPass", pCreateInfo->pDependencies[dependency].dstStageMask);
502 }
503
504 return skip;
505}
506
Tony-LunarG767180f2020-04-23 14:03:59 -0600507bool BestPractices::ValidateAttachments(const VkRenderPassCreateInfo2* rpci, uint32_t attachmentCount,
508 const VkImageView* image_views) const {
509 bool skip = false;
510
511 // Check for non-transient attachments that should be transient and vice versa
512 for (uint32_t i = 0; i < attachmentCount; ++i) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200513 const auto& attachment = rpci->pAttachments[i];
Tony-LunarG767180f2020-04-23 14:03:59 -0600514 bool attachment_should_be_transient =
515 (attachment.loadOp != VK_ATTACHMENT_LOAD_OP_LOAD && attachment.storeOp != VK_ATTACHMENT_STORE_OP_STORE);
516
517 if (FormatHasStencil(attachment.format)) {
518 attachment_should_be_transient &= (attachment.stencilLoadOp != VK_ATTACHMENT_LOAD_OP_LOAD &&
519 attachment.stencilStoreOp != VK_ATTACHMENT_STORE_OP_STORE);
520 }
521
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600522 auto view_state = Get<IMAGE_VIEW_STATE>(image_views[i]);
Tony-LunarG767180f2020-04-23 14:03:59 -0600523 if (view_state) {
Jeremy Gebben057f9d52021-11-05 14:12:31 -0600524 const auto& ici = view_state->image_state->createInfo;
Tony-LunarG767180f2020-04-23 14:03:59 -0600525
526 bool image_is_transient = (ici.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) != 0;
527
528 // The check for an image that should not be transient applies to all GPUs
529 if (!attachment_should_be_transient && image_is_transient) {
530 skip |= LogPerformanceWarning(
531 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldNotBeTransient,
532 "Attachment %u in VkFramebuffer uses loadOp/storeOps which need to access physical memory, "
533 "but the image backing the image view has VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
534 "Physical memory will need to be backed lazily to this image, potentially causing stalls.",
535 i);
536 }
537
538 bool supports_lazy = false;
539 for (uint32_t j = 0; j < phys_dev_mem_props.memoryTypeCount; j++) {
540 if (phys_dev_mem_props.memoryTypes[j].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
541 supports_lazy = true;
542 }
543 }
544
545 // The check for an image that should be transient only applies to GPUs supporting
546 // lazily allocated memory
547 if (supports_lazy && attachment_should_be_transient && !image_is_transient) {
548 skip |= LogPerformanceWarning(
549 device, kVUID_BestPractices_CreateFramebuffer_AttachmentShouldBeTransient,
550 "Attachment %u in VkFramebuffer uses loadOp/storeOps which never have to be backed by physical memory, "
551 "but the image backing the image view does not have VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT set. "
552 "You can save physical memory by using transient attachment backed by lazily allocated memory here.",
553 i);
554 }
555 }
556 }
557 return skip;
558}
559
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000560bool BestPractices::PreCallValidateCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo* pCreateInfo,
561 const VkAllocationCallbacks* pAllocator, VkFramebuffer* pFramebuffer) const {
562 bool skip = false;
563
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600564 auto rp_state = Get<RENDER_PASS_STATE>(pCreateInfo->renderPass);
Mike Schuchardt2df08912020-12-15 16:28:09 -0800565 if (rp_state && !(pCreateInfo->flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT)) {
Tony-LunarG767180f2020-04-23 14:03:59 -0600566 skip = ValidateAttachments(rp_state->createInfo.ptr(), pCreateInfo->attachmentCount, pCreateInfo->pAttachments);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000567 }
568
569 return skip;
570}
571
Sam Wallse746d522020-03-16 21:20:23 +0000572bool BestPractices::PreCallValidateAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
573 VkDescriptorSet* pDescriptorSets, void* ads_state_data) const {
574 bool skip = false;
575 skip |= ValidationStateTracker::PreCallValidateAllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets, ads_state_data);
576
577 if (!skip) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700578 const auto pool_state = Get<bp_state::DescriptorPool>(pAllocateInfo->descriptorPool);
Sam Wallse746d522020-03-16 21:20:23 +0000579 // if the number of freed sets > 0, it implies they could be recycled instead if desirable
580 // this warning is specific to Arm
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700581 if (VendorCheckEnabled(kBPVendorArm) && pool_state && (pool_state->freed_count > 0)) {
Sam Wallse746d522020-03-16 21:20:23 +0000582 skip |= LogPerformanceWarning(
583 device, kVUID_BestPractices_AllocateDescriptorSets_SuboptimalReuse,
584 "%s Descriptor set memory was allocated via vkAllocateDescriptorSets() for sets which were previously freed in the "
585 "same logical device. On some drivers or architectures it may be most optimal to re-use existing descriptor sets.",
586 VendorSpecificTag(kBPVendorArm));
587 }
588 }
589
590 return skip;
591}
592
Mark Lobodzinski84101d72020-04-24 09:43:48 -0600593void BestPractices::ManualPostCallRecordAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
594 VkDescriptorSet* pDescriptorSets, VkResult result, void* ads_state) {
Sam Wallse746d522020-03-16 21:20:23 +0000595 if (result == VK_SUCCESS) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700596 auto pool_state = Get<bp_state::DescriptorPool>(pAllocateInfo->descriptorPool);
597 if (pool_state) {
Sam Wallse746d522020-03-16 21:20:23 +0000598 // we record successful allocations by subtracting the allocation count from the last recorded free count
599 const auto alloc_count = pAllocateInfo->descriptorSetCount;
600 // clamp the unsigned subtraction to the range [0, last_free_count]
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700601 if (pool_state->freed_count > alloc_count) {
602 pool_state->freed_count -= alloc_count;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700603 } else {
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700604 pool_state->freed_count = 0;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700605 }
Sam Wallse746d522020-03-16 21:20:23 +0000606 }
607 }
608}
609
610void BestPractices::PostCallRecordFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
611 const VkDescriptorSet* pDescriptorSets, VkResult result) {
612 ValidationStateTracker::PostCallRecordFreeDescriptorSets(device, descriptorPool, descriptorSetCount, pDescriptorSets, result);
613 if (result == VK_SUCCESS) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700614 auto pool_state = Get<bp_state::DescriptorPool>(descriptorPool);
Sam Wallse746d522020-03-16 21:20:23 +0000615 // we want to track frees because we're interested in suggesting re-use
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700616 if (pool_state) {
617 pool_state->freed_count += descriptorSetCount;
Sam Wallse746d522020-03-16 21:20:23 +0000618 }
619 }
620}
621
Camden5b184be2019-08-13 07:50:19 -0600622bool BestPractices::PreCallValidateAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500623 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory) const {
Camden5b184be2019-08-13 07:50:19 -0600624 bool skip = false;
625
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700626 if ((Count<DEVICE_MEMORY_STATE>() + 1) > kMemoryObjectWarningLimit) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700627 skip |= LogPerformanceWarning(device, kVUID_BestPractices_AllocateMemory_TooManyObjects,
628 "Performance Warning: This app has > %" PRIu32 " memory objects.", kMemoryObjectWarningLimit);
Camden5b184be2019-08-13 07:50:19 -0600629 }
630
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000631 if (pAllocateInfo->allocationSize < kMinDeviceAllocationSize) {
632 skip |= LogPerformanceWarning(
633 device, kVUID_BestPractices_AllocateMemory_SmallAllocation,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600634 "vkAllocateMemory(): Allocating a VkDeviceMemory of size %" PRIu64 ". This is a very small allocation (current "
635 "threshold is %" PRIu64 " bytes). "
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000636 "You should make large allocations and sub-allocate from one large VkDeviceMemory.",
637 pAllocateInfo->allocationSize, kMinDeviceAllocationSize);
638 }
639
Camden83a9c372019-08-14 11:41:38 -0600640 // TODO: Insert get check for GetPhysicalDeviceMemoryProperties once the state is tracked in the StateTracker
641
642 return skip;
643}
644
Mark Lobodzinski84101d72020-04-24 09:43:48 -0600645void BestPractices::ManualPostCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo,
646 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory,
647 VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700648 if (result != VK_SUCCESS) {
649 static std::vector<VkResult> error_codes = {VK_ERROR_OUT_OF_HOST_MEMORY, VK_ERROR_OUT_OF_DEVICE_MEMORY,
650 VK_ERROR_TOO_MANY_OBJECTS, VK_ERROR_INVALID_EXTERNAL_HANDLE,
Mike Schuchardt2df08912020-12-15 16:28:09 -0800651 VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS};
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700652 static std::vector<VkResult> success_codes = {};
Nathaniel Cesariodb3f43f2021-05-12 09:08:23 -0600653 ValidateReturnCodes("vkAllocateMemory", result, error_codes, success_codes);
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700654 return;
655 }
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700656}
Camden Stocker9738af92019-10-16 13:54:03 -0700657
Mark Lobodzinskide15e582020-04-29 08:06:00 -0600658void BestPractices::ValidateReturnCodes(const char* api_name, VkResult result, const std::vector<VkResult>& error_codes,
659 const std::vector<VkResult>& success_codes) const {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700660 auto error = std::find(error_codes.begin(), error_codes.end(), result);
661 if (error != error_codes.end()) {
Gareth Webb586c46b2021-01-13 11:17:22 +0000662 static const std::vector<VkResult> common_failure_codes = {VK_ERROR_OUT_OF_DATE_KHR,
663 VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT};
664
665 auto common_failure = std::find(common_failure_codes.begin(), common_failure_codes.end(), result);
666 if (common_failure != common_failure_codes.end()) {
667 LogInfo(instance, kVUID_BestPractices_Failure_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
668 } else {
669 LogWarning(instance, kVUID_BestPractices_Error_Result, "%s(): Returned error %s.", api_name, string_VkResult(result));
670 }
Mark Lobodzinski205b7a02020-02-21 13:23:17 -0700671 return;
672 }
673 auto success = std::find(success_codes.begin(), success_codes.end(), result);
674 if (success != success_codes.end()) {
Mark Lobodzinskie7215152020-05-11 08:21:23 -0600675 LogInfo(instance, kVUID_BestPractices_NonSuccess_Result, "%s(): Returned non-success return code %s.", api_name,
676 string_VkResult(result));
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500677 }
678}
679
Jeff Bolz5c801d12019-10-09 10:38:45 -0500680bool BestPractices::PreCallValidateFreeMemory(VkDevice device, VkDeviceMemory memory,
681 const VkAllocationCallbacks* pAllocator) const {
Mark Lobodzinski91e50bf2020-01-14 09:55:11 -0700682 if (memory == VK_NULL_HANDLE) return false;
Camden83a9c372019-08-14 11:41:38 -0600683 bool skip = false;
684
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700685 auto mem_info = Get<DEVICE_MEMORY_STATE>(memory);
Camden83a9c372019-08-14 11:41:38 -0600686
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700687 for (const auto& item : mem_info->ObjectBindings()) {
688 const auto& obj = item.first;
Mark Lobodzinski818425a2020-03-16 18:19:03 -0600689 LogObjectList objlist(device);
690 objlist.add(obj);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600691 objlist.add(mem_info->mem());
Mark Lobodzinski818425a2020-03-16 18:19:03 -0600692 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 -0600693 report_data->FormatHandle(obj).c_str(), report_data->FormatHandle(mem_info->mem()).c_str());
Camden83a9c372019-08-14 11:41:38 -0600694 }
695
Camden5b184be2019-08-13 07:50:19 -0600696 return skip;
697}
698
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000699bool BestPractices::ValidateBindBufferMemory(VkBuffer buffer, VkDeviceMemory memory, const char* api_name) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600700 bool skip = false;
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700701 auto buffer_state = Get<BUFFER_STATE>(buffer);
Camden Stockerb603cc82019-09-03 10:09:02 -0600702
sfricke-samsunge2441192019-11-06 14:07:57 -0800703 if (!buffer_state->memory_requirements_checked && !buffer_state->external_memory_handle) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -0700704 skip |= LogWarning(device, kVUID_BestPractices_BufferMemReqNotCalled,
705 "%s: Binding memory to %s but vkGetBufferMemoryRequirements() has not been called on that buffer.",
706 api_name, report_data->FormatHandle(buffer).c_str());
Camden Stockerb603cc82019-09-03 10:09:02 -0600707 }
708
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700709 auto mem_state = Get<DEVICE_MEMORY_STATE>(memory);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000710
AndreyVK_D3D0416a332021-11-02 23:22:28 +0300711 if (mem_state && mem_state->alloc_info.allocationSize == buffer_state->createInfo.size &&
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000712 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
713 skip |= LogPerformanceWarning(
714 device, kVUID_BestPractices_SmallDedicatedAllocation,
715 "%s: Trying to bind %s to a memory block which is fully consumed by the buffer. "
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600716 "The required size of the allocation is %" PRIu64 ", but smaller buffers like this should be sub-allocated from "
717 "larger memory blocks. (Current threshold is %" PRIu64 " bytes.)",
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000718 api_name, report_data->FormatHandle(buffer).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
719 }
720
Camden Stockerb603cc82019-09-03 10:09:02 -0600721 return skip;
722}
723
724bool BestPractices::PreCallValidateBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500725 VkDeviceSize memoryOffset) const {
Camden Stockerb603cc82019-09-03 10:09:02 -0600726 bool skip = false;
727 const char* api_name = "BindBufferMemory()";
728
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000729 skip |= ValidateBindBufferMemory(buffer, memory, api_name);
Camden Stockerb603cc82019-09-03 10:09:02 -0600730
731 return skip;
732}
733
734bool BestPractices::PreCallValidateBindBufferMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500735 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600736 char api_name[64];
737 bool skip = false;
738
739 for (uint32_t i = 0; i < bindInfoCount; i++) {
740 sprintf(api_name, "vkBindBufferMemory2() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000741 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600742 }
743
744 return skip;
745}
Camden Stockerb603cc82019-09-03 10:09:02 -0600746
747bool BestPractices::PreCallValidateBindBufferMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500748 const VkBindBufferMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600749 char api_name[64];
750 bool skip = false;
Camden Stockerb603cc82019-09-03 10:09:02 -0600751
Camden Stocker8b798ab2019-09-03 10:33:28 -0600752 for (uint32_t i = 0; i < bindInfoCount; i++) {
753 sprintf(api_name, "vkBindBufferMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000754 skip |= ValidateBindBufferMemory(pBindInfos[i].buffer, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600755 }
756
757 return skip;
758}
759
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000760bool BestPractices::ValidateBindImageMemory(VkImage image, VkDeviceMemory memory, const char* api_name) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600761 bool skip = false;
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700762 auto image_state = Get<IMAGE_STATE>(image);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600763
sfricke-samsung71bc6572020-04-29 15:49:43 -0700764 if (image_state->disjoint == false) {
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600765 if (!image_state->memory_requirements_checked[0] && !image_state->external_memory_handle) {
sfricke-samsungd7ea5de2020-04-08 09:19:18 -0700766 skip |= LogWarning(device, kVUID_BestPractices_ImageMemReqNotCalled,
767 "%s: Binding memory to %s but vkGetImageMemoryRequirements() has not been called on that image.",
768 api_name, report_data->FormatHandle(image).c_str());
769 }
770 } else {
771 // TODO If binding disjoint image then this needs to check that VkImagePlaneMemoryRequirementsInfo was called for each
772 // plane.
Camden Stocker8b798ab2019-09-03 10:33:28 -0600773 }
774
Jeremy Gebbenf4449392022-01-28 10:09:10 -0700775 auto mem_state = Get<DEVICE_MEMORY_STATE>(memory);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000776
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600777 if (mem_state->alloc_info.allocationSize == image_state->requirements[0].size &&
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000778 mem_state->alloc_info.allocationSize < kMinDedicatedAllocationSize) {
779 skip |= LogPerformanceWarning(
780 device, kVUID_BestPractices_SmallDedicatedAllocation,
781 "%s: Trying to bind %s to a memory block which is fully consumed by the image. "
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600782 "The required size of the allocation is %" PRIu64 ", but smaller images like this should be sub-allocated from "
783 "larger memory blocks. (Current threshold is %" PRIu64 " bytes.)",
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000784 api_name, report_data->FormatHandle(image).c_str(), mem_state->alloc_info.allocationSize, kMinDedicatedAllocationSize);
785 }
786
787 // If we're binding memory to a image which was created as TRANSIENT and the image supports LAZY allocation,
788 // make sure this type is actually used.
789 // This warning will only trigger if this layer is run on a platform that supports LAZILY_ALLOCATED_BIT
790 // (i.e.most tile - based renderers)
791 if (image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) {
792 bool supports_lazy = false;
793 uint32_t suggested_type = 0;
794
795 for (uint32_t i = 0; i < phys_dev_mem_props.memoryTypeCount; i++) {
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600796 if ((1u << i) & image_state->requirements[0].memoryTypeBits) {
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000797 if (phys_dev_mem_props.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
798 supports_lazy = true;
799 suggested_type = i;
800 break;
801 }
802 }
803 }
804
805 uint32_t allocated_properties = phys_dev_mem_props.memoryTypes[mem_state->alloc_info.memoryTypeIndex].propertyFlags;
806
807 if (supports_lazy && (allocated_properties & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) == 0) {
808 skip |= LogPerformanceWarning(
809 device, kVUID_BestPractices_NonLazyTransientImage,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -0600810 "%s: Attempting to bind memory type %u to VkImage which was created with TRANSIENT_ATTACHMENT_BIT,"
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000811 "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 -0600812 "%" PRIu64 " bytes of physical memory.",
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600813 api_name, mem_state->alloc_info.memoryTypeIndex, suggested_type, image_state->requirements[0].size);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000814 }
815 }
816
Camden Stocker8b798ab2019-09-03 10:33:28 -0600817 return skip;
818}
819
820bool BestPractices::PreCallValidateBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500821 VkDeviceSize memoryOffset) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600822 bool skip = false;
823 const char* api_name = "vkBindImageMemory()";
824
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000825 skip |= ValidateBindImageMemory(image, memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600826
827 return skip;
828}
829
830bool BestPractices::PreCallValidateBindImageMemory2(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500831 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600832 char api_name[64];
833 bool skip = false;
834
835 for (uint32_t i = 0; i < bindInfoCount; i++) {
836 sprintf(api_name, "vkBindImageMemory2() pBindInfos[%u]", i);
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700837 if (!LvlFindInChain<VkBindImageMemorySwapchainInfoKHR>(pBindInfos[i].pNext)) {
Tony-LunarG5e60b852020-04-27 11:27:54 -0600838 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
839 }
Camden Stocker8b798ab2019-09-03 10:33:28 -0600840 }
841
842 return skip;
843}
844
845bool BestPractices::PreCallValidateBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500846 const VkBindImageMemoryInfo* pBindInfos) const {
Camden Stocker8b798ab2019-09-03 10:33:28 -0600847 char api_name[64];
848 bool skip = false;
849
850 for (uint32_t i = 0; i < bindInfoCount; i++) {
851 sprintf(api_name, "vkBindImageMemory2KHR() pBindInfos[%u]", i);
Attilio Provenzanof31788e2020-02-27 12:00:36 +0000852 skip |= ValidateBindImageMemory(pBindInfos[i].image, pBindInfos[i].memory, api_name);
Camden Stocker8b798ab2019-09-03 10:33:28 -0600853 }
854
855 return skip;
856}
Camden83a9c372019-08-14 11:41:38 -0600857
Attilio Provenzano02859b22020-02-27 14:17:28 +0000858static inline bool FormatHasFullThroughputBlendingArm(VkFormat format) {
859 switch (format) {
860 case VK_FORMAT_B10G11R11_UFLOAT_PACK32:
861 case VK_FORMAT_R16_SFLOAT:
862 case VK_FORMAT_R16G16_SFLOAT:
863 case VK_FORMAT_R16G16B16_SFLOAT:
864 case VK_FORMAT_R16G16B16A16_SFLOAT:
865 case VK_FORMAT_R32_SFLOAT:
866 case VK_FORMAT_R32G32_SFLOAT:
867 case VK_FORMAT_R32G32B32_SFLOAT:
868 case VK_FORMAT_R32G32B32A32_SFLOAT:
869 return false;
870
871 default:
872 return true;
873 }
874}
875
876bool BestPractices::ValidateMultisampledBlendingArm(uint32_t createInfoCount,
877 const VkGraphicsPipelineCreateInfo* pCreateInfos) const {
878 bool skip = false;
879
880 for (uint32_t i = 0; i < createInfoCount; i++) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700881 auto create_info = &pCreateInfos[i];
Attilio Provenzano02859b22020-02-27 14:17:28 +0000882
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700883 if (!create_info->pColorBlendState || !create_info->pMultisampleState ||
884 create_info->pMultisampleState->rasterizationSamples == VK_SAMPLE_COUNT_1_BIT ||
885 create_info->pMultisampleState->sampleShadingEnable) {
Attilio Provenzano02859b22020-02-27 14:17:28 +0000886 return skip;
887 }
888
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600889 auto rp_state = Get<RENDER_PASS_STATE>(create_info->renderPass);
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200890 const auto& subpass = rp_state->createInfo.pSubpasses[create_info->subpass];
Attilio Provenzano02859b22020-02-27 14:17:28 +0000891
Hans-Kristian Arntzenc2742e72021-07-01 14:31:06 +0200892 // According to spec, pColorBlendState must be ignored if subpass does not have color attachments.
893 uint32_t num_color_attachments = std::min(subpass.colorAttachmentCount, create_info->pColorBlendState->attachmentCount);
894
895 for (uint32_t j = 0; j < num_color_attachments; j++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200896 const auto& blend_att = create_info->pColorBlendState->pAttachments[j];
Attilio Provenzano02859b22020-02-27 14:17:28 +0000897 uint32_t att = subpass.pColorAttachments[j].attachment;
898
899 if (att != VK_ATTACHMENT_UNUSED && blend_att.blendEnable && blend_att.colorWriteMask) {
900 if (!FormatHasFullThroughputBlendingArm(rp_state->createInfo.pAttachments[att].format)) {
901 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultisampledBlending,
902 "%s vkCreateGraphicsPipelines() - createInfo #%u: Pipeline is multisampled and "
903 "color attachment #%u makes use "
904 "of a format which cannot be blended at full throughput when using MSAA.",
905 VendorSpecificTag(kBPVendorArm), i, j);
906 }
907 }
908 }
909 }
910
911 return skip;
912}
913
Nadav Gevaf0808442021-05-21 13:51:25 -0400914void BestPractices::ManualPostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
915 const VkComputePipelineCreateInfo* pCreateInfos,
916 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
917 VkResult result, void* pipe_state) {
918 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700919 pipeline_cache_ = pipelineCache;
Nadav Gevaf0808442021-05-21 13:51:25 -0400920}
921
Camden5b184be2019-08-13 07:50:19 -0600922bool BestPractices::PreCallValidateCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
923 const VkGraphicsPipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -0600924 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500925 void* cgpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -0600926 bool skip = StateTracker::PreCallValidateCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos,
927 pAllocator, pPipelines, cgpl_state_data);
ziga-lunarg08c81582022-03-08 17:33:45 +0100928 if (skip) {
929 return skip;
930 }
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600931 create_graphics_pipeline_api_state* cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state*>(cgpl_state_data);
Camden5b184be2019-08-13 07:50:19 -0600932
933 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -0700934 skip |= LogPerformanceWarning(
935 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
936 "Performance Warning: This vkCreateGraphicsPipelines call is creating multiple pipelines but is not using a "
937 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -0600938 }
939
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000940 for (uint32_t i = 0; i < createInfoCount; i++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200941 const auto& create_info = pCreateInfos[i];
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000942
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600943 if (!(cgpl_state->pipe_state[i]->active_shaders & VK_SHADER_STAGE_MESH_BIT_NV)) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +0200944 const auto& vertex_input = *create_info.pVertexInputState;
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600945 uint32_t count = 0;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700946 for (uint32_t j = 0; j < vertex_input.vertexBindingDescriptionCount; j++) {
947 if (vertex_input.pVertexBindingDescriptions[j].inputRate == VK_VERTEX_INPUT_RATE_INSTANCE) {
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600948 count++;
949 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000950 }
Mark Lobodzinski8dd14d82020-04-10 14:16:33 -0600951 if (count > kMaxInstancedVertexBuffers) {
952 skip |= LogPerformanceWarning(
953 device, kVUID_BestPractices_CreatePipelines_TooManyInstancedVertexBuffers,
954 "The pipeline is using %u instanced vertex buffers (current limit: %u), but this can be inefficient on the "
955 "GPU. If using instanced vertex attributes prefer interleaving them in a single buffer.",
956 count, kMaxInstancedVertexBuffers);
957 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000958 }
Attilio Provenzano02859b22020-02-27 14:17:28 +0000959
Szilard Pappaaf2da32020-06-22 10:37:35 +0100960 if ((pCreateInfos[i].pRasterizationState->depthBiasEnable) &&
961 (pCreateInfos[i].pRasterizationState->depthBiasConstantFactor == 0.0f) &&
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +0200962 (pCreateInfos[i].pRasterizationState->depthBiasSlopeFactor == 0.0f) &&
963 VendorCheckEnabled(kBPVendorArm)) {
964 skip |= LogPerformanceWarning(
965 device, kVUID_BestPractices_CreatePipelines_DepthBias_Zero,
966 "%s Performance Warning: This vkCreateGraphicsPipelines call is created with depthBiasEnable set to true "
967 "and both depthBiasConstantFactor and depthBiasSlopeFactor are set to 0. This can cause reduced "
968 "efficiency during rasterization. Consider disabling depthBias or increasing either "
969 "depthBiasConstantFactor or depthBiasSlopeFactor.",
970 VendorSpecificTag(kBPVendorArm));
Szilard Pappaaf2da32020-06-22 10:37:35 +0100971 }
972
Attilio Provenzano02859b22020-02-27 14:17:28 +0000973 skip |= VendorCheckEnabled(kBPVendorArm) && ValidateMultisampledBlendingArm(createInfoCount, pCreateInfos);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +0000974 }
Nadav Gevaf0808442021-05-21 13:51:25 -0400975 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700976 auto prev_pipeline = pipeline_cache_.load();
977 if (pipelineCache && prev_pipeline && pipelineCache != prev_pipeline) {
Nadav Gevaf0808442021-05-21 13:51:25 -0400978 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MultiplePipelineCaches,
979 "%s Performance Warning: A second pipeline cache is in use. Consider using only one pipeline cache to "
980 "improve cache hit rate", VendorSpecificTag(kBPVendorAMD));
981 }
982
Jeremy Gebben20da7a12022-02-25 14:07:46 -0700983 if (num_pso_ > kMaxRecommendedNumberOfPSOAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -0400984 skip |=
985 LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_TooManyPipelines,
986 "%s Performance warning: Too many pipelines created, consider consolidation",
987 VendorSpecificTag(kBPVendorAMD));
988 }
989
Nathaniel Cesario1a7e1a92021-08-30 14:34:20 -0600990 if (pCreateInfos->pInputAssemblyState && pCreateInfos->pInputAssemblyState->primitiveRestartEnable) {
Nadav Gevaf0808442021-05-21 13:51:25 -0400991 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_AvoidPrimitiveRestart,
992 "%s Performance warning: Use of primitive restart is not recommended",
993 VendorSpecificTag(kBPVendorAMD));
994 }
995
996 // TODO: this might be too aggressive of a check
997 if (pCreateInfos->pDynamicState && pCreateInfos->pDynamicState->dynamicStateCount > kDynamicStatesWarningLimitAMD) {
998 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelines_MinimizeNumDynamicStates,
999 "%s Performance warning: Dynamic States usage incurs a performance cost. Ensure that they are truly needed",
1000 VendorSpecificTag(kBPVendorAMD));
1001 }
1002 }
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00001003
Camden5b184be2019-08-13 07:50:19 -06001004 return skip;
1005}
1006
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001007static std::vector<bp_state::AttachmentInfo> GetAttachmentAccess(const safe_VkGraphicsPipelineCreateInfo& create_info,
1008 std::shared_ptr<const RENDER_PASS_STATE>& rp) {
1009 std::vector<bp_state::AttachmentInfo> result;
1010 if (!rp) {
1011 return result;
Hans-Kristian Arntzenb033ab12021-06-16 11:16:59 +02001012 }
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001013
1014 const auto& subpass = rp->createInfo.pSubpasses[create_info.subpass];
1015
1016 // NOTE: see PIPELINE_LAYOUT and safe_VkGraphicsPipelineCreateInfo constructors. pColorBlendState and pDepthStencilState
1017 // are only non-null if they are enabled.
1018 if (create_info.pColorBlendState) {
1019 // According to spec, pColorBlendState must be ignored if subpass does not have color attachments.
1020 uint32_t num_color_attachments = std::min(subpass.colorAttachmentCount, create_info.pColorBlendState->attachmentCount);
1021 for (uint32_t j = 0; j < num_color_attachments; j++) {
1022 if (create_info.pColorBlendState->pAttachments[j].colorWriteMask != 0) {
1023 uint32_t attachment = subpass.pColorAttachments[j].attachment;
1024 if (attachment != VK_ATTACHMENT_UNUSED) {
1025 result.push_back({attachment, VK_IMAGE_ASPECT_COLOR_BIT});
1026 }
1027 }
1028 }
1029 }
1030
1031 if (create_info.pDepthStencilState &&
1032 (create_info.pDepthStencilState->depthTestEnable || create_info.pDepthStencilState->depthBoundsTestEnable ||
1033 create_info.pDepthStencilState->stencilTestEnable)) {
1034 uint32_t attachment = subpass.pDepthStencilAttachment ? subpass.pDepthStencilAttachment->attachment : VK_ATTACHMENT_UNUSED;
1035 if (attachment != VK_ATTACHMENT_UNUSED) {
1036 VkImageAspectFlags aspects = 0;
1037 if (create_info.pDepthStencilState->depthTestEnable || create_info.pDepthStencilState->depthBoundsTestEnable) {
1038 aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
1039 }
1040 if (create_info.pDepthStencilState->stencilTestEnable) {
1041 aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
1042 }
1043 result.push_back({attachment, aspects});
1044 }
1045 }
1046 return result;
1047}
1048
1049bp_state::Pipeline::Pipeline(const ValidationStateTracker* state_data, const VkGraphicsPipelineCreateInfo* pCreateInfo,
1050 std::shared_ptr<const RENDER_PASS_STATE>&& rpstate,
1051 std::shared_ptr<const PIPELINE_LAYOUT_STATE>&& layout)
1052 : PIPELINE_STATE(state_data, pCreateInfo, std::move(rpstate), std::move(layout)),
1053 access_framebuffer_attachments(GetAttachmentAccess(create_info.graphics, rp_state)) {}
1054
1055std::shared_ptr<PIPELINE_STATE> BestPractices::CreateGraphicsPipelineState(
1056 const VkGraphicsPipelineCreateInfo* pCreateInfo, std::shared_ptr<const RENDER_PASS_STATE>&& render_pass,
1057 std::shared_ptr<const PIPELINE_LAYOUT_STATE>&& layout) const {
1058 return std::static_pointer_cast<PIPELINE_STATE>(
1059 std::make_shared<bp_state::Pipeline>(this, pCreateInfo, std::move(render_pass), std::move(layout)));
Hans-Kristian Arntzenb033ab12021-06-16 11:16:59 +02001060}
1061
Sam Walls0961ec02020-03-31 16:39:15 +01001062void BestPractices::ManualPostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
1063 const VkGraphicsPipelineCreateInfo* pCreateInfos,
1064 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
1065 VkResult result, void* cgpl_state_data) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001066 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001067 pipeline_cache_ = pipelineCache;
Sam Walls0961ec02020-03-31 16:39:15 +01001068}
1069
Camden5b184be2019-08-13 07:50:19 -06001070bool BestPractices::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
1071 const VkComputePipelineCreateInfo* pCreateInfos,
Mark Lobodzinski2a162a02019-09-06 11:02:12 -06001072 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001073 void* ccpl_state_data) const {
Mark Lobodzinski8317a3e2019-09-20 10:07:08 -06001074 bool skip = StateTracker::PreCallValidateCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos,
1075 pAllocator, pPipelines, ccpl_state_data);
Camden5b184be2019-08-13 07:50:19 -06001076
1077 if ((createInfoCount > 1) && (!pipelineCache)) {
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07001078 skip |= LogPerformanceWarning(
1079 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1080 "Performance Warning: This vkCreateComputePipelines call is creating multiple pipelines but is not using a "
1081 "pipeline cache, which may help with performance");
Camden5b184be2019-08-13 07:50:19 -06001082 }
1083
Nadav Gevaf0808442021-05-21 13:51:25 -04001084 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001085 auto prev_pipeline = pipeline_cache_.load();
1086 if (pipelineCache && prev_pipeline && pipelineCache != prev_pipeline) {
1087 skip |= LogPerformanceWarning(
1088 device, kVUID_BestPractices_CreatePipelines_MultiplePipelines,
1089 "%s Performance Warning: A second pipeline cache is in use. Consider using only one pipeline cache to "
Nadav Gevaf0808442021-05-21 13:51:25 -04001090 "improve cache hit rate",
1091 VendorSpecificTag(kBPVendorAMD));
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001092 }
1093 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001094
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001095 for (uint32_t i = 0; i < createInfoCount; i++) {
1096 const VkComputePipelineCreateInfo& createInfo = pCreateInfos[i];
1097 if (VendorCheckEnabled(kBPVendorArm)) {
1098 skip |= ValidateCreateComputePipelineArm(createInfo);
1099 }
sfricke-samsung86d055a2022-02-11 14:43:50 -08001100
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001101 if (IsExtEnabled(device_extensions.vk_khr_maintenance4)) {
1102 auto module_state = Get<SHADER_MODULE_STATE>(createInfo.stage.module);
1103 for (const auto& builtin : module_state->static_data_.builtin_decoration_list) {
1104 if (builtin.builtin == spv::BuiltInWorkgroupSize) {
1105 skip |= LogWarning(device, kVUID_BestPractices_SpirvDeprecated_WorkgroupSize,
1106 "vkCreateComputePipelines(): pCreateInfos[ %" PRIu32
1107 "] is using the Workgroup built-in which SPIR-V 1.6 deprecated. The VK_KHR_maintenance4 "
1108 "extension exposes a new LocalSizeId execution mode that should be used instead.",
1109 i);
sfricke-samsung86d055a2022-02-11 14:43:50 -08001110 }
1111 }
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001112 }
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001113 }
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001114
1115 return skip;
1116}
1117
1118bool BestPractices::ValidateCreateComputePipelineArm(const VkComputePipelineCreateInfo& createInfo) const {
1119 bool skip = false;
sfricke-samsungef15e482022-01-26 11:32:49 -08001120 auto module_state = Get<SHADER_MODULE_STATE>(createInfo.stage.module);
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001121 // Generate warnings about work group sizes based on active resources.
sfricke-samsungef15e482022-01-26 11:32:49 -08001122 auto entrypoint = module_state->FindEntrypoint(createInfo.stage.pName, createInfo.stage.stage);
1123 if (entrypoint == module_state->end()) return false;
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001124
1125 uint32_t x = 1, y = 1, z = 1;
sfricke-samsungef15e482022-01-26 11:32:49 -08001126 module_state->FindLocalSize(entrypoint, x, y, z);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001127
1128 uint32_t thread_count = x * y * z;
1129
1130 // Generate a priori warnings about work group sizes.
1131 if (thread_count > kMaxEfficientWorkGroupThreadCountArm) {
1132 skip |= LogPerformanceWarning(
1133 device, kVUID_BestPractices_CreateComputePipelines_ComputeWorkGroupSize,
1134 "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, %u, "
1135 "%u) (%u threads total), has more threads than advised in a single work group. It is advised to use work "
1136 "groups with less than %u threads, especially when using barrier() or shared memory.",
1137 VendorSpecificTag(kBPVendorArm), x, y, z, thread_count, kMaxEfficientWorkGroupThreadCountArm);
1138 }
1139
1140 if (thread_count == 1 || ((x > 1) && (x & (kThreadGroupDispatchCountAlignmentArm - 1))) ||
1141 ((y > 1) && (y & (kThreadGroupDispatchCountAlignmentArm - 1))) ||
1142 ((z > 1) && (z & (kThreadGroupDispatchCountAlignmentArm - 1)))) {
1143 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeThreadGroupAlignment,
1144 "%s vkCreateComputePipelines(): compute shader with work group dimensions (%u, "
1145 "%u, %u) is not aligned to %u "
1146 "threads. On Arm Mali architectures, not aligning work group sizes to %u may "
1147 "leave threads idle on the shader "
1148 "core.",
1149 VendorSpecificTag(kBPVendorArm), x, y, z, kThreadGroupDispatchCountAlignmentArm,
1150 kThreadGroupDispatchCountAlignmentArm);
1151 }
1152
sfricke-samsungef15e482022-01-26 11:32:49 -08001153 auto accessible_ids = module_state->MarkAccessibleIds(entrypoint);
1154 auto descriptor_uses = module_state->CollectInterfaceByDescriptorSlot(accessible_ids);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001155
1156 unsigned dimensions = 0;
1157 if (x > 1) dimensions++;
1158 if (y > 1) dimensions++;
1159 if (z > 1) dimensions++;
1160 // Here the dimension will really depend on the dispatch grid, but assume it's 1D.
1161 dimensions = std::max(dimensions, 1u);
1162
1163 // If we're accessing images, we almost certainly want to have a 2D workgroup for cache reasons.
1164 // There are some false positives here. We could simply have a shader that does this within a 1D grid,
1165 // or we may have a linearly tiled image, but these cases are quite unlikely in practice.
1166 bool accesses_2d = false;
1167 for (const auto& usage : descriptor_uses) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001168 auto dim = module_state->GetShaderResourceDimensionality(usage.second);
Sam Wallsd7ab6db2020-06-19 20:41:54 +01001169 if (dim < 0) continue;
1170 auto spvdim = spv::Dim(dim);
1171 if (spvdim != spv::Dim1D && spvdim != spv::DimBuffer) accesses_2d = true;
1172 }
1173
1174 if (accesses_2d && dimensions < 2) {
1175 LogPerformanceWarning(device, kVUID_BestPractices_CreateComputePipelines_ComputeSpatialLocality,
1176 "%s vkCreateComputePipelines(): compute shader has work group dimensions (%u, %u, %u), which "
1177 "suggests a 1D dispatch, but the shader is accessing 2D or 3D images. The shader may be "
1178 "exhibiting poor spatial locality with respect to one or more shader resources.",
1179 VendorSpecificTag(kBPVendorArm), x, y, z);
1180 }
1181
Camden5b184be2019-08-13 07:50:19 -06001182 return skip;
1183}
1184
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001185bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags flags) const {
Camden5b184be2019-08-13 07:50:19 -06001186 bool skip = false;
1187
1188 if (flags & VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001189 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1190 "You are using VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -06001191 } else if (flags & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07001192 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1193 "You are using VK_PIPELINE_STAGE_ALL_COMMANDS_BIT when %s is called\n", api_name.c_str());
Camden5b184be2019-08-13 07:50:19 -06001194 }
1195
1196 return skip;
1197}
1198
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001199bool BestPractices::CheckPipelineStageFlags(const std::string& api_name, VkPipelineStageFlags2KHR flags) const {
1200 bool skip = false;
1201
1202 if (flags & VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR) {
1203 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1204 "You are using VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT_KHR when %s is called\n", api_name.c_str());
1205 } else if (flags & VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR) {
1206 skip |= LogWarning(device, kVUID_BestPractices_PipelineStageFlags,
1207 "You are using VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT_KHR when %s is called\n", api_name.c_str());
1208 }
1209
1210 return skip;
1211}
1212
1213bool BestPractices::CheckDependencyInfo(const std::string& api_name, const VkDependencyInfoKHR& dep_info) const {
1214 bool skip = false;
1215 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
1216
1217 skip |= CheckPipelineStageFlags(api_name, stage_masks.src);
1218 skip |= CheckPipelineStageFlags(api_name, stage_masks.dst);
1219
1220 return skip;
1221}
1222
Mark Lobodzinski84101d72020-04-24 09:43:48 -06001223void BestPractices::ManualPostCallRecordQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo, VkResult result) {
Mark Lobodzinski9b133c12020-03-10 10:42:56 -06001224 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; ++i) {
1225 auto swapchains_result = pPresentInfo->pResults ? pPresentInfo->pResults[i] : result;
1226 if (swapchains_result == VK_SUBOPTIMAL_KHR) {
1227 LogPerformanceWarning(
1228 pPresentInfo->pSwapchains[i], kVUID_BestPractices_SuboptimalSwapchain,
1229 "vkQueuePresentKHR: %s :VK_SUBOPTIMAL_KHR was returned. VK_SUBOPTIMAL_KHR - Presentation will still succeed, "
1230 "subject to the window resize behavior, but the swapchain is no longer configured optimally for the surface it "
1231 "targets. Applications should query updated surface information and recreate their swapchain at the next "
1232 "convenient opportunity.",
1233 report_data->FormatHandle(pPresentInfo->pSwapchains[i]).c_str());
1234 }
1235 }
Nadav Gevaf0808442021-05-21 13:51:25 -04001236
1237 // AMD best practice
1238 // end-of-frame cleanup
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001239 num_queue_submissions_ = 0;
1240 num_barriers_objects_ = 0;
1241 ClearPipelinesUsedInFrame();
Mark Lobodzinski9b133c12020-03-10 10:42:56 -06001242}
1243
Jeff Bolz5c801d12019-10-09 10:38:45 -05001244bool BestPractices::PreCallValidateQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
1245 VkFence fence) const {
Camden5b184be2019-08-13 07:50:19 -06001246 bool skip = false;
1247
1248 for (uint32_t submit = 0; submit < submitCount; submit++) {
1249 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreCount; semaphore++) {
1250 skip |= CheckPipelineStageFlags("vkQueueSubmit", pSubmits[submit].pWaitDstStageMask[semaphore]);
1251 }
1252 }
1253
1254 return skip;
1255}
1256
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001257bool BestPractices::PreCallValidateQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR* pSubmits,
1258 VkFence fence) const {
1259 bool skip = false;
1260
1261 for (uint32_t submit = 0; submit < submitCount; submit++) {
1262 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) {
1263 skip |= CheckPipelineStageFlags("vkQueueSubmit2KHR", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask);
1264 }
1265 }
1266
1267 return skip;
1268}
1269
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001270bool BestPractices::PreCallValidateQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2* pSubmits,
1271 VkFence fence) const {
1272 bool skip = false;
1273
1274 for (uint32_t submit = 0; submit < submitCount; submit++) {
1275 for (uint32_t semaphore = 0; semaphore < pSubmits[submit].waitSemaphoreInfoCount; semaphore++) {
1276 skip |= CheckPipelineStageFlags("vkQueueSubmit2", pSubmits[submit].pWaitSemaphoreInfos[semaphore].stageMask);
1277 }
1278 }
1279
1280 return skip;
1281}
1282
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001283bool BestPractices::PreCallValidateCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo,
1284 const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool) const {
1285 bool skip = false;
1286
1287 if (pCreateInfo->flags & VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT) {
1288 skip |= LogPerformanceWarning(
1289 device, kVUID_BestPractices_CreateCommandPool_CommandBufferReset,
1290 "vkCreateCommandPool(): VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT is set. Consider resetting entire "
1291 "pool instead.");
1292 }
1293
1294 return skip;
1295}
1296
1297bool BestPractices::PreCallValidateBeginCommandBuffer(VkCommandBuffer commandBuffer,
1298 const VkCommandBufferBeginInfo* pBeginInfo) const {
1299 bool skip = false;
1300
1301 if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
1302 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_SimultaneousUse,
1303 "vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT is set.");
1304 }
1305
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001306 if (!(pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT) && VendorCheckEnabled(kBPVendorArm)) {
1307 skip |= LogPerformanceWarning(device, kVUID_BestPractices_BeginCommandBuffer_OneTimeSubmit,
Attilio Provenzano02859b22020-02-27 14:17:28 +00001308 "%s vkBeginCommandBuffer(): VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT is not set. "
1309 "For best performance on Mali GPUs, consider setting ONE_TIME_SUBMIT by default.",
1310 VendorSpecificTag(kBPVendorArm));
1311 }
1312
Attilio Provenzano746e43e2020-02-27 11:23:50 +00001313 return skip;
1314}
1315
Jeff Bolz5c801d12019-10-09 10:38:45 -05001316bool BestPractices::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -06001317 bool skip = false;
1318
1319 skip |= CheckPipelineStageFlags("vkCmdSetEvent", stageMask);
1320
1321 return skip;
1322}
1323
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001324bool BestPractices::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
1325 const VkDependencyInfoKHR* pDependencyInfo) const {
1326 return CheckDependencyInfo("vkCmdSetEvent2KHR", *pDependencyInfo);
1327}
1328
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001329bool BestPractices::PreCallValidateCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
1330 const VkDependencyInfo* pDependencyInfo) const {
1331 return CheckDependencyInfo("vkCmdSetEvent2", *pDependencyInfo);
1332}
1333
Jeff Bolz5c801d12019-10-09 10:38:45 -05001334bool BestPractices::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
1335 VkPipelineStageFlags stageMask) const {
Camden5b184be2019-08-13 07:50:19 -06001336 bool skip = false;
1337
1338 skip |= CheckPipelineStageFlags("vkCmdResetEvent", stageMask);
1339
1340 return skip;
1341}
1342
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001343bool BestPractices::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
1344 VkPipelineStageFlags2KHR stageMask) const {
1345 bool skip = false;
1346
1347 skip |= CheckPipelineStageFlags("vkCmdResetEvent2KHR", stageMask);
1348
1349 return skip;
1350}
1351
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001352bool BestPractices::PreCallValidateCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
1353 VkPipelineStageFlags2 stageMask) const {
1354 bool skip = false;
1355
1356 skip |= CheckPipelineStageFlags("vkCmdResetEvent2", stageMask);
1357
1358 return skip;
1359}
1360
Camden5b184be2019-08-13 07:50:19 -06001361bool BestPractices::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1362 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
1363 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
1364 uint32_t bufferMemoryBarrierCount,
1365 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1366 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001367 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -06001368 bool skip = false;
1369
1370 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", srcStageMask);
1371 skip |= CheckPipelineStageFlags("vkCmdWaitEvents", dstStageMask);
1372
1373 return skip;
1374}
1375
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001376bool BestPractices::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1377 const VkDependencyInfoKHR* pDependencyInfos) const {
1378 bool skip = false;
1379 for (uint32_t i = 0; i < eventCount; i++) {
1380 skip = CheckDependencyInfo("vkCmdWaitEvents2KHR", pDependencyInfos[i]);
1381 }
1382
1383 return skip;
1384}
1385
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001386bool BestPractices::PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
1387 const VkDependencyInfo* pDependencyInfos) const {
1388 bool skip = false;
1389 for (uint32_t i = 0; i < eventCount; i++) {
1390 skip = CheckDependencyInfo("vkCmdWaitEvents2", pDependencyInfos[i]);
1391 }
1392
1393 return skip;
1394}
1395
Camden5b184be2019-08-13 07:50:19 -06001396bool BestPractices::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1397 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1398 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
1399 uint32_t bufferMemoryBarrierCount,
1400 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
1401 uint32_t imageMemoryBarrierCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001402 const VkImageMemoryBarrier* pImageMemoryBarriers) const {
Camden5b184be2019-08-13 07:50:19 -06001403 bool skip = false;
1404
1405 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", srcStageMask);
1406 skip |= CheckPipelineStageFlags("vkCmdPipelineBarrier", dstStageMask);
1407
Nadav Gevaf0808442021-05-21 13:51:25 -04001408 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001409 auto num = num_barriers_objects_.load();
1410 if (num + imageMemoryBarrierCount + bufferMemoryBarrierCount > kMaxRecommendedBarriersSizeAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -04001411 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_highBarrierCount,
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001412 "%s Performance warning: In this frame, %" PRIu32
1413 " barriers were already submitted. Barriers have a high cost and can "
1414 "stall the GPU. "
1415 "Consider consolidating and re-organizing the frame to use fewer barriers.",
1416 VendorSpecificTag(kBPVendorAMD), num);
Nadav Gevaf0808442021-05-21 13:51:25 -04001417 }
1418
1419 std::array<VkImageLayout, 3> read_layouts = {
1420 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
1421 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
1422 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1423 };
1424
1425 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
1426 // read to read barriers
1427 auto found = std::find(read_layouts.begin(), read_layouts.end(), pImageMemoryBarriers[i].oldLayout);
1428 bool old_is_read_layout = found != read_layouts.end();
1429 found = std::find(read_layouts.begin(), read_layouts.end(), pImageMemoryBarriers[i].newLayout);
1430 bool new_is_read_layout = found != read_layouts.end();
1431 if (old_is_read_layout && new_is_read_layout) {
1432 skip |= LogPerformanceWarning(device, kVUID_BestPractices_PipelineBarrier_readToReadBarrier,
1433 "%s Performance warning: Don't issue read-to-read barriers. Get the resource in the right state the first "
1434 "time you use it.",
1435 VendorSpecificTag(kBPVendorAMD));
1436 }
1437
1438 // general with no storage
1439 if (pImageMemoryBarriers[i].newLayout == VK_IMAGE_LAYOUT_GENERAL) {
1440 auto image_state = Get<IMAGE_STATE>(pImageMemoryBarriers[i].image);
1441 if (!(image_state->createInfo.usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1442 skip |= LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidGeneral,
1443 "%s Performance warning: VK_IMAGE_LAYOUT_GENERAL should only be used with "
1444 "VK_IMAGE_USAGE_STORAGE_BIT images.",
1445 VendorSpecificTag(kBPVendorAMD));
1446 }
1447 }
1448 }
1449 }
1450
Camden5b184be2019-08-13 07:50:19 -06001451 return skip;
1452}
1453
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001454bool BestPractices::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
1455 const VkDependencyInfoKHR* pDependencyInfo) const {
1456 return CheckDependencyInfo("vkCmdPipelineBarrier2KHR", *pDependencyInfo);
1457}
1458
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001459bool BestPractices::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer,
1460 const VkDependencyInfo* pDependencyInfo) const {
1461 return CheckDependencyInfo("vkCmdPipelineBarrier2", *pDependencyInfo);
1462}
1463
Camden5b184be2019-08-13 07:50:19 -06001464bool BestPractices::PreCallValidateCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeff Bolz5c801d12019-10-09 10:38:45 -05001465 VkQueryPool queryPool, uint32_t query) const {
Camden5b184be2019-08-13 07:50:19 -06001466 bool skip = false;
1467
Jeremy Gebbena3705f42021-01-19 16:47:43 -07001468 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp", static_cast<VkPipelineStageFlags>(pipelineStage));
1469
1470 return skip;
1471}
1472
1473bool BestPractices::PreCallValidateCmdWriteTimestamp2KHR(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
1474 VkQueryPool queryPool, uint32_t query) const {
1475 bool skip = false;
1476
1477 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2KHR", pipelineStage);
Camden5b184be2019-08-13 07:50:19 -06001478
1479 return skip;
1480}
1481
Tony-LunarGd36f5f32022-01-20 11:49:59 -07001482bool BestPractices::PreCallValidateCmdWriteTimestamp2(VkCommandBuffer commandBuffer, VkPipelineStageFlags2 pipelineStage,
1483 VkQueryPool queryPool, uint32_t query) const {
1484 bool skip = false;
1485
1486 skip |= CheckPipelineStageFlags("vkCmdWriteTimestamp2", pipelineStage);
1487
1488 return skip;
1489}
1490
Sam Walls0961ec02020-03-31 16:39:15 +01001491void BestPractices::PostCallRecordCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
1492 VkPipeline pipeline) {
1493 StateTracker::PostCallRecordCmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline);
1494
Nadav Gevaf0808442021-05-21 13:51:25 -04001495 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001496 PipelineUsedInFrame(pipeline);
Nadav Gevaf0808442021-05-21 13:51:25 -04001497
Sam Walls0961ec02020-03-31 16:39:15 +01001498 if (pipelineBindPoint == VK_PIPELINE_BIND_POINT_GRAPHICS) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001499 auto pipeline_state = Get<bp_state::Pipeline>(pipeline);
Sam Walls0961ec02020-03-31 16:39:15 +01001500 // check for depth/blend state tracking
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001501 if (pipeline_state) {
1502 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001503 assert(cb_node);
1504 auto& render_pass_state = cb_node->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01001505
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001506 render_pass_state.nextDrawTouchesAttachments = pipeline_state->access_framebuffer_attachments;
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001507 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02001508
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001509 const auto* blend_state = pipeline_state->create_info.graphics.pColorBlendState;
1510 const auto* stencil_state = pipeline_state->create_info.graphics.pDepthStencilState;
Sam Walls0961ec02020-03-31 16:39:15 +01001511
1512 if (blend_state) {
1513 // assume the pipeline is depth-only unless any of the attachments have color writes enabled
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001514 render_pass_state.depthOnly = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001515 for (size_t i = 0; i < blend_state->attachmentCount; i++) {
1516 if (blend_state->pAttachments[i].colorWriteMask != 0) {
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001517 render_pass_state.depthOnly = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001518 }
1519 }
1520 }
1521
1522 // check for depth value usage
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001523 render_pass_state.depthEqualComparison = false;
Sam Walls0961ec02020-03-31 16:39:15 +01001524
1525 if (stencil_state && stencil_state->depthTestEnable) {
1526 switch (stencil_state->depthCompareOp) {
1527 case VK_COMPARE_OP_EQUAL:
1528 case VK_COMPARE_OP_GREATER_OR_EQUAL:
1529 case VK_COMPARE_OP_LESS_OR_EQUAL:
Hans-Kristian Arntzen56232b92021-06-16 14:37:48 +02001530 render_pass_state.depthEqualComparison = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001531 break;
1532 default:
1533 break;
1534 }
1535 }
Sam Walls0961ec02020-03-31 16:39:15 +01001536 }
1537 }
1538}
1539
Hans-Kristian Arntzen237663c2021-07-01 14:36:40 +02001540static inline bool RenderPassUsesAttachmentAsResolve(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1541 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
1542 const auto& subpass_info = createInfo.pSubpasses[subpass];
1543 if (subpass_info.pResolveAttachments) {
1544 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1545 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
1546 }
1547 }
1548 }
1549
1550 return false;
1551}
1552
Attilio Provenzano02859b22020-02-27 14:17:28 +00001553static inline bool RenderPassUsesAttachmentOnTile(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1554 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001555 const auto& subpass_info = createInfo.pSubpasses[subpass];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001556
1557 // If an attachment is ever used as a color attachment,
1558 // resolve attachment or depth stencil attachment,
1559 // it needs to exist on tile at some point.
1560
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001561 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1562 if (subpass_info.pColorAttachments[i].attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001563 }
1564
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001565 if (subpass_info.pResolveAttachments) {
1566 for (uint32_t i = 0; i < subpass_info.colorAttachmentCount; i++) {
1567 if (subpass_info.pResolveAttachments[i].attachment == attachment) return true;
1568 }
1569 }
1570
1571 if (subpass_info.pDepthStencilAttachment && subpass_info.pDepthStencilAttachment->attachment == attachment) return true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001572 }
1573
1574 return false;
1575}
1576
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001577static inline bool RenderPassUsesAttachmentAsImageOnly(const safe_VkRenderPassCreateInfo2& createInfo, uint32_t attachment) {
1578 if (RenderPassUsesAttachmentOnTile(createInfo, attachment)) {
1579 return false;
1580 }
1581
1582 for (uint32_t subpass = 0; subpass < createInfo.subpassCount; subpass++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001583 const auto& subpassInfo = createInfo.pSubpasses[subpass];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001584
1585 for (uint32_t i = 0; i < subpassInfo.inputAttachmentCount; i++) {
1586 if (subpassInfo.pInputAttachments[i].attachment == attachment) {
1587 return true;
1588 }
1589 }
1590 }
1591
1592 return false;
1593}
1594
Attilio Provenzano02859b22020-02-27 14:17:28 +00001595bool BestPractices::ValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
1596 const VkRenderPassBeginInfo* pRenderPassBegin) const {
1597 bool skip = false;
1598
1599 if (!pRenderPassBegin) {
1600 return skip;
1601 }
1602
Gareth Webbdc6549a2021-06-16 03:52:24 +01001603 if (pRenderPassBegin->renderArea.extent.width == 0 || pRenderPassBegin->renderArea.extent.height == 0) {
1604 skip |= LogWarning(device, kVUID_BestPractices_BeginRenderPass_ZeroSizeRenderArea,
1605 "This render pass has a zero-size render area. It cannot write to any attachments, "
1606 "and can only be used for side effects such as layout transitions.");
1607 }
1608
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001609 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001610 if (rp_state) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001611 if (rp_state->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07001612 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
Tony-LunarG767180f2020-04-23 14:03:59 -06001613 if (rpabi) {
1614 skip = ValidateAttachments(rp_state->createInfo.ptr(), rpabi->attachmentCount, rpabi->pAttachments);
1615 }
1616 }
Attilio Provenzano02859b22020-02-27 14:17:28 +00001617 // Check if any attachments have LOAD operation on them
1618 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001619 const auto& attachment = rp_state->createInfo.pAttachments[att];
Attilio Provenzano02859b22020-02-27 14:17:28 +00001620
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001621 bool attachment_has_readback = false;
Hans-Kristian Arntzen4afb59b2021-06-18 12:41:36 +02001622 if (!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001623 attachment_has_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001624 }
1625
1626 if (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == 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
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001630 bool attachment_needs_readback = false;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001631
1632 // Check if the attachment is actually used in any subpass on-tile
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001633 if (attachment_has_readback && RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1634 attachment_needs_readback = true;
Attilio Provenzano02859b22020-02-27 14:17:28 +00001635 }
1636
1637 // Using LOAD_OP_LOAD is expensive on tiled GPUs, so flag it as a potential improvement
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001638 if (attachment_needs_readback && VendorCheckEnabled(kBPVendorArm)) {
1639 skip |= LogPerformanceWarning(
1640 device, kVUID_BestPractices_BeginRenderPass_AttachmentNeedsReadback,
1641 "%s Attachment #%u in render pass has begun with VK_ATTACHMENT_LOAD_OP_LOAD.\n"
1642 "Submitting this renderpass will cause the driver to inject a readback of the attachment "
Nadav Gevaf0808442021-05-21 13:51:25 -04001643 "which will copy in total %u pixels (renderArea = "
1644 "{ %" PRId32 ", %" PRId32 ", %" PRIu32", %" PRIu32 " }) to the tile buffer.",
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001645 VendorSpecificTag(kBPVendorArm), att,
1646 pRenderPassBegin->renderArea.extent.width * pRenderPassBegin->renderArea.extent.height,
1647 pRenderPassBegin->renderArea.offset.x, pRenderPassBegin->renderArea.offset.y,
1648 pRenderPassBegin->renderArea.extent.width, pRenderPassBegin->renderArea.extent.height);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001649 }
1650 }
1651 }
1652
1653 return skip;
1654}
1655
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001656void BestPractices::QueueValidateImageView(QueueCallbacks &funcs, const char* function_name,
1657 IMAGE_VIEW_STATE* view, IMAGE_SUBRESOURCE_USAGE_BP usage) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001658 if (view) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001659 auto image_state = std::static_pointer_cast<bp_state::Image>(view->image_state);
1660 QueueValidateImage(funcs, function_name, image_state, usage, view->normalized_subresource_range);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001661 }
1662}
1663
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001664void BestPractices::QueueValidateImage(QueueCallbacks& funcs, const char* function_name, std::shared_ptr<bp_state::Image>& state,
1665 IMAGE_SUBRESOURCE_USAGE_BP usage, const VkImageSubresourceRange& subresource_range) {
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001666 // If we're viewing a 3D slice, ignore base array layer.
1667 // The entire 3D subresource is accessed as one atomic unit.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001668 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 +02001669
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001670 const uint32_t max_layers = state->createInfo.arrayLayers - base_array_layer;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001671 const uint32_t array_layers = std::min(subresource_range.layerCount, max_layers);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001672 const uint32_t max_levels = state->createInfo.mipLevels - subresource_range.baseMipLevel;
1673 const uint32_t mip_levels = std::min(state->createInfo.mipLevels, max_levels);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001674
1675 for (uint32_t layer = 0; layer < array_layers; layer++) {
1676 for (uint32_t level = 0; level < mip_levels; level++) {
Hans-Kristian Arntzen93264202021-05-21 17:07:46 +02001677 QueueValidateImage(funcs, function_name, state, usage, layer + base_array_layer,
1678 level + subresource_range.baseMipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001679 }
1680 }
1681}
1682
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001683void BestPractices::QueueValidateImage(QueueCallbacks& funcs, const char* function_name, std::shared_ptr<bp_state::Image>& state,
1684 IMAGE_SUBRESOURCE_USAGE_BP usage, const VkImageSubresourceLayers& subresource_layers) {
1685 const uint32_t max_layers = state->createInfo.arrayLayers - subresource_layers.baseArrayLayer;
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001686 const uint32_t array_layers = std::min(subresource_layers.layerCount, max_layers);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001687
1688 for (uint32_t layer = 0; layer < array_layers; layer++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001689 QueueValidateImage(funcs, function_name, state, usage, layer + subresource_layers.baseArrayLayer, subresource_layers.mipLevel);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001690 }
1691}
1692
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001693void BestPractices::QueueValidateImage(QueueCallbacks &funcs, const char* function_name,
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001694 std::shared_ptr<bp_state::Image> &state, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001695 uint32_t array_layer, uint32_t mip_level) {
Jeremy Gebben7fd238f2022-03-04 10:32:24 -07001696 if (VendorCheckEnabled(kBPVendorArm)) {
1697 funcs.push_back([this, function_name, state, usage, array_layer, mip_level](
1698 const ValidationStateTracker&, const QUEUE_STATE&, const CMD_BUFFER_STATE&) -> bool {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001699 ValidateImageInQueue(function_name, *state, usage, array_layer, mip_level);
Jeremy Gebben7fd238f2022-03-04 10:32:24 -07001700 return false;
1701 });
1702 }
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001703}
1704
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001705void BestPractices::ValidateImageInQueueArm(const char* function_name, const bp_state::Image& image,
1706 IMAGE_SUBRESOURCE_USAGE_BP last_usage, IMAGE_SUBRESOURCE_USAGE_BP usage,
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001707 uint32_t array_layer, uint32_t mip_level) {
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001708 // Swapchain images are implicitly read so clear after store is expected.
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001709 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 -07001710 !image.IsSwapchainImage()) {
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001711 LogPerformanceWarning(
1712 device, kVUID_BestPractices_RenderPass_RedundantStore,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001713 "%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 +02001714 "image was used, it was written to with STORE_OP_STORE. "
1715 "Storing to the image is probably redundant in this case, and wastes bandwidth on tile-based "
1716 "architectures.",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001717 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level);
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001718 } 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 +02001719 LogPerformanceWarning(
1720 device, kVUID_BestPractices_RenderPass_RedundantClear,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001721 "%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 +02001722 "image was used, it was written to with vkCmdClear*Image(). "
1723 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
1724 "tile-based architectures."
1725 "architectures.",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001726 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level);
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001727 } else if (usage == IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE &&
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001728 (last_usage == IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE || last_usage == IMAGE_SUBRESOURCE_USAGE_BP::CLEARED ||
1729 last_usage == IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE || last_usage == IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE)) {
Hans-Kristian Arntzen44f9d862021-03-22 13:56:39 +01001730 const char *last_cmd = nullptr;
1731 const char *vuid = nullptr;
1732 const char *suggestion = nullptr;
1733
1734 switch (last_usage) {
1735 case IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE:
1736 vuid = kVUID_BestPractices_RenderPass_BlitImage_LoadOpLoad;
1737 last_cmd = "vkCmdBlitImage";
1738 suggestion =
1739 "The blit is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
1740 "Rather than blitting, just render the source image in a fragment shader in this render pass, "
1741 "which avoids the memory roundtrip.";
1742 break;
1743 case IMAGE_SUBRESOURCE_USAGE_BP::CLEARED:
1744 vuid = kVUID_BestPractices_RenderPass_InefficientClear;
1745 last_cmd = "vkCmdClear*Image";
1746 suggestion =
1747 "Clearing the image with vkCmdClear*Image() is probably redundant in this case, and wastes bandwidth on "
1748 "tile-based architectures. "
1749 "Use LOAD_OP_CLEAR instead to clear the image for free.";
1750 break;
1751 case IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE:
1752 vuid = kVUID_BestPractices_RenderPass_CopyImage_LoadOpLoad;
1753 last_cmd = "vkCmdCopy*Image";
1754 suggestion =
1755 "The copy is probably redundant in this case, and wastes bandwidth on tile-based architectures. "
1756 "Rather than copying, just render the source image in a fragment shader in this render pass, "
1757 "which avoids the memory roundtrip.";
1758 break;
1759 case IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE:
1760 vuid = kVUID_BestPractices_RenderPass_ResolveImage_LoadOpLoad;
1761 last_cmd = "vkCmdResolveImage";
1762 suggestion =
1763 "The resolve is probably redundant in this case, and wastes a lot of bandwidth on tile-based architectures. "
1764 "Rather than resolving, and then loading, try to keep rendering in the same render pass, "
1765 "which avoids the memory roundtrip.";
1766 break;
1767 default:
1768 break;
1769 }
1770
1771 LogPerformanceWarning(
1772 device, vuid,
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001773 "%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 +01001774 "time image was used, it was written to with %s. %s",
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02001775 function_name, VendorSpecificTag(kBPVendorArm), array_layer, mip_level, last_cmd, suggestion);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001776 }
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001777}
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001778
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001779void BestPractices::ValidateImageInQueue(const char* function_name, bp_state::Image& state, IMAGE_SUBRESOURCE_USAGE_BP usage,
1780 uint32_t array_layer, uint32_t mip_level) {
1781 auto last_usage = state.UpdateUsage(array_layer, mip_level, usage);
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001782 if (VendorCheckEnabled(kBPVendorArm)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001783 ValidateImageInQueueArm(function_name, state, last_usage, usage, array_layer, mip_level);
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02001784 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001785}
1786
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001787void BestPractices::AddDeferredQueueOperations(bp_state::CommandBuffer& cb) {
1788 cb.queue_submit_functions.insert(cb.queue_submit_functions.end(), cb.queue_submit_functions_after_render_pass.begin(),
1789 cb.queue_submit_functions_after_render_pass.end());
1790 cb.queue_submit_functions_after_render_pass.clear();
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001791}
1792
1793void BestPractices::PreCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
1794 ValidationStateTracker::PreCallRecordCmdEndRenderPass(commandBuffer);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001795 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
1796 if (cb_node) {
1797 AddDeferredQueueOperations(*cb_node);
1798 }
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001799}
1800
1801void BestPractices::PreCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassInfo) {
1802 ValidationStateTracker::PreCallRecordCmdEndRenderPass2(commandBuffer, pSubpassInfo);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001803 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
1804 if (cb_node) {
1805 AddDeferredQueueOperations(*cb_node);
1806 }
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001807}
1808
1809void BestPractices::PreCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR *pSubpassInfo) {
1810 ValidationStateTracker::PreCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassInfo);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001811 auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
1812 if (cb_node) {
1813 AddDeferredQueueOperations(*cb_node);
1814 }
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001815}
1816
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02001817void BestPractices::PreCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer,
1818 const VkRenderPassBeginInfo* pRenderPassBegin,
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001819 VkSubpassContents contents) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001820 ValidationStateTracker::PreCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02001821 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1822}
1823
1824void BestPractices::PreCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer,
1825 const VkRenderPassBeginInfo* pRenderPassBegin,
1826 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1827 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1828 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1829}
1830
1831void BestPractices::PreCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1832 const VkRenderPassBeginInfo* pRenderPassBegin,
1833 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1834 ValidationStateTracker::PreCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1835 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin);
1836}
1837
1838void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001839
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001840 if (!pRenderPassBegin) {
1841 return;
1842 }
1843
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001844 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01001845
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001846 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001847 if (rp_state) {
1848 // Check load ops
1849 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001850 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001851
1852 if (!RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att) &&
1853 !RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1854 continue;
1855 }
1856
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001857 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::UNDEFINED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001858
1859 if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) ||
1860 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001861 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_READ_TO_TILE;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02001862 } else if ((!FormatIsStencilOnly(attachment.format) && attachment.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) ||
1863 (FormatHasStencil(attachment.format) && attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_CLEAR)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001864 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_CLEARED;
Hans-Kristian Arntzen5e56e552021-03-29 11:45:20 +02001865 } else if (RenderPassUsesAttachmentAsImageOnly(rp_state->createInfo, att)) {
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01001866 usage = IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001867 }
1868
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001869 auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
Jeremy Gebben9f537102021-10-05 16:37:12 -06001870 std::shared_ptr<IMAGE_VIEW_STATE> image_view = nullptr;
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001871
Tony-LunarGb3ab3572021-07-02 09:45:17 -06001872 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001873 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
1874 if (rpabi) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001875 image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001876 }
1877 } else {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001878 image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001879 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001880
Jeremy Gebben9f537102021-10-05 16:37:12 -06001881 QueueValidateImageView(cb->queue_submit_functions, "vkCmdBeginRenderPass()", image_view.get(), usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001882 }
1883
1884 // Check store ops
1885 for (uint32_t att = 0; att < rp_state->createInfo.attachmentCount; att++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02001886 const auto& attachment = rp_state->createInfo.pAttachments[att];
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001887
1888 if (!RenderPassUsesAttachmentOnTile(rp_state->createInfo, att)) {
1889 continue;
1890 }
1891
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001892 IMAGE_SUBRESOURCE_USAGE_BP usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_DISCARDED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001893
1894 if ((!FormatIsStencilOnly(attachment.format) && attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE) ||
1895 (FormatHasStencil(attachment.format) && attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
Hans-Kristian Arntzen5b466db2021-03-18 13:59:46 +01001896 usage = IMAGE_SUBRESOURCE_USAGE_BP::RENDER_PASS_STORED;
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001897 }
1898
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001899 auto framebuffer = Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001900
Jeremy Gebben9f537102021-10-05 16:37:12 -06001901 std::shared_ptr<IMAGE_VIEW_STATE> image_view;
Tony-LunarGb3ab3572021-07-02 09:45:17 -06001902 if (framebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001903 const VkRenderPassAttachmentBeginInfo* rpabi = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
1904 if (rpabi) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001905 image_view = Get<IMAGE_VIEW_STATE>(rpabi->pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001906 }
1907 } else {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001908 image_view = Get<IMAGE_VIEW_STATE>(framebuffer->createInfo.pAttachments[att]);
Hans-Kristian Arntzen9710e142021-03-18 12:19:02 +01001909 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001910
Jeremy Gebben9f537102021-10-05 16:37:12 -06001911 QueueValidateImageView(cb->queue_submit_functions_after_render_pass, "vkCmdEndRenderPass()", image_view.get(), usage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01001912 }
1913 }
1914}
1915
Attilio Provenzano02859b22020-02-27 14:17:28 +00001916bool BestPractices::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
1917 VkSubpassContents contents) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001918 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1919 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001920 return skip;
1921}
1922
1923bool BestPractices::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1924 const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001925 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001926 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1927 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001928 return skip;
1929}
1930
1931bool BestPractices::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001932 const VkSubpassBeginInfo* pSubpassBeginInfo) const {
Sam Walls0961ec02020-03-31 16:39:15 +01001933 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1934 skip |= ValidateCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
Attilio Provenzano02859b22020-02-27 14:17:28 +00001935 return skip;
1936}
1937
Sam Walls0961ec02020-03-31 16:39:15 +01001938void BestPractices::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, RenderPassCreateVersion rp_version,
1939 const VkRenderPassBeginInfo* pRenderPassBegin) {
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001940 // Reset the renderpass state
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001941 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06001942 cb->hasDrawCmd = false;
1943 assert(cb);
1944 auto& render_pass_state = cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02001945 render_pass_state.touchesAttachments.clear();
1946 render_pass_state.earlyClearAttachments.clear();
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001947 render_pass_state.numDrawCallsDepthOnly = 0;
1948 render_pass_state.numDrawCallsDepthEqualCompare = 0;
1949 render_pass_state.colorAttachment = false;
1950 render_pass_state.depthAttachment = false;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02001951 render_pass_state.drawTouchAttachments = true;
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001952 // Don't reset state related to pipeline state.
Sam Walls0961ec02020-03-31 16:39:15 +01001953
Jeremy Gebbenf4449392022-01-28 10:09:10 -07001954 auto rp_state = Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Sam Walls0961ec02020-03-31 16:39:15 +01001955
1956 // track depth / color attachment usage within the renderpass
1957 for (size_t i = 0; i < rp_state->createInfo.subpassCount; i++) {
1958 // record if depth/color attachments are in use for this renderpass
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001959 if (rp_state->createInfo.pSubpasses[i].pDepthStencilAttachment != nullptr) render_pass_state.depthAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001960
Hans-Kristian Arntzena900f5d2021-06-14 15:09:31 +02001961 if (rp_state->createInfo.pSubpasses[i].colorAttachmentCount > 0) render_pass_state.colorAttachment = true;
Sam Walls0961ec02020-03-31 16:39:15 +01001962 }
1963}
1964
1965void BestPractices::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
1966 VkSubpassContents contents) {
1967 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
1968 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_1, pRenderPassBegin);
1969}
1970
1971void BestPractices::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin,
1972 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1973 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1974 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
1975}
1976
1977void BestPractices::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
1978 const VkRenderPassBeginInfo* pRenderPassBegin,
1979 const VkSubpassBeginInfo* pSubpassBeginInfo) {
1980 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
1981 RecordCmdBeginRenderPass(commandBuffer, RENDER_PASS_VERSION_2, pRenderPassBegin);
1982}
1983
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07001984// Generic function to handle validation for all CmdDraw* type functions
1985bool BestPractices::ValidateCmdDrawType(VkCommandBuffer cmd_buffer, const char* caller) const {
1986 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07001987 const auto cb_state = GetRead<bp_state::CommandBuffer>(cmd_buffer);
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07001988 if (cb_state) {
locke-lunargb8d7a7a2020-10-25 16:01:52 -06001989 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
1990 const auto* pipeline_state = cb_state->lastBound[lv_bind_point].pipeline_state;
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07001991 const auto& current_vtx_bfr_binding_info = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings;
locke-lunargb8d7a7a2020-10-25 16:01:52 -06001992
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07001993 // Verify vertex binding
1994 if (pipeline_state->vertex_binding_descriptions_.size() <= 0) {
1995 if ((!current_vtx_bfr_binding_info.empty()) && (!cb_state->vertex_buffer_used)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001996 skip |= LogPerformanceWarning(cb_state->commandBuffer(), kVUID_BestPractices_DrawState_VtxIndexOutOfBounds,
Mark Lobodzinskif95a2662020-01-29 15:43:32 -07001997 "Vertex buffers are bound to %s but no vertex buffers are attached to %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001998 report_data->FormatHandle(cb_state->commandBuffer()).c_str(),
1999 report_data->FormatHandle(pipeline_state->pipeline()).c_str());
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002000 }
2001 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002002
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002003 const auto* pipe = cb_state->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002004 if (pipe) {
2005 const auto* rp_state = pipe->rp_state.get();
2006 if (rp_state) {
2007 for (uint32_t i = 0; i < rp_state->createInfo.subpassCount; ++i) {
2008 const auto& subpass = rp_state->createInfo.pSubpasses[i];
Jeremy Gebben11af9792021-08-20 10:20:09 -06002009 const auto& create_info = pipe->create_info.graphics;
2010 const uint32_t depth_stencil_attachment =
2011 GetSubpassDepthStencilAttachmentIndex(create_info.pDepthStencilState, subpass.pDepthStencilAttachment);
2012 if ((depth_stencil_attachment == VK_ATTACHMENT_UNUSED) && create_info.pRasterizationState &&
2013 create_info.pRasterizationState->depthBiasEnable == VK_TRUE) {
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002014 skip |= LogWarning(cb_state->commandBuffer(), kVUID_BestPractices_DepthBiasNoAttachment,
2015 "%s: depthBiasEnable == VK_TRUE without a depth-stencil attachment.", caller);
2016 }
2017 }
2018 }
2019 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002020 }
2021 return skip;
2022}
2023
Sam Walls0961ec02020-03-31 16:39:15 +01002024void BestPractices::RecordCmdDrawType(VkCommandBuffer cmd_buffer, uint32_t draw_count, const char* caller) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002025 auto cb_node = GetWrite<bp_state::CommandBuffer>(cmd_buffer);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002026 assert(cb_node);
Sam Walls0961ec02020-03-31 16:39:15 +01002027 if (VendorCheckEnabled(kBPVendorArm)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002028 RecordCmdDrawTypeArm(*cb_node, draw_count, caller);
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002029 }
2030
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002031 if (cb_node->render_pass_state.drawTouchAttachments) {
2032 for (auto& touch : cb_node->render_pass_state.nextDrawTouchesAttachments) {
2033 RecordAttachmentAccess(*cb_node, touch.framebufferAttachment, touch.aspects);
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002034 }
2035 // No need to touch the same attachments over and over.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002036 cb_node->render_pass_state.drawTouchAttachments = false;
Sam Walls0961ec02020-03-31 16:39:15 +01002037 }
2038}
2039
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002040void BestPractices::RecordCmdDrawTypeArm(bp_state::CommandBuffer& cb_node, uint32_t draw_count, const char* caller) {
2041 auto& render_pass_state = cb_node.render_pass_state;
Hans-Kristian Arntzen8abca1e2021-06-16 13:57:45 +02002042 if (draw_count >= kDepthPrePassMinDrawCountArm) {
2043 if (render_pass_state.depthOnly) render_pass_state.numDrawCallsDepthOnly++;
2044 if (render_pass_state.depthEqualComparison) render_pass_state.numDrawCallsDepthEqualCompare++;
Sam Walls0961ec02020-03-31 16:39:15 +01002045 }
2046}
2047
Camden5b184be2019-08-13 07:50:19 -06002048bool BestPractices::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002049 uint32_t firstVertex, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06002050 bool skip = false;
2051
2052 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002053 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
2054 "Warning: You are calling vkCmdDraw() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002055 }
Nathaniel Cesariof7b732a2021-06-03 14:08:27 -06002056 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDraw()");
Camden5b184be2019-08-13 07:50:19 -06002057
2058 return skip;
2059}
2060
Sam Walls0961ec02020-03-31 16:39:15 +01002061void BestPractices::PostCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2062 uint32_t firstVertex, uint32_t firstInstance) {
2063 StateTracker::PostCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
2064 RecordCmdDrawType(commandBuffer, vertexCount * instanceCount, "vkCmdDraw()");
2065}
2066
Camden5b184be2019-08-13 07:50:19 -06002067bool BestPractices::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002068 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
Camden5b184be2019-08-13 07:50:19 -06002069 bool skip = false;
2070
2071 if (instanceCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002072 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_InstanceCountZero,
2073 "Warning: You are calling vkCmdDrawIndexed() with an instanceCount of Zero.");
Camden5b184be2019-08-13 07:50:19 -06002074 }
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002075 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexed()");
2076
Attilio Provenzano02859b22020-02-27 14:17:28 +00002077 // Check if we reached the limit for small indexed draw calls.
2078 // Note that we cannot update the draw call count here, so we do it in PreCallRecordCmdDrawIndexed.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002079 const auto cmd_state = GetRead<bp_state::CommandBuffer>(commandBuffer);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002080 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices &&
Hans-Kristian Arntzenb2147952021-04-28 14:32:00 +02002081 (cmd_state->small_indexed_draw_call_count == kMaxSmallIndexedDrawcalls - 1) &&
2082 VendorCheckEnabled(kBPVendorArm)) {
2083 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_ManySmallIndexedDrawcalls,
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -06002084 "%s: The command buffer contains many small indexed drawcalls "
Attilio Provenzano02859b22020-02-27 14:17:28 +00002085 "(at least %u drawcalls with less than %u indices each). This may cause pipeline bubbles. "
2086 "You can try batching drawcalls or instancing when applicable.",
2087 VendorSpecificTag(kBPVendorArm), kMaxSmallIndexedDrawcalls, kSmallIndexedDrawcallIndices);
2088 }
2089
Sam Walls8e77e4f2020-03-16 20:47:40 +00002090 if (VendorCheckEnabled(kBPVendorArm)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002091 ValidateIndexBufferArm(*cmd_state, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002092 }
2093
2094 return skip;
2095}
2096
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002097bool BestPractices::ValidateIndexBufferArm(const bp_state::CommandBuffer& cmd_state, uint32_t indexCount, uint32_t instanceCount,
Sam Walls8e77e4f2020-03-16 20:47:40 +00002098 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
2099 bool skip = false;
2100
2101 // check for sparse/underutilised index buffer, and post-transform cache thrashing
Sam Walls8e77e4f2020-03-16 20:47:40 +00002102
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002103 const auto* ib_state = cmd_state.index_buffer_binding.buffer_state.get();
2104 if (ib_state == nullptr || cmd_state.index_buffer_binding.buffer_state->Destroyed()) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002105
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002106 const VkIndexType ib_type = cmd_state.index_buffer_binding.index_type;
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002107 const auto& ib_mem_state = *ib_state->MemState();
Sam Walls8e77e4f2020-03-16 20:47:40 +00002108 const VkDeviceSize ib_mem_offset = ib_mem_state.mapped_range.offset;
2109 const void* ib_mem = ib_mem_state.p_driver_data;
2110 bool primitive_restart_enable = false;
2111
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002112 const auto lv_bind_point = ConvertToLvlBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002113 const auto& pipeline_binding_iter = cmd_state.lastBound[lv_bind_point];
locke-lunargb8d7a7a2020-10-25 16:01:52 -06002114 const auto* pipeline_state = pipeline_binding_iter.pipeline_state;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002115
Jeremy Gebben11af9792021-08-20 10:20:09 -06002116 if (pipeline_state != nullptr && pipeline_state->create_info.graphics.pInputAssemblyState != nullptr) {
2117 primitive_restart_enable = pipeline_state->create_info.graphics.pInputAssemblyState->primitiveRestartEnable == VK_TRUE;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002118 }
Sam Walls8e77e4f2020-03-16 20:47:40 +00002119
2120 // 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 -06002121 if (ib_mem && pipeline_binding_iter.IsUsing()) {
Sam Walls8e77e4f2020-03-16 20:47:40 +00002122 uint32_t scan_stride;
2123 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2124 scan_stride = sizeof(uint8_t);
2125 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2126 scan_stride = sizeof(uint16_t);
2127 } else {
2128 scan_stride = sizeof(uint32_t);
2129 }
2130
2131 const uint8_t* scan_begin = static_cast<const uint8_t*>(ib_mem) + ib_mem_offset + firstIndex * scan_stride;
2132 const uint8_t* scan_end = scan_begin + indexCount * scan_stride;
2133
2134 // Min and max are important to track for some Mali architectures. In older Mali devices without IDVS, all
2135 // vertices corresponding to indices between the minimum and maximum may be loaded, and possibly shaded,
2136 // irrespective of whether or not they're part of the draw call.
2137
2138 // start with minimum as 0xFFFFFFFF and adjust to indices in the buffer
2139 uint32_t min_index = ~0u;
2140 // start with maximum as 0 and adjust to indices in the buffer
2141 uint32_t max_index = 0u;
2142
2143 // first scan-through, we're looking to simulate a model LRU post-transform cache, estimating the number of vertices shaded
2144 // for the given index buffer
2145 uint32_t vertex_shade_count = 0;
2146
2147 PostTransformLRUCacheModel post_transform_cache;
2148
2149 // The size of the cache being modelled positively correlates with how much behaviour it can capture about
2150 // arbitrary ground-truth hardware/architecture cache behaviour. I.e. it's a good solution when we don't know the
2151 // target architecture.
2152 // However, modelling a post-transform cache with more than 32 elements gives diminishing returns in practice.
2153 // http://eelpi.gotdns.org/papers/fast_vert_cache_opt.html
2154 post_transform_cache.resize(32);
2155
2156 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
2157 uint32_t scan_index;
2158 uint32_t primitive_restart_value;
2159 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2160 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
2161 primitive_restart_value = 0xFF;
2162 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2163 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
2164 primitive_restart_value = 0xFFFF;
2165 } else {
2166 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
2167 primitive_restart_value = 0xFFFFFFFF;
2168 }
2169
2170 max_index = std::max(max_index, scan_index);
2171 min_index = std::min(min_index, scan_index);
2172
2173 if (!primitive_restart_enable || scan_index != primitive_restart_value) {
2174 bool in_cache = post_transform_cache.query_cache(scan_index);
2175 // if the shaded vertex corresponding to the index is not in the PT-cache, we need to shade again
2176 if (!in_cache) vertex_shade_count++;
2177 }
2178 }
2179
2180 // 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 +01002181 // 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
2182 if (max_index < min_index || max_index == min_index) return skip;
Sam Walls8e77e4f2020-03-16 20:47:40 +00002183
2184 if (max_index - min_index >= indexCount) {
Mark Young0ec6b062020-11-19 15:32:17 -07002185 skip |=
2186 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
2187 "%s The indices which were specified for the draw call only utilise approximately %.02f%% of "
2188 "index buffer value range. Arm Mali architectures before G71 do not have IDVS (Index-Driven "
2189 "Vertex Shading), meaning all vertices corresponding to indices between the minimum and "
2190 "maximum would be loaded, and possibly shaded, whether or not they are used.",
2191 VendorSpecificTag(kBPVendorArm),
2192 (static_cast<float>(indexCount) / static_cast<float>(max_index - min_index)) * 100.0f);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002193 return skip;
2194 }
2195
2196 // use a dynamic vector of bitsets as a memory-compact representation of which indices are included in the draw call
2197 // 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 +01002198 const size_t refs_per_bucket = 64;
2199 std::vector<std::bitset<refs_per_bucket>> vertex_reference_buckets;
2200
2201 const uint32_t n_indices = max_index - min_index + 1;
2202 const uint32_t n_buckets = (n_indices / static_cast<uint32_t>(refs_per_bucket)) +
2203 ((n_indices % static_cast<uint32_t>(refs_per_bucket)) != 0 ? 1 : 0);
2204
2205 // there needs to be at least one bitset to store a set of indices smaller than n_buckets
2206 vertex_reference_buckets.resize(std::max(1u, n_buckets));
Sam Walls8e77e4f2020-03-16 20:47:40 +00002207
2208 // To avoid using too much memory, we run over the indices again.
2209 // Knowing the size from the last scan allows us to record index usage with bitsets
2210 for (const uint8_t* scan_ptr = scan_begin; scan_ptr < scan_end; scan_ptr += scan_stride) {
2211 uint32_t scan_index;
2212 if (ib_type == VK_INDEX_TYPE_UINT8_EXT) {
2213 scan_index = *reinterpret_cast<const uint8_t*>(scan_ptr);
2214 } else if (ib_type == VK_INDEX_TYPE_UINT16) {
2215 scan_index = *reinterpret_cast<const uint16_t*>(scan_ptr);
2216 } else {
2217 scan_index = *reinterpret_cast<const uint32_t*>(scan_ptr);
2218 }
2219 // keep track of the set of all indices used to reference vertices in the draw call
2220 size_t index_offset = scan_index - min_index;
Sam Walls61b06892020-07-23 16:20:50 +01002221 size_t bitset_bucket_index = index_offset / refs_per_bucket;
2222 uint64_t used_indices = 1ull << ((index_offset % refs_per_bucket) & 0xFFFFFFFFu);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002223 vertex_reference_buckets[bitset_bucket_index] |= used_indices;
2224 }
2225
2226 uint32_t vertex_reference_count = 0;
2227 for (const auto& bitset : vertex_reference_buckets) {
2228 vertex_reference_count += static_cast<uint32_t>(bitset.count());
2229 }
2230
2231 // 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 -07002232 float utilization = static_cast<float>(vertex_reference_count) / static_cast<float>(max_index - min_index + 1);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002233 // 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 -07002234 float cache_hit_rate = static_cast<float>(vertex_reference_count) / static_cast<float>(vertex_shade_count);
Sam Walls8e77e4f2020-03-16 20:47:40 +00002235
2236 if (utilization < 0.5f) {
2237 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_SparseIndexBuffer,
2238 "%s The indices which were specified for the draw call only utilise approximately "
2239 "%.02f%% of the bound vertex buffer.",
2240 VendorSpecificTag(kBPVendorArm), utilization);
2241 }
2242
2243 if (cache_hit_rate <= 0.5f) {
2244 skip |=
2245 LogPerformanceWarning(device, kVUID_BestPractices_CmdDrawIndexed_PostTransformCacheThrashing,
2246 "%s The indices which were specified for the draw call are estimated to cause thrashing of "
2247 "the post-transform vertex cache, with a hit-rate of %.02f%%. "
2248 "I.e. the ordering of the index buffer may not make optimal use of indices associated with "
2249 "recently shaded vertices.",
2250 VendorSpecificTag(kBPVendorArm), cache_hit_rate * 100.0f);
2251 }
2252 }
2253
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002254 return skip;
2255}
2256
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002257bool BestPractices::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
2258 const VkCommandBuffer* pCommandBuffers) const {
2259 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002260 const auto primary = GetRead<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002261 for (uint32_t i = 0; i < commandBufferCount; i++) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002262 const auto secondary_cb = GetRead<bp_state::CommandBuffer>(pCommandBuffers[i]);
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002263 if (secondary_cb == nullptr) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002264 continue;
2265 }
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002266 const auto& secondary = secondary_cb->render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002267 for (auto& clear : secondary.earlyClearAttachments) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002268 if (ClearAttachmentsIsFullClear(*primary, uint32_t(clear.rects.size()), clear.rects.data())) {
2269 skip |= ValidateClearAttachment(*primary, clear.framebufferAttachment, clear.colorAttachment, clear.aspects, true);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002270 }
2271 }
2272 }
Nadav Gevaf0808442021-05-21 13:51:25 -04002273
2274 if (VendorCheckEnabled(kBPVendorAMD)) {
2275 if (commandBufferCount > 0) {
2276 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CmdBuffer_AvoidSecondaryCmdBuffers,
2277 "%s Performance warning: Use of secondary command buffers is not recommended. ",
2278 VendorSpecificTag(kBPVendorAMD));
2279 }
2280 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002281 return skip;
2282}
2283
2284void BestPractices::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
2285 const VkCommandBuffer* pCommandBuffers) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002286 ValidationStateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
2287
2288 auto primary = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2289 if (!primary) {
2290 return;
2291 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002292
2293 for (uint32_t i = 0; i < commandBufferCount; i++) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002294 auto secondary = GetWrite<bp_state::CommandBuffer>(pCommandBuffers[i]);
2295 if (!secondary) {
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002296 continue;
2297 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002298
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002299 for (auto& early_clear : secondary->render_pass_state.earlyClearAttachments) {
2300 if (ClearAttachmentsIsFullClear(*primary, uint32_t(early_clear.rects.size()), early_clear.rects.data())) {
2301 RecordAttachmentClearAttachments(*primary, early_clear.framebufferAttachment, early_clear.colorAttachment,
2302 early_clear.aspects, uint32_t(early_clear.rects.size()), early_clear.rects.data());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002303 } else {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002304 RecordAttachmentAccess(*primary, early_clear.framebufferAttachment, early_clear.aspects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002305 }
2306 }
2307
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002308 for (auto& touch : secondary->render_pass_state.touchesAttachments) {
2309 RecordAttachmentAccess(*primary, touch.framebufferAttachment, touch.aspects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002310 }
Hans-Kristian Arntzenc7eb82a2021-06-16 13:57:18 +02002311
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002312 primary->render_pass_state.numDrawCallsDepthEqualCompare += secondary->render_pass_state.numDrawCallsDepthEqualCompare;
2313 primary->render_pass_state.numDrawCallsDepthOnly += secondary->render_pass_state.numDrawCallsDepthOnly;
Hans-Kristian Arntzenaf81dca2021-06-18 11:14:28 +02002314
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002315 if (secondary->hasDrawCmd) {
Hans-Kristian Arntzenaf81dca2021-06-18 11:14:28 +02002316 primary->hasDrawCmd = true;
2317 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002318 }
2319
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002320}
2321
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002322void BestPractices::RecordAttachmentAccess(bp_state::CommandBuffer& cb_state, uint32_t fb_attachment, VkImageAspectFlags aspects) {
2323 auto& state = cb_state.render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002324 // Called when we have a partial clear attachment, or a normal draw call which accesses an attachment.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002325 auto itr =
2326 std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
2327 [fb_attachment](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == fb_attachment; });
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002328
2329 if (itr != state.touchesAttachments.end()) {
2330 itr->aspects |= aspects;
2331 } else {
2332 state.touchesAttachments.push_back({ fb_attachment, aspects });
2333 }
2334}
2335
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002336void BestPractices::RecordAttachmentClearAttachments(bp_state::CommandBuffer& cmd_state, uint32_t fb_attachment,
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002337 uint32_t color_attachment, VkImageAspectFlags aspects, uint32_t rectCount,
2338 const VkClearRect* pRects) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002339 auto& state = cmd_state.render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002340 // If we observe a full clear before any other access to a frame buffer attachment,
2341 // we have candidate for redundant clear attachments.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002342 auto itr =
2343 std::find_if(state.touchesAttachments.begin(), state.touchesAttachments.end(),
2344 [fb_attachment](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == fb_attachment; });
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002345
2346 uint32_t new_aspects = aspects;
2347 if (itr != state.touchesAttachments.end()) {
2348 new_aspects = aspects & ~itr->aspects;
2349 itr->aspects |= aspects;
2350 } else {
2351 state.touchesAttachments.push_back({ fb_attachment, aspects });
2352 }
2353
2354 if (new_aspects == 0) {
2355 return;
2356 }
2357
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002358 if (cmd_state.createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002359 // The first command might be a clear, but might not be the first in the render pass, defer any checks until
2360 // CmdExecuteCommands.
2361 state.earlyClearAttachments.push_back({ fb_attachment, color_attachment, new_aspects,
2362 std::vector<VkClearRect>{pRects, pRects + rectCount} });
2363 }
2364}
2365
2366void BestPractices::PreCallRecordCmdClearAttachments(VkCommandBuffer commandBuffer,
2367 uint32_t attachmentCount, const VkClearAttachment* pClearAttachments,
2368 uint32_t rectCount, const VkClearRect* pRects) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002369 ValidationStateTracker::PreCallRecordCmdClearAttachments(commandBuffer, attachmentCount, pClearAttachments, rectCount, pRects);
2370
2371 auto cmd_state = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2372 auto* rp_state = cmd_state->activeRenderPass.get();
2373 auto* fb_state = cmd_state->activeFramebuffer.get();
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002374 bool is_secondary = cmd_state->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY;
2375
2376 if (rectCount == 0 || !rp_state) {
2377 return;
2378 }
2379
2380 if (!is_secondary && !fb_state) {
2381 return;
2382 }
2383
2384 // 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 -07002385 bool full_clear = ClearAttachmentsIsFullClear(*cmd_state, rectCount, pRects);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002386
ziga-lunarg885c6542022-03-07 01:08:25 +01002387 if (!rp_state->use_dynamic_rendering && !rp_state->use_dynamic_rendering_inherited) {
2388 auto& subpass = rp_state->createInfo.pSubpasses[cmd_state->activeSubpass];
2389 for (uint32_t i = 0; i < attachmentCount; i++) {
2390 auto& attachment = pClearAttachments[i];
2391 uint32_t fb_attachment = VK_ATTACHMENT_UNUSED;
2392 VkImageAspectFlags aspects = attachment.aspectMask;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002393
ziga-lunarg885c6542022-03-07 01:08:25 +01002394 if (aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
2395 if (subpass.pDepthStencilAttachment) {
2396 fb_attachment = subpass.pDepthStencilAttachment->attachment;
2397 }
2398 } else if (aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
2399 fb_attachment = subpass.pColorAttachments[attachment.colorAttachment].attachment;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002400 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002401
ziga-lunarg885c6542022-03-07 01:08:25 +01002402 if (fb_attachment != VK_ATTACHMENT_UNUSED) {
2403 if (full_clear) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002404 RecordAttachmentClearAttachments(*cmd_state, fb_attachment, attachment.colorAttachment,
ziga-lunarg885c6542022-03-07 01:08:25 +01002405 aspects, rectCount, pRects);
2406 } else {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002407 RecordAttachmentAccess(*cmd_state, fb_attachment, aspects);
ziga-lunarg885c6542022-03-07 01:08:25 +01002408 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002409 }
2410 }
2411 }
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002412}
2413
Attilio Provenzano02859b22020-02-27 14:17:28 +00002414void BestPractices::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2415 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2416 ValidationStateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset,
2417 firstInstance);
2418
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002419 auto cmd_state = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Attilio Provenzano02859b22020-02-27 14:17:28 +00002420 if ((indexCount * instanceCount) <= kSmallIndexedDrawcallIndices) {
2421 cmd_state->small_indexed_draw_call_count++;
2422 }
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002423
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002424 ValidateBoundDescriptorSets(*cmd_state, "vkCmdDrawIndexed()");
Attilio Provenzano02859b22020-02-27 14:17:28 +00002425}
2426
Sam Walls0961ec02020-03-31 16:39:15 +01002427void BestPractices::PostCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
2428 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
2429 StateTracker::PostCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
2430 RecordCmdDrawType(commandBuffer, indexCount * instanceCount, "vkCmdDrawIndexed()");
2431}
2432
sfricke-samsung681ab7b2020-10-29 01:53:35 -07002433bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2434 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
2435 uint32_t maxDrawCount, uint32_t stride) const {
2436 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCount()");
2437
2438 return skip;
2439}
2440
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002441bool BestPractices::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
2442 VkDeviceSize offset, VkBuffer countBuffer,
2443 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
2444 uint32_t stride) const {
2445 bool skip = ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirectCountKHR()");
Camden5b184be2019-08-13 07:50:19 -06002446
2447 return skip;
2448}
2449
2450bool BestPractices::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002451 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06002452 bool skip = false;
2453
2454 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002455 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
2456 "Warning: You are calling vkCmdDrawIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002457 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndirect()");
Camden5b184be2019-08-13 07:50:19 -06002458 }
2459
2460 return skip;
2461}
2462
Sam Walls0961ec02020-03-31 16:39:15 +01002463void BestPractices::PostCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2464 uint32_t count, uint32_t stride) {
2465 StateTracker::PostCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, count, stride);
2466 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndirect()");
2467}
2468
Camden5b184be2019-08-13 07:50:19 -06002469bool BestPractices::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002470 uint32_t drawCount, uint32_t stride) const {
Camden5b184be2019-08-13 07:50:19 -06002471 bool skip = false;
2472
2473 if (drawCount == 0) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002474 skip |= LogWarning(device, kVUID_BestPractices_CmdDraw_DrawCountZero,
2475 "Warning: You are calling vkCmdDrawIndexedIndirect() with a drawCount of Zero.");
Mark Lobodzinski4c4cf942019-12-20 11:09:51 -07002476 skip |= ValidateCmdDrawType(commandBuffer, "vkCmdDrawIndexedIndirect()");
Camden5b184be2019-08-13 07:50:19 -06002477 }
2478
2479 return skip;
2480}
2481
Sam Walls0961ec02020-03-31 16:39:15 +01002482void BestPractices::PostCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2483 uint32_t count, uint32_t stride) {
2484 StateTracker::PostCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, count, stride);
2485 RecordCmdDrawType(commandBuffer, count, "vkCmdDrawIndexedIndirect()");
2486}
2487
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002488void BestPractices::ValidateBoundDescriptorSets(bp_state::CommandBuffer& cb_state, const char* function_name) {
2489 for (auto descriptor_set : cb_state.validated_descriptor_sets) {
2490 const auto& layout = *descriptor_set->GetLayout();
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002491
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002492 for (uint32_t index = 0; index < descriptor_set->GetBindingCount(); ++index) {
2493 // For bindless scenarios, we should not attempt to track descriptor set state.
2494 // It is highly uncertain which resources are actually bound.
2495 // Resources which are written to such a descriptor should be marked as indeterminate w.r.t. state.
2496 VkDescriptorBindingFlags flags = layout.GetDescriptorBindingFlagsFromIndex(index);
2497 if (flags & (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT |
2498 VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT)) {
2499 continue;
2500 }
Hans-Kristian Arntzena8199012021-03-22 12:10:07 +01002501
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002502 auto index_range = layout.GetGlobalIndexRangeFromIndex(index);
2503 for (uint32_t i = index_range.start; i < index_range.end; ++i) {
2504 VkImageView image_view{VK_NULL_HANDLE};
2505
2506 auto descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2507 switch (descriptor->GetClass()) {
2508 case cvdescriptorset::DescriptorClass::Image: {
2509 if (const auto image_descriptor = static_cast<const cvdescriptorset::ImageDescriptor*>(descriptor)) {
2510 image_view = image_descriptor->GetImageView();
2511 }
2512 break;
2513 }
2514 case cvdescriptorset::DescriptorClass::ImageSampler: {
2515 if (const auto image_sampler_descriptor =
2516 static_cast<const cvdescriptorset::ImageSamplerDescriptor*>(descriptor)) {
2517 image_view = image_sampler_descriptor->GetImageView();
2518 }
2519 break;
2520 }
2521 default:
2522 break;
Hans-Kristian Arntzena8199012021-03-22 12:10:07 +01002523 }
2524
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002525 if (image_view) {
2526 auto image_view_state = Get<IMAGE_VIEW_STATE>(image_view);
2527 QueueValidateImageView(cb_state.queue_submit_functions, function_name, image_view_state.get(),
2528 IMAGE_SUBRESOURCE_USAGE_BP::DESCRIPTOR_ACCESS);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002529 }
2530 }
2531 }
2532 }
2533}
2534
2535void BestPractices::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
2536 uint32_t firstVertex, uint32_t firstInstance) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002537 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2538 ValidateBoundDescriptorSets(*cb_node, "vkCmdDraw()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002539}
2540
2541void BestPractices::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
2542 uint32_t drawCount, uint32_t stride) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002543 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2544 ValidateBoundDescriptorSets(*cb_node, "vkCmdDrawIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002545}
2546
2547void BestPractices::PreCallRecordCmdDrawIndexedIndirect(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, "vkCmdDrawIndexedIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002551}
2552
Camden5b184be2019-08-13 07:50:19 -06002553bool BestPractices::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t groupCountX, uint32_t groupCountY,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002554 uint32_t groupCountZ) const {
Camden5b184be2019-08-13 07:50:19 -06002555 bool skip = false;
2556
2557 if ((groupCountX == 0) || (groupCountY == 0) || (groupCountZ == 0)) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002558 skip |= LogWarning(device, kVUID_BestPractices_CmdDispatch_GroupCountZero,
2559 "Warning: You are calling vkCmdDispatch() while one or more groupCounts are zero (groupCountX = %" PRIu32
2560 ", groupCountY = %" PRIu32 ", groupCountZ = %" PRIu32 ").",
2561 groupCountX, groupCountY, groupCountZ);
Camden5b184be2019-08-13 07:50:19 -06002562 }
2563
2564 return skip;
2565}
Camden83a9c372019-08-14 11:41:38 -06002566
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002567bool BestPractices::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
2568 bool skip = false;
2569 skip |= StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
2570 skip |= ValidateCmdEndRenderPass(commandBuffer);
2571 return skip;
2572}
2573
2574bool BestPractices::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo* pSubpassEndInfo) const {
2575 bool skip = false;
2576 skip |= StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
2577 skip |= ValidateCmdEndRenderPass(commandBuffer);
2578 return skip;
2579}
2580
Sam Walls0961ec02020-03-31 16:39:15 +01002581bool BestPractices::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2582 bool skip = false;
Sam Walls0961ec02020-03-31 16:39:15 +01002583 skip |= StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
Hans-Kristian Arntzend9941a92021-06-18 12:31:30 +02002584 skip |= ValidateCmdEndRenderPass(commandBuffer);
2585 return skip;
2586}
2587
2588bool BestPractices::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
2589 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002590 const auto cmd = GetRead<bp_state::CommandBuffer>(commandBuffer);
Sam Walls0961ec02020-03-31 16:39:15 +01002591
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002592 if (cmd == nullptr) return skip;
2593 auto &render_pass_state = cmd->render_pass_state;
Sam Walls0961ec02020-03-31 16:39:15 +01002594
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002595 bool uses_depth = (render_pass_state.depthAttachment || render_pass_state.colorAttachment) &&
2596 render_pass_state.numDrawCallsDepthEqualCompare >= kDepthPrePassNumDrawCallsArm &&
2597 render_pass_state.numDrawCallsDepthOnly >= kDepthPrePassNumDrawCallsArm;
Sam Walls0961ec02020-03-31 16:39:15 +01002598 if (uses_depth) {
2599 skip |= LogPerformanceWarning(
2600 device, kVUID_BestPractices_EndRenderPass_DepthPrePassUsage,
2601 "%s Depth pre-passes may be in use. In general, this is not recommended, as in Arm Mali GPUs since "
2602 "Mali-T620, Forward Pixel Killing (FPK) can already perform automatic hidden surface removal; in which "
2603 "case, using depth pre-passes for hidden surface removal may worsen performance.",
2604 VendorSpecificTag(kBPVendorArm));
2605 }
2606
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002607 RENDER_PASS_STATE* rp = cmd->activeRenderPass.get();
2608
2609 if (VendorCheckEnabled(kBPVendorArm) && rp) {
2610
2611 // If we use an attachment on-tile, we should access it in some way. Otherwise,
2612 // it is redundant to have it be part of the render pass.
2613 // Only consider it redundant if it will actually consume bandwidth, i.e.
2614 // LOAD_OP_LOAD is used or STORE_OP_STORE. CLEAR -> DONT_CARE is benign,
2615 // as is using pure input attachments.
2616 // CLEAR -> STORE might be considered a "useful" thing to do, but
2617 // the optimal thing to do is to defer the clear until you're actually
2618 // going to render to the image.
2619
2620 uint32_t num_attachments = rp->createInfo.attachmentCount;
2621 for (uint32_t i = 0; i < num_attachments; i++) {
Hans-Kristian Arntzen237663c2021-07-01 14:36:40 +02002622 if (!RenderPassUsesAttachmentOnTile(rp->createInfo, i) ||
2623 RenderPassUsesAttachmentAsResolve(rp->createInfo, i)) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002624 continue;
2625 }
2626
2627 auto& attachment = rp->createInfo.pAttachments[i];
2628
2629 VkImageAspectFlags bandwidth_aspects = 0;
2630
2631 if (!FormatIsStencilOnly(attachment.format) &&
2632 (attachment.loadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
2633 attachment.storeOp == VK_ATTACHMENT_STORE_OP_STORE)) {
2634 if (FormatHasDepth(attachment.format)) {
2635 bandwidth_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
2636 } else {
2637 bandwidth_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
2638 }
2639 }
2640
2641 if (FormatHasStencil(attachment.format) &&
2642 (attachment.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
2643 attachment.stencilStoreOp == VK_ATTACHMENT_STORE_OP_STORE)) {
2644 bandwidth_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
2645 }
2646
2647 if (!bandwidth_aspects) {
2648 continue;
2649 }
2650
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002651 auto itr = std::find_if(render_pass_state.touchesAttachments.begin(), render_pass_state.touchesAttachments.end(),
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002652 [i](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == i; });
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002653 uint32_t untouched_aspects = bandwidth_aspects;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002654 if (itr != render_pass_state.touchesAttachments.end()) {
Hans-Kristian Arntzen808bfa12021-06-18 13:52:45 +02002655 untouched_aspects &= ~itr->aspects;
2656 }
2657
2658 if (untouched_aspects) {
2659 skip |= LogPerformanceWarning(
2660 device, kVUID_BestPractices_EndRenderPass_RedundantAttachmentOnTile,
2661 "%s Render pass was ended, but attachment #%u (format: %u, untouched aspects 0x%x) "
2662 "was never accessed by a pipeline or clear command. "
2663 "On tile-based architectures, LOAD_OP_LOAD and STORE_OP_STORE consume bandwidth and should not be part of the render pass "
2664 "if the attachments are not intended to be accessed.",
2665 VendorSpecificTag(kBPVendorArm), i, attachment.format, untouched_aspects);
2666 }
2667 }
2668 }
2669
Sam Walls0961ec02020-03-31 16:39:15 +01002670 return skip;
2671}
2672
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002673void BestPractices::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002674 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2675 ValidateBoundDescriptorSets(*cb_node, "vkCmdDispatch()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002676}
2677
2678void BestPractices::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002679 const auto cb_node = GetWrite<bp_state::CommandBuffer>(commandBuffer);
2680 ValidateBoundDescriptorSets(*cb_node, "vkCmdDispatchIndirect()");
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01002681}
2682
Camden Stocker9c051442019-11-06 14:28:43 -08002683bool BestPractices::ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(VkPhysicalDevice physicalDevice,
2684 const char* api_name) const {
2685 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002686 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Camden Stocker9c051442019-11-06 14:28:43 -08002687
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06002688 if (bp_pd_state) {
2689 if (bp_pd_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState == UNCALLED) {
2690 skip |= LogWarning(physicalDevice, kVUID_BestPractices_DisplayPlane_PropertiesNotCalled,
2691 "Potential problem with calling %s() without first retrieving properties from "
2692 "vkGetPhysicalDeviceDisplayPlanePropertiesKHR or vkGetPhysicalDeviceDisplayPlaneProperties2KHR.",
2693 api_name);
2694 }
Camden Stocker9c051442019-11-06 14:28:43 -08002695 }
2696
2697 return skip;
2698}
2699
Camden83a9c372019-08-14 11:41:38 -06002700bool BestPractices::PreCallValidateGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002701 uint32_t* pDisplayCount, VkDisplayKHR* pDisplays) const {
Camden83a9c372019-08-14 11:41:38 -06002702 bool skip = false;
2703
Camden Stocker9c051442019-11-06 14:28:43 -08002704 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneSupportedDisplaysKHR");
Camden83a9c372019-08-14 11:41:38 -06002705
Camden Stocker9c051442019-11-06 14:28:43 -08002706 return skip;
2707}
2708
2709bool BestPractices::PreCallValidateGetDisplayPlaneCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkDisplayModeKHR mode,
2710 uint32_t planeIndex,
2711 VkDisplayPlaneCapabilitiesKHR* pCapabilities) const {
2712 bool skip = false;
2713
2714 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilitiesKHR");
2715
2716 return skip;
2717}
2718
2719bool BestPractices::PreCallValidateGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice,
2720 const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo,
2721 VkDisplayPlaneCapabilities2KHR* pCapabilities) const {
2722 bool skip = false;
2723
2724 skip |= ValidateGetPhysicalDeviceDisplayPlanePropertiesKHRQuery(physicalDevice, "vkGetDisplayPlaneCapabilities2KHR");
Camden83a9c372019-08-14 11:41:38 -06002725
2726 return skip;
2727}
Camden05de2d42019-08-19 10:23:56 -06002728
2729bool BestPractices::PreCallValidateGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002730 VkImage* pSwapchainImages) const {
Camden05de2d42019-08-19 10:23:56 -06002731 bool skip = false;
2732
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002733 auto swapchain_state = Get<bp_state::Swapchain>(swapchain);
Camden05de2d42019-08-19 10:23:56 -06002734
Nathaniel Cesario39152e62021-07-02 13:04:16 -06002735 if (swapchain_state && pSwapchainImages) {
Camden05de2d42019-08-19 10:23:56 -06002736 // Compare the preliminary value of *pSwapchainImageCount with the value this time:
Nathaniel Cesario39152e62021-07-02 13:04:16 -06002737 if (swapchain_state->vkGetSwapchainImagesKHRState == UNCALLED) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002738 skip |=
2739 LogWarning(device, kVUID_Core_Swapchain_PriorCount,
2740 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImageCount; but no prior positive value has "
2741 "been seen for pSwapchainImages.");
Camden05de2d42019-08-19 10:23:56 -06002742 }
Camden05de2d42019-08-19 10:23:56 -06002743
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06002744 if (*pSwapchainImageCount > swapchain_state->get_swapchain_image_count) {
2745 skip |= LogWarning(
2746 device, kVUID_BestPractices_Swapchain_InvalidCount,
2747 "vkGetSwapchainImagesKHR() called with non-NULL pSwapchainImages, and with pSwapchainImageCount set to a "
Nadav Gevaf0808442021-05-21 13:51:25 -04002748 "value (%" PRId32 ") that is greater than the value (%" PRId32 ") that was returned when pSwapchainImages was NULL.",
Nathaniel Cesario4ce98382021-05-28 11:33:20 -06002749 *pSwapchainImageCount, swapchain_state->get_swapchain_image_count);
2750 }
2751 }
2752
Camden05de2d42019-08-19 10:23:56 -06002753 return skip;
2754}
2755
2756// Common function to handle validation for GetPhysicalDeviceQueueFamilyProperties & 2KHR version
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002757bool BestPractices::ValidateCommonGetPhysicalDeviceQueueFamilyProperties(const PHYSICAL_DEVICE_STATE* bp_pd_state,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002758 uint32_t requested_queue_family_property_count,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002759 const CALL_STATE call_state,
2760 const char* caller_name) const {
Camden05de2d42019-08-19 10:23:56 -06002761 bool skip = false;
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002762 // Verify that for each physical device, this command is called first with NULL pQueueFamilyProperties in order to get count
2763 if (UNCALLED == call_state) {
2764 skip |= LogWarning(
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002765 bp_pd_state->Handle(), kVUID_Core_DevLimit_MissingQueryCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002766 "%s is called with non-NULL pQueueFamilyProperties before obtaining pQueueFamilyPropertyCount. It is "
2767 "recommended "
2768 "to first call %s with NULL pQueueFamilyProperties in order to obtain the maximal pQueueFamilyPropertyCount.",
2769 caller_name, caller_name);
2770 // Then verify that pCount that is passed in on second call matches what was returned
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002771 } else if (bp_pd_state->queue_family_known_count != requested_queue_family_property_count) {
2772 skip |= LogWarning(bp_pd_state->Handle(), kVUID_Core_DevLimit_CountMismatch,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002773 "%s is called with non-NULL pQueueFamilyProperties and pQueueFamilyPropertyCount value %" PRIu32
2774 ", but the largest previously returned pQueueFamilyPropertyCount for this physicalDevice is %" PRIu32
2775 ". It is recommended to instead receive all the properties by calling %s with "
2776 "pQueueFamilyPropertyCount that was "
2777 "previously obtained by calling %s with NULL pQueueFamilyProperties.",
Jeremy Gebben383b9a32021-09-08 16:31:33 -06002778 caller_name, requested_queue_family_property_count, bp_pd_state->queue_family_known_count, caller_name,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002779 caller_name);
Camden05de2d42019-08-19 10:23:56 -06002780 }
2781
2782 return skip;
2783}
2784
Jeff Bolz5c801d12019-10-09 10:38:45 -05002785bool BestPractices::PreCallValidateBindAccelerationStructureMemoryNV(
2786 VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV* pBindInfos) const {
Camden Stocker82510582019-09-03 14:00:16 -06002787 bool skip = false;
2788
2789 for (uint32_t i = 0; i < bindInfoCount; i++) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002790 auto as_state = Get<ACCELERATION_STRUCTURE_STATE>(pBindInfos[i].accelerationStructure);
Camden Stocker82510582019-09-03 14:00:16 -06002791 if (!as_state->memory_requirements_checked) {
2792 // There's not an explicit requirement in the spec to call vkGetImageMemoryRequirements() prior to calling
2793 // BindAccelerationStructureMemoryNV but it's implied in that memory being bound must conform with
2794 // VkAccelerationStructureMemoryRequirementsInfoNV from vkGetAccelerationStructureMemoryRequirementsNV
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002795 skip |= LogWarning(
2796 device, kVUID_BestPractices_BindAccelNV_NoMemReqQuery,
Camden Stocker82510582019-09-03 14:00:16 -06002797 "vkBindAccelerationStructureMemoryNV(): "
2798 "Binding memory to %s but vkGetAccelerationStructureMemoryRequirementsNV() has not been called on that structure.",
2799 report_data->FormatHandle(pBindInfos[i].accelerationStructure).c_str());
2800 }
2801 }
2802
2803 return skip;
2804}
2805
Camden05de2d42019-08-19 10:23:56 -06002806bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
2807 uint32_t* pQueueFamilyPropertyCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002808 VkQueueFamilyProperties* pQueueFamilyProperties) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002809 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002810 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002811 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002812 bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
2813 "vkGetPhysicalDeviceQueueFamilyProperties()");
2814 }
2815 return false;
Camden05de2d42019-08-19 10:23:56 -06002816}
2817
Mike Schuchardt2df08912020-12-15 16:28:09 -08002818bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
2819 uint32_t* pQueueFamilyPropertyCount,
2820 VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002821 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002822 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002823 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002824 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
2825 "vkGetPhysicalDeviceQueueFamilyProperties2()");
2826 }
2827 return false;
Camden05de2d42019-08-19 10:23:56 -06002828}
2829
Jeff Bolz5c801d12019-10-09 10:38:45 -05002830bool BestPractices::PreCallValidateGetPhysicalDeviceQueueFamilyProperties2KHR(
Mike Schuchardt2df08912020-12-15 16:28:09 -08002831 VkPhysicalDevice physicalDevice, uint32_t* pQueueFamilyPropertyCount, VkQueueFamilyProperties2* pQueueFamilyProperties) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002832 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002833 if (pQueueFamilyProperties && bp_pd_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002834 return ValidateCommonGetPhysicalDeviceQueueFamilyProperties(bp_pd_state.get(), *pQueueFamilyPropertyCount,
Nathaniel Cesario56a96652020-12-30 13:23:42 -07002835 bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
2836 "vkGetPhysicalDeviceQueueFamilyProperties2KHR()");
2837 }
2838 return false;
Camden05de2d42019-08-19 10:23:56 -06002839}
2840
2841bool BestPractices::PreCallValidateGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
2842 uint32_t* pSurfaceFormatCount,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002843 VkSurfaceFormatKHR* pSurfaceFormats) const {
Camden05de2d42019-08-19 10:23:56 -06002844 if (!pSurfaceFormats) return false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002845 const auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06002846 const auto& call_state = bp_pd_state->vkGetPhysicalDeviceSurfaceFormatsKHRState;
Camden05de2d42019-08-19 10:23:56 -06002847 bool skip = false;
2848 if (call_state == UNCALLED) {
2849 // Since we haven't recorded a preliminary value of *pSurfaceFormatCount, that likely means that the application didn't
2850 // previously call this function with a NULL value of pSurfaceFormats:
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002851 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_MustQueryCount,
2852 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount; but no prior "
2853 "positive value has been seen for pSurfaceFormats.");
Camden05de2d42019-08-19 10:23:56 -06002854 } else {
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06002855 if (*pSurfaceFormatCount > bp_pd_state->surface_formats_count) {
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002856 skip |= LogWarning(physicalDevice, kVUID_Core_DevLimit_CountMismatch,
2857 "vkGetPhysicalDeviceSurfaceFormatsKHR() called with non-NULL pSurfaceFormatCount, and with "
2858 "pSurfaceFormats set to a value (%u) that is greater than the value (%u) that was returned "
2859 "when pSurfaceFormatCount was NULL.",
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06002860 *pSurfaceFormatCount, bp_pd_state->surface_formats_count);
Camden05de2d42019-08-19 10:23:56 -06002861 }
2862 }
2863 return skip;
2864}
Camden Stocker23cc47d2019-09-03 14:53:57 -06002865
2866bool BestPractices::PreCallValidateQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002867 VkFence fence) const {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002868 bool skip = false;
2869
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002870 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
2871 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
Camden Stocker23cc47d2019-09-03 14:53:57 -06002872 // 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 -07002873 layer_data::unordered_set<const IMAGE_STATE*> sparse_images;
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002874 // Track images getting metadata bound by this call in a set, it'll be recorded into the image_state
2875 // in RecordQueueBindSparse.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07002876 layer_data::unordered_set<const IMAGE_STATE*> sparse_images_with_metadata;
Camden Stocker23cc47d2019-09-03 14:53:57 -06002877 // 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 -07002878 for (uint32_t i = 0; i < bind_info.imageBindCount; ++i) {
2879 const auto& image_bind = bind_info.pImageBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04002880 auto image_state = Get<IMAGE_STATE>(image_bind.image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002881 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002882 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002883 }
Jeremy Gebben9f537102021-10-05 16:37:12 -06002884 sparse_images.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002885 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
2886 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
2887 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002888 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002889 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
2890 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002891 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002892 }
2893 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002894 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002895 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002896 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002897 "vkQueueBindSparse(): Binding sparse memory to %s without first calling "
2898 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002899 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002900 }
2901 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002902 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
2903 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04002904 auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002905 if (!image_state) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002906 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002907 }
Jeremy Gebben9f537102021-10-05 16:37:12 -06002908 sparse_images.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002909 if (image_state->createInfo.flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
2910 if (!image_state->get_sparse_reqs_called || image_state->sparse_requirements.empty()) {
2911 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002912 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002913 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
2914 "vkGetImageSparseMemoryRequirements[2KHR]() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002915 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002916 }
2917 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06002918 if (!image_state->memory_requirements_checked[0]) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002919 // For now just warning if sparse image binding occurs without calling to get reqs first
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002920 skip |= LogWarning(image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002921 "vkQueueBindSparse(): Binding opaque sparse memory to %s without first calling "
2922 "vkGetImageMemoryRequirements() to retrieve requirements.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002923 report_data->FormatHandle(image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002924 }
2925 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
2926 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002927 sparse_images_with_metadata.insert(image_state.get());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002928 }
2929 }
2930 }
2931 for (const auto& sparse_image_state : sparse_images) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002932 if (sparse_image_state->sparse_metadata_required && !sparse_image_state->sparse_metadata_bound &&
2933 sparse_images_with_metadata.find(sparse_image_state) == sparse_images_with_metadata.end()) {
Camden Stocker23cc47d2019-09-03 14:53:57 -06002934 // Warn if sparse image binding metadata required for image with sparse binding, but metadata not bound
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002935 skip |= LogWarning(sparse_image_state->image(), kVUID_Core_MemTrack_InvalidState,
Mark Lobodzinskib6e2a282020-01-29 16:03:26 -07002936 "vkQueueBindSparse(): Binding sparse memory to %s which requires a metadata aspect but no "
2937 "binding with VK_SPARSE_MEMORY_BIND_METADATA_BIT set was made.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002938 report_data->FormatHandle(sparse_image_state->image()).c_str());
Camden Stocker23cc47d2019-09-03 14:53:57 -06002939 }
2940 }
2941 }
2942
2943 return skip;
2944}
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002945
Mark Lobodzinski84101d72020-04-24 09:43:48 -06002946void BestPractices::ManualPostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
2947 VkFence fence, VkResult result) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07002948 if (result != VK_SUCCESS) {
Mark Lobodzinski205b7a02020-02-21 13:23:17 -07002949 return;
2950 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002951
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002952 for (uint32_t bind_idx = 0; bind_idx < bindInfoCount; bind_idx++) {
2953 const VkBindSparseInfo& bind_info = pBindInfo[bind_idx];
2954 for (uint32_t i = 0; i < bind_info.imageOpaqueBindCount; ++i) {
2955 const auto& image_opaque_bind = bind_info.pImageOpaqueBinds[i];
Nadav Gevaf0808442021-05-21 13:51:25 -04002956 auto image_state = Get<IMAGE_STATE>(bind_info.pImageOpaqueBinds[i].image);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002957 if (!image_state) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002958 continue; // Param/Object validation should report image_bind.image handles being invalid, so just skip here.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002959 }
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002960 for (uint32_t j = 0; j < image_opaque_bind.bindCount; ++j) {
2961 if (image_opaque_bind.pBinds[j].flags & VK_SPARSE_MEMORY_BIND_METADATA_BIT) {
2962 image_state->sparse_metadata_bound = true;
2963 }
2964 }
2965 }
2966 }
2967}
Camden Stocker0e0f89b2019-10-16 12:24:31 -07002968
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002969bool BestPractices::ClearAttachmentsIsFullClear(const bp_state::CommandBuffer& cmd, uint32_t rectCount,
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06002970 const VkClearRect* pRects) const {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002971 if (cmd.createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002972 // We don't know the accurate render area in a secondary,
2973 // so assume we clear the entire frame buffer.
2974 // This is resolved in CmdExecuteCommands where we can check if the clear is a full clear.
2975 return true;
2976 }
2977
2978 // If we have a rect which covers the entire frame buffer, we have a LOAD_OP_CLEAR-like command.
2979 for (uint32_t i = 0; i < rectCount; i++) {
2980 auto& rect = pRects[i];
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002981 auto& render_area = cmd.activeRenderPassBeginInfo.renderArea;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002982 if (rect.rect.extent.width == render_area.extent.width && rect.rect.extent.height == render_area.extent.height) {
2983 return true;
2984 }
2985 }
2986
2987 return false;
2988}
2989
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002990bool BestPractices::ValidateClearAttachment(const bp_state::CommandBuffer& cmd, uint32_t fb_attachment, uint32_t color_attachment,
2991 VkImageAspectFlags aspects, bool secondary) const {
2992 const RENDER_PASS_STATE* rp = cmd.activeRenderPass.get();
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02002993 bool skip = false;
2994
2995 if (!rp || fb_attachment == VK_ATTACHMENT_UNUSED) {
2996 return skip;
2997 }
2998
Jeremy Gebben20da7a12022-02-25 14:07:46 -07002999 const auto& rp_state = cmd.render_pass_state;
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003000
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003001 auto attachment_itr =
3002 std::find_if(rp_state.touchesAttachments.begin(), rp_state.touchesAttachments.end(),
3003 [fb_attachment](const bp_state::AttachmentInfo& info) { return info.framebufferAttachment == fb_attachment; });
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003004
3005 // Only report aspects which haven't been touched yet.
3006 VkImageAspectFlags new_aspects = aspects;
Jeremy Gebben7c2cd8b2021-08-11 15:40:38 -06003007 if (attachment_itr != rp_state.touchesAttachments.end()) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003008 new_aspects &= ~attachment_itr->aspects;
3009 }
3010
3011 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003012 if (!cmd.hasDrawCmd) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003013 skip |= LogPerformanceWarning(
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003014 cmd.Handle(), kVUID_BestPractices_DrawState_ClearCmdBeforeDraw,
Hans-Kristian Arntzen4ddd6182021-06-18 12:16:33 +02003015 "vkCmdClearAttachments() issued on %s prior to any Draw Cmds in current render pass. It is recommended you "
3016 "use RenderPass LOAD_OP_CLEAR on attachments instead.",
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003017 report_data->FormatHandle(cmd.Handle()).c_str());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003018 }
3019
3020 if ((new_aspects & VK_IMAGE_ASPECT_COLOR_BIT) &&
3021 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
3022 skip |= LogPerformanceWarning(
3023 device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3024 "%svkCmdClearAttachments() issued on %s for color attachment #%u in this subpass, "
3025 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3026 "it is more efficient.",
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003027 secondary ? "vkCmdExecuteCommands(): " : "", report_data->FormatHandle(cmd.Handle()).c_str(), color_attachment);
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003028 }
3029
3030 if ((new_aspects & VK_IMAGE_ASPECT_DEPTH_BIT) &&
3031 rp->createInfo.pAttachments[fb_attachment].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003032 skip |=
3033 LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3034 "%svkCmdClearAttachments() issued on %s for the depth attachment in this subpass, "
3035 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3036 "it is more efficient.",
3037 secondary ? "vkCmdExecuteCommands(): " : "", report_data->FormatHandle(cmd.Handle()).c_str());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003038 }
3039
3040 if ((new_aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&
3041 rp->createInfo.pAttachments[fb_attachment].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003042 skip |=
3043 LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachments_ClearAfterLoad,
3044 "%svkCmdClearAttachments() issued on %s for the stencil attachment in this subpass, "
3045 "but LOAD_OP_LOAD was used. If you need to clear the framebuffer, always use LOAD_OP_CLEAR as "
3046 "it is more efficient.",
3047 secondary ? "vkCmdExecuteCommands(): " : "", report_data->FormatHandle(cmd.Handle()).c_str());
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003048 }
3049
3050 return skip;
3051}
3052
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003053bool BestPractices::PreCallValidateCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
Camden Stockerf55721f2019-09-09 11:04:49 -06003054 const VkClearAttachment* pAttachments, uint32_t rectCount,
3055 const VkClearRect* pRects) const {
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003056 bool skip = false;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003057 const auto cb_node = GetRead<bp_state::CommandBuffer>(commandBuffer);
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003058 if (!cb_node) return skip;
3059
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003060 if (cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
3061 // Defer checks to ExecuteCommands.
3062 return skip;
3063 }
3064
3065 // Only care about full clears, partial clears might have legitimate uses.
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003066 if (!ClearAttachmentsIsFullClear(*cb_node, rectCount, pRects)) {
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003067 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003068 }
3069
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003070 // Check for uses of ClearAttachments along with LOAD_OP_LOAD,
3071 // as it can be more efficient to just use LOAD_OP_CLEAR
locke-lunargaecf2152020-05-12 17:15:41 -06003072 const RENDER_PASS_STATE* rp = cb_node->activeRenderPass.get();
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003073 if (rp) {
3074 const auto& subpass = rp->createInfo.pSubpasses[cb_node->activeSubpass];
3075
3076 for (uint32_t i = 0; i < attachmentCount; i++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02003077 const auto& attachment = pAttachments[i];
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003078
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003079 if (attachment.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
3080 uint32_t color_attachment = attachment.colorAttachment;
3081 uint32_t fb_attachment = subpass.pColorAttachments[color_attachment].attachment;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003082 skip |= ValidateClearAttachment(*cb_node, fb_attachment, color_attachment, attachment.aspectMask, false);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003083 }
3084
Hans-Kristian Arntzenb6586312021-07-05 11:43:39 +02003085 if (subpass.pDepthStencilAttachment &&
3086 (attachment.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))) {
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003087 uint32_t fb_attachment = subpass.pDepthStencilAttachment->attachment;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003088 skip |= ValidateClearAttachment(*cb_node, fb_attachment, VK_ATTACHMENT_UNUSED, attachment.aspectMask, false);
Attilio Provenzano1d9a8362020-02-27 12:23:51 +00003089 }
3090 }
3091 }
3092
Nadav Gevaf0808442021-05-21 13:51:25 -04003093 if (VendorCheckEnabled(kBPVendorAMD)) {
3094 for (uint32_t attachment_idx = 0; attachment_idx < attachmentCount; attachment_idx++) {
3095 if (pAttachments[attachment_idx].aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) {
3096 bool black_check = false;
3097 black_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 0.0f;
3098 black_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 0.0f;
3099 black_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 0.0f;
3100 black_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f &&
3101 pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f;
3102
3103 bool white_check = false;
3104 white_check |= pAttachments[attachment_idx].clearValue.color.float32[0] != 1.0f;
3105 white_check |= pAttachments[attachment_idx].clearValue.color.float32[1] != 1.0f;
3106 white_check |= pAttachments[attachment_idx].clearValue.color.float32[2] != 1.0f;
3107 white_check |= pAttachments[attachment_idx].clearValue.color.float32[3] != 0.0f &&
3108 pAttachments[attachment_idx].clearValue.color.float32[3] != 1.0f;
3109
3110 if (black_check && white_check) {
3111 skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues,
3112 "%s Performance warning: vkCmdClearAttachments() clear value for color attachment %" PRId32 " is not a fast clear value."
3113 "Consider changing to one of the following:"
3114 "RGBA(0, 0, 0, 0) "
3115 "RGBA(0, 0, 0, 1) "
3116 "RGBA(1, 1, 1, 0) "
3117 "RGBA(1, 1, 1, 1)",
3118 VendorSpecificTag(kBPVendorAMD), attachment_idx);
3119 }
3120 } else {
3121 if ((pAttachments[attachment_idx].clearValue.depthStencil.depth != 0 &&
3122 pAttachments[attachment_idx].clearValue.depthStencil.depth != 1) &&
3123 pAttachments[attachment_idx].clearValue.depthStencil.stencil != 0) {
3124 skip |= LogPerformanceWarning(device, kVUID_BestPractices_ClearAttachment_FastClearValues,
3125 "%s Performance warning: vkCmdClearAttachments() clear value for depth/stencil "
3126 "attachment %" PRId32 " is not a fast clear value."
3127 "Consider changing to one of the following:"
3128 "D=0.0f, S=0"
3129 "D=1.0f, S=0",
3130 VendorSpecificTag(kBPVendorAMD), attachment_idx);
3131 }
3132 }
3133 }
3134 }
3135
Camden Stockerf55721f2019-09-09 11:04:49 -06003136 return skip;
Camden Stocker0e0f89b2019-10-16 12:24:31 -07003137}
Attilio Provenzano02859b22020-02-27 14:17:28 +00003138
3139bool BestPractices::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3140 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3141 const VkImageResolve* pRegions) const {
3142 bool skip = false;
3143
3144 skip |= VendorCheckEnabled(kBPVendorArm) &&
3145 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage_ResolvingImage,
3146 "%s Attempting to use vkCmdResolveImage to resolve a multisampled image. "
3147 "This is a very slow and extremely bandwidth intensive path. "
3148 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3149 VendorSpecificTag(kBPVendorArm));
3150
3151 return skip;
3152}
3153
Jeff Leger178b1e52020-10-05 12:22:23 -04003154bool BestPractices::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
3155 const VkResolveImageInfo2KHR* pResolveImageInfo) const {
3156 bool skip = false;
3157
3158 skip |= VendorCheckEnabled(kBPVendorArm) &&
3159 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2KHR_ResolvingImage,
3160 "%s Attempting to use vkCmdResolveImage2KHR to resolve a multisampled image. "
3161 "This is a very slow and extremely bandwidth intensive path. "
3162 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3163 VendorSpecificTag(kBPVendorArm));
3164
3165 return skip;
3166}
3167
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003168bool BestPractices::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer,
3169 const VkResolveImageInfo2* pResolveImageInfo) const {
3170 bool skip = false;
3171
3172 skip |= VendorCheckEnabled(kBPVendorArm) &&
3173 LogPerformanceWarning(device, kVUID_BestPractices_CmdResolveImage2_ResolvingImage,
3174 "%s Attempting to use vkCmdResolveImage2 to resolve a multisampled image. "
3175 "This is a very slow and extremely bandwidth intensive path. "
3176 "You should always resolve multisampled images on-tile with pResolveAttachments in VkRenderPass.",
3177 VendorSpecificTag(kBPVendorArm));
3178
3179 return skip;
3180}
3181
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003182void BestPractices::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3183 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3184 const VkImageResolve* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003185 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003186 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003187 auto src = Get<bp_state::Image>(srcImage);
3188 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003189
3190 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003191 QueueValidateImage(funcs, "vkCmdResolveImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pRegions[i].srcSubresource);
3192 QueueValidateImage(funcs, "vkCmdResolveImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003193 }
3194}
3195
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003196void BestPractices::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
3197 const VkResolveImageInfo2KHR* pResolveImageInfo) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003198 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003199 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003200 auto src = Get<bp_state::Image>(pResolveImageInfo->srcImage);
3201 auto dst = Get<bp_state::Image>(pResolveImageInfo->dstImage);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003202 uint32_t regionCount = pResolveImageInfo->regionCount;
3203
3204 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003205 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ, pResolveImageInfo->pRegions[i].srcSubresource);
3206 QueueValidateImage(funcs, "vkCmdResolveImage2KHR()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE, pResolveImageInfo->pRegions[i].dstSubresource);
Hans-Kristian Arntzen9e030f12021-03-17 13:09:30 +01003207 }
3208}
3209
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003210void BestPractices::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer,
3211 const VkResolveImageInfo2* pResolveImageInfo) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003212 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003213 auto& funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003214 auto src = Get<bp_state::Image>(pResolveImageInfo->srcImage);
3215 auto dst = Get<bp_state::Image>(pResolveImageInfo->dstImage);
Tony-LunarGd36f5f32022-01-20 11:49:59 -07003216 uint32_t regionCount = pResolveImageInfo->regionCount;
3217
3218 for (uint32_t i = 0; i < regionCount; i++) {
3219 QueueValidateImage(funcs, "vkCmdResolveImage2()", src, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_READ,
3220 pResolveImageInfo->pRegions[i].srcSubresource);
3221 QueueValidateImage(funcs, "vkCmdResolveImage2()", dst, IMAGE_SUBRESOURCE_USAGE_BP::RESOLVE_WRITE,
3222 pResolveImageInfo->pRegions[i].dstSubresource);
3223 }
3224}
3225
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003226void BestPractices::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3227 const VkClearColorValue* pColor, uint32_t rangeCount,
3228 const VkImageSubresourceRange* pRanges) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003229 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003230 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003231 auto dst = Get<bp_state::Image>(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003232
3233 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003234 QueueValidateImage(funcs, "vkCmdClearColorImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003235 }
3236}
3237
3238void BestPractices::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3239 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
3240 const VkImageSubresourceRange* pRanges) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003241 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003242 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003243 auto dst = Get<bp_state::Image>(image);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003244
3245 for (uint32_t i = 0; i < rangeCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003246 QueueValidateImage(funcs, "vkCmdClearDepthStencilImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::CLEARED, pRanges[i]);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003247 }
3248}
3249
3250void BestPractices::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3251 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3252 const VkImageCopy* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003253 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003254 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003255 auto src = Get<bp_state::Image>(srcImage);
3256 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003257
3258 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003259 QueueValidateImage(funcs, "vkCmdCopyImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].srcSubresource);
3260 QueueValidateImage(funcs, "vkCmdCopyImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003261 }
3262}
3263
3264void BestPractices::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3265 VkImageLayout dstImageLayout, uint32_t regionCount,
3266 const VkBufferImageCopy* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003267 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003268 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003269 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003270
3271 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003272 QueueValidateImage(funcs, "vkCmdCopyBufferToImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::COPY_WRITE, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003273 }
3274}
3275
3276void BestPractices::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3277 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy* pRegions) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003278 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003279 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003280 auto src = Get<bp_state::Image>(srcImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003281
3282 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003283 QueueValidateImage(funcs, "vkCmdCopyImageToBuffer()", src, IMAGE_SUBRESOURCE_USAGE_BP::COPY_READ, pRegions[i].imageSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003284 }
3285}
3286
3287void BestPractices::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3288 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3289 const VkImageBlit* pRegions, VkFilter filter) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003290 auto cb = GetWrite<bp_state::CommandBuffer>(commandBuffer);
Hans-Kristian Arntzendd8acbb2021-03-22 13:41:47 +01003291 auto &funcs = cb->queue_submit_functions;
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003292 auto src = Get<bp_state::Image>(srcImage);
3293 auto dst = Get<bp_state::Image>(dstImage);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003294
3295 for (uint32_t i = 0; i < regionCount; i++) {
Hans-Kristian Arntzenc8b831c2021-04-28 15:29:49 +02003296 QueueValidateImage(funcs, "vkCmdBlitImage()", src, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_READ, pRegions[i].srcSubresource);
3297 QueueValidateImage(funcs, "vkCmdBlitImage()", dst, IMAGE_SUBRESOURCE_USAGE_BP::BLIT_WRITE, pRegions[i].dstSubresource);
ZandroFargnoli1ced2b62020-06-18 16:49:34 +01003298 }
3299}
3300
Attilio Provenzano02859b22020-02-27 14:17:28 +00003301bool BestPractices::PreCallValidateCreateSampler(VkDevice device, const VkSamplerCreateInfo* pCreateInfo,
3302 const VkAllocationCallbacks* pAllocator, VkSampler* pSampler) const {
3303 bool skip = false;
3304
3305 if (VendorCheckEnabled(kBPVendorArm)) {
3306 if ((pCreateInfo->addressModeU != pCreateInfo->addressModeV) || (pCreateInfo->addressModeV != pCreateInfo->addressModeW)) {
3307 skip |= LogPerformanceWarning(
3308 device, kVUID_BestPractices_CreateSampler_DifferentWrappingModes,
3309 "%s Creating a sampler object with wrapping modes which do not match (U = %u, V = %u, W = %u). "
3310 "This may cause reduced performance even if only U (1D image) or U/V wrapping modes (2D "
3311 "image) are actually used. If you need different wrapping modes, disregard this warning.",
Jeremy Gebbenda6b48f2021-05-13 10:46:18 -06003312 VendorSpecificTag(kBPVendorArm), pCreateInfo->addressModeU, pCreateInfo->addressModeV, pCreateInfo->addressModeW);
Attilio Provenzano02859b22020-02-27 14:17:28 +00003313 }
3314
3315 if ((pCreateInfo->minLod != 0.0f) || (pCreateInfo->maxLod < VK_LOD_CLAMP_NONE)) {
3316 skip |= LogPerformanceWarning(
3317 device, kVUID_BestPractices_CreateSampler_LodClamping,
3318 "%s Creating a sampler object with LOD clamping (minLod = %f, maxLod = %f). This may cause reduced performance. "
3319 "Instead of clamping LOD in the sampler, consider using an VkImageView which restricts the mip-levels, set minLod "
3320 "to 0.0, and maxLod to VK_LOD_CLAMP_NONE.",
3321 VendorSpecificTag(kBPVendorArm), pCreateInfo->minLod, pCreateInfo->maxLod);
3322 }
3323
3324 if (pCreateInfo->mipLodBias != 0.0f) {
3325 skip |=
3326 LogPerformanceWarning(device, kVUID_BestPractices_CreateSampler_LodBias,
3327 "%s Creating a sampler object with LOD bias != 0.0 (%f). This will lead to less efficient "
3328 "descriptors being created and may cause reduced performance.",
3329 VendorSpecificTag(kBPVendorArm), pCreateInfo->mipLodBias);
3330 }
3331
3332 if ((pCreateInfo->addressModeU == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
3333 pCreateInfo->addressModeV == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
3334 pCreateInfo->addressModeW == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) &&
3335 (pCreateInfo->borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK)) {
3336 skip |= LogPerformanceWarning(
3337 device, kVUID_BestPractices_CreateSampler_BorderClampColor,
3338 "%s Creating a sampler object with border clamping and borderColor != VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK. "
3339 "This will lead to less efficient descriptors being created and may cause reduced performance. "
3340 "If possible, use VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK as the border color.",
3341 VendorSpecificTag(kBPVendorArm));
3342 }
3343
3344 if (pCreateInfo->unnormalizedCoordinates) {
3345 skip |= LogPerformanceWarning(
3346 device, kVUID_BestPractices_CreateSampler_UnnormalizedCoordinates,
3347 "%s Creating a sampler object with unnormalized coordinates. This will lead to less efficient "
3348 "descriptors being created and may cause reduced performance.",
3349 VendorSpecificTag(kBPVendorArm));
3350 }
3351
3352 if (pCreateInfo->anisotropyEnable) {
3353 skip |= LogPerformanceWarning(
3354 device, kVUID_BestPractices_CreateSampler_Anisotropy,
3355 "%s Creating a sampler object with anisotropy. This will lead to less efficient descriptors being created "
3356 "and may cause reduced performance.",
3357 VendorSpecificTag(kBPVendorArm));
3358 }
3359 }
3360
3361 return skip;
3362}
Sam Walls8e77e4f2020-03-16 20:47:40 +00003363
Nadav Gevaf0808442021-05-21 13:51:25 -04003364void BestPractices::PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
3365 const VkGraphicsPipelineCreateInfo* pCreateInfos,
3366 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
3367 void* cgpl_state) {
3368 ValidationStateTracker::PreCallRecordCreateGraphicsPipelines(device, pipelineCache, createInfoCount, pCreateInfos, pAllocator,
3369 pPipelines);
3370 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003371 num_pso_ += createInfoCount;
Nadav Gevaf0808442021-05-21 13:51:25 -04003372}
3373
3374bool BestPractices::PreCallValidateUpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount,
3375 const VkWriteDescriptorSet* pDescriptorWrites, uint32_t descriptorCopyCount,
3376 const VkCopyDescriptorSet* pDescriptorCopies) const {
3377 bool skip = false;
3378 if (VendorCheckEnabled(kBPVendorAMD)) {
3379 if (descriptorCopyCount > 0) {
3380 skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_AvoidCopyingDescriptors,
3381 "%s Performance warning: copying descriptor sets is not recommended",
3382 VendorSpecificTag(kBPVendorAMD));
3383 }
3384 }
3385
3386 return skip;
3387}
3388
3389bool BestPractices::PreCallValidateCreateDescriptorUpdateTemplate(VkDevice device,
3390 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
3391 const VkAllocationCallbacks* pAllocator,
3392 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate) const {
3393 bool skip = false;
3394 if (VendorCheckEnabled(kBPVendorAMD)) {
3395 skip |= LogPerformanceWarning(device, kVUID_BestPractices_UpdateDescriptors_PreferNonTemplate,
3396 "%s Performance warning: using DescriptorSetWithTemplate is not recommended. Prefer using "
3397 "vkUpdateDescriptorSet instead",
3398 VendorSpecificTag(kBPVendorAMD));
3399 }
3400
3401 return skip;
3402}
3403
3404bool BestPractices::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
3405 const VkClearColorValue* pColor, uint32_t rangeCount,
3406 const VkImageSubresourceRange* pRanges) const {
3407 bool skip = false;
3408 if (VendorCheckEnabled(kBPVendorAMD)) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003409 skip |= LogPerformanceWarning(
3410 device, kVUID_BestPractices_ClearAttachment_ClearImage,
Nadav Gevaf0808442021-05-21 13:51:25 -04003411 "%s Performance warning: using vkCmdClearColorImage is not recommended. Prefer using LOAD_OP_CLEAR or "
3412 "vkCmdClearAttachments instead",
3413 VendorSpecificTag(kBPVendorAMD));
3414 }
3415
3416 return skip;
3417}
3418
3419bool BestPractices::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
3420 VkImageLayout imageLayout,
3421 const VkClearDepthStencilValue* pDepthStencil, uint32_t rangeCount,
3422 const VkImageSubresourceRange* pRanges) const {
3423 bool skip = false;
3424 if (VendorCheckEnabled(kBPVendorAMD)) {
3425 skip |= LogPerformanceWarning(
3426 device, kVUID_BestPractices_ClearAttachment_ClearImage,
3427 "%s Performance warning: using vkCmdClearDepthStencilImage is not recommended. Prefer using LOAD_OP_CLEAR or "
3428 "vkCmdClearAttachments instead",
3429 VendorSpecificTag(kBPVendorAMD));
3430 }
3431
3432 return skip;
3433}
3434
3435bool BestPractices::PreCallValidateCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo* pCreateInfo,
3436 const VkAllocationCallbacks* pAllocator,
3437 VkPipelineLayout* pPipelineLayout) const {
3438 bool skip = false;
3439 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003440 uint32_t descriptor_size = enabled_features.core.robustBufferAccess ? 4 : 2;
Nadav Gevaf0808442021-05-21 13:51:25 -04003441 // Descriptor sets cost 1 DWORD each.
3442 // Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF.
3443 // Dynamic buffers cost 4 DWORDs each when robust buffer access is ON.
3444 // Push constants cost 1 DWORD per 4 bytes in the Push constant range.
3445 uint32_t pipeline_size = pCreateInfo->setLayoutCount; // in DWORDS
3446 for (uint32_t i = 0; i < pCreateInfo->setLayoutCount; i++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003447 auto descriptor_set_layout_state = Get<cvdescriptorset::DescriptorSetLayout>(pCreateInfo->pSetLayouts[i]);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003448 pipeline_size += descriptor_set_layout_state->GetDynamicDescriptorCount() * descriptor_size;
Nadav Gevaf0808442021-05-21 13:51:25 -04003449 }
3450
3451 for (uint32_t i = 0; i < pCreateInfo->pushConstantRangeCount; i++) {
3452 pipeline_size += pCreateInfo->pPushConstantRanges[i].size / 4;
3453 }
3454
3455 if (pipeline_size > kPipelineLayoutSizeWarningLimitAMD) {
3456 skip |= LogPerformanceWarning(device, kVUID_BestPractices_CreatePipelinesLayout_KeepLayoutSmall,
3457 "%s Performance warning: pipeline layout size is too large. Prefer smaller pipeline layouts."
3458 "Descriptor sets cost 1 DWORD each. "
3459 "Dynamic buffers cost 2 DWORDs each when robust buffer access is OFF. "
3460 "Dynamic buffers cost 4 DWORDs each when robust buffer access is ON. "
3461 "Push constants cost 1 DWORD per 4 bytes in the Push constant range. ",
3462 VendorSpecificTag(kBPVendorAMD));
3463 }
3464 }
3465
3466 return skip;
3467}
3468
3469bool BestPractices::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3470 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3471 const VkImageCopy* pRegions) const {
3472 bool skip = false;
3473 std::stringstream src_image_hex;
3474 std::stringstream dst_image_hex;
3475 src_image_hex << "0x" << std::hex << HandleToUint64(srcImage);
3476 dst_image_hex << "0x" << std::hex << HandleToUint64(dstImage);
3477
3478 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003479 auto src_state = Get<IMAGE_STATE>(srcImage);
3480 auto dst_state = Get<IMAGE_STATE>(dstImage);
Nadav Gevaf0808442021-05-21 13:51:25 -04003481
3482 if (src_state && dst_state) {
3483 VkImageTiling src_Tiling = src_state->createInfo.tiling;
3484 VkImageTiling dst_Tiling = dst_state->createInfo.tiling;
3485 if (src_Tiling != dst_Tiling && (src_Tiling == VK_IMAGE_TILING_LINEAR || dst_Tiling == VK_IMAGE_TILING_LINEAR)) {
3486 skip |=
3487 LogPerformanceWarning(device, kVUID_BestPractices_vkImage_AvoidImageToImageCopy,
3488 "%s Performance warning: image %s and image %s have differing tilings. Use buffer to "
3489 "image (vkCmdCopyImageToBuffer) "
3490 "and image to buffer (vkCmdCopyBufferToImage) copies instead of image to image "
3491 "copies when converting between linear and optimal images",
3492 VendorSpecificTag(kBPVendorAMD), src_image_hex.str().c_str(), dst_image_hex.str().c_str());
3493 }
3494 }
3495 }
3496
3497 return skip;
3498}
3499
3500bool BestPractices::PreCallValidateCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
3501 VkPipeline pipeline) const {
3502 bool skip = false;
3503
3504 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003505 if (IsPipelineUsedInFrame(pipeline)) {
Nadav Gevaf0808442021-05-21 13:51:25 -04003506 skip |= LogPerformanceWarning(device, kVUID_BestPractices_Pipeline_SortAndBind,
3507 "%s Performance warning: Pipeline %s was bound twice in the frame. Keep pipeline state changes to a minimum,"
3508 "for example, by sorting draw calls by pipeline.",
3509 VendorSpecificTag(kBPVendorAMD), report_data->FormatHandle(pipeline).c_str());
3510 }
3511 }
3512
3513 return skip;
3514}
3515
3516void BestPractices::ManualPostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits,
3517 VkFence fence, VkResult result) {
3518 // AMD best practice
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003519 num_queue_submissions_ += submitCount;
Nadav Gevaf0808442021-05-21 13:51:25 -04003520}
3521
3522bool BestPractices::PreCallValidateQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) const {
3523 bool skip = false;
3524
3525 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003526 auto num = num_queue_submissions_.load();
3527 if (num > kNumberOfSubmissionWarningLimitAMD) {
3528 skip |= LogPerformanceWarning(device, kVUID_BestPractices_Submission_ReduceNumberOfSubmissions,
3529 "%s Performance warning: command buffers submitted %" PRId32
3530 " times this frame. Submitting command buffers has a CPU "
3531 "and GPU overhead. Submit fewer times to incur less overhead.",
3532 VendorSpecificTag(kBPVendorAMD), num);
Nadav Gevaf0808442021-05-21 13:51:25 -04003533 }
3534 }
3535
3536 return skip;
3537}
3538
3539void BestPractices::PostCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3540 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3541 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
3542 uint32_t bufferMemoryBarrierCount,
3543 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
3544 uint32_t imageMemoryBarrierCount,
3545 const VkImageMemoryBarrier* pImageMemoryBarriers) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003546 num_barriers_objects_ += (memoryBarrierCount + imageMemoryBarrierCount + bufferMemoryBarrierCount);
Nadav Gevaf0808442021-05-21 13:51:25 -04003547}
3548
3549bool BestPractices::PreCallValidateCreateSemaphore(VkDevice device, const VkSemaphoreCreateInfo* pCreateInfo,
3550 const VkAllocationCallbacks* pAllocator, VkSemaphore* pSemaphore) const {
3551 bool skip = false;
3552 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003553 if (Count<SEMAPHORE_STATE>() > kMaxRecommendedSemaphoreObjectsSizeAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -04003554 skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfSemaphores,
3555 "%s Performance warning: High number of vkSemaphore objects created."
3556 "Minimize the amount of queue synchronization that is used. "
3557 "Semaphores and fences have overhead. Each fence has a CPU and GPU cost with it.",
3558 VendorSpecificTag(kBPVendorAMD));
3559 }
3560 }
3561
3562 return skip;
3563}
3564
3565bool BestPractices::PreCallValidateCreateFence(VkDevice device, const VkFenceCreateInfo* pCreateInfo,
3566 const VkAllocationCallbacks* pAllocator, VkFence* pFence) const {
3567 bool skip = false;
3568 if (VendorCheckEnabled(kBPVendorAMD)) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003569 if (Count<FENCE_STATE>() > kMaxRecommendedFenceObjectsSizeAMD) {
Nadav Gevaf0808442021-05-21 13:51:25 -04003570 skip |= LogPerformanceWarning(device, kVUID_BestPractices_SyncObjects_HighNumberOfFences,
3571 "%s Performance warning: High number of VkFence objects created."
3572 "Minimize the amount of CPU-GPU synchronization that is used. "
3573 "Semaphores and fences have overhead.Each fence has a CPU and GPU cost with it.",
3574 VendorSpecificTag(kBPVendorAMD));
3575 }
3576 }
3577
3578 return skip;
3579}
3580
Sam Walls8e77e4f2020-03-16 20:47:40 +00003581void BestPractices::PostTransformLRUCacheModel::resize(size_t size) { _entries.resize(size); }
3582
3583bool BestPractices::PostTransformLRUCacheModel::query_cache(uint32_t value) {
3584 // look for a cache hit
3585 auto hit = std::find_if(_entries.begin(), _entries.end(), [value](const CacheEntry& entry) { return entry.value == value; });
3586 if (hit != _entries.end()) {
3587 // mark the cache hit as being most recently used
3588 hit->age = iteration++;
3589 return true;
3590 }
3591
3592 // if there's no cache hit, we need to model the entry being inserted into the cache
3593 CacheEntry new_entry = {value, iteration};
3594 if (iteration < static_cast<uint32_t>(std::distance(_entries.begin(), _entries.end()))) {
3595 // if there is still space left in the cache, use the next available slot
3596 *(_entries.begin() + iteration) = new_entry;
3597 } else {
3598 // otherwise replace the least recently used cache entry
3599 auto lru = std::min_element(_entries.begin(), hit, [](const CacheEntry& a, const CacheEntry& b) { return a.age < b.age; });
3600 *lru = new_entry;
3601 }
3602 iteration++;
3603 return false;
3604}
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003605
3606bool BestPractices::PreCallValidateAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout,
3607 VkSemaphore semaphore, VkFence fence, uint32_t* pImageIndex) const {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003608 auto swapchain_data = Get<SWAPCHAIN_NODE>(swapchain);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003609 bool skip = false;
3610 if (swapchain_data && swapchain_data->images.size() == 0) {
3611 skip |= LogWarning(swapchain, kVUID_Core_DrawState_SwapchainImagesNotFound,
3612 "vkAcquireNextImageKHR: No images found to acquire from. Application probably did not call "
3613 "vkGetSwapchainImagesKHR after swapchain creation.");
3614 }
3615 return skip;
3616}
3617
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003618void BestPractices::CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(CALL_STATE& call_state, bool no_pointer) {
3619 if (no_pointer) {
3620 if (UNCALLED == call_state) {
3621 call_state = QUERY_COUNT;
3622 }
3623 } else { // Save queue family properties
3624 call_state = QUERY_DETAILS;
3625 }
3626}
3627
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003628void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
3629 uint32_t* pQueueFamilyPropertyCount,
3630 VkQueueFamilyProperties* pQueueFamilyProperties) {
3631 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount,
3632 pQueueFamilyProperties);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003633 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003634 if (bp_pd_state) {
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003635 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyPropertiesState,
3636 nullptr == pQueueFamilyProperties);
3637 }
3638}
3639
3640void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,
3641 uint32_t* pQueueFamilyPropertyCount,
3642 VkQueueFamilyProperties2* pQueueFamilyProperties) {
3643 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(physicalDevice, pQueueFamilyPropertyCount,
3644 pQueueFamilyProperties);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003645 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003646 if (bp_pd_state) {
3647 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2State,
3648 nullptr == pQueueFamilyProperties);
3649 }
3650}
3651
3652void BestPractices::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(VkPhysicalDevice physicalDevice,
3653 uint32_t* pQueueFamilyPropertyCount,
3654 VkQueueFamilyProperties2* pQueueFamilyProperties) {
3655 ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(physicalDevice, pQueueFamilyPropertyCount,
3656 pQueueFamilyProperties);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003657 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario56a96652020-12-30 13:23:42 -07003658 if (bp_pd_state) {
3659 CommonPostCallRecordGetPhysicalDeviceQueueFamilyProperties(bp_pd_state->vkGetPhysicalDeviceQueueFamilyProperties2KHRState,
3660 nullptr == pQueueFamilyProperties);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003661 }
3662}
3663
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003664void BestPractices::PostCallRecordGetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice, VkPhysicalDeviceFeatures* pFeatures) {
3665 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures(physicalDevice, pFeatures);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003666 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003667 if (bp_pd_state) {
3668 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3669 }
3670}
3671
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003672void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
3673 VkPhysicalDeviceFeatures2* pFeatures) {
3674 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003675 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003676 if (bp_pd_state) {
3677 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3678 }
3679}
3680
Nathaniel Cesariof121d122020-10-08 13:09:46 -06003681void BestPractices::PostCallRecordGetPhysicalDeviceFeatures2KHR(VkPhysicalDevice physicalDevice,
3682 VkPhysicalDeviceFeatures2* pFeatures) {
3683 ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003684 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003685 if (bp_pd_state) {
3686 bp_pd_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
3687 }
3688}
3689
3690void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice physicalDevice,
3691 VkSurfaceKHR surface,
3692 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities,
3693 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003694 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003695 if (bp_pd_state) {
3696 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3697 }
3698}
3699
3700void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2KHR(
3701 VkPhysicalDevice physicalDevice, const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
3702 VkSurfaceCapabilities2KHR* pSurfaceCapabilities, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003703 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003704 if (bp_pd_state) {
3705 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3706 }
3707}
3708
3709void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceCapabilities2EXT(VkPhysicalDevice physicalDevice,
3710 VkSurfaceKHR surface,
3711 VkSurfaceCapabilities2EXT* pSurfaceCapabilities,
3712 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003713 auto bp_pd_state = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003714 if (bp_pd_state) {
3715 bp_pd_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHRState = QUERY_DETAILS;
3716 }
3717}
3718
3719void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice physicalDevice,
3720 VkSurfaceKHR surface, uint32_t* pPresentModeCount,
3721 VkPresentModeKHR* pPresentModes, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003722 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003723 if (bp_pd_data) {
3724 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfacePresentModesKHRState;
3725
3726 if (*pPresentModeCount) {
3727 if (call_state < QUERY_COUNT) {
3728 call_state = QUERY_COUNT;
3729 }
3730 }
3731 if (pPresentModes) {
3732 if (call_state < QUERY_DETAILS) {
3733 call_state = QUERY_DETAILS;
3734 }
3735 }
3736 }
3737}
3738
3739void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
3740 uint32_t* pSurfaceFormatCount,
3741 VkSurfaceFormatKHR* pSurfaceFormats, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003742 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003743 if (bp_pd_data) {
3744 auto& call_state = bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState;
3745
3746 if (*pSurfaceFormatCount) {
3747 if (call_state < QUERY_COUNT) {
3748 call_state = QUERY_COUNT;
3749 }
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06003750 bp_pd_data->surface_formats_count = *pSurfaceFormatCount;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003751 }
3752 if (pSurfaceFormats) {
3753 if (call_state < QUERY_DETAILS) {
3754 call_state = QUERY_DETAILS;
3755 }
3756 }
3757 }
3758}
3759
3760void BestPractices::ManualPostCallRecordGetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice physicalDevice,
3761 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
3762 uint32_t* pSurfaceFormatCount,
3763 VkSurfaceFormat2KHR* pSurfaceFormats, VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003764 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003765 if (bp_pd_data) {
3766 if (*pSurfaceFormatCount) {
3767 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_COUNT) {
3768 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_COUNT;
3769 }
Jeremy Gebbenc7a834a2021-09-08 18:39:30 -06003770 bp_pd_data->surface_formats_count = *pSurfaceFormatCount;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003771 }
3772 if (pSurfaceFormats) {
3773 if (bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState < QUERY_DETAILS) {
3774 bp_pd_data->vkGetPhysicalDeviceSurfaceFormatsKHRState = QUERY_DETAILS;
3775 }
3776 }
3777 }
3778}
3779
3780void BestPractices::ManualPostCallRecordGetPhysicalDeviceDisplayPlanePropertiesKHR(VkPhysicalDevice physicalDevice,
3781 uint32_t* pPropertyCount,
3782 VkDisplayPlanePropertiesKHR* pProperties,
3783 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003784 auto bp_pd_data = Get<bp_state::PhysicalDevice>(physicalDevice);
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003785 if (bp_pd_data) {
3786 if (*pPropertyCount) {
3787 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_COUNT) {
3788 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_COUNT;
3789 }
3790 }
3791 if (pProperties) {
3792 if (bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState < QUERY_DETAILS) {
3793 bp_pd_data->vkGetPhysicalDeviceDisplayPlanePropertiesKHRState = QUERY_DETAILS;
3794 }
3795 }
3796 }
3797}
3798
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003799void BestPractices::ManualPostCallRecordGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain,
3800 uint32_t* pSwapchainImageCount, VkImage* pSwapchainImages,
3801 VkResult result) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003802 auto swapchain_state = Get<bp_state::Swapchain>(swapchain);
Nathaniel Cesario39152e62021-07-02 13:04:16 -06003803 if (swapchain_state && (pSwapchainImages || *pSwapchainImageCount)) {
3804 if (swapchain_state->vkGetSwapchainImagesKHRState < QUERY_DETAILS) {
3805 swapchain_state->vkGetSwapchainImagesKHRState = QUERY_DETAILS;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06003806 }
3807 }
3808}
3809
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003810void BestPractices::PreCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence) {
3811 ValidationStateTracker::PreCallRecordQueueSubmit(queue, submitCount, pSubmits, fence);
3812
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06003813 auto queue_state = Get<QUEUE_STATE>(queue);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003814 for (uint32_t submit = 0; submit < submitCount; submit++) {
Hans-Kristian Arntzen69ace7d2021-04-28 14:17:19 +02003815 const auto& submit_info = pSubmits[submit];
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003816 for (uint32_t cb_index = 0; cb_index < submit_info.commandBufferCount; cb_index++) {
Jeremy Gebben20da7a12022-02-25 14:07:46 -07003817 auto cb = GetWrite<bp_state::CommandBuffer>(submit_info.pCommandBuffers[cb_index]);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003818 for (auto &func : cb->queue_submit_functions) {
Jeremy Gebbene5361dd2021-11-18 14:23:56 -07003819 func(*this, *queue_state, *cb);
Hans-Kristian Arntzen66f4b522021-03-22 11:35:58 +01003820 }
3821 }
3822 }
3823}